Skip to content

Metadata: MSVC RTTI class-name recovery (s1_rtti, x86+x64) - #108

Merged
mahaloz merged 2 commits into
mainfrom
metadata-rtti
Jun 27, 2026
Merged

mahaloz merged 2 commits into
mainfrom
metadata-rtti

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Metadata frontier, PR 1 of 3 — MSVC RTTI class-name recovery

The kuna analog of Ghidra's RttiAnalyzer (a Microsoft-PE analyzer), and the first of the three PE/Mach-O deep-metadata analyzers laid out in the canonical design doc this PR also lands (docs/metadata-analyzers-design.md). On a Windows PE built by MSVC (or clang -target …-windows-msvc), it recovers the C++ class names from the run-time type information graph — the one self-contained, highest-ROI metadata analyzer.

Before → after

On a polymorphic-C++ PE (tests/fixtures/msvc_rtti_x{64,86}.exe, one base class Shape + one derived class Box with a virtual method):

                               symbol table / .rdata
--option rtti off (default) →  .rdata is raw bytes; the vtable is an unnamed
                               DAT_<addr>; no `Box`/`Shape` class names exist.
--option rtti on            →  Box::RTTI_Type_Descriptor, Shape::RTTI_Type_Descriptor,
                               Box::RTTI_Complete_Object_Locator, Box::vftable —
                               so `Box` and `Shape` surface as recovered C++ classes.

The walk (port of RttiAnalyzer.added(), entirely in the loaded image)

Find the common type_info vftable (the pointer every RTTI0 TypeDescriptor shares; ≥2 agreeing) → locate the TypeDescriptors by scanning for the .?A…@@ type-info name strings → byte-search the image for a reference to each TypeDescriptor, −12 = the CompleteObjectLocator (COL) base → validate the COL→RTTI3 ClassHierarchyDescriptor→RTTI2 BaseClassArray→RTTI1 BaseClassDescriptor→RTTI0 reachability chain → demangle each .?A…@@ class name → emit the <Class>::RTTI_Type_Descriptor / <Class>::RTTI_Complete_Object_Locator / <Class>::vftable Data labels.

x86 vs x64 ref-kind handling (design §3.1f — the one place a port goes wrong)

Isolated in s1_rtti/refkind.rs (RefKind::new(is64, image_base, end)); the walk never branches on is64 directly:

x86 (raw VA) x64 (IBO32)
COL→TD/CHD, RTTI2 entries full 4-byte raw VA image_base + disp32
RTTI0 class-name string offset +8 +16
COL meta-pointer before vftable full ptr-width VA full ptr-width VA

Both arches verified against the real fixtures (objdump -s -j .rdata).

Zero new demangler code: the new s1_demangle::demangle_rtti_class_name reuses the same msvc_demangler crate already driving the MSVC arm — it wraps the .?A…@@ type-info name as ??_R0<name>@8 (the Ghidra RttiUtil recipe) and demangles that, taking the class prefix of …::RTTI Type Descriptor'. Recovers Box, Shape, namespaced ns::Inner, and templated Vec→Vec`.

Gate + catalog

Default-OFF (--option rtti on), registered in passes_for ONLY for BinaryFormat::Pe (the pass also self-gates on PE in run); the facts are committed only behind the "rtti" => arch.analysis_rtti gate. Real-PE-path-only + default-off ⇒ the XML datatest oracles are structurally untouched. Settable count 65 → 66 (stages.toml + all count assertions); kuna catalog --check → catalog OK.

Gates (all four green)

  • make test — 675/675 PARITY OK
  • make test-stages — 235/235 PARITY OK (KUNA-CATALOG Stage reorg #5/Fix test stages #9 use_when/change_kind max 66 → 67: the catalog <script> emits the full catalog + one returnpair single-row, so the count is settables+1)
  • make rust-test — green, incl. the new verify_rtti e2e (4 tests — x64+x86 × on/off — ran, not skipped) + the regenerated catalog/count tests
  • kuna catalog --check — catalog OK

Fixtures

clang -target {x86_64,i686}-pc-windows-msvc -fuse-ld=lld -nostdlib emits a linked PE with the genuine MSVC RTTI bytes in .rdata (a one-cell inline-asm stub supplies the only CRT symbol the Type Descriptors reference, ??_7type_info@@6B@, so it links freestanding). Both arches built in kuna-dev, vendored with the .cpp source + a README entry pinning the COL/RTTI0/vftable VMAs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 27, 2026 17:07
…, x86+x64)

PR 1 of 3 of the PE/Mach-O deep-metadata frontier (canonical design in the
co-landed docs/metadata-analyzers-design.md). The kuna analog of Ghidra's
RttiAnalyzer: on a Windows PE built by MSVC (or clang -target ...-windows-msvc),
recover the C++ class names from the run-time type information graph in
.rdata/.data.

The walk (port of RttiAnalyzer.added(), entirely in the loaded image): find the
common type_info vftable (the pointer every RTTI0 TypeDescriptor shares) -> locate
the TypeDescriptors by scanning for the `.?A...@@` type-info names -> byte-search
back to each CompleteObjectLocator (COL = ref - 12) -> validate the
COL -> RTTI3 -> RTTI2 -> RTTI1 -> RTTI0 chain -> demangle each class name -> emit
`<Class>::RTTI_Type_Descriptor` / `<Class>::RTTI_Complete_Object_Locator` /
`<Class>::vftable` Data labels, so `Box`/`Shape` surface as recovered classes.

x86 vs x64 (design 3.1f, isolated in refkind.rs): inter-structure refs are raw VAs
on x86 but image-base-relative IBO32 disp32 on x64; the RTTI0 name string sits at
TypeDescriptor offset 8 (x86) / 16 (x64). Both verified against the real fixtures.

Zero new demangler code: s1_demangle::demangle_rtti_class_name reuses the existing
msvc_demangler crate via the Ghidra RttiUtil `??_R0...@8` wrap.

Gated default-OFF (--option rtti on), registered in passes_for ONLY for a PE image
(the pass also self-gates on PE in run); the facts commit only behind the
analysis_rtti gate. Real-PE-path-only + default-off => the XML datatest oracles are
structurally untouched. Settable count 65 -> 66; catalog regenerated.

Fixtures: clang -target {x86_64,i686}-pc-windows-msvc -fuse-ld=lld -nostdlib emits
a linked PE with the genuine MSVC RTTI bytes (a one-cell inline-asm stub supplies
the only CRT symbol the Type Descriptors reference, ??_7type_info@@6b@). Both arches
vendored with the .cpp source + a README entry pinning the COL/RTTI0/vftable VMAs.

Gates: make test 675/675 PARITY OK; make test-stages 235/235 PARITY OK
(KUNA-CATALOG #5/#9 max 66 -> 67 -- the catalog script emits the full catalog + one
returnpair single-row, so the count is settables+1); make rust-test green incl. the
new verify_rtti e2e (4 tests, x64+x86 x on/off, ran not skipped); kuna catalog
--check catalog OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
# Conflicts:
#	decompiler/crates/kuna-analysis/src/lib.rs
#	docs/analysis-port-log.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant