| Issue |
244488
|
| Summary |
[X86][KCFI] -fsanitize-kcfi-arity: preserve_none function with 8 register arguments reads past ArityToRegMap (crash / invalid asm)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
`-fsanitize=kcfi -fsanitize-kcfi-arity` reads past the end of `ArityToRegMap` in `X86AsmPrinter::emitKCFITypeId` for a `preserve_none` function with 8 register arguments. Depending on the build, clang crashes, hits an assertion, or emits a `__cfi_` prefix that cannot be assembled.
```c
typedef __attribute__((preserve_none)) long (*pn8)(long, long, long, long,
long, long, long, long);
__attribute__((preserve_none)) long fn(long a, long b, long c, long d,
long e, long f, long g, long h) {
return a + b + c + d + e + f + g + h;
}
int main(void) {
void *volatile vp = (void *)fn;
pn8 p = (pn8)vp;
return (int)(p(1, 2, 3, 4, 5, 6, 7, 8) - 36);
}
```
`clang -fsanitize=kcfi -fsanitize-kcfi-arity -O1 -S t.c`:
- clang 23.1.5 and 24.0.0 (`d5a22c0d1c26`): segmentation fault in the X86 Assembly Printer on `@fn`, at `-O0`, `-O1` and `-O2`.
- clang 21.1.8 and 22.1.8: exit 0, but `__cfi_fn` contains `movl $498736786, %ah`, which the assembler rejects with "invalid operand for instruction".
- With `-c` instead of `-S`, clang 24.0.0 (`d5a22c0d1c26`) exits 0 and encodes `movl $0x1dba1e92, %eax` in `__cfi_fn`, i.e. arity 0 for an 8-argument function.
- Without `-fsanitize-kcfi-arity` all of these compile fine. clang 20 does not have the option.
Godbolt (clang trunk and assertions trunk, `-O1`): https://godbolt.org/z/T59ffhMq1
On Compiler Explorer, clang trunk prints `movl $498736786, %` (empty register name) in `__cfi_fn`, assertions trunk fails with `Assertion 'RegNo && RegNo < 388 && "Invalid register number!"' failed` in `X86ATTInstPrinter::getRegisterName`, and clang 22.1.0 segfaults.
In `X86AsmPrinter::emitKCFITypeId` (`llvm/lib/Target/X86/X86AsmPrinter.cpp`), the arity is 7 if the function has stack arguments, otherwise it is the number of GPR live-ins:
```cpp
const unsigned ArityToRegMap[8] = {X86::EAX, X86::ECX, X86::EDX, X86::EBX,
X86::ESP, X86::EBP, X86::ESI, X86::EDI};
int Arity;
if (MF.getInfo<X86MachineFunctionInfo>()->getArgumentStackSize() > 0) {
Arity = 7;
} else {
Arity = 0;
for (const auto &LI : MF.getRegInfo().liveins()) {
...
++Arity;
}
}
DestReg = ArityToRegMap[Arity];
```
The comment above it assumes the SysV limit of 6 integer argument registers. `preserve_none` passes all 8 arguments of `fn` in registers (R12, R13, R14, R15, RDI, RSI, RDX, RCX), so there are no stack arguments, `Arity` is 8, and `ArityToRegMap[8]` reads one element past the table. #142867 fixed a similar crash for functions with more than 6 arguments counting XMM registers; the case where more than 7 GPRs carry arguments is still not handled. The problem seems to be that the GPR live-in count is not limited to the range the table covers.
Versions: clang 21.1.8, 22.1.8, 23.1.5 and 24.0.0 (`d5a22c0d1c26`) are affected as described above; Compiler Explorer clang trunk (`b86056e175f6`) too. The code quoted above is unchanged on main (`aa8482fc9fe4`).
cc @scottconstable @samitolvanen
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