Skip to content

aarch64 (macOS): misaligned SP in the prologue of a function with a frame > 512 bytes that passes 1–8 bytes of stack arguments → SIGBUS #614

Description

@kylezh

Versions: CompCert 3.18 (release tag v3.18, via opam coq-compcert 3.18; the tag's VERSION file, and therefore ccomp -version, still say 3.17), target aarch64-macos, macOS 26.7.1 on Apple Silicon (arm64). The Pallocframe expansion in aarch64/Asmexpand.ml on current master looks the same.

Reproducer (rep.c):

#include <stdio.h>
unsigned g(unsigned a,unsigned b,unsigned c,unsigned d,unsigned e,unsigned f,
           unsigned h,unsigned i,unsigned j)
{ return a+b+c+d+e+f+h+i+j; }
unsigned f(unsigned x)
{ volatile unsigned char buf[12000]; buf[0]=x; return g(x,1,2,3,4,5,6,7,buf[0]); }
int main(void) { printf("%u\n", f(3)); return 0; }
$ ccomp -o rep rep.c && ./rep ; echo $?
138                      # SIGBUS (EXC_ARM_SP_ALIGN)
$ cc -o rep rep.c && ./rep
34

With 11 arguments (more than 8 bytes of stack arguments) or a small frame, it runs correctly.

Generated prologue of f (ccomp -S):

_f:
	mov	x15, sp
	sub	sp, sp, #8192
	sub	sp, sp, #3832
	stp	x15, x30, [sp, #0]

The frame is 12024 bytes, not a multiple of 16: sp is misaligned when the frame is set up and when g is called. In Asmexpand.ml, Pallocframe(sz, linkofs, retaddrofs) subtracts sz1 = sz - linkofs first (the > 512 branch: expand_addimm64 XSP XSP (-sz1), then stp x15, x30, [sp]) and the remaining linkofs bytes afterwards. When the outgoing arguments make linkofs 8, sz1 is ≡ 8 (mod 16) and the intermediate sp (and the stp through it) is misaligned. macOS enforces 16-byte SP alignment and traps.

Expected: sp stays 16-byte aligned at every point where it is used as a base register (AAPCS64 §6.2.2; required on Darwin).

Context: found while building a CompCert/VST-verified filesystem kernel. A 9-argument function that had been inlined became a real call after it was exported. We work around it by never passing more than 8 integer arguments from code compiled with CompCert.

Activity

  1. xavierleroy commented on Nov 16, 2026

    @xavierleroy
    Contributor

    Thanks for the report. Since #583, AArch64 stack frames can be allocated in two steps: first, everything except the outgoing arguments area; then the outgoing arguments area. As shown in this example, even though the whole frame size is a multiple of 16, the sizes of the two parts may not, causing the SP misalignment issue observed. The simple fix is to 16-align the outgoing arguments area of the stack frame in aarch64/Stacklayout.v.

  2. added a commit that references this issue on Nov 18, 2026
    59bc044
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