Repository navigation
[AUTOMATED] feat(p1): dwarfstructs — import DWARF aggregate size and fields (DIV-85) - #330
Merged
Merged
Conversation
…fields (DIV-85) The DWARF type mapper interned every DW_TAG_structure_type/union_type/ class_type as a named, EMPTY, zero-size shell: it never read DW_AT_byte_size and never walked a DW_TAG_member. That is enough for `struct foo *p` to render and nothing else, and a zero width is not a conservative answer -- x86-64 parameter-storage assignment reads the size, so a by-value struct parameter degraded to the raw register it arrives in and an 8-byte struct return (a REGISTER return on this ABI) was classified as a hidden-return- buffer call, growing a phantom `rethidden` parameter the body then did arithmetic on. The new arm reads DW_AT_byte_size, walks the DW_TAG_member children, places each at its DW_AT_data_member_location verbatim (bitfields off DW_AT_bit_size with either the DWARF 4/5 DW_AT_data_bit_offset or the DWARF 2/3 byte_size+bit_offset spelling), and recurses each member type through the same DIE switch. Offsets go in through a new raw field-setting seam (TypeFactory::set_fields_struct_raw / set_fields_union_raw) rather than the C packing rules, which would re-derive them and size the structure at 0. Two hazards come with populating fields. The type factory refuses a second, different definition of a name it holds -- invisible while every aggregate was a sizeless shell -- so aggregates are interned parent-qualified and, on a size clash, under a size-suffixed name (rustc names every enum payload struct bare: a five-function Rust witness carries four distinct `Some` DIEs of sizes 16/24/16/12). And a self-referential struct reaches its own DIE mid-populate, so the shell is interned before the members are walked and TypeWalk refuses the re-entrant population. DW_TAG_variant_part/variant/DW_AT_discr are deliberately not read. Behind `--option dwarfstructs on|off` (analysis tier, default-ON, DIV-85), gated through the same load-time env bridge as `typedepth`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
…ds use the access geometry
Three defects found by measurement after the first commit, all in the new arm.
1. Union collapse. Every union member sits at offset 0, and the struct-side
dedup-on-offset therefore reduced a union to its FIRST member: `u->f` and
`u->c[2]` silently became reads of `u->i`. Union fields now keep declaration
order and are not deduplicated. Covered by two new fixture readers and a new
e2e assertion.
2. Bitfield container geometry. Giving each bitfield the whole storage unit as
its container reports `byte_offset == 0` for a member the compiler reaches
through a PTRSUB at +3, so the printer's `checkBitFieldMember` did not skip
that PTRSUB and emitted `p.b` on a POINTER -- invalid C. Each bitfield now
gets the smallest byte span that covers it, which is the geometry the access
agrees with.
3. A bitfield that occupies whole aligned bytes of a natural width is not a
bitfield: on little-endian `unsigned c:16` at bit 16 of a 4-byte unit IS the
two bytes at +2, and it now goes in as a plain field of that width. That is
exact, and it is also containment for a pre-existing engine defect this pass
is the first thing to reach: BitFieldPullTransform diverges when several
pulls share one extraction chain (the second record's op_destroy_recursive
walks a mod_op whose operands the first walk unset, and the port asserts on
the cleared slot). `struct { int head; unsigned f1:4, f2:12, f3:16; }` with
all three read reproduces it. Tolerating the cleared slot only turns the
assertion into a non-terminating rule pool, so the fix belongs in that
transform; the byte-exact rule keeps this pass inside what it handles.
Also pins `dwarfstructs off` inside verify_typedepth so that file keeps
measuring its own axis, and records the final measurements in the DIV-85 row:
891 of 3764 functions change across 27 targets, cast-and-offset dereferences
10147 -> 1662, `decompilation failed` 0 -> 0, worst speed delta +0.75%.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9
mahaloz
force-pushed
the
feat/dwarfstructs
branch
from
August 22, 2026 17:16
0a3ab85 to
5eb44b3
Compare
This was referenced Aug 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
analyzers/dwarf/mod.rs::intern_aggregateresolved everyDW_TAG_structure_type/union_type/class_typetoget_type_struct(name)—a named, empty, zero-size shell. It never read
DW_AT_byte_sizeand neverwalked a single
DW_TAG_member. The module header stated the scope openly("Type recovery is name/signature-level") and the call site's comment called it
sufficient ("A named opaque struct is enough for a pointer-to-struct to render").
It is sufficient for exactly that one case. A width of zero is not a
conservative answer — the ABI classifier acts on it. x86-64 parameter-storage
assignment reads the size, so:
take_structint take_struct(unsigned long p,int k)+ a hand-rolled>> 0x20int take_struct(P8 p,int k) { return p.b + p.a + k; }ret_structP8 *ret_struct(P8 *rethidden,unsigned int x)…(int)rethidden * 7P8 ret_struct(unsigned int x)take_nest*(int *)((long)n + 4)(n->inner).atake_bits(*(unsigned char *)b & 7) + (*(unsigned char *)b >> 3) + …b->lo + b->mid + b->hiOn the survey's phantom-
rethiddencaveat: I DID reproduce it, on the exactwitness quoted in the task, with the prebuilt
origin/mainbinary. An 8-bytestruct return is a register return on this ABI; a zero-size return type is
classified as a hidden-return-buffer call, so a phantom first parameter appears,
the real one shifts, and the body does arithmetic on the phantom. It disappears
with
--option dwarf off, which pins the cause on this pass. My guess at why thesurvey saw
unsigned long ret_struct(int a0)instead: their build had gcc emitthe function as a nameless out-of-line concrete instance
(
DW_AT_abstract_origin, noDW_AT_name), which the DWARF pass skips entirely— an adjacent, pre-existing gap noted below.
What ships
Option
dwarfstructs(on|off, analysis tier, default-ON, DIV-85), gatedthrough the same load-time env bridge as
typedepth(
p0_knowledge/kuna_dwarfstructs.rs+KUNA_DWARFSTRUCTS; the layout isinstalled at
load file, upstream of everyoptioncommand).analyzers/dwarf/kuna_dwarfstructs.rsreadsDW_AT_byte_size, walks theDW_TAG_memberchildren, places each at itsDW_AT_data_member_locationverbatim, and recurses each member type through the same DIE switch. Offsets
go in through a new raw seam (
TypeFactory::set_fields_struct_raw/set_fields_union_raw, the structural analog ofget_type_enum_sized) ratherthan
assign_raw_fields_struct, whose C packing rules skip a field whose offsetis already set and would therefore also size the structure at 0.
DW_TAG_variant_part/DW_TAG_variant/DW_AT_discrare deliberately notread — a Rust enum recovers its width and no fields.
The collision trap: confirmed, and load-bearing
The factory interns by
(name, hash(name))andfind_addrefuses a second,different definition of a name it holds. That was invisible while every aggregate
was a sizeless shell (two shells compare equal); it goes live the moment fields
exist. I verified the Rust claim on a fresh witness: a five-function
rustc -gbinary carries four distinct
SomeDIEs of sizes 16, 24, 16 and 12(
core::option::Option<usize>,Option<(NonNull<u8>, Layout)>,Option<&[Placeholder]>,Option<rw::Pair>).Aggregates are therefore interned parent-qualified (reusing
kuna_cppproto'sancestry walk) and, when that name is still held by a different-sized aggregate,
under a size-suffixed variant. Verified by ablation: with the disambiguation
clamped off, the fixture's second
struct Samedegrades its whole parameter toint read_same_big(void *s); with it,Same(4 bytes) andSame_16both keeptheir members.
Self-reference (
struct node { struct node *next; }) is handled by interning theshell before walking the members plus a new
TypeWalk::begin_aggregateclaimthat refuses the re-entrant populate. Documented LOSS: kuna's interned types are
immutable, so completing one mints a new
Rcand the pointer the inner framecaptured still refers to the pre-completion shell —
n->nextrenders and namesits type,
n->next->valdoes not chain (the same model the wiredecode_compositepath lives with).Three defects the measurement found before they shipped
dedup-on-offset reduced a union to its FIRST member —
u->fandu->c[2]silently became reads of
u->i. Fixed; a dedicated e2e assertion now pins it.as its container reports
byte_offset == 0for a member the compiler reachesthrough a
PTRSUBat +3, socheckBitFieldMemberdid not skip thatPTRSUBand printed
p.bon a pointer — invalid C. Each bitfield now gets thesmallest byte span covering it, which is what the access geometry agrees with.
An engine defect this change is the first thing to reach
BitFieldPullTransform(p5_types/bitfield/pull.rs) diverges when several pullsshare one extraction chain: the second record's
op_destroy_recursivewalks amod_opwhose operands the first record's walk already unset, andFuncdata::op_destroy_recursiveasserts on the cleared slot(
op_destroy_recursive: null input (C++ UB)).Reproducer, two lines of C, on
maintoday via the Ghidra-wire<bitfield>decode as well — it is not specific to this pass:
I tried the obvious fix (tolerate the cleared slot; skip already-dead ops; bound
the worklist) and it only converts the assertion into a non-terminating rule
pool — so the fix belongs in that transform and I reverted my engine edit
rather than ship a hang. Containment instead: a "bitfield" occupying whole
aligned bytes of a natural width is not one (on little-endian
unsigned c:16atbit 16 of a 4-byte unit is the two bytes at +2) and goes in as a plain field,
which keeps the shapes this pass produces inside what the transform handles. A
nine-shape bitfield stress corpus decompiles clean, and
decompilation failedis0 → 0 across the whole measurement corpus.
Measurement
The gates cannot see this change — both parity corpora are
<bytechunk>XMLthat never reaches the analyzer tier — so the A/B below is the deliverable.
Breadth — whole-binary
decompile-all, off vs on, 27 targets / 3764functions (decbench-dataset zlib/bzip2/gzip/coreutils-fmt/diffutils/libexpat/
openssh O0 + zlib/bzip2 O2, every repo
-gfixture including the four C++ ones, aRust
-gbinary, and purpose-built C witnesses):*(T *)((long)x + N)->fieldaccessesvoid *decompilation failedundefined<n>Line counts fall on the bigger targets (ssh-keygen 38009 → 37722, bzip2-O0
6860 → 6730, gzip 6593 → 6544) because a named field replaces a cast expression.
Representative (zlib
gz_look,deflate_stored, the C++cppprotofixture):The wrongness axis, reported as measured rather than as zero.
undefined<n>+8 and duplicated-declaration functions +6 (of 3764; the pathology is
pre-existing on 1320 of them). Both come from one place: a local that is now a
real aggregate with a union member gets per-use union resolution, and the
declaration emitter prints one declaration per resolved width. That is the
existing
dedupvardecls/union-resolution seam, not a new one — 6 functionsagainst 891 improved.
Controls. The stripped copy of zlib
minigzipis 0 changed lines(so the decbench scored corpus, which is stripped, is a structural zero here —
verified, not assumed, exactly as for DIV-63). The four
-gfixtures with noaggregates (
dwarf_globals,stacklocal,dwarf_enums,dwarf_stripped) andthree of the four C++ fixtures are 0 changed lines.
Speed — the layout walk runs once per binary inside
load file, so LOAD isthe primary metric. Interleaved off/on, MINIMUM of N pairs on a box shared with
concurrent agents. An early n=15 read of +2.87% (gzip) and +3.58% (bzip2) was
contention; both vanish at higher N:
Worst delta +0.75%, budget 5%.
Default-ON argument
Both parity corpora are structurally blind to this change (0/675 is true but
vacuous), so the case rests on the A/B: 891 functions improved, every counted
quality pattern moving the right way, zero new decompilation failures, 0 lines on
the stripped control, worst speed delta +0.75%, and a wrongness axis of +6
functions on a pathology that already affects 1320. A zero-size aggregate is not
a conservative answer — it is a wrong one the ABI classifier acts on — so
offis the reproduction, not the safe default.
--option dwarfstructs offrestoresthe name-only mapping byte for byte.
Adjacent gaps observed, NOT fixed here
DW_AT_abstract_originsubprograms lose their prototype. When gcc inlines afunction somewhere it emits a named abstract instance plus a nameless
out-of-line concrete instance; the name-only arm skips the latter (no
DW_AT_name) andkuna_cppprotofollows onlyDW_AT_specificationfor asubprogram. Four functions in my first witness lost their signature this way,
identically off and on. Pre-existing, orthogonal, worth its own PR.
DWARF type while the body still reads the register as a pointer, leaving one
unassigned local (
rw::f_some_pair). 5 of 540 functions changed on the Rustwitness; the type is right and the body is no worse than the
u64it replaced.Tests
decompiler/crates/kuna-console/tests/verify_dwarfstructs.rs— 9 two-passe2e assertions over a new
-g -O1two-translation-unit fixturedwarfstructs_x86_64(both sources vendored): by-value struct parameter,phantom small-struct sret, the REAL 24-byte sret surviving, union, a union
keeping EVERY member, nested struct, bitfield, self-referential node
terminating, and two same-named different-sized aggregates both surviving.
kuna_dwarfstructs.rsenv-gate unit test;TypeWalkaggregate-claim unit test.verify_typedepth.rspinsdwarfstructs offso it keeps measuring its ownaxis (that arm renames the anonymous fallback and interns a name-colliding
aggregate as
code_4with fields — both strict improvements on what thoseassertions pin, and both covered by
verify_dwarfstructs.rs).Gates (run locally — CI skips
make rust-teston a same-repo branch)No baseline was re-pinned.
🤖 Generated with Claude Code
https://claude.ai/code/session_016WUuE4zdiEqZFWLL2YEUk9