Repository navigation
[AUTOMATED] fix(p9): (NOVEL) one declaration per ScopeLocal Symbol (DIV-52) - #242
Merged
Merged
Conversation
…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
This was referenced Aug 2, 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 bug
A single 4-byte
hsvColor_t colorlocal, declared twice under one name with twotypes --
betaflight_STM32F405applyLedFixedLayers, O2-noinline:The function does not compile, and no body occurrence of
v12can be resolved bya reader. Ghidra emits one
undefined4 local_24;, IDA oneint 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::emitLocalVarDeclswalks HighVariables. UpstreamemitScopeVarDeclswalks 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 onthe 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
5v13carry the namev12; all nine are whole 4-byte covers atoffset 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 arereferenced 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::linkSymbolitself 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/v16partials of thesame symbol keep their own declarations.
Which type survives. When the collapsing highs disagree, the survivor
declares the Symbol's own type -- what C++
emitVarDecldeclares(
sym->getType(), printc.cc:1719) -- unless that type is narrower than thewidest 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
restructurewould have grown the symbol); declaring a 2-byte objectfor 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. Onthe campaign function the symbol type is 4 bytes and wins, giving
unsigned int v12;-- the same choice as Ghidra'sundefined4 local_24.No new option
Emitting invalid C is wrong output, so per
AGENTS.mdthis is a strict bug fixand takes no flag. It lands inside the existing default-on
dedupvardecls,which already owns "collapse duplicate local declarations" -- so
option dedupvardecls offstill restores the one-declaration-per-high rendering forbyte-comparison work, and no catalog counts move. Recorded as DIV-51.
Measured -- 18 -> 0 across 14 binaries
kuna decompile-all --json --mode reliableover 14 decbench binaries (x86-64 andARM Cortex-M, -O0 and -O2), before and after. Nobody had measured this bug's scope
before:
The blast radius is exactly the broken functions. Every survivor is at least as
wide as every access it absorbed --
undefined3/unsigned short/charshadows of a 4-byte slot become one
unsigned int: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,387functions) 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 realbinary -> decompile(addr)path: the 72-byte0x804f5b4from the same strippedCortex-M firmware, the smallest witness of the same defect (its callees stubbed
bx lrat their real addresses). Pass 1 (default) asserts the slot is declaredonce, at the width the body uses; pass 2 (
option dedupvardecls off) pinsthe 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, fixturedeclmerge_x86_64) now assertsboth failure modes -- no name undeclared, no name declared twice -- so a fix
for either cannot re-open the other.
docs/spec/09-emission.mdsection 9.3 now describes the collapse astwo steps (by symbol, then by rendered line) and why the symbol step is what
makes it total for a mapped slot.
Gates
decompiler/crates/kuna-base/src/xml.rscorpus count 182 -> 183, and thephase_catalog.jsonbytecompat fixture was regenerated for the option's updatedprose.
inspiration = "decbench:O2-noinline-betaflight-betaflight_STM32F405-applyLedFixedLayers; NOVEL pool"
🤖 Generated with Claude Code
https://claude.ai/code/session_011RXVU58TKJxb518oViChwo