Skip to content

tee-o2-tail-jumps-4a1f49: recover -O2 tail jumps as tail calls (option tailcalljump) - #59

Merged
mahaloz merged 3 commits into
mainfrom
feat/angr-tee-o2-tail-jumps-4a1f49
Jun 25, 2026
Merged

mahaloz merged 3 commits into
mainfrom
feat/angr-tee-o2-tail-jumps-4a1f49

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

tee-o2-tail-jumps: recover -O2 tail jumps as tail calls (option tailcalljump)

Closes the gap where angr's decompiler beats kuna on
test_decompiling_tee_O2_tail_jumps :: setlocale_null_androidfix
(binaries/tests/x86_64/decompiler/tee_O2, x86-64 PIE, -O2).

See docs/features/tee-o2-tail-jumps-4a1f49/analysis.md
for the full analysis.

The gap

At -O2, a leaf function whose last act is "call X; return" is compiled to a direct
tail jump - jmp X instead of call X; ret. When X is an external symbol the jump
targets the PLT thunk:

setlocale_null_androidfix:
    xor    %esi,%esi          ; arg2 = NULL
    jmp    setlocale@plt       ; tail jump (direct BRANCH) to the thunk

kuna's flow follower runs with the whole address space in-bounds, so it treats the direct
jmp setlocale@plt as ordinary intraprocedural flow and follows it into the PLT thunk.
The thunk's body is jmp qword [rip+GOT] (an indirect jump); jump-table recovery fails on
it, so it is rewritten to a CALLIND through the GOT pointer plus a
"Treating indirect jump as call" warning. The thunk got inlined instead of the jmp
being recognized as a tail call, so the callee renders as (*dat_209f68)(...) rather than
the named setlocale.

The mechanism

A new S2 flow-classification predicate kuna_is_tail_call_branch
(decompiler/crates/kuna-decomp/src/s2_lift/kuna_tailcalljump.rs, ElementId 4100), modeled
exactly on the existing kuna_v850indbranch feature (a FlowEnvironment predicate consulted
inside xref_control_flow, gated by an Architecture bool flag). In the CPUI_BRANCH arm of
FlowInfo::xref_control_flow, when the option is on and a direct branch's target is the entry
of another known function (query_call(dest).is_some(), which includes PLT thunks) and not the
function's own entry, the BRANCH is rewritten to a CPUI_CALL + an artificial RETURN (the
in-file truncate_indirect_jump / setup_callind_specs halt-insert idiom), instead of flowing
into the callee. The callee then resolves by name and the spurious warning / GOT-pointer
indirect call are gone.

(The remaining void / return; vs angr's return setlocale(...) is an S4
return-value-recovery concern - the option-OFF path is also void, so it is inherent to
kuna's wrapper-return recovery, not introduced or fixable by this S2 change.)

The option

option tailcalljump on|off (default off, default-pipeline byte-identical). Flip per
decompilation:

kuna decompile ./tee_O2 setlocale_null_androidfix --option tailcalljump on

It is LLM-discoverable via kuna catalog --json (provenance: source_decompiler=angr,
change_kind=structure-recovery).

Ablation / parity / speed

  • Default-OFF (shipped): byte-identical - make test PARITY OK (675/675), make test-stages PARITY OK (162/162).
  • Default-ON ablation: regresses 2 upstream datatests (Long double #1/#2), so the
    feature ships default-OFF opt-in (no DIV entry).
  • Speed (setlocale_null_androidfix, median of 5): off 259.0 ms / on 264.9 ms (+2.26%,
    within the 5% budget).
  • New firing stage test tests/stages/ghangr-tee-o2-tail-jumps-4a1f49.xml (pass 1 off proves
    the bug; pass 2 on proves the fix).

🤖 Generated with Claude Code


Before / after — setlocale_null_androidfix in tee_O2

Real captured kuna output on the function this feature was built for, with option tailcalljump flipped. Ported from angr — test_decompiling_tee_O2_tail_jumps; angr tail-call handling (direct jmp to a function/PLT thunk); setlocale_null_androidfix.

metric tailcalljump off (default) tailcalljump on
gotos 0 0
labels 0 0
loc 6 5 ⬇️
switches 0 0
speed 259.04 ms 264.9 ms — Δ 2.26% (budget 5.0%)
kuna — before (option tailcalljump off)
void setlocale_null_androidfix(unsigned long a0)

{
                    /* WARNING: Treating indirect jump as call */
  (*dat_209f68)(a0,0);
  return;
}
kuna — after (option tailcalljump on)
void setlocale_null_androidfix(unsigned long a0)

{
  setlocale(a0,0);
  return;
}
angr reference (the goal, v9.2.213)
char * setlocale_null_androidfix(void)
{
    int v1;  // edi

    return setlocale(v1, NULL);
}

…n tailcalljump)

Closes the angr-vs-kuna gap on test_decompiling_tee_O2_tail_jumps ::
setlocale_null_androidfix. At -O2 a leaf function whose last act is "call X;
return" is compiled to a direct tail jump (`jmp setlocale@plt`). kuna's flow
follower treated the direct jmp as intraprocedural flow and followed it INTO the
PLT thunk, whose `jmp qword [GOT]` then failed jump-table recovery and became a
`(*dat_...)(...)` CALLIND + a "Treating indirect jump as call" warning.

New S2 flow-classification predicate kuna_is_tail_call_branch
(kuna_tailcalljump.rs, ElementId 4100, modeled on kuna_v850indbranch): in
FlowInfo::xref_control_flow's CPUI_BRANCH arm, when `option tailcalljump on` and
a direct branch targets another known function's entry (query_call resolves it,
incl. PLT thunks) and not the function's own entry, rewrite BRANCH -> CALL + an
artificial RETURN instead of flowing into the callee. The callee then resolves by
name (setlocale) and the spurious warning / GOT-pointer indirect call are gone.

Default OFF (default-pipeline byte-identical; default-ON regresses 2 datatests
Long double #1/#2). make test PARITY OK 675/675, test-stages 162/162, catalog OK.
Speed +2.26% (within budget). New firing stage test
tests/stages/ghangr-tee-o2-tail-jumps-4a1f49.xml.

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

mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

We need the following changes:

  1. This feature should be on by default, but still have a logger saying that it ran and introduced a new call here
  2. We should document this case for another agent: why are we not returning the setlocale call? Why is our function type wrong? It should be char*. We need to fix this in another agent with this context.

mahaloz and others added 2 commits June 25, 2026 17:38
…low-up)

Reviewer asked to (a) make `option tailcalljump` on-by-default and (b) add a
logger when the pass introduces a new call. Resolved as follows:

- LOGGER (done): when the S2 pass rewrites a tail `jmp <func>` into a CALL it
  now emits `Funcdata::warning("tailcalljump: recovered tail call -> introduced
  call to <dest>")` at the branch site, rendered as a /* WARNING: ... */ comment
  so the introduced call is observable in the output (output-changing-feature
  logging contract). New stage-test assertion #4 asserts it.

- DEFAULT-ON (could NOT ship): flipping `tail_call_jumps` default-true in
  `Architecture::reset_defaults_internal` regresses 2 of the 675 upstream
  datatest assertions (Long double #1/#2), exactly as the original PR's ablation
  predicted. Per the standing rule (never modify docs/baseline.json; default-on
  must not change any datatest assertion), the feature STAYS default-OFF opt-in.
  No DIV entry.

Merge with main (branch was behind): union the option-registration files
(stages.toml settable/surface rows + counts, options.rs KUNA_OPTION_NAMES,
architecture.rs flags, kuna-catalog.xml provenance counts angr 10->11 /
structure-recovery 6->7, baseline-stages.json, assertions.md). Renumbered the
tailcalljump ElementId 4100 -> 4101 (gotoreduce took 4100 on main). Bumped the
merged-tree count tests: stages 91->92 surfaces / 45->46 settables /
21->22 live-readers, catalog byte-compat fixture regenerated (46 settables),
xml-corpus file count 137->138.

Gates: `make test` PARITY OK 675/675; `make test-stages` PARITY OK 175/175;
`make rust-test` green; `kuna catalog --check` OK. Speed: off ~= on (within
noise, well under the 5% budget).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
Resolve union conflicts with main (which landed #60 switchguardbound +
#68/#60 region work). Both opt-in/default-off knobs coexist:

- stages.toml: union [[settable]]/[[surface]] -> surface 91->93, settable
  45->47 (29 stage-model + 15 analysis + 3 loader); header counts updated.
- kuna_stages/tests.rs: surface_count 93, settable_count 47,
  live_value present-for-23 (switch_guard_bound + tail_call_jumps both
  field-backed), catalog-json trailing-commas 46.
- catalog_bytecompat.rs: fixture_has_all_47_settables; stage_catalog.json
  fixture auto-unioned (byte-for-byte test green).
- xml.rs corpus count 138->139 (83 datatests + 56 stage tests: both the
  missing-function-call and tee-o2-tail-jumps stage tests present).
- kuna-catalog.xml: source_decompiler=angr 12, structure-recovery 7,
  opt-in-tool 8.
- PROGRESS.md: union both session entries.
- tailcalljump ElementId stays 4101 (no collision; gotoreduce=4100 on main).

Regenerated docs/assertions.md + docs/baseline-stages.json (180/180).
Gates: kuna catalog --check OK; make test PARITY OK 675/675; make
test-stages PARITY OK 180/180. docs/baseline.json untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz merged commit d0d0f70 into main Jun 25, 2026
mahaloz added a commit that referenced this pull request Jun 25, 2026
Resolve union conflicts with main (which since this branch's base landed
gotoreduce, loopbreak_recovery/DIV-10, switchguardbound/#60, tailcalljump/#59,
macho-arm64e, and more). branchflip (S8 readability, opt-in/default-off,
ElementId-free) coexists with all of them:

- stages.toml: union [[settable]]/[[surface]] -> surface 93->94, settable
  47->48 (30 stage-model + 15 analysis + 3 loader); branchflip inserted
  after stackguard, before loopbreak_recovery; header counts updated.
- options.rs KUNA_OPTION_NAMES: union (branchflip + loopbreak_recovery).
- architecture.rs: union the set_kuna_option arm (branchflip on_off! +
  loopbreak_recovery); branch_flip field/ctor/reset/seam-copy all preserved.
- kuna_stages/tests.rs: surface_count 94, settable_count 48, live_value
  present-for-23 / suppressed-for-25 (branchflip is NOT field-backed),
  catalog-json trailing-commas 47.
- catalog_bytecompat.rs: fixture_has_all_48_settables; stage_catalog.json
  fixture regenerated from the binary (byte-for-byte test green).
- verify_w8_fw_universalaction.rs: nonblank 261 (252 C++ + branchflip +
  gotoreduce + 7 headers); list_action_decompile_oracle.txt re-numbered
  for the branchflip + gotoreduce leaves (byte-equal test green).
- xml.rs corpus count 139->140 (83 datatests + 57 stage tests).
- kuna-catalog.xml: source_decompiler=angr 13, structure-recovery 7,
  opt-in-tool 9.
- PROGRESS.md: union both session sets.

No DIV entry (opt-in/default-off; output byte-identical when off).
Regenerated docs/assertions.md + docs/baseline-stages.json (183/183).
Gates: kuna catalog --check OK; make test PARITY OK 675/675; make
test-stages PARITY OK 183/183. docs/baseline.json untouched.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
mahaloz added a commit that referenced this pull request Jun 25, 2026
Follow-up on PR #59's reviewer note (tee_O2 :: setlocale_null_androidfix):
"why are we not returning the setlocale call? Why is our function type wrong?
It should be char*."

Two independent defects sat behind that one report. This commit fixes the
clean, parity-safe one; the other is a documented larger follow-up.

Defect 1 (FIXED) - missing libc prototype. kuna's ApplyDataArchiveAnalyzer
analog (kuna-analysis::s1_protos, the built-in LIBC prototype table) had no
setlocale entry, so the call's result was an untyped undefined8: `category`
came out `unsigned long` not `int`, the NULL `locale` arg rendered bare `0`,
and main's setlocale(LC_ALL,"") idiom rendered setlocale(6,0x6dc1) (string
literal lost). Fix: one row, `char *setlocale(int, const char *)`. The pass
already parks the PrototypePieces on the callee and ActionDefaultParams types
the callers. Now main -> setlocale(6,""), the NULL arg -> (char *)0x0, and any
wrapper that keeps the result alive returns char*. Parity-safe (0/675),
default-on, DIV-11, no option flag (like every other LIBC entry).

Defect 2 (NOT fixed; follow-up) - a tail-position `return F();` compiled to
`call F; ret` (or the -O2 tail jmp PR #59 recovers) with no intervening use of
the return register leaves the wrapper `void` + bare `return;` and drops the
call output. This is GENERAL (reproduces with `return strchr(s,c);` and any
already-typed libc fn, NOT prototype-related) and is a faithful port of Ghidra's
AncestorRealistic killedbycall rejection. A fix is a new return-recovery
heuristic (output-changing, own option/ablation) entangled with PR #59's
artificial RETURN; documented in docs/features/setlocale-rettype/analysis.md.

Tests/gates: unit s1_protos::tests::setlocale_signature_is_char_ptr_int_char_ptr;
stage tests/stages/ghangr-setlocale-rettype.xml (NULL arg types (char *)0x0,
call resolves to named setlocale, vs pre-fix bare 0; parse line stands in for
the analysis-tier seeding the stage harness does not run). make test PARITY OK
675/675, make test-stages 179/179 (+3), make rust-test green (corpus file-count
bumped to 139 in kuna-base/src/xml.rs), kuna catalog --check OK.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01PBrckq3YYAzBLq5k4KpWac
@mahaloz
mahaloz deleted the feat/angr-tee-o2-tail-jumps-4a1f49 branch July 29, 2026 08:01
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