DISCLOSURE: this patch was prepared by GPT-6 Astra. It fixes a real crash though that I encountered in a project that uses libtcc as a backend. As long as the bug gets fixed, feel free to ignore this patch. End human message.
--
Apple's arm64 ABI does not round each fixed stack argument up to an
eight-byte slot. arm64_pcs_aux currently applies the generic AAPCS64
rounding to spilled floats and homogeneous floating-point aggregates
(HFAs), so TinyCC and native Clang code disagree about argument offsets.
For example, with V3 = struct { float x, y, z; }, consider:
int f(V3 a, V3 b, V3 c, float d, double e);
The first two V3 arguments use v0-v5. The third does not fit in v6-v7
and goes on the stack. Apple Clang places c at sp+0, d at sp+12 and e
at sp+16. TinyCC instead places d at sp+16 and e at sp+24. Consecutive
spilled scalar floats likewise occupy four-byte slots in Clang, but
eight-byte slots in TinyCC. Calls within TinyCC can hide this mismatch;
calls across a native/libtcc boundary silently pass or read wrong values.
For TCC_TARGET_MACHO, allocate fixed floating-point stack arguments at
their natural size and alignment, retaining vector-register exhaustion
when an HFA spills. Keep the existing rules for variadic arguments and
other targets. Round the end of the fixed arguments up to eight bytes
both when a caller starts placing variadic arguments and when the callee
computes its va_start position.
Add ABI tests for a spilled three-float HFA followed by a float, several
spilled scalar floats, mixed float/HFA/double alignment, and the boundary
between fixed HFAs and variadic arguments. Each test exercises both
native-to-TinyCC and TinyCC-to-native calls.
---
I encountered this through Goose's libtcc JIT calling a native physics
library on macOS arm64: sphere and mesh ray casts incorrectly reported
no hit. The float following a spilled three-float HFA was passed at the
wrong stack offset. The attached regression tests reproduce the ABI
mismatch independently of Goose or the physics library.
Both the HFA and scalar-float cases come from the same generic stack-slot
rounding in arm64_pcs_aux, so I have kept them in one patch. The variadic
alignment changes preserve the existing variadic layout when the fixed
arguments now end partway through an eight-byte slot. The patch is scoped
to fixed floating-point arguments on Apple arm64; it does not attempt to
address other Apple ABI differences, including small integer stack slots.
Apple's ABI documentation describes the smaller fixed stack slots and
the separate eight-byte slot rule for variadic arguments:
https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms
Based on canonical mob at 43c7708b85681a2fd4451c8a541af4494a8919b2.
Public patch branch (only the fix and ABI tests):
https://github.com/aardappel/tinycc/tree/codex/apple-arm64-float-stack
Patch commit:
https://github.com/aardappel/tinycc/commit/104441ba1d4f0f91efebe5e884571b7a82566002
Validation:
- macOS 15 arm64, Apple Clang 17.0.0: the three fixed-argument tests fail
in BOTH directions before the fix and pass afterward, with the native
test driver compiled at both -O0 and -O2. The variadic test passes
before and after the fix.
- Full upstream make test passes on macOS arm64 and Linux x86_64
(GCC 13.3).
- The downstream Goose macOS suite, including the original physics
regression, also passes with the compiler fix.
The before/after workflow is on a separate verification branch and is
not part of the submitted patch:
https://github.com/aardappel/tinycc/actions/runs/38163007158
Downstream CI:
https://github.com/aardappel/goose/actions/runs/38123932979/job/114424223620
To reproduce on an Apple Silicon Mac, check out the public patch branch
and run the following from the repository root. This first restores only
the unpatched compiler source while retaining the new regression tests:
git show 43c7708b:arm64-gen.c > arm64-gen.c
./configure --cc=clang
make
make -C tests abitest-cc.exe
./tests/abitest-cc.exe -B. run_test=float_stack_hfa_test
./tests/abitest-cc.exe -B. run_test=float_stack_scalar_test
./tests/abitest-cc.exe -B. run_test=float_stack_align_test
Each test reports both "TinyCC -> native failed" and
"native -> TinyCC failed" and exits unsuccessfully. Then restore the fix:
git show 104441ba:arm64-gen.c > arm64-gen.c
make
make test
All tests should pass. If this is not the preferred implementation, the
native/TinyCC disagreement and the regression tests should still be
useful as a bug report.
0001-arm64-pack-fixed-floating-point-stack-arguments-on-A.patch
Description: Binary data
_______________________________________________ Tinycc-devel mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/tinycc-devel
