Skip to content

[AUTOMATED] Drop a value left in the AArch64 hidden-return register from a call - #941

Merged
mahaloz merged 1 commit into
mainfrom
fix/fix854
Oct 6, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/fix854

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

On AArch64, a value a function leaves in x8 before a call is printed as that call's first argument. x8 is the indirect-result register for a struct-returning callee, and foo below neither takes it nor returns a struct:

$ cat x8.s
.text
.globl foo
.type foo,%function
foo:
  add x0, x0, #1
  ret
.globl f
.type f,%function
f:
  stp x29, x30, [sp, #-16]!
  mov x8, #0x38
  mov x0, #3
  bl foo
  ldp x29, x30, [sp], #16
  ret
$ clang --target=aarch64-linux-gnu -c x8.s -o x8.o && kuna decompile-all x8.o
...
void f(void)
{
  foo(0x38,3);
}

The same happens across AArch64 glibc and bionic, where x8 still holds the syscall number (__open prints sub_d4de8(0x38,v3 & 0xffffffff,...)), and in clang -O0 code, which uses w8 as a scratch register.

Fix

  • New option hiddenretarg (default on). Once scoring has made an x8 trial active, the trial is rescored with the evidence below.
  • The callee's body decides first, read through the entry walk calleedeadarg caches. A callee that never takes x8 drops it: no read before a write, and no exit to unread code while x8 is unwritten. A callee that reads x8 keeps it, whatever the value; that covers a struct return through it, a tail call into one, and a helper that loops on it. So does a callee that reaches svc with the caller's x8, since the kernel reads it as the syscall number.
  • Otherwise the value decides. A value already dropped from one call is dropped wherever it reappears, so freeing it at one call does not hand it to another (if (pg < 1) return corrupt(4321); if (pagecount(bt) == 0) ... at -O0). A value whose every leaf is a constant below 0x1000 is dropped too, because no OS maps that page.
  • One limit: a callee that takes a small number in x8 by its own convention, behind an import the walk cannot read, loses it. The OCaml runtime's caml_allocN(0x90) now prints caml_allocN(). Only register entries are touched: SPARC's [sp+64] slot and tricore's shared a4 are left alone.

Tests

  • tests/stages/gh854-hiddenretarg-a64.xml, two passes. option hiddenretarg off reproduces foo(0x38,3), foo(a1 + 1,3), lib(0x38) and cancel(a1). Both passes must keep corrupt(0x10e1), helper(0x40,1), cnt(10,...), mk(v1,7) and mkthunk(v1,9). Three of its functions also run under the Apple (-a64apple) and Windows (-a64win) compiler specs.
  • 0/675 datatest assertions move. Footprint over 227 AArch64 inputs (24,493 functions) changes 1,377:
    • glibc: 84 changed (libc.so.6 77, ld-linux 7). Bionic: 263 changed (libc.so 211, linker64 52). Each loses a leftover syscall number.
    • sqlite3 clang -O0 (no -g): 973 of 2,488 change. 559 only lose a leading x8 value. The rest also align arguments with their parameters: 34 parameters gain a pointer type and 12 lose one, and five calls now pass the argument the source does (sqlite3StatusDown(1,1), jsonAppendSeparator(v1)).
    • Elsewhere: two bionic calls swap __errno(0x81) for a leftover x0 (__errno(v7)), and 3 OCaml caml_allocN calls lose their size (the limit above).
  • Speed (decompile-all, min of 11, interleaved): gzip +0.73%, kmod -0.01%, grep-O0 -1.99%, ls +0.32%, ld-linux +1.12%, bzip2 -O0 -2.96%; total +0.30%.

Fixes #854
Part of #890

🤖 Generated with Claude Code

https://claude.ai/code/session_01Qihrih8NnnexavqvZxp8CF

@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 6, 2026
@mahaloz
mahaloz force-pushed the fix/fix854 branch 2 times, most recently from 68c36b1 to 56463ab Compare October 6, 2026 02:53
…rom a call

On AArch64 a value a function leaves in x8, the indirect-result register, was
printed as the next call's first argument (`mov x8,#0x38; mov x0,#3; bl foo`
-> `foo(0x38,3)`), throughout glibc and bionic after every `svc` and in clang
-O0 code that uses w8 as scratch. New option `hiddenretarg` (default on)
rescores an active x8 trial no-use when the callee's own body never takes x8,
when the value was already dropped from another call it reaches (frame
addresses excepted), or when it is a constant in the null page. A callee that
reads x8 or reaches `svc` with it unwritten keeps it, and so does a real
struct return.

Fixes #854
Part of #890

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Qihrih8NnnexavqvZxp8CF
@mahaloz
mahaloz merged commit f0b0653 into main Oct 6, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/fix854 branch October 6, 2026 04:42
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] AArch64: a value left in x8 becomes the first argument of the next call

1 participant