Skip to content

AArch64: misaligned SP in the prologue of large frames (SIGBUS on Linux) #616

Closed as not planned

Description

@jsmorph

Since #583 (commit 0ef26da), the AArch64 expansion of Pallocframe in aarch64/Asmexpand.ml can store X15 and X30 through an SP that is misaligned by 8. Linux enables SP alignment checking at EL0, so the store raises SIGBUS.

The back link's offset linkofs is olink := align (4 * b.(bound_outgoing)) 8 (aarch64/Stacklayout.v), a multiple of 8 but not always of 16, while the frame size sz is a multiple of 16. When sz1 = sz - linkofs exceeds 512, the expansion emits

mov  x15, sp
sub  sp, sp, #sz1
stp  x15, x30, [sp]

When the outgoing arguments fill an odd number of 8-byte slots, sz1 is 8 modulo 16, and the stp uses a misaligned SP as its base. When sz1 is at most 512, the pre-indexed stp x15, x30, [sp, #-sz1]! has the aligned SP as its base, and no access uses the misaligned SP it writes back before the next sub restores alignment.

Reproducer (CompCert 3.18, aarch64-linux, Linux 7.0, Cortex-X925/A725):

#include <stdio.h>

double g(double a, double b, double c, double d, double e, double f, double h, double i,
         double j) {
  return a + b + c + d + e + f + h + i + j;
}

double f(int n) {
  volatile char buf[600];
  buf[n] = 1;
  return g(1, 2, 3, 4, 5, 6, 7, 8, buf[n]);
}

int main(void) {
  printf("%g\n", f(3));
  return 0;
}

ccomp -o repro repro.c && ./repro ends with "Bus error". The call to g passes one double on the stack, so linkofs is 8, and the prologue of f is mov x15, sp; sub sp, sp, #616; stp x15, x30, [sp]. With buf[300], sz1 is 328, the pre-indexed form is used, and the program prints 37.

This change rounds the first adjustment up to a multiple of 16 and stores the pair at the difference:

      let sz1 = Z.(sub sz linkofs) in
      let sz1a =
        if Z.(le sz1 (of_uint 512)) then sz1
        else Z.(mul (div (add sz1 (of_uint 15)) (of_uint 16)) (of_uint 16)) in
      if Z.(le sz1 (of_uint 512)) then begin
        emit (Pstp(X15, X30, ADpreincr(XSP, I64.repr (Z.neg sz1))));
        emit (Pcfi_adjust sz1)
      end else begin
        expand_addimm64 XSP XSP (Ptrofs.repr (Z.neg sz1a));
        emit (Pcfi_adjust sz1a);
        emit (Pstp(X15, X30, ADimm(XSP, I64.repr (Z.sub sz1a sz1))))
      end;
      let sz2 = Z.(sub sz sz1a) in

With it, the reproducer prints 37, and about 70,000 test calls of generated C programs pass on the same host. Master has the same code as 3.18.

Activity

  1. jsmorph commented on Nov 17, 2026

    @jsmorph
    Author

    Duplicate of #614. The same misaligned SP raises SIGBUS on Linux/aarch64, which checks SP alignment at EL0.

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