# 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, 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): 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, 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::::empty(), 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.view(); 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.view()).view() == "01050515053401000403041201500000", hex_of(bytes.view()).view()); # 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().view()).view() == "01090500091100001000040111000000", hex_of(big.bytes().view()).view()); } @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.view()).view() == "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.view()); var image := pe::image::new(0x400000, 0x401000); image.section(".text", 0x401000, code[:], 1, pe::readable | pe::executable); image.section(".buildid", 0x402000, named.view(), 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.view(), header_of(bytes.view(), 1) + 20, 4); val raw := word(bytes.view(), entry + 24, 4); t.expects(word(bytes.view(), entry + 12, 4) == 2 && word(bytes.view(), entry + 20, 4) == 0x201C); t.expects(bytes.view()[raw] == 'R' as u8 && bytes.view()[raw + 3] == 'S' as u8); t.expects(word(bytes.view(), raw + 20, 4) == 1 && bytes.view()[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.view(), entry + 4, 4) == 0x12345678); var stamped := true; for k in 0..16u { stamped = stamped && bytes.view()[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.view()); t.expects(headers.length == 80 && word(headers.view(), 40 + 12, 4) == 0x2000); }