[llvm] 837a4ea - [X86] Avoid invalid ret for EH_RETURN with SHSTK (#219322)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 9 06:54:50 PDT 2026
Author: Linisha
Date: 2026-09-09T21:54:44+08:00
New Revision: 837a4eab85a2f4e638034a254a2bc74b1986b2ac
URL: https://github.com/llvm/llvm-project/commit/837a4eab85a2f4e638034a254a2bc74b1986b2ac
DIFF: https://github.com/llvm/llvm-project/commit/837a4eab85a2f4e638034a254a2bc74b1986b2ac.diff
LOG: [X86] Avoid invalid ret for EH_RETURN with SHSTK (#219322)
## Summary
Fixes #215298
When lowering `llvm.eh.return` on x86, LLVM rewrites the normal stack
return
address and then emits `ret`. This is incompatible with CET shadow
stacks:
the shadow stack still contains the original return address, so `ret`
raises
a control-protection fault.
This affects code using `__builtin_eh_return` with: -mshstk
-fcf-protection=full
## What changed
When SHSTK is enabled, the x86 `EH_RETURN` pseudo-instruction now
expands to:
```asm
movq %rcx, %rsp
popq %rcx
jmpq *%rcx
```
- The `pop` consumes the rewritten return target and advances the stack
pointer.
- `llvm.eh.return` performs a non-local control transfer to an
explicitly supplied handler address, which was never pushed by a
matching `call`. It therefore cannot satisfy `ret`'s architectural
shadow-stack matching requirement regardless of implementation, so
an indirect `jmp` is used instead.
- The existing `mov %rcx, %rsp; ret` sequence is preserved for targets
without SHSTK support.
- The expansion handles 32-bit and 64-bit targets, including the
existing
Windows/UEFI x64 indirect-jump variant.
## Testing
Added: `eh-return-shstk.ll`
The test verifies that:
- non-SHSTK targets continue to emit `ret`
- SHSTK targets emit `pop` followed by an indirect `jmp`
- machine instruction verification succeeds in both configurations
Validation performed:
```bash
ninja -C build llc FileCheck llvm-config count not
build/bin/llvm-lit -sv llvm/test/CodeGen/X86/eh-return-shstk.ll
```
Passed: 1 (100.00%)
The original C reproducer was also compiled with the rebuilt Clang. Its
`pop_frame` function now emits:
```asm
movq %rcx, %rsp
popq %rcx
jmpq *%rcx
```
Runtime execution of the reproducer could not be fully validated because
the
available environment is WSL2 without usable CET shadow-stack support.
The
compiler output and LLVM regression test both pass, but this does not
confirm the control-protection fault is resolved on real CET-enabled
hardware, only that the expected instruction sequence is now emitted.
---------
Co-authored-by: Phoebe Wang <phoebe.wang at intel.com>
Added:
llvm/test/CodeGen/X86/eh-return-shstk-32.ll
llvm/test/CodeGen/X86/eh-return-shstk.ll
Modified:
llvm/lib/Target/X86/X86ExpandPseudo.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Target/X86/X86ExpandPseudo.cpp b/llvm/lib/Target/X86/X86ExpandPseudo.cpp
index 4a8e8bef897f3..b5c196b29f958 100644
--- a/llvm/lib/Target/X86/X86ExpandPseudo.cpp
+++ b/llvm/lib/Target/X86/X86ExpandPseudo.cpp
@@ -399,6 +399,18 @@ bool X86ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
BuildMI(MBB, MBBI, DL,
TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr)
.addReg(DestAddr.getReg());
+ if (STI->hasSHSTK()) {
+ unsigned PopOpcode = STI->is64Bit() ? X86::POP64r : X86::POP32r;
+ unsigned JumpOpcode = X86::JMP32r;
+ if (Uses64BitFramePtr)
+ JumpOpcode = STI->isTargetWin64() || STI->isTargetUEFI64()
+ ? X86::JMP64r_REX
+ : X86::JMP64r;
+ BuildMI(MBB, MBBI, DL, TII->get(PopOpcode))
+ .addReg(DestAddr.getReg(), RegState::Define);
+ BuildMI(MBB, MBBI, DL, TII->get(JumpOpcode)).addReg(DestAddr.getReg());
+ MBB.erase(MBBI);
+ }
// The EH_RETURN pseudo is really removed during the MC Lowering.
return true;
}
diff --git a/llvm/test/CodeGen/X86/eh-return-shstk-32.ll b/llvm/test/CodeGen/X86/eh-return-shstk-32.ll
new file mode 100644
index 0000000000000..9ec821e484f64
--- /dev/null
+++ b/llvm/test/CodeGen/X86/eh-return-shstk-32.ll
@@ -0,0 +1,39 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=i386-pc-linux -verify-machineinstrs < %s | FileCheck %s --check-prefix=RET
+; RUN: llc -mtriple=i386-pc-linux -mattr=+shstk -verify-machineinstrs < %s | FileCheck %s --check-prefix=SHSTK
+
+define void @test(i32 %offset, ptr %handler) nounwind {
+; RET-LABEL: test:
+; RET: # %bb.0: # %entry
+; RET-NEXT: pushl %ebp
+; RET-NEXT: movl %esp, %ebp
+; RET-NEXT: pushl %eax
+; RET-NEXT: movl 12(%ebp), %ecx
+; RET-NEXT: movl 8(%ebp), %eax
+; RET-NEXT: movl %ecx, 4(%ebp,%eax)
+; RET-NEXT: leal 4(%ebp,%eax), %ecx
+; RET-NEXT: popl %eax
+; RET-NEXT: popl %ebp
+; RET-NEXT: movl %ecx, %esp
+; RET-NEXT: retl # eh_return, addr: %ecx
+;
+; SHSTK-LABEL: test:
+; SHSTK: # %bb.0: # %entry
+; SHSTK-NEXT: pushl %ebp
+; SHSTK-NEXT: movl %esp, %ebp
+; SHSTK-NEXT: pushl %eax
+; SHSTK-NEXT: movl 12(%ebp), %ecx
+; SHSTK-NEXT: movl 8(%ebp), %eax
+; SHSTK-NEXT: movl %ecx, 4(%ebp,%eax)
+; SHSTK-NEXT: leal 4(%ebp,%eax), %ecx
+; SHSTK-NEXT: popl %eax
+; SHSTK-NEXT: popl %ebp
+; SHSTK-NEXT: movl %ecx, %esp
+; SHSTK-NEXT: popl %ecx
+; SHSTK-NEXT: jmpl *%ecx
+entry:
+ call void @llvm.eh.return.i32(i32 %offset, ptr %handler)
+ unreachable
+}
+
+declare void @llvm.eh.return.i32(i32, ptr)
diff --git a/llvm/test/CodeGen/X86/eh-return-shstk.ll b/llvm/test/CodeGen/X86/eh-return-shstk.ll
new file mode 100644
index 0000000000000..44adddd677c06
--- /dev/null
+++ b/llvm/test/CodeGen/X86/eh-return-shstk.ll
@@ -0,0 +1,55 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu -verify-machineinstrs < %s | FileCheck %s --check-prefix=RET
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu -mattr=+shstk -verify-machineinstrs < %s | FileCheck %s --check-prefix=SHSTK
+; RUN: llc -mtriple=x86_64-pc-windows-msvc -mattr=+shstk -verify-machineinstrs < %s | FileCheck %s --check-prefix=WIN64
+; RUN: llc -mtriple=x86_64-uefi -verify-machineinstrs < %s | FileCheck %s --check-prefix=WINRET
+; RUN: llc -mtriple=x86_64-uefi -mattr=+shstk -verify-machineinstrs < %s | FileCheck %s --check-prefix=WIN64
+
+define void @test(i64 %offset, ptr %handler) nounwind {
+; RET-LABEL: test:
+; RET: # %bb.0: # %entry
+; RET-NEXT: pushq %rbp
+; RET-NEXT: movq %rsp, %rbp
+; RET-NEXT: movq %rsi, 8(%rbp,%rdi)
+; RET-NEXT: leaq 8(%rbp,%rdi), %rcx
+; RET-NEXT: popq %rbp
+; RET-NEXT: movq %rcx, %rsp
+; RET-NEXT: retq # eh_return, addr: %rcx
+;
+; SHSTK-LABEL: test:
+; SHSTK: # %bb.0: # %entry
+; SHSTK-NEXT: pushq %rbp
+; SHSTK-NEXT: movq %rsp, %rbp
+; SHSTK-NEXT: movq %rsi, 8(%rbp,%rdi)
+; SHSTK-NEXT: leaq 8(%rbp,%rdi), %rcx
+; SHSTK-NEXT: popq %rbp
+; SHSTK-NEXT: movq %rcx, %rsp
+; SHSTK-NEXT: popq %rcx
+; SHSTK-NEXT: jmpq *%rcx
+;
+; WIN64-LABEL: test:
+; WIN64: # %bb.0: # %entry
+; WIN64-NEXT: pushq %rbp
+; WIN64-NEXT: movq %rsp, %rbp
+; WIN64-NEXT: movq %rdx, 8(%rbp,%rcx)
+; WIN64-NEXT: leaq 8(%rbp,%rcx), %rcx
+; WIN64-NEXT: popq %rbp
+; WIN64-NEXT: movq %rcx, %rsp
+; WIN64-NEXT: popq %rcx
+; WIN64-NEXT: rex64 jmpq *%rcx
+;
+; WINRET-LABEL: test:
+; WINRET: # %bb.0: # %entry
+; WINRET-NEXT: pushq %rbp
+; WINRET-NEXT: movq %rsp, %rbp
+; WINRET-NEXT: movq %rdx, 8(%rbp,%rcx)
+; WINRET-NEXT: leaq 8(%rbp,%rcx), %rcx
+; WINRET-NEXT: popq %rbp
+; WINRET-NEXT: movq %rcx, %rsp
+; WINRET-NEXT: retq # eh_return, addr: %rcx
+entry:
+ call void @llvm.eh.return.i64(i64 %offset, ptr %handler)
+ unreachable
+}
+
+declare void @llvm.eh.return.i64(i64, ptr)
More information about the llvm-commits
mailing list