https://github.com/libffi/libffi/issues/1088 https://github.com/libffi/libffi/pull/1093 From 0134c1aa206ccdb2e25a1b80cf9b1cd03bfffd4d Mon Sep 17 00:00:00 2001 From: Anthony Green Date: Sat, 28 Nov 2026 19:16:35 -0500 Subject: [PATCH] x86: give i386 closures an aligned copy of under-aligned arguments On i386, GCC passes a structure at 4-byte alignment unless some member needs 16; alignment on the structure type itself is ignored. ffi_closure_inner placed such an argument correctly but pointed avalue straight at its 4-aligned stack slot. A handler that dereferences it as the over-aligned type may copy it with aligned SSE moves, and cls_struct_overaligned.c segfaulted when built with -march=x86-64 (movdqa on the 128-aligned block). A compiled callee reads the slot unaligned instead. When the slot is less aligned than the type, copy the argument into an aligned alloca buffer and pass that. In an i386 Debian trixie container (gcc 14, CC="gcc -march=x86-64") the testsuite goes from 4334 passes and 2 failures (that test, at -O0 and -O2) to 4336 passes and none. Fixes #1088. Co-Authored-By: Claude Opus 5.5 --- src/x86/ffi.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/x86/ffi.c b/src/x86/ffi.c index aa816733f..25ba40755 100644 --- a/src/x86/ffi.c +++ b/src/x86/ffi.c @@ -566,6 +566,20 @@ ffi_closure_inner (struct closure_frame *frame, char *stack) valp = argp; argp += za; } + + /* The slot is only as aligned as the ABI places it, which can be + less than the type's own alignment (a structure aligned by an + attribute, say). The handler may rely on the latter, for + instance by copying the value with aligned SSE moves, so hand + it an aligned copy. */ + if (((size_t) valp & (ty->alignment - 1)) != 0) + { + char *copy = alloca (z + ty->alignment - 1); + + copy = (char *)FFI_ALIGN (copy, ty->alignment); + memcpy (copy, valp, z); + valp = copy; + } } avalue[i] = valp;