LLVM 18.0.0git
AArch64SLSHardening.cpp
Go to the documentation of this file.
1//===- AArch64SLSHardening.cpp - Harden Straight Line Missspeculation -----===//
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 file contains a pass to insert code to mitigate against side channel
10// vulnerabilities that may happen under straight line miss-speculation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AArch64InstrInfo.h"
15#include "AArch64Subtarget.h"
27#include "llvm/IR/DebugLoc.h"
28#include "llvm/Pass.h"
30#include "llvm/Support/Debug.h"
32#include <cassert>
33
34using namespace llvm;
35
36#define DEBUG_TYPE "aarch64-sls-hardening"
37
38#define AARCH64_SLS_HARDENING_NAME "AArch64 sls hardening pass"
39
40namespace {
41
42class AArch64SLSHardening : public MachineFunctionPass {
43public:
44 const TargetInstrInfo *TII;
46 const AArch64Subtarget *ST;
47
48 static char ID;
49
50 AArch64SLSHardening() : MachineFunctionPass(ID) {
52 }
53
54 bool runOnMachineFunction(MachineFunction &Fn) override;
55
56 StringRef getPassName() const override { return AARCH64_SLS_HARDENING_NAME; }
57
58private:
59 bool hardenReturnsAndBRs(MachineBasicBlock &MBB) const;
60 bool hardenBLRs(MachineBasicBlock &MBB) const;
63};
64
65} // end anonymous namespace
66
67char AArch64SLSHardening::ID = 0;
68
69INITIALIZE_PASS(AArch64SLSHardening, "aarch64-sls-hardening",
70 AARCH64_SLS_HARDENING_NAME, false, false)
71
72static void insertSpeculationBarrier(const AArch64Subtarget *ST,
77 assert(MBBI != MBB.begin() &&
78 "Must not insert SpeculationBarrierEndBB as only instruction in MBB.");
79 assert(std::prev(MBBI)->isBarrier() &&
80 "SpeculationBarrierEndBB must only follow unconditional control flow "
81 "instructions.");
82 assert(std::prev(MBBI)->isTerminator() &&
83 "SpeculationBarrierEndBB must only follow terminators.");
84 const TargetInstrInfo *TII = ST->getInstrInfo();
85 unsigned BarrierOpc = ST->hasSB() && !AlwaysUseISBDSB
86 ? AArch64::SpeculationBarrierSBEndBB
87 : AArch64::SpeculationBarrierISBDSBEndBB;
88 if (MBBI == MBB.end() ||
89 (MBBI->getOpcode() != AArch64::SpeculationBarrierSBEndBB &&
90 MBBI->getOpcode() != AArch64::SpeculationBarrierISBDSBEndBB))
91 BuildMI(MBB, MBBI, DL, TII->get(BarrierOpc));
92}
93
94bool AArch64SLSHardening::runOnMachineFunction(MachineFunction &MF) {
98
99 bool Modified = false;
100 for (auto &MBB : MF) {
101 Modified |= hardenReturnsAndBRs(MBB);
102 Modified |= hardenBLRs(MBB);
103 }
104
105 return Modified;
106}
107
108static bool isBLR(const MachineInstr &MI) {
109 switch (MI.getOpcode()) {
110 case AArch64::BLR:
111 case AArch64::BLRNoIP:
112 return true;
113 case AArch64::BLRAA:
114 case AArch64::BLRAB:
115 case AArch64::BLRAAZ:
116 case AArch64::BLRABZ:
117 llvm_unreachable("Currently, LLVM's code generator does not support "
118 "producing BLRA* instructions. Therefore, there's no "
119 "support in this pass for those instructions.");
120 }
121 return false;
122}
123
124bool AArch64SLSHardening::hardenReturnsAndBRs(MachineBasicBlock &MBB) const {
125 if (!ST->hardenSlsRetBr())
126 return false;
127 bool Modified = false;
130 for (; MBBI != E; MBBI = NextMBBI) {
131 MachineInstr &MI = *MBBI;
132 NextMBBI = std::next(MBBI);
133 if (MI.isReturn() || isIndirectBranchOpcode(MI.getOpcode())) {
134 assert(MI.isTerminator());
135 insertSpeculationBarrier(ST, MBB, std::next(MBBI), MI.getDebugLoc());
136 Modified = true;
137 }
138 }
139 return Modified;
140}
141
142static const char SLSBLRNamePrefix[] = "__llvm_slsblr_thunk_";
143
144static const struct ThunkNameAndReg {
145 const char* Name;
147} SLSBLRThunks[] = {
148 { "__llvm_slsblr_thunk_x0", AArch64::X0},
149 { "__llvm_slsblr_thunk_x1", AArch64::X1},
150 { "__llvm_slsblr_thunk_x2", AArch64::X2},
151 { "__llvm_slsblr_thunk_x3", AArch64::X3},
152 { "__llvm_slsblr_thunk_x4", AArch64::X4},
153 { "__llvm_slsblr_thunk_x5", AArch64::X5},
154 { "__llvm_slsblr_thunk_x6", AArch64::X6},
155 { "__llvm_slsblr_thunk_x7", AArch64::X7},
156 { "__llvm_slsblr_thunk_x8", AArch64::X8},
157 { "__llvm_slsblr_thunk_x9", AArch64::X9},
158 { "__llvm_slsblr_thunk_x10", AArch64::X10},
159 { "__llvm_slsblr_thunk_x11", AArch64::X11},
160 { "__llvm_slsblr_thunk_x12", AArch64::X12},
161 { "__llvm_slsblr_thunk_x13", AArch64::X13},
162 { "__llvm_slsblr_thunk_x14", AArch64::X14},
163 { "__llvm_slsblr_thunk_x15", AArch64::X15},
164 // X16 and X17 are deliberately missing, as the mitigation requires those
165 // register to not be used in BLR. See comment in ConvertBLRToBL for more
166 // details.
167 { "__llvm_slsblr_thunk_x18", AArch64::X18},
168 { "__llvm_slsblr_thunk_x19", AArch64::X19},
169 { "__llvm_slsblr_thunk_x20", AArch64::X20},
170 { "__llvm_slsblr_thunk_x21", AArch64::X21},
171 { "__llvm_slsblr_thunk_x22", AArch64::X22},
172 { "__llvm_slsblr_thunk_x23", AArch64::X23},
173 { "__llvm_slsblr_thunk_x24", AArch64::X24},
174 { "__llvm_slsblr_thunk_x25", AArch64::X25},
175 { "__llvm_slsblr_thunk_x26", AArch64::X26},
176 { "__llvm_slsblr_thunk_x27", AArch64::X27},
177 { "__llvm_slsblr_thunk_x28", AArch64::X28},
178 { "__llvm_slsblr_thunk_x29", AArch64::FP},
179 // X30 is deliberately missing, for similar reasons as X16 and X17 are
180 // missing.
181 { "__llvm_slsblr_thunk_x31", AArch64::XZR},
183
184namespace {
185struct SLSBLRThunkInserter : ThunkInserter<SLSBLRThunkInserter> {
186 const char *getThunkPrefix() { return SLSBLRNamePrefix; }
187 bool mayUseThunk(const MachineFunction &MF, bool InsertedThunks) {
188 if (InsertedThunks)
189 return false;
190 ComdatThunks &= !MF.getSubtarget<AArch64Subtarget>().hardenSlsNoComdat();
191 // FIXME: This could also check if there are any BLRs in the function
192 // to more accurately reflect if a thunk will be needed.
193 return MF.getSubtarget<AArch64Subtarget>().hardenSlsBlr();
194 }
195 bool insertThunks(MachineModuleInfo &MMI, MachineFunction &MF);
196 void populateThunk(MachineFunction &MF);
197
198private:
199 bool ComdatThunks = true;
200};
201} // namespace
202
203bool SLSBLRThunkInserter::insertThunks(MachineModuleInfo &MMI,
204 MachineFunction &MF) {
205 // FIXME: It probably would be possible to filter which thunks to produce
206 // based on which registers are actually used in BLR instructions in this
207 // function. But would that be a worthwhile optimization?
208 for (auto T : SLSBLRThunks)
209 createThunkFunction(MMI, T.Name, ComdatThunks);
210 return true;
211}
212
213void SLSBLRThunkInserter::populateThunk(MachineFunction &MF) {
214 // FIXME: How to better communicate Register number, rather than through
215 // name and lookup table?
216 assert(MF.getName().startswith(getThunkPrefix()));
217 auto ThunkIt = llvm::find_if(
218 SLSBLRThunks, [&MF](auto T) { return T.Name == MF.getName(); });
219 assert(ThunkIt != std::end(SLSBLRThunks));
220 Register ThunkReg = ThunkIt->Reg;
221
222 const TargetInstrInfo *TII =
223 MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
224 assert (MF.size() == 1);
225 MachineBasicBlock *Entry = &MF.front();
226 Entry->clear();
227
228 // These thunks need to consist of the following instructions:
229 // __llvm_slsblr_thunk_xN:
230 // BR xN
231 // barrierInsts
232 Entry->addLiveIn(ThunkReg);
233 // MOV X16, ThunkReg == ORR X16, XZR, ThunkReg, LSL #0
234 BuildMI(Entry, DebugLoc(), TII->get(AArch64::ORRXrs), AArch64::X16)
235 .addReg(AArch64::XZR)
236 .addReg(ThunkReg)
237 .addImm(0);
238 BuildMI(Entry, DebugLoc(), TII->get(AArch64::BR)).addReg(AArch64::X16);
239 // Make sure the thunks do not make use of the SB extension in case there is
240 // a function somewhere that will call to it that for some reason disabled
241 // the SB extension locally on that function, even though it's enabled for
242 // the module otherwise. Therefore set AlwaysUseISBSDB to true.
243 insertSpeculationBarrier(&MF.getSubtarget<AArch64Subtarget>(), *Entry,
244 Entry->end(), DebugLoc(), true /*AlwaysUseISBDSB*/);
245}
246
247MachineBasicBlock &AArch64SLSHardening::ConvertBLRToBL(
249 // Transform a BLR to a BL as follows:
250 // Before:
251 // |-----------------------------|
252 // | ... |
253 // | instI |
254 // | BLR xN |
255 // | instJ |
256 // | ... |
257 // |-----------------------------|
258 //
259 // After:
260 // |-----------------------------|
261 // | ... |
262 // | instI |
263 // | BL __llvm_slsblr_thunk_xN |
264 // | instJ |
265 // | ... |
266 // |-----------------------------|
267 //
268 // __llvm_slsblr_thunk_xN:
269 // |-----------------------------|
270 // | BR xN |
271 // | barrierInsts |
272 // |-----------------------------|
273 //
274 // The __llvm_slsblr_thunk_xN thunks are created by the SLSBLRThunkInserter.
275 // This function merely needs to transform BLR xN into BL
276 // __llvm_slsblr_thunk_xN.
277 //
278 // Since linkers are allowed to clobber X16 and X17 on function calls, the
279 // above mitigation only works if the original BLR instruction was not
280 // BLR X16 nor BLR X17. Code generation before must make sure that no BLR
281 // X16|X17 was produced if the mitigation is enabled.
282
283 MachineInstr &BLR = *MBBI;
284 assert(isBLR(BLR));
285 unsigned BLOpcode;
287 bool RegIsKilled;
288 switch (BLR.getOpcode()) {
289 case AArch64::BLR:
290 case AArch64::BLRNoIP:
291 BLOpcode = AArch64::BL;
292 Reg = BLR.getOperand(0).getReg();
293 assert(Reg != AArch64::X16 && Reg != AArch64::X17 && Reg != AArch64::LR);
294 RegIsKilled = BLR.getOperand(0).isKill();
295 break;
296 case AArch64::BLRAA:
297 case AArch64::BLRAB:
298 case AArch64::BLRAAZ:
299 case AArch64::BLRABZ:
300 llvm_unreachable("BLRA instructions cannot yet be produced by LLVM, "
301 "therefore there is no need to support them for now.");
302 default:
303 llvm_unreachable("unhandled BLR");
304 }
305 DebugLoc DL = BLR.getDebugLoc();
306
307 // If we'd like to support also BLRAA and BLRAB instructions, we'd need
308 // a lot more different kind of thunks.
309 // For example, a
310 //
311 // BLRAA xN, xM
312 //
313 // instruction probably would need to be transformed to something like:
314 //
315 // BL __llvm_slsblraa_thunk_x<N>_x<M>
316 //
317 // __llvm_slsblraa_thunk_x<N>_x<M>:
318 // BRAA x<N>, x<M>
319 // barrierInsts
320 //
321 // Given that about 30 different values of N are possible and about 30
322 // different values of M are possible in the above, with the current way
323 // of producing indirect thunks, we'd be producing about 30 times 30, i.e.
324 // about 900 thunks (where most might not be actually called). This would
325 // multiply further by two to support both BLRAA and BLRAB variants of those
326 // instructions.
327 // If we'd want to support this, we'd probably need to look into a different
328 // way to produce thunk functions, based on which variants are actually
329 // needed, rather than producing all possible variants.
330 // So far, LLVM does never produce BLRA* instructions, so let's leave this
331 // for the future when LLVM can start producing BLRA* instructions.
332 MachineFunction &MF = *MBBI->getMF();
334 auto ThunkIt =
335 llvm::find_if(SLSBLRThunks, [Reg](auto T) { return T.Reg == Reg; });
336 assert (ThunkIt != std::end(SLSBLRThunks));
337 MCSymbol *Sym = Context.getOrCreateSymbol(ThunkIt->Name);
338
339 MachineInstr *BL = BuildMI(MBB, MBBI, DL, TII->get(BLOpcode)).addSym(Sym);
340
341 // Now copy the implicit operands from BLR to BL and copy other necessary
342 // info.
343 // However, both BLR and BL instructions implictly use SP and implicitly
344 // define LR. Blindly copying implicit operands would result in SP and LR
345 // operands to be present multiple times. While this may not be too much of
346 // an issue, let's avoid that for cleanliness, by removing those implicit
347 // operands from the BL created above before we copy over all implicit
348 // operands from the BLR.
349 int ImpLROpIdx = -1;
350 int ImpSPOpIdx = -1;
351 for (unsigned OpIdx = BL->getNumExplicitOperands();
352 OpIdx < BL->getNumOperands(); OpIdx++) {
353 MachineOperand Op = BL->getOperand(OpIdx);
354 if (!Op.isReg())
355 continue;
356 if (Op.getReg() == AArch64::LR && Op.isDef())
357 ImpLROpIdx = OpIdx;
358 if (Op.getReg() == AArch64::SP && !Op.isDef())
359 ImpSPOpIdx = OpIdx;
360 }
361 assert(ImpLROpIdx != -1);
362 assert(ImpSPOpIdx != -1);
363 int FirstOpIdxToRemove = std::max(ImpLROpIdx, ImpSPOpIdx);
364 int SecondOpIdxToRemove = std::min(ImpLROpIdx, ImpSPOpIdx);
365 BL->removeOperand(FirstOpIdxToRemove);
366 BL->removeOperand(SecondOpIdxToRemove);
367 // Now copy over the implicit operands from the original BLR
368 BL->copyImplicitOps(MF, BLR);
369 MF.moveCallSiteInfo(&BLR, BL);
370 // Also add the register called in the BLR as being used in the called thunk.
371 BL->addOperand(MachineOperand::CreateReg(Reg, false /*isDef*/, true /*isImp*/,
372 RegIsKilled /*isKill*/));
373 // Remove BLR instruction
374 MBB.erase(MBBI);
375
376 return MBB;
377}
378
379bool AArch64SLSHardening::hardenBLRs(MachineBasicBlock &MBB) const {
380 if (!ST->hardenSlsBlr())
381 return false;
382 bool Modified = false;
384 E = MBB.instr_end();
386 for (; MBBI != E; MBBI = NextMBBI) {
387 MachineInstr &MI = *MBBI;
388 NextMBBI = std::next(MBBI);
389 if (isBLR(MI)) {
390 ConvertBLRToBL(MBB, MBBI);
391 Modified = true;
392 }
393 }
394 return Modified;
395}
396
398 return new AArch64SLSHardening();
399}
400
401namespace {
402class AArch64IndirectThunks : public MachineFunctionPass {
403public:
404 static char ID;
405
406 AArch64IndirectThunks() : MachineFunctionPass(ID) {}
407
408 StringRef getPassName() const override { return "AArch64 Indirect Thunks"; }
409
410 bool doInitialization(Module &M) override;
411 bool runOnMachineFunction(MachineFunction &MF) override;
412
413private:
414 std::tuple<SLSBLRThunkInserter> TIs;
415
416 // FIXME: When LLVM moves to C++17, these can become folds
417 template <typename... ThunkInserterT>
418 static void initTIs(Module &M,
419 std::tuple<ThunkInserterT...> &ThunkInserters) {
420 (void)std::initializer_list<int>{
421 (std::get<ThunkInserterT>(ThunkInserters).init(M), 0)...};
422 }
423 template <typename... ThunkInserterT>
424 static bool runTIs(MachineModuleInfo &MMI, MachineFunction &MF,
425 std::tuple<ThunkInserterT...> &ThunkInserters) {
426 bool Modified = false;
427 (void)std::initializer_list<int>{
428 Modified |= std::get<ThunkInserterT>(ThunkInserters).run(MMI, MF)...};
429 return Modified;
430 }
431};
432
433} // end anonymous namespace
434
435char AArch64IndirectThunks::ID = 0;
436
438 return new AArch64IndirectThunks();
439}
440
441bool AArch64IndirectThunks::doInitialization(Module &M) {
442 initTIs(M, TIs);
443 return false;
444}
445
446bool AArch64IndirectThunks::runOnMachineFunction(MachineFunction &MF) {
447 LLVM_DEBUG(dbgs() << getPassName() << '\n');
448 auto &MMI = getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
449 return runTIs(MMI, MF, TIs);
450}
aarch64 promote const
#define AARCH64_SLS_HARDENING_NAME
static const struct ThunkNameAndReg SLSBLRThunks[]
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc bool AlwaysUseISBDSB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DEBUG(X)
Definition: Debug.h:101
Symbol * Sym
Definition: ELF_riscv.cpp:477
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Contains a base class for Passes that inject an MI thunk.
unsigned const TargetRegisterInfo * TRI
LLVMContext & Context
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
Context object for machine code objects.
Definition: MCContext.h:76
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
instr_iterator instr_begin()
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Instructions::iterator instr_iterator
instr_iterator instr_end()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
bool doInitialization(Module &) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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.
MCContext & getContext() const
unsigned size() const
const MachineBasicBlock & front() const
void moveCallSiteInfo(const MachineInstr *Old, const MachineInstr *New)
Move the call site info from Old to \New call site info.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:68
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:543
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:472
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:553
This class contains meta information specific to a module.
MachineOperand class - Representation of each machine instruction operand.
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Reg
All possible values of the reg field in the ModR/M byte.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static bool isIndirectBranchOpcode(int Opc)
FunctionPass * createAArch64IndirectThunks()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void initializeAArch64SLSHardeningPass(PassRegistry &)
FunctionPass * createAArch64SLSHardeningPass()
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1753