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 "AArch64.h"
15#include "AArch64InstrInfo.h"
16#include "AArch64Subtarget.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/CodeGen/IndirectThunks.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineOperand.h"
25#include "llvm/CodeGen/RegisterScavenging.h"
26#include "llvm/IR/DebugLoc.h"
27#include "llvm/Pass.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/FormatVariadic.h"
30#include "llvm/Target/TargetMachine.h"
31#include <cassert>
32#include <climits>
33#include <tuple>
34
35using namespace llvm;
36
37#define DEBUG_TYPE "aarch64-sls-hardening"
38
39#define AARCH64_SLS_HARDENING_NAME "AArch64 sls hardening pass"
40
41// Common name prefix of all thunks generated by this pass.
42//
43// The generic form is
44// __llvm_slsblr_thunk_xN for BLR thunks
45// __llvm_slsblr_thunk_(aaz|abz)_xN for BLRAAZ and BLRABZ thunks
46// __llvm_slsblr_thunk_(aa|ab)_xN_xM for BLRAA and BLRAB thunks
47static constexpr StringRef CommonNamePrefix = "__llvm_slsblr_thunk_";
48
49namespace {
50
51struct ThunkKind {
52 enum ThunkKindId {
53 ThunkBR,
54 ThunkBRAA,
55 ThunkBRAB,
56 ThunkBRAAZ,
57 ThunkBRABZ,
58 };
59
60 ThunkKindId Id;
61 StringRef NameInfix;
62 bool HasXmOperand;
63 bool NeedsPAuth;
64
65 // Opcode to perform indirect jump from inside the thunk.
66 unsigned BROpcode;
67
68 static const ThunkKind BR;
69 static const ThunkKind BRAA;
70 static const ThunkKind BRAB;
71 static const ThunkKind BRAAZ;
72 static const ThunkKind BRABZ;
73};
74
75// Set of inserted thunks.
76class ThunksSet {
77public:
78 static constexpr unsigned NumXRegisters = 32;
79
80 // Given Xn register, returns n.
81 static unsigned indexOfXReg(Register Xn);
82 // Given n, returns Xn register.
83 static Register xRegByIndex(unsigned N);
84
85 ThunksSet &operator|=(const ThunksSet &Other) {
86 BLRThunks |= Other.BLRThunks;
87 BLRAAZThunks |= Other.BLRAAZThunks;
88 BLRABZThunks |= Other.BLRABZThunks;
89 for (unsigned I = 0; I < NumXRegisters; ++I)
90 BLRAAThunks[I] |= Other.BLRAAThunks[I];
91 for (unsigned I = 0; I < NumXRegisters; ++I)
92 BLRABThunks[I] |= Other.BLRABThunks[I];
93
94 return *this;
95 }
96
97 bool get(ThunkKind::ThunkKindId Kind, Register Xn, Register Xm) {
98 reg_bitmask_t XnBit = reg_bitmask_t(1) << indexOfXReg(Xn);
99 return getBitmask(Kind, Xm) & XnBit;
100 }
101
102 void set(ThunkKind::ThunkKindId Kind, Register Xn, Register Xm) {
103 reg_bitmask_t XnBit = reg_bitmask_t(1) << indexOfXReg(Xn);
104 getBitmask(Kind, Xm) |= XnBit;
105 }
106
107private:
108 typedef uint32_t reg_bitmask_t;
109 static_assert(NumXRegisters <= sizeof(reg_bitmask_t) * CHAR_BIT,
110 "Bitmask is not wide enough to hold all Xn registers");
111
112 // Bitmasks representing operands used, with n-th bit corresponding to Xn
113 // register operand. If the instruction has a second operand (Xm), an array
114 // of bitmasks is used, indexed by m.
115 // Indexes corresponding to the forbidden x16, x17 and x30 registers are
116 // always unset, for simplicity there are no holes.
117 reg_bitmask_t BLRThunks = 0;
118 reg_bitmask_t BLRAAZThunks = 0;
119 reg_bitmask_t BLRABZThunks = 0;
120 reg_bitmask_t BLRAAThunks[NumXRegisters] = {};
121 reg_bitmask_t BLRABThunks[NumXRegisters] = {};
122
123 reg_bitmask_t &getBitmask(ThunkKind::ThunkKindId Kind, Register Xm) {
124 switch (Kind) {
125 case ThunkKind::ThunkBR:
126 return BLRThunks;
127 case ThunkKind::ThunkBRAAZ:
128 return BLRAAZThunks;
129 case ThunkKind::ThunkBRABZ:
130 return BLRABZThunks;
131 case ThunkKind::ThunkBRAA:
132 return BLRAAThunks[indexOfXReg(Xn: Xm)];
133 case ThunkKind::ThunkBRAB:
134 return BLRABThunks[indexOfXReg(Xn: Xm)];
135 }
136 llvm_unreachable("Unknown ThunkKindId enum");
137 }
138};
139
140} // end anonymous namespace
141
142struct SLSHardeningInserter : ThunkInserter<SLSHardeningInserter, ThunksSet> {
143public:
144 const char *getThunkPrefix() { return CommonNamePrefix.data(); }
145 bool mayUseThunk(const MachineFunction &MF) {
146 ComdatThunks &= !MF.getSubtarget<AArch64Subtarget>().hardenSlsNoComdat();
147 // We are inserting barriers aside from thunk calls, so
148 // check hardenSlsRetBr() as well.
149 return MF.getSubtarget<AArch64Subtarget>().hardenSlsBlr() ||
150 MF.getSubtarget<AArch64Subtarget>().hardenSlsRetBr();
151 }
152 ThunksSet insertThunks(MachineModuleInfo &MMI, MachineFunction &MF,
153 ThunksSet ExistingThunks);
154 void populateThunk(MachineFunction &MF);
155
156private:
157 bool ComdatThunks = true;
158
159 bool hardenReturnsAndBRs(MachineModuleInfo &MMI, MachineBasicBlock &MBB);
160 bool hardenBLRs(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
161 ThunksSet &Thunks);
162
163 void convertBLRToBL(MachineModuleInfo &MMI, MachineBasicBlock &MBB,
164 MachineBasicBlock::instr_iterator MBBI,
165 ThunksSet &Thunks);
166};
167
168const ThunkKind ThunkKind::BR = {.Id: ThunkBR, .NameInfix: "", /*HasXmOperand=*/false,
169 /*NeedsPAuth=*/false, .BROpcode: AArch64::BR};
170const ThunkKind ThunkKind::BRAA = {.Id: ThunkBRAA, .NameInfix: "aa_", /*HasXmOperand=*/true,
171 /*NeedsPAuth=*/true, .BROpcode: AArch64::BRAA};
172const ThunkKind ThunkKind::BRAB = {.Id: ThunkBRAB, .NameInfix: "ab_", /*HasXmOperand=*/true,
173 /*NeedsPAuth=*/true, .BROpcode: AArch64::BRAB};
174const ThunkKind ThunkKind::BRAAZ = {.Id: ThunkBRAAZ, .NameInfix: "aaz_", /*HasXmOperand=*/false,
175 /*NeedsPAuth=*/true, .BROpcode: AArch64::BRAAZ};
176const ThunkKind ThunkKind::BRABZ = {.Id: ThunkBRABZ, .NameInfix: "abz_", /*HasXmOperand=*/false,
177 /*NeedsPAuth=*/true, .BROpcode: AArch64::BRABZ};
178
179// Returns thunk kind to emit, or nullptr if not a BLR* instruction.
180static const ThunkKind *getThunkKind(unsigned OriginalOpcode) {
181 switch (OriginalOpcode) {
182 case AArch64::BLR:
183 case AArch64::BLRNoIP:
184 return &ThunkKind::BR;
185 case AArch64::BLRAA:
186 return &ThunkKind::BRAA;
187 case AArch64::BLRAB:
188 return &ThunkKind::BRAB;
189 case AArch64::BLRAAZ:
190 return &ThunkKind::BRAAZ;
191 case AArch64::BLRABZ:
192 return &ThunkKind::BRABZ;
193 }
194 return nullptr;
195}
196
197static bool isBLR(const MachineInstr &MI) {
198 return getThunkKind(OriginalOpcode: MI.getOpcode()) != nullptr;
199}
200
201unsigned ThunksSet::indexOfXReg(Register Reg) {
202 assert(AArch64::GPR64RegClass.contains(Reg));
203 assert(Reg != AArch64::X16 && Reg != AArch64::X17 && Reg != AArch64::LR);
204
205 // Most Xn registers have consecutive ids, except for FP and XZR.
206 unsigned Result = (unsigned)Reg - (unsigned)AArch64::X0;
207 if (Reg == AArch64::FP)
208 Result = 29;
209 else if (Reg == AArch64::XZR)
210 Result = 31;
211
212 assert(Result < NumXRegisters && "Internal register numbering changed");
213 assert(AArch64::GPR64RegClass.getRegister(Result).id() == Reg &&
214 "Internal register numbering changed");
215
216 return Result;
217}
218
219Register ThunksSet::xRegByIndex(unsigned N) {
220 return AArch64::GPR64RegClass.getRegister(i: N);
221}
222
223static void insertSpeculationBarrier(const AArch64Subtarget *ST,
224 MachineBasicBlock &MBB,
225 MachineBasicBlock::iterator MBBI,
226 DebugLoc DL,
227 bool AlwaysUseISBDSB = false) {
228 assert(MBBI != MBB.begin() &&
229 "Must not insert SpeculationBarrierEndBB as only instruction in MBB.");
230 assert(std::prev(MBBI)->isBarrier() &&
231 "SpeculationBarrierEndBB must only follow unconditional control flow "
232 "instructions.");
233 assert(std::prev(MBBI)->isTerminator() &&
234 "SpeculationBarrierEndBB must only follow terminators.");
235 const TargetInstrInfo *TII = ST->getInstrInfo();
236 unsigned BarrierOpc = ST->hasSB() && !AlwaysUseISBDSB
237 ? AArch64::SpeculationBarrierSBEndBB
238 : AArch64::SpeculationBarrierISBDSBEndBB;
239 if (MBBI == MBB.end() ||
240 (MBBI->getOpcode() != AArch64::SpeculationBarrierSBEndBB &&
241 MBBI->getOpcode() != AArch64::SpeculationBarrierISBDSBEndBB))
242 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: BarrierOpc));
243}
244
245ThunksSet SLSHardeningInserter::insertThunks(MachineModuleInfo &MMI,
246 MachineFunction &MF,
247 ThunksSet ExistingThunks) {
248 const AArch64Subtarget *ST = &MF.getSubtarget<AArch64Subtarget>();
249
250 for (auto &MBB : MF) {
251 if (ST->hardenSlsRetBr())
252 hardenReturnsAndBRs(MMI, MBB);
253 if (ST->hardenSlsBlr())
254 hardenBLRs(MMI, MBB, Thunks&: ExistingThunks);
255 }
256 return ExistingThunks;
257}
258
259bool SLSHardeningInserter::hardenReturnsAndBRs(MachineModuleInfo &MMI,
260 MachineBasicBlock &MBB) {
261 const AArch64Subtarget *ST =
262 &MBB.getParent()->getSubtarget<AArch64Subtarget>();
263 bool Modified = false;
264 MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator(), E = MBB.end();
265 MachineBasicBlock::iterator NextMBBI;
266 for (; MBBI != E; MBBI = NextMBBI) {
267 MachineInstr &MI = *MBBI;
268 NextMBBI = std::next(x: MBBI);
269 if (MI.isReturn() || isIndirectBranchOpcode(Opc: MI.getOpcode())) {
270 assert(MI.isTerminator());
271 insertSpeculationBarrier(ST, MBB, MBBI: std::next(x: MBBI), DL: MI.getDebugLoc());
272 Modified = true;
273 }
274 }
275 return Modified;
276}
277
278// Currently, the longest possible thunk name is
279// __llvm_slsblr_thunk_aa_xNN_xMM
280// which is 31 characters (without the '\0' character).
281static SmallString<32> createThunkName(const ThunkKind &Kind, Register Xn,
282 Register Xm) {
283 unsigned N = ThunksSet::indexOfXReg(Reg: Xn);
284 if (!Kind.HasXmOperand)
285 return formatv(Fmt: "{0}{1}x{2}", Vals: CommonNamePrefix, Vals: Kind.NameInfix, Vals&: N);
286
287 unsigned M = ThunksSet::indexOfXReg(Reg: Xm);
288 return formatv(Fmt: "{0}{1}x{2}_x{3}", Vals: CommonNamePrefix, Vals: Kind.NameInfix, Vals&: N, Vals&: M);
289}
290
291static std::tuple<const ThunkKind &, Register, Register>
292parseThunkName(StringRef ThunkName) {
293 assert(ThunkName.starts_with(CommonNamePrefix) &&
294 "Should be filtered out by ThunkInserter");
295 // Thunk name suffix, such as "x1" or "aa_x2_x3".
296 StringRef NameSuffix = ThunkName.drop_front(N: CommonNamePrefix.size());
297
298 // Parse thunk kind based on thunk name infix.
299 const ThunkKind &Kind = *StringSwitch<const ThunkKind *>(NameSuffix)
300 .StartsWith(S: "aa_", Value: &ThunkKind::BRAA)
301 .StartsWith(S: "ab_", Value: &ThunkKind::BRAB)
302 .StartsWith(S: "aaz_", Value: &ThunkKind::BRAAZ)
303 .StartsWith(S: "abz_", Value: &ThunkKind::BRABZ)
304 .Default(Value: &ThunkKind::BR);
305
306 auto ParseRegName = [](StringRef Name) {
307 unsigned N;
308
309 assert(Name.starts_with("x") && "xN register name expected");
310 bool Fail = Name.drop_front(N: 1).getAsInteger(/*Radix=*/10, Result&: N);
311 assert(!Fail && N < ThunksSet::NumXRegisters && "Unexpected register");
312 (void)Fail;
313
314 return ThunksSet::xRegByIndex(N);
315 };
316
317 // For example, "x1" or "x2_x3".
318 StringRef RegsStr = NameSuffix.drop_front(N: Kind.NameInfix.size());
319 StringRef XnStr, XmStr;
320 std::tie(args&: XnStr, args&: XmStr) = RegsStr.split(Separator: '_');
321
322 // Parse register operands.
323 Register Xn = ParseRegName(XnStr);
324 Register Xm = Kind.HasXmOperand ? ParseRegName(XmStr) : Register();
325
326 return std::make_tuple(args: std::ref(t: Kind), args&: Xn, args&: Xm);
327}
328
329void SLSHardeningInserter::populateThunk(MachineFunction &MF) {
330 assert(MF.getFunction().hasComdat() == ComdatThunks &&
331 "ComdatThunks value changed since MF creation");
332 Register Xn, Xm;
333 auto KindAndRegs = parseThunkName(ThunkName: MF.getName());
334 const ThunkKind &Kind = std::get<0>(t&: KindAndRegs);
335 std::tie(args: std::ignore, args&: Xn, args&: Xm) = KindAndRegs;
336
337 const TargetInstrInfo *TII =
338 MF.getSubtarget<AArch64Subtarget>().getInstrInfo();
339
340 // Depending on whether this pass is in the same FunctionPassManager as the
341 // IR->MIR conversion, the thunk may be completely empty, or contain a single
342 // basic block with a single return instruction. Normalise it to contain a
343 // single empty basic block.
344 if (MF.size() == 1) {
345 assert(MF.front().size() == 1);
346 assert(MF.front().front().getOpcode() == AArch64::RET);
347 MF.front().erase(I: MF.front().begin());
348 } else {
349 assert(MF.size() == 0);
350 MF.push_back(MBB: MF.CreateMachineBasicBlock());
351 }
352
353 MachineBasicBlock *Entry = &MF.front();
354 Entry->clear();
355
356 // These thunks need to consist of the following instructions:
357 // __llvm_slsblr_thunk_...:
358 // MOV x16, xN ; BR* instructions are not compatible with "BTI c"
359 // ; branch target unless xN is x16 or x17.
360 // BR* ... ; One of: BR x16
361 // ; BRA(A|B) x16, xM
362 // ; BRA(A|B)Z x16
363 // barrierInsts
364 Entry->addLiveIn(PhysReg: Xn);
365 // MOV X16, Reg == ORR X16, XZR, Reg, LSL #0
366 BuildMI(BB: Entry, MIMD: DebugLoc(), MCID: TII->get(Opcode: AArch64::ORRXrs), DestReg: AArch64::X16)
367 .addReg(RegNo: AArch64::XZR)
368 .addReg(RegNo: Xn)
369 .addImm(Val: 0);
370 MachineInstrBuilder Builder =
371 BuildMI(BB: Entry, MIMD: DebugLoc(), MCID: TII->get(Opcode: Kind.BROpcode)).addReg(RegNo: AArch64::X16);
372 if (Xm.isValid()) {
373 Entry->addLiveIn(PhysReg: Xm);
374 Builder.addReg(RegNo: Xm);
375 }
376
377 // Make sure the thunks do not make use of the SB extension in case there is
378 // a function somewhere that will call to it that for some reason disabled
379 // the SB extension locally on that function, even though it's enabled for
380 // the module otherwise. Therefore set AlwaysUseISBSDB to true.
381 insertSpeculationBarrier(ST: &MF.getSubtarget<AArch64Subtarget>(), MBB&: *Entry,
382 MBBI: Entry->end(), DL: DebugLoc(), AlwaysUseISBDSB: true /*AlwaysUseISBDSB*/);
383}
384
385void SLSHardeningInserter::convertBLRToBL(
386 MachineModuleInfo &MMI, MachineBasicBlock &MBB,
387 MachineBasicBlock::instr_iterator MBBI, ThunksSet &Thunks) {
388 // Transform a BLR* instruction (one of BLR, BLRAA/BLRAB or BLRAAZ/BLRABZ) to
389 // a BL to the thunk containing BR, BRAA/BRAB or BRAAZ/BRABZ, respectively.
390 //
391 // Before:
392 // |-----------------------------|
393 // | ... |
394 // | instI |
395 // | BLR* xN or BLR* xN, xM |
396 // | instJ |
397 // | ... |
398 // |-----------------------------|
399 //
400 // After:
401 // |-----------------------------|
402 // | ... |
403 // | instI |
404 // | BL __llvm_slsblr_thunk_... |
405 // | instJ |
406 // | ... |
407 // |-----------------------------|
408 //
409 // __llvm_slsblr_thunk_...:
410 // |-----------------------------|
411 // | MOV x16, xN |
412 // | BR* x16 or BR* x16, xM |
413 // | barrierInsts |
414 // |-----------------------------|
415 //
416 // This function needs to transform BLR* instruction into BL with the correct
417 // thunk name and lazily create the thunk if it does not exist yet.
418 //
419 // Since linkers are allowed to clobber X16 and X17 on function calls, the
420 // above mitigation only works if the original BLR* instruction had neither
421 // X16 nor X17 as one of its operands. Code generation before must make sure
422 // that no such BLR* instruction was produced if the mitigation is enabled.
423
424 MachineInstr &BLR = *MBBI;
425 assert(isBLR(BLR));
426 const ThunkKind &Kind = *getThunkKind(OriginalOpcode: BLR.getOpcode());
427
428 unsigned NumRegOperands = Kind.HasXmOperand ? 2 : 1;
429 assert(BLR.getNumExplicitOperands() == NumRegOperands &&
430 "Expected one or two register inputs");
431 Register Xn = BLR.getOperand(i: 0).getReg();
432 Register Xm = Kind.HasXmOperand ? BLR.getOperand(i: 1).getReg() : Register();
433
434 DebugLoc DL = BLR.getDebugLoc();
435
436 MachineFunction &MF = *MBBI->getMF();
437 MCContext &Context = MBB.getParent()->getContext();
438 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
439
440 auto ThunkName = createThunkName(Kind, Xn, Xm);
441 MCSymbol *Sym = Context.getOrCreateSymbol(Name: ThunkName);
442
443 if (!Thunks.get(Kind: Kind.Id, Xn, Xm)) {
444 StringRef TargetAttrs = Kind.NeedsPAuth ? "+pauth" : "";
445 Thunks.set(Kind: Kind.Id, Xn, Xm);
446 createThunkFunction(MMI, Name: ThunkName, Comdat: ComdatThunks, TargetAttrs);
447 }
448
449 MachineInstr *BL = BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: AArch64::BL)).addSym(Sym);
450
451 // Now copy the implicit operands from BLR to BL and copy other necessary
452 // info.
453 // However, both BLR and BL instructions implicitly use SP and implicitly
454 // define LR. Blindly copying implicit operands would result in SP and LR
455 // operands to be present multiple times. While this may not be too much of
456 // an issue, let's avoid that for cleanliness, by removing those implicit
457 // operands from the BL created above before we copy over all implicit
458 // operands from the BLR.
459 int ImpLROpIdx = -1;
460 int ImpSPOpIdx = -1;
461 for (unsigned OpIdx = BL->getNumExplicitOperands();
462 OpIdx < BL->getNumOperands(); OpIdx++) {
463 MachineOperand Op = BL->getOperand(i: OpIdx);
464 if (!Op.isReg())
465 continue;
466 if (Op.getReg() == AArch64::LR && Op.isDef())
467 ImpLROpIdx = OpIdx;
468 if (Op.getReg() == AArch64::SP && !Op.isDef())
469 ImpSPOpIdx = OpIdx;
470 }
471 assert(ImpLROpIdx != -1);
472 assert(ImpSPOpIdx != -1);
473 int FirstOpIdxToRemove = std::max(a: ImpLROpIdx, b: ImpSPOpIdx);
474 int SecondOpIdxToRemove = std::min(a: ImpLROpIdx, b: ImpSPOpIdx);
475 BL->removeOperand(OpNo: FirstOpIdxToRemove);
476 BL->removeOperand(OpNo: SecondOpIdxToRemove);
477 // Now copy over the implicit operands from the original BLR
478 BL->copyImplicitOps(MF, MI: BLR);
479 MF.moveAdditionalCallInfo(Old: &BLR, New: BL);
480 // Also add the register operands of the original BLR* instruction
481 // as being used in the called thunk.
482 for (unsigned OpIdx = 0; OpIdx < NumRegOperands; ++OpIdx) {
483 MachineOperand &Op = BLR.getOperand(i: OpIdx);
484 BL->addOperand(Op: MachineOperand::CreateReg(Reg: Op.getReg(), /*isDef=*/false,
485 /*isImp=*/true, isKill: Op.isKill()));
486 }
487 // Remove BLR instruction
488 MBB.erase(I: MBBI);
489}
490
491bool SLSHardeningInserter::hardenBLRs(MachineModuleInfo &MMI,
492 MachineBasicBlock &MBB,
493 ThunksSet &Thunks) {
494 bool Modified = false;
495 MachineBasicBlock::instr_iterator MBBI = MBB.instr_begin(),
496 E = MBB.instr_end();
497 MachineBasicBlock::instr_iterator NextMBBI;
498 for (; MBBI != E; MBBI = NextMBBI) {
499 MachineInstr &MI = *MBBI;
500 NextMBBI = std::next(x: MBBI);
501 if (isBLR(MI)) {
502 convertBLRToBL(MMI, MBB, MBBI, Thunks);
503 Modified = true;
504 }
505 }
506 return Modified;
507}
508
509namespace {
510class AArch64SLSHardeningLegacy
511 : public ThunkInserterPass<SLSHardeningInserter> {
512public:
513 static char ID;
514
515 AArch64SLSHardeningLegacy() : ThunkInserterPass(ID) {}
516
517 StringRef getPassName() const override { return AARCH64_SLS_HARDENING_NAME; }
518};
519
520} // end anonymous namespace
521
522char AArch64SLSHardeningLegacy::ID = 0;
523
524INITIALIZE_PASS(AArch64SLSHardeningLegacy, "aarch64-sls-hardening",
525 AARCH64_SLS_HARDENING_NAME, false, false)
526
527FunctionPass *llvm::createAArch64SLSHardeningLegacyPass() {
528 return new AArch64SLSHardeningLegacy();
529}
530
531AArch64SLSHardeningPass::AArch64SLSHardeningPass()
532 : Inserter(std::make_unique<SLSHardeningInserter>()) {}
533
534AArch64SLSHardeningPass::AArch64SLSHardeningPass(AArch64SLSHardeningPass &&) =
535 default;
536AArch64SLSHardeningPass::~AArch64SLSHardeningPass() = default;
537
538PreservedAnalyses
539AArch64SLSHardeningPass::run(MachineFunction &MF,
540 MachineFunctionAnalysisManager &MFAM) {
541 MachineModuleAnalysis::Result *MMI =
542 MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF)
543 .getCachedResult<MachineModuleAnalysis>(
544 IR&: *MF.getFunction().getParent());
545 assert(MMI && "MachineModuleAnalysis must be available");
546
547 if (Inserter->run(MMI&: MMI->getMMI(), MF)) {
548 PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
549 PA.preserveSet<CFGAnalyses>();
550 return PA;
551 }
552 return PreservedAnalyses::all();
553}
554