| Issue |
242132
|
| Summary |
[X86] -mretpoline puts the callee address in R11 and overwrites a preserve_none R11D argument
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
With `-mretpoline`, an indirect call on x86-64 is lowered to `__llvm_retpoline_r11`, and `X86TargetLowering::EmitLoweredIndirectThunk` copies the call target into `R11` just before the call. It filters out registers used by the call with `llvm::replace(AvailableRegs, MO.getReg(), Register())`, which compares against `X86::R11` exactly, so an argument in the sub-register `R11D` is not seen. `preserve_none` passes its 11th 32-bit integer argument in `R11D` (`CC_X86_64_Preserve_None`), so the callee receives the low half of its own address instead of the argument. A full 64-bit argument in `R11` is detected and rejected ("calling convention incompatible with retpoline, no available registers"), so only the 32-bit case miscompiles.
```c
#include <stdio.h>
typedef unsigned __attribute__((preserve_none)) (*fp_t)(
unsigned, unsigned, unsigned, unsigned, unsigned, unsigned, unsigned,
unsigned, unsigned, unsigned, unsigned);
__attribute__((preserve_none, noinline))
unsigned f(unsigned a0, unsigned a1, unsigned a2, unsigned a3, unsigned a4,
unsigned a5, unsigned a6, unsigned a7, unsigned a8, unsigned a9,
unsigned a10) {
return a10;
}
__attribute__((noinline))
unsigned call(fp_t fp) {
return fp(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0x12345678u);
}
fp_t volatile gp = f;
int main(void) {
unsigned r = call(gp);
printf("got %x\n", r);
return r == 0x12345678u ? 0 : 1;
}
```
```console
$ clang -O2 -mretpoline t.c && ./a.out
got 87dbc140 # varies, low 32 bits of &f
$ clang -O2 t.c && ./a.out
got 12345678
```
At `-O2` the 11th argument is never materialized; the only write to R11 before the call is the callee address:
```asm
call:
...
mov r11, rdi
mov r12d, 1
...
mov r9d, 10
call __llvm_retpoline_r11
```
At `-O0` the constant is written to `r11d` and then overwritten by `movq -56(%rbp), %r11` right before `callq __llvm_retpoline_r11`. The thunk does `movq %r11, (%rsp); retq`, so the callee starts with R11D equal to the low half of its address.
Godbolt (trunk, asm and execution): https://godbolt.org/z/sbGz4564d
Versions: wrong at `-O0` and `-O2` with clang 19, 20, 21, 22, 23 and trunk (Godbolt clang trunk 6f8c0d84c339). Correct without `-mretpoline`. clang 18 and older ignore `preserve_none`. The 64-bit variant (11 `unsigned long long` arguments) fails with "calling convention incompatible with retpoline, no available registers" on all of them.
Where: `llvm/lib/Target/X86/X86ISelLowering.cpp`, `X86TargetLowering::EmitLoweredIndirectThunk`. On 64-bit the only candidate is `X86::R11`, and uses are removed with `llvm::replace(AvailableRegs, MO.getReg(), Register())`, which does not match `R11D` (or `R11W`/`R11B`) against `R11`. Windows `x86_regcall` also passes 32-bit values in `R11D` and goes through the same code (asm only, not executed).
Possible fix (not tested): compare overlapping registers when filtering, e.g. `if (MO.isReg() && MO.isUse()) for (Register &R : AvailableRegs) if (R && TRI->regsOverlap(R, MO.getReg())) R = Register();`, so an `R11D` argument makes R11 unavailable and the existing "no available registers" error is reported instead of silent wrong code.
Related but different: #241108 (KCFI check scratch registers, `EmitKCFICheck`), #239045 (stack-clash probe scratch R11), #232957 (preserve_none indirect musttail running out of registers).
cc @weiguozhi @phoebewang
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