112#define DEBUG_TYPE "aarch64-speculation-hardening"
114#define AARCH64_SPECULATION_HARDENING_NAME "AArch64 speculation hardening pass"
117 cl::desc(
"Sanitize loads from memory."),
140 unsigned MisspeculatingTaintReg;
141 unsigned MisspeculatingTaintReg32Bit;
142 bool UseControlFlowSpeculationBarrier;
148 bool &UsesFullSpeculationBarrier);
158 unsigned TmpReg)
const;
168 bool UsesFullSpeculationBarrier);
171 bool UsesFullSpeculationBarrier);
178char AArch64SpeculationHardening::ID = 0;
183bool AArch64SpeculationHardening::endsWithCondControlFlow(
185 AArch64CC::CondCode &CondCode)
const {
191 if (analyzeBranchCondCode.
empty())
209 assert(analyzeBranchCondCode.
size() == 1 &&
"unknown Cond array format");
214void AArch64SpeculationHardening::insertFullSpeculationBarrier(
222void AArch64SpeculationHardening::insertTrackingCode(
225 if (UseControlFlowSpeculationBarrier) {
226 insertFullSpeculationBarrier(SplitEdgeBB, SplitEdgeBB.
begin(),
DL);
229 .
addDef(MisspeculatingTaintReg)
230 .
addUse(MisspeculatingTaintReg)
237bool AArch64SpeculationHardening::instrumentControlFlow(
246 if (!endsWithCondControlFlow(
MBB,
TBB, FBB, CondCode)) {
258 assert(SplitEdgeTBB !=
nullptr);
259 assert(SplitEdgeFBB !=
nullptr);
265 insertTrackingCode(*SplitEdgeTBB, CondCode,
DL);
266 insertTrackingCode(*SplitEdgeFBB, InvCondCode,
DL);
283 bool TmpRegisterNotAvailableEverywhere =
false;
290 if (!
MI.isReturn() && !
MI.isCall())
308 << ((TmpReg == 0) ?
"no register " :
"register ");
310 dbgs() <<
"to be available at MI " <<
MI);
312 TmpRegisterNotAvailableEverywhere =
true;
315 else if (
MI.isCall())
319 if (TmpRegisterNotAvailableEverywhere) {
326 UsesFullSpeculationBarrier =
true;
329 for (
auto MI_Reg : ReturnInstructions) {
330 assert(MI_Reg.second != 0);
333 <<
" About to insert Reg to SP taint propagation with temp register "
335 <<
" on instruction: " << *MI_Reg.first);
336 insertRegToSPTaintPropagation(
MBB, MI_Reg.first, MI_Reg.second);
340 for (
auto MI_Reg : CallInstructions) {
341 assert(MI_Reg.second != 0);
343 "propagation with temp register "
345 <<
" around instruction: " << *MI_Reg.first);
347 insertSPToRegTaintPropagation(
350 insertRegToSPTaintPropagation(
MBB, MI_Reg.first, MI_Reg.second);
357void AArch64SpeculationHardening::insertSPToRegTaintPropagation(
362 if (UseControlFlowSpeculationBarrier) {
375 .
addDef(MisspeculatingTaintReg)
381void AArch64SpeculationHardening::insertRegToSPTaintPropagation(
383 unsigned TmpReg)
const {
387 if (UseControlFlowSpeculationBarrier)
410bool AArch64SpeculationHardening::functionUsesHardeningRegister(
418 if (
MI.readsRegister(MisspeculatingTaintReg,
TRI) ||
419 MI.modifiesRegister(MisspeculatingTaintReg,
TRI))
429bool AArch64SpeculationHardening::makeGPRSpeculationSafe(
433 AArch64::GPR64allRegClass.
contains(Reg));
440 if (Reg == AArch64::SP || Reg == AArch64::WSP)
444 if (RegsAlreadyMasked[Reg])
447 const bool Is64Bit = AArch64::GPR64allRegClass.contains(Reg);
448 LLVM_DEBUG(
dbgs() <<
"About to harden register : " << Reg <<
"\n");
450 TII->get(Is64Bit ? AArch64::SpeculationSafeValueX
451 : AArch64::SpeculationSafeValueW))
454 RegsAlreadyMasked.set(Reg);
463 RegsAlreadyMasked.reset();
467 for (;
MBBI != E;
MBBI = NextMBBI) {
469 NextMBBI = std::next(
MBBI);
484 return Op.isReg() && (AArch64::GPR32allRegClass.contains(Op.getReg()) ||
485 AArch64::GPR64allRegClass.contains(Op.getReg()));
491 bool HardenLoadedData = AllDefsAreGPR;
492 bool HardenAddressLoadedFrom = !HardenLoadedData;
499 RegsAlreadyMasked.reset(*AI);
507 if (HardenLoadedData)
508 for (
auto Def :
MI.defs()) {
520 if (HardenAddressLoadedFrom)
521 for (
auto Use :
MI.uses()) {
535 if (!(AArch64::GPR32allRegClass.
contains(Reg) ||
536 AArch64::GPR64allRegClass.
contains(Reg)))
546bool AArch64SpeculationHardening::expandSpeculationSafeValue(
548 bool UsesFullSpeculationBarrier) {
550 unsigned Opcode =
MI.getOpcode();
556 case AArch64::SpeculationSafeValueW:
559 case AArch64::SpeculationSafeValueX:
563 if (!UseControlFlowSpeculationBarrier && !UsesFullSpeculationBarrier) {
571 RegsNeedingCSDBBeforeUse.set(*AI);
575 Is64Bit ?
TII->get(AArch64::ANDXrs) :
TII->get(AArch64::ANDWrs))
578 .
addUse(Is64Bit ? MisspeculatingTaintReg
579 : MisspeculatingTaintReg32Bit)
582 MI.eraseFromParent();
591 assert(!UseControlFlowSpeculationBarrier &&
"No need to insert CSDBs when "
592 "control flow miss-speculation "
593 "is already blocked");
596 RegsNeedingCSDBBeforeUse.reset();
600bool AArch64SpeculationHardening::lowerSpeculationSafeValuePseudos(
604 RegsNeedingCSDBBeforeUse.reset();
620 DL =
MI.getDebugLoc();
626 bool NeedToEmitBarrier =
false;
627 if (RegsNeedingCSDBBeforeUse.any() && (
MI.isCall() ||
MI.isTerminator()))
628 NeedToEmitBarrier =
true;
629 if (!NeedToEmitBarrier)
631 if (
Op.isReg() && RegsNeedingCSDBBeforeUse[
Op.getReg()]) {
632 NeedToEmitBarrier =
true;
636 if (NeedToEmitBarrier && !UsesFullSpeculationBarrier)
640 expandSpeculationSafeValue(
MBB,
MBBI, UsesFullSpeculationBarrier);
645 if (RegsNeedingCSDBBeforeUse.any() && !UsesFullSpeculationBarrier)
651bool AArch64SpeculationHardening::runOnMachineFunction(
MachineFunction &MF) {
655 MisspeculatingTaintReg = AArch64::X16;
656 MisspeculatingTaintReg32Bit = AArch64::W16;
659 RegsNeedingCSDBBeforeUse.resize(
TRI->getNumRegs());
660 RegsAlreadyMasked.resize(
TRI->getNumRegs());
661 UseControlFlowSpeculationBarrier = functionUsesHardeningRegister(MF);
668 dbgs() <<
"***** AArch64SpeculationHardening - automatic insertion of "
669 "SpeculationSafeValue intrinsics *****\n");
677 <<
"***** AArch64SpeculationHardening - track control flow *****\n");
682 EntryBlocks.
push_back(LPI.LandingPadBlock);
683 for (
auto *Entry : EntryBlocks)
684 insertSPToRegTaintPropagation(
685 *Entry,
Entry->SkipPHIsLabelsAndDebug(
Entry->begin()));
688 for (
auto &
MBB : MF) {
689 bool UsesFullSpeculationBarrier =
false;
690 Modified |= instrumentControlFlow(
MBB, UsesFullSpeculationBarrier);
692 lowerSpeculationSafeValuePseudos(
MBB, UsesFullSpeculationBarrier);
700 return new AArch64SpeculationHardening();
#define AARCH64_SPECULATION_HARDENING_NAME
static cl::opt< bool > HardenLoads("aarch64-slh-loads", cl::Hidden, cl::desc("Sanitize loads from memory."), cl::init(true))
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
const HexagonInstrInfo * TII
static DebugLoc getDebugLoc(MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
Return the first found DebugLoc that has a DILocation, given a range of instructions.
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file declares the machine register scavenger class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
This file defines the SmallVector class.
This class represents an Operation in the Expression.
FunctionPass class - This class is used to implement most global optimizations.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
MCRegAliasIterator enumerates all registers aliasing Reg.
instr_iterator instr_begin()
MachineBasicBlock * getFallThrough(bool JumpToFallThrough=true)
Return the fallthrough block if the block can implicitly transfer control to the block after it by fa...
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P, std::vector< SparseBitVector<> > *LiveInSets=nullptr, MachineDomTreeUpdater *MDTU=nullptr)
Split the critical edge from this block to the given successor block, and return the newly created bl...
unsigned succ_size() const
instr_iterator instr_end()
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
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.
void enterBasicBlockEnd(MachineBasicBlock &MBB)
Start tracking liveness from the end of basic block MBB.
Register FindUnusedReg(const TargetRegisterClass *RC) const
Find an unused register of the specified register class.
void backward()
Update internal register state and move MBB iterator backwards.
void enterBasicBlock(MachineBasicBlock &MBB)
Start tracking liveness from the begin of basic block MBB.
Wrapper class representing virtual and physical registers.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
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
A Use represents the edge between a Value definition and its users.
static CondCode getInvertedCondCode(CondCode Code)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
@ Renamable
Register that may be renamed.
@ Kill
The last use of a register.
Reg
All possible values of the reg field in the ModR/M byte.
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeAArch64SpeculationHardeningPass(PassRegistry &)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createAArch64SpeculationHardeningPass()
Returns an instance of the pseudo instruction expansion pass.
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
This structure is used to retain landing pad info for the current function.