Skip to content

Commit 2472245

Browse files
dangowrtatgreen
authored andcommitted
aarch64: give small composites full stack slots on Darwin
Darwin packs scalar arguments on the stack to their natural alignment, and libffi applies the same rule to composites of up to 16 bytes that are not HFAs. Apple's compilers coerce such composites, structs and complex types alike, to an array of register-sized words, so each takes a pointer-aligned slot whose size is a multiple of the pointer size. A struct of two chars or a _Complex char after the eighth integer argument therefore lands at the wrong offset, as does every argument behind it, in ffi_call and in closures (cf. issue #750). Round the alignment and the occupied size of such a composite up to the pointer size on Apple targets. The generic cif->bytes accounting already reserves a full slot per argument, so the stack frame stays large enough. Fixes: 4330fdc ("Darwin/aarch64: Respect iOS ABI re: stack argument alignment") Assisted-by: LLM Signed-off-by: Daniel Golle <[email protected]>
1 parent fc345b3 commit 2472245

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

‎src/aarch64/ffi.c‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,23 @@ allocate_to_stack (struct arg_state *state, void *stack,
408408
return (char *)stack + nsaa;
409409
}
410410

411+
/* Darwin packs only scalars to their natural alignment; a non-HFA
412+
composite of up to 16 bytes still takes whole register-sized slots. */
413+
static void *
414+
allocate_composite_to_stack (struct arg_state *state, void *stack,
415+
const ffi_type *ty)
416+
{
417+
size_t alignment = ty->alignment;
418+
size_t size = ty->size;
419+
420+
#if defined (__APPLE__)
421+
alignment = FFI_ALIGN (alignment, sizeof (void *));
422+
size = FFI_ALIGN (size, sizeof (void *));
423+
#endif
424+
425+
return allocate_to_stack (state, stack, alignment, size);
426+
}
427+
411428
/* Allocate and copy a structure that is passed by value on the stack and
412429
return a pointer to it. */
413430
static void *
@@ -966,7 +983,7 @@ ffi_call_int (ffi_cif *cif, void (*fn)(void), void *orig_rvalue,
966983
adjusted and the argument is copied to memory at the
967984
adjusted NSAA. */
968985
state.ngrn = N_X_ARG_REG;
969-
dest = allocate_to_stack (&state, stack, ty->alignment, s);
986+
dest = allocate_composite_to_stack (&state, stack, ty);
970987
}
971988
}
972989
memcpy (dest, a, s);
@@ -1266,8 +1283,7 @@ ffi_closure_SYSV_inner (ffi_cif *cif,
12661283
else
12671284
{
12681285
state.ngrn = N_X_ARG_REG;
1269-
avalue[i] = allocate_to_stack(&state, stack,
1270-
ty->alignment, s);
1286+
avalue[i] = allocate_composite_to_stack (&state, stack, ty);
12711287
}
12721288
}
12731289
break;

0 commit comments

Comments
 (0)