28#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"
32class AArch64PointerAuthImpl {
65 return new AArch64PointerAuthLegacy();
68char AArch64PointerAuthLegacy::ID = 0;
97template <
typename BuildPACMIFn>
100 BuildPACMIFn BuildPACMI) {
106 auto &MF = *
MBB.getParent();
109 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
112 .cfi_llvm_set_ra_sign_state;
114 if (MFnI.branchProtectionPAuthLR()) {
117 CFIBuilder.buildNegateRAStateWithPC();
123 MCSymbol *PACSym = MFnI.getSigningInstrLabel();
124 assert(PACSym &&
"No PAC instruction to refer to");
125 CFIBuilder.buildSetRAState(2, PACSym);
134 if (!TT.isOSBinFormatMachO()) {
135 CFIBuilder.buildNegateRAState();
140 CFIBuilder.buildSetRAState(1,
nullptr);
151 auto &MF = *
MBB.getParent();
154 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
157 .cfi_llvm_set_ra_sign_state;
159 if (MFnI.branchProtectionPAuthLR()) {
198 CFIBuilder.buildSetRAState(0,
nullptr);
201 }
else if (!TT.isOSBinFormatMachO()) {
205 CFIBuilder.buildNegateRAState();
208 CFIBuilder.buildSetRAState(0,
nullptr);
229 auto &MFnI = *MF.
getInfo<AArch64FunctionInfo>();
230 bool UseBKey = MFnI.shouldSignWithBKey();
231 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);
246 if (MFnI.branchProtectionPAuthLR()) {
248 MFnI.setSigningInstrLabel(PACSym);
253 if (MFnI.branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
256 TII->get(UseBKey ? AArch64::PACIBSPPC : AArch64::PACIASPPC))
261 if (MFnI.branchProtectionPAuthLR()) {
267 TII->get(UseBKey ? AArch64::PACIBSP : AArch64::PACIASP))
273 if (!EmitCFI && NeedsWinCFI) {
275 "Windows SEH PAC unwind info only supports B-key signing");
281void AArch64PointerAuthImpl::authenticateLR(
283 const AArch64FunctionInfo *MFnI = MF.
getInfo<AArch64FunctionInfo>();
298 auto &AFL = *
static_cast<const AArch64FrameLowering *
>(
300 int64_t ArgumentStackToRestore = AFL.getArgumentStackToRestore(MF,
MBB);
312 bool TerminatorIsCombinable = std::next(
MBBI) == TI && TI !=
MBB.
end() &&
313 TI->getOpcode() == AArch64::RET &&
314 ArgumentStackToRestore == 0 &&
317 if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&
320 assert(PACSym &&
"No PAC instruction to refer to");
322 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))
333 BuildMI(
MBB, TI,
DL,
TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))
343 if (ArgumentStackToRestore == 0) {
345 assert(PACSym &&
"No PAC instruction to refer to");
347 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))
360 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))
367 "Windows SEH PAC unwind info only supports B-key signing");
397 assert(PACSym &&
"No PAC instruction to refer to");
400 unsigned AutOpc = UseBKey ? AArch64::AUTIB171615 : AArch64::AUTIA171615;
409 assert(PACSym &&
"No PAC instruction to refer to");
420 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
426 }
else if (Subtarget->hasPAuth()) {
436 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
446 "Windows SEH PAC unwind info only supports B-key signing");
475 for (
auto &
MBB : MF) {
476 for (
auto &
MI :
MBB) {
477 switch (
MI.getOpcode()) {
480 case AArch64::PAUTH_PROLOGUE:
481 case AArch64::PAUTH_EPILOGUE:
488 for (
auto It : PAuthPseudoInstrs) {
489 switch (It->getOpcode()) {
490 case AArch64::PAUTH_PROLOGUE:
493 case AArch64::PAUTH_EPILOGUE:
494 authenticateLR(MF, It);
499 It->eraseFromParent();
503 Modified |= emitSignReturnAddressHardening(MF);
508bool AArch64PointerAuthImpl::emitSignReturnAddressHardening(
510 const auto *FI = MF.
getInfo<AArch64FunctionInfo>();
511 assert(FI &&
"FI can't be null");
512 if (!FI->shouldSignReturnAddress(MF) || !FI->shouldHardenSignReturnAddress())
514 assert(Subtarget &&
"Subtarget must be initialized");
518 for (MachineBasicBlock &
MBB : MF) {
521 if (RetInstIter ==
MBB.
end() || RetInstIter->getOpcode() != AArch64::RET)
524 assert(RetInstIter->getOperand(0).getReg() == AArch64::LR &&
525 "Return instruction must be returning via LR");
532 const bool NeedsWinCFI = MF.hasWinCFI();
535 assert(InsertionPoint->getOpcode() == AArch64::SEH_EpilogEnd);
538 const auto EmitSEHNopIfRequired = [&]() {
544 RS.enterBasicBlockEnd(
MBB);
546 AArch64::GPR64RegClass, InsertionPoint,
548 if (XReg == AArch64::NoRegister) {
550 MF.getContext().reportWarning(
551 SMLoc(),
"harden-pac-ret failed for function " + MF.getName());
560 EmitSEHNopIfRequired();
566 if (Subtarget->hasPAuth()) {
570 EmitSEHNopIfRequired();
577 MachinePointerInfo(),
581 EmitSEHNopIfRequired();
586 .buildRegister(AArch64::LR, XReg);
589 EmitSEHNopIfRequired();
590 BuildMI(
MBB, InsertionPoint,
DL,
TII->get(AArch64::LDRWui), AArch64::W30)
594 MachinePointerInfo(),
598 EmitSEHNopIfRequired();
610bool AArch64PointerAuthLegacy::runOnMachineFunction(
MachineFunction &MF) {
611 return AArch64PointerAuthImpl().run(MF);
617 const bool Changed = AArch64PointerAuthImpl().run(MF);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define AARCH64_POINTER_AUTH_NAME
static void emitEpiloguePACSymOffsetIntoReg(const TargetInstrInfo &TII, MachineBasicBlock &MBB, MachineBasicBlock::iterator I, DebugLoc DL, MCSymbol *PACSym, Register Reg)
static void emitMOVWithFrameDestroy(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI, DebugLoc DL, const AArch64InstrInfo *TII, Register Dst, Register Src)
static void emitAUTCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, bool EmitCFI)
static void decoratePACWithCFI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, bool EmitCFI, BuildPACMIFn BuildPACMI)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
Module.h This file contains the declarations for the Module class.
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file declares the machine register scavenger class.
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool branchProtectionPAuthLR() const
bool shouldHardenSignReturnAddress() const
bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const
MCSymbol * getSigningInstrLabel() const
bool shouldSignWithBKey() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const AArch64RegisterInfo * getRegisterInfo() const override
const AArch64InstrInfo * getInstrInfo() const override
Represents analyses that only rely on functions' control flow.
Helper class for creating CFI instructions and inserting them into MIR.
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.
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
LLVM_ABI iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
LLVM_ABI instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
LLVM_ABI instr_iterator getFirstInstrTerminator()
Same getFirstTerminator but it ignores bundles and return an instr_iterator instead.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MCContext & getContext() const
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 TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addSym(MCSymbol *Sym, unsigned char TargetFlags=0) const
const MachineInstrBuilder & copyImplicitOps(const MachineInstr &OtherMI) const
Copy all the implicit operands from OtherMI onto this one.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
@ MOVolatile
The memory access is volatile.
@ MOLoad
The memory access reads data.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
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.
int64_t getFixed() const
Returns the fixed component of the stack.
Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
const Triple & getTargetTriple() const
Triple - Helper class for working with autoconf configuration names.
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ MO_NC
MO_NC - Indicates whether the linker is expected to check the symbol reference for overflow.
@ MO_PAGEOFF
MO_PAGEOFF - A symbol operand with this flag represents the offset of that symbol within a 4K page.
@ MO_PAGE
MO_PAGE - A symbol operand with this flag represents the pc-relative offset of the 4K page containing...
unsigned getCheckerSizeInBytes(AuthCheckMethod Method)
Returns the number of bytes added by checkAuthenticatedRegister.
AuthCheckMethod
Variants of check performed on an authenticated pointer.
@ XPACHint
Check by comparing the authenticated value with an XPAC-ed one without using PAuth instructions not e...
@ DummyLoad
Perform a load to a temporary register.
@ HighBitsNoTBI
Check by comparing bits 62 and 61 of the authenticated address.
@ None
Do not check the value at all.
@ XPAC
Similar to XPACHint but using Armv8.3-only XPAC instruction, thus not restricted to LR:
SetRAStateMode
Control the emission of .cfi_set_ra_state, which replaces the deprecated .cfi_negate_ra_state_with_pc...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createAArch64PointerAuthPass()
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
void emitFrameOffset(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, unsigned DestReg, unsigned SrcReg, StackOffset Offset, const TargetInstrInfo *TII, MachineInstr::MIFlag=MachineInstr::NoFlags, bool SetNZCV=false, bool NeedsWinCFI=false, bool *HasWinCFI=nullptr, bool EmitCFAOffset=false, StackOffset InitialOffset={}, unsigned FrameReg=AArch64::SP)
emitFrameOffset - Emit instructions as needed to set DestReg to SrcReg plus Offset.