Skip to content

Commit 43f031d

Browse files
committed
Enable IBT(Indirect Branch Tracking) in JIT with CET(Control-flow Enforcement Technology)
Summary: This patch comes from H.J.'s hjl-tools@2bd54ce **This patch fix the failed llvm unit tests which running on CET machine. **(e.g. ExecutionEngine/MCJIT/MCJITTests) The reason we enable IBT at "JIT compiled with CET" is mainly that: the JIT don't know the its caller program is CET enable or not. If JIT's caller program is non-CET, it is no problem JIT generate CET code or not. But if JIT's caller program is CET enabled, JIT must generate CET code or it will cause Control protection exceptions. I have test the patch at llvm-unit-test and llvm-test-suite at CET machine. It passed. and H.J. also test it at building and running VNCserver(Virtual Network Console), it works too. (if not apply this patch, VNCserver will crash at CET machine.) Reviewers: hjl.tools, craig.topper, LuoYuanke, annita.zhang, pengfei Subscribers: tstellar, efriedma, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D76900
1 parent be0a4fe commit 43f031d

File tree

5 files changed

+92
-8
lines changed

5 files changed

+92
-8
lines changed

llvm/lib/Target/X86/X86IndirectBranchTracking.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "X86.h"
1919
#include "X86InstrInfo.h"
2020
#include "X86Subtarget.h"
21+
#include "X86TargetMachine.h"
2122
#include "llvm/ADT/Statistic.h"
2223
#include "llvm/CodeGen/MachineFunctionPass.h"
2324
#include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -102,7 +103,16 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
102103
// Check that the cf-protection-branch is enabled.
103104
Metadata *isCFProtectionSupported =
104105
MF.getMMI().getModule()->getModuleFlag("cf-protection-branch");
105-
if (!isCFProtectionSupported && !IndirectBranchTracking)
106+
// NB: We need to enable IBT in jitted code if JIT compiler is CET
107+
// enabled.
108+
const X86TargetMachine *TM =
109+
static_cast<const X86TargetMachine *>(&MF.getTarget());
110+
#ifdef __CET__
111+
bool isJITwithCET = TM->isJIT();
112+
#else
113+
bool isJITwithCET = false;
114+
#endif
115+
if (!isCFProtectionSupported && !IndirectBranchTracking && !isJITwithCET)
106116
return false;
107117

108118
// True if the current MF was changed and false otherwise.
@@ -111,10 +121,11 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
111121
TII = SubTarget.getInstrInfo();
112122
EndbrOpcode = SubTarget.is64Bit() ? X86::ENDBR64 : X86::ENDBR32;
113123

114-
// Non-internal function or function whose address was taken, can be
115-
// accessed through indirect calls. Mark the first BB with ENDBR instruction
116-
// unless nocf_check attribute is used.
117-
if ((MF.getFunction().hasAddressTaken() ||
124+
// Large code model, non-internal function or function whose address
125+
// was taken, can be accessed through indirect calls. Mark the first
126+
// BB with ENDBR instruction unless nocf_check attribute is used.
127+
if ((TM->getCodeModel() == CodeModel::Large ||
128+
MF.getFunction().hasAddressTaken() ||
118129
!MF.getFunction().hasLocalLinkage()) &&
119130
!MF.getFunction().doesNoCfCheck()) {
120131
auto MBB = MF.begin();
@@ -136,8 +147,8 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) {
136147
Changed |= addENDBR(MBB, std::next(I));
137148

138149
if (EHPadIBTNeeded && I->isEHLabel()) {
139-
Changed |= addENDBR(MBB, std::next(I));
140-
EHPadIBTNeeded = false;
150+
Changed |= addENDBR(MBB, std::next(I));
151+
EHPadIBTNeeded = false;
141152
}
142153
}
143154
}

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
214214
getEffectiveRelocModel(TT, JIT, RM),
215215
getEffectiveX86CodeModel(CM, JIT, TT.getArch() == Triple::x86_64),
216216
OL),
217-
TLOF(createTLOF(getTargetTriple())) {
217+
TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) {
218218
// On PS4, the "return address" of a 'noreturn' call must still be within
219219
// the calling function, and TrapUnreachable is an easy way to get that.
220220
if (TT.isPS4() || TT.isOSBinFormatMachO()) {

llvm/lib/Target/X86/X86TargetMachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class TargetTransformInfo;
3030
class X86TargetMachine final : public LLVMTargetMachine {
3131
std::unique_ptr<TargetLoweringObjectFile> TLOF;
3232
mutable StringMap<std::unique_ptr<X86Subtarget>> SubtargetMap;
33+
// True if this is used in JIT.
34+
bool IsJIT;
3335

3436
public:
3537
X86TargetMachine(const Target &T, const Triple &TT, StringRef CPU,
@@ -52,6 +54,8 @@ class X86TargetMachine final : public LLVMTargetMachine {
5254
TargetLoweringObjectFile *getObjFileLowering() const override {
5355
return TLOF.get();
5456
}
57+
58+
bool isJIT() const { return IsJIT; }
5559
};
5660

5761
} // end namespace llvm
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: llc -mtriple=x86_64-unknown-unknown -code-model=large < %s | FileCheck %s
2+
3+
; In large code model indirect branches are needed when branching to addresses
4+
; whose offset from the current instruction pointer is unknown.
5+
;CHECK-COUNT-3: endbr
6+
7+
@a = dso_local local_unnamed_addr global i32 1, align 4
8+
9+
; Function Attrs: nofree noinline norecurse nounwind uwtable writeonly
10+
define dso_local void @ext() local_unnamed_addr #0 {
11+
entry:
12+
store i32 0, i32* @a, align 4
13+
ret void
14+
}
15+
16+
; Function Attrs: nofree norecurse nounwind uwtable
17+
define dso_local i32 @main() local_unnamed_addr #1 {
18+
entry:
19+
tail call fastcc void @foo()
20+
%0 = load i32, i32* @a, align 4
21+
ret i32 %0
22+
}
23+
24+
; Function Attrs: nofree noinline norecurse nounwind uwtable writeonly
25+
define internal fastcc void @foo() unnamed_addr #0 {
26+
entry:
27+
tail call void @ext()
28+
ret void
29+
}
30+
31+
!llvm.module.flags = !{!0, !1, !2, !3}
32+
33+
!0 = !{i32 1, !"wchar_size", i32 4}
34+
!1 = !{i32 4, !"cf-protection-return", i32 1}
35+
!2 = !{i32 4, !"cf-protection-branch", i32 1}
36+
!3 = !{i32 1, !"Code Model", i32 4}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: %lli -code-model=large %s > /dev/null
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
@a = dso_local local_unnamed_addr global i32 1, align 4
5+
6+
; Function Attrs: nofree noinline norecurse nounwind uwtable writeonly
7+
define dso_local void @ext() local_unnamed_addr #0 {
8+
entry:
9+
store i32 0, i32* @a, align 4
10+
ret void
11+
}
12+
13+
; Function Attrs: nofree norecurse nounwind uwtable
14+
define dso_local i32 @main() local_unnamed_addr #1 {
15+
entry:
16+
tail call fastcc void @foo()
17+
%0 = load i32, i32* @a, align 4
18+
ret i32 %0
19+
}
20+
21+
; Function Attrs: nofree noinline norecurse nounwind uwtable writeonly
22+
define internal fastcc void @foo() unnamed_addr #0 {
23+
entry:
24+
tail call void @ext()
25+
ret void
26+
}
27+
28+
!llvm.module.flags = !{!0, !1, !2, !3}
29+
30+
!0 = !{i32 1, !"wchar_size", i32 4}
31+
!1 = !{i32 4, !"cf-protection-return", i32 1}
32+
!2 = !{i32 4, !"cf-protection-branch", i32 1}
33+
!3 = !{i32 1, !"Code Model", i32 4}

0 commit comments

Comments
 (0)