llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[AArch64] Miscompilation due to integer overflow in immediate offset for stack store/load instructions with preserve_all

Open simon902 opened this issue 5 months ago • 3 comments

Description

In the AArch64 backend, a miscompilation occurs when a large memory offset overflows the immediate field of a stack store/load instruction in the prolog or epilog code.

In a Release build, this overflow happens silently. The compiler generates negative stack offsets in the final assembly that write below the stack pointer (corrupting memory). In a Debug build, this is caught by the following assert in AArch64FrameLowering.cpp:

    assert((!RPI.isPaired() ||
            (!RPI.isScalable() && RPI.Offset >= -64 && RPI.Offset <= 63) ||
            (RPI.isScalable() && RPI.Offset >= -256 && RPI.Offset <= 255)) &&
           "Offset out of bounds for LDP/STP immediate");

Steps to reproduce

  1. Compile the following C program (bug.c) with clang -o bug bug.c:
__attribute__((preserve_all))
void trigger_stack_spill() {
    asm volatile (
        ""
        :
        :
        : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9",
          "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18",
          "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27",
          "x28", "x29", "x30",
          "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
          "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
          "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
          "v30", "v31",
          "memory", "cc"
    );

}

int main() {
    trigger_stack_spill();
    return 0;
}
  1. In the prolog (and epilog), the immediate overflows the 7-bit immediate for stp and wraps around, resulting in out-of-bounds negative stack offsets that corrupts memory:
