Skip to content

Preserve SP alignment when expanding AArch64 stack frames - #615

Open
kylezh wants to merge 1 commit into
AbsInt:masterfrom
kylezh:fix/aarch64-frame-sp-alignment
Open

kylezh wants to merge 1 commit into
AbsInt:masterfrom
kylezh:fix/aarch64-frame-sp-alignment

Conversation

@kylezh

@kylezh kylezh commented Nov 15, 2026

Copy link
Copy Markdown

Fixes #614.

A function with a large local buffer and stack-passed arguments can crash with SIGBUS in its prologue on AArch64 macOS. The reproducer calls a nine-argument function from a function containing a 12,000-byte volatile buffer.

The stack layout in aarch64/Stacklayout.v rounds the total frame size (sz) to 16 bytes, but rounds the back-link offset (linkofs) to only 8 bytes. Pallocframe in aarch64/Asmexpand.ml currently allocates the frame in two parts:

  1. Subtract sz - linkofs from SP and store the saved SP and return address at that intermediate SP.
  2. Subtract the remaining linkofs bytes to reach the final SP.

When linkofs is 8 modulo 16, the intermediate SP is misaligned. In the large-frame path, the separate stp instruction then accesses memory through that misaligned SP, which traps on macOS. The final frame size is already correct; execution faults before the second adjustment can restore alignment.

For the reproducer, sz = 12032 and linkofs = 8. The original sequence is:

mov x15, sp
sub sp, sp, #8192
sub sp, sp, #3832       // allocated 12024 bytes: SP is 8 modulo 16
stp x15, x30, [sp]     // SIGBUS here
sub sp, sp, #8          // would complete the 12032-byte allocation

The fix calculates:

linkdisp = linkofs mod 16
sz1 = sz - linkofs + linkdisp
sz2 = sz - sz1 = linkofs - linkdisp

Since the frame size is 16-aligned, both allocation sizes are now multiples of 16. The saved pair is stored at SP + linkdisp, where linkdisp is either 0 or 8. Its address remains exactly the same:

entry_SP - sz1 + linkdisp = entry_SP - sz + linkofs

The patched compiler emits the following for the reproducer:

mov x15, sp
sub sp, sp, #8192
sub sp, sp, #3840       // allocate all 12032 bytes with aligned SP
stp x15, x30, [sp, #8] // preserve the existing saved-register locations

This changes only the pseudo-instruction expansion. The frame layout and total allocation are unchanged, so outgoing arguments, locals, saved-register loads, and frame deallocation keep their existing offsets. The CFI adjustments continue to describe the actual allocation at each stage and sum to the same frame size. The existing AAPCS64 variadic register-save area is also unchanged.

The pre-indexed stp optimization remains available when linkdisp is zero and the first allocation fits its immediate range. When linkdisp is 8, use a separate aligned SP adjustment followed by an offset store, including for small frames. This avoids introducing a misaligned intermediate SP in either path.

Validation:

  • Built CompCert for aarch64-macos with OCaml 4.14.5, Coq 8.20.1, and Menhir 20260209.
  • Compiled and ran the issue's C reproducer with both versions on Apple Silicon: the original compiler produces SIGBUS (signal 10); the patched compiler prints 34 and exits 0.
  • Inspected the patched compiler's generated assembly, including the 12032-byte allocation and saved-pair offset of 8.
  • make -C test -j8 all and make -C test test pass. The harness skips struct.cc2compcert and struct.compcert2cc.
  • Independently checked the allocation arithmetic, alignment, and saved-link address for 1,048,576 valid frame layouts up to 16 KiB. These are arithmetic checks, not a million compiler executions.
  • git diff --check passes.

The build emits linker warnings about absent OCaml str/unix search directories but links successfully. The upstream tests live in the separate CompCert-small-tests submodule; this change does not update that submodule or add a permanent regression test there.

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.

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

1 participant