# Tests of a whole PDB file, read back as a debugger reads one: the MSF file's # description and map of free blocks, the stream naming the file and its map # of named streams, the stream of modules and its numbers of the others, the # table of names, and the GUID made from what the file holds. import std::io; import binary::pdb; import std::testing; # A small file: a type, a module with a procedure and its lines, the linker's, # a public, the global naming the procedure, and two sections. def small_file(public_name: strview): pdb::writer { var made := pdb::writer::new(); val arguments := made.types.arguments(span::::empty()); val procedure := made.types.procedure(pdb::t_void, arguments, 0); var symbols := pdb::symbol_stream::new(); symbols.object_name("main.sakura"); val version: u16[3] = [1, 0, 0]; symbols.compiler(pdb::language_cxx, pdb::cv_machine_x64, version, "sakura"); val at := symbols.procedure(true, "main", procedure, 2, 0, 16, 4, 15, pdb::proc_has_frame_pointer); symbols.end(); var sums := pdb::file_checksums::new(); val file := sums.file(made.names.offset_of("/src/main.sakura")); var lines := pdb::function_lines::new(2, 0, 16); lines.line(file, 0, 1, 1); lines.finish(); var subsections := byte_writer::new(); pdb::subsection(&mut subsections, pdb::debug_s_lines, lines.bytes.view()); pdb::subsection(&mut subsections, pdb::debug_s_file_checksums, sums.bytes.view()); val files: strview[1] = ["/src/main.sakura"]; val ours := made.add_module("main.sakura", "main.sakura", &mut symbols.bytes, &mut subsections, files[:]); var linker := pdb::symbol_stream::new(); linker.object_name("* Linker *"); var none := byte_writer::new(); _ = made.add_module("* Linker *", "", &mut linker.bytes, &mut none, span::::empty()); # Two sections' headers: data, then code. val names: strview[2] = [".data", ".text"]; val characteristics: u32[2] = [0xC0000040, 0x60000020]; for k in 0..2u { for i in 0..8u { made.sections.byte(i < names[k].length ? names[k][i] : 0); } made.sections.u32(16); # Its virtual size, made.sections.u32(0x1000 * (k + 1)); # address, made.sections.u32(0x1000); # bytes in the file, made.sections.u32(0x1000 * (k + 1)); # and where. made.sections.zeros(12); made.sections.u32(characteristics[k]); } made.contribute(ours, 2, 0, 16, 0x60000020); made.public(public_name, 2, 0, pdb::public_function | pdb::public_code); made.procedure("main", ours, at, true); return made; } @test def the_file_is_an_msf_file_of_blocks(t: &mut tester): void { var made := small_file("main"); val bytes := made.file(); val all := bytes.view(); val magic: strview = "Microsoft C/C++ MSF 7.00\r\n"; t.expects(holds_at(all, 0, magic)); t.expects(all[26] == 0x1A && all[27] == 'D' as u8 && all[28] == 'S' as u8); t.expects(word(all, 32, 4) == 4096 && word(all, 36, 4) == 1); t.expects(word(all, 40, 4) * 4096 == all.length); t.expects(stream_count(all) == 14); # The map of free blocks says each block the file holds is taken, and those # past them free. val blocks := word(all, 40, 4); for b in 0..blocks { t.expects((all[4096 + b / 8] & (1u << (b % 8)) as u8) == 0); } t.expects((all[4096 + blocks / 8] & (1u << (blocks % 8)) as u8) != 0); } @test def the_file_is_named_by_its_guid_and_age(t: &mut tester): void { var made := small_file("main"); val bytes := made.file(); val info := stream_of(bytes.view(), 1); val held := info.view(); t.expects(word(held, 0, 4) == 20000404); t.expects(word(held, 4, 4) == made.signature as u64 && word(held, 8, 4) == 1); var named := false; for k in 0..16u { t.expects(held[12 + k] == made.guid[k]); named = named || made.guid[k] != 0; } t.expects(named); # The map of named streams names `/names`, in its hash's bucket of eight; the # table follows the name's seven bytes. t.expects(word(held, 28, 4) == 7 && holds_at(held, 32, "/names")); t.expects(word(held, 39, 4) == 1 && word(held, 43, 4) == 8 && word(held, 47, 4) == 1); t.expects(word(held, 51, 4) == (1u << ((pdb::hash_v1("/names") & 0xFFFF) % 8)) as u64); t.expects(word(held, 55, 4) == 0 && word(held, 59, 4) == 0 && word(held, 63, 4) == 5); t.expects(word(held, 67, 4) == 20140508 && held.length == 71); # The same build names its file the same way, and another another. var again := small_file("main"); val same := again.file(); t.expects(same.view() == bytes.view()); var other := small_file("start"); _ = other.file(); t.expects(other.signature != made.signature); } @test def the_stream_of_modules_numbers_the_others(t: &mut tester): void { var made := small_file("main"); val bytes := made.file(); val dbi := stream_of(bytes.view(), 3); val held := dbi.view(); t.expects(word(held, 0, 4) == 0xFFFFFFFF && word(held, 4, 4) == 19990903 && word(held, 8, 4) == 1); t.expects(word(held, 12, 2) == 8 && word(held, 16, 2) == 9 && word(held, 20, 2) == 10); t.expects(word(held, 58, 2) == 0x8664); # The headers of the executable's sections are the stream the debugging # headers name. val end := held.length; t.expects(word(held, end - 22 + 10, 2) == 11); val sections := stream_of(bytes.view(), 11); t.expects(sections.view() == made.sections.view()); # The first module's stream holds its symbols then its lines. val first := stream_of(bytes.view(), 12); t.expects(word(first.view(), 0, 4) == 4 && word(first.view(), 6, 2) == pdb::s_objname as u64); } @test def names_are_found_by_their_hash(t: &mut tester): void { var made := small_file("main"); val bytes := made.file(); val names := stream_of(bytes.view(), 5); val held := names.view(); t.expects(word(held, 0, 4) == 0xEFFEEFFE && word(held, 4, 4) == 1); val size := word(held, 8, 4); val buckets := word(held, 12 + size, 4); val wanted: strview = "/src/main.sakura"; var slot := pdb::hash_v1(wanted) as u64 % buckets; val offset := word(held, 16 + size + 4 * slot, 4); t.expects(offset != 0 && holds_at(held, 12 + offset, wanted)); t.expects(word(held, 16 + size + 4 * buckets, 4) == 1); }