stp     x12, x11, [sp, #480]
stp     x10, x9, [sp, #496]
stp     x29, x30, [sp, #-512]
stp     x28, x27, [sp, #-496]
stp     x26, x25, [sp, #-480]

Environment

LLVM Version: Reproduced on both main (commit: f5b589e5cc500c3e10e02ed9ae2a275f6c7e3184) and the 20.1.8 release.

simon902 avatar Jun 18 '26 11:06 simon902

@llvm/issue-subscribers-backend-aarch64

Author: Simon Scherer (simon902)

## Description In the AArch64 backend, a miscompilation occurs when a large memory offset overflows the immediate field of a stack store/load instruction in the prolog or epilog code.

In a Release build, this overflow happens silently. The compiler generates negative stack offsets in the final assembly that write below the stack pointer (corrupting memory). In a Debug build, this is caught by the following assert in AArch64FrameLowering.cpp:

    assert((!RPI.isPaired() ||
            (!RPI.isScalable() &amp;&amp; RPI.Offset &gt;= -64 &amp;&amp; RPI.Offset &lt;= 63) ||
            (RPI.isScalable() &amp;&amp; RPI.Offset &gt;= -256 &amp;&amp; RPI.Offset &lt;= 255)) &amp;&amp;
           "Offset out of bounds for LDP/STP immediate");

Steps to reproduce

  1. Compile the following C program (bug.c) with clang -o bug bug.c:
__attribute__((preserve_all))
void trigger_stack_spill() {
    asm volatile (
        ""
        :
        :
        : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9",
          "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18",
          "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", "x27",
          "x28", "x29", "x30",
          "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9",
          "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19",
          "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29",
          "v30", "v31",
          "memory", "cc"
    );

}

int main() {
    trigger_stack_spill();
    return 0;
}
  1. In the prolog (and epilog), the immediate overflows the 7-bit immediate for stp and wraps around, resulting in out-of-bounds negative stack offsets that corrupts memory:
stp     x12, x11, [sp, #<!-- -->480]
stp     x10, x9, [sp, #<!-- -->496]
stp     x29, x30, [sp, #-512]
stp     x28, x27, [sp, #-496]
stp     x26, x25, [sp, #-480]

Environment

LLVM Version: Reproduced on both main (commit: f5b589e5cc500c3e10e02ed9ae2a275f6c7e3184) and the 20.1.8 release.

I've opened a PR to fix this: https://github.com/llvm/llvm-project/pull/207026

The idea is - for GPR pairs whose byte offset exceeds the stp imm7 range, emit two individual str/ldr instructions instead. The unsigned-offset form of str/ldr encodes a 12-bit unsigned immediate scaled ×8, giving a range of 0–32760 bytes which is sufficient to cover any realistic callee-save area. Let me know if that makes sense, or we want to pursue another route for fixing.

Verified against aarch64-linux-gnu and aarch64-apple-macosx targets by assembling with -c (not just inspecting -S output) to confirm all emitted offsets are encodable.

Before the fix:

0000000000000000 <trigger_stack_spill>:
       0:      	sub	sp, sp, #0x260
       4:      	stp	q31, q30, [sp, #0x40]
       8:      	stp	q29, q28, [sp, #0x60]
       c:      	stp	q27, q26, [sp, #0x80]
      10:      	stp	q25, q24, [sp, #0xa0]
      14:      	stp	q23, q22, [sp, #0xc0]
      18:      	stp	q21, q20, [sp, #0xe0]
      1c:      	stp	q19, q18, [sp, #0x100]
      20:      	stp	q17, q16, [sp, #0x120]
      24:      	stp	q15, q14, [sp, #0x140]
      28:      	stp	q13, q12, [sp, #0x160]
      2c:      	stp	q11, q10, [sp, #0x180]
      30:      	stp	q9, q8, [sp, #0x1a0]
      34:      	str	x15, [sp, #0x1c0]
      38:      	stp	x14, x13, [sp, #0x1d0]
      3c:      	stp	x12, x11, [sp, #0x1e0]
      40:      	stp	x10, x9, [sp, #0x1f0]
      44:      	stp	x29, x30, [sp, #-0x200]
      48:      	stp	x28, x27, [sp, #-0x1f0]
      4c:      	stp	x26, x25, [sp, #-0x1e0]
      50:      	stp	x24, x23, [sp, #-0x1d0]
      54:      	stp	x22, x21, [sp, #-0x1c0]
      58:      	stp	x20, x19, [sp, #-0x1b0]
      5c:      	add	sp, sp, #0x30
      60:      	sub	sp, sp, #0x30
      64:      	ldp	x20, x19, [sp, #-0x1b0]
      68:      	ldp	x22, x21, [sp, #-0x1c0]
      6c:      	ldp	x24, x23, [sp, #-0x1d0]
      70:      	ldp	x26, x25, [sp, #-0x1e0]
      74:      	ldp	x28, x27, [sp, #-0x1f0]
      78:      	ldp	x29, x30, [sp, #-0x200]
      7c:      	ldp	x10, x9, [sp, #0x1f0]
      80:      	ldp	x12, x11, [sp, #0x1e0]
      84:      	ldp	x14, x13, [sp, #0x1d0]
      88:      	ldr	x15, [sp, #0x1c0]
      8c:      	ldp	q9, q8, [sp, #0x1a0]
      90:      	ldp	q11, q10, [sp, #0x180]
      94:      	ldp	q13, q12, [sp, #0x160]
      98:      	ldp	q15, q14, [sp, #0x140]
      9c:      	ldp	q17, q16, [sp, #0x120]
      a0:      	ldp	q19, q18, [sp, #0x100]
      a4:      	ldp	q21, q20, [sp, #0xe0]
      a8:      	ldp	q23, q22, [sp, #0xc0]
      ac:      	ldp	q25, q24, [sp, #0xa0]
      b0:      	ldp	q27, q26, [sp, #0x80]
      b4:      	ldp	q29, q28, [sp, #0x60]
      b8:      	ldp	q31, q30, [sp, #0x40]
      bc:      	add	sp, sp, #0x260
      c0:      	ret

After the fix:

0000000000000000 <trigger_stack_spill>:
       0:      	stp	q31, q30, [sp, #-0x220]!
       4:      	stp	q29, q28, [sp, #0x20]
       8:      	stp	q27, q26, [sp, #0x40]
       c:      	stp	q25, q24, [sp, #0x60]
      10:      	stp	q23, q22, [sp, #0x80]
      14:      	stp	q21, q20, [sp, #0xa0]
      18:      	stp	q19, q18, [sp, #0xc0]
      1c:      	stp	q17, q16, [sp, #0xe0]
      20:      	stp	q15, q14, [sp, #0x100]
      24:      	stp	q13, q12, [sp, #0x120]
      28:      	stp	q11, q10, [sp, #0x140]
      2c:      	stp	q9, q8, [sp, #0x160]
      30:      	str	x15, [sp, #0x180]
      34:      	stp	x14, x13, [sp, #0x188]
      38:      	stp	x12, x11, [sp, #0x198]
      3c:      	stp	x10, x9, [sp, #0x1a8]
      40:      	stp	x29, x30, [sp, #0x1b8]
      44:      	stp	x28, x27, [sp, #0x1c8]
      48:      	stp	x26, x25, [sp, #0x1d8]
      4c:      	stp	x24, x23, [sp, #0x1e8]
      50:      	stp	x22, x21, [sp, #0x1f8]
      54:      	str	x20, [sp, #0x208]
      58:      	str	x19, [sp, #0x210]
      5c:      	sub	sp, sp, #0x10
      60:      	add	sp, sp, #0x10
      64:      	ldr	x19, [sp, #0x210]
      68:      	ldr	x20, [sp, #0x208]
      6c:      	ldp	x22, x21, [sp, #0x1f8]
      70:      	ldp	x24, x23, [sp, #0x1e8]
      74:      	ldp	x26, x25, [sp, #0x1d8]
      78:      	ldp	x28, x27, [sp, #0x1c8]
      7c:      	ldp	x29, x30, [sp, #0x1b8]
      80:      	ldp	x10, x9, [sp, #0x1a8]
      84:      	ldp	x12, x11, [sp, #0x198]
      88:      	ldp	x14, x13, [sp, #0x188]
      8c:      	ldr	x15, [sp, #0x180]
      90:      	ldp	q9, q8, [sp, #0x160]
      94:      	ldp	q11, q10, [sp, #0x140]
      98:      	ldp	q13, q12, [sp, #0x120]
      9c:      	ldp	q15, q14, [sp, #0x100]
      a0:      	ldp	q17, q16, [sp, #0xe0]
      a4:      	ldp	q19, q18, [sp, #0xc0]
      a8:      	ldp	q21, q20, [sp, #0xa0]
      ac:      	ldp	q23, q22, [sp, #0x80]
      b0:      	ldp	q25, q24, [sp, #0x60]
      b4:      	ldp	q27, q26, [sp, #0x40]
      b8:      	ldp	q29, q28, [sp, #0x20]
      bc:      	ldp	q31, q30, [sp], #0x220
      c0:      	ret

kuroyukiasuna avatar Jul 01 '26 16:07 kuroyukiasuna

Approach seems okay.

The alternative is that we could adjust SP multiple times: adjust, save GPRs, then adjust again, and save FPRs. Saves a few instructions, but maybe not worth the complexity if we don't end up actually splitting a significant number of stores.

efriedma-quic avatar Jul 01 '26 18:07 efriedma-quic