Skip to content

regionstructure Inc 5: short-circuit/condition folding - #79

Merged
mahaloz merged 1 commit into
mainfrom
feat/regionstructure-inc5
Jun 26, 2026
Merged

mahaloz merged 1 commit into
mainfrom
feat/regionstructure-inc5

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

regionstructure Inc 5 — short-circuit / condition folding (&& / || compound conditions)

Extends the regionstructure option (default OFF) so the region structurer folds
cascading short-circuit conditions into a single compound &&/|| condition,
matching Ghidra byte-for-byte. This is the missing piece that makes the region
structurer match Ghidra on compound conditions — the default-on blocker.

The bug this fixes

With --option regionstructure on, a cascading if (A) { if (B) { body } } rendered as
a nested if, where Ghidra folds the condition:

// regionstructure ON (before this PR, WRONG):
else if (b == 0xc9) { if (a <= 299) { printf("Else"); } }

// Ghidra / regionstructure ON (after this PR):
else if ((b == 0xc9) && (a <= 299)) { printf("Else"); }

This was the only datatest (tests/datatests/elseif.xml, Else-if #14) where the
region structurer diverged from Ghidra.

What landed

region_structurer::match_acyclic_short_circuit_conditions (→ try_block_or) is a
faithful port of Ghidra CollapseStructure::ruleBlockOr (blockaction.cc:1321) — the
kuna analog of angr phoenix._match_acyclic_short_circuit_conditions (types a–d). When a
2-out condition bl has a single-in, non-complex, 2-out successor orblock that
reconverges through a shared clause, the pair folds into a BlockCondition via
BlockGraph::new_block_condition, which picks CPUI_INT_AND/CPUI_INT_OR from the edge
orientation exactly as Ghidra does — so the rendered &&/|| is byte-identical to
the default CollapseStructure form.

  • Runs as an up-front fixpoint before the sequence/ITE/loop cascade, mirroring
    Ghidra's collapseAll order (collapseConditions, which is only ruleBlockOr, runs
    before collapseInternal).
  • The isComplex gate reuses BlockBasic::isComplex (precomputed complex_blocks, the
    same set CollapseStructure consumes); orientation uses the existing
    negate_condition_rec (recording the deferred data-flow flips).
  • Honest-partial-safe: a node that does not match the cascade is left for the
    sequence/ITE/virtualize schemas — never a panic, never a whole-function abort.

Parity & default-on readiness

  • Default-OFF: make test 675/675 PARITY OK (byte-identical).
  • Default-ON (verified by temporarily flipping the default, then reverting — the flip
    is not in this PR): the full 675-assertion suite is byte-identical to OFF
    (0/675 divergences, PARITY OK)
    . The per-function C of the elseif binary is
    byte-identical between the OFF (CollapseStructure) and ON (region structurer) passes.
    → the future Inc 6 default-on flip is clean.

Tests / gates

  • New stage test tests/stages/regionstructure-shortcircuit.xml asserts the &&-folded
    else if ((b == 0xc9) && (a <= 299)) form across both passes (off + on), plus a
    negative (the inner a <= 299 is not left as a standalone nested if).
  • make test → 675/675 PARITY OK
  • make test-stages → 203/203 PARITY OK (baseline-stages.json regenerated 200 → 203)
  • make rust-test → green (xml.rs corpus count bumped 144 → 145)
  • kuna catalog --check → OK (no new option; docs/assertions.md unchanged)
  • docs/baseline.json → untouched

Speed

Measured decomp_test_dbg on the elseif binary, 30 runs each: OFF 5.064s vs ON 5.082s
(~169 ms/run) — a 0.4% delta, within noise. No measurable regression (the fold only runs
under the option; the up-front fixpoint is bounded by the same round_cap as the main loop).

Files

  • decompiler/crates/kuna-decomp/src/s8_structure/region_structurer.rs — the schema
    (match_acyclic_short_circuit_conditions / try_block_or / is_complex /
    compute_complex_blocks / with_complex_blocks) + up-front fixpoint wiring.
  • decompiler/crates/kuna-base/src/xml.rs — corpus count 144 → 145.
  • tests/stages/regionstructure-shortcircuit.xml — new both-directions stage test.
  • docs/baseline-stages.json — +3 passing keys.
  • docs/region-structurer-roadmap.md — Inc 5 status section.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac

…und conditions)

Port Ghidra's `CollapseStructure::ruleBlockOr` (the `collapseConditions`
pre-pass) into the region structurer as `match_acyclic_short_circuit_conditions`
(the kuna analog of angr `phoenix._match_acyclic_short_circuit_conditions`,
types a-d).  When a 2-out condition `bl` has a single-in, non-complex, 2-out
successor `orblock` that reconverges through a shared clause, the cascading
`if (A) { if (B) { body } }` shape folds into a single `BlockCondition` carrying a
compound `A && B` / `A || B` condition via `BlockGraph::new_block_condition`
(which picks `CPUI_INT_AND`/`CPUI_INT_OR` from the edge orientation exactly as
Ghidra does, so the rendered `&&`/`||` is byte-identical).

The fold runs as an up-front fixpoint before the sequence/ITE/loop cascade,
mirroring Ghidra's `collapseAll` order (`collapseConditions`, which is only
`ruleBlockOr`, runs before `collapseInternal`).  The `isComplex` gate reuses
`BlockBasic::isComplex` (precomputed `complex_blocks`, the same set
`CollapseStructure` consumes); orientation uses the existing `negate_condition_rec`.

This closes the only datatest where the region structurer (`regionstructure on`)
diverged from Ghidra: `elseif.xml` Else-if #14, which now folds to the identical
`else if ((b == 0xc9) && (a <= 299))` instead of a nested `if`.  With the option
temporarily flipped default-ON, the full 675-assertion datatest suite is
byte-identical to default-OFF (0/675 divergences, PARITY OK) -- so the future
default-on flip is clean.

Honest-partial-safe: a node that does not match the cascade is left for the
sequence/ITE/virtualize schemas (never a panic).  Default-OFF -> 675/675
byte-identical; ON-vs-OFF decompile speed unchanged.

New stage test `tests/stages/regionstructure-shortcircuit.xml` asserts the
`&&`-folded form across both passes (off=CollapseStructure, on=region structurer);
baseline-stages.json regenerated (200 -> 203), xml.rs corpus count bumped
(144 -> 145), roadmap Inc 5 status section added.  docs/baseline.json untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
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