Skip to content

[PROPOSAL] angr-phoenix-natural-loop-region-1c12bf: recover head-in-body natural loops (S7 structuring) - #44

Closed
mahaloz wants to merge 1 commit into
mainfrom
feat/angr-phoenix-natural-loop-region-1c12bf
Closed

mahaloz wants to merge 1 commit into
mainfrom
feat/angr-phoenix-natural-loop-region-1c12bf

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

[PROPOSAL] phoenix-natural-loop-region: recover head-in-body natural loops (angr Phoenix)

Status: proposal / draft — no engine code changed. Human go/no-go requested on
which implementation path to fund.

The problem

angr's decompiler structures sub_442300
(test_decompiling_phoenix_natural_loop_region_head_in_body, 0x442300, a Go Windows
PE) as a single clean while (true) { … continue; … } with exactly one goto.
kuna renders the same function with 31 goto/label_ tokens: the natural-loop
head at 0x4425a0 becomes label_4425a0: with goto label_4425a0; back-edges, and
the loop body is scattered across ~10 more labels. See
docs/features/phoenix-natural-loop-region-1c12bf/analysis.md,
angr-vs-kuna.txt, kuna-full.txt.

(kuna currently loads this binary only under --experimental-formats; the default
loader rejects the PE. A stage testcase would therefore need a synthesised bytechunk
of just the loop CFG, not the whole Go function.)

angr reference

  • Pass/class: RegionIdentifier (Phoenix structuring),
    angr/analyses/decompiler/region_identifier.py — natural-loop region construction
    where the loop head node is also reachable inside the loop body.
  • angr 9.2.213.

Why it is large (not a single Action/Rule)

kuna's printed C comes from the upstream Ghidra collapse-based structurer
(s8_structure/blockaction.rs: CollapseStructure / LoopBody / TraceDAG), not
from a region identifier. Unlike kuna_loweredswitch.rs — which manufactures an S2
BRANCHIND+JumpTable artifact upstream and lets the existing structurer+printer
emit the switch — there is no single artifact to inject here; the loop-recovery
logic lives inside the structurer. The decider subagent returned scope: "large".

Two candidate implementation paths (pick one to fund)

Path A — extend the collapse structurer's loop detection (in-place)

Teach blockaction.rs LoopBody/CollapseStructure to recognise the head-in-body
natural loop and prefer a loop-header collapse over emitting a back-edge goto,
gated by a new option naturalloophead on|off (default off).

  • Pros: stays in the engine kuna already uses to print C; no emit-path rewrite;
    the option can be ablated against the 675-datatest corpus like every other feature.
  • Cons: touches S7/S8 structuring core (loop body/loop-edge selection). High
    regression risk across the datatest corpus; requires careful guards so it only fires
    on the head-in-body shape. Multi-step: (1) detect the shape on the collapsed graph,
    (2) reorder/relabel the loop-edge so the header collapses, (3) verify goto/label
    count drops with no datatest churn.

Path B — wire the ported RegionIdentifier into emit

Promote s7_regions/kuna_regionid.rs (today analysis-only) into a real structuring
pass that produces the region tree the printer consumes, gated by the new option.

  • Pros: reuses the already-ported, already-faithful angr algorithm — the
    head-in-body case is already handled in that port; closest to "do what angr does."
  • Cons: a new pass type + new emit infrastructure (region-tree → C printer
    adapter, getStart()/lastOp() block-graph adapter that is currently SEAM'd off).
    The largest of the two; effectively a second structurer.

Speed / risk assessment

  • Speed: the target function is large; region identification is ~O(n²) (the port
    carries 2n²+64 guard caps). Either path must measure decompile wall-time off vs on
    (Hard rule 6) and stay within the +5% budget, else ship default-OFF opt-in.
  • Risk: Path A risks datatest-corpus churn (it edits the structurer everyone uses);
    Path B risks correctness drift between two structurers but isolates blast radius
    behind the option (default-off, only the gated path uses the region tree).
  • Recommendation: Path A as a narrow, heavily-guarded gated structurer tweak is
    the smaller bet if the head-in-body shape can be detected cheaply; Path B is the
    faithful-to-angr bet if a second structurer is acceptable. Human decision requested.

Proposed option

option naturalloophead on|off (default off), change_kind = structure-recovery,
source_decompiler = angr,
inspiration = "test_decompiling_phoenix_natural_loop_region_head_in_body; RegionIdentifier (Phoenix); 0x442300".

What this PR contains

Analysis + reproduction artifacts + this proposal only. No kuna_*.rs, no option
registration, no stage test, no baseline change.

…atural loops

angr's RegionIdentifier (Phoenix) structures sub_442300 (Go Windows PE, 0x442300)
as a single clean while(true) with continue and one goto; kuna emits 31 goto/label
tokens, rendering the natural-loop head at 0x4425a0 as a goto target instead of a
loop header.

The gap lives in kuna's S7/S8 collapse-based structurer (blockaction.rs); unlike
loweredswitch there is no single upstream artifact to inject, so it cannot be a
single option-gated Action/Rule. Decider verdict: scope=large. Escalated as a
[PROPOSAL] draft PR (analysis + reproduction artifacts only, no engine change) for
human go/no-go on path A (extend collapse structurer) vs B (wire in the ported
RegionIdentifier).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
mahaloz added a commit that referenced this pull request Jun 25, 2026
…dmap

Append an "Inc 1 - implementation status & findings" section recording: the
landed parity-safe seam (sequence + structural-ITE + SAILR virtualize, RI over
the real CFG, honest-partial fallback), and the empirical finding that the
roadmap's "one fewer goto on if(c){A}else{B}->join->tail" target does not
reproduce - Ghidra's CollapseStructure already emits zero gotos on all acyclic
shapes tested (25+, gcc -O0/1/2 + clang), emitting only minimal crossing-edge
gotos that need Inc 2 ITE. The goto-count reduction belongs to the cyclic
increments (Inc 3 loops: the PR #44 / 1after909 multi-exit-cleanup family).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz

mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing — folded into the loop-refinement follow-up (PR #95 landed the parity-safe infra; the goto-win for these irreducible/multi-exit loops is documented as Inc-6 in docs/region-structurer-roadmap.md: acyclic-schema completion + SAILR continuation hoisting).

@mahaloz mahaloz closed this Jun 26, 2026
@mahaloz
mahaloz deleted the feat/angr-phoenix-natural-loop-region-1c12bf branch August 9, 2026 02:45
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