Skip to content

[AUTOMATED] fix(p4): a tail call to a function with no parameters loses its return value - #877

Merged
mahaloz merged 5 commits into
mainfrom
fix/tailcall-noparam-return
Oct 3, 2026
Merged

mahaloz merged 5 commits into
mainfrom
fix/tailcall-noparam-return

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

A function that tail-calls a function with no parameters is printed void, although the same output prints the callee returning a value. On x86-64 the same happens when the function used rax before its tail call.

// pr827.c
int g;
long G[2];
__attribute__((noinline)) int getv(void) { return g * 3; }
__attribute__((noinline)) long getk(long x) { return x * 7 + G[1]; }
int f(void) { return getv(); }                        // jmp getv
long sum2(long *p) { return getk(p[0] + p[1]); }      // mov (%rdi),%rax; add 8(%rdi),%rax; mov %rax,%rdi; jmp getk
$ gcc -O2 -c pr827.c -o pr827.o && kuna decompile-all pr827.o
...
void f(void)
{
  getv(); // tail-call
}

void sum2(struct_0 *a0)
{
  getk(a0->field_0x0 + a0->field_0x8); // tail-call
}

With this change they print int f(void) { return getv(); } and long sum2(...) { ... return getk(v1 + v2); }. The f case also prints void on ARM, AArch64, PowerPC and MIPS builds, and is fixed there too.

Fix

  • protoorder declined to state anything for a function with no parameters. It now states that function's return value. An empty list is not an arity claim, so the only reader is passthrough's tail-call rule. argclobber still declines an empty list.
  • passthrough gave a function its tail call's result only if the function never touched the return register. A register that carries no argument (x86 eax/rax, i386 eax, MIPS v0) may now be written before the call, since the call overwrites it. Nothing may read or write it between the call and the RETURN. The RETURN's eax is followed through the low SUBPIECE that heritage inserts when the function wrote all of rax.
  • Argument registers keep the old rule. On ARM, mov r0,#5; b g is as often a void function calling g(5) as an int one returning it: tried on ARM firmware, DWARF confirmed 31 of 97 such returns.

Not fixed here: a tail call to a declared libc function (return strlen(s);) still prints void unless a caller reads the result. Taking those returns was DWARF-confirmed for only 43 of 54, because void wrappers ending in putc or clock_gettime are common. A callee whose own return type is recovered wrongly passes that type to its tail callers, as it already did for callees with parameters (#873, and the MIPS $v1 read of #878).

Tests

  • kuna-cli/tests/tail_call_returns.rs builds x86-64, ARM and AArch64 objects. A tail call to a callee with no parameters, and x86-64 wrappers that write rax before the call, print void on main and return the callee's result now. ARM mov r0,#5; b getk stays void.
  • tests/cli/protoorder-cycles-types-the-callers-of-a-recursive-function.json no longer pins its -O0 callers as void. They load through rax before their last call and now return its result.
  • Main vs head decompile-all on 24 debug-stripped decbench binaries (x86-64 O0/O2/O2-noinline, ARM firmware): 126 functions gain a return (DWARF: 97 value, 4 void, 25 no entry). Two O0 wrappers widen from unsigned int to their callee's unsigned long, and two mains lose a cast. Nothing else changes.
  • Speed, interleaved min of 11, main vs head decompile-all: coreutils date -0.9%, gzip -2.1%, nuttx -2.8%, O0 tr -4.9% (noise; no slowdown).

Fixes #827

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

@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 3, 2026
mahaloz and others added 5 commits October 3, 2026 02:35
…ameters

WIP: protoorder now states an empty parameter list when passthrough recorded
a return value, so a tail call to it keeps that value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…e register first or the callee is declared

WIP: the tail-return arm of passthrough now checks only the path from the
call to the RETURN, follows the low SUBPIECE heritage reads out of a wider
register, and takes a declared callee's single-register output.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…that carries no argument

Drop the declared-callee arm (its gained returns contradict DWARF far more
often than the existing rule's) and keep the whole-function untouched rule for
an argument register such as ARM r0. Add the tail_call_returns CLI test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…s callees and return-only registers

Spec prose, the passthrough row and options.md; the protoorder cycles probe no
longer pins its -O0 callers' return types.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
…er without overflowing

A constant at the top of its space overflowed the end-offset sum under debug
assertions. Recapture the phase catalog for the passthrough row, and let the
cycles test not pin its -O0 callers' return type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/tailcall-noparam-return branch from c69a44f to 02fd7cd Compare October 3, 2026 02:40
@mahaloz
mahaloz merged commit 64d21a5 into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/tailcall-noparam-return branch October 3, 2026 02:56
mahaloz added a commit to phix33/kuna that referenced this pull request Oct 3, 2026
Brings in Noelo-Lab#872, Noelo-Lab#832 (indexaliasguard global, now the default), Noelo-Lab#875 (varargforward) and Noelo-Lab#877.
Heritage::guard keeps the stackstoreguard branch first and main's global STORE guard as the else
branch; spec 03 keeps main's global/full paragraphs and notes the stack guard also runs at global.
Counts re-derived (254 settables, corpus 392), catalog fixture recaptured, options.md regenerated,
stages baseline re-recorded (1795 = main 1792 + the 3 STACK STORE keys).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
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] A tail call to a function with no parameters loses its return value

1 participant