Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 09:45:43 -06:00
commit 8b72f6a52b
33 files changed
+7510

No files matched your search

+138
View File
@@ -0,0 +1,138 @@
# 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::<u32>::blank());
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.span);
pdb::subsection(&mut subsections, pdb::debug_s_file_checksums, sums.bytes.span);
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::<strview>::blank());
# 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.span;
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.span, 1);
val held := info.span;
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.span == bytes.span);
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.span, 3);
val held := dbi.span;
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.span, 11);
t.expects(sections.span == made.sections.span);
# The first module's stream holds its symbols then its lines.
val first := stream_of(bytes.span, 12);
t.expects(word(first.span, 0, 4) == 4 && word(first.span, 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.span, 5);
val held := names.span;
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);
}
+51
View File
@@ -0,0 +1,51 @@
# Tests of the hashes a PDB file finds names and records by, against the values
# Microsoft's algorithms give, and of the digest naming a file.
import binary::pdb;
import std::testing;
@test def names_hash_as_microsofts_tools_hash_them(t: &mut tester): void {
# Words, then a half word, then a byte, cases made alike.
t.expects(pdb::hash_v1("") == 0x20240400);
t.expects(pdb::hash_v1("a") == 0x20240441);
t.expects(pdb::hash_v1("ab") == 0x20244649);
t.expects(pdb::hash_v1("abc") == 0x2024460A);
t.expects(pdb::hash_v1("abcd") == 0x646F8A62);
t.expects(pdb::hash_v1("abcde") == 0x646F8A27);
t.expects(pdb::hash_v1("/names") == 0x6D6CFC21);
t.expects(pdb::hash_v1("main") == 0x6E64C225);
t.expects(pdb::hash_v1("MAIN") == pdb::hash_v1("main"));
}
@test def records_hash_by_crc_begun_at_zero(t: &mut tester): void {
t.expects(pdb::hash_record("123456789") == 0x2DFD2D88);
t.expects(pdb::hash_record("") == 0);
}
@test def files_are_named_by_their_md5(t: &mut tester): void {
val digest := pdb::md5_of("abc");
val expected: u8[16] = [0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72];
var same := true;
for k in 0..16u {
same = same && digest[k] == expected[k];
}
t.expects(same);
val empty := pdb::md5_of("");
t.expects(empty[0] == 0xd4 && empty[15] == 0x7e); # d41d8cd98f00b204e9800998ecf8427e
}
@test def registers_are_numbered_as_codeview_numbers_them(t: &mut tester): void {
t.expects(pdb::amd64_register(0, 8) == 328); # rax
t.expects(pdb::amd64_register(3, 8) == 329); # rbx
t.expects(pdb::amd64_register(5, 8) == 334); # rbp
t.expects(pdb::amd64_register(6, 8) == 332); # rsi
t.expects(pdb::amd64_register(15, 8) == 343); # r15
t.expects(pdb::amd64_register(1, 4) == 18); # ecx
t.expects(pdb::amd64_register(9, 4) == 361); # r9d
t.expects(pdb::amd64_register(2, 2) == 11); # dx
t.expects(pdb::amd64_register(7, 1) == 325); # dil
t.expects(pdb::amd64_register(12, 1) == 348); # r12b
t.expects(pdb::amd64_xmm(0) == 154);
t.expects(pdb::amd64_xmm(7) == 161);
t.expects(pdb::amd64_xmm(8) == 252);
}
+54
View File
@@ -0,0 +1,54 @@
# Tests of a module's lines: the blocks of each file's lines with their
# columns, the subsections holding them, and the annotations of inline sites.
import std::io;
import binary::pdb;
import std::testing;
@test def lines_are_written_in_a_block_for_each_file(t: &mut tester): void {
var lines := pdb::function_lines::new(1, 0x10, 0x20);
lines.line(0, 0, 3, 5);
lines.line(0, 8, 4, 1);
lines.line(8, 12, 9, 2);
lines.finish();
var expected := string::new(nothing {});
expected.append("100000000100010020000000"); # Where the code is, with columns,
expected.append("000000000200000024000000000000000300008008000000040000800500000001000000"); # the first file's,
expected.append("0800000001000000180000000c0000000900008002000000"); # and the second's.
t.expects(hex_of(lines.bytes.span).span == expected.span, hex_of(lines.bytes.span).span);
var section := byte_writer::new();
pdb::subsection(&mut section, pdb::debug_s_lines, "abc");
t.expects(hex_of(section.span).span == "f20000000300000061626300");
}
@test def files_are_named_by_where_their_entries_are(t: &mut tester): void {
var sums := pdb::file_checksums::new();
t.expects(sums.file(1) == 0 && sums.file(9) == 8);
t.expects(hex_of(sums.bytes.span).span == "01000000000000000900000000000000");
var inlinees := pdb::inlinee_lines::new();
inlinees.add(0x1000, 8, 42);
t.expects(inlinees.count == 1 && hex_of(inlinees.bytes.span).span == "0000000000100000080000002a000000");
}
@test def annotations_say_each_part_of_a_site_and_its_line(t: &mut tester): void {
var notes := pdb::annotations::new(0, 10);
# The first part, of the line the function begins at, says it is.
notes.at_line(0x20, 0, 10);
t.expects(hex_of(notes.bytes.span).span == "06000320");
# A near part a near line on is one annotation.
notes.at_line(0x24, 0, 12);
# A part of the same line goes on.
notes.at_line(0x26, 0, 12);
# Code not the site's ends the part, then a part of another file follows.
notes.away(0x30);
notes.at_line(0x40, 8, 11);
notes.finish(0x48);
t.expects(hex_of(notes.bytes.span).span == "060003200b44040c0508060303100408");
# Lengths and offsets past seven bits take two bytes.
var far := pdb::annotations::new(0, 1);
far.at_line(0x1234, 0, 1);
far.finish(0x1240);
t.expects(hex_of(far.bytes.span).span == "0600039234040c");
}
+129
View File
@@ -0,0 +1,129 @@
# Tests of type records and symbols: numbers as numeric leaves, records padded
# to four bytes, records added twice held once but functions' IDs, field lists
# too long for one record going on in another, the header of a stream of types,
# and scopes naming where they end.
import std::io;
import binary::pdb;
import std::testing;
# The bytes a number is written as.
def unsigned_hex(value: u64): string {
var made := byte_writer::new();
pdb::put_unsigned(&mut made, value);
return hex_of(made.span);
}
def signed_hex(value: i64): string {
var made := byte_writer::new();
pdb::put_signed(&mut made, value);
return hex_of(made.span);
}
@test def numbers_are_written_as_numeric_leaves(t: &mut tester): void {
t.expects(unsigned_hex(0x7FFF).span == "ff7f");
t.expects(unsigned_hex(0x8000).span == "02800080");
t.expects(unsigned_hex(0x12345).span == "048045230100");
t.expects(unsigned_hex(0x123456789).span == "0a808967452301000000");
t.expects(signed_hex(5).span == "0500");
t.expects(signed_hex(-1).span == "0080ff");
t.expects(signed_hex(-200).span == "018038ff");
t.expects(signed_hex(-100000).span == "03806079feff");
}
@test def type_records_are_padded_and_held_once(t: &mut tester): void {
var table := pdb::type_table::new();
val pointer := table.pointer(pdb::t_int4, false);
t.expects(pointer == pdb::first_type);
t.expects(hex_of(table.records.span).span == "0a000210740000000c000100");
val again := table.pointer(pdb::t_int4, false);
t.expects(again == pointer && table.count == 1);
# A reference, and an array, whose name's zero is padded by `LF_PAD1`.
val reference := table.pointer(pdb::t_int4, true);
t.expects(reference == pdb::first_type + 1);
val array := table.array(pdb::t_int4, 16);
t.expects(array == pdb::first_type + 2);
val bytes := table.records.span;
t.expects(hex_of(span::<u8>::over(bytes.data + 24, 16)).span == "0e0003157400000023000000100000f1");
# Each function's ID is its own, though two are alike.
var ids := pdb::type_table::new();
val first := ids.function_id(0, 0x1001, "write_all");
val second := ids.function_id(0, 0x1001, "write_all");
t.expects(first != second && ids.count == 2);
}
@test def a_long_field_list_goes_on_in_a_record_before_it(t: &mut tester): void {
var table := pdb::type_table::new();
var list := pdb::field_list::new();
val name: strview = "a_member_whose_name_is_long_enough_to_fill_the_record";
for k in 0..1200u {
list.member(pdb::t_int8, 8 * k, name);
}
t.expects(list.count == 1200);
val fields := table.fields(&list);
t.expects(table.count == 2 && fields == pdb::first_type + 1);
# The second record, added last, is the list's first, and ends naming the
# one it goes on in.
val all := table.records.span;
val second := word(all, 0, 2) + 2;
val length := word(all, second, 2);
t.expects(length <= 0xFF00);
val end := second + 2 + length;
t.expects(word(all, end - 8, 2) == pdb::lf_index as u64 && word(all, end - 4, 4) == pdb::first_type as u64);
}
@test def a_stream_of_types_says_what_it_holds(t: &mut tester): void {
var table := pdb::type_table::new();
_ = table.pointer(pdb::t_void, false);
val named := table.structure("point", "sakura@1", 0, 0, 0, true);
val stream := table.stream(6);
val bytes := stream.span;
t.expects(word(bytes, 0, 4) == 20040203 && word(bytes, 4, 4) == 56);
t.expects(word(bytes, 8, 4) == 0x1000 && word(bytes, 12, 4) == 0x1002);
t.expects(word(bytes, 16, 4) == table.records.length && word(bytes, 20, 2) == 6);
t.expects(word(bytes, 28, 4) == 0x3FFFF);
t.expects(bytes.length == 56 + table.records.length);
# A declaration is hashed by its bytes, a definition by its name, so the one
# finds the other.
val complete := table.structure("point", "sakura@1", 0, 0, 16, false);
val hashes := table.hash_stream();
val held := hashes.span;
t.expects(named == 0x1001 && complete == 0x1002);
t.expects(word(held, 8, 4) == (pdb::hash_v1("point") % 0x3FFFF) as u64);
t.expects(word(held, 4, 4) != word(held, 8, 4));
# The first record's index and offset, which a reader begins from.
t.expects(word(held, 12, 4) == 0x1000 && word(held, 16, 4) == 0);
}
@test def scopes_name_where_they_begin_and_end(t: &mut tester): void {
var symbols := pdb::symbol_stream::new();
val proc := symbols.procedure(true, "main", 0x1001, 3, 0x40, 0x20, 4, 0x1F, pdb::proc_has_frame_pointer);
t.expects(proc == 4);
symbols.block(3, 0x48, 8);
val block := proc + word(symbols.bytes.span, proc, 2) + 2;
symbols.local(pdb::t_int8, 0, "x");
symbols.in_frame(-8, pdb::code_range { section = 3, offset = 0x48, length = 8 });
symbols.end();
val block_end := symbols.bytes.length - 4;
val notes: u8[6] = [6, 0, 3, 4, 4, 2];
symbols.inline_site(0x1000, notes[:]);
val site := block_end + 4;
symbols.end();
val site_end := symbols.bytes.length - 4;
symbols.end();
val proc_end := symbols.bytes.length - 4;
val all := symbols.bytes.span;
t.expects(word(all, 0, 4) == 4);
t.expects(word(all, proc + 2, 2) == pdb::s_gproc32 as u64 && word(all, proc + 4, 4) == 0 && word(all, proc + 8, 4) == proc_end);
t.expects(word(all, block + 2, 2) == pdb::s_block32 as u64 && word(all, block + 4, 4) == proc && word(all, block + 8, 4) == block_end);
t.expects(word(all, site + 4, 4) == proc && word(all, site + 8, 4) == site_end);
t.expects(word(all, block_end + 2, 2) == pdb::s_end as u64);
t.expects(word(all, site_end + 2, 2) == pdb::s_inlinesite_end as u64);
t.expects(word(all, proc_end + 2, 2) == pdb::s_end as u64);
# Every record is padded to four bytes.
t.expects(all.length % 4 == 0);
}
+88
View File
@@ -0,0 +1,88 @@
# What the tests share: reading words of bytes, and reading a stream back out
# of an MSF file, as a debugger does.
import std::io;
import binary::pdb;
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;
}
# Whether bytes hold text at a place.
def holds_at(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;
}
# The bytes as hex, as the tests write what they expect.
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;
}
# How many streams an MSF file holds.
def stream_count(file: span<u8>): u64 {
val directory := directory_of(file);
return word(directory.span, 0, 4);
}
# The directory of an MSF file, gathered from the blocks the block map names.
def directory_of(file: span<u8>): byte_writer {
val block := word(file, 32, 4);
val size := word(file, 44, 4);
val map := word(file, 52, 4);
var made := byte_writer::new();
var left := size;
var k: u64 = 0;
while left > 0 {
val at := word(file, map * block + 4 * k, 4) * block;
val take := left < block ? left : block;
for i in 0..take {
made.byte(file[at + i]);
}
left -= take;
k++;
}
return made;
}
# A stream of an MSF file, gathered from its blocks.
def stream_of(file: span<u8>, number: u64): byte_writer {
val block := word(file, 32, 4);
val directory := directory_of(file);
val names := directory.span;
val count := word(names, 0, 4);
var at := 4 + 4 * count;
for s in 0..number {
val size := word(names, 4 + 4 * s, 4);
at += 4 * ((size + block - 1) / block);
}
val size := word(names, 4 + 4 * number, 4);
var made := byte_writer::new();
var left := size;
var k: u64 = 0;
while left > 0 {
val from := word(names, at + 4 * k, 4) * block;
val take := left < block ? left : block;
for i in 0..take {
made.byte(file[from + i]);
}
left -= take;
k++;
}
return made;
}