[llvm] [AArch64] Codegen for AArch64 Return Address Signing Hardening (PR #176187)

Victor Campos via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 3 08:29:26 PST 2026


================
@@ -276,5 +327,78 @@ bool AArch64PointerAuth::runOnMachineFunction(MachineFunction &MF) {
     Modified = true;
   }
 
+  Modified |= emitSignReturnAddressHardening(MF);
+
+  return Modified;
+}
+
+bool AArch64PointerAuth::emitSignReturnAddressHardening(MachineFunction &MF) {
+  const auto *FI = MF.getInfo<AArch64FunctionInfo>();
+  assert(FI && "FI can't be null");
+  if (!FI->shouldSignReturnAddress(MF) || !FI->shouldHardenSignReturnAddress())
+    return false;
+  assert(Subtarget && "Subtarget must be initialized");
+
+  bool Modified = false;
+  for (MachineBasicBlock &MBB : MF) {
+    if (!MBB.isReturnBlock())
+      continue;
+
+    MachineBasicBlock::iterator MBBI = MBB.getFirstTerminator();
+
+    if (MBBI == MBB.end() || MBBI->getOpcode() != AArch64::RET)
+      continue;
+
+    DebugLoc DL = MBBI->getDebugLoc();
+
+    Register XReg = RSHelper.findRegister(MBBI);
+
+    // Register copies are done using ORRXrs directly instead of using the
+    // pseudo-instruction COPY because this function can be called after
+    // pseudo-instruction expansion takes place, for example via the machine
+    // outliner pass.
+    BuildMI(MBB, MBBI, DL, TII->get(AArch64::ORRXrs), XReg)
+        .addUse(AArch64::XZR)
+        .addUse(AArch64::LR)
+        .addImm(0)
+        .setMIFlag(MachineInstr::FrameDestroy);
+
+    // The XPACI instruction is only available with FEAT_PAUTH. So if the
+    // subtarget does not have it, the alternative XPACLRI instruction must be
+    // used instead. The latter is in hint space, therefore can be present even
+    // if FEAT_PAUTH is absent.
----------------
vhscampos wrote:

With FPAC present in hardware, the mitigating code sequence could be simplified to just:
```
AUTIASP
XPACLRI
RET
```
However, some important caveats:
- The currently proposed code sequence (with a load of the return address) also works with FPAC, although it is not as compact.
- If the target does not have FPAC, the simplified code sequence defeats the authentication entirely: the XPAC instruction would render the auth useless in a normal execution (non-speculative).

https://github.com/llvm/llvm-project/pull/176187


More information about the llvm-commits mailing list