Skip to content

s8 structure: reject compound-head while-do fold under regionlooprefine (fix invalid C) - #145

Merged
mahaloz merged 1 commit into
mainfrom
feat/gbk-looprefine-compound-head
Jul 7, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/gbk-looprefine-compound-head

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

A fresh ghidra-beats-kuna case (from the post-#6 fair re-run's current gap pool): libedit vi_history_word (O0), kuna GED 60 vs Ghidra 9. kuna emits INVALID C — a full nested loop with its body inside the outer loop's controlling expression:

while ( while (v3=*v7, iswspace(v3)!=0) {v7=&v7[1]} *v7 != 0 ) { ... }   // statement-in-condition

It is brace-balanced, so neither naive nor literal-aware brace counting flags it — only structural inspection does (FINDING 3 in reverse: not every invalid-C bug is a brace imbalance). Ghidra renders the clean while(true){ inner-while; if(*p==0) break; …; if(cond) break; }.

Root cause

regionlooprefine (S8, region_structurer.rs, default-on DIV-12 port of angr RegionIdentifier._refine_loop_successors_to_guarded_successors) refines the outer loop into a top-tested while-do candidate whose head has absorbed the inner skip-whitespace loop (head type BlockList). try_fold_loop's while-do branch guarded against interior goto targets but not against a compound (loop/if-containing) head, so the emitter renders that compound head inside the outer while(...) condition. Confirmed culprit: --option regionlooprefine off restores valid, Ghidra-matching C.

Fix (23 lines, gated on loop_refine)

A valid while-do head is always a single condition block — BlockCopy (comma-expr basic block) or a folded BlockCondition (&&/||). Reject anything else so the loop folds as BlockInfLoop → while(true){…break}. Gated on loop_refine, so the base structurer (which produces the 675 byte-identical datatests, and to which regionlooprefine ON is byte-identical on reducible code) is completely untouched.

Verification

Note

A separate regionstructure compound-head case (bzip2 generateMTFValues) is not covered by this loop_refine guard and remains for follow-up. Related to (but distinct from) #143 (regionstructure complex-head overflow syntax) — both are while-do invalid-C fixes in the same branch; may need a trivial rebase if merged together.

🤖 Generated with Claude Code

https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J

…ne (fix invalid C)

A fresh ghidra-beats-kuna case (libedit vi_history_word, O0) where kuna emits INVALID C: a
full nested loop WITH ITS BODY inside the outer loop's controlling expression —
`while ( while(v3=*v7, iswspace(v3)!=0){v7=&v7[1]} *v7!=0 ) {...}` — a statement-in-condition
C syntax error. It is brace-BALANCED, so neither naive nor literal-aware brace counting flags
it; only structural inspection does. Ghidra renders the clean `while(true){ inner-while;
if(*p==0) break; ...; if(cond) break; }`.

Root cause: regionlooprefine (S8, region_structurer.rs, default-on DIV-12 port of angr
RegionIdentifier._refine_loop_successors_to_guarded_successors) refines the outer loop into a
top-tested while-do candidate whose HEAD has absorbed the inner skip-whitespace loop (head
type = BlockList). try_fold_loop's while-do branch guarded against interior goto targets but
not against a compound (loop/if-containing) head, so the emitter renders that compound head
inside the outer `while(...)` condition under COMMA_SEPARATE. Confirmed culprit:
`--option regionlooprefine off` restores valid Ghidra-matching C.

Fix (23 lines, gated on loop_refine): a valid while-do head is always a single condition
block — BlockCopy (comma-expr basic block) or a folded BlockCondition (&&/||). Reject anything
else (BlockList/absorbed loop) so the loop folds as BlockInfLoop -> `while(true){...break}`, the
OFF/Ghidra form. Gated on loop_refine, so the base structurer that produces the 675
byte-identical datatests is completely untouched.

Verification: make test 675/675 PARITY OK, make test-stages 254/254, make rust-test 0 failures.
Whole-binary safety (libedit.so, 777 fns, true #142 baseline): SURGICAL — vi_history_word's
bad loop -> valid while(true) (while(true) 38->39), total gotos unchanged (105), 0
newly-unbalanced, 0 indent corruption; bzip2 (144 gotos) completely unchanged. Note: a
separate regionstructure compound-head case (bzip2 generateMTFValues) is NOT covered by this
loop_refine guard and remains for follow-up.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018DV2MXxQAAWK7xLPBmsq9J
@mahaloz
mahaloz merged commit cf12ab7 into main Jul 7, 2026
@mahaloz
mahaloz deleted the feat/gbk-looprefine-compound-head branch July 29, 2026 08:01
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