27#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"
31class AArch64PointerAuthImpl {
64 return new AArch64PointerAuthLegacy();
67char AArch64PointerAuthLegacy::ID = 0;
96template <
typename BuildPACMIFn>
99 BuildPACMIFn BuildPACMI) {
105 auto &MF = *
MBB.getParent();
108 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
111 .cfi_llvm_set_ra_sign_state;
113 if (MFnI.branchProtectionPAuthLR()) {
116 CFIBuilder.buildNegateRAStateWithPC();
122 MCSymbol *PACSym = MFnI.getSigningInstrLabel();
123 assert(PACSym &&
"No PAC instruction to refer to");
124 CFIBuilder.buildSetRAState(2, PACSym);
133 if (!TT.isOSBinFormatMachO()) {
134 CFIBuilder.buildNegateRAState();
139 CFIBuilder.buildSetRAState(1,
nullptr);
150 auto &MF = *
MBB.getParent();
153 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
156 .cfi_llvm_set_ra_sign_state;
158 if (MFnI.branchProtectionPAuthLR()) {
197 CFIBuilder.buildSetRAState(0,
nullptr);
200 }
else if (!TT.isOSBinFormatMachO()) {
204 CFIBuilder.buildNegateRAState();
207 CFIBuilder.buildSetRAState(0,
nullptr);
228 auto &MFnI = *MF.
getInfo<AArch64FunctionInfo>();
229 bool UseBKey = MFnI.shouldSignWithBKey();
230 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);
245 if (MFnI.branchProtectionPAuthLR()) {
247 MFnI.setSigningInstrLabel(PACSym);
252 if (MFnI.branchProtectionPAuthLR() &&
Subtarget->hasPAuthLR()) {
259 TII->get(UseBKey ? AArch64::PACIBSPPC : AArch64::PACIASPPC))
264 if (MFnI.branchProtectionPAuthLR()) {
270 TII->get(UseBKey ? AArch64::PACIBSP : AArch64::PACIASP))
276 if (!EmitCFI && NeedsWinCFI) {
278 "Windows SEH PAC unwind info only supports B-key signing");
284void AArch64PointerAuthImpl::authenticateLR(
286 const AArch64FunctionInfo *MFnI = MF.
getInfo<AArch64FunctionInfo>();
301 auto &AFL = *
static_cast<const AArch64FrameLowering *
>(
303 int64_t ArgumentStackToRestore = AFL.getArgumentStackToRestore(MF,
MBB);
306 bool EntrySPInX16 =
MBBI->getOpcode() == AArch64::PAUTH_EPILOGUE_ENTRY_SP;
307 assert((!EntrySPInX16 || ArgumentStackToRestore != 0) &&
308 "Early authentication is only for epilogues that change SP");
320 bool TerminatorIsCombinable = std::next(
MBBI) == TI && TI !=
MBB.
end() &&
321 TI->getOpcode() == AArch64::RET &&
322 ArgumentStackToRestore == 0 &&
325 if (
Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&
328 assert(PACSym &&
"No PAC instruction to refer to");
330 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))
341 BuildMI(
MBB, TI,
DL,
TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))
351 if (ArgumentStackToRestore == 0) {
353 assert(PACSym &&
"No PAC instruction to refer to");
355 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))
368 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))
375 "Windows SEH PAC unwind info only supports B-key signing");
390 if (NeedsWinCFI && !EntrySPInX16)
392 "argument size in a function that signs its return "
393 "address on Windows");
424 assert(ViaX17 &&
"Expected to authenticate via X17");
428 assert(PACSym &&
"No PAC instruction to refer to");
431 unsigned AutOpc = UseBKey ? AArch64::AUTIB171615 : AArch64::AUTIA171615;
438 assert(ViaX17 &&
"Expected to authenticate via X17");
442 assert(PACSym &&
"No PAC instruction to refer to");
453 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
460 assert(!ViaX17 &&
"Expected to authenticate using LR directly");
468 assert(ViaX17 &&
"Expected to authenticate via X17");
472 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
480 if (NeedsWinCFI && !EntrySPInX16) {
482 "Windows SEH PAC unwind info only supports B-key signing");
511 for (
auto &
MBB : MF) {
512 for (
auto &
MI :
MBB) {
513 switch (
MI.getOpcode()) {
516 case AArch64::PAUTH_PROLOGUE:
517 case AArch64::PAUTH_EPILOGUE:
518 case AArch64::PAUTH_EPILOGUE_ENTRY_SP:
525 for (
auto It : PAuthPseudoInstrs) {
526 switch (It->getOpcode()) {
527 case AArch64::PAUTH_PROLOGUE:
530 case AArch64::PAUTH_EPILOGUE:
531 case AArch64::PAUTH_EPILOGUE_ENTRY_SP:
532 authenticateLR(MF, It);
537 It->eraseFromParent();
541 Modified |= emitSignReturnAddressHardening(MF);
546bool AArch64PointerAuthImpl::emitSignReturnAddressHardening(
548 const auto *FI = MF.
getInfo<AArch64FunctionInfo>();
549 assert(FI &&
"FI can't be null");
550 if (!FI->shouldSignReturnAddress(MF) || !FI->shouldHardenSignReturnAddress())
556 for (MachineBasicBlock &
MBB : MF) {
559 if (RetInstIter ==
MBB.
end() || RetInstIter->getOpcode() != AArch64::RET)
562 assert(RetInstIter->getOperand(0).getReg() == AArch64::LR &&
563 "Return instruction must be returning via LR");
570 const bool NeedsWinCFI = MF.hasWinCFI();
573 assert(InsertionPoint->getOpcode() == AArch64::SEH_EpilogEnd);
576 const auto EmitSEHNopIfRequired = [&]() {
582 RS.enterBasicBlockEnd(
MBB);
584 AArch64::GPR64RegClass, InsertionPoint,
586 if (XReg == AArch64::NoRegister) {
588 MF.getContext().reportWarning(
589 SMLoc(),
"harden-pac-ret failed for function " + MF.getName());
598 EmitSEHNopIfRequired();
608 EmitSEHNopIfRequired();
615 MachinePointerInfo(),
619 EmitSEHNopIfRequired();
624 .buildRegister(AArch64::LR, XReg);
627 EmitSEHNopIfRequired();
628 BuildMI(
MBB, InsertionPoint,
DL,
TII->get(AArch64::LDRWui), AArch64::W30)
632 MachinePointerInfo(),
636 EmitSEHNopIfRequired();
648bool AArch64PointerAuthLegacy::runOnMachineFunction(
MachineFunction &MF) {
649 return AArch64PointerAuthImpl().run(MF);
655 const bool Changed = AArch64PointerAuthImpl().run(MF);
const AArch64Subtarget & Subtarget
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
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.
constexpr bool authenticatesLRViaX17(bool BranchProtectionPAuthLR, bool HasPAuth)
Controls whether, for PAUTH_EPILOGUE_ENTRY_SP, we use the hint-space AUTI[AB]1716 instruction to auth...
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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)