Skip to content

tee-o2-x2nrealloc: structural no-return propagation (option noreturn_propagate) - #52

Merged
mahaloz merged 4 commits into
mainfrom
feat/angr-tee-o2-x2nrealloc-6981e7
Jun 25, 2026
Merged

mahaloz merged 4 commits into
mainfrom
feat/angr-tee-o2-x2nrealloc-6981e7

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

noreturn_propagate: structural no-return propagation (angr CFGFast analog)

Closes the angr gap test_decompiling_tee_O2_x2nrealloc::x2nrealloc (coreutils
tee_O2, x86_64). Implements the approved
proposal; analysis in
analysis.md.

The gap

angr renders x2nrealloc cleanly; kuna emits invalid C — a spurious
while(true) loop + goto label_5813 + dead stack-spill artifacts, 38% longer
(39 vs 24 loc). Root cause: xalloc_die is structurally no-return (its body
unconditionally ends in error(...) then abort(), and kuna already models
abort no-return), but kuna never propagates that to xalloc_die — so the
call xalloc_die in x2nrealloc is assumed to return, and at -O2 (cold error
path laid out last) the bytes after the call become a spurious fall-through
back-edge. Marking xalloc_die no-return by hand (option noreturn xalloc_die)
collapses the output to the angr shape, confirming the mechanism is exactly
no-return propagation.

The mechanism

A new kuna-analysis Listing-consumer pass
(s1_noreturn_propagate::NoReturnPropagatePass, the kuna analog of angr's CFGFast
call-graph no-return propagation). It seeds the terminal set from the Known
no-return list and concludes a function no-return when its last real
instruction
(last by address, skipping trailing NOP alignment padding) is a
CALL/tail JMP to an already-no-return callee, with no RETURN path, no
computed jump, and no branch escaping the reachable body
— iterated to a
fixpoint, with no evidence threshold. This is the key difference from the
existing noreturn_disc, whose ≥3-call-site rule and "valid fall-through after the
call" predicate both miss xalloc_die (one cold call site; call abort followed
by valid NOP padding).

It emits the existing NoReturnFact → the existing set_function_no_return commit
seam → the inherited flow.rs artificial-halt path. No new commit arm, no S7
work.
Soundness: with no RETURN and the only reachable exit a call/jump to an
already-no-return function, the function cannot return — strictly more conservative
than angr's propagation.

The option (LLM-discoverable)

option noreturn_propagate on — default-OFF opt-in, requires option listing on (it reads the Listing/xref tier). Provenance in stages.toml:
source_decompiler="angr", change_kind="structure-recovery",
inspiration="test_decompiling_tee_O2_x2nrealloc; angr CFGFast no-return propagation …; x2nrealloc".

kuna decompile tee_O2 x2nrealloc --option listing on --option noreturn_propagate on

Why a proposal (large), and testing

