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