Skip to content

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

Closed

Description

@ehartford

C functions that return (or take) a struct of 16 bytes or less are miscalled on every target except darwin-arm64 and windows-x86_64.

Symptom

Release validation 35388740596, linux-x86_64, release-raylib-spiral-uat: the window opens and OpenGL initialises, then every sample logs

WARNING: Requested image pixel (1523262040, 304) out of bounds
WARNING: Requested image pixel (1523262040, 310) out of bounds

for GetImageColor(image, x, y) (Color GetImageColor(Image image, int x, int y)). The first value is constant garbage; the second steps by 6 — it is the fixture's x.

Proof (cross-compiled object, no Linux needed)

type Img { data: *mut u8, w: i32, h: i32, m: i32, f: i32 }
impl Copy for Img
type Color { r: u8, g: u8, b: u8, a: u8 }
impl Copy for Color
extern fn get_color(img: Img, x: i32, y: i32) -> Color
fn sample(img: Img) -> i32:
    let c = get_color(img, 7, 9)
    c.r as i32

with build abi2.w --target x86_64-unknown-linux-gnu --emit-obj, llvm-objdump -d:

movups  %xmm0, (%rsp)          ; Img copied to the stack — correct (MEMORY class)
leaq    0x18(%rsp), %rdi       ; hidden return pointer in rdi  <-- wrong
movl    $0x7, %esi             ; x in esi (should be edi)
movl    $0x9, %edx             ; y in edx (should be esi)
callq   get_color
movzbl  0x18(%rsp), %eax       ; result read from memory (C returned it in eax)

SysV x86_64 returns a 4-byte integer-class struct in eax. The callee therefore reads x from edi (the low 32 bits of that stack address — the constant) and y from esi (the caller's x). With -> i32 instead of -> Color the same call is correct (edi=7, esi=9), which isolates the return.

Exact lines

src/Codegen.w c_abi_direct_struct_return_type and c_abi_direct_struct_param_type (~5088–5133) implement register coercion for codegen_windows_x86_64() and then if not codegen_c_abi_darwin_arm64(): return 0. On every other target a small struct is passed/returned as an LLVM first-class aggregate, which is not the C ABI: for {i8,i8,i8,i8} LLVM needs four return registers, has two, and demotes the return to sret. c_abi_needs_sret / c_abi_needs_indirect_param (size > 16, plus byval on x86_64) are right; only the ≤16-byte classification is missing.

Fix

  • aarch64 (Linux, Windows): the existing arm64 rule (HFA in vector registers, otherwise ≤8 bytes → iN, ≤16 → [2 x i64]) is the AAPCS64 rule, not a darwin one.
  • x86_64 SysV (Linux, macOS): classify each eightbyte INTEGER or SSE and coerce to iN / double / float or a two-element struct of those.

ABI change: alone in its batch, with --dump-abi / analyze matrix evidence and cross-target object checks for each (target × struct shape) cell. FnAbi.w's comment "Every other target passes aggregates by LLVM value and lets LLVM lower them" is the mistaken premise and goes with it.

Found from the with get campaign (PR #1191); affects any c_import library that returns Color, Vector2, etc. by value on Linux.

Activity

  1. ehartford commented on Sep 19, 2026

    @ehartford
    ContributorAuthor

    Corrections after building the matrix against our own clang (10 struct shapes x 5 targets):

    • windows-x86_64 is affected too: { float, float } (8 bytes) was returned through a hidden pointer; clang returns i64. The Windows rule required integer-only members; the Microsoft x64 rule is size-only (1/2/4/8).
    • darwin-arm64 had one wrong cell: a non-HFA struct with a float member ({ i32, f32 }) stayed an LLVM aggregate (w0 + s0); AAPCS64 puts it in one general register.
    • The src/FnAbi.w comment I quoted is about the internal With convention, not the C one; it is correct and stays.

    Fix is on branch fix-1195-c-abi-small-structs; test/behavior/behav_c_abi_small_structs.w asserts the declared signatures per target.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions