Repository navigation
s9 emit: frame-local pending-brace close (fix branchflip extra-} invalid C + cross-function indent leak) - #142
Merged
Merged
Conversation
… cross-fn indent leak)
`emit_block_if`/`emit_block_if_ite` decided whether to close a lazy
`else { … }` brace by reading the emitter's single shared
`pend_brace_indent` slot. C++'s `pendingBrace.getIndentId()` is a
per-frame local, set only when *that* frame's brace fires. When an
else-clause BlockIf registered a pending brace that was then shadowed by
a nested/descendant BlockIf's fire (never firing its own), the Rust port
read the nested frame's stale fired id and emitted one unmatched `}` —
dumping the rest of the function to file scope (invalid C). In the
load-once/decompile-many `decompile-all` path the same stale slot leaked
across functions and corrupted later functions' indentation (bodies at
column 0).
Fix: stamp each pending-brace registration with a monotonic generation
(`pend_reg_gen`), record `(gen, indent)` for each brace that actually
fires (`pend_fired`), and have each frame close only if *its own*
generation fired (`pending_fired_indent(my_gen)`), matching C++'s
frame-local semantics. `reset_pending_fired()` at function-body start
bounds the log. No-op for every already-correct case.
Repro (branchflip default-on): keycompare @0x9550 in O2 stripped
coreutils `sort` went 57 open / 58 close (invalid) -> 57/57 (valid),
branchflip still firing (12 flips, 31 gotos = Ghidra's target).
Verification:
- datatests 675/675 PARITY OK; test-stages 254/254 PARITY OK; rust-test green.
- whole-binary `sort` (489 fns): unbalanced 1 -> 0, zero newly-unbalanced,
total gotos 231 -> 231; 29 column-0-body functions -> 0 (indentation now
matches the fresh-subprocess path).
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J
This was referenced Jul 7, 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.
A ghidra-beats-kuna case (
sort/keycompare, O2-noinline) where kuna lost purely to invalid C: it emitted one unmatched}(57 open / 58 close), dumping the rest of the function to file scope. Structurally kuna already matched Ghidra (both 31 gotos).Root cause (a C++ port-fidelity gap)
emit_block_if/emit_block_if_ite(s9_emit/printc.rs) decided whether to close a lazyelse { … }brace by reading the emitter's single sharedpend_brace_indentslot. Upstream C++pendingBrace.getIndentId()is frame-local — set only when that frame's own brace fires. When an else-clauseBlockIf's pending brace was shadowed by a nestedBlockIf's fire (so its own brace never opened), the port read the nested frame's stale id and emitted one extra}. Confirmed via a brace trace: keycompare had 6 frames closing a pending brace but only 5 fires.Bonus: the same shared slot leaked across functions in the in-process
decompile-all(load-once/decompile-many) path, emitting later functions' bodies at column 0. Fixed by the same change (+ a per-function reset).Fix
Made the fired-indent per-registration (frame-local), mirroring C++'s
PendingBrace: eachset_pending_bracestamps a monotonic generation; eachemit_block_ifframe closes only if its own generation actually fired (pending_fired_indent(my_gen) >= 0). It can no longer close on a stale sibling/descendant fire.Verification
make test675/675 PARITY OK,make test-stages254/254,make rust-test0 failures (independently re-confirmed 675/675 + keycompare balance).sort, 489 fns, branchflip default-on), true pre/post baseline: keycompare 57/58 invalid → 57/57 valid (branchflip still fires, 31 gotos = Ghidra's target); unbalanced 1→0, newly-unbalanced 0, total gotos 231→231, column-0-body functions 29→0.🤖 Generated with Claude Code
https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J