| Issue |
239045
|
| Summary |
[X86] -fstack-clash-protection probe code overwrites an incoming argument register (EAX with regparm/inreg on i386, R11 with preserve_none)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
With `-fstack-clash-protection`, the X86 inline stack probe uses a hard-wired scratch register (`EAX` on i386, `R11` on x86-64) in the entry prologue without checking whether that register carries an incoming argument. On i386 the first `regparm(3)` / `inreg` argument is in `EAX`, so a function with a large frame (probe loop) or a local aligned to the probe size (probed realignment) silently loses its first argument. The same happens on x86-64 to the 11th `preserve_none` argument, which is passed in `R11`.
This is not the shrink-wrapping case from #237885: it happens in the entry block, at `-O0`, and with `-mllvm -enable-shrink-wrap=false`, so the open fix #237945 (`canUseAsPrologue`) does not cover it.
### Reproducer 1: i386 `regparm(3)` (also `-mregparm=3`)
`f.c`:
```c
void g(char *);
__attribute__((regparm(3), noinline)) int f(int a, int b, int c) {
char buf[40000];
g(buf);
return a + b + c;
}
```
`main.c` (separate TU):
```c
#include <stdio.h>
__attribute__((regparm(3))) int f(int, int, int);
void g(char *p) { p[0] = 0; }
int main(void) { int r = f(1000, 20, 3); printf("%d\n", r); return r != 1023; }
```
```console
$ clang -m32 -O2 -fstack-clash-protection -c f.c && clang -m32 -O2 main.c f.o && ./a.out
-3333901 # varies; a stack address minus 36864 plus 23
$ clang -m32 -O2 -c f.c && clang -m32 -O2 main.c f.o && ./a.out
1023
```
The prologue (clang trunk, `-O2 -m32 -fstack-clash-protection`) overwrites `EAX` before it is copied anywhere:
```asm
f:
push ebp
push ebx
push edi
push esi
mov eax, esp # EAX = a is clobbered here
sub eax, 36864
.LBB0_1:
sub esp, 4096
mov dword ptr [esp], 0
cmp esp, eax
jne .LBB0_1
sub esp, 3148
mov esi, ecx
mov edi, edx
mov ebp, eax # "a" is read back from the clobbered EAX
```
The same function compiled with `-mregparm=3` instead of the attribute gives the same wrong result. A 32-byte local with `__attribute__((aligned(4096)))` instead of the big buffer also returns garbage, through the realignment path (`f_align` in the godbolt link). gcc 14.2 `-m32 -O2 -fstack-clash-protection` prints 1023.
### Reproducer 2: x86-64 `preserve_none`, 11th argument in R11
```c
__attribute__((preserve_none, noinline))
unsigned long long f(unsigned long long a0, unsigned long long a1,
unsigned long long a2, unsigned long long a3,
unsigned long long a4, unsigned long long a5,
unsigned long long a6, unsigned long long a7,
unsigned long long a8, unsigned long long a9,
unsigned long long a10) {
char buf[40000];
char *end = buf + 39999;
asm volatile("" : : "r"(buf), "r"(end) : "memory");
*(volatile char *)buf = 1;
*(volatile char *)end = 2;
return a10;
}
```
Called from another TU as `f(1, ..., 10, 0x12345678)`, it returns a stack address with `-fstack-clash-protection` (`mov r11, rsp; sub r11, 36864; ...; mov rax, r11`) and `0x12345678` without it. A 32-byte `aligned(4096)` local instead of the big buffer fails the same way (`mov r11, rsp; and r11, -4096; ...; mov rax, r11`).
Godbolt (clang trunk): i386 https://godbolt.org/z/nd3Mcfczn, preserve_none https://godbolt.org/z/f4Er9j57j
Note: the caller must be in a separate TU. In one TU, IPSCCP propagates the returned argument and hides the bad machine code.
### Related: `no_caller_saved_registers` loses R11
The same `BuildStackAlignAND` path also breaks `__attribute__((no_caller_saved_registers))` on x86-64: R11 must be preserved there, but a 32-byte `aligned(4096)` local with `-fstack-clash-protection` returns with R11 set to the aligned stack pointer (`ncsr_align.c`; gcc `-mgeneral-regs-only` keeps R11). Wrong with clang 15 to 17 and 19 to trunk; correct without `-fstack-clash-protection` or with `aligned(2048)`.
### Versions
- i386 regparm: wrong with clang 15.0.6, 16.0.4, 17.0.6, 19, 20, 21, 22, 23 and trunk (24.0.0 at cf44dd9e317e, and godbolt trunk f2823c28d856), at `-O0`, `-O1`, `-O2`, `-O3` and `-Os` (the aligned-local variant also fails on 15.0.6 and 17.0.6). Correct without `-fstack-clash-protection`. (18 not tested.)
- preserve_none: wrong with clang 19 to trunk at every `-O` level. clang 15 to 17 ignore `preserve_none`.
### Where
`llvm/lib/Target/X86/X86FrameLowering.cpp`:
- `emitStackProbeInlineGenericLoop` (line 939 at 605fe44a3) and `BuildStackAlignAND` (line 1453) both pick `Register FinalStackProbed = Uses64BitFramePtr ? X86::R11 : Is64Bit ? X86::R11D : X86::EAX;` and copy `SP` into it with no live-in check.
- `canUseAsPrologue` only checks `EFLAGS`, so the entry block is always accepted.
The same file already guards `EAX` elsewhere: `emitSPUpdate` checks `isEAXLiveIn(MBB)` before using `EAX` (line 283), and the `_chkstk` path checks `isEAXAlive` (line 2231). The inline-probe paths have no such check.
### Possible fix (not tested)
In both places, pick the scratch register with a live-in check (as `emitSPUpdate` does for `EAX`), for example reuse a free caller-saved register from `LivePhysRegs` of the entry block. If none is free, save and restore the live one around the probe (push/pop, as the `_chkstk` path does for `EAX`).
cc @phoebewang @RKSimon
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs