LLVM 24.0.0git
AArch64PointerAuth.cpp
Go to the documentation of this file.
1//===-- AArch64PointerAuth.cpp -- Harden code using PAuth ------------------==//
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
10
11#include "AArch64.h"
13#include "AArch64InstrInfo.h"
15#include "AArch64Subtarget.h"
20
21using namespace llvm;
22using namespace llvm::AArch64PAuth;
23
24#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"
25
26namespace {
27
28class AArch64PointerAuthImpl {
29public:
30 bool run(MachineFunction &MF);
31
32private:
33 const AArch64Subtarget *Subtarget = nullptr;
34 const AArch64InstrInfo *TII = nullptr;
35
36 void signLR(MachineFunction &MF, MachineBasicBlock::iterator MBBI) const;
37
38 void authenticateLR(MachineFunction &MF,
40};
41
42class AArch64PointerAuthLegacy : public MachineFunctionPass {
43public:
44 static char ID;
45
46 AArch64PointerAuthLegacy() : MachineFunctionPass(ID) {}
47
48 bool runOnMachineFunction(MachineFunction &MF) override;
49
50 StringRef getPassName() const override { return AARCH64_POINTER_AUTH_NAME; }
51};
52
53} // end anonymous namespace
54
55INITIALIZE_PASS(AArch64PointerAuthLegacy, "aarch64-ptrauth",
56 AARCH64_POINTER_AUTH_NAME, false, false)
57
59 return new AArch64PointerAuthLegacy();
60}
61
62char AArch64PointerAuthLegacy::ID = 0;
63
78
79// Wrap a given PAC instruction in CFI that describes it.
80// Depending on the type of CFI required, we may need to emit the directive
81// either before or after the instruction, so that unwinders can correctly
82// interpret the location of the signing instruction.
83template <typename BuildPACMIFn>
86 BuildPACMIFn BuildPACMI) {
87 if (!EmitCFI) {
88 BuildPACMI();
89 return;
90 }
91
92 auto &MF = *MBB.getParent();
93 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
94
96 if (MFnI.branchProtectionPAuthLR()) {
97 CFIBuilder.buildNegateRAStateWithPC();
98 BuildPACMI();
99 } else {
100 BuildPACMI();
101 if (!MF.getTarget().getTargetTriple().isOSBinFormatMachO()) {
102 CFIBuilder.buildNegateRAState();
103 }
104 }
105}
106
108 bool EmitCFI) {
109 if (!EmitCFI)
110 return;
111
112 auto &MF = *MBB.getParent();
113 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
115 const Triple &TT = MF.getTarget().getTargetTriple();
116
117 if (MFnI.branchProtectionPAuthLR()) {
118 // DW_CFA_AARCH64_negate_ra_state_with_pc is semantically broken for
119 // functions where shrinkwrapping places signing/authenticating pairs on
120 // distinct CFG paths.
121 //
122 // DWARF CFI is evaluated linearly over the byte stream, not along control
123 // flow edges. The toggle semantics of this directive therefore cannot
124 // faithfully represent the signed/unsigned RA state for all possible CFG
125 // paths. The added complexity versus DW_CFA_AARCH64_negate_ra_state is
126 // that an unwinder must also reconstruct the PC of the PACI[AB]SPPC in
127 // order to verify the signed LR, and that address is derived from the
128 // location of this directive in the linear CFI stream.
129 //
130 // The correct fix is to use DW_CFA_AARCH64_set_ra_state_with_pc, which
131 // sets the RA state and signing address absolutely rather than toggling
132 // them. An unwinder that supports this directive can reconstruct the
133 // correct state on any CFG path, regardless of how many
134 // signing/authenticating pairs exist in the function. However, not all
135 // unwinders support this directive, so we cannot rely on it exclusively.
136 //
137 // For unwinders that only support DW_CFA_AARCH64_negate_ra_state_with_pc,
138 // libunwind exploits a loophole: it records the address at the
139 // DW_CFA_AARCH64_negate_ra_state_with_pc site to authenticate the LR, but
140 // does not care that the CFI state remains "signed with pc" after
141 // authentication has occurred. This means we can safely omit the
142 // FrameDestroy emission of this directive, treating it solely as a marker
143 // for the signing site, as long as each function has at most one such
144 // signing location. That invariant holds today because shrinkwrapping
145 // does not yet hoist or sink PAuth_LR frame code across CFG join/split
146 // points; once it does, we must avoid those transformations on platforms
147 // that have this limitation.
148 //
149 // https://github.com/ARM-software/abi-aa/issues/327
150 // https://github.com/ARM-software/abi-aa/pull/346
151 } else if (!TT.isOSBinFormatMachO()) {
152 CFIBuilder.buildNegateRAState();
153 }
154}
155
156void AArch64PointerAuthImpl::signLR(MachineFunction &MF,
158 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
159 bool UseBKey = MFnI.shouldSignWithBKey();
160 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);
161 bool NeedsWinCFI = MF.hasWinCFI();
162
163 MachineBasicBlock &MBB = *MBBI->getParent();
164
165 // Debug location must be unknown, see AArch64FrameLowering::emitPrologue.
166 DebugLoc DL;
167
168 if (UseBKey && !MF.getTarget().getTargetTriple().isOSBinFormatMachO()) {
169 BuildMI(MBB, MBBI, DL, TII->get(AArch64::EMITBKEY))
171 }
172
173 // PAuthLR authentication instructions need to know the value of PC at the
174 // point of signing (PACI*).
175 if (MFnI.branchProtectionPAuthLR()) {
176 MCSymbol *PACSym = MF.getContext().createTempSymbol();
177 MFnI.setSigningInstrLabel(PACSym);
178 }
179
180 // No SEH opcode for this one; it doesn't materialize into an
181 // instruction on Windows.
182 if (MFnI.branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
183 decoratePACWithCFI(MBB, MBBI, EmitCFI, [&]() {
184 BuildMI(MBB, MBBI, DL,
185 TII->get(UseBKey ? AArch64::PACIBSPPC : AArch64::PACIASPPC))
187 ->setPreInstrSymbol(MF, MFnI.getSigningInstrLabel());
188 });
189 } else {
190 if (MFnI.branchProtectionPAuthLR()) {
191 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
193 }
194 decoratePACWithCFI(MBB, MBBI, EmitCFI, [&]() {
195 BuildMI(MBB, MBBI, DL,
196 TII->get(UseBKey ? AArch64::PACIBSP : AArch64::PACIASP))
198 ->setPreInstrSymbol(MF, MFnI.getSigningInstrLabel());
199 });
200 }
201
202 if (!EmitCFI && NeedsWinCFI) {
203 assert(UseBKey &&
204 "Windows SEH PAC unwind info only supports B-key signing");
205 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PACSignLR))
207 }
208}
209
210void AArch64PointerAuthImpl::authenticateLR(
211 MachineFunction &MF, MachineBasicBlock::iterator MBBI) const {
212 const AArch64FunctionInfo *MFnI = MF.getInfo<AArch64FunctionInfo>();
213 bool UseBKey = MFnI->shouldSignWithBKey();
214 bool EmitAsyncCFI = MFnI->needsAsyncDwarfUnwindInfo(MF);
215 bool NeedsWinCFI = MF.hasWinCFI();
216
217 MachineBasicBlock &MBB = *MBBI->getParent();
218 DebugLoc DL = MBBI->getDebugLoc();
219 // MBBI points to a PAUTH_EPILOGUE instruction to be replaced and
220 // TI points to a terminator instruction that may or may not be combined.
221 // Note that inserting new instructions "before MBBI" and "before TI" is
222 // not the same because if ShadowCallStack is enabled, its instructions
223 // are placed between MBBI and TI.
225
226 // The AUTIASP instruction assembles to a hint instruction before v8.3a so
227 // this instruction can safely used for any v8a architecture.
228 // From v8.3a onwards there are optimised authenticate LR and return
229 // instructions, namely RETA{A,B}, that can be used instead. In this case the
230 // DW_CFA_AARCH64_negate_ra_state can't be emitted.
231 bool TerminatorIsCombinable =
232 TI != MBB.end() && TI->getOpcode() == AArch64::RET;
233 MCSymbol *PACSym = MFnI->getSigningInstrLabel();
234
235 if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&
236 !MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) {
237 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
238 assert(PACSym && "No PAC instruction to refer to");
239 BuildMI(MBB, TI, DL,
240 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))
241 .addSym(PACSym)
244 } else {
245 if (MFnI->branchProtectionPAuthLR()) {
247 AArch64::X16);
248 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
250 }
251 BuildMI(MBB, TI, DL, TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))
254 }
255 MBB.erase(TI);
256 return;
257 }
258
259 auto &AFL = *static_cast<const AArch64FrameLowering *>(
260 MF.getSubtarget().getFrameLowering());
261 int64_t ArgumentStackToRestore = AFL.getArgumentStackToRestore(MF, MBB);
262
263 // When ArgumentStackToRestore < 0, the tail callee pops more argument space
264 // than this function received, so after the frame teardown SP is below the
265 // entry SP used as the signing modifier. Reconstruct entry SP in x16 and
266 // authenticate using AUTI[AB]1716 (x17=LR, x16=entry_SP).
267 if (ArgumentStackToRestore < 0) {
268 emitFrameOffset(MBB, MBBI, DL, AArch64::X16, AArch64::SP,
269 StackOffset::getFixed(-ArgumentStackToRestore), TII,
271
272 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::X17)
273 .addReg(AArch64::XZR)
274 .addReg(AArch64::LR)
275 .addImm(0)
277
278 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
279 assert(PACSym && "No PAC instruction to refer to");
281 AArch64::X15);
282
283 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
284 unsigned AutOpc = UseBKey ? AArch64::AUTIB171615 : AArch64::AUTIA171615;
285 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
287 } else if (MFnI->branchProtectionPAuthLR()) {
288 assert(PACSym && "No PAC instruction to refer to");
290 AArch64::X15);
291
292 // The PACM hint-space instruction modifies the following AUTI[AB]1716
293 // to optionally take x15 as an extra operand depending on the
294 // presence of +pauth-lr at runtime. On machines without +pauth-lr, it
295 // behaves as a nop, and the address of the PACI[AB]SP in x15 is
296 // ignored.
297 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
299
300 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
301 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
302 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
304 } else {
305 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
306 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
308 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
309 }
310
311 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), AArch64::LR)
312 .addReg(AArch64::XZR)
313 .addReg(AArch64::X17)
314 .addImm(0)
316 return;
317 }
318
319 // When ArgumentStackToRestore > 0, this function received more argument
320 // space than the tail callee pops. The epilogue contains an SP adjustment
321 // (e.g. "add sp, sp, #N") to discard the leftover argument space. We must
322 // authenticate *before* that adjustment so that AUTI[AB]SP sees the entry
323 // SP discriminator. Move any such SP-adjusting instructions to after the
324 // authentication instruction.
325 //
326 // We cannot simply bump SP first and then use AUTI[AB]SP with the bumped
327 // value, because the live arguments would fall below SP and potentially
328 // outside the red-zone.
329 SmallVector<MachineInstr *, 2> SPMods;
330 if (ArgumentStackToRestore > 0) {
331 for (auto I = MBBI; I->getFlag(MachineInstr::FrameDestroy); --I) {
332 if ((I->getOpcode() == AArch64::ADDXri ||
333 I->getOpcode() == AArch64::SUBXri) &&
334 I->getOperand(0).getReg() == AArch64::SP &&
335 I->getOperand(1).getReg() == AArch64::SP)
336 SPMods.push_back(&*I);
337 }
338 }
339 for (auto *MI : SPMods)
340 MI->removeFromParent();
341
342 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
343 assert(PACSym && "No PAC instruction to refer to");
344 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
345 BuildMI(MBB, MBBI, DL,
346 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))
347 .addSym(PACSym)
349 } else {
350 if (MFnI->branchProtectionPAuthLR()) {
352 AArch64::X16);
353
354 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
356 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
357 }
358 BuildMI(MBB, MBBI, DL,
359 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))
361 if (!MFnI->branchProtectionPAuthLR())
362 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
363 }
364
365 if (NeedsWinCFI) {
366 assert(UseBKey &&
367 "Windows SEH PAC unwind info only supports B-key signing");
368 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PACSignLR))
370 }
371
372 for (auto *MI : SPMods)
373 MBB.insert(MBBI, MI);
374}
375
377 switch (Method) {
379 return 0;
381 return 4;
383 return 12;
386 return 20;
387 }
388 llvm_unreachable("Unknown AuthCheckMethod enum");
389}
390
391bool AArch64PointerAuthImpl::run(MachineFunction &MF) {
392 Subtarget = &MF.getSubtarget<AArch64Subtarget>();
393 TII = Subtarget->getInstrInfo();
394
396
397 bool Modified = false;
398
399 for (auto &MBB : MF) {
400 for (auto &MI : MBB) {
401 switch (MI.getOpcode()) {
402 default:
403 break;
404 case AArch64::PAUTH_PROLOGUE:
405 case AArch64::PAUTH_EPILOGUE:
406 PAuthPseudoInstrs.push_back(MI.getIterator());
407 break;
408 }
409 }
410 }
411
412 for (auto It : PAuthPseudoInstrs) {
413 switch (It->getOpcode()) {
414 case AArch64::PAUTH_PROLOGUE:
415 signLR(MF, It);
416 break;
417 case AArch64::PAUTH_EPILOGUE:
418 authenticateLR(MF, It);
419 break;
420 default:
421 llvm_unreachable("Unhandled opcode");
422 }
423 It->eraseFromParent();
424 Modified = true;
425 }
426
427 return Modified;
428}
429
430bool AArch64PointerAuthLegacy::runOnMachineFunction(MachineFunction &MF) {
431 return AArch64PointerAuthImpl().run(MF);
432}
433
434PreservedAnalyses
437 const bool Changed = AArch64PointerAuthImpl().run(MF);
438 if (!Changed)
439 return PreservedAnalyses::all();
442 return PA;
443}
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 & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
bool needsAsyncDwarfUnwindInfo(const MachineFunction &MF) const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
const AArch64InstrInfo * getInstrInfo() const override
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Helper class for creating CFI instructions and inserting them into MIR.
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:723
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 ...
Definition MCSymbol.h:42
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.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
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.
Definition TypeSize.h:46
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
const Triple & getTargetTriple() const
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition Triple.h:872
Changed
#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.
Definition CallingConv.h:24
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.