[llvm] [X86] Emit ENDBR at WinEH funclet entries under CET-IBT (PR #200333)
Justin Lebar via llvm-commits
llvm-commits at lists.llvm.org
Fri May 29 01:14:05 PDT 2026
https://github.com/jlebar updated https://github.com/llvm/llvm-project/pull/200333
>From 0b5441d1aaf2b294572dd090195a407267ae55a3 Mon Sep 17 00:00:00 2001
From: Justin Lebar <justin.lebar at gmail.com>
Date: Thu, 28 May 2026 23:12:26 -0700
Subject: [PATCH] [X86] Emit ENDBR at WinEH funclet entries under CET-IBT
X86IndirectBranchTracking only marks landing pads reached via an EH_LABEL. WinEH catch/cleanup funclet entries are also indirect targets of the EH runtime but carry no EH_LABEL, so under -fcf-protection=branch a thrown exception lands on a funclet whose first instruction is not endbr64 and an IBT-enforcing CPU raises #CP.
This bug was found by a large run of Opus 4.7 looking for bugs in LLVM.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply at anthropic.com>
---
.../Target/X86/X86IndirectBranchTracking.cpp | 8 ++++-
.../X86/indirect-branch-tracking-wineh.ll | 30 +++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/X86/indirect-branch-tracking-wineh.ll
diff --git a/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp
index 6ccad26a890dd..7b9e7333a08dc 100644
--- a/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp
+++ b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp
@@ -171,7 +171,13 @@ static bool runIndirectBranchTracking(MachineFunction &MF) {
break;
}
}
- } else if (MBB.isEHPad()){
+ } else if (MBB.isEHFuncletEntry()) {
+ // WinEH catch/cleanup funclet entries are indirect call targets of the
+ // EH runtime's dispatcher. They carry no EH_LABEL, so the isEHPad()
+ // handling below would miss them; emit ENDBR at the funclet entry so a
+ // CET-IBT-enforcing CPU does not #CP-fault when an exception is thrown.
+ Changed |= addENDBR(MBB, MBB.begin());
+ } else if (MBB.isEHPad()) {
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
if (!I->isEHLabel())
continue;
diff --git a/llvm/test/CodeGen/X86/indirect-branch-tracking-wineh.ll b/llvm/test/CodeGen/X86/indirect-branch-tracking-wineh.ll
new file mode 100644
index 0000000000000..c65499eabcdce
--- /dev/null
+++ b/llvm/test/CodeGen/X86/indirect-branch-tracking-wineh.ll
@@ -0,0 +1,30 @@
+; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
+
+; Under CET-IBT (-fcf-protection=branch), WinEH catch/cleanup funclet entries
+; are indirect call targets of the C++ EH runtime dispatcher and must begin
+; with endbr64, otherwise throwing an exception #CP-faults on an IBT-enforcing
+; CPU.
+
+declare i32 @__CxxFrameHandler3(...)
+declare void @throws()
+
+define void @f() personality ptr @__CxxFrameHandler3 {
+; CHECK-LABEL: f:
+; CHECK: endbr64
+entry:
+ invoke void @throws() to label %cont unwind label %cd
+cont:
+ ret void
+cd:
+ %cs = catchswitch within none [label %c] unwind to caller
+c:
+ %cp = catchpad within %cs [ptr null, i32 64, ptr null]
+ catchret from %cp to label %cont
+}
+
+; The catch funclet entry must also carry endbr64.
+; CHECK: "?catch${{[^"]*}}":
+; CHECK: endbr64
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 8, !"cf-protection-branch", i32 1}
More information about the llvm-commits
mailing list