Skip to content

[AUTOMATED] fix(p4): an argument moved back into its own register is a returned half - #797

Merged
mahaloz merged 3 commits into
mainfrom
fix/own-register-return
Oct 1, 2026
Merged

mahaloz merged 3 commits into
mainfrom
fix/own-register-return

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A function that keeps an argument in a callee-saved register across a call and moves it back into its own argument register to return it loses that half of the return value, and the argument drops out of the prototype.

cat > own.c <<'EOF2'
void ext(void);
unsigned long long own(unsigned a, unsigned b) { ext(); return (unsigned long long)b << 32 | a; }
EOF2
clang --target=arm-linux-gnueabi -O2 -c own.c -o own.o   # mov r4,r1; mov r5,r0; bl ext; mov r0,r5; mov r1,r4
kuna decompile-all own.o
unsigned int own(unsigned int a0)
{
  ext();
  return a0;
}

The same happens on Thumb, RISC-V and PowerPC, and to 128-bit pairs on x86-64 and AArch64. A leaf function hits it too. compiler-rt's __ashrdi3 on RISC-V 32 copies the high word aside at entry, and its zero-shift path returns a with mv a1,a3; ret. kuna prints return a0; there, so the high word is lost.

The fix

Tests

  • tests/stages/kuna-ownreturn.xml (ARM) passes 3 of 10 assertions on main and 10 of 10 with the fix. It adds an svc control (sys2nr stays int4 sys2nr(int4 a0)) and a hand-written __ashrdi3 shape.
  • kuna-cli/tests/own_register_returns.rs compiles the printed ARM, Thumb and RISC-V 32 code with gcc and clang at -O0 and -O2 and runs it against the source. The RISC-V 32 case fails on main (return a0;).
  • Footprint against main over 836 zlib/compiler-rt objects (AArch64, RISC-V 32/64, PowerPC 32/64, ARM, Thumb) and x86-64/MIPS libraries: 3 of 4,641 functions change, each to the right value: __umoddi3 (ARM), __ashrdi3 (RISC-V 32), __ashrti3 (AArch64). 18 decbench ARM firmware images (26,448 functions) change none.
  • Speed: betaflight O2 decompile-all, interleaved min-of-11, 37.40 s on main and 36.97 s here.

Fixes #770

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU

@mahaloz
mahaloz force-pushed the fix/own-register-return branch from 40209b9 to daec3be Compare October 1, 2026 20:32
@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 and others added 3 commits October 1, 2026 22:46
…a returned half

A function that keeps an argument in a callee-saved register across a call
and moves it back into its own argument register to return it (ARM
`mov r4,r1; bl ext; mov r1,r4`) lost that half of its returned pair and the
parameter with it: `unsigned long long own(unsigned a, unsigned b)
{ ext(); return (u64)b << 32 | a; }` printed `unsigned int own(unsigned int a0)`.
After copy propagation the half reaches the RETURN as the caller's own
register, which the pair repair's placement test reads as leftover.

Return recovery still sees the moves, so it now records each used return
register whose value at some RETURN walks back to its own input through
another register, and the pair repair skips the placement test for that
register at every RETURN. A move made by an instruction that also writes the
stack pointer (SPARC save/restore, a pop) or an incidental copy (Xtensa's
call8 window swap) does not count, so the GH-6990 SPARC pass-through and a
merely preserved register still read as leftover. The rule lives inside
retinputhalf and is off with it.

Tests: stage test kuna-ownreturn (1/6 on main, 6/6 after), unit tests for
the walk, and kuna-cli own_register_returns, which compiles the printed ARM
and Thumb functions back and compares them with the source and checks the
AArch64 and x86-64 128-bit pairs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017cAUeUBnQ9xRA9oRJNGWjw
… a call or svc after the move ends it

A high register moved back while the low one is the function's untouched
argument now keeps the pair instead of returning the high word alone: the
zero-shift path of compiler-rt's __ashrdi3 on RISC-V 32 (`mv a3,a1` at
entry, `mv a1,a3; ret`) printed `return a1;`, and __ashrti3 on AArch64 and
RISC-V 64 `v3 = a1;`. Any other low half takes the strict rule's answer.

A move back that a call or a CALLOTHER can follow on a path to the RETURN
no longer counts: an inline svc/ecall/sc reads its arguments from registers
its p-code does not show, so `r1 = b; svc 0` in an int function turned it
into a pair return on ARM, Thumb, AArch64, RISC-V and PowerPC.

Tests: kuna-ownreturn gains sys2nr (svc control) and a hand-written
__ashrdi3 shape (10/10; the previous head 6/10, main 3/10), unit tests for
the call barrier, and own_register_returns round-trips the RISC-V 32 shift
through gcc and clang.

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

Re-recorded on origin/main 30616a8 plus this branch: the 10 OWNRETURN 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/own-register-return branch from daec3be to 8065612 Compare October 1, 2026 22:58
@mahaloz
mahaloz merged commit ffb97ef into main Oct 1, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/own-register-return branch October 1, 2026 23:13
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 argument carried across a call and returned in its own register is dropped as leftover

1 participant