Skip to content

[AUTOMATED] fix(p4): a caller passes its inputs on to a forwarder that calls its target (passthrough) - #764

Merged
mahaloz merged 2 commits into
mainfrom
fix/issue-749
Sep 29, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fix/issue-749

Conversation

@mahaloz

@mahaloz mahaloz commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

A function that forwards its registers to a callee gets them as parameters when the callee forwards by a jump (jmp narrow), but not when the callee forwards by a call (sub rsp,8; call narrow; add rsp,8; ret). The caller of the second kind loses its arguments.

mk.py from #749 (writes fwdcall.elf, no toolchain needed)
import struct
BASE = 0x401000
funcs = {
    'narrow': '488d0437c3',
    'fwd': 'e900000000',
    'fwd_call': '4883ec08e8000000004883c408c3',
    'pass_fwd': '53e8000000004801c05bc3',
    'pass_fwd_call': '53e8000000004801c05bc3',
}
code, syms = bytearray(), {}
for n, h in funcs.items():
    syms[n] = len(code)
    code += bytes.fromhex(h)
    code += b'\xcc' * (-len(code) % 16)
def rel(src, off, dst):
    at = syms[src] + off
    code[at:at + 4] = struct.pack('<i', syms[dst] - (at + 4))
rel('fwd', 1, 'narrow'); rel('fwd_call', 5, 'narrow')
rel('pass_fwd', 2, 'fwd'); rel('pass_fwd_call', 2, 'fwd_call')
strtab = b'\0' + b''.join(n.encode() + b'\0' for n in syms)
symtab, o = bytearray(24), 1
for n, a in syms.items():
    symtab += struct.pack('<IBBHQQ', o, 0x12, 0, 1, BASE + a, len(bytes.fromhex(funcs[n])))
    o += len(n) + 1
shstr = b'\0.text\0.symtab\0.strtab\0.shstrtab\0'
t_off = 0x1000
s_off = t_off + len(code)
st_off = s_off + len(symtab)
sh_off_str = st_off + len(strtab)
shoff = (sh_off_str + len(shstr) + 7) & ~7
eh = b'\x7fELF\x02\x01\x01' + bytes(9) + struct.pack('<HHIQQQIHHHHHH', 2, 62, 1, BASE, 64, shoff, 0, 64, 56, 1, 64, 5, 4)
ph = struct.pack('<IIQQQQQQ', 1, 5, t_off, BASE, BASE, len(code), len(code), 0x1000)
sh = [bytes(64),
      struct.pack('<IIQQQQIIQQ', 1, 1, 6, BASE, t_off, len(code), 0, 0, 16, 0),
      struct.pack('<IIQQQQIIQQ', 7, 2, 0, 0, s_off, len(symtab), 3, 1, 8, 24),
      struct.pack('<IIQQQQIIQQ', 15, 3, 0, 0, st_off, len(strtab), 0, 0, 1, 0),
      struct.pack('<IIQQQQIIQQ', 23, 3, 0, 0, sh_off_str, len(shstr), 0, 0, 1, 0)]
out = bytearray(shoff + 64 * 5)
out[0:64], out[64:120] = eh, ph
out[t_off:t_off + len(code)] = code
out[s_off:s_off + len(symtab)] = symtab
out[st_off:st_off + len(strtab)] = strtab
out[sh_off_str:sh_off_str + len(shstr)] = shstr
out[shoff:] = b''.join(sh)
open('fwdcall.elf', 'wb').write(out)
python3 mk.py && kuna decompile-all fwdcall.elf
long pass_fwd(long a0,long a1)      // push rbx; call fwd; add rax,rax; pop rbx; ret
{
  return fwd(a0,a1) * 2;
}

long pass_fwd_call(void)            // the same, but calls fwd_call
{
  return fwd_call() * 2;
}

With this change the second one is long pass_fwd_call(long a0,long a1) { return fwd_call(a0,a1) * 2; }.

The fix

  • passthrough only takes a register from a callee whose body is seen reading it. The body walk stops at every call, so a callee that forwards by a call reads nothing. The walk now also counts what each direct call's target takes, for the register bytes nothing wrote before that call, up to three calls deep (reads_through_calls in kuna_calleedeadarg.rs).
  • A target only counts for a register its own recovered prototype accepts on the same terms passthrough already asks of a callee. Without this check, a variadic function's register-save prologue on some error path would count as reads of every argument register. In dash, or's fourth parameter came out unsigned long with casts at every use.
  • A target's read never counts for a register the forwarder reads itself. The forwarder's own read already shows the register is an input, and the deeper read could only change its width. Without this, openssh channel_by_id (which does push %rsi to pass id to a variadic logger) made channel_send_open(ssh, int id) and the other callers of channel_lookup take a 64-bit id.
  • Only passthrough's own check reads the wider set. calleedeadarg, calleearitybody and the arity check see the same walk as before. Body probes stay cached for the whole run, and the rest is memoized per caller because callee prototypes are still being added as the run goes.
  • This is part of passthrough's own rule, so --option passthrough off still gives the old output.

Tests

  • New fixture passthrough_callfwd_x86_64 (generated by the .py next to it) with two tests/cli probes, one with the default and one with passthrough off. Callers of a one-deep and a two-deep call forwarder get (a0,a1). A forwarder that writes esi first passes only a0. An indirect-call forwarder and a call cycle get nothing. The default probe fails on main.
  • Fixture passthrough_widthfwd_x86_64, built by gcc from the .c next to it, has the openssh shape above. Its probe expects send_open and use_send to gain the struct pointer and keep unsigned int a1. It fails on main (no arguments) and without the width check (unsigned long a1).
  • Unit tests for the read-through step, the width check, and the prototype check on the target.
  • 675/675 and stages parity OK. No output changes in decompile-all over the 264 ELF fixtures (5,423 functions).
  • decompile-all over 266 stripped x86-64 -O2 binaries from 25 projects (92,275 functions): 111 functions gain 179 parameters and 163 calls gain arguments. Every one with a DWARF twin agrees with it, and nothing loses an argument. Parameter widths move toward DWARF in 28 places and away in 2. The 2 are gnulib mkstemp_len/mkdtemp_len, which hand a size_t to gen_tempname_len's int and get the int the callee reads.
  • Speed, decompile-all, interleaved min of 11: dash -O2 5.755s -> 5.767s, openssh ssh -O2-noinline 39.0s -> 38.4s.

Fixes #749

🤖 Generated with Claude Code

https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG

mahaloz and others added 2 commits September 29, 2026 20:57
…t calls its target (passthrough)

passthrough takes a register at a call only when the callee's body is
seen reading it, and the callee-body walk ends at every call. A callee
that forwards by a call (sub rsp,8; call narrow; add rsp,8; ret, or a
tail call the spec models as a call) therefore read nothing, so its
callers lost the arguments it passes on, while a jmp forwarder kept them.

The walk now also counts what each direct call's target takes, for the
register bytes nothing on the forwarder's path wrote before that call,
up to three calls deep. A target takes a read only where its own
protoorder statement would pass passthrough's callee-side tests (an
arity-sound list naming the register, not as a variadic tail); without
that, a variadic's register-save prologue on an error path leaks 8-byte
reads of every argument register. Only passthrough's stated_width reads
the widened summary; calleedeadarg, calleearitybody and the arity claim
see the plain walk. Probes stay cached for the run; the fold reads
statements, so it is memoized per caller.

Fixes #749

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
…ster at (passthrough)

A target's read through a call no longer counts for a register the
forwarder reads itself. That register is already proven an input by the
forwarder's own read, and the deeper read could only change its width:
openssh channel_by_id pushes all of rsi for a variadic %d, which made
channel_send_open(ssh, int id) and the other channel_lookup callers take
a 64-bit id. The through-call step now only adds registers the forwarder
never touches.

Also routes resolve_node and seed_callee_entry_dead through probe_cached,
and adds the gcc-built passthrough_widthfwd_x86_64 fixture with a probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01TzSHsg8KAakpogyVw5dtjG
@mahaloz
mahaloz merged commit b8b6fc9 into main Sep 29, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/issue-749 branch September 29, 2026 21:16
mahaloz added a commit to phix33/kuna that referenced this pull request Sep 30, 2026
…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
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.

[AUTOMATED] passthrough: a caller does not pass its inputs on to a forwarder that calls its target (call f; ret)

1 participant