| Issue |
244137
|
| Summary |
[X86][WinEH][CFGuard] setjmp long-jump target is one byte past the return address when a nop is emitted after the call
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
SimonCropp
|
Since LLVM 22, on `x86_64-pc-windows-msvc` with Control Flow Guard enabled, the CFG long-jump target recorded for a `setjmp` call can point one byte past the call's real return address. Windows then rejects the `longjmp` and kills the process.
It happens when `_setjmp` is an `invoke` followed by an EH state change, which is the normal case for C++ code that calls `setjmp` with a destructor in scope (for example libjpeg/libpng error handling). Since #144745, `X86AsmPrinter` emits a padding `nop` after such calls. `CFGuardLongjmp` attaches the long-jump label to the call as a post-instruction symbol, but `AsmPrinter::emitFunctionBody` emits post-instruction symbols only after `X86AsmPrinter::emitInstruction` returns, and by then the `nop` is already out. So the label (and its `.gljmp$y` entry) lands after the `nop`, while `setjmp` returns to the `nop` itself.
### Reduced test (llc)
```llvm
target triple = "x86_64-pc-windows-msvc"
@env = dso_local global [256 x i8] zeroinitializer, align 16
declare i32 @_setjmp(ptr, ptr) returns_twice
declare ptr @llvm.frameaddress.p0(i32)
declare void @cleanup()
declare i32 @__CxxFrameHandler3(...)
define i32 @f() personality ptr @__CxxFrameHandler3 {
entry:
%fp = call ptr @llvm.frameaddress.p0(i32 0)
%r = invoke i32 @_setjmp(ptr @env, ptr %fp) #0
to label %cont unwind label %ehcleanup
cont:
ret i32 %r
ehcleanup:
%cp = cleanuppad within none []
call void @cleanup() [ "funclet"(token %cp) ]
cleanupret from %cp unwind to caller
}
attributes #0 = { returns_twice }
!llvm.module.flags = !{!0}
!0 = !{i32 2, !"cfguard", i32 2}
```
`llc -mtriple=x86_64-pc-windows-msvc`
llc 21.1.0 (correct, label on the return address):
```
callq _setjmp
$cfgsj_f0:
.Ltmp1:
nop
...
.section .gljmp$y,"dr"
.symidx $cfgsj_f0
```
llc 22.1.0 and trunk (label one byte late):
```
callq _setjmp
nop
$cfgsj_f0:
.Ltmp1: # EH_LABEL
...
.section .gljmp$y,"dr"
.symidx $cfgsj_f0
```
A FileCheck expectation that passes on 21 and fails on 22/trunk:
```
; CHECK-LABEL: f:
; CHECK: callq _setjmp
; CHECK-NEXT: $cfgsj_f0:
; CHECK: .section .gljmp$y,"dr"
; CHECK-NEXT: .symidx $cfgsj_f0
```
### End-to-end reproducer (clang-cl)
```cpp
// clang-cl /O2 /EHsc /guard:cf repro.cpp && repro.exe
#include <setjmp.h>
#include <stdio.h>
static jmp_buf env;
struct Cleanup {
~Cleanup() { puts("~Cleanup"); }
};
__declspec(noinline) void fail() {
longjmp(env, 1);
}
__declspec(noinline) int run() {
Cleanup c; // live cleanup => setjmp is emitted as an invoke
if (setjmp(env)) {
puts("recovered");
return 1;
}
fail();
return 0;
}
int main() {
int r = run();
printf("run() = %d\n", r);
return 0;
}
```
Expected output: `recovered`, `~Cleanup`, `run() = 1`.
Results on Compiler Explorer's Windows clang-cl compilers, built and executed there:
| Compiler | Flags | Result |
|---|---|---|
| clang-cl 18.1.0 | `/O2 /EHsc /guard:cf` | expected output, exit 0 |
| clang-cl 22.1.6 (MSVC v19.50) | `/O2 /EHsc /guard:cf` | no output, exit `0xC0000409` |
| clang-cl 23.1.3 (MSVC v19.51) | `/O2 /EHsc /guard:cf` | no output, exit `0xC0000409` |
| clang-cl 22.1.6 | `/O2 /EHsc /guard:cf -fuse-ld=lld` | no output, exit `0xC0000409` |
| clang-cl 22.1.6 | `/O2 /EHsc` (no CFG) | expected output, exit 0 |
| clang-cl 22.1.6 | `/O2 /EHsc /guard:cf`, without `Cleanup c;` | expected output, exit 0 |
| clang-cl 22.1.6 | `/O2 /EHsc -Xclang -cfguard -fuse-ld=lld /link /guard:cf` | no output, exit `0xC0000409` |
| clang-cl 22.1.6 | `/O2 /EHsc -Xclang -cfguard -fuse-ld=lld /link /guard:cf,nolongjmp` | expected output, exit 0 |
The last two rows isolate the long-jump table: same objects, and the crash disappears when the linker omits the table. The clang-cl 22.1.6 assembly for `run()` shows the same pattern as the llc test:
```
lea rcx, [rip + env]
lea rdx, [rbp - 32]
call _setjmp
nop
"$cfgsj_?run@@YAHXZ0":
.Ltmp3: # EH_LABEL
test eax, eax
```
clang-cl 18.1.0 places `"$cfgsj_?run@@YAHXZ0":` directly after `call _setjmp`.
Inside a process with CET user shadow stacks enabled (for example any .NET 9+ host), the CRT's `longjmp` always takes the `RtlUnwindEx(STATUS_LONGJUMP)` path. `RtlUnwindEx` rejects the target and returns, and `longjmp` then faults on a clobbered register (`0xC0000005`), which makes the cause hard to see. Found via mono/SkiaSharp#5645: in its x64 `libSkiaSharp.dll`, 10 of the 21 long-jump table entries (all the libjpeg `setjmp` sites) are off by one, and any JPEG decode error kills the host process.
### Analysis
- `CFGuardLongjmp::runOnMachineFunction` calls `Setjmp->setPostInstrSymbol(MF, SjSymbol)` and `MF.addLongjmpTarget(SjSymbol)`.
- `X86AsmPrinter::emitInstruction` lowers the call via `emitCallInstruction(TmpInst)` and then calls `maybeEmitNopAfterCallForWindowsEH(MI)`. That emits a `nop` when the next instruction is an `EH_LABEL` (with an EH personality) or `SEH_BeginEpilogue`.
- `AsmPrinter::emitFunctionBody` emits `MI.getPostInstrSymbol()` only after `emitInstruction(&MI)` has returned, so after the `nop`.
The padding `nop` itself is fine: it keeps the return address inside the call's IP2State region. The long-jump label just has to be emitted between the call and the `nop`, because the return address is the `nop`'s address. One option is to emit the call's post-instruction symbol before the padding in `X86AsmPrinter` and have the generic code skip it. Another is a hook that lets the target emit the padding after the post-instruction symbol. Other post-instruction symbols on padded calls may be misplaced the same way; I only checked the CFG long-jump label, which is where it causes a runtime failure.
Introduced by #144745 (b933f0c376c9, first released in LLVM 22). Related but different: #213548.
**Workaround:** link with `/guard:cf,nolongjmp` (lld-link) to omit the long-jump table, keeping CFG for indirect calls. With the clang-cl driver this needs `-Xclang -cfguard` instead of `/guard:cf`, because the driver appends its own `-guard:cf` after user `/link` arguments, which turns the table back on.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs