Skip to content

Pass and return small C structs the way the target C ABI does (#1195) - #1202

Merged
ehartford merged 1 commit into
mainfrom
fix-1195-c-abi-small-structs
Sep 19, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1195-c-abi-small-structs

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1195 — the linux-x86_64 raylib-spiral release UAT cell ("Requested image pixel (1523262040, 304) out of bounds").

Cause

compute_fn_abi coerced a C struct of at most 16 bytes to its register shape only on darwin-arm64 and windows-x86_64. Elsewhere it stayed an LLVM first-class aggregate, which is not the C ABI. On linux-x86_64 LLVM cannot return { i8, i8, i8, i8 } in registers and demotes it to a hidden pointer, so Color GetImageColor(Image, int, int) got the return slot's address as x and x as y.

Cross-compiled object, before: leaq 0x18(%rsp), %rdi; movl $7, %esi; movl $9, %edx. After: movl $7, %edi; movl $9, %esi, result read from eax.

Change

  • System V x86_64 (Linux, macOS): §3.2.3 eightbyte classification; coerce to iN / float / double or a pair. A struct that does not fit in the registers still free goes to the stack whole (byval).
  • aarch64: the existing rule was AAPCS64's, gated on Darwin; it now covers Linux and Windows. A non-HFA composite with a float member ({ i32, f32 }) goes in a general register (was w0 + s0, wrong on darwin too).
  • windows-x86_64: 1/2/4/8-byte structs go in one integer register whatever their members ({ float, float } was returned through a hidden pointer).

Ground truth is our own clang: 10 struct shapes x 5 targets. With's declarations match it, with two register-equivalent spellings (double for clang's <2 x float>; { double, i64 } for { double, i32 }, the tail being padding). test/behavior/behav_c_abi_small_structs.w asserts the signatures for linux_x86_64, windows_x86_64, linux_aarch64, darwin_aarch64, windows_aarch64.

Battery (seed-driven, ABI change alone in its batch)

build 284s, fixpoint ok, test-with-audits ok (977s), move-audit ok, test-green, last-green.

Workarounds noted in the diff for two bugs found on the way: #1196 (inferred return type invisible before the definition), #1197 (for x in xs over &[T]).

compute_fn_abi coerced a struct of at most 16 bytes to its register shape
only on darwin-arm64 and windows-x86_64. Everywhere else it stayed an LLVM
first-class aggregate, which is not the C ABI: on linux-x86_64 LLVM could
not return { i8, i8, i8, i8 } in registers and demoted it to a hidden
pointer, so `Color GetImageColor(Image, int, int)` received the return
slot's address as `x` and `x` as `y` (release UAT run 35388740596:
"Requested image pixel (1523262040, 304) out of bounds"; the cross-compiled
object shows `leaq 0x18(%rsp), %rdi; movl $7, %esi; movl $9, %edx`).

- System V x86_64 (Linux, macOS): classify each eightbyte INTEGER or SSE
  (§3.2.3) and coerce to iN / float / double, or a pair of them. A struct
  that does not fit in the registers still free goes to the stack whole.
- aarch64: the existing rule is AAPCS64's, not Darwin's; it now applies to
  Linux and Windows too, and a non-HFA composite with float members goes in
  general registers like any other.
- windows-x86_64: 1, 2, 4 and 8 byte structs go in one integer register
  whatever their members are ({ float, float } was returned through a
  hidden pointer).

Expected signatures in the test are clang's (our own build) for the same C
declarations on each target.

Fixes #1195
@ehartford
ehartford merged commit 16d9d24 into main Sep 19, 2026
5 checks passed
ehartford pushed a commit that referenced this pull request Sep 23, 2026
…1195 follow-up)

c_import emits `@[packed]` for every struct whose C alignment is 1, so
raylib's `Color { unsigned char r, g, b, a }` lowers to `<{ i8, i8, i8, i8 }>`.
`c_abi_sysv_classify` returned -1 for any packed struct, so Color stayed a
first-class LLVM aggregate. LLVM demoted the return to a hidden sret pointer
in rdi and shifted every argument. The linux-x86_64 release-raylib-spiral-uat
still failed after #1202 (nightly 35616828206: `GetImageColor` read
x = -342673224, and y = the caller's x). The hand-written extern repro in
#1195 used a non-packed Color, so it did not catch this.

Proof: the #1195 repro with `@[packed]` on Color, cross-emitted with
`--target x86_64-unknown-linux-gnu --emit-obj`, before the fix:
  movq %r14, %rdi ; movl %ebp, %esi ; movl %r15d, %edx ; callq get_color
and after the fix: movl %ebp, %edi ; movl %r14d, %esi ; callq get_color.

A packed struct is now classified at its packed offsets. A field whose offset
its alignment does not divide makes the object MEMORY (§3.2.3), which means
sret/byval. Clang agrees on all three shapes the test now pins:
<{i8 x4}> -> i32, packed {i32, u8} -> i40, packed {u8, i32} -> sret + byval.
Only SysV x86_64 changes. AAPCS64 and Microsoft x64 classify by size and
were already right, which is why the spiral passes on linux-aarch64.

(cherry picked from commit 41184d6)
ehartford added a commit that referenced this pull request Sep 23, 2026
…1195 follow-up) (#1369)

c_import emits `@[packed]` for every struct whose C alignment is 1, so
raylib's `Color { unsigned char r, g, b, a }` lowers to `<{ i8, i8, i8, i8 }>`.
`c_abi_sysv_classify` returned -1 for any packed struct, so Color stayed a
first-class LLVM aggregate. LLVM demoted the return to a hidden sret pointer
in rdi and shifted every argument. The linux-x86_64 release-raylib-spiral-uat
still failed after #1202 (nightly 35616828206: `GetImageColor` read
x = -342673224, and y = the caller's x). The hand-written extern repro in
#1195 used a non-packed Color, so it did not catch this.

Proof: the #1195 repro with `@[packed]` on Color, cross-emitted with
`--target x86_64-unknown-linux-gnu --emit-obj`, before the fix:
  movq %r14, %rdi ; movl %ebp, %esi ; movl %r15d, %edx ; callq get_color
and after the fix: movl %ebp, %edi ; movl %r14d, %esi ; callq get_color.

A packed struct is now classified at its packed offsets. A field whose offset
its alignment does not divide makes the object MEMORY (§3.2.3), which means
sret/byval. Clang agrees on all three shapes the test now pins:
<{i8 x4}> -> i32, packed {i32, u8} -> i40, packed {u8, i32} -> sret + byval.
Only SysV x86_64 changes. AAPCS64 and Microsoft x64 classify by size and
were already right, which is why the spiral passes on linux-aarch64.

(cherry picked from commit 41184d6)

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the fix-1195-c-abi-small-structs branch September 24, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C ABI: small structs are passed and returned as LLVM aggregates on Linux and windows-aarch64 (raylib GetImageColor reads a garbage x)

1 participant