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"
20#include "AArch64MachineFunctionInfo.h"
21#include "AArch64Subtarget.h"
22#include "Utils/AArch64BaseInfo.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineJumpTableInfo.h"
26#include "llvm/CodeGen/MachineModuleInfo.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:
48 const AArch64Subtarget *Subtarget;
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();
74 MachineFunctionPass::getAnalysisUsage(AU);
75}
76
77FunctionPass *llvm::createAArch64BranchTargetsPass() {
78 return new AArch64BranchTargetsLegacy();
79}
80
81bool AArch64BranchTargetsLegacy::runOnMachineFunction(MachineFunction &MF) {
82 return AArch64BranchTargetsImpl().run(MF);
83}
84
85PreservedAnalyses
86AArch64BranchTargetsPass::run(MachineFunction &MF,
87 MachineFunctionAnalysisManager &MFAM) {
88 bool Changed = AArch64BranchTargetsImpl().run(MF);
89 if (!Changed)
90 return PreservedAnalyses::all();
91 PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
92 PA.preserveSet<CFGAnalyses>();
93 return PA;
94}
95
96bool AArch64BranchTargetsImpl::run(MachineFunction &MF) {
97 if (!MF.getInfo<AArch64FunctionInfo>()->branchTargetEnforcement())
98 return false;
99
100 LLVM_DEBUG(dbgs() << "********** AArch64 Branch Targets **********\n"
101 << "********** Function: " << MF.getName() << '\n');
102 const Function &F = MF.getFunction();
103
104 Subtarget = &MF.getSubtarget<AArch64Subtarget>();
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.
109 SmallPtrSet<MachineBasicBlock *, 8> JumpTableTargets;
110 if (auto *JTI = MF.getJumpTableInfo())
111 for (auto &JTE : JTI->getJumpTables())
112 JumpTableTargets.insert_range(R: 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(Ptr: &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, NeedsWinCFI: HasWinCFI);
151 MadeChange = true;
152 }
153 }
154
155 return MadeChange;
156}
157
158MachineBasicBlock::iterator getFirstExecutableInstr(MachineBasicBlock &MBB) {
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();
180 auto MBBI = getFirstExecutableInstr(MBB);
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(Opcode: 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(CallTarget: CouldCall, JumpTarget: CouldJump);
212 auto MBBI = getFirstExecutableInstr(MBB);
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.
227 const DebugLoc DL = MBB.findDebugLoc(MBBI);
228 MachineInstr *BTI = BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: AArch64::HINT))
229 .addImm(Val: 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(Flag: MachineInstr::FrameSetup)) {
235 auto AfterBTI = std::next(x: MachineBasicBlock::iterator(BTI));
236 BuildMI(BB&: MBB, I: AfterBTI, MIMD: DL, MCID: TII->get(Opcode: AArch64::SEH_Nop));
237 }
238}
239