Skip to content

regionstructure: loop-successor refinement infrastructure (convergence; goto-win is documented follow-up) - #95

Merged
mahaloz merged 4 commits into
mainfrom
feat/regionstructure-loop-refine
Jun 26, 2026
Merged

mahaloz merged 4 commits into
mainfrom
feat/regionstructure-loop-refine

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Lands the parity-safe portion of the region structurer's loop-successor refinement work as a focused increment, and documents the remaining path to the goto win as a future increment (Inc 6). This is the infrastructure + roadmap, not the goto win — by design.

Builds on the committed regionlooprefine infrastructure (default-OFF opt-in) and merges current main.

What this lands (parity-safe)

  • RI cyclic-loop projection — KunaRegionIdentifier::cyclic_loops() projects each identified cyclic region onto basic-block start addresses (head, refined body, exit frontier) — the read side of angr RegionIdentifier._refine_loop_successors_to_guarded_successors / _ensure_jump_at_loop_exit_ends (force_loop_single_exit), which kuna's RI port had omitted.
  • refine_loop_edges (re-enabled, RI-grounded) — for a multi-exit / multi-latch / mid-entry loop the base cyclic schemas declined, virtualize the secondary exits and latches to gotos so the body collapses toward the clean single-exit/single-latch shape try_fold_loop accepts (lowered to break;/continue; by the existing scopeBreak/loop-construction passes). Only loops the base schemas already declined are touched.
  • Gated behind the regionlooprefine opt-in (default-OFF, settable [PROPOSAL] angr-true-1804-x86-64-8a690c: condition-aware if/else tail dedup (S8 structuring) #53). Wired end-to-end: Architecture/seam field, option, KUNA_OPTION_NAMES, stages.toml, docs/assertions.md, kuna catalog --check OK.

Parity invariant (the honest result)

The refinement is wired in and actively fires on multi-exit loops (KUNA_RS_DEBUG=1 shows [rs] RI loop head=… + [rs] refined loop head#N). But after it fires, the structurer still reports ok=false and the function falls back to CollapseStructure for the final collapse — because the acyclic convergence around the refined loop is incomplete. The net effect on emitted C is therefore zero: regionlooprefine off and on produce byte-identical output. So make test stays PARITY OK 675/675 with regionstructure default-ON.

Convergence stage test

tests/stages/regionstructure-loop-refine.xml — a multi-exit for loop (mexit: early return -1 + break, x86-64 GCC -O1) where the new RI projection is consumed and refine_loop_edges fires. Asserts the emitted C is byte-identical across both passes (off and on): same do { … } while (…) body, both early returns, ZERO gotos — the increment's parity invariant proved end-to-end.

Remaining path to the goto win (documented as Inc 6 — NOT in this PR)

The refinement firing without a structuring win localizes the goto-reduction blocker precisely: it is not the loop schemas (complete, Inc 3) and not the refinement wiring (it fires — this PR). It is the acyclic convergence around the refined loop. Closing it (a genuinely deep, multi-increment effort) needs:

  1. Complete the acyclic schema set — the false-clause if-then (needs a negateCondition orientation flip in the structurer) and the no-exit if-then (an if-then region with no fall-through successor). kuna's structural ITE handles only the symmetric two-arm shape today. Until these collapse, the body the refinement frees stays multi-component and try_fold_loop never sees the single-block loop it needs.
  2. Continuation hoisting (SAILR-style) — hoist a virtualized exit's continuation region into the loop's structured successor before re-running the acyclic cascade, so the freed graph converges to a single root (ok=true) instead of fragmenting back to CollapseStructure.

Only then does a refined loop fold to a structured root and the virtualized secondary-exit gotos become break;/continue; — a measurable goto reduction over Ghidra on the parse_str-class functions that motivated the work. See docs/region-structurer-roadmap.md → Inc 5b section for the full write-up.

Gates (regionstructure default-ON, regionlooprefine default-OFF)

Gate Result
make test 675/675 PARITY OK
make test-stages 208/208 PARITY OK (203 + 5 new)
make rust-test green
kuna catalog --check OK

Notes

  • Merged origin/main (brought in eh_frame_full/operand_refs/funcstart_patterns settables + the catalog-fixture refresh for regionstructure default-on); resolved the settable-count bookkeeping conflicts (49 → 53 settables, fixture regenerated, kuna-catalog counts at angr=15 / structure-recovery=9).
  • docs/baseline.json untouched.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac

mahaloz and others added 4 commits June 26, 2026 16:52
…refine, default-off)

Add the `regionlooprefine` opt-in (default-OFF) that extends the region
structurer (`regionstructure`) with cyclic loop-successor refinement — the read
side of angr `RegionIdentifier`'s omitted
`_refine_loop_successors_to_guarded_successors` / `_ensure_jump_at_loop_exit_ends`
(the `force_loop_single_exit` path).

What it does:
  * `KunaRegionIdentifier::cyclic_loops()` projects each identified cyclic region
    onto basic-block start addresses: (head, refined loop body, exit/successor
    frontier).  This exposes the region identifier's correct loop-body analysis
    (which absorbs the dominated switch-case successors a structural natural-loop
    walk on the sblocks graph misses).
  * `region_structurer::refine_loop_edges` (re-enabled, RI-grounded): for a
    multi-exit / multi-latch / mid-entry loop the base cyclic schemas could not
    fold, virtualize the SECONDARY exits and latches (keeping one normal exit +
    one primary latch) so the loop folds into a structured while/do-while/inf-loop;
    the existing scopeBreak / loop-construction passes then lower the virtualized
    loop-exit/back-edge gotos to break;/continue;.  Only loops the base schemas
    declined to fold are touched, so reducible code stays byte-identical.

Gating + parity: the refinement only fires when BOTH `regionstructure` and
`regionlooprefine` are on; OFF (default) it is dead code, so `make test` stays
PARITY OK 675/675 (docs/baseline.json untouched).  Wired end-to-end:
Architecture/seam field `region_loop_refine`, option `regionlooprefine`,
KUNA_OPTION_NAMES, stages.toml, docs/assertions.md regenerated, catalog --check OK.

Gates: make test 675/675 PARITY OK; make test-stages 203/203 PARITY OK (kuna-catalog
counts bumped for the +1 settable); make rust-test green (settable_count 49->50 and
the catalog-shape tests updated).

NOTE: this is the infrastructure increment.  A goto win on tr_O2.o::parse_str is
NOT yet realized — see the follow-up analysis commit: the region structurer's
ACYCLIC convergence (its limited sequence/ITE schemas + virtualize fallback
fragment the graph and fall back to CollapseStructure) blocks any loop-refinement
win, on small and large functions alike, before the loop schemas can matter.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Brings in the analysis-tier features that landed on main since this branch was
cut (eh_frame_full / operand_refs / funcstart_patterns settables, +3) plus the
catalog-fixture refresh for regionstructure default-on. Merged the +1
regionlooprefine settable on top: settable count 49 -> 53, stage_catalog.json
fixture regenerated, kuna-catalog.xml angr/structure-recovery counts kept at
15/9. Gates green: make test 675/675 PARITY OK, make test-stages 203/203 PARITY
OK, make rust-test green, kuna catalog --check OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…in deferred)

Land the parity-safe portion of the loop-successor refinement increment on top
of the regionlooprefine infrastructure (commit 55764be, now merged with main):
a convergence stage test proving the new machinery is byte-safe and active, and
the roadmap section documenting the remaining goto-win path as a future
increment.

What this adds:
  * tests/stages/regionstructure-loop-refine.xml — a multi-exit `for` loop
    (mexit: early `return -1` + `break`, x86-64 GCC -O1) where the new RI
    loop projection (`KunaRegionIdentifier::cyclic_loops`) is consumed and
    `refine_loop_edges` FIRES (verified `[rs] RI loop head=…` +
    `[rs] refined loop head#N` under KUNA_RS_DEBUG). Asserts the emitted C is
    byte-identical with `regionlooprefine off` and `on` across both passes
    (same do-while body, both early returns, ZERO gotos) — the increment's
    parity invariant: the refinement is wired in and active at zero output risk.
  * docs/region-structurer-roadmap.md — Inc 5b section: what landed (RI
    projection + force_loop_single_exit refinement, default-OFF, parity-safe
    convergence that fires-but-falls-back) and the REMAINING goto-win path as
    the deferred Inc 6 — complete the acyclic schema set (false-clause if-then,
    no-exit if-then) + SAILR-style continuation hoisting, so a refined
    multi-exit loop folds to a structured root (ok=true) instead of falling
    back to CollapseStructure. Status header updated to LANDED & DEFAULT-ON.
  * docs/baseline-stages.json — +5 keys for the new stage test (203 -> 208).
  * kuna-base xml corpus-count test bumped 145 -> 146 (the +1 stage file).

Gates (regionstructure default-ON, regionlooprefine default-OFF):
  make test         675/675 PARITY OK
  make test-stages  208/208 PARITY OK
  make rust-test    green
  kuna catalog --check OK

The goto win itself (a measurable goto reduction on parse_str-class functions)
is NOT in this increment — it needs the acyclic-schema completion +
continuation hoisting documented as Inc 6. This lands the parity-safe
infrastructure + a clear roadmap for the rest.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
…loop-refine

# Conflicts:
#	decompiler/crates/kuna-base/src/xml.rs
#	decompiler/crates/kuna-decomp/src/p0_knowledge/kuna_stages/tests.rs
#	decompiler/crates/kuna-decomp/tests/catalog_bytecompat.rs
#	docs/baseline-stages.json
#	tests/stages/kuna-catalog.xml
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