The no-return flag is consumed pre-pipeline during initial flow generation
(s2_lift/flow.rs:1838, &self), so a kuna_loweredswitch.rs-style in-pipeline
Action runs too late — this must be an analysis-tier pass. And the mandated firing
tests/stages/*.xml cannot be authored: the XML decomp_test_dbg bytechunk path
never runs analysis passes or builds the Listing (exactly why
noreturn_known/noreturn_disc have no stages XML test). Coverage instead:

  • Cross-crate e2e verify_noreturn_propagate.rs over a vendored differential
    fixture (noreturn_propagate_x86_64: a custom my_die() no-return wrapper called
    ONCE — below noreturn_disc's ≥3 threshold — ending in call abort + NOP
    padding). Three assertions: propagation eliminates the dead code; the existing
    noreturn_disc consumer does not fix it (the differential proving a new
    capability); the wrapper itself is concluded no-return.
  • kuna-analysis pass-identity unit tests.

Ablation / parity / speed

  • Ablation 0/675 — the XML datatest path never runs analysis passes; make test stays PARITY OK. Kept default-OFF (real-ELF-only flow heuristic) —
    no DIV entry, default output byte-identical.
  • make test-stages PARITY OK (the two KUNA-CATALOG provenance-count
    assertions re-pinned for the new angr/structure-recovery row; baseline
    regenerated).
  • make rust-test green except the pre-existing verify_w10_proto_unlock
    failure (reproduces on the unmodified branch; unrelated to this change).
  • Speed (x2nrealloc, repeat 5): off=154.6 ms / on=151.1 ms (−2.21%, within
    the 5% budget). The pass is one bounded call-graph fixpoint over an
    already-built Listing.

🤖 Generated with Claude Code


Before / after — x2nrealloc in tee_O2

Real captured kuna output on the function this feature was built for, with option noreturn_propagate flipped. Ported from angr — test_decompiling_tee_O2_x2nrealloc; angr CFGFast no-return propagation (returning / _iteratively_analyze_function_features); x2nrealloc.

metric noreturn_propagate off (default) noreturn_propagate on
gotos 1 0 ⬇️
labels 1 0 ⬇️
loc 39 27 ⬇️
switches 0 0
speed 154.55 ms 151.14 ms — Δ -2.21% (budget 5.0%)
kuna — before (option noreturn_propagate off)
void x2nrealloc(void *a0,uint8 *a1,uint8 a2)

{
  char v1 [16];
  uint8 v2; // rax
  uint8 v3;
  void *v4;
  void *v5; // rsp
  uint8 *v6;
  bool v8; // cf
  
  v4 = PTRSUB(RSP,0);
  while( true ) {
    v7 = a1;
    *(uint8 **)&v4[-8] = v6;
    *(uint8 *)&v4[-0x10] = v3;
    v5 = &v4[-0x18];
    v3 = *v7;
    if (a0 == (void *)0x0) break;
    v2 = (v3 >> 1) + 1;
    v8 = CARRY8(v3,v2);
    v3 = v3 + v2;
    if (!v8) goto label_5813;
    *(void *)&v4[-0x20] = 0x5856;
    a1 = v7;
    xalloc_die(v4[-0x20]);
    a2 = 1;
    v4 = v5;
    v6 = v7;
  }
  if (v3 == 0) {
    v1[8] = 0;
    v1[0] = a2;
    v3 = SUB168((ZEXT816(0) << 0x40 | ZEXT816(0x80)) / v1,0) + (uint8)(0x81 <= a2);
  }
label_5813:
  *(void *)&v4[-0x20] = 0x581e;
  xreallocarray(a0,v3,a2,v4[-0x20]);
  *v7 = v3;
  return;
}
kuna — after (option noreturn_propagate on)
void x2nrealloc(void *a0,uint8 *a1,uint8 a2)

{
  uint8 n;
  char v1 [16];
  uint8 v2; // rax
  bool v3; // cf
  
  n = *a1;
  if (a0 == (void *)0x0) {
    if (n == 0) {
      v1[8] = 0;
      v1[0] = a2;
      n = SUB168((ZEXT816(0) << 0x40 | ZEXT816(0x80)) / v1,0) + (uint8)(0x81 <= a2);
    }
  }
  else {
    v2 = (n >> 1) + 1;
    v3 = CARRY8(n,v2);
    n = n + v2;
    if (v3) {
                    /* WARNING: Subroutine does not return */
      xalloc_die();
    }
  }
  xreallocarray(a0,n,a2);
  *a1 = n;
  return;
}
angr reference (the goal, v9.2.213)
unsigned long long x2nrealloc(unsigned long long a0, unsigned long long *ptr, unsigned long long a2)
{
    unsigned long long v1;  // rbx
    unsigned long v2;  // rax
    unsigned long long v4;  // rbx
    unsigned long long ptr;  // rax

    v1 = *(ptr);
    if (a0)
    {
        v2 = (v1 >> 1) + 1;
        v4 = v1 + v2;
        if ((char)__CFADD__(v1, v2))
            xalloc_die(); /* do not return */
    }
    else
    {
        v4 = v1;
        if (!v4)
            v4 = (unsigned long long)(128 / a2) + (a2 > 128);
    }
    ptr = xreallocarray();
    *(ptr) = v4;
    return ptr;
}

… (noreturn_propagate)

angr renders tee_O2:x2nrealloc cleanly; kuna emits invalid C (spurious
while(true) loop + goto + dead stack spills) because xalloc_die is no-return
(body unconditionally ends in error(); abort()) but kuna never propagates that
fact to xalloc_die. Confirmed: manual `option noreturn xalloc_die` collapses the
output to the angr shape.

