Skip to content

taildup: SAILR tail duplication goto reduction - #103

Merged
mahaloz merged 3 commits into
mainfrom
feat/taildup
Jun 26, 2026
Merged

mahaloz merged 3 commits into
mainfrom
feat/taildup

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What

Implements proposal #54 — angr SAILR goto-reduction by return-tail
duplication when the tail contains a call
. New ActionTailDup (option
taildup, default-OFF), the third SAILR goto-reduction pass.

This is the genuinely distinct shape the proposal targets (morton
my_message_callback): a shared epilogue free(v6); return; reached by a goto
from each command arm. angr emits zero gotos by duplicating that epilogue into
each arm.

Why it is NOT covered by the two merged siblings

pass tail kind calls in tail morton?
gotoreduce (#57) return tail rejected (any CALL → decline) ✗
crossjumprevert (#92) non-return fall-through tail ≤1 allowed ✗ (declines return-ending tails)
taildup (this) return tail ≤2 allowed ✓

The morton epilogue is a return tail containing a call — it falls in the gap
between the two. Verified: with BOTH gotoreduce on AND crossjumprevert on,
the morton goto+label remain; only taildup on removes them (1 goto / 1 label
→ 0 / 0, matching angr). It mirrors angr ReturnDuplicatorBase, whose budget is
calls (max_calls_in_regions = 2), not call-rejection
(angr/analyses/decompiler/optimization_passes/return_duplicator_{base,low}.py).

Before / after (morton my_message_callback, option taildup on)

// before (default / off): 1 goto + 1 label
        else {
          v5 = strlen("Invalid Command");
          mosquitto_publish(a0,0,"app/error",v5 & 0xffffffff,"Invalid Command",1);
          goto label_4041d6;
        }
        ...
label_4041d6:
  free(v6);
  return;

// after (taildup on): 0 goto + 0 label
        else {
          v2 = sub_4038a0(0x40d019);
          sub_403a00(a0,0,0x40d029,v2,0x40d019,1);
          /* WARNING: taildup: duplicated return-call tail to remove goto */
          free(v6);
          return;
        }

How

Runs once after ActionFinalStructure (right after gotoreduce). Finds an
unstructured goto T where T is a bounded single-successor bb chain ending in
return (≤3 blocks / ≤12 printed ops / ≤2 calls, no STORE), and inlines the
tail. Two source shapes, each with the matching existing surgery:

  • bare BlockIf if (cond) goto T → if (cond) { <tail> }
    (kuna_inline_return_tail, gotoreduce's helper);
  • BlockGoto arm { stmts; goto T } → { stmts; <tail> }
    (kuna_inline_crossjump_tail, crossjumprevert's helper) — the morton case.

The tail is fresh BlockCopy leaves over the same bblocks (print-tree
duplication: no p-code cloned, SSA/def-use untouched). Sound because the tail
returns — a closed path, so the duplicated call is reached on exactly one
structured path (the early-return arm), never both; no convergence precondition
needed (unlike crossjumprevert). Each duplication is logged via
Funcdata::warning (taildup: duplicated return-call tail to remove goto).

Fully registered: stages.toml settable row (count 60→61), options.rs name,
architecture flag (field/ctor/reset/option-set/seam-copy), seams.rs field,
universalaction.rs registration, ElementId 4106. Catalog fixture +
kuna-catalog.xml bounds + baseline-stages.json (222→225) +
docs/assertions.md + action-dump oracle all updated.

Default + speed

  • Default OFF (opt-in), matching its two siblings. Ablation: option taildup on forced across all 83 datatest files keeps the corpus 675/675 and the
    rendered C byte-identical — the upstream regression corpus has no
    shared-free();return;-via-goto epilogue, so it never fires there (0/675
    change). Per the proposal, tail duplication changes rendering on real functions,
    so default-OFF is the safe, sibling-consistent choice even though it is correct.
  • Speed: no measurable impact (cheap post-structuring scan) — morton
    single-func ~0.17 s off/on; full datatest corpus 6.8 s off vs 6.7 s on (noise).

Tests / gates

  • New two-pass stage test
    tests/stages/ghangr-morton-my-message-callback-bfd2fa.xml on a loadable x86-64
    ELF (morton) bytechunk: pass 1 (off) = 1 goto + 1 label (bug); pass 2 (on) = 0
    goto / 0 label, tail duplicated.
  • make test PARITY OK (675/675 — default-off byte-identical to upstream).
  • make test-stages PARITY OK (225/225, +3 morton assertions).
  • make rust-test 0 failures (258 ok — incl. catalog byte-compat, action
    order, golden, .sla parity).
  • kuna catalog --check OK; docs/assertions.md regenerated.

Does not touch docs/baseline.json.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac

mahaloz and others added 3 commits June 26, 2026 20:27
The third SAILR goto-reduction pass (angr ReturnDuplicatorLow /
ReturnDuplicatorBase max_calls_in_regions). Fills the gap between the two
merged siblings, both of which decline the morton my_message_callback shape:

  * gotoreduce (#57): return tail, but REJECTS any CALL in the tail, so it
    cannot handle a `free(p); return;` epilogue;
  * crossjumprevert (#92): permits a call but only for a NON-return
    fall-through tail; it explicitly declines a tail that ends in `return`.

The morton epilogue is a return tail CONTAINING a call (`free(v6); return;`)
reached by a `BlockGoto` arm (`if (c) { ...; goto label; }`), which lands in
neither. ActionTailDup (option `taildup`, default-OFF) duplicates exactly that:
a bounded (<=3 blocks / <=12 printed ops / <=2 calls, no STORE) return-call
tail, inlined in place of the goto. Two source shapes handled — a bare
`BlockIf` if-goto (gotoreduce's kuna_inline_return_tail surgery) and a
`BlockGoto` arm (crossjumprevert's kuna_inline_crossjump_tail surgery). Sound
because the tail returns (closed path: the duplicated call is reached on
exactly one structured path, never both — no convergence precondition needed).
Each duplication is logged via Funcdata::warning.

Verified distinct: with BOTH gotoreduce AND crossjumprevert on, the morton
goto+label remain; only taildup removes them (0 goto / 0 label, matching angr).

Fully registered: stages.toml settable row + count 60->61, options.rs name,
architecture flag (field/ctor/reset/option-set/seam-copy), seams.rs field,
universalaction.rs registration (after gotoreduce), ElementId 4106.
kuna-catalog.xml bounds + baseline-stages.json regenerated (222->225),
docs/assertions.md regenerated. catalog --check OK.

Gates: make test PARITY OK (675/675, default-off byte-identical to upstream);
make test-stages PARITY OK (225/225, +3 morton taildup two-pass assertions on
a loadable x86-64 ELF bytechunk).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Wire the new `taildup` option/action through the cross-checking tests:
  * kuna-base xml.rs: corpus file count 151->152 (the new stages XML).
  * kuna_stages tests: no-live-reader allowlist += taildup; catalog row
    trailing-comma count 59->60.
  * catalog_bytecompat: fixture settable count 60->61; recaptured
    tests/fixtures/stage_catalog.json (taildup row after crossjumprevert).
  * universalaction action-dump oracle + verify_w8_fw_universalaction:
    nonblank 263->264, the structuring tail now
    finalstructure -> gotoreduce -> taildup -> ifelseflatten -> crossjumprevert.

Ablation: with `option taildup on` forced across all 83 datatest files, the
corpus stays 675/675 and the rendered C is byte-identical (the upstream Ghidra
regression corpus has no shared `free();return;`-via-goto epilogue, so the pass
never fires there) — 0/675 change. Kept default-OFF to match its two siblings
(gotoreduce, crossjumprevert), per the proposal's "expected to ship default-OFF
opt-in even if correct" (tail duplication changes rendering on real functions).

Speed: no measurable impact (cheap post-structuring scan) — morton single-func
~0.17s off/on; full datatest corpus 6.8s off vs 6.7s on (within noise).

All three gates green: make test PARITY OK (675/675), make test-stages PARITY
OK (225/225), make rust-test 0 failures (258 ok). catalog --check OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Reconcile the taildup feature with main after the FID PRs (#97-#101) and
switchmultipred (#102) landed:

- ElementId: taildup collided with switchsharedcase (both 4106 on the merged
  tree). Renumbered taildup -> 4108 (next free above ifelseflatten's 4107).
- Count unions (taildup +1 settable, switchmultipred +1, fid +1 vs merge-base):
  settable 60 -> 63, surface stays 97 (taildup adds no surface row),
  corpus files 151 -> 153, universalaction leaves -> 264 (252 C++ + 5 kuna),
  bytecompat fixture 63, stages.toml header counts.
- kuna-catalog.xml provenance: source_decompiler angr 22, structure-recovery 12
  (+taildup), opt-in-tool 11 (+switchmultipred), use_when/change_kind max 64
  (settables + returnpair double-count in `stage catalog`).
- Regenerated docs/assertions.md (no change) and docs/baseline-stages.json
  (230 passing keys).

Gates: catalog --check OK; make test PARITY OK 675/675 (baseline.json untouched);
make test-stages PARITY OK 230/230; make rust-test green.
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