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 "AArch64RegisterInfo.h"
16#include "AArch64Subtarget.h"
23#include "llvm/IR/Module.h"
24
25using namespace llvm;
26using namespace llvm::AArch64PAuth;
27
28#define AARCH64_POINTER_AUTH_NAME "AArch64 Pointer Authentication"
29
30namespace {
31
32class AArch64PointerAuthImpl {
33public:
34 bool run(MachineFunction &MF);
35
36private:
37 const AArch64Subtarget *Subtarget = nullptr;
38 const AArch64InstrInfo *TII = nullptr;
39
40 void signLR(MachineFunction &MF, MachineBasicBlock::iterator MBBI) const;
41
42 void authenticateLR(MachineFunction &MF,
44
45 bool emitSignReturnAddressHardening(MachineFunction &MF);
46};
47
48class AArch64PointerAuthLegacy : public MachineFunctionPass {
49public:
50 static char ID;
51
52 AArch64PointerAuthLegacy() : MachineFunctionPass(ID) {}
53
54 bool runOnMachineFunction(MachineFunction &MF) override;
55
56 StringRef getPassName() const override { return AARCH64_POINTER_AUTH_NAME; }
57};
58
59} // end anonymous namespace
60
61INITIALIZE_PASS(AArch64PointerAuthLegacy, "aarch64-ptrauth",
62 AARCH64_POINTER_AUTH_NAME, false, false)
63
65 return new AArch64PointerAuthLegacy();
66}
67
68char AArch64PointerAuthLegacy::ID = 0;
69
84
85// Wrap a given PAC instruction in CFI that describes it.
86//
87// Depending on the type of CFI required, we may need to emit the directive
88// either before or after the instruction, so that unwinders can correctly
89// interpret the location of the signing instruction.
90//
91// As a general rule, CFI opcodes describe the actions needed to recover the
92// register state leading up to a not-yet-retired instruction, with one
93// exception: .cfi_negate_ra_state_with_pc always comes before the paci[ab]sppc,
94// since the unwinder uses the location of the CFI itself to derive the address
95// of the signing instruction [1].
96// 1: https://github.com/llvm/llvm-project/pull/137795#issuecomment-2838779129
97template <typename BuildPACMIFn>
100 BuildPACMIFn BuildPACMI) {
101 if (!EmitCFI) {
102 BuildPACMI();
103 return;
104 }
105
106 auto &MF = *MBB.getParent();
107 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
109 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
110 SetRAStateMode Mode = MF.getSubtarget<AArch64Subtarget>()
111 .getCLOpts()
112 .cfi_llvm_set_ra_sign_state;
113
114 if (MFnI.branchProtectionPAuthLR()) {
115 switch (Mode) {
117 CFIBuilder.buildNegateRAStateWithPC();
118 BuildPACMI();
119 break;
122 BuildPACMI();
123 MCSymbol *PACSym = MFnI.getSigningInstrLabel();
124 assert(PACSym && "No PAC instruction to refer to");
125 CFIBuilder.buildSetRAState(2, PACSym);
126 break;
127 }
128 }
129 } else {
130 switch (Mode) {
133 BuildPACMI();
134 if (!TT.isOSBinFormatMachO()) {
135 CFIBuilder.buildNegateRAState();
136 }
137 break;
139 BuildPACMI();
140 CFIBuilder.buildSetRAState(1, nullptr);
141 break;
142 }
143 }
144}
145
147 bool EmitCFI) {
148 if (!EmitCFI)
149 return;
150
151 auto &MF = *MBB.getParent();
152 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
154 const Triple &TT = MF.getFunction().getParent()->getTargetTriple();
155 SetRAStateMode Mode = MF.getSubtarget<AArch64Subtarget>()
156 .getCLOpts()
157 .cfi_llvm_set_ra_sign_state;
158
159 if (MFnI.branchProtectionPAuthLR()) {
160 switch (Mode) {
162 // DW_CFA_AARCH64_negate_ra_state_with_pc is semantically broken for
163 // functions where shrinkwrapping places signing/authenticating pairs on
164 // distinct CFG paths.
165 //
166 // DWARF CFI is evaluated linearly over the byte stream, not along control
167 // flow edges. The toggle semantics of this directive therefore cannot
168 // faithfully represent the signed/unsigned RA state for all possible CFG
169 // paths. The added complexity versus DW_CFA_AARCH64_negate_ra_state is
170 // that an unwinder must also reconstruct the PC of the PACI[AB]SPPC in
171 // order to verify the signed LR, and that address is derived from the
172 // location of this directive in the linear CFI stream.
173 //
174 // The correct fix is to use DW_CFA_AARCH64_set_ra_state_with_pc, which
175 // sets the RA state and signing address absolutely rather than toggling
176 // them. An unwinder that supports this directive can reconstruct the
177 // correct state on any CFG path, regardless of how many
178 // signing/authenticating pairs exist in the function. However, not all
179 // unwinders support this directive, so we cannot rely on it exclusively.
180 //
181 // For unwinders that only support DW_CFA_AARCH64_negate_ra_state_with_pc,
182 // libunwind exploits a loophole: it records the address at the
183 // DW_CFA_AARCH64_negate_ra_state_with_pc site to authenticate the LR, but
184 // does not care that the CFI state remains "signed with pc" after
185 // authentication has occurred. This means we can safely omit the
186 // FrameDestroy emission of this directive, treating it solely as a marker
187 // for the signing site, as long as each function has at most one such
188 // signing location. That invariant holds today because shrinkwrapping
189 // does not yet hoist or sink PAuth_LR frame code across CFG join/split
190 // points; once it does, we must avoid those transformations on platforms
191 // that have this limitation.
192 //
193 // https://github.com/ARM-software/abi-aa/issues/327
194 // https://github.com/ARM-software/abi-aa/pull/346
195 break;
198 CFIBuilder.buildSetRAState(0, nullptr);
199 break;
200 }
201 } else if (!TT.isOSBinFormatMachO()) {
202 switch (Mode) {
205 CFIBuilder.buildNegateRAState();
206 break;
208 CFIBuilder.buildSetRAState(0, nullptr);
209 break;
210 }
211 }
212}
213
216 DebugLoc DL,
217 const AArch64InstrInfo *TII,
218 Register Dst, Register Src) {
219 assert(&MBB == MBBI->getParent());
220 BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), Dst)
221 .addReg(AArch64::XZR)
222 .addReg(Src)
223 .addImm(0)
225}
226
227void AArch64PointerAuthImpl::signLR(MachineFunction &MF,
229 auto &MFnI = *MF.getInfo<AArch64FunctionInfo>();
230 bool UseBKey = MFnI.shouldSignWithBKey();
231 bool EmitCFI = MFnI.needsDwarfUnwindInfo(MF);
232 bool NeedsWinCFI = MF.hasWinCFI();
233
234 MachineBasicBlock &MBB = *MBBI->getParent();
235
236 // Debug location must be unknown, see AArch64FrameLowering::emitPrologue.
237 DebugLoc DL;
238
239 if (UseBKey && !MF.getTarget().getTargetTriple().isOSBinFormatMachO()) {
240 BuildMI(MBB, MBBI, DL, TII->get(AArch64::EMITBKEY))
242 }
243
244 // PAuthLR authentication instructions need to know the value of PC at the
245 // point of signing (PACI*).
246 if (MFnI.branchProtectionPAuthLR()) {
247 MCSymbol *PACSym = MF.getContext().createTempSymbol();
248 MFnI.setSigningInstrLabel(PACSym);
249 }
250
251 // No SEH opcode for this one; it doesn't materialize into an
252 // instruction on Windows.
253 if (MFnI.branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
255 BuildMI(MBB, MBBI, DL, TII->get(AArch64::EMITNONHINTED))
257
258 decoratePACWithCFI(MBB, MBBI, EmitCFI, [&]() {
259 BuildMI(MBB, MBBI, DL,
260 TII->get(UseBKey ? AArch64::PACIBSPPC : AArch64::PACIASPPC))
262 ->setPreInstrSymbol(MF, MFnI.getSigningInstrLabel());
263 });
264 } else {
265 if (MFnI.branchProtectionPAuthLR()) {
266 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
268 }
269 decoratePACWithCFI(MBB, MBBI, EmitCFI, [&]() {
270 BuildMI(MBB, MBBI, DL,
271 TII->get(UseBKey ? AArch64::PACIBSP : AArch64::PACIASP))
273 ->setPreInstrSymbol(MF, MFnI.getSigningInstrLabel());
274 });
275 }
276
277 if (!EmitCFI && NeedsWinCFI) {
278 assert(UseBKey &&
279 "Windows SEH PAC unwind info only supports B-key signing");
280 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PACSignLR))
282 }
283}
284
285void AArch64PointerAuthImpl::authenticateLR(
287 const AArch64FunctionInfo *MFnI = MF.getInfo<AArch64FunctionInfo>();
288 bool UseBKey = MFnI->shouldSignWithBKey();
289 bool EmitAsyncCFI = MFnI->needsAsyncDwarfUnwindInfo(MF);
290 bool NeedsWinCFI = MF.hasWinCFI();
291
292 MachineBasicBlock &MBB = *MBBI->getParent();
293 DebugLoc DL = MBBI->getDebugLoc();
294 // MBBI points to a PAUTH_EPILOGUE instruction to be replaced and
295 // TI points to a terminator instruction that may or may not be combined.
296 // Note that inserting new instructions "before MBBI" and "before TI" is
297 // not the same because if ShadowCallStack is enabled, its instructions
298 // are placed between MBBI and TI.
300
301 MCSymbol *PACSym = MFnI->getSigningInstrLabel();
302 auto &AFL = *static_cast<const AArch64FrameLowering *>(
303 MF.getSubtarget().getFrameLowering());
304 int64_t ArgumentStackToRestore = AFL.getArgumentStackToRestore(MF, MBB);
305
306 // The signing SP is already in X16 if we are before the epilogue proper.
307 bool EntrySPInX16 = MBBI->getOpcode() == AArch64::PAUTH_EPILOGUE_ENTRY_SP;
308 assert((!EntrySPInX16 || ArgumentStackToRestore != 0) &&
309 "Early authentication is only for epilogues that change SP");
310
311 // The AUTIASP instruction assembles to a hint instruction before v8.3a so
312 // this instruction can safely be used for any v8a architecture.
313 // From v8.3a onwards there are optimised authenticate LR and return
314 // instructions, namely RETA{A,B}, that can be used instead. In this case
315 // the DW_CFA_AARCH64_negate_ra_state can't be emitted. Additionally,
316 // RET{A,B} requires the SP to match its incoming value on entry to the
317 // function.
318 //
319 // If the PAC-RET hardening based on load from the return address is
320 // enabled, fallback to the use of AUTIASP/AUTIBSP and RET.
321 bool TerminatorIsCombinable = std::next(MBBI) == TI && TI != MBB.end() &&
322 TI->getOpcode() == AArch64::RET &&
323 ArgumentStackToRestore == 0 &&
325
326 if (Subtarget->hasPAuth() && TerminatorIsCombinable && !NeedsWinCFI &&
327 !MF.getFunction().hasFnAttribute(Attribute::ShadowCallStack)) {
328 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
329 assert(PACSym && "No PAC instruction to refer to");
330 BuildMI(MBB, TI, DL,
331 TII->get(UseBKey ? AArch64::RETABSPPCi : AArch64::RETAASPPCi))
332 .addSym(PACSym)
335 } else {
336 if (MFnI->branchProtectionPAuthLR()) {
338 AArch64::X16);
339 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
341 }
342 BuildMI(MBB, TI, DL, TII->get(UseBKey ? AArch64::RETAB : AArch64::RETAA))
345 }
346 MBB.erase(TI);
347 return;
348 }
349
350 // If PAUTH_EPILOGUE is at insertion point with a net zero offset on SP, we
351 // can use an AUT form with a hardcoded SP discriminator.
352 if (ArgumentStackToRestore == 0) {
353 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
354 assert(PACSym && "No PAC instruction to refer to");
355 BuildMI(MBB, MBBI, DL,
356 TII->get(UseBKey ? AArch64::AUTIBSPPCi : AArch64::AUTIASPPCi))
357 .addSym(PACSym)
359 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
360 } else {
361 if (MFnI->branchProtectionPAuthLR()) {
363 AArch64::X16);
364
365 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
367 }
368 BuildMI(MBB, MBBI, DL,
369 TII->get(UseBKey ? AArch64::AUTIBSP : AArch64::AUTIASP))
371 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
372 }
373
374 if (NeedsWinCFI) {
375 assert(UseBKey &&
376 "Windows SEH PAC unwind info only supports B-key signing");
377 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PACSignLR))
379 }
380
381 return;
382 }
383
384 // Windows unwind info describes the authentication as a single pac_sign_lr
385 // unwind code, which the unwinder applies with the SP it has at that point.
386 // That is the signing SP only if the epilogue leaves SP at its entry value,
387 // and a single code can't stand in for the multi-instruction sequence below.
388 // So there, the epilogue emitter authenticates before the epilogue starts,
389 // where the prologue's unwind codes still apply, and the epilogue's unwind
390 // codes don't mention pac_sign_lr.
391 if (NeedsWinCFI && !EntrySPInX16)
392 report_fatal_error("Can't handle a tail call that changes the stack "
393 "argument size in a function that signs its return "
394 "address on Windows");
395
396 // When ArgumentStackToRestore > 0, this function received more argument
397 // space than the tail callee pops. The epilogue contains an SP adjustment
398 // (e.g. "add sp, sp, #N") to discard the leftover argument space.
399 //
400 // When ArgumentStackToRestore < 0, the tail callee pops more argument space
401 // than this function received, so after the frame teardown, SP is below the
402 // entry SP used as the signing modifier.
403 //
404 // We cannot simply bump SP first and then use AUTI[AB]SP with the bumped
405 // value, because the live arguments would fall below SP and potentially
406 // outside the red-zone.
407 //
408 // At this point there is an offset to the incoming SP, and we can't use the
409 // aut variants that hard-code SP. Reconstruct entry SP in x16 and
410 // authenticate using AUTI[AB]1716 (x17=LR, x16=entry_SP).
411 if (!EntrySPInX16)
412 emitFrameOffset(MBB, MBBI, DL, AArch64::X16, AArch64::SP,
413 StackOffset::getFixed(-ArgumentStackToRestore), TII,
415
416 // At this point, if EntrySPInX16 is true, it means we're using
417 // PAUTH_EPILOGUE_ENTRY_SP, so the signed return address will already be in
418 // x17 if the sequence below works on that, as the epilogue emitter works out
419 // using AArch64PAuth::authenticatesLRViaX17, and in LR if it doesn't. Each
420 // of the sequences below asserts that it agrees.
421 [[maybe_unused]] const bool ViaX17 = AArch64PAuth::authenticatesLRViaX17(
422 MFnI->branchProtectionPAuthLR(), Subtarget->hasPAuth());
423
424 if (MFnI->branchProtectionPAuthLR() && Subtarget->hasPAuthLR()) {
425 assert(ViaX17 && "Expected to authenticate via X17");
426 if (!EntrySPInX16)
427 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::X17, AArch64::LR);
428
429 assert(PACSym && "No PAC instruction to refer to");
430 emitEpiloguePACSymOffsetIntoReg(*TII, MBB, MBBI, DL, PACSym, AArch64::X15);
431
432 unsigned AutOpc = UseBKey ? AArch64::AUTIB171615 : AArch64::AUTIA171615;
433 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
435 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
436
437 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::LR, AArch64::X17);
438 } else if (MFnI->branchProtectionPAuthLR()) {
439 assert(ViaX17 && "Expected to authenticate via X17");
440 if (!EntrySPInX16)
441 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::X17, AArch64::LR);
442
443 assert(PACSym && "No PAC instruction to refer to");
444 emitEpiloguePACSymOffsetIntoReg(*TII, MBB, MBBI, DL, PACSym, AArch64::X15);
445
446 // The PACM hint-space instruction modifies the following AUTI[AB]1716
447 // to optionally take x15 as an extra operand depending on the
448 // presence of +pauth-lr at runtime. On machines without +pauth-lr, it
449 // behaves as a nop, and the address of the PACI[AB]SP in x15 is
450 // ignored.
451 BuildMI(MBB, MBBI, DL, TII->get(AArch64::PACM))
453
454 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
455 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
457 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
458
459 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::LR, AArch64::X17);
460 } else if (Subtarget->hasPAuth()) {
461 assert(!ViaX17 && "Expected to authenticate using LR directly");
462 BuildMI(MBB, MBBI, DL, TII->get(UseBKey ? AArch64::AUTIB : AArch64::AUTIA),
463 AArch64::LR)
464 .addUse(AArch64::LR)
465 .addUse(AArch64::X16)
467 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
468 } else {
469 assert(ViaX17 && "Expected to authenticate via X17");
470 if (!EntrySPInX16)
471 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::X17, AArch64::LR);
472
473 unsigned AutOpc = UseBKey ? AArch64::AUTIB1716 : AArch64::AUTIA1716;
474 BuildMI(MBB, MBBI, DL, TII->get(AutOpc))
476 emitAUTCFI(MBB, MBBI, EmitAsyncCFI);
477
478 emitMOVWithFrameDestroy(MBB, MBBI, DL, TII, AArch64::LR, AArch64::X17);
479 }
480
481 if (NeedsWinCFI && !EntrySPInX16) {
482 assert(UseBKey &&
483 "Windows SEH PAC unwind info only supports B-key signing");
484 BuildMI(MBB, MBBI, DL, TII->get(AArch64::SEH_PACSignLR))
486 }
487}
488
490 switch (Method) {
492 return 0;
494 return 4;
496 return 12;
499 return 20;
500 }
501 llvm_unreachable("Unknown AuthCheckMethod enum");
502}
503
504bool AArch64PointerAuthImpl::run(MachineFunction &MF) {
505 Subtarget = &MF.getSubtarget<AArch64Subtarget>();
506 TII = Subtarget->getInstrInfo();
507
509
510 bool Modified = false;
511
512 for (auto &MBB : MF) {
513 for (auto &MI : MBB) {
514 switch (MI.getOpcode()) {
515 default:
516 break;
517 case AArch64::PAUTH_PROLOGUE:
518 case AArch64::PAUTH_EPILOGUE:
519 case AArch64::PAUTH_EPILOGUE_ENTRY_SP:
520 PAuthPseudoInstrs.push_back(MI.getIterator());
521 break;
522 }
523 }
524 }
525
526 for (auto It : PAuthPseudoInstrs) {
527 switch (It->getOpcode()) {
528 case AArch64::PAUTH_PROLOGUE:
529 signLR(MF, It);
530 break;
531 case AArch64::PAUTH_EPILOGUE:
532 case AArch64::PAUTH_EPILOGUE_ENTRY_SP:
533 authenticateLR(MF, It);
534 break;
535 default:
536 llvm_unreachable("Unhandled opcode");
537 }
538 It->eraseFromParent();
539 Modified = true;
540 }
541
542 Modified |= emitSignReturnAddressHardening(MF);
543
544 return Modified;
545}
546
547bool AArch64PointerAuthImpl::emitSignReturnAddressHardening(
548 MachineFunction &MF) {
549 const auto *FI = MF.getInfo<AArch64FunctionInfo>();
550 assert(FI && "FI can't be null");
551 if (!FI->shouldSignReturnAddress(MF) || !FI->shouldHardenSignReturnAddress())
552 return false;
553 assert(Subtarget && "Subtarget must be initialized");
554
555 RegScavenger RS;
556 bool Modified = false;
557 for (MachineBasicBlock &MBB : MF) {
559
560 if (RetInstIter == MBB.end() || RetInstIter->getOpcode() != AArch64::RET)
561 continue;
562
563 assert(RetInstIter->getOperand(0).getReg() == AArch64::LR &&
564 "Return instruction must be returning via LR");
565
566 MachineBasicBlock::iterator InsertionPoint = RetInstIter;
567 // In the case of Windows SEH, the hardening sequence does not immediately
568 // precede the return instruction. Instead, it precedes the SEH_EpilogEnd
569 // pseudo-instruction, which itself is expected to be the predecessor of
570 // the return. Plus, each instruction in the sequence needs one SEH_Nop.
571 const bool NeedsWinCFI = MF.hasWinCFI();
572 if (NeedsWinCFI) {
573 --InsertionPoint;
574 assert(InsertionPoint->getOpcode() == AArch64::SEH_EpilogEnd);
575 }
576 DebugLoc DL = InsertionPoint->getDebugLoc();
577 const auto EmitSEHNopIfRequired = [&]() {
578 if (NeedsWinCFI)
579 BuildMI(MBB, InsertionPoint, DL, TII->get(AArch64::SEH_Nop))
581 };
582
583 RS.enterBasicBlockEnd(MBB);
584 Register XReg = RS.scavengeRegisterBackwards(
585 AArch64::GPR64RegClass, InsertionPoint,
586 /*RestoreAfter=*/false, /*SPAdj=*/0, /*AllowSpill=*/false);
587 if (XReg == AArch64::NoRegister) {
588 // Couldn't find a free register to use for the hardening. Skip.
589 MF.getContext().reportWarning(
590 SMLoc(), "harden-pac-ret failed for function " + MF.getName());
591 continue;
592 }
593
594 // Register copies are done using ORRXrs directly instead of using the
595 // pseudo-instruction COPY because this function can be called after
596 // pseudo-instruction expansion takes place, for example via the machine
597 // outliner pass.
598 emitMOVWithFrameDestroy(MBB, InsertionPoint, DL, TII, XReg, AArch64::LR);
599 EmitSEHNopIfRequired();
600
601 // The XPACI instruction is only available with FEAT_PAUTH. So if the
602 // subtarget does not have it, the alternative XPACLRI instruction must be
603 // used instead. The latter is in hint space, therefore can be used even
604 // if FEAT_PAUTH is absent.
605 if (Subtarget->hasPAuth()) {
606 BuildMI(MBB, InsertionPoint, DL, TII->get(AArch64::XPACI), XReg)
607 .addUse(XReg)
609 EmitSEHNopIfRequired();
610 Register WReg =
611 Subtarget->getRegisterInfo()->getSubReg(XReg, AArch64::sub_32);
612 BuildMI(MBB, InsertionPoint, DL, TII->get(AArch64::LDRWui), WReg)
613 .addUse(XReg)
614 .addImm(0)
615 .addMemOperand(MF.getMachineMemOperand(
616 MachinePointerInfo(),
618 Align(4)))
620 EmitSEHNopIfRequired();
621 } else {
622 // Emit a CFI directive to tell unwinders that the return address is now
623 // saved in XReg.
624 CFIInstBuilder(MBB, InsertionPoint, MachineInstr::FrameDestroy)
625 .buildRegister(AArch64::LR, XReg);
626 BuildMI(MBB, InsertionPoint, DL, TII->get(AArch64::XPACLRI))
628 EmitSEHNopIfRequired();
629 BuildMI(MBB, InsertionPoint, DL, TII->get(AArch64::LDRWui), AArch64::W30)
630 .addUse(AArch64::LR)
631 .addImm(0)
632 .addMemOperand(MF.getMachineMemOperand(
633 MachinePointerInfo(),
635 Align(4)))
637 EmitSEHNopIfRequired();
638 BuildMI(MBB, RetInstIter, DL, TII->get(AArch64::RET))
639 .addUse(XReg)
640 .copyImplicitOps(*RetInstIter);
641 MBB.erase(RetInstIter);
642 }
643 Modified = true;
644 }
645
646 return Modified;
647}
648
649bool AArch64PointerAuthLegacy::runOnMachineFunction(MachineFunction &MF) {
650 return AArch64PointerAuthImpl().run(MF);
651}
652
653PreservedAnalyses
656 const bool Changed = AArch64PointerAuthImpl().run(MF);
657 if (!Changed)
658 return PreservedAnalyses::all();
661 return PA;
662}
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 & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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 needsAsyncDwarfUnwindInfo(const MachineFunction &MF) 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.
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:312
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
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 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.
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:47
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:48
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
Definition Triple.h:876
#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)
Definition Error.cpp:163