Skip to content

[AUTOMATED] fix(p4): a 64-bit return whose high word comes from the low word, or whose low word is the argument, printed void - #879

Merged
mahaloz merged 1 commit into
mainfrom
fix/pair-return-ext
Oct 3, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/pair-return-ext

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

The problem

On 32-bit targets, a function returning a 64-bit integer whose high word is computed from the low word, or whose low word is its own argument left in place, printed as void f(void), losing both the return value and the parameter.

printf 'typedef unsigned long long u64;\nu64 zx(unsigned a) { return a; }\nlong long sx(int a) { return a + 3; }\n' > fu.c
clang --target=arm-linux-gnueabi -O2 -c fu.c -o fu.o   # zx: mov r1,#0; bx lr   sx: add r0,r0,#3; asr r1,r0,#31; bx lr
kuna decompile-all fu.o
// Function: zx @ 0x400000
void zx(void)
{
  return;
}

// Function: sx @ 0x400008
void sx(void)
{
  return;
}

sx is also void on i386 (sar $31,%edx, gcc cltd) and MIPS (sra $3,$2,31). With this change they print unsigned long long zx(unsigned int a0) { return a0; } and long long sx(int a0) { return a0 + 3; }.

The fix

  • The [AUTOMATED] fix(p4): a caller that changes only the high word of a callee result prints void #855 rule that keeps a call's untouched result as the low register of a pair now also keeps the function's own argument left in place, and a low word read only by the RETURN and by what turns it into the high word.
  • The high register must be computed by the function and read by nothing but the RETURN. A load does not count, a zero counts only when the function names that register nowhere else (so a -fzero-call-used-regs scrub stays void), and a register also handed to a store, a call or vmsr fpscr/msr cpsr_c stays void.
  • The late pair repair kept the high register alone when the low one was the argument reloaded from its spill (clang -O0 printed return 5; for a | 5ULL << 32); it keeps the pair now.
  • No option: the rule only fires where the function returns nothing today. The argument kinds turn off with retinputhalf.

Not fixed: RISC-V li a1,0; ret (no instruction names a0), a loaded high word beside the argument, a high register that feeds the low one (also how an int division by a constant ends), and an ARM conditional return (asrlt r1,r0,#31; bxlt lr) whose r1 is also taken as an argument of a later call.

Tests

  • tests/stages/kuna-retpairhalf.xml: 5 of 8 assertions fail on main; the rest are controls (int div7, a stored sign word, a scrubbed r1, an svc argument, r1 written to fpscr or cpsr).
  • kuna-cli/tests/pair_return_halves.rs compiles the ARM, MIPS and i386 output back with gcc and clang at -O0 and -O2 and checks it against the source over 225 input pairs; it fails on main.
  • decompile-all over 38 stripped ARM, i386 and x86-64 decbench binaries (30,964 functions) changes 20: __aeabi_dadd/__aeabi_ddiv exits in eight firmware images, which printed the new high word alone and now return CONCAT44(hi,a0); u-boot's memcpy and __of_translate_address; and two copies of a betaflight tbb jump table decoded as code. Lua 5.4 for five 32-bit targets at O0 and O2 (12,503 functions) changes none.
  • decompile-all CPU time over betaflight, crazyflie and nuttx (O2), paired runs, min of 11: 70.3 s on main, 69.7 s with the change.

Fixes #807

🤖 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
mahaloz force-pushed the fix/pair-return-ext branch 2 times, most recently from 4adb8ce to 5cfd9c9 Compare October 3, 2026 03:28
@mahaloz
mahaloz force-pushed the fix/pair-return-ext branch 2 times, most recently from 329e677 to e18c9d6 Compare October 3, 2026 04:49
…ow word, or whose low word is the argument, printed void

On 32-bit targets `long long sx(int a) { return a + 3; }` (ARM `add r0,r0,#3;
asr r1,r0,#31`) and `u64 zx(unsigned a) { return a; }` (ARM `mov r1,#0; bx lr`)
printed as `void f(void)`. The rule that keeps a call's untouched result as
the first register of a pair now also keeps the function's own argument left
in place, and a value read only by the RETURN and by what turns it into the
second register. For these kinds the second register's value must be read by
nothing but the RETURN, so an r1 written to fpscr or cpsr beside an untouched
r0 stays void. The late pair repair keeps the pair when the low half is the
argument reloaded from its spill, instead of returning the high register alone.

Fixes #807

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/pair-return-ext branch from e18c9d6 to 0228789 Compare October 3, 2026 05:12
@mahaloz
mahaloz merged commit 2267619 into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/pair-return-ext branch October 3, 2026 05:29
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 64-bit return whose high word is the sign of the low word, or whose low word is the untouched argument, prints void

1 participant