Skip to content

[AUTOMATED] fix(p4): a variadic call dropped an argument its caller forwards in the register it arrived in - #875

Merged
mahaloz merged 7 commits into
mainfrom
fix/variadic-forwarded-arg
Oct 3, 2026
Merged

mahaloz merged 7 commits into
mainfrom
fix/variadic-forwarded-arg

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

The problem

A variadic call drops an argument that the caller received as a declared parameter and passes on in the same register, untouched. A wrapper around open is the usual case:

cat > w.c <<'EOF'
#include <fcntl.h>
int my_open(const char *p, int flags, int mode) { return open(p, flags, mode); }
int main(int argc, char **argv) { return my_open(argv[0], argc, 0); }
EOF
gcc -O2 -g -fno-inline -o w w.c
kuna decompile w my_open

my_open is xor %eax,%eax; jmp open, with mode still in edx. Main prints:

int my_open(char *p,int flags,int mode)
{
  return open(p,flags); // tail-call
}

The issue's AArch64 cases fail the same way: double c2(int k, double x) { return vr(k, x); } prints return vr(k);, and vr(k, g(k)) prints g(k); return vr(k);, because nothing moves x or g's result out of d0.

The fix

  • New option varargforward (default on, p4_calls/kuna_varargforward.rs). In the variable part of a call whose prototype ends in ..., an argument register is kept when it holds the caller's own input and the caller declares a parameter there, or the result of a call whose return type is declared. Upstream refuses both: it wants to see a value moved into the register, and never takes another call's result.
  • Nothing else in the function may read that value. if (a > 5) return 0; return pr(f, a); compiles the same with or without a, so it still prints pr(f). A parameter counts only when the function's own prototype is declared (DWARF, --assert), not recovered.
  • A constant printf format passed as the last fixed argument caps the count. sqlite's sqlite3InvalidFunction declares an unused NotUsed2 in the register after sqlite3_mprintf("... %s ...", zName)'s one argument, and does not gain it. Where the caller states how many floating-point registers it filled (al on x86-64, CR bit 6 on 32-bit PowerPC), that count still decides.
  • Not fixed: a declared parameter that nothing uses, sitting in a variadic register with no constant format to cap the call, is passed. int d6(const char *f, int a) { return pr(f); } prints pr(f,a). The register holds a when pr starts, so the printed call compiles to the same instructions. A forwarded value that is also read elsewhere is still dropped, as before.

The tests

tests/stages/gh829-varargforward-{x86,a64}.xml run each case with the option off (the bug) and on, plus a parameter that is also compared and stays out. 31 callers built for x86-64 with gcc and clang -O2, compiled back from the printed C with both compilers and run against the source: main mismatches 11 (gcc) and 12 (clang) of them, this branch 2, both the "read elsewhere" shape above. The -O0 builds print the same C as main.

decompile-all main vs this branch changes no function in 17 stripped binaries and 234 DWARF builds (72,183 functions), nor in the sqlite3 amalgamation built for x86-64 and AArch64. Wall time with the option off vs on, interleaved min of 11: numfmt -3.0%, dash +1.1%, kmod -0.8%.

Fixes #829

🤖 Generated with Claude Code

https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt

mahaloz and others added 7 commits October 3, 2026 02:12
The rebase took main's baseline where both sides recorded new stage tests; this re-records it with SLEIGHHOME set. Only the eight GH829 keys are new and none of main's are missing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
@mahaloz
mahaloz force-pushed the fix/variadic-forwarded-arg branch from c7bab15 to 125fec6 Compare October 3, 2026 02:16
@mahaloz
mahaloz merged commit a63762a into main Oct 3, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/variadic-forwarded-arg branch October 3, 2026 02:32
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 variadic call drops an argument the caller forwards unchanged in its own register

1 participant