The faithful fix is angr's CFGFast no-return call-graph propagation: a new
analysis-tier pass seeding from the known/discovered no-return set and marking
any function whose last reachable insn is a call to a no-return callee, to a
fixpoint. A decider ruled scope=large: the no-return flag is consumed pre-S2-flow
via an &self seam (no loweredswitch-style Action possible), and no firing
tests/stages/*.xml can be authored (the XML path never runs analysis passes).

Proposal only -- no implementation. See docs/features/tee-o2-x2nrealloc-6981e7/.

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

Discovered the real fix (noreturn-by-body inference for usage()) is the EXACT
feature already proposed as `noreturn_propagate` (draft PR #52, from xalloc_die in
tee_O2). This mv_-O2/usage case is a second witness for that feature. Per Hard rule 5
(do not duplicate a covered gap), no competing proposal is spun up; this opportunity
is closed as covered. Reframed findings.md/proposal-v2.md/record.json/PROGRESS.md
accordingly. No code shipped.

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

Closes the angr gap test_decompiling_tee_O2_x2nrealloc::x2nrealloc. xalloc_die is
structurally no-return (body ends unconditionally in error(); abort(), and abort is
already modeled no-return) but kuna never propagates that, so the call xalloc_die in
x2nrealloc is assumed to return and at -O2 the cold-path bytes after it form a spurious
while(true) loop + goto + dead stack spills (invalid C, 38% longer than angr).

New kuna-analysis Listing-consumer pass s1_noreturn_propagate::NoReturnPropagatePass
(gate id noreturn_propagate, default-OFF, requires option listing on) — the kuna analog
of angr's CFGFast call-graph no-return propagation. Seeds the terminal set from the Known
no-return list and concludes a function no-return when its last real instruction (skipping
trailing NOP padding) is a CALL/tail JMP to an already-no-return callee, with no RETURN
path, no computed jump, and no escaping branch — iterated to a fixpoint, with NO evidence
threshold (the difference from noreturn_disc, which misses xalloc_die: one cold call site,
call abort followed by valid NOP padding). Emits the existing NoReturnFact -> the existing
set_function_no_return commit seam (no new commit arm, no S7 work).

Implements the approved docs/features/tee-o2-x2nrealloc-6981e7/proposal.md.

Coverage (no stages XML — analysis tier, like noreturn_known/noreturn_disc): a vendored
differential fixture noreturn_propagate_x86_64 (custom my_die() wrapper called once, ending
in call abort + NOP padding) drives verify_noreturn_propagate.rs (3 assertions incl. the
noreturn_disc-does-not-fix-it differential) + kuna-analysis pass-identity unit tests.

Default-OFF opt-in (real-ELF-only flow heuristic, listing-gated): ablation 0/675, make test
PARITY OK, no DIV entry. Speed x2nrealloc off=154.6ms/on=151.1ms (-2.21%, within budget).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz
mahaloz marked this pull request as ready for review June 25, 2026 05:43
…nion + ordering)

The before/after demo for an analysis-tier option must (a) emit `option` lines
BEFORE `read symbols` — the kuna_analysis passes commit inside read symbols, so an
option set after it is too late (the Rust `kuna decompile` CLI already does this;
the python shim scripts/decompile.py did the opposite, a latent bug); and (b)
apply a companion flag the feature requires (`listing on`) to BOTH sides so the
demo isolates the feature option's own effect.

- scripts/decompile.py: move `option` emission before `read symbols` (match
  kuna-cli/src/decompile.rs). No-option decompiles are byte-identical.
- scripts/pipeline/prdemo.py: add `demo_extra_options` (applied to before+after).
- record.json: demo_extra_options = [["listing","on"]].

With this, the embedded demo shows the real fix: gotos 1->0, labels 1->0, loc 39->27.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@mahaloz mahaloz changed the title [PROPOSAL] angr-tee-o2-x2nrealloc-6981e7: structural no-return propagation (noreturn_propagate) tee-o2-x2nrealloc: structural no-return propagation (option noreturn_propagate) Jun 25, 2026
@mahaloz

mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Great work, this is ready to merge after documenting for another agent:

  1. This is an example of branch flipping that we should implement in Ghidra. Notice how in angr the code looks more linear and instead of showing a0 == (void *)0x0, it just flipped the branch in angr and had if(a0), which is better.

Bring the noreturn_propagate (structural no-return propagation) feature branch up
to current main. noreturn_propagate is opt-in (default-off, no DIV entry).
Conflicts resolved by UNION:
- stages.toml: 42 settable blocks; count assertions updated to 42
- stage_catalog.json fixture: union noreturn_propagate + gopclntab + relocobjects rows
- kuna-catalog.xml / baseline: angr provenance 8 (noreturn_propagate added),
  structure-recovery 4 (noreturn_propagate is structure-recovery)
- PROGRESS.md: noreturn_propagate session kept ahead of the main sessions
- assertions.md + baseline-stages.json regenerated

Gates: catalog OK, make test PARITY OK (675/675), make test-stages PARITY OK (163/163).

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