Skip to content

[AUTOMATED] fix(p9): (NOVEL) one declaration per ScopeLocal Symbol (DIV-52) - #242

Merged
mahaloz merged 3 commits into
mainfrom
fix/symbol-keyed-local-decls
Aug 2, 2026
Merged

mahaloz merged 3 commits into
mainfrom
fix/symbol-keyed-local-decls

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

The bug

A single 4-byte hsvColor_t color local, declared twice under one name with two
types -- betaflight_STM32F405 applyLedFixedLayers, O2-noinline:

void applyLedFixedLayers(void)
{
  ...
  unsigned int v12; // stack - 0x24
  uint4 v12;        // stack - 0x24

The function does not compile, and no body occurrence of v12 can be resolved by
a reader. Ghidra emits one undefined4 local_24;, IDA one int v23;.

Case: docs/decbench/triage/O2-noinline-betaflight-betaflight_STM32F405-applyLedFixedLayers.md
(NOVEL pool). kuna is already tied-best on this function (GED 25 == ghidra 25,
vs angr 42 / ida 41), so this fix moves zero benchmark points. Its value is
that the output stops being invalid C.

Root cause -- upstream-fidelity verdict: an UNPORTED rule

PrintC::emitLocalVarDecls walks HighVariables. Upstream emitScopeVarDecls
walks the ScopeLocal Symbol table (printc.cc:2667/2696), which is exactly what
makes one symbol one declaration. When two live ranges of one mapped slot did not
merge, the shared-storage naming gave both the same name and P5 recovered
different types, so both were declared.

option dedupvardecls (DIV-7, default on) is the only collapse, and it keys on
the fully rendered line -- its module doc records the same-name/different-type
residual as intentional. The composite (array/struct/union) collapse next to
it was already Symbol-keyed; the scalar case was the documented gap ("primitive
types are shared by every scalar local of that type", so the interned-type key
does not identify a symbol). Not mis-ported, not a deliberate simplification --
the rule was never ported for scalars.

The duplicate name is not the deeper problem. On the campaign function nine
highs of symbol 5v13 carry the name v12; all nine are whole 4-byte covers at
offset 0, so naming them alike is correct (upstream would too). The same
symbol's partial covers get their own names (v14, v15, v16), and those are
referenced in the body. So the naming layer is right and the declaration layer is
where the rule was missing.

The fix

The scalar collapse is Symbol-keyed too, inside the same option: highs whose
declaration representative resolves to the same containing ScopeLocal Symbol --
base-byte containment, the query C++ Funcdata::linkSymbol itself uses
(funcdata_varnode.cc:1190) -- and that render the same identifier emit one
declaration.

Requiring the identifier to match is what makes the collapse incapable of
removing the last declaration of a referenced name (the mirror-image failure
#131 fixed), and it is load-bearing here: the v14/v15/v16 partials of the
same symbol keep their own declarations.

Which type survives. When the collapsing highs disagree, the survivor
declares the Symbol's own type -- what C++ emitVarDecl declares
(sym->getType(), printc.cc:1719) -- unless that type is narrower than the
widest storage the group covers
, in which case the widest member wins. kuna's
ScopeLocal ranges are sometimes narrower than the accesses that reach them
(upstream restructure would have grown the symbol); declaring a 2-byte object
for a slot the body writes 4 bytes into and passes by address would be a new
defect rather than a faithful one. A group that already agrees keeps its
recovered type, which is sharper than the usually-undefined<N> symbol type. On
the campaign function the symbol type is 4 bytes and wins, giving
unsigned int v12; -- the same choice as Ghidra's undefined4 local_24.

No new option

Emitting invalid C is wrong output, so per AGENTS.md this is a strict bug fix
and takes no flag. It lands inside the existing default-on dedupvardecls,
which already owns "collapse duplicate local declarations" -- so option dedupvardecls off still restores the one-declaration-per-high rendering for
byte-comparison work, and no catalog counts move. Recorded as DIV-51.

Measured -- 18 -> 0 across 14 binaries

kuna decompile-all --json --mode reliable over 14 decbench binaries (x86-64 and
ARM Cortex-M, -O0 and -O2), before and after. Nobody had measured this bug's scope
before:

before after
functions emitting a duplicate local declaration 18 / 11,874 0
functions whose emitted text changes at all -- 18
declaration lines 35,412 35,391
functions that lose a declared name -- 0
functions that gain a declared name -- 0

The blast radius is exactly the broken functions. Every survivor is at least as
wide as every access it absorbed -- undefined3 / unsigned short / char
shadows of a 4-byte slot become one unsigned int:

-  char $$undef00000001; // stack - 0x14
-  undefined3 $$undef00000001; // stack - 0x14
+  unsigned int $$undef00000001; // stack - 0x14

Speed. The collapse only runs where two declarations already share an
identifier, so the symbol lookups are skipped on nearly every function.
Interleaved medians, decompile-all --mode reliable: betaflight STM32F405 (6,387
functions) 69,782 -> 69,754 ms (-0.04%), bzip2 9,926 -> 9,863 ms (-0.63%), gzip
3,162 -> 3,211 ms (+1.55%, 9 pairs).

Tests

  • tests/stages/ghdec-symbol-keyed-local-decls.xml -- end-to-end over a real
    binary -> decompile(addr) path: the 72-byte 0x804f5b4 from the same stripped
    Cortex-M firmware, the smallest witness of the same defect (its callees stubbed
    bx lr at their real addresses). Pass 1 (default) asserts the slot is declared
    once, at the width the body uses; pass 2 (option dedupvardecls off) pins
    the un-collapsed bug form. 4 of 6 assertions pass pre-fix, 6/6 post-fix.
  • verify_declared_vars (the s9/printc: fix undeclared-variable invalid-C (declare locals overlapping a param register) #131 gate, fixture declmerge_x86_64) now asserts
    both failure modes -- no name undeclared, no name declared twice -- so a fix
    for either cannot re-open the other.
  • Spec prose: docs/spec/09-emission.md section 9.3 now describes the collapse as
    two steps (by symbol, then by rendered line) and why the symbol step is what
    makes it total for a mapped slot.

Gates

make test          PARITY OK (675/675, docs/baseline.json untouched)
make test-stages   PARITY OK (352/352; re-recorded, delta is purely the 6 new keys)
make rust-test     green
make check-spec    OK
kuna catalog --check  OK

decompiler/crates/kuna-base/src/xml.rs corpus count 182 -> 183, and the
phase_catalog.json bytecompat fixture was regenerated for the option's updated
prose.

inspiration = "decbench:O2-noinline-betaflight-betaflight_STM32F405-applyLedFixedLayers; NOVEL pool"

🤖 Generated with Claude Code

https://claude.ai/code/session_011RXVU58TKJxb518oViChwo

mahaloz and others added 3 commits August 1, 2026 23:58
…bol (DIV-51)

`PrintC::emitLocalVarDecls` walks HighVariables; upstream `emitScopeVarDecls`
walks the ScopeLocal *Symbol* table (printc.cc:2667/2696), which is what makes
one symbol one declaration.  When two live ranges of one mapped stack slot did
not merge, the shared-storage naming gave both the same name and P5 recovered
different types for them, so the function opened with the same identifier
declared twice at two widths:

    void applyLedFixedLayers(void)   // betaflight_STM32F405, O2-noinline
    {
      ...
      unsigned int v12; // stack - 0x24
      uint4 v12;        // stack - 0x24

That is not compilable C, and a reader cannot tell which `v12` any body
occurrence means.  Ghidra emits one `undefined4 local_24;`, IDA one `int v23;`.

The only collapse that existed, `option dedupvardecls` (DIV-7, default on),
keys on the fully rendered LINE, so two spellings of one slot both survive by
construction -- its own module doc recorded that residual as intentional.  The
composite (array/struct/union) collapse sitting beside it was already
Symbol-keyed; the scalar case was the documented gap ("primitive types are
shared by every scalar local of that type", so the type-Rc key does not
identify a symbol).

Upstream-fidelity verdict: an UNPORTED rule, not a mis-ported or deliberately
simplified one.  The scalar collapse is now Symbol-keyed too, inside that same
option: highs whose declaration representative resolves to the same containing
ScopeLocal Symbol -- base-byte containment, the query C++ `Funcdata::linkSymbol`
itself uses (funcdata_varnode.cc:1190) -- AND that render the same identifier
emit one declaration.

Requiring the identifier to match is what makes the collapse incapable of
removing the last declaration of a referenced name, i.e. the mirror-image
failure #131 fixed.  It is load-bearing: on the campaign function nine highs of
symbol 5v13 are named `v12` (all whole 4-byte covers, so naming them alike is
correct) while its partial covers carry their own names `v14`/`v15`/`v16`, and
those must all keep their declarations.  The duplicate NAME is therefore not a
naming defect -- naming per symbol is right, and the declaration layer is where
the rule was missing.

Type arbitration when the collapsing highs disagree: the Symbol's own type,
which is what C++ `emitVarDecl` declares (`sym->getType()`, printc.cc:1719) --
unless it is narrower than the widest storage the group covers, in which case
the widest member wins.  kuna's ScopeLocal ranges are sometimes narrower than
the accesses that reach them (upstream `restructure` would have grown the
symbol), and declaring a 2-byte object for a slot the body writes 4 bytes into
and passes by address would be a new defect rather than a faithful one.  A
group that already agrees keeps its recovered type, which is sharper than the
usually-`undefined<N>` symbol type.

Strict bug fix inside an existing default-on option, so no new flag; recorded
as DIV-51.  `option dedupvardecls off` still restores the per-high rendering.

Measured before/after on 14 decbench binaries / 11,874 functions (x86-64 and
ARM Cortex-M, -O0 and -O2), the first measurement of this bug's real scope:
functions emitting a duplicate local declaration 18 -> 0.  The blast radius is
exactly those 18 -- no other function changes a byte, 21 declaration lines
disappear (35,412 -> 35,391), no function loses a declared name and none gains
one.  Every survivor is at least as wide as every access it absorbed
(`undefined3`/`unsigned short`/`char` shadows of a 4-byte slot become one
`unsigned int`).  The collapse only runs where two declarations already share an
identifier, so the symbol lookups are skipped on nearly every function;
interleaved medians of `decompile-all --mode reliable`: betaflight STM32F405
(6,387 functions) 69,782 -> 69,754 ms (-0.04%), bzip2 9,926 -> 9,863 ms
(-0.63%), gzip 3,162 -> 3,211 ms (+1.55%, 9 pairs).

`verify_declared_vars` now asserts both failure modes on the `declmerge`
fixture: no name undeclared (#131) and no name declared twice.
`tests/stages/ghdec-symbol-keyed-local-decls.xml` is the end-to-end witness over
a real binary -> decompile(addr) path (4 of 6 assertions pass pre-fix, 6/6
post-fix); it uses the smallest function in the same firmware showing the same
defect, with `option dedupvardecls off` as pass 2 pinning the bug form.

This function was already tied-best on the benchmark (kuna GED 25 == ghidra 25),
so the fix moves zero benchmark points; its value is that the output stops
being invalid C.

Gates: make test PARITY OK (675/675, baseline untouched), make test-stages
PARITY OK (352/352, re-recorded -- delta is purely the 6 new keys), make
rust-test green, make check-spec OK, kuna catalog --check OK.

inspiration = "decbench:O2-noinline-betaflight-betaflight_STM32F405-applyLedFixedLayers; NOVEL pool"

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011RXVU58TKJxb518oViChwo
…l-decls

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	docs/baseline-stages.json
#	docs/history.md
@mahaloz mahaloz changed the title [AUTOMATED] fix(p9): (NOVEL) one local declaration per ScopeLocal Symbol (DIV-51) [AUTOMATED] fix(p9): (NOVEL) one declaration per ScopeLocal Symbol (DIV-52) Aug 2, 2026
@mahaloz
mahaloz merged commit 36fe8d9 into main Aug 2, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/symbol-keyed-local-decls branch August 2, 2026 00:08
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