| Issue |
237885
|
| Summary |
[X86] Shrink-wrapped prologue's inline stack probe clobbers a live r11 (miscompile since LLVM 17)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
lbguilherme
|
On x86-64, when a function needs an inline stack probe loop (`"probe-stack"="inline-asm"`, frame larger than the probe-unroll threshold), shrink-wrapping can place the prologue in a block where `r11` is live-in. The probe loop uses `r11` as its bound (`FinalStackProbed` in `X86FrameLowering::emitStackProbeInlineGenericLoop`), so the live value is overwritten.
`X86FrameLowering::canUseAsPrologue` only rejects a block when `EFLAGS` is live-in; it does not check the scratch register used by the probe.
### Reproducer
```llvm
target triple = "x86_64-unknown-linux-gnu"
declare void @g(ptr, ptr)
define void @f(ptr %p, i64 %a, i64 %b, i64 %c, i64 %d, i64 %e) "probe-stack"="inline-asm" {
entry:
%buf = alloca [36928 x i8], align 8
%x = load ptr, ptr %p, align 8
%p1 = getelementptr inbounds i8, ptr %p, i64 8
%y = load ptr, ptr %p1, align 8
%p2 = getelementptr inbounds i8, ptr %p, i64 16
%z = load ptr, ptr %p2, align 8
%old = atomicrmw add ptr %z, i64 1 monotonic, align 8
%neg = icmp slt i64 %old, 0
br i1 %neg, label %spin, label %done
spin:
br label %spin
done:
store ptr %x, ptr %buf, align 8
%b1 = getelementptr inbounds i8, ptr %buf, i64 8
store ptr %y, ptr %b1, align 8
%b2 = getelementptr inbounds i8, ptr %buf, i64 16
store ptr %z, ptr %b2, align 8
%b3 = getelementptr inbounds i8, ptr %buf, i64 24
store i64 %a, ptr %b3, align 8
%b4 = getelementptr inbounds i8, ptr %buf, i64 32
store i64 %b, ptr %b4, align 8
%b5 = getelementptr inbounds i8, ptr %buf, i64 40
store i64 %c, ptr %b5, align 8
%b6 = getelementptr inbounds i8, ptr %buf, i64 48
store i64 %d, ptr %b6, align 8
%b7 = getelementptr inbounds i8, ptr %buf, i64 56
store i64 %e, ptr %b7, align 8
call void @g(ptr %p, ptr %buf)
ret void
}
```
```
llc -O2 -mtriple=x86_64-unknown-linux-gnu repro.ll -o -
```
### Actual output (llc 22.1.8)
```asm
f:
movq (%rdi), %rax
movq 8(%rdi), %r10
movq 16(%rdi), %r11 # %z is live in r11
lock incq (%r11)
jle .LBB0_1
# %bb.2: # %done -- shrink-wrapped prologue
movq %rsp, %r11 # <-- probe bound overwrites %z
subq $36864, %r11
.LBB0_3:
subq $4096, %rsp
movq $0, (%rsp)
cmpq %r11, %rsp
jne .LBB0_3
subq $72, %rsp
movq %rax, 8(%rsp)
movq %r10, 16(%rsp)
movq %r11, 24(%rsp) # <-- stores rsp-36864 instead of %z
...
callq g@PLT
```
### Expected (same llc with `-enable-shrink-wrap=false`)
```asm
f:
movq %rsp, %r11 # probe loop in the entry block
subq $36864, %r11
.LBB0_1:
subq $4096, %rsp
movq $0, (%rsp)
cmpq %r11, %rsp
jne .LBB0_1
subq $72, %rsp
movq (%rdi), %rax
movq 8(%rdi), %r10
movq 16(%rdi), %r11 # %z loaded after the probe
lock incq (%r11)
jle .LBB0_3
...
movq %r11, 24(%rsp) # correct
```
Linking the object with this driver prints `miscompiled: buf[2]=0x7ffd..., expected 0x...` with the default options and `ok` with `-enable-shrink-wrap=false`:
```c
#include <stdio.h>
void f(void **p, long a, long b, long c, long d, long e);
static long counter;
static void *slots[3] = {0, 0, &counter};
void g(void **p, void **buf) {
if (buf[2] != p[2]) { printf("miscompiled: buf[2]=%p, expected %p\n", buf[2], p[2]); return; }
puts("ok");
}
int main(void) { f(slots, 1, 2, 3, 4, 5); return 0; }
```
### Regression range
- Good: LLVM 16 (16.0.5), and `759b21762634b906ff545353deae47dfbfc45c0b`.
- Bad: LLVM 17.0.2, 18.1.8, 22.1.8, and 23.1.4 (via rustc nightly); first bad commit is `02ba5b8c6b9f0c1ce6df421db5dd5eb307d7d27d` ("Ignore load/store until stack address computation", D152213).
That commit lets shrink-wrapping move the prologue past the loads and the `atomicrmw`, which previously pinned it to the entry block. The underlying problem looks older: `canUseAsPrologue` does not account for `r11` being clobbered by the inline probe loop. A possible fix is to also return `false` from `canUseAsPrologue` when the function needs an inline probe loop and `R11` is live-in to the candidate block. The probe-call path (`emitStackProbeCall`) also uses scratch registers and may need the same check; I have not tested it.
Originally found through Rust, in safe code: https://github.com/rust-lang/rust/issues/165204
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs