Skip to content

s8 structure: set overflow syntax for complex-head while-do (fix regionstructure invalid C) - #143

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

mahaloz merged 1 commit into
mainfrom
feat/gbk-compare_random

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

A ghidra-beats-kuna case (sort/compare_random, O0) where kuna lost to invalid C: regionstructure folded a loop whose condition block is a complex region (a BlockList with nested if/else) into a top-tested while(...) and the s9 emitter flattened the region's statements inline into the condition parentheses — an unterminated while (v1 = … head with if v18 < v1 { (missing parens). Confirmed culprit: --option regionstructure off is the only one of the seven default-on passes that restores valid, Ghidra-like C.

Root cause (C++ port gap)

RegionStructurer::try_fold_loop's while-do branch (s8_structure/region_structurer.rs:1580) folded the loop into a BlockWhileDo without setting overflow syntax. Upstream CollapseStructure::ruleBlockWhileDo (blockaction.cc:1518; kuna mirror blockaction.rs:2351) guards exactly this: overflow = isComplex(head) → setOverflowSyntax(), which renders while(true){ …; if(cond) break; … } instead of inlining the region into while(cond).

Fix

19-line port of ruleBlockWhileDo's overflow logic into try_fold_loop: compute is_complex(head), negate on (i==0) != overflow, pass the pre-negation clause block, and set_overflow_syntax() when complex. Non-complex heads are byte-for-byte unchanged.

Verification

  • Gates: make test 675/675 PARITY OK, make test-stages 254/254, make rust-test 0 failures (independently re-confirmed 675/675 + compare_random balanced 25/25).
  • Whole-binary safety (coreutils sort, 504 fns, true pre/post baseline): malformed-C conditions 12→0 (compare_random 11→0, sub_10057 1→0); total gotos 91→91; 0 newly-unbalanced; 0 new indent corruption. The other 7 changed functions improved from valid-but-ugly while(a,cond){} comma-expr to Ghidra-like while(true){…; if(cond) break;}. Zero regressions.

🤖 Generated with Claude Code

https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J

…w syntax)

Root cause: RegionStructurer::try_fold_loop's while-do branch
(region_structurer.rs:1580) folded a loop whose condition block (`head`)
is a *complex* region (a BlockList with nested if/else statements) into a
top-tested BlockWhileDo WITHOUT setting overflow syntax. The s9 emitter
(emit_block_while_do, printc.rs:3060) then renders such a head inline in
COMMA_SEPARATE mode -> the region's nested `if`/statements print INSIDE the
`while (...)` condition parentheses (unparenthesized `if COND {`, an
unterminated `while (` head) = INVALID C.

Ghidra's CollapseStructure::ruleBlockWhileDo (blockaction.cc:1518, mirrored
in blockaction.rs:2351) guards exactly this with `overflow = isComplex(head)`:
a complex head is flagged setOverflowSyntax() so it renders as
`while (true) { <cond-body>; if (<cond>) break; <clause-body> }`, and the
loop-continue edge is flipped accordingly (`(i==0) != overflow`). try_fold_loop
omitted this. Fix ports the ruleBlockWhileDo overflow logic verbatim into the
region structurer: compute is_complex(head), negate on (i==0)!=overflow, pass
the pre-negation clause block, and set_overflow_syntax() when overflow.
Non-complex head (overflow==false) keeps the prior negate-when-i==0 behavior
byte-for-byte, so reducible code is unchanged.

Confirmed by `--option regionstructure off` restoring valid C
(coreutils/sort O0 compare_random @0x7755). The fix makes regionstructure-on
byte-identical to regionstructure-off for that function.

Gates: datatests 675/675 PARITY OK; test-stages 254/254 PARITY OK;
rust-test 0 failures (273 ok result lines).
Whole-binary safety (kuna decompile-all sort, pre-fix baseline via git stash):
504/504 functions, 0 errors before+after; total gotos 91->91 (no increase);
0 functions newly brace-unbalanced; 0 new column-0/indent corruption;
9 functions changed, all valid; real malformed-C conditions (comments
stripped) 12->0 whole-binary (compare_random 11->0, sub_10057 1->0), the
other 7 changed from valid comma-expr `while(a,cond){}` to Ghidra-like
`while(true){...; if(cond) break;}`. Zero regressions.

Co-Authored-By: Claude Fable 5 <[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