Skip to content

[AUTOMATED] fix(p2): tail-call a veneer whose only target is another function - #793

Merged
mahaloz merged 6 commits into
mainfrom
fix/arm-veneer-bound
Oct 1, 2026
Merged

mahaloz merged 6 commits into
mainfrom
fix/arm-veneer-bound

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

An ARM veneer that jumps through a literal to another function (ldr r1,[pc]; bx r1) is decompiled as a one-case switch that walks into the callee. If the veneer's exact extent is declared, the callee is outside it, and the transfer turns into halt_missing().

cat > gen.py <<'PY'
import struct
b = 0x02000000   # veneer: ldr r1,[pc]; bx r1; .word arm_helper; .word 0; arm_helper: add r0,r0,#1; bx lr
text = struct.pack("<6I", 0xe59f1000, 0xe12fff11, b + 0x10, 0, 0xe2800001, 0xe12fff1e)
names = b"\0.text\0.shstrtab\0"
img = bytearray(84) + text + names
img += bytes(-len(img) % 4)
shoff = len(img)
img[:52] = struct.pack("<16sHHIIIIIHHHHHH", b"\x7fELF\x01\x01\x01", 2, 40, 1, b, 52, shoff, 0x05000000, 52, 32, 1, 40, 3, 2)
img[52:84] = struct.pack("<8I", 1, 84, b, b, len(text), len(text), 5, 4)
img += bytes(40) + struct.pack("<10I", 1, 1, 6, b, 84, len(text), 0, 0, 4, 0)
img += struct.pack("<10I", 7, 3, 0, 0, 84 + len(text), len(names), 0, 0, 1, 0)
open("veneer.elf", "wb").write(img)
PY
python3 gen.py
kuna decompile veneer.elf 0x02000000 --target ARM:LE:32:v5t:default --isa arm \
  --assert 'function 0x02000000-0x02000008=veneer' --assert 'function 0x02000010-0x02000018=arm_helper' \
  --assert 'prototype 0x02000000 unsigned int veneer(unsigned int value)' \
  --assert 'prototype 0x02000010 unsigned int arm_helper(unsigned int value)'
unsigned int veneer(unsigned int value) // warn: Function flows out of bounds
{
  switch(0x2000010) {
    case 0x2000010:
      halt_missing(); // warn: Function flow out of bounds: r0x02000004 flows to r0x02000010
  }
}

With function 0x02000000=veneer (no end) you get the same switch with return value + 1; copied in from arm_helper. With this PR both print return arm_helper(value); // tail-call.

Fix

  • When jump-table recovery finds a computed jump whose entries all name one address, and that address is another known function's entry, the jump gets the same rewrite as a direct b f: a call, then a return (or the callee's no-return halt), marked // tail-call. The register that held the target is never taken as an argument.
  • This happens only where the call loses nothing: the destination is outside the declared extent (the alternative is halt_missing()), or the callee's prototype is stated (declared, DWARF, a library signature, a demangled C++ name) and its return value reaches the caller, because that prototype states a void return or the caller's own output is stated. kuna's return recovery never takes a tail call's result, so without a stated output the call would print void where the copied-in body returns the callee's value; those veneers keep the body.
  • A prototype with no return type is an unknown return, not void. That is what a demangled C++ name gives (_Z3addii states (int,int) and nothing about the return), so a C++ veneer or long-call stub keeps its body unless DWARF or a declaration states the return.
  • The rule is not ARM-specific. With DWARF it also turns an x86-64 -fno-plt jmp *GOT and a jump through a read-only function-pointer slot into a tail call, e.g. switch(0x1100){ case 0x1100: return a * b + 7; } becomes return g(a,b); // tail-call.
  • option tailcalljump gains a value: direct keeps direct-jump tail calls and restores the old flow into a veneer's destination. on stays the default.

What it does not fix:

  • A one-target table whose target is not a function entry still prints switch(0x...) (RuleSwitchSingle is a stub).
  • A veneer inside an open extent to a callee with no stated prototype, or to a callee whose return type is not stated while the veneer's own output is not stated either (a C++ callee known only by its mangled name), keeps the copied-in body.
  • Outside a declared extent a callee with no prototype is called with whatever argument registers the caller shows and no return value, e.g. void veneer(unsigned int a0) { arm_helper(a0); return; }, exactly as a direct b arm_helper there already prints.

Tests

  • tests/stages/kuna-veneer-tailcall.xml: two passes (direct, then on) over A32 ldr/bx, ldr pc and Thumb veneers, bounded and open, with and without prototypes. The direct pass reproduces the bug; on the unpatched tree the file errors on the new option value, and with off substituted 5 of its 9 assertions fail.
  • kuna-cli/tests/arm_veneer_tailcall.rs: the issue's CLI path on generated ELFs, including the open veneer with no output of its own (keeps return a0 + 1;), a helper declared void (tail-called), and veneers _Z3venii and ven_c into _Z2g2ii, which keep return a0 + a1; as on main.
  • decompile-all before and after on 109,815 functions in 258 binaries: 225 ARM firmware ELFs from the decbench corpus at O0/O2/O2-noinline (130 with DWARF, 95 stripped) plus 33 x86-64 DWARF builds. 20 functions changed, all in ARM DWARF builds: libopencm3 _usbd_standard_request_{device,interface,endpoint}, FreeRTOS HardFault_Handler and NuttX irq_dispatch. Each copied-in callee body became a call with the callee's DWARF arguments, e.g. return usb_standard_set_address(usbd_dev,req,buf,len); // tail-call, and each matches the source. No stripped binary changed, and none of those 33 x86-64 builds has a one-target jump to another function. 14 x86-64 C++ binaries and libraries from /usr (libsnappy, libpcrecpp, libgmpxx, pdfunite, ...): 0 of 1,381 functions changed. Small C++ builds with g++ and clang++ (x86-64 -fno-plt shared libraries, ARM and Thumb -mlong-calls): every veneer whose return is known only from its mangled name is unchanged from main; with DWARF on the veneer, return add(a,b); // tail-call and vsink(p,a); // tail-call, matching the source.
  • Speed, decompile-all, interleaved against main: cdcacm.elf 0.509 s to 0.412 s (min of 21, fewer copied-in bodies), RTOSDemo 1.017 s to 0.971 s and libpcrecpp 0.592 s to 0.592 s (min of 11), x86 gzip 5.93 s to 5.86 s (min of 21).

Fixes #781

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU

@mahaloz
mahaloz force-pushed the fix/arm-veneer-bound branch from 1f5c529 to 618637b Compare October 1, 2026 19:07
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 1, 2026
@mahaloz
mahaloz force-pushed the fix/arm-veneer-bound branch 2 times, most recently from 0e19b9b to 91d06b2 Compare October 1, 2026 22:09
mahaloz and others added 6 commits October 1, 2026 23:15
…or lies outside its extent

An ARM veneer that jumps through a read-only literal (`ldr r1,[pc]; bx r1`,
`ldr pc,[pc,#-4]`, Thumb `ldr r1,[pc]; bx r1`) recovered as a one-entry jump
table, so the follower walked into the callee as a one-case switch. With the
veneer's exact extent declared, the callee lay outside it and the case became
halt_missing() under a "flows out of bounds" warning.

Under `option tailcalljump on` (the default), a recovered table whose only
destination is another known function's entry becomes a tail call, rewritten
exactly like a direct `b f`, when the callee's prototype is input-locked or
the destination lies outside the declared extent. A callee with no prototype
inside an open extent keeps the copied-in body, which states the return value
and arguments a call to it could not. The register the jump read its target
from is recorded on the call spec and never recovered as an argument.
`option tailcalljump direct` keeps direct-jump tail calls and restores the
flow into a veneer's destination.

Fixes #781

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
…eaches the caller

A callee prototype parked as pieces (DWARF, a library signature) now
counts as stated, as it does at the call site.  Inside an open extent a
callee that returns a value is tail-called only when the caller's own
output is stated: an unstated caller output never takes a tail call's
return value and would print void where the copied-in body returns it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…d say why in the spec

Adds ret_veneer to the stage test (an open veneer to a declared helper with
no prototype of its own keeps the copied-in body in both passes) and two
CLI cases, and rewrites the option text and the chapter 02 prose for the
narrowed condition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…wn return, not void

A prototype parked from a demangled C++ name states the arguments but
not the return type, so the veneer gate read it as `void` and printed a
tail call that dropped the callee's return value. Only a stated `void`
return now says the callee returns nothing, and only parked pieces that
name a return type or storage (or a locked output) say the veneer's own
output is stated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…veneer's return unknown

Spec chapter 02 and the tailcalljump row now say that only a stated
`void` means the callee returns nothing, that cppsig's demangled
signatures leave the return unknown, and that the rule also reaches x86
`-fno-plt` GOT jumps and read-only function-pointer slots. The stages
baseline is re-recorded on the new base (+9 VENEERTAILCALL keys only).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
…d base

Re-recorded on origin/main ffb97ef plus this branch: the 9 VENEERTAILCALL keys are the only additions, none removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
@mahaloz
mahaloz force-pushed the fix/arm-veneer-bound branch from 91d06b2 to 5e31e88 Compare October 1, 2026 23:29
@mahaloz
mahaloz merged commit 48f49b3 into main Oct 1, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/arm-veneer-bound branch October 1, 2026 23:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] An exact ARM veneer bound turns a valid tail transfer into halt_missing

1 participant