| Issue |
237864
|
| Summary |
[SafeStack][AArch64] Scalable-vector alloca moved to the unsafe stack gets a fixed 16-byte slot; SVE store overruns the caller's unsafe-stack objects
|
| Labels |
|
| Assignees |
|
| Reporter |
zjy-dev
|
When an SVE local escapes, SafeStack moves it to the unsafe stack but reserves only 16 bytes for it,
regardless of the vector length. With VL > 128 bits the store to the local writes past its slot into
the caller's unsafe-stack frame.
```c
#include <arm_sve.h>
void sink(svint32_t *p);
void f(void) {
svint32_t v = svdup_s32(-1);
sink(&v); /* escapes -> moved to the unsafe stack */
}
```
Compile flag: `clang --target=aarch64-linux-gnu -O2 -march=armv8-a+sve -fsanitize=safe-stack -S`
```asm
f:
ldr x21, [x20, x19] // x21 = __safestack_unsafe_stack_ptr
sub x0, x21, #16 // &v = usp - 16
sub x9, x21, #16
str x9, [x20, x19] // unsafe frame = 16 bytes (fixed)
st1w {z0.s}, p0, [x21, x8, lsl #2] // x8 = -4: stores VL bytes at usp-16
bl sink
```
Run under `qemu-aarch64 -cpu max,sve-default-vector-length=N` with a caller that keeps a 32-byte
`buf` (filled with 'A') on the unsafe stack and prints it after calling `f()` (full program in `runtime.c`):
```
4141414141414141414141414141414141414141414141414141414141414141 (VL=16 bytes)
ffffffffffffffffffffffffffffffff41414141414141414141414141414141 (VL=32 bytes) # 16 bytes of caller buf overwritten
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff (VL=64 bytes) # 48 bytes overwritten
```
Without `-fsanitize=safe-stack` the same local is correctly placed with `addvl sp, sp, #-1`.
**Root cause.** `SafeStack::getStaticAllocaAllocationSize` (llvm/lib/CodeGen/SafeStack.cpp:202-207)
returns 0 when `AI->getAllocationSize()` is scalable. Since #180547, scalable accesses are classified as unsafe,
so the alloca goes into `StaticAllocas`, and `moveStaticAllocasToUnsafeStack` (SafeStack.cpp:543-545)
replaces size 0 with 1 ("Don't create zero-sized stack objects"). After alignment this becomes a fixed
16-byte slot. The dynamic path (SafeStack.cpp:676, `CreateAllocationSize`) already handles vscale correctly.
The new test `llvm/test/CodeGen/AArch64/safestack_scalar.ll` CHECKs this 16-byte layout (`i32 -16`) for
`<vscale x 16 x i8>`.
**Suggested fix.** Keep scalable static allocas out of the fixed-offset `StaticAllocas` layout: either
leave them on the safe stack (where the frame lowering sizes them correctly with `addvl`) or move them via the dynamic
path (`moveDynamicAllocasToUnsafeStack`, which computes `vscale * N` at run time).
Environment: clang 23.1.2 (85ac560262434c9ccfc0c183ec22d4138ed647fb), aarch64-linux-gnu; Compiler Explorer clang trunk
(2026-10-31) emits the same 16-byte unsafe frame.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs