Skip to content

Commit 9bc142a

Browse files
authored
[AArch64][PAC] Refactor aarch64-ptrauth pass (#70446)
Refactor Pointer Authentication pass in preparation for adding more PAUTH_* pseudo instructions: * dropped early return from runOnMachineFunction() as other PAUTH_* instructions need expansion even when pac-ret is disabled * refactored runOnMachineFunction() to first collect all the instructions of interest without modifying anything and then performing changes in the later loops. There are two types of relevant instructions: PAUTH_* pseudos that should definitely be replaced by this pass and tail call instructions that may require attention if pac-ret is enabled * made the loop iterating over all of the instructions handle instruction bundles by itself: even though this pass still does not support bundled TCRETURN* instructions (such as produced by KCFI) it does not crash anymore when no support is actually required
1 parent 6353787 commit 9bc142a

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

llvm/lib/Target/AArch64/AArch64PointerAuth.cpp

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -297,52 +297,67 @@ bool AArch64PointerAuth::checkAuthenticatedLR(
297297

298298
bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {
299299
const auto *MFnI = MF.getInfo<AArch64FunctionInfo>();
300-
if (!MFnI->shouldSignReturnAddress(true))
301-
return false;
302300

303301
Subtarget = &MF.getSubtarget<AArch64Subtarget>();
304302
TII = Subtarget->getInstrInfo();
305303
TRI = Subtarget->getRegisterInfo();
306304

307-
SmallVector<MachineBasicBlock::iterator> DeletedInstrs;
308-
SmallVector<MachineBasicBlock::iterator> TailCallInstrs;
305+
SmallVector<MachineBasicBlock::instr_iterator> PAuthPseudoInstrs;
306+
SmallVector<MachineBasicBlock::instr_iterator> TailCallInstrs;
309307

310308
bool Modified = false;
311309
bool HasAuthenticationInstrs = false;
312310

313311
for (auto &MBB : MF) {
314-
for (auto &MI : MBB) {
315-
auto It = MI.getIterator();
312+
// Using instr_iterator to catch unsupported bundled TCRETURN* instructions
313+
// instead of just skipping them.
314+
for (auto &MI : MBB.instrs()) {
316315
switch (MI.getOpcode()) {
317316
default:
317+
// Bundled TCRETURN* instructions (such as created by KCFI)
318+
// are not supported yet, but no support is required if no
319+
// PAUTH_EPILOGUE instructions exist in the same function.
320+
// Skip the BUNDLE instruction itself (actual bundled instructions
321+
// follow it in the instruction list).
322+
if (MI.isBundle())
323+
continue;
318324
if (AArch64InstrInfo::isTailCallReturnInst(MI))
319-
TailCallInstrs.push_back(It);
325+
TailCallInstrs.push_back(MI.getIterator());
320326
break;
321327
case AArch64::PAUTH_PROLOGUE:
322-
signLR(MF, It);
323-
DeletedInstrs.push_back(It);
324-
Modified = true;
325-
break;
326328
case AArch64::PAUTH_EPILOGUE:
327-
authenticateLR(MF, It);
328-
DeletedInstrs.push_back(It);
329-
Modified = true;
330-
HasAuthenticationInstrs = true;
329+
assert(!MI.isBundled());
330+
PAuthPseudoInstrs.push_back(MI.getIterator());
331331
break;
332332
}
333333
}
334334
}
335335

336+
for (auto It : PAuthPseudoInstrs) {
337+
switch (It->getOpcode()) {
338+
case AArch64::PAUTH_PROLOGUE:
339+
signLR(MF, It);
340+
break;
341+
case AArch64::PAUTH_EPILOGUE:
342+
authenticateLR(MF, It);
343+
HasAuthenticationInstrs = true;
344+
break;
345+
default:
346+
llvm_unreachable("Unhandled opcode");
347+
}
348+
It->eraseFromParent();
349+
Modified = true;
350+
}
351+
336352
// FIXME Do we need to emit any PAuth-related epilogue code at all
337353
// when SCS is enabled?
338354
if (HasAuthenticationInstrs &&
339355
!MFnI->needsShadowCallStackPrologueEpilogue(MF)) {
340-
for (auto TailCall : TailCallInstrs)
356+
for (auto TailCall : TailCallInstrs) {
357+
assert(!TailCall->isBundled() && "Not yet supported");
341358
Modified |= checkAuthenticatedLR(TailCall);
359+
}
342360
}
343361

344-
for (auto MI : DeletedInstrs)
345-
MI->eraseFromParent();
346-
347362
return Modified;
348363
}

0 commit comments

Comments
 (0)