LLVM 24.0.0git
AArch64BranchTargets.cpp
Go to the documentation of this file.
1//===-- AArch64BranchTargets.cpp -- Harden code using v8.5-A BTI extension -==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass inserts BTI instructions at the start of every function and basic
10// block which could be indirectly called. The hardware will (when enabled)
11// trap when an indirect branch or call instruction targets an instruction
12// which is not a valid BTI instruction. This is intended to guard against
13// control-flow hijacking attacks. Note that this does not do anything for RET
14// instructions, as they can be more precisely protected by return address
15// signing.
16//
17//===----------------------------------------------------------------------===//
18
19#include "AArch64.h"
21#include "AArch64Subtarget.h"
27#include "llvm/Support/Debug.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "aarch64-branch-targets"
32#define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
33
34namespace {
35// BTI HINT encoding: base (32) plus 'c' (2) and/or 'j' (4).
36enum : unsigned {
37 BTIBase = 32, // Base immediate for BTI HINT
38 BTIC = 1u << 1, // 2
39 BTIJ = 1u << 2, // 4
40 BTIMask = BTIC | BTIJ,
41};
42
43class AArch64BranchTargetsImpl {
44public:
45 bool run(MachineFunction &MF);
46
47private:
49
50 void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump,
51 bool NeedsWinCFI);
52
53 bool replaceWithNonBranchTarget(MachineBasicBlock &MBB);
54};
55
56class AArch64BranchTargetsLegacy : public MachineFunctionPass {
57public:
58 static char ID;
59 AArch64BranchTargetsLegacy() : MachineFunctionPass(ID) {}
60 void getAnalysisUsage(AnalysisUsage &AU) const override;
61 bool runOnMachineFunction(MachineFunction &MF) override;
62 StringRef getPassName() const override { return AARCH64_BRANCH_TARGETS_NAME; }
63};
64
65} // end anonymous namespace
66
67char AArch64BranchTargetsLegacy::ID = 0;
68
69INITIALIZE_PASS(AArch64BranchTargetsLegacy, "aarch64-branch-targets",
70 AARCH64_BRANCH_TARGETS_NAME, false, false)
71
72void AArch64BranchTargetsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesCFG();
75}
76
78 return new AArch64BranchTargetsLegacy();
79}
80
81bool AArch64BranchTargetsLegacy::runOnMachineFunction(MachineFunction &MF) {
82 return AArch64BranchTargetsImpl().run(MF);
83}
84
85PreservedAnalyses
88 bool Changed = AArch64BranchTargetsImpl().run(MF);
89 if (!Changed)
93 return PA;
94}
95
96bool AArch64BranchTargetsImpl::run(MachineFunction &MF) {
98 return false;
99
100 LLVM_DEBUG(dbgs() << "********** AArch64 Branch Targets **********\n"
101 << "********** Function: " << MF.getName() << '\n');
102 const Function &F = MF.getFunction();
103
105
106 // LLVM does not consider basic blocks which are the targets of jump tables
107 // to be address-taken (the address can't escape anywhere else), but they are
108 // used for indirect branches, so need BTI instructions.
110 if (auto *JTI = MF.getJumpTableInfo())
111 for (auto &JTE : JTI->getJumpTables())
112 JumpTableTargets.insert_range(JTE.MBBs);
113
114 bool MadeChange = false;
115 bool HasWinCFI = MF.hasWinCFI();
116 for (MachineBasicBlock &MBB : MF) {
117 MadeChange |= replaceWithNonBranchTarget(MBB);
118
119 bool CouldCall = false, CouldJump = false;
120 // If the function is address-taken or externally-visible, it could be
121 // indirectly called. PLT entries and tail-calls use BR, but when they are
122 // are in guarded pages should all use x16 or x17 to hold the called
123 // address, so we don't need to set CouldJump here. BR instructions in
124 // non-guarded pages (which might be non-BTI-aware code) are allowed to
125 // branch to a "BTI c" using any register.
126 //
127 // For ELF targets, this is enough, because AAELF64 says that if the static
128 // linker later wants to use an indirect branch instruction in a
129 // long-branch thunk, it's also responsible for adding a 'landing pad' with
130 // a BTI, and pointing the indirect branch at that. For non-ELF targets we
131 // can't rely on that, so we assume that `CouldCall` is _always_ true due
132 // to the risk of long-branch thunks at link time.
133 if (&MBB == &*MF.begin() && (!Subtarget->isTargetELF() ||
134 (F.hasAddressTaken() || !F.hasLocalLinkage())))
135 CouldCall = true;
136
137 // If the block itself is address-taken, it could be indirectly branched
138 // to, but not called.
139 if (MBB.isMachineBlockAddressTaken() || MBB.isIRBlockAddressTaken() ||
140 JumpTableTargets.count(&MBB))
141 CouldJump = true;
142
143 if (MBB.isEHPad()) {
144 if (HasWinCFI && (MBB.isEHFuncletEntry() || MBB.isCleanupFuncletEntry()))
145 CouldCall = true;
146 else
147 CouldJump = true;
148 }
149 if (CouldCall || CouldJump) {
150 addBTI(MBB, CouldCall, CouldJump, HasWinCFI);
151 MadeChange = true;
152 }
153 }
154
155 return MadeChange;
156}
157
159 auto MBBI = MBB.begin();
160
161 // If the block starts with EH_LABEL(s), skip them first.
162 while (MBBI != MBB.end() && MBBI->isEHLabel()) {
163 ++MBBI;
164 }
165
166 // Skip meta/CFI/etc. (and EMITBKEY) to reach the first executable insn.
167 for (; MBBI != MBB.end() &&
168 (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY ||
169 MBBI->getOpcode() == AArch64::EMITNONHINTED);
170 ++MBBI)
171 ;
172
173 return MBBI;
174}
175
176bool AArch64BranchTargetsImpl::replaceWithNonBranchTarget(
177 MachineBasicBlock &MBB) {
178 bool MadeChange = false;
179 auto MF = MBB.getParent();
181
182 if (MBBI == MBB.end() ||
183 (&MBB == &MF->front() && (MBBI->getOpcode() == AArch64::PACIASPPC ||
184 MBBI->getOpcode() == AArch64::PACIBSPPC)))
185 return MadeChange;
186
187 // If there are any PACI[AB]SPPC instructions after the first executable
188 // instruction, replace with the address signing opcode which is not a branch
189 // target to prevent an implicit BTI in the middle of the basic block.
190 for (; MBBI != MBB.end(); ++MBBI) {
191 if (MBBI->getOpcode() == AArch64::PACIASPPC ||
192 MBBI->getOpcode() == AArch64::PACIBSPPC) {
193 unsigned NBOp = MBBI->getOpcode() == AArch64::PACIASPPC
194 ? AArch64::PACNBIASPPC
195 : AArch64::PACNBIBSPPC;
196
197 MBBI->setDesc(Subtarget->getInstrInfo()->get(NBOp));
198 MadeChange = true;
199 }
200 }
201
202 return MadeChange;
203}
204
205void AArch64BranchTargetsImpl::addBTI(MachineBasicBlock &MBB, bool CouldCall,
206 bool CouldJump, bool HasWinCFI) {
207 LLVM_DEBUG(dbgs() << "Adding BTI " << (CouldJump ? "j" : "")
208 << (CouldCall ? "c" : "") << " to " << MBB.getName()
209 << "\n");
210
211 unsigned HintNum = getBTIHintNum(CouldCall, CouldJump);
213
214 // PACI[AB]SP(PC)? are implicitly BTI c so insertion of a BTI can be skipped
215 // in this case. Depending on the runtime value of SCTLR_EL1.BT[01], they are
216 // not equivalent to a BTI jc, which still requires an additional BTI.
217 if (MBBI != MBB.end() && ((HintNum & BTIMask) == BTIC) &&
218 (MBBI->getOpcode() == AArch64::PACIASP ||
219 MBBI->getOpcode() == AArch64::PACIBSP ||
220 MBBI->getOpcode() == AArch64::PACIASPPC ||
221 MBBI->getOpcode() == AArch64::PACIBSPPC))
222 return;
223
224 const AArch64InstrInfo *TII = Subtarget->getInstrInfo();
225
226 // Insert BTI exactly at the first executable instruction.
228 MachineInstr *BTI = BuildMI(MBB, MBBI, DL, TII->get(AArch64::HINT))
229 .addImm(HintNum)
230 .getInstr();
231
232 // WinEH: put .seh_nop after BTI when the first real insn is FrameSetup.
233 if (HasWinCFI && MBBI != MBB.end() &&
234 MBBI->getFlag(MachineInstr::FrameSetup)) {
235 auto AfterBTI = std::next(MachineBasicBlock::iterator(BTI));
236 BuildMI(MBB, AfterBTI, DL, TII->get(AArch64::SEH_Nop));
237 }
238}
#define AARCH64_BRANCH_TARGETS_NAME
MachineBasicBlock::iterator getFirstExecutableInstr(MachineBasicBlock &MBB)
const AArch64Subtarget & Subtarget
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
const AArch64InstrInfo * getInstrInfo() const override
Represent the analysis usage information of a pass.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void insert_range(Range &&R)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
static unsigned getBTIHintNum(bool CallTarget, bool JumpTarget)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createAArch64BranchTargetsPass()