| Issue |
242135
|
| Summary |
[X86] Windows CoreCLR stack probe overwrites an incoming ECX argument
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
A Windows x64 CoreCLR function whose frame is at least one probe page (4096 bytes) returns the low 32 bits of the TEB stack limit instead of its first `int` argument. The argument arrives in `ECX`. `emitStackProbeInlineWindowsCoreCLR64` zeros `RCX` and then loads `%gs:16` into `RCX`. It spills `RCX` only when the exact 64-bit register is a live-in (`MBB.isLiveIn(X86::RCX)`), so `ECX` is not saved. With `gs:16 == 0x42` the compare takes the fast path and the function returns `0x42`.
```c
void *esc;
__attribute__((noinline)) int ret1(int a) {
char buf[8000];
esc = buf;
return a;
}
__attribute__((noinline)) long long ret1_64(long long a) {
char buf[8000];
esc = buf;
return a;
}
__attribute__((noinline)) int ret1_small(int a) { return a; }
```
Compile with `--target=x86_64-pc-windows-coreclr`. Before `prolog-epilog`, `@ret1` has `liveins: $ecx` while `@ret1_64` has `liveins: $rcx`. At `-O2`, `ret1` has no save before the probe:
```asm
movl $8000, %eax
xorq %rcx, %rcx
movq %rsp, %rdx
subq %rax, %rdx
cmovbq %rcx, %rdx
movq %gs:16, %rcx
cmpq %rcx, %rdx
jae .LBB0_3
...
subq %rax, %rsp
movl %ecx, %eax
```
`ret1_64` does `movq %rcx, 8(%rsp)` before the `xorq` and reloads it. `ret1_small` (no probe) is `movl %ecx, %eax` and returns the argument.
Host re-exec of the stripped CoreCLR object (GS base set so `gs:16 == 0x42`) vs SysV twin:
- Expected: `ret1 12345678 small 12345678 ret64 123456789abcdef0`, exit 0
- Bad CoreCLR: `ret1 00000042 small 12345678 ret64 123456789abcdef0`, exit 1
- Host SysV (`t.c` + `linux_main.c`, no CoreCLR probe): prints the sentinel, exit 0
Versions (executed where noted; otherwise same asm pattern with probe and no RCX save for `ret1`, RCX save present for `ret1_64`):
- clang 15–17 and 19–23 at `-O0`/`-O2`: fails (executed: clang-15 `-O2`, clang-19 `-O0`)
- clang-24 `-O0`/`-O1`/`-O2`/`-O3`/`-Os`: fails (executed, `ret1 00000042`)
- clang-24 `-O2 -mllvm -opt-bisect-limit=0`: fails (not an IR pass; PEI expands `STACKALLOC_W_PROBING`)
- clang-18: not installed
- clang-24 / gcc `-O0`/`-O2` SysV twin: OK
Where: `PrologEpilogInserter` calls `X86FrameLowering::inlineStackProbe` → `emitStackProbeInline` → `emitStackProbeInlineWindowsCoreCLR64` (`X86FrameLowering.cpp` ~1053 at `cf44dd9e317e`; same live-in test still at HEAD `7a37be2989`). The save test is exact:
```cpp
const bool IsRCXLiveIn = MBB.isLiveIn(X86::RCX);
const bool IsRDXLiveIn = MBB.isLiveIn(X86::RDX);
```
`MachineBasicBlock::isLiveIn` compares `LI.PhysReg == Reg`. `ECX` does not match `RCX`. `isLiveInOverlappedWith` walks aliases and is not used here. `isEAXLiveIn` already walks `RAX/EAX/AX/AH/AL` and `emitPrologue` uses it; the CoreCLR probe does not do that for `RCX`/`RDX`.
Win64 C passes the first integer in `RCX`/`ECX` and the second in `RDX`/`EDX`. A second `int` is the same `IsRDXLiveIn` hole, not a separate bug.
Related but different: #239045 is the generic / clash probe's hard-wired `EAX`/`R11`, not CoreCLR/`RCX`. #37724 (closed) added the exact `isLiveIn(RCX)` test after the probe always spilled `RCX`/`RDX`; this is the hole that left for `ECX`. PR #106828 fixed virtual-register live-ins in the same helper, not the sub-register save.
Possible fix (not tested): treat `ECX`/`CX`/`CL` as live the way `isEAXLiveIn` walks the `AX` aliases (or call `isLiveInOverlappedWith(X86::RCX)`), and spill the full `RCX` before `XOR64rr`. Do the same for `EDX`/`DX`/`DL` and `RDX`.
Godbolt (asm only; CE cannot run the GS-base host re-exec): https://godbolt.org/z/ec4jq5h5K
Shows `ret1` CoreCLR probe: `xorq %rcx, %rcx` / `movq %gs:16, %rcx` with no preceding `movq %rcx` save at `-O0`/`-O2`. Runtime wrong-value proof remains the local host re-exec in `repro.sh` / `driver.c` (`gs:16 == 0x42` → `ret1 00000042`).
cc @topperc @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