Skip to content

[AUTOMATED] fix(p4): passthrough hands back an ARM or MIPS wrapper's call result - #752

Merged
mahaloz merged 7 commits into
Noelo-Lab:mainfrom
phix33:fix/arm-wrapper-returns
Sep 30, 2026
Merged

mahaloz merged 7 commits into
Noelo-Lab:mainfrom
phix33:fix/arm-wrapper-returns

Conversation

@phix33

@phix33 phix33 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A caller tests a wrapper's result, but whole-program decompilation declares an ARM wrapper that calls and returns (push {r4,lr}; bl provider; pop {r4,pc}) void. The same wrapper on x86-64 (call provider; ret) already gets return provider(a0);.

cat > wrapper.s <<'ASM'
.syntax unified
.arm
.text
.global wrapper,provider,consumer
.type wrapper,%function
wrapper: push {r4,lr}; bl provider; pop {r4,pc}
.type provider,%function
provider: ldr r0,[r0]; bx lr
.type consumer,%function
consumer: push {r4,lr}; bl wrapper; cmp r0,#0; moveq r0,#9; pop {r4,pc}
ASM
clang --target=armv7a-linux-gnueabihf -c -o wrapper.o wrapper.s   # or arm-linux-gnueabi-as
decompiler/target/release/kuna decompile-all wrapper.o
void wrapper(unsigned int *a0)
{
  provider(a0);
}
...
int consumer(unsigned int *a0)
{
  int v1; // r0

  v1 = wrapper(a0);

Fix

  • The ARM compiler specs inject the mode switch of every bx lr and pop {...,pc} as an incidental r0 = r0 (MIPS does v0 = v0 at jr ra). passthrough's tail-return rule requires that the function never touch the return register, and it counted that no-op. It no longer does, and the RETURN's value is followed back through the no-op to the call's result: unsigned int wrapper(unsigned int *a0) { return provider(a0); }.
  • A return register the body touches, even only through that no-op, keeps its call-site argument trials scored as they are with passthrough off. So bl g; bl f; pop {r4,pc} prints v1 = g(a0); return f(v1);, not g(a0); return f();.
  • There is no new option. This is passthrough's existing rule, which is on by default and already applies to x86-64. Its phases.toml row and spec now state the ARM/MIPS reach and the rates below.
  • The cost is the same as on x86-64: a source-void wrapper that ends in a call to a value-returning function compiles to the same bytes, so it gets that function's return value.

Tests

  • kuna-cli/tests/arm_wrapper_returns.rs builds its images inside the test. These cases fail on main:

    • A32 wrappers ending in pop {r4,pc} or bx lr, one whose caller ignores the result, and a three-deep chain.
    • Thumb wrappers ending in pop {r4,pc} or pop.w {r4,lr}; bx lr.
    • Little- and big-endian MIPS jal provider; ...; jr ra wrappers, built as a linked image because kuna does not apply MIPS object relocations.

    These controls are unchanged: a void callee, a call cycle, an indirect call, a write to the return register before or after the call, declared void prototypes, and store(provider(a0)) in A32 and Thumb.

  • Base vs head decompile-all over the 272 fixture ELFs changes one function, a synthetic Cortex-M reset handler. Over 92 debug-stripped ARM firmware binaries (decbench O0, O2 and O2-noinline), 223 functions gain a return. DWARF confirms 182 and says void for 39, and 2 have no DWARF entry: 82.4%.

    • betaflight (49 of 74) and cleanflight (28 of 34) are -Og builds. -Og turns off sibling calls, so every one-line void wrapper ends in bl f; pop {..,pc}.
    • Without those two projects the rate is 105 of 113 (92.9%). Existing tail-jump returns on the same set are 157 of 164 (95.7%), and x86-64 tail / call; ret returns on 64 binaries are 93.4% / 94.6%.
  • Datatests and stages are unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG

@phix33
phix33 force-pushed the fix/arm-wrapper-returns branch from 82c3ec9 to ee487d2 Compare September 30, 2026 00:14
@phix33 phix33 changed the title [AUTOMATED] Recover caller-supported ARM wrapper returns [AUTOMATED] feat(p4): recover caller-supported ARM wrapper returns Sep 30, 2026
@phix33
phix33 force-pushed the fix/arm-wrapper-returns branch from ee487d2 to 985201e Compare September 30, 2026 01:01
mahaloz and others added 2 commits September 30, 2026 02:03
Resolves the stage corpus count, the option count tests and the stage
baseline against main's fieldtype option and stage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
…call result

An ARM wrapper that calls and returns (`push {r4,lr}; bl provider;
pop {r4,pc}`) stayed `void` in decompile-all, while the same wrapper on
x86-64 (`call provider; ret`) already gets `return provider(a0);` from
passthrough's tail-return rule. The ARM compiler specs inject the mode
switch of every `bx lr` / `pop {...,pc}` as an incidental `r0 = r0`
(MIPS: `v0 = v0` at `jr ra`), and the rule's "the function never touches
the return register" test counted it.

- The tail-return rule no longer counts an injected COPY of a register
  onto itself, and returns_tail_result follows the RETURN's value back
  through it to the call's result.
- A returned register the body touches (if only through that no-op)
  keeps its call-site argument trials scored as with the option off, so
  `bl g; bl f; pop {r4,pc}` stays `return f(g(a0))`.
- The argument side is unchanged.

This replaces the opt-in `wrapperreturn` option and its whole-program
caller ledger: main already handled the argument side (Noelo-Lab#764), the return
side needed no caller evidence on x86-64, and with the option on
`mov r0,Noelo-Lab#5; bl inc; pop {r4,pc}` printed `return inc();`. The
docs/features/wrapperreturn fixtures and the stage test that only pinned
the no-evidence case are removed; the stage harness states no callee
prototype, so the positive witness is the kuna-cli test, as for the
other passthrough fixes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
@mahaloz mahaloz changed the title [AUTOMATED] feat(p4): recover caller-supported ARM wrapper returns [AUTOMATED] fix(p4): passthrough hands back an ARM or MIPS wrapper's call result Sep 30, 2026
mahaloz and others added 4 commits September 30, 2026 03:20
…s its ARM/MIPS reach

- arm_wrapper_returns.rs gains a Thumb object (pop {r4,pc}, pop.w
  {r4,lr}; bx lr, a returning and a void call chain, an argument write)
  and a linked little- and big-endian MIPS image (jal; jr ra, a write
  after the call, jalr); each fails on main where it should.
- The passthrough phases.toml row and the spec's Default paragraph state
  that the return arm now takes ARM/MIPS call-then-return wrappers, with
  the rates over 92 ARM firmware binaries (182 of 221 DWARF-described,
  82.4%; 105 of 113 without the two -Og projects) next to the tail-jump
  rate on the same set. docs/options.md and phase_catalog.json are
  regenerated.
- The spec quotes the chain's actual output, v1 = g(a0); return f(v1);.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
The 92 ARM firmware binaries behind the passthrough return-arm rates
include 28 O0 builds, so the phases.toml row, docs/options.md, the
catalog fixture and the spec say "O0, O2 and O2-noinline".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
@mahaloz
mahaloz merged commit 815bd4c into Noelo-Lab:main Sep 30, 2026
2 checks passed
mahaloz added a commit to phix33/kuna that referenced this pull request Sep 30, 2026
No conflicts (Noelo-Lab#752 passthrough wrapper returns). options.md,
phase_catalog.json and the stages baseline regenerate unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
mahaloz added a commit that referenced this pull request Sep 30, 2026
…ads returns it under voidret too

arm_wrapper_returns.rs (#752) pinned void for the indirect and argument-write
wrappers and for passthrough off, while the fixture's consumer reads r0 after
the call and printed v1 = wrapper() of a void wrapper. decompile-all's voidret
redo returns those results; a wrapper no caller reads stays void.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiRmwW65ibgRi4sd1CkDiE
mahaloz added a commit that referenced this pull request Sep 30, 2026
…ads returns it under voidret too

arm_wrapper_returns.rs (#752) pinned void for the indirect and argument-write
wrappers and for passthrough off, while the fixture's consumer reads r0 after
the call and printed v1 = wrapper() of a void wrapper. decompile-all's voidret
redo returns those results; a wrapper no caller reads stays void.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiRmwW65ibgRi4sd1CkDiE
mahaloz added a commit that referenced this pull request Sep 30, 2026
…ads returns it under voidret too

arm_wrapper_returns.rs (#752) pinned void for the indirect and argument-write
wrappers and for passthrough off, while the fixture's consumer reads r0 after
the call and printed v1 = wrapper() of a void wrapper. decompile-all's voidret
redo returns those results; a wrapper no caller reads stays void.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01JiRmwW65ibgRi4sd1CkDiE
mahaloz added a commit that referenced this pull request Sep 30, 2026
…ion computes itself (#772)

Follow-up to #752.

## Problem

A function that calls something and then computes a float return value is decompiled as returning the callee's integer result, and its float parameter disappears. #752 made this happen on hard-float ARM and MIPS; x86-64 and AArch64 have done the same since `passthrough` became the default.

```sh
cat > f.c <<'EOF'
extern int counter;
__attribute__((noinline)) int ise(int x) { counter++; return x * 3; }
float float_after_i(int x, float y) { ise(x); return y * 2.0f; }
EOF
clang --target=armv7a-linux-gnueabihf -mfloat-abi=hard -mfpu=vfpv3-d16 -O2 -c f.c -o f.o
decompiler/target/release/kuna decompile-all f.o
```

```text
// Function: float_after_i @ 0x400020
int float_after_i(int a0)
{
  return ise(a0);
}
```

`gcc -O2 -fno-ipa-ra -c f.c` on x86-64 prints the same (`call ise; movss 4(%rsp),%xmm0; addss %xmm0,%xmm0; ret`), and so do AArch64 and MIPS builds of it.

## Fix

- `passthrough` only hands back a tail call's result when the function returns nothing of its own. Once the return trials are scored, it runs the output model over the trials it does not own, which gives the return value the function would have with `passthrough off`. If that value is in another storage class (`s0`, `xmm0`, `$f0` against `r0`, `rax`, `$v0`), the claim is dropped. The example now prints `float float_after_i(float a0,int a1) { ise(a1); return a0 + a0; }`. That is the pre-#752 output, and it is the same with `armfloatreturn` on or off.
- A register of the same class, like the `r1` in `bl g; mov r1,#0`, can be the rest of the claimed value, so it keeps the claim.
- A value that is zero at every RETURN keeps the claim too. `-fzero-call-used-regs` writes zero into every call-used register a function does not return in, so openssh's `sshkey_certify` ends `call ...; pxor %xmm0,%xmm0; ret`, and it still returns its `int`. The cost is that `g(); return 0.0;` keeps the callee's result, as it does today.
- There is no new option. This is a bug fix inside `passthrough`'s own rule.

## Tests

- `kuna-cli/tests/passthrough_own_return.rs` builds ARM, x86-64, AArch64 and little- and big-endian MIPS objects inside the test. It fails on main at the first ARM function.
  - The failing cases are a float computed after the call on every architecture, a float constant loaded after it (ARM, with `armfloatreturn` off and on) and an x86-64 double.
  - The controls keep their callee's result: a plain wrapper on every arch, the ARM `r1` write, and an x86-64 forwarder that ends in the zeroing epilogue.
- Base vs head `decompile-all` changes no function over the 274 ELF fixtures and 562 debug-stripped decbench binaries: 98 ARM firmware images at O0, O2 and O2-noinline, also run with `armfloatreturn on`, and 464 x86-64 binaries at O2 and O2-noinline. Without the zero rule, the nine copies of openssh's `sshkey_certify` in that set became `return 0;`.
- Datatests and stages are unchanged, and `arm_wrapper_returns.rs` and the `passthrough-*` CLI probes pass unchanged.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
@phix33
phix33 deleted the fix/arm-wrapper-returns branch October 1, 2026 01:36
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.

2 participants