Skip to content

s9 emit: frame-local pending-brace close (fix branchflip extra-} invalid C + cross-function indent leak) - #142

Merged
mahaloz merged 1 commit into
mainfrom
feat/gbk-branchflip-braces
Jul 7, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/gbk-branchflip-braces

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

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 lazy else { … } brace by reading the emitter's single shared pend_brace_indent slot. Upstream C++ pendingBrace.getIndentId() is frame-local — set only when that frame's own brace fires. When an else-clause BlockIf's pending brace was shadowed by a nested BlockIf'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: each set_pending_brace stamps a monotonic generation; each emit_block_if frame 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

  • Gates: make test 675/675 PARITY OK, make test-stages 254/254, make rust-test 0 failures (independently re-confirmed 675/675 + keycompare balance).
  • Whole-binary safety (coreutils 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

… 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
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