@@ -157,6 +157,9 @@ class AArch64AsmPrinter : public AsmPrinter {
157157 bool ShouldTrap,
158158 const MCSymbol *OnFailure);
159159
160+ // Check authenticated LR before tail calling.
161+ void emitPtrauthTailCallHardening (const MachineInstr *TC);
162+
160163 // Emit the sequence for AUT or AUTPAC.
161164 void emitPtrauthAuthResign (const MachineInstr *MI);
162165
@@ -1870,6 +1873,30 @@ void AArch64AsmPrinter::emitPtrauthCheckAuthenticatedValue(
18701873 OutStreamer->emitLabel (SuccessSym);
18711874}
18721875
1876+ // With Pointer Authentication, it may be needed to explicitly check the
1877+ // authenticated value in LR before performing a tail call.
1878+ // Otherwise, the callee may re-sign the invalid return address,
1879+ // introducing a signing oracle.
1880+ void AArch64AsmPrinter::emitPtrauthTailCallHardening (const MachineInstr *TC) {
1881+ if (!AArch64FI->shouldSignReturnAddress (*MF))
1882+ return ;
1883+
1884+ auto LRCheckMethod = STI->getAuthenticatedLRCheckMethod (*MF);
1885+ if (LRCheckMethod == AArch64PAuth::AuthCheckMethod::None)
1886+ return ;
1887+
1888+ const AArch64RegisterInfo *TRI = STI->getRegisterInfo ();
1889+ Register ScratchReg =
1890+ TC->readsRegister (AArch64::X16, TRI) ? AArch64::X17 : AArch64::X16;
1891+ assert (!TC->readsRegister (ScratchReg, TRI) &&
1892+ " Neither x16 nor x17 is available as a scratch register" );
1893+ AArch64PACKey::ID Key =
1894+ AArch64FI->shouldSignWithBKey () ? AArch64PACKey::IB : AArch64PACKey::IA;
1895+ emitPtrauthCheckAuthenticatedValue (
1896+ AArch64::LR, ScratchReg, Key, LRCheckMethod,
1897+ /* ShouldTrap=*/ true , /* OnFailure=*/ nullptr );
1898+ }
1899+
18731900void AArch64AsmPrinter::emitPtrauthAuthResign (const MachineInstr *MI) {
18741901 const bool IsAUTPAC = MI->getOpcode () == AArch64::AUTPAC;
18751902
@@ -2312,27 +2339,6 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
23122339 OutStreamer->emitLabel (LOHLabel);
23132340 }
23142341
2315- // With Pointer Authentication, it may be needed to explicitly check the
2316- // authenticated value in LR when performing a tail call.
2317- // Otherwise, the callee may re-sign the invalid return address,
2318- // introducing a signing oracle.
2319- auto CheckLRInTailCall = [this ](Register CallDestinationReg) {
2320- if (!AArch64FI->shouldSignReturnAddress (*MF))
2321- return ;
2322-
2323- auto LRCheckMethod = STI->getAuthenticatedLRCheckMethod (*MF);
2324- if (LRCheckMethod == AArch64PAuth::AuthCheckMethod::None)
2325- return ;
2326-
2327- Register ScratchReg =
2328- CallDestinationReg == AArch64::X16 ? AArch64::X17 : AArch64::X16;
2329- AArch64PACKey::ID Key =
2330- AArch64FI->shouldSignWithBKey () ? AArch64PACKey::IB : AArch64PACKey::IA;
2331- emitPtrauthCheckAuthenticatedValue (
2332- AArch64::LR, ScratchReg, Key, LRCheckMethod,
2333- /* ShouldTrap=*/ true , /* OnFailure=*/ nullptr );
2334- };
2335-
23362342 AArch64TargetStreamer *TS =
23372343 static_cast <AArch64TargetStreamer *>(OutStreamer->getTargetStreamer ());
23382344 // Do any manual lowerings.
@@ -2479,7 +2485,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
24792485 ? AArch64::X17
24802486 : AArch64::X16;
24812487
2482- CheckLRInTailCall (MI-> getOperand ( 0 ). getReg () );
2488+ emitPtrauthTailCallHardening (MI);
24832489
24842490 unsigned DiscReg = AddrDisc;
24852491 if (Disc) {
@@ -2511,7 +2517,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
25112517 case AArch64::TCRETURNrix17:
25122518 case AArch64::TCRETURNrinotx16:
25132519 case AArch64::TCRETURNriALL: {
2514- CheckLRInTailCall (MI-> getOperand ( 0 ). getReg () );
2520+ emitPtrauthTailCallHardening (MI);
25152521
25162522 MCInst TmpInst;
25172523 TmpInst.setOpcode (AArch64::BR);
@@ -2520,7 +2526,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
25202526 return ;
25212527 }
25222528 case AArch64::TCRETURNdi: {
2523- CheckLRInTailCall (AArch64::NoRegister );
2529+ emitPtrauthTailCallHardening (MI );
25242530
25252531 MCOperand Dest;
25262532 MCInstLowering.lowerOperand (MI->getOperand (0 ), Dest);
0 commit comments