Issue 243318
Summary [X86] i386 `-fsplit-stack` overwrites a thiscall / regparm(3) ECX argument
Labels new issue
Assignees
Reporter 20000419
    i386 `x86_thiscallcc` passes the first integer argument in ECX. `regparm(3)` marks the first three integer arguments `inreg`, and those land in EAX, EDX, ECX. `-fsplit-stack` then overwrites ECX in the prologue whenever the final frame is at least 256 bytes.

`X86FrameLowering::GetScratchRegister` returns ECX as the 32-bit primary scratch unless the convention is `X86_FastCall`, `Fast`, or `Tail` (those return EAX) or the function has a nest argument (EDX). `ThisCall` and a C function with `inreg` are not special-cased, so it returns ECX. `adjustForSegmentedStacks` asserts the scratch is not live-in; that assert is compiled out of these release builds. When `StackSize >= 256` (`kSplitStackAvailable`), the Linux check block does `leal -StackSize(%esp), %ecx` and `cmpl %gs:0x30, %ecx`. PEI runs this as a new block in front of the prologue, so the body's `movl %ecx, %eax` reads the lea result.

```c
/* t.c — compile separately from the caller; one TU lets IPSCCP fold the returned argument. */
__attribute__((noinline, thiscall, used))
int victim(int self, int x) {
  char buf[512];
  buf[0] = 1;
  buf[511] = (char)x;
  asm volatile("" : "+m"(buf));
  return self;
}
```

```c
/* driver.c */
#include <stdio.h>
#include <unistd.h>
__attribute__((thiscall)) int victim(int self, int x);
__attribute__((no_split_stack, used))
void __morestack(void) {
  const char msg[] = "morestack\n";
  write(2, msg, sizeof msg - 1);
  _exit(97);
}
__attribute__((no_split_stack))
int main(void) {
  int r = victim(0x12345678, 1);
  printf("%08x\n", (unsigned)r);
  return r == 0x12345678 ? 0 : 1;
}
```

```console
$ clang -m32 -O2 -fsplit-stack -c t.c -o t.o
$ clang -m32 -O0 -c driver.c -o d.o
$ clang -m32 t.o d.o -o bad && qemu-i386 ./bad
# prints a stack address (not 12345678), exit 1
$ clang -m32 -O2 -c t.c -o ok.o && clang -m32 ok.o d.o -o ok && qemu-i386 ./ok
12345678
$ gcc -m32 -O2 -fsplit-stack -c t.c && ... && qemu-i386 ./g
12345678
```

`llc -mtriple=i386-unknown-linux-gnu` of the IR emits:

```asm
victim:
	leal	-512(%esp), %ecx
	cmpl	%gs:48, %ecx
	jbe	.LBB0_1
	subl	$512, %esp
	movl	%ecx, %eax
	...
	retl	$4
```

`0x12345678` becomes `esp-512`. A frame the compiler keeps under 256 bytes compares `%esp` and returns the sentinel. The same function without `-fsplit-stack` returns the sentinel at every opt level. `-mllvm -opt-bisect-limit=0` still fails (not an IR pass).

`regparm(3)` variant (`__attribute__((noinline, regparm(3), used)) int victim(int a, int b, int c)` with the same 512-byte frame, returning `c`, called from a separate `no_split_stack` main with `c = 0x12345678`): argument 3 arrives in ECX; same scratch overwrite, prints a stack address. GCC 14.2 thiscall uses `leal -512(%esp), %edx` then `movl %ecx, %eax` (argument survives). GCC rejects `regparm(3)` with `-fsplit-stack`: `sorry, unimplemented: '-fsplit-stack' does not support 3 register parameters`. The Darwin i386 path in the same function already push/pops a live-in second scratch; the Linux primary scratch has no such save.

Versions (native `-m32` SIGSEGVs on the host — no IA32 emulation; every run is qemu-i386; bad output is a stack address, not a fixed constant):

- clang 15–17 and 19–24: `-fsplit-stack` thiscall fails at `-O0` through `-Os` (clang-24 also `-Oz`); `regparm(3)` fails the same way
- clang trunk `58561153d5fc` (2026-11-15): thiscall `-O0`/`-O2`/`-Os` and `regparm(3)` `-O2` with `-fsplit-stack` fail; CE clang trunk `abcbd442e332` (2026-11-16) emits the same `leal -512(%esp), %ecx` / `cmpl %gs:48, %ecx` / `movl %ecx, %eax`
- without `-fsplit-stack`: OK (`12345678`)
- clang-24 frame 32 bytes with `-fsplit-stack`: OK (compares `%esp`)
- clang-24 `-O2 -fsplit-stack -mllvm -opt-bisect-limit=0`: fails; `-enable-shrink-wrap=false`: fails
- clang-18: not installed
- gcc 14.2 `-fsplit-stack` thiscall `-O0`/`-O2`: OK; `regparm(3)`: rejected

Related but different: #239045 is stack-clash probe / `BuildStackAlignAND`, not split-stack. #54123 is missing `__morestack` adjustment for calls from split-stack to `no_split_stack`, not a live argument register. #42350 is wrong CFI on the split-stack cold block.

Possible fix (not tested): on Linux i386, do not use a live-in ECX as the split-stack scratch — pick EAX/EDX the way the fastcall and GCC thiscall paths do, or save and restore it the way the Darwin path already does, and reject `regparm(3)` if no free scratch remains.

Godbolt (asm of `t.c` only): https://godbolt.org/z/nvecYxPro
`-m32 -O2 -fsplit-stack` shows `leal -512(%esp), %ecx` and `cmpl %gs:48, %ecx`; without `-fsplit-stack` the probe is absent. CE single-TU execute folds the returned argument via IPSCCP (prints `12345678` even with `-fsplit-stack`), so the runtime wrong-value proof needs the separate TUs and `qemu-i386` commands above.

cc @rnk @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

Reply via email to