Repository navigation
s8 structure: set overflow syntax for complex-head while-do (fix regionstructure invalid C) - #143
Merged
Merged
Conversation
…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
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/compare_random, O0) where kuna lost to invalid C:regionstructurefolded a loop whose condition block is a complex region (aBlockListwith nested if/else) into a top-testedwhile(...)and the s9 emitter flattened the region's statements inline into the condition parentheses — an unterminatedwhile (v1 = …head withif v18 < v1 {(missing parens). Confirmed culprit:--option regionstructure offis 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 aBlockWhileDowithout setting overflow syntax. UpstreamCollapseStructure::ruleBlockWhileDo(blockaction.cc:1518; kuna mirror blockaction.rs:2351) guards exactly this:overflow = isComplex(head)→setOverflowSyntax(), which renderswhile(true){ …; if(cond) break; … }instead of inlining the region intowhile(cond).Fix
19-line port of
ruleBlockWhileDo's overflow logic intotry_fold_loop: computeis_complex(head), negate on(i==0) != overflow, pass the pre-negation clause block, andset_overflow_syntax()when complex. Non-complex heads are byte-for-byte unchanged.Verification
make test675/675 PARITY OK,make test-stages254/254,make rust-test0 failures (independently re-confirmed 675/675 + compare_random balanced 25/25).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-uglywhile(a,cond){}comma-expr to Ghidra-likewhile(true){…; if(cond) break;}. Zero regressions.🤖 Generated with Claude Code
https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J