Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-13 21:37:35 -06:00
commit 308cc7507d
35 files changed
+8351

No files matched your search

+162
View File
@@ -0,0 +1,162 @@
# Tests of PE files: the headers and sections of an executable, a long name in
# the table of strings, the data directories, how functions unwind, and the
# debug directory naming a PDB file, stamped with its GUID once it is written.
import std::io;
import binary::pe;
import std::testing;
# A word of bytes, least significant byte first.
def word(bytes: span<u8>, at: u64, size: u64): u64 {
var made: u64 = 0;
for k in 0..size {
made = made | (bytes[at + k] as u64 << (8 * k));
}
return made;
}
# The bytes as hex.
def hex_of(bytes: span<u8>): string {
val digits: strview = "0123456789abcdef";
var made := string::new(nothing {});
for &each in bytes {
made.append(digits.substr((each >> 4) as u64, 1));
made.append(digits.substr((each & 15) as u64, 1));
}
return made;
}
# Where a section's header is, by its number from 0.
def header_of(file: span<u8>, k: u64): u64 {
val pe := word(file, 0x3C, 4);
return pe + 24 + word(file, pe + 20, 2) + 40 * k;
}
@test def an_executable_holds_its_sections_where_they_load(t: &mut tester): void {
val code: u8[4] = [0x31, 0xC0, 0xC3, 0x90];
val data: u8[3] = [1, 2, 3];
var image := pe::image::new(0x400000, 0x402000);
image.stack_reserve = 0x100000;
image.stack_commit = 0x1000;
image.section(".data", 0x401000, data[:], 3, pe::readable | pe::writable);
image.section(".text", 0x402000, code[:], 4, pe::readable | pe::executable);
image.section(".empty", 0x403000, span::<u8>::blank(), 0, pe::readable);
image.section(".debug_info", 0x403000, data[:], 3, pe::readable | pe::discardable);
image.symbols.append(pe::symbol { name = "start", address = 0x402000 });
image.imports = pe::directory { address = 0x401000, size = 3 };
val bytes := image.file();
val all := bytes.span;
t.expects(all[0] == 'M' as u8 && all[1] == 'Z' as u8);
val pe := word(all, 0x3C, 4);
t.expects(word(all, pe, 4) == 0x4550 && word(all, pe + 4, 2) == 0x8664);
# The empty section is left out.
t.expects(word(all, pe + 6, 2) == 3);
val o := pe + 24;
t.expects(word(all, o, 2) == 0x20B && word(all, o + 16, 4) == 0x2000 && word(all, o + 24, 8) == 0x400000);
t.expects(word(all, o + 68, 2) == 3 && word(all, o + 56, 4) == 0x4000);
t.expects(word(all, o + 112 + 8, 4) == 0x1000 && word(all, o + 116 + 8, 4) == 3);
# Each section is where its address says, page for page, with its permissions.
val text := header_of(all, 1);
t.expects(word(all, text + 12, 4) == 0x2000 && word(all, text + 8, 4) == 4);
t.expects(word(all, text + 36, 4) == 0x60000020);
val at := word(all, text + 20, 4);
t.expects(all[at] == 0x31 && all[at + 2] == 0xC3);
t.expects(word(all, header_of(all, 0) + 36, 4) == 0xC0000040);
t.expects(word(all, header_of(all, 2) + 36, 4) == 0x42000040);
# A name of more than eight bytes is where `/` and its offset say, in the
# table of strings after the symbols.
val named := header_of(all, 2);
t.expects(all[named] == '/' as u8);
val symbols := word(all, pe + 12, 4);
t.expects(word(all, pe + 16, 4) == 1);
val strings := symbols + 18 * 1;
var offset: u64 = 0;
var k: u64 = 1;
while all[named + k] >= '0' as u8 && all[named + k] <= '9' as u8 {
offset = offset * 10 + (all[named + k] - '0' as u8) as u64;
k++;
}
val name: strview = ".debug_info";
var same := true;
for i in 0..name.length {
same = same && all[strings + offset + i] == name[i];
}
t.expects(same);
# The symbol names its function within the code, by its offset there.
t.expects(word(all, symbols + 8, 4) == 0 && word(all, symbols + 12, 2) == 2);
}
@test def a_prologue_is_described_latest_first(t: &mut tester): void {
# push rbp; mov rbp, rsp; push rbx: the frame found from `rbp`, sixteen past
# its base, `rbx` saved at its base plus eight.
var info := pe::unwind_info::new();
info.pushed(1, 5);
info.allocated(4, 16);
info.framed(4, 5, 16);
info.saved(5, 3, 8);
val bytes := info.bytes();
t.expects(hex_of(bytes.span).span == "01050515053401000403041201500000", hex_of(bytes.span).span);
# Allocations of each size take the slots their sizes need.
var big := pe::unwind_info::new();
big.allocated(4, 136);
big.allocated(9, 0x100000);
t.expects(hex_of(big.bytes().span).span == "01090500091100001000040111000000", hex_of(big.bytes().span).span);
}
@test def records_are_shared_and_the_table_sorted(t: &mut tester): void {
var records := pe::unwind_table::new();
var info := pe::unwind_info::new();
info.allocated(4, 40);
val first := records.add(&info);
val again := records.add(&info);
var other := pe::unwind_info::new();
other.pushed(1, 5);
val second := records.add(&other);
t.expects(first == 0 && again == 0 && second == 8);
var functions := pe::function_table::new();
functions.add(0x2040, 0x2080, second);
functions.add(0x2000, 0x2040, first);
val table := functions.bytes();
t.expects(hex_of(table.span).span == "002000004020000000000000402000008020000008000000");
}
@test def the_debug_directory_names_a_pdb_file_stamped_once_written(t: &mut tester): void {
val code: u8[1] = [0xC3];
var named := byte_writer::new();
val record := pe::codeview_record(1, "app.pdb");
pe::debug_entry(&mut named, pe::debug_codeview, 0x2000 + pe::debug_entry_bytes as u32, record.length as u32);
named.put(record.span);
var image := pe::image::new(0x400000, 0x401000);
image.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable);
image.section(".buildid", 0x402000, named.span, named.length, pe::readable);
image.debug = pe::directory { address = 0x402000, size = pe::debug_entry_bytes };
var bytes := image.file();
# The entry says where its record is in the file.
val entry := word(bytes.span, header_of(bytes.span, 1) + 20, 4);
val raw := word(bytes.span, entry + 24, 4);
t.expects(word(bytes.span, entry + 12, 4) == 2 && word(bytes.span, entry + 20, 4) == 0x201C);
t.expects(bytes.span[raw] == 'R' as u8 && bytes.span[raw + 3] == 'S' as u8);
t.expects(word(bytes.span, raw + 20, 4) == 1 && bytes.span[raw + 24] == 'a' as u8);
var guid: u8[16];
for k in 0..16u {
guid[k] = (k * 17) as u8;
}
t.expects(pe::stamp_codeview(&mut bytes, guid, 0x12345678));
t.expects(word(bytes.span, entry + 4, 4) == 0x12345678);
var stamped := true;
for k in 0..16u {
stamped = stamped && bytes.span[raw + 4 + k] == guid[k];
}
t.expects(stamped);
# The headers of the sections, as a PDB file keeps them.
val headers := pe::section_headers(bytes.span);
t.expects(headers.length == 80 && word(headers.span, 40 + 12, 4) == 0x2000);
}
+190
View File
@@ -0,0 +1,190 @@
# Tests of an executable's resources: icons of PNG files and icon files, the
# tree `.rsrc` holds them in, found again by their types and numbers, what the
# file's properties say of the program, and its text as Windows holds it.
import std::io;
import binary::pe;
import std::testing;
# The header of a PNG of a size, which is all an icon's group reads of it.
def png_of(width: u32, height: u32): byte_writer {
var made := byte_writer::new();
val signature: u8[8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
made.put(signature[:]);
made.byte(0); made.byte(0); made.byte(0); made.byte(13);
made.put("IHDR");
for each in [width, height] {
made.byte((each >> 24) as u8);
made.byte((each >> 16) as u8);
made.byte((each >> 8) as u8);
made.byte(each as u8);
}
made.put("pixels");
return made;
}
# An icon file of two images, 16 and 256 pixels wide, each its bytes as text.
def ico_file(): byte_writer {
var made := byte_writer::new();
made.u16(0);
made.u16(1);
made.u16(2);
# 16x16, 32 bits, "small" at 38; 256x256, as 0, "large!" at 43.
made.byte(16); made.byte(16); made.byte(0); made.byte(0); made.u16(1); made.u16(32); made.u32(5); made.u32(38);
made.byte(0); made.byte(0); made.byte(0); made.byte(0); made.u16(1); made.u16(32); made.u32(6); made.u32(43);
made.put("small");
made.put("large!");
return made;
}
# Whether bytes of a span are text.
def holds_text(bytes: span<u8>, at: u64, text: strview): bool {
return false if at + text.length > bytes.length;
for k in 0..text.length {
return false if bytes[at + k] != text[k];
}
return true;
}
# Whether bytes hold text as UTF-16, the least significant byte first.
def holds_utf16(bytes: span<u8>, text: strview): bool {
var wide := byte_writer::new();
pe::put_utf16(&mut wide, text);
val want := wide.span;
val length := want.length - 2;
return false if bytes.length < length;
for at in 0..=(bytes.length - length) {
var same := true;
for k in 0..length {
same = same && bytes[at + k] == want[k];
}
return true if same;
}
return false;
}
@test def an_icons_images_are_pngs_and_icon_files_but_not_larger_than_256(t: &mut tester): void {
var icon := pe::icon_group::new();
val small := png_of(32, 32);
t.expects(icon.add(small.span) == pe::icon_read::added);
val large := png_of(256, 256);
t.expects(icon.add(large.span) == pe::icon_read::added);
val huge := png_of(512, 512);
t.expects(icon.add(huge.span) == pe::icon_read::too_large);
val file := ico_file();
t.expects(icon.add(file.span) == pe::icon_read::added);
t.expects(icon.length == 4);
t.expects(icon.add("GIF89a and the rest") == pe::icon_read::unknown);
# An icon file whose image reaches past its end.
var cut := ico_file();
cut.rewind(cut.length - 3);
t.expects(icon.add(cut.span) == pe::icon_read::broken);
t.expects(icon.length == 4);
val size := pe::png_size(huge.span);
t.expects(size[0] == 512 && size[1] == 512);
t.expects(pe::png_size("not one")[0] == 0);
}
@test def resources_are_found_by_their_types_and_numbers(t: &mut tester): void {
var icon := pe::icon_group::new();
val small := png_of(16, 16);
val large := png_of(256, 256);
_ = icon.add(small.span);
_ = icon.add(large.span);
var made := pe::resources::new();
made.add_icon(&icon, 1, pe::language_neutral);
val version := pe::version_resource("Hearth", "1.2.3", "hearth.exe");
made.add(pe::resource_version, 1, pe::language_english, version.span);
t.expects(made.length == 4 && !made.empty);
t.expects(made.next_id(pe::resource_icon) == 3 && made.next_id(pe::resource_manifest) == 1);
# The section, in an executable meant for windows of its own.
val code: u8[1] = [0xC3];
val section := made.bytes(0x2000);
var image := pe::image::new(0x400000, 0x401000);
image.subsystem = pe::subsystem_windows;
image.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable);
image.section(".rsrc", 0x402000, section.span, section.length, pe::readable);
image.resources = pe::directory { address = 0x402000, size = section.length };
val bytes := image.file();
val all := bytes.span;
val pe_at := word(all, 0x3C, 4);
t.expects(word(all, pe_at + 24 + 68, 2) == 2);
t.expects(word(all, pe_at + 24 + 112 + 16, 4) == 0x2000 && word(all, pe_at + 24 + 112 + 20, 4) == section.length);
t.expects(word(all, header_of(all, 1) + 36, 4) == 0x40000040);
# The group: two images, the second 256 pixels each way, written as 0, by
# the numbers of the icons holding them.
val group := pe::find_resource(all, pe::resource_icon_group, 1) else {
t.expects(false, "no icon group");
return;
};
t.expects(group.length == 6 + 14 * 2 && word(group, 2, 2) == 1 && word(group, 4, 2) == 2);
t.expects(group[6] == 16 && group[7] == 16 && word(group, 6 + 6, 2) == 32 && word(group, 6 + 8, 4) == small.length);
t.expects(group[20] == 0 && group[21] == 0 && word(group, 20 + 12, 2) == 2);
val second := pe::find_resource(all, pe::resource_icon, word(group, 20 + 12, 2) as u32) else {
t.expects(false, "no icon of the group's");
return;
};
t.expects(second.length == large.length && holds_text(second, 12, "IHDR") && second[18] == 1);
val found := pe::find_resource(all, pe::resource_version, 1) else {
t.expects(false, "no version");
return;
};
t.expects(found.length == version.length && holds_utf16(found, "Hearth"));
t.expects(pe::find_resource(all, pe::resource_icon_group, 2) is none);
t.expects(pe::find_resource(all, pe::resource_manifest, 1) is none);
# An executable without them has none to find.
var plain := pe::image::new(0x400000, 0x401000);
plain.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable);
val none := plain.file();
t.expects(pe::find_resource(none.span, pe::resource_icon_group, 1) is none);
t.expects(word(none.span, word(none.span, 0x3C, 4) + 24 + 68, 2) == 3);
}
@test def a_resource_added_again_takes_the_place_of_the_first(t: &mut tester): void {
var made := pe::resources::new();
made.add(pe::resource_manifest, 1, 0, "first");
made.add(pe::resource_manifest, 1, 0, "second");
made.add(pe::resource_manifest, 1, pe::language_english, "english");
t.expects(made.length == 2);
val section := made.bytes(0x1000);
# One type, one number, two languages, in order, the neutral one first.
t.expects(word(section.span, 14, 2) == 1 && word(section.span, 16, 4) == 24);
t.expects((word(section.span, 20, 4) & 0x7FFFFFFF) == 24);
val languages := word(section.span, 24 + 20, 4) & 0x7FFFFFFF;
t.expects(word(section.span, languages + 14, 2) == 2);
t.expects(word(section.span, languages + 16, 4) == 0 && word(section.span, languages + 24, 4) == 0x409);
val entry := word(section.span, languages + 20, 4);
t.expects(word(section.span, entry + 4, 4) == 6);
val data := word(section.span, entry, 4) - 0x1000;
t.expects(holds_text(section.span, data, "second"));
}
@test def the_version_says_the_program_its_numbers_and_its_file(t: &mut tester): void {
val made := pe::version_resource("Hearth", "1.2.3-alpha", "hearth.edit.exe");
val bytes := made.span;
t.expects(word(bytes, 0, 2) == made.length && word(bytes, 2, 2) == 52 && word(bytes, 4, 2) == 0);
t.expects(holds_utf16(bytes, "VS_VERSION_INFO"));
# The fixed part, after the key and its zero, at four bytes.
t.expects(word(bytes, 40, 4) == 0xFEEF04BD);
t.expects(word(bytes, 48, 4) == 0x00010002 && word(bytes, 52, 4) == 0x00030000);
t.expects(word(bytes, 68, 4) == 2 && word(bytes, 76, 4) == 1); # A prerelease, and a program.
for text in ["StringFileInfo", "040904B0", "FileDescription", "Hearth", "1.2.3-alpha", "hearth.edit", "hearth.edit.exe", "VarFileInfo", "Translation"] {
t.expects(holds_utf16(bytes, text), text);
}
# With no title, it's named by its file; with no version, it says none.
val plain := pe::version_resource("", "", "tool.exe");
t.expects(word(plain.span, 48, 4) == 0 && !holds_utf16(plain.span, "FileVersion") && holds_utf16(plain.span, "ProductName"));
t.expects(word(plain.span, 68, 4) == 0);
}
@test def text_is_written_as_utf16(t: &mut tester): void {
var made := byte_writer::new();
pe::put_utf16(&mut made, "A\xC3\xA9\xE2\x82\xAC\xF0\x9F\x98\x80");
t.expects(hex_of(made.span).span == "4100e900ac203dd800de0000", hex_of(made.span).span);
}