Files
binary/tests/pdb/records.sakura
T
2026-11-10 09:45:43 -06:00

130 lines
5.5 KiB
Plaintext

# 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);
}