24#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"
45enum class SetRAStateMode {
51 "aarch64-cfi-llvm-set-ra-sign-state",
cl::init(SetRAStateMode::Never),
52 cl::desc(
"Control emission of .cfi_set_ra_state for PAC return address "
55 "Always use legacy .cfi_negate_ra_state[_with_pc]"),
56 clEnumValN(SetRAStateMode::PAuthLR,
"pauth-lr",
57 "Use new CFI only for PAuth_LR (default)"),
59 "Use new CFI for both PAuth and PAuth_LR")),
62class AArch64PointerAuthImpl {
93 return new AArch64PointerAuthLegacy();
96char AArch64PointerAuthLegacy::ID = 0;
125template <
typename BuildPACMIFn>
128 BuildPACMIFn BuildPACMI) {
134 auto &MF = *
MBB.getParent();
137 const Triple &TT = MF.getTarget().getTargetTriple();
139 if (MFnI.branchProtectionPAuthLR()) {
140 switch (CFILLVMSetRASignStateMode) {
141 case SetRAStateMode::Never:
142 CFIBuilder.buildNegateRAStateWithPC();
145 case SetRAStateMode::PAuthLR:
146 case SetRAStateMode::Always: {
148 MCSymbol *PACSym = MFnI.getSigningInstrLabel();
149 assert(PACSym &&
"No PAC instruction to refer to");
150 CFIBuilder.buildSetRAState(2, PACSym);
155 switch (CFILLVMSetRASignStateMode) {
156 case SetRAStateMode::Never:
157 case SetRAStateMode::PAuthLR:
159 if (!TT.isOSBinFormatMachO()) {
160 CFIBuilder.buildNegateRAState();
163 case SetRAStateMode::Always:
165 CFIBuilder.buildSetRAState(1,
nullptr);
176 auto &MF = *
MBB.getParent();
179 const Triple &TT = MF.getTarget().getTargetTriple();
181 if (MFnI.branchProtectionPAuthLR()) {
182 switch (CFILLVMSetRASignStateMode) {
183 case SetRAStateMode::Never:
218 case SetRAStateMode::PAuthLR:
219 case SetRAStateMode::Always:
220 CFIBuilder.buildSetRAState(0,
nullptr);
223 }
else if (!TT.isOSBinFormatMachO()) {
224 switch (CFILLVMSetRASignStateMode) {
225 case SetRAStateMode::Never:
226 case SetRAStateMode::PAuthLR:
227 CFIBuilder.buildNegateRAState();
229 case SetRAStateMode::Always:
230 CFIBuilder.buildSetRAState(0,
nullptr);
238 auto &MFnI = *MF.
getInfo<AArch64FunctionInfo>();
239 bool UseBKey = MFnI.shouldSignWithBKey();
240 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);
255 if (MFnI.branchProtectionPAuthLR()) {
257 MFnI.setSigningInstrLabel(PACSym);
262 if (MFnI.branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
265 TII->get(UseBKey ? AArch64::PACIBSPPC : AArch64::PACIASPPC))
270 if (MFnI.branchProtectionPAuthLR()) {
276 TII->get(UseBKey ? AArch64::PACIBSP : AArch64::PACIASP))
282 if (!EmitCFI && NeedsWinCFI) {
284 "Windows SEH PAC unwind info only supports B-key signing");
290void AArch64PointerAuthImpl::authenticateLR(
292 const AArch64FunctionInfo *MFnI = MF.
getInfo<AArch64FunctionInfo>();
311 bool TerminatorIsCombinable =
312 TI !=
MBB.
end() && TI->getOpcode() == AArch64::RET;
315 if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&
318 assert(PACSym &&
"No PAC instruction to refer to");
320 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))
331 BuildMI(
MBB, TI,
DL,
TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))
339 auto &AFL = *
static_cast<const AArch64FrameLowering *
>(
341 int64_t ArgumentStackToRestore = AFL.getArgumentStackToRestore(MF,
MBB);
347 if (ArgumentStackToRestore < 0) {
359 assert(PACSym &&
"No PAC instruction to refer to");
364 unsigned AutOpc = UseBKey ? AArch64::AUTIB171615 : AArch64::AUTIA171615;
368 assert(PACSym &&
"No PAC instruction to refer to");
381 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
385 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
409 SmallVector<MachineInstr *, 2> SPMods;
410 if (ArgumentStackToRestore > 0) {
412 if ((
I->getOpcode() == AArch64::ADDXri ||
413 I->getOpcode() == AArch64::SUBXri) &&
414 I->getOperand(0).getReg() == AArch64::SP &&
415 I->getOperand(1).getReg() == AArch64::SP)
419 for (
auto *
MI : SPMods)
420 MI->removeFromParent();
423 assert(PACSym &&
"No PAC instruction to refer to");
426 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))
439 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))
447 "Windows SEH PAC unwind info only supports B-key signing");
452 for (
auto *
MI : SPMods)
479 for (
auto &
MBB : MF) {
480 for (
auto &
MI :
MBB) {
481 switch (
MI.getOpcode()) {
484 case AArch64::PAUTH_PROLOGUE:
485 case AArch64::PAUTH_EPILOGUE:
492 for (
auto It : PAuthPseudoInstrs) {
493 switch (It->getOpcode()) {
494 case AArch64::PAUTH_PROLOGUE:
497 case AArch64::PAUTH_EPILOGUE:
498 authenticateLR(MF, It);
503 It->eraseFromParent();
510bool AArch64PointerAuthLegacy::runOnMachineFunction(MachineFunction &MF) {
511 return AArch64PointerAuthImpl().run(MF);
517 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 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
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
const HexagonInstrInfo * TII
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool branchProtectionPAuthLR() const
bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const
MCSymbol * getSigningInstrLabel() const
bool shouldSignWithBKey() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
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 instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
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 & 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.
LLVM_ABI void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol)
Set a symbol that will be emitted just prior to the instruction itself.
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:
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
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.
@ Never
Never set the bit.
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.
@ Always
Always emit .debug_str_offsets talbes as DWARF64 for testing.