Skip to content

Commit bf1f5ca

Browse files
phoebewangtstellar
authored andcommitted
CET for Exception Handle
Summary: Bug fix for https://bugs.llvm.org/show_bug.cgi?id=45182 Exception handle may indirectly jump to catch pad, So we should add ENDBR instruction before catch pad instructions. Reviewers: craig.topper, hjl.tools, LuoYuanke, annita.zhang, pengfei Reviewed By: LuoYuanke Subscribers: hiraditya, llvm-commits Patch By: Xiang Zhang (xiangzhangllvm) Differential Revision: https://reviews.llvm.org/D76190 (cherry picked from commit 974d649)
1 parent 964da81 commit bf1f5ca

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

llvm/lib/Target/X86/X86IndirectBranchTracking.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,18 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
127127
if (MBB.hasAddressTaken())
128128
Changed |= addENDBR(MBB, MBB.begin());
129129

130+
// Exception handle may indirectly jump to catch pad, So we should add
131+
// ENDBR before catch pad instructions.
132+
bool EHPadIBTNeeded = MBB.isEHPad();
133+
130134
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
131-
if (!I->isCall())
132-
continue;
133-
if (IsCallReturnTwice(I->getOperand(0)))
135+
if (I->isCall() && IsCallReturnTwice(I->getOperand(0)))
134136
Changed |= addENDBR(MBB, std::next(I));
137+
138+
if (EHPadIBTNeeded && I->isEHLabel()) {
139+
Changed |= addENDBR(MBB, std::next(I));
140+
EHPadIBTNeeded = false;
141+
}
135142
}
136143
}
137144
return Changed;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: llc -mtriple=x86_64-unknown-unknown < %s | FileCheck %s
2+
; RUN: llc -mtriple=i386-unknown-unknown < %s | FileCheck %s
3+
4+
;There should be 2 endbr* instruction at entry and catch pad.
5+
;CHECK-COUNT-2: endbr
6+
7+
declare void @_Z20function_that_throwsv()
8+
declare i32 @__gxx_personality_sj0(...)
9+
declare i8* @__cxa_begin_catch(i8*)
10+
declare void @__cxa_end_catch()
11+
12+
define void @test8() personality i8* bitcast (i32 (...)* @__gxx_personality_sj0 to i8*) {
13+
entry:
14+
invoke void @_Z20function_that_throwsv()
15+
to label %try.cont unwind label %lpad
16+
17+
lpad:
18+
%0 = landingpad { i8*, i32 }
19+
catch i8* null
20+
%1 = extractvalue { i8*, i32 } %0, 0
21+
%2 = tail call i8* @__cxa_begin_catch(i8* %1)
22+
tail call void @__cxa_end_catch()
23+
br label %try.cont
24+
25+
try.cont:
26+
ret void
27+
}
28+
29+
!llvm.module.flags = !{!0}
30+
31+
!0 = !{i32 4, !"cf-protection-branch", i32 1}

0 commit comments

Comments
 (0)