From aaf18ca03ba6e4355332220d9130bfedb7b4f6b2 Mon Sep 17 00:00:00 2001 From: r0ny123 <49360849+r0ny123@users.noreply.github.com> Date: Tue, 15 Sep 2026 13:10:15 +0530 Subject: [PATCH] strip a C7 or C11 module-stream signature, and read nothing after one unknown module_symbol_bytes stripped the 4-byte CodeView signature only when it was C13. A C7 (0) or C11 (1) signature stayed on the front of the records, where iter_records read it as a record header and walked every record after it at the wrong alignment, silently. All three signatures are now stripped. A first word that is none of them yields no symbols, since the layout after it is not known, and Diagnostics.unrecognised_signatures counts such modules by the value found, which a diagnose() warning names. Because a returned body now always had exactly four bytes stripped, the fixed + CV_SIGNATURE_SIZE applied to record offsets matches the strip on every path that yields records. Fixes #64 --- CHANGELOG.md | 11 +++++++++++ purepdb/pdb.py | 42 ++++++++++++++++++++++++++++++++++++------ tests/_synth.py | 4 ++-- tests/test_diagnose.py | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 79 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b82d0fd..c32a550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,17 @@ resolve *differently* would be breaking, and would say so here. - A pull request that changes `purepdb/` or `pyproject.toml` has to add a `CHANGELOG.md` entry or carry the `no-changelog` label; CI checks it. +### Fixed + +- A module stream signed C7 (`0`) or C11 (`1`) has its signature stripped like + a C13 one. Only C13 was recognised, so the other two left the signature word + on the front of the records, where it was read as a record header and every + record after it was walked at the wrong alignment -- silently, with no error + and no warning. A first word that is none of the three is no longer parsed + as records at all: the module contributes no symbols, and + `Diagnostics.unrecognised_signatures` counts it by the value found, which + the new `diagnose()` warning names. + ## [0.5.0] - 2026-08-28 ### Added diff --git a/purepdb/pdb.py b/purepdb/pdb.py index 923a797..a2ca1a1 100644 --- a/purepdb/pdb.py +++ b/purepdb/pdb.py @@ -40,7 +40,10 @@ STREAM_IPI = 4 # CodeView signature that prefixes each module symbol substream, and its size. +CV_SIGNATURE_C7 = 0 +CV_SIGNATURE_C11 = 1 CV_SIGNATURE_C13 = 4 +CV_SIGNATURES = frozenset({CV_SIGNATURE_C7, CV_SIGNATURE_C11, CV_SIGNATURE_C13}) CV_SIGNATURE_SIZE = 4 # The fixed header of the PDB Info stream: version, signature, age, GUID. It @@ -293,6 +296,11 @@ class Diagnostics: files teaches a reader to skip the list, which costs the entries that do matter. The count and the note beside it in `purepdb diagnose` are the explanation, in the report rather than in the alarm channel.""" + unrecognised_signatures: dict[int, int] = field(default_factory=dict) + """Module streams whose first word is not a CodeView signature (C7, C11 or + C13), counted by that word. Their records are not read: the layout after + an unknown signature is not known, and parsing the word as a record header + would misalign every record after it.""" @property def truncated_streams(self) -> int: @@ -362,6 +370,14 @@ def warnings(self) -> list[str]: f"records. This is what /DEBUG:FASTLINK and some pre-2010 " f"toolchains produce" ) + if self.unrecognised_signatures: + found = ", ".join(f"{sig:#x}x{n}" for sig, n in + sorted(self.unrecognised_signatures.items())) + out.append( + f"{sum(self.unrecognised_signatures.values())} module " + f"stream(s) begin with a word that is not a CodeView signature " + f"(found: {found}); their symbols were not read" + ) if self.public_records == 0: out.append( "no public records in the symbol-record stream; thunks and " @@ -696,15 +712,25 @@ def module_symbol_bytes(self, mod) -> bytes: info`, and only the first region holds symbol records. `sym_byte_size` bounds it *including* the 4-byte signature, so parsing past it walks line-info bytes as if they were records. Returns b"" when the module - has no symbols. + has no symbols, or when its first word is not a signature purepdb + recognises -- `Diagnostics.unrecognised_signatures` counts those. """ + return self._module_symbols(mod)[1] + + def _module_symbols(self, mod) -> tuple[int | None, bytes]: + """`module_symbol_bytes` and the unrecognised signature, if any.""" if not mod.has_symbols or not self.msf.is_valid_stream(mod.sym_stream): - return b"" + return None, b"" raw = self.msf.read_stream(mod.sym_stream) end = min(mod.sym_byte_size, len(raw)) - if len(raw) >= 4 and struct.unpack_from(" bytes: """The C13 line-info region of one module's stream. @@ -1115,9 +1141,12 @@ def diagnose(self) -> Diagnostics: malformed = 0 malformed_inline = 0 line_bytes = 0 + signatures: dict[int, int] = {} for mod in self.dbi.modules: line_bytes += len(self.module_c13_bytes(mod)) - body = self.module_symbol_bytes(mod) + signature, body = self._module_symbols(mod) + if signature is not None: + signatures[signature] = signatures.get(signature, 0) + 1 if not body: continue with_symbols += 1 @@ -1199,6 +1228,7 @@ def diagnose(self) -> Diagnostics: pdb_info_error=pdb_info_error, module_list_stopped_at=self.dbi.module_list_stopped_at, thread_local_records=thread_locals, + unrecognised_signatures=signatures, ) def _is_code(self, segment: int) -> bool: diff --git a/tests/_synth.py b/tests/_synth.py index 8fb3705..57b625d 100644 --- a/tests/_synth.py +++ b/tests/_synth.py @@ -326,9 +326,9 @@ def section_header(name: str, vaddr: int, vsize: int = 0x1000, ) -def module_sym_stream(records: bytes) -> bytes: +def module_sym_stream(records: bytes, signature: int = 4) -> bytes: """A module symbol substream: 4-byte CV signature + records.""" - return struct.pack(" bytes: diff --git a/tests/test_diagnose.py b/tests/test_diagnose.py index b816563..fce46ee 100644 --- a/tests/test_diagnose.py +++ b/tests/test_diagnose.py @@ -6,6 +6,8 @@ import struct +import pytest + from purepdb import PDB from tests._synth import ( build_msf, @@ -24,9 +26,10 @@ UNKNOWN_KIND = 0x1167 -def _pdb(*, module_records=b"", pub_records=(), section_stream=6): +def _pdb(*, module_records=b"", pub_records=(), section_stream=6, + signature=4): symrecords = b"".join(pub_records) - module_syms = module_sym_stream(module_records) + module_syms = module_sym_stream(module_records, signature) mods = module_info("main.obj", "main.obj", sym_stream=5, sym_byte_size=len(module_syms)) streams = [ @@ -118,3 +121,28 @@ def test_line_info_after_the_symbols_is_not_parsed_as_records(): pdb = PDB.from_bytes(build_msf(streams)) names = [p.name for p in pdb.module_procs()] assert names == ["real"], "must stop at sym_byte_size" + + +@pytest.mark.parametrize("signature", [0, 1, 4], ids=["C7", "C11", "C13"]) +def test_every_codeview_signature_is_stripped(signature): + pdb = _pdb(module_records=gproc32("main", 1, 0x10), + pub_records=[pub32("main", 1, 0x10)], signature=signature) + assert [p.name for p in pdb.module_procs()] == ["main"] + d = pdb.diagnose() + assert d.unrecognised_signatures == {} + assert d.warnings == [] + + +def test_an_unrecognised_signature_is_not_parsed_as_a_record(): + # 0x1110_0010 read as a header is a 16-byte S_GPROC32, so a reader that + # walked past the unknown word would find a record in it. + pdb = _pdb(module_records=gproc32("main", 1, 0x10), + pub_records=[pub32("main", 1, 0x10)], signature=0x1110_0010) + assert pdb.module_procs() == [] + d = pdb.diagnose() + assert d.modules_with_symbols == 0 + assert d.malformed_records == 0 + assert d.unrecognised_signatures == {0x1110_0010: 1} + warning = "\n".join(d.warnings) + assert "0x11100010" in warning, "the value found must be named" + assert "not a CodeView signature" in warning