@@ -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
@@ -1891,6 +1894,30 @@ void AArch64AsmPrinter::emitPtrauthCheckAuthenticatedValue(
18911894 OutStreamer->emitLabel (SuccessSym);
18921895}
18931896
1897+ // With Pointer Authentication, it may be needed to explicitly check the
1898+ // authenticated value in LR before performing a tail call.
1899+ // Otherwise, the callee may re-sign the invalid return address,
1900+ // introducing a signing oracle.
1901+ void AArch64AsmPrinter::emitPtrauthTailCallHardening (const MachineInstr *TC) {
1902+ if (!AArch64FI->shouldSignReturnAddress (*MF))
1903+ return ;
1904+
1905+ auto LRCheckMethod = STI->getAuthenticatedLRCheckMethod (*MF);
1906+ if (LRCheckMethod == AArch64PAuth::AuthCheckMethod::None)
1907+ return ;
1908+
1909+ const AArch64RegisterInfo *TRI = STI->getRegisterInfo ();
1910+ Register ScratchReg =
1911+ TC->readsRegister (AArch64::X16, TRI) ? AArch64::X17 : AArch64::X16;
1912+ assert (!TC->readsRegister (ScratchReg, TRI) &&
1913+ " Neither x16 nor x17 is available as a scratch register" );
1914+ AArch64PACKey::ID Key =
1915+ AArch64FI->shouldSignWithBKey () ? AArch64PACKey::IB : AArch64PACKey::IA;
1916+ emitPtrauthCheckAuthenticatedValue (
1917+ AArch64::LR, ScratchReg, Key, LRCheckMethod,
1918+ /* ShouldTrap=*/ true , /* OnFailure=*/ nullptr );
1919+ }
1920+
18941921void AArch64AsmPrinter::emitPtrauthAuthResign (const MachineInstr *MI) {
18951922 const bool IsAUTPAC = MI->getOpcode () == AArch64::AUTPAC;
18961923
@@ -2443,27 +2470,6 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
24432470 OutStreamer->emitLabel (LOHLabel);
24442471 }
24452472
2446- // With Pointer Authentication, it may be needed to explicitly check the
2447- // authenticated value in LR when performing a tail call.
2448- // Otherwise, the callee may re-sign the invalid return address,
2449- // introducing a signing oracle.
2450- auto CheckLRInTailCall = [this ](Register CallDestinationReg) {
2451- if (!AArch64FI->shouldSignReturnAddress (*MF))
2452- return ;
2453-
2454- auto LRCheckMethod = STI->getAuthenticatedLRCheckMethod (*MF);
2455- if (LRCheckMethod == AArch64PAuth::AuthCheckMethod::None)
2456- return ;
2457-
2458- Register ScratchReg =
2459- CallDestinationReg == AArch64::X16 ? AArch64::X17 : AArch64::X16;
2460- AArch64PACKey::ID Key =
2461- AArch64FI->shouldSignWithBKey () ? AArch64PACKey::IB : AArch64PACKey::IA;
2462- emitPtrauthCheckAuthenticatedValue (
2463- AArch64::LR, ScratchReg, Key, LRCheckMethod,
2464- /* ShouldTrap=*/ true , /* OnFailure=*/ nullptr );
2465- };
2466-
24672473 AArch64TargetStreamer *TS =
24682474 static_cast <AArch64TargetStreamer *>(OutStreamer->getTargetStreamer ());
24692475 // Do any manual lowerings.
@@ -2614,7 +2620,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
26142620 ? AArch64::X17
26152621 : AArch64::X16;
26162622
2617- CheckLRInTailCall (MI-> getOperand ( 0 ). getReg () );
2623+ emitPtrauthTailCallHardening (MI);
26182624
26192625 unsigned DiscReg = AddrDisc;
26202626 if (Disc) {
@@ -2646,7 +2652,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
26462652 case AArch64::TCRETURNrix17:
26472653 case AArch64::TCRETURNrinotx16:
26482654 case AArch64::TCRETURNriALL: {
2649- CheckLRInTailCall (MI-> getOperand ( 0 ). getReg () );
2655+ emitPtrauthTailCallHardening (MI);
26502656
26512657 MCInst TmpInst;
26522658 TmpInst.setOpcode (AArch64::BR);
@@ -2655,7 +2661,7 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
26552661 return ;
26562662 }
26572663 case AArch64::TCRETURNdi: {
2658- CheckLRInTailCall (AArch64::NoRegister );
2664+ emitPtrauthTailCallHardening (MI );
26592665
26602666 MCOperand Dest;
26612667 MCInstLowering.lowerOperand (MI->getOperand (0 ), Dest);
0 commit comments