[AArch64] Add ptrauth-keys module flag and accessors. #243564
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-binary-utilities
@llvm/pr-subscribers-backend-aarch64
Author: Peter Smith (smithp35)
<details>
<summary>Changes</summary>
Express the ptrauth keys as process independent or process dependent to mirror the values in ptrauth.h. Introduce a module flag ptrauth-keys with format:
"ixdxiydy" where x and y can be a or b. In order the keys are:
global code AKA process independent code in Arm64e
global data AKA process independent data in Arm64e
local code AKA process dependent code in Arm64e
local data AKA process dependent data in Arm64e
All keys must be present.
Use utility functions to access these. I have used the AArch64TargetMachine following the example of getFloatABI in the ARM Backend, although the defaults are not currently Target Specific.
On MachO the accessor functions ignore the module flag, using the default keys.
The deactivation symbols irelative code path only support the DA key due to limitations of the PAC emulation library. However if given a different key it will fall back gracefully to another code path that can support DB.
Added tests based on existing implicit usage of keys in backend.
---
Patch is 70.59 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243564.diff
17 Files Affected:
````````````diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index f0078587d5abe..41829b0a8c76a 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -17,6 +17,7 @@
#include "AArch64MachineFunctionInfo.h"
#include "AArch64RegisterInfo.h"
#include "AArch64Subtarget.h"
+#include "AArch64TargetMachine.h"
#include "AArch64TargetObjectFile.h"
#include "MCTargetDesc/AArch64AddressingModes.h"
#include "MCTargetDesc/AArch64InstPrinter.h"
@@ -1089,11 +1090,12 @@ void AArch64AsmPrinter::emitEndOfAsmFile(Module &M) {
}
// With signed ELF GOT enabled, the linker looks at the symbol type to
- // choose between keys IA (for STT_FUNC) and DA (for other types). Symbols
- // for functions not defined in the module have STT_NOTYPE type by default.
- // This makes linker to emit signing schema with DA key (instead of IA) for
- // corresponding R_AARCH64_AUTH_GLOB_DAT dynamic reloc. To avoid that, force
- // all function symbols used in the module to have STT_FUNC type. See
+ // choose between global code keys (for STT_FUNC) and global data keys (for
+ // other types). Symbols for functions not defined in the module have
+ // STT_NOTYPE type by default. This makes the linker emit signing schema
+ // with global data key (instead of global code key) for corresponding
+ // R_AARCH64_AUTH_GLOB_DAT dynamic reloc. To avoid that, force all function
+ // symbols used in the module to have STT_FUNC type. See
// https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#default-signing-schema
const auto *PtrAuthELFGOTFlag = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("ptrauth-elf-got"));
@@ -1532,7 +1534,10 @@ void AArch64AsmPrinter::emitXXStructor(const DataLayout &DL,
IntegerType *Int64Ty = IntegerType::get(C, 64);
PointerType *PtrTy = PointerType::get(C, 0);
- ConstantInt *Key = ConstantInt::get(Int32Ty, AArch64PAuth::InitFiniKey);
+ const auto &ATM = static_cast<AArch64TargetMachine &>(TM);
+ AArch64PACKey::ID InitFiniKey =
+ ATM.getGlobalCodeKey(*MMI->getModule());
+ ConstantInt *Key = ConstantInt::get(Int32Ty, InitFiniKey);
ConstantInt *IntDisc = ConstantInt::get(
Int64Ty, AArch64PAuth::InitFiniPointerConstantDiscriminator);
Constant *Null = ConstantPointerNull::get(PtrTy);
@@ -3046,7 +3051,10 @@ void AArch64AsmPrinter::LowerMOVaddrPAC(const MachineInstr &MI) {
assert(GAOp.getGlobal()->getValueType() != nullptr);
bool IsFunctionTy = GAOp.getGlobal()->getValueType()->isFunctionTy();
- auto AuthKey = IsFunctionTy ? AArch64PACKey::IA : AArch64PACKey::DA;
+ const auto &ATM = static_cast<AArch64TargetMachine &>(TM);
+ const Module &M = *MMI->getModule();
+ auto AuthKey = IsFunctionTy ? ATM.getGlobalCodeKey(M)
+ : ATM.getGlobalDataKey(M);
emitAUT(AuthKey, AArch64::X16, AArch64::X17);
if (!STI->hasFPAC())
@@ -3125,7 +3133,10 @@ void AArch64AsmPrinter::LowerLOADgotAUTH(const MachineInstr &MI) {
assert(GAMO.getGlobal()->getValueType() != nullptr);
bool IsFunctionTy = GAMO.getGlobal()->getValueType()->isFunctionTy();
- auto AuthKey = IsFunctionTy ? AArch64PACKey::IA : AArch64PACKey::DA;
+ const auto &ATM = static_cast<AArch64TargetMachine &>(TM);
+ const Module &M = *MMI->getModule();
+ auto AuthKey = IsFunctionTy ? ATM.getGlobalCodeKey(M)
+ : ATM.getGlobalDataKey(M);
emitAUT(AuthKey, AuthResultReg, AArch64::X17);
if (GAMO.getGlobal()->hasExternalWeakLinkage())
@@ -3144,9 +3155,12 @@ AArch64AsmPrinter::lowerBlockAddressConstant(const BlockAddress &BA) {
const MCExpr *BAE = AsmPrinter::lowerBlockAddressConstant(BA);
const Function &Fn = *BA.getFunction();
+ const auto &ATM = static_cast<AArch64TargetMachine &>(TM);
+ AArch64PACKey::ID Key =
+ ATM.getGlobalCodeKey(*MMI->getModule());
if (std::optional<uint16_t> BADisc =
STI->getPtrAuthBlockAddressDiscriminatorIfEnabled(Fn))
- return AArch64AuthMCExpr::create(BAE, *BADisc, AArch64PACKey::IA,
+ return AArch64AuthMCExpr::create(BAE, *BADisc, Key,
/*HasAddressDiversity=*/false, OutContext);
return BAE;
@@ -3846,11 +3860,15 @@ void AArch64AsmPrinter::emitInstruction(const MachineInstr *MI) {
--InstsEmitted; // no code emitted
#endif
- MCInst Blraa;
- Blraa.setOpcode(AArch64::BLRAA);
- Blraa.addOperand(MCOperand::createReg(AArch64::X16));
- Blraa.addOperand(MCOperand::createReg(AArch64::X0));
- EmitToStreamer(*OutStreamer, Blraa);
+ const auto &ATM = static_cast<AArch64TargetMachine &>(TM);
+ AArch64PACKey::ID Key =
+ ATM.getGlobalCodeKey(*MMI->getModule());
+ unsigned Opcode = getBranchOpcodeForKey(/* IsCall */ true, Key, /* IsZeroDisc */ false);
+ MCInst Blra;
+ Blra.setOpcode(Opcode);
+ Blra.addOperand(MCOperand::createReg(AArch64::X16));
+ Blra.addOperand(MCOperand::createReg(AArch64::X0));
+ EmitToStreamer(*OutStreamer, Blra);
return;
}
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index a11a813f3e821..edf9c785ce73e 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -11654,10 +11654,14 @@ AArch64TargetLowering::LowerDarwinGlobalTLSAddress(SDValue Op,
Ops.push_back(Chain);
Ops.push_back(FuncTLVGet);
- // With ptrauth-calls, the tlv access thunk pointer is authenticated (IA, 0).
+ // With ptrauth-calls, the tlv access thunk pointer is authenticated (global
+ // code key, 0).
if (DAG.getMachineFunction().getFunction().hasFnAttribute("ptrauth-calls")) {
+ MachineFunction &MF = DAG.getMachineFunction();
+ const Module &M = *MF.getFunction().getParent();
+ AArch64PACKey::ID Key = getTM().getGlobalCodeKey(M);
Opcode = AArch64ISD::AUTH_CALL;
- Ops.push_back(DAG.getTargetConstant(AArch64PACKey::IA, DL, MVT::i32));
+ Ops.push_back(DAG.getTargetConstant(Key, DL, MVT::i32));
Ops.push_back(DAG.getTargetConstant(0, DL, MVT::i64)); // Integer Disc.
Ops.push_back(DAG.getRegister(Register(), MVT::i64)); // Addr Disc.
}
@@ -13641,7 +13645,9 @@ SDValue AArch64TargetLowering::LowerBRIND(SDValue Op, SelectionDAG &DAG) const {
SDLoc DL(Op);
SDValue Disc = DAG.getTargetConstant(*BADisc, DL, MVT::i64);
- SDValue Key = DAG.getTargetConstant(AArch64PACKey::IA, DL, MVT::i32);
+ const Module& M = *MF.getFunction().getParent();
+ AArch64PACKey::ID KeyId = getTM().getGlobalCodeKey(M);
+ SDValue Key = DAG.getTargetConstant(KeyId, DL, MVT::i32);
SDValue AddrDisc = DAG.getRegister(AArch64::XZR, MVT::i64);
SDNode *BrA = DAG.getMachineNode(AArch64::BRA, DL, MVT::Other,
@@ -13680,8 +13686,10 @@ SDValue AArch64TargetLowering::LowerBlockAddress(SDValue Op,
SDValue TargetBA = DAG.getTargetBlockAddress(BA, BAN->getValueType(0));
SDValue Disc = DAG.getTargetConstant(*BADisc, DL, MVT::i64);
-
- SDValue Key = DAG.getTargetConstant(AArch64PACKey::IA, DL, MVT::i32);
+ MachineFunction &MF = DAG.getMachineFunction();
+ const Module& M = *MF.getFunction().getParent();
+ AArch64PACKey::ID KeyId = getTM().getGlobalCodeKey(M);
+ SDValue Key = DAG.getTargetConstant(KeyId, DL, MVT::i32);
SDValue AddrDisc = DAG.getRegister(AArch64::XZR, MVT::i64);
SDNode *MOV =
diff --git a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp
index 7129e332585fa..980ce65275828 100644
--- a/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp
@@ -16,6 +16,7 @@
#include "AArch64MachineFunctionInfo.h"
#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
+#include "AArch64TargetMachine.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DiagnosticInfo.h"
@@ -83,8 +84,11 @@ static SignReturnAddress GetSignReturnAddress(const Function &F) {
}
static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
- if (F.hasFnAttribute("ptrauth-returns"))
- return true;
+ if (F.hasFnAttribute("ptrauth-returns")) {
+ const Module& M = *F.getParent();
+ return STI.getTargetLowering()->getTM().getLocalCodeKey(M) ==
+ AArch64PACKey::IB;
+ }
if (!F.hasFnAttribute("sign-return-address-key")) {
if (STI.getTargetTriple().isOSWindowsOrUEFI())
return true;
diff --git a/llvm/lib/Target/AArch64/AArch64PointerAuth.h b/llvm/lib/Target/AArch64/AArch64PointerAuth.h
index 55ed3698bdecc..e81ff2aee01aa 100644
--- a/llvm/lib/Target/AArch64/AArch64PointerAuth.h
+++ b/llvm/lib/Target/AArch64/AArch64PointerAuth.h
@@ -14,9 +14,6 @@
namespace llvm {
namespace AArch64PAuth {
-/// PAuth key to be used with function pointers in .init_array and .fini_array.
-constexpr AArch64PACKey::ID InitFiniKey = AArch64PACKey::IA;
-
/// Constant discriminator to be used with function pointers in .init_array and
/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
constexpr unsigned InitFiniPointerConstantDiscriminator = 0xD9D4;
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index 919cf2f103c29..a1973a9058f86 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -132,6 +132,47 @@ void AArch64TargetMachine::reset() {
LastSubtarget.clear();
}
+static AArch64PACKey::ID parsePtrAuthKeys(StringRef Keys, size_t Start) {
+ if (Keys.size() != 8)
+ report_fatal_error("ptrauth-keys must be in form ixdxiydy where x and y "
+ "are either a or b.");
+ if (auto OptKey = AArch64StringToPACKeyID(Keys.substr(Start, 2)))
+ return *OptKey;
+ report_fatal_error("unrecognized ptrauth key");
+}
+
+AArch64PACKey::ID
+AArch64TargetMachine::getGlobalCodeKey(const Module &M) const {
+ if (getTargetTriple().isOSBinFormatELF())
+ if (auto *Val = dyn_cast_or_null<MDString>(M.getModuleFlag("ptrauth-keys")))
+ return parsePtrAuthKeys(Val->getString(), /* Start */ 0);
+ return AArch64PACKey::IA;
+}
+
+AArch64PACKey::ID
+AArch64TargetMachine::getGlobalDataKey(const Module &M) const {
+ if (getTargetTriple().isOSBinFormatELF())
+ if (auto *Val = dyn_cast_or_null<MDString>(M.getModuleFlag("ptrauth-keys")))
+ return parsePtrAuthKeys(Val->getString(), /* Start */ 2);
+ return AArch64PACKey::DA;
+}
+
+AArch64PACKey::ID
+AArch64TargetMachine::getLocalCodeKey(const Module &M) const {
+ if (getTargetTriple().isOSBinFormatELF())
+ if (auto *Val = dyn_cast_or_null<MDString>(M.getModuleFlag("ptrauth-keys")))
+ return parsePtrAuthKeys(Val->getString(), /* Start */ 4);
+ return AArch64PACKey::IB;
+}
+
+AArch64PACKey::ID
+AArch64TargetMachine::getLocalDataKey(const Module &M) const {
+ if (getTargetTriple().isOSBinFormatELF())
+ if (auto *Val = dyn_cast_or_null<MDString>(M.getModuleFlag("ptrauth-keys")))
+ return parsePtrAuthKeys(Val->getString(), /* Start */ 6);
+ return AArch64PACKey::DB;
+}
+
//===----------------------------------------------------------------------===//
// AArch64 Lowering public interface.
//===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.h b/llvm/lib/Target/AArch64/AArch64TargetMachine.h
index c771fcf2b2685..890a9ffee19c6 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.h
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.h
@@ -15,6 +15,7 @@
#include "AArch64InstrInfo.h"
#include "AArch64Subtarget.h"
+#include "Utils/AArch64BaseInfo.h"
#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DataLayout.h"
@@ -112,6 +113,23 @@ class AArch64TargetMachine : public CodeGenTargetMachineImpl {
/// be because of an optnone function.
bool isGlobalISelOptNone() const;
+ /// Returns the global to program, AKA process-independent in Arm64e, key used
+ /// to sign code pointers. Taken from the pauthabi-keys module flag if present
+ /// on ELF platforms, otherwise the key implied by the target triple.
+ AArch64PACKey::ID getGlobalCodeKey(const Module &M) const;
+ /// Returns the global to program, AKA process-independent in Arm64e, key used
+ /// to sign data pointers. Taken from the pauthabi-keys module flag if present
+ /// on ELF platforms, otherwise the key implied by the target triple.
+ AArch64PACKey::ID getGlobalDataKey(const Module &M) const;
+ /// Returns the local to function, AKA process-dependent in Arm64e, key used
+ /// to sign code pointers. Taken from the pauthabi-keys module flag if present
+ /// on ELF platforms, otherwise the key implied by the target triple.
+ AArch64PACKey::ID getLocalCodeKey(const Module &M) const;
+ /// Returns the local to function, AKA process-dependent in Arm64e, key used
+ /// to sign code pointers. Taken from the pauthabi-keys module flag if present
+ /// on ELF platforms, otherwise the key implied by the target triple.
+ AArch64PACKey::ID getLocalDataKey(const Module &M) const;
+
private:
bool isLittle;
};
diff --git a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
index 8acf29159bc79..61e34c055b751 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetObjectFile.cpp
@@ -51,10 +51,12 @@ void AArch64_ELFTargetObjectFile::emitPersonalityValueImpl(
return;
}
auto *TS = static_cast<AArch64TargetStreamer *>(Streamer.getTargetStreamer());
+ const auto &ATM = static_cast<const AArch64TargetMachine &>(*TM);
+ AArch64PACKey::ID Key = ATM.getGlobalCodeKey(*MMI->getModule());
// The value is ptrauth_string_discriminator("personality")
constexpr uint16_t Discriminator = 0x7EAD;
TS->emitAuthValue(MCSymbolRefExpr::create(Sym, getContext()), Discriminator,
- AArch64PACKey::IA, /*HasAddressDiversity=*/true);
+ Key, /*HasAddressDiversity=*/true);
}
const MCExpr *AArch64_ELFTargetObjectFile::getIndirectSymViaGOTPCRel(
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
index 2b84ee14d4366..c100d619c33ce 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp
@@ -2496,7 +2496,9 @@ bool AArch64InstructionSelector::earlySelect(MachineInstr &I) {
if (std::optional<uint16_t> BADisc =
STI.getPtrAuthBlockAddressDiscriminatorIfEnabled(Fn)) {
auto MI = MIB.buildInstr(AArch64::BRA, {}, {I.getOperand(0).getReg()});
- MI.addImm(AArch64PACKey::IA);
+ const Module &M = *MF.getFunction().getParent();
+ AArch64PACKey::ID Key = TM.getGlobalCodeKey(M);
+ MI.addImm(Key);
MI.addImm(*BADisc);
MI.addReg(/*AddrDisc=*/AArch64::XZR);
I.eraseFromParent();
@@ -3476,9 +3478,11 @@ bool AArch64InstructionSelector::select(MachineInstr &I) {
Function *BAFn = I.getOperand(1).getBlockAddress()->getFunction();
if (std::optional<uint16_t> BADisc =
STI.getPtrAuthBlockAddressDiscriminatorIfEnabled(*BAFn)) {
+ const Module &M = *MF.getFunction().getParent();
+ AArch64PACKey::ID Key = TM.getGlobalCodeKey(M);
MIB.buildInstr(AArch64::MOVaddrPAC)
.addBlockAddress(I.getOperand(1).getBlockAddress())
- .addImm(AArch64PACKey::IA)
+ .addImm(Key)
.addReg(/*AddrDisc=*/AArch64::XZR)
.addImm(*BADisc)
.constrainAllUses(TII, TRI, RBI);
diff --git a/llvm/test/CodeGen/AArch64/ptrauth-arm64-tls-dynamics.ll b/llvm/test/CodeGen/AArch64/ptrauth-arm64-tls-dynamics.ll
index 0fc8588df51c8..ed8d6941b7caf 100644
--- a/llvm/test/CodeGen/AArch64/ptrauth-arm64-tls-dynamics.ll
+++ b/llvm/test/CodeGen/AArch64/ptrauth-arm64-tls-dynamics.ll
@@ -1,12 +1,26 @@
+; RUN: rm -rf %t && split-file %s %t && cd %t
+; RUN: cat common.ll authflag.ll > auth.ll
+; RUN: cat common.ll authflag-bb.ll > auth-bb.ll
+
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
+; RUN: -verify-machineinstrs < auth.ll | FileCheck --check-prefixes=CHECK,CHECKA %s
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
+; RUN: -filetype=obj < auth.ll | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
+; RUN: -global-isel=1 -verify-machineinstrs < auth.ll 2>&1 | FileCheck --check-prefixes=CHECK,CHECKA %s
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
-; RUN: -verify-machineinstrs < %s | FileCheck %s
+; RUN: -global-isel=1 -filetype=obj < auth.ll | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
+
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
+; RUN: -verify-machineinstrs < auth-bb.ll | FileCheck --check-prefixes=CHECK,CHECKB %s
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
-; RUN: -filetype=obj < %s | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
+; RUN: -filetype=obj < auth-bb.ll | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
-; RUN: -global-isel=1 -verify-machineinstrs < %s 2>&1 | FileCheck %s
+; RUN: -global-isel=1 -verify-machineinstrs < auth-bb.ll 2>&1 | FileCheck --check-prefixes=CHECK,CHECKB %s
; RUN: llc -mtriple=aarch64-unknown-linux-gnu -mattr=+pauth -relocation-model=pic \
-; RUN: -global-isel=1 -filetype=obj < %s | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
+; RUN: -global-isel=1 -filetype=obj < auth-bb.ll | llvm-readelf -r -s - | FileCheck --check-prefix=CHECK-OBJ %s
+;--- common.ll
@general_dynamic_var = external thread_local global i32
define i32 @test_generaldynamic() {
@@ -19,7 +33,8 @@ define i32 @test_generaldynamic() {
; CHECK-NEXT: ldr x16, [x[[TLSDESC_HI]], :tlsdesc_auth_lo12:general_dynamic_var]
; CHECK-NEXT: add x0, x[[TLSDESC_HI]], :tlsdesc_auth_lo12:general_dynamic_var
; CHECK-NEXT: .tlsauthdesccall general_dynamic_var
-; CHECK-NEXT: blraa x16, x0
+; CHECKA-NEXT: blraa x16, x0
+; CHECKB-NEXT: blrab x16, x0
; CHECK-NEXT: mrs x[[TPIDR:[0-9]+]], TPIDR_EL0
; CHECK-NEXT: ldr w0, [x[[TPIDR]], x0]
@@ -38,7 +53,8 @@ define ptr @test_generaldynamic_addr() {
; CHECK-NEXT: ldr x16, [x[[TLSDESC_HI]], :tlsdesc_auth_lo12:general_dynamic_var]
; CHECK-NEXT: add x0, x[[TLSDESC_HI]], :tlsdesc_auth_lo12:general_dynamic_var
; CHECK-NEXT: .tlsauthdesccall general_dynamic_var
-; CHECK-NEXT: blraa x16, x0
+; CHECKA-NEXT: blraa x16, x0
+; CHECKB-NEXT: blrab x16, x0
; CHECK-NEXT: mrs [[TP:x[0-9]+]], TPIDR_EL0
; CHECK-NEXT: add x0, [[TP]], x0
@@ -63,7 +79,8 @@ define i32 @test_localdynamic() {
; CHECK-NEXT: ldr x16, [x[[TLSDESC_HI]], :tlsdesc_auth_lo12:local_dynamic_var]
; CHECK-NEXT: add x0, x[[TLSDESC_HI]], :tlsdesc_auth_lo12:local_dynamic_var
; CHECK-NEXT: .tlsauthdesccall local_dynamic_var
-; CHECK-NEXT: blraa x16, x0
+; CHECKA-NEXT: blraa x16, x0
+; CHECKB-NEXT: blrab x16, x0
; CHECK-NEXT: mrs x[[TPIDR:[0-9]+]], TPIDR_EL0
; CHECK-NEXT: ldr w0, [x[[TPIDR]], x0]
@@ -82,7 +99,8 @@ define ptr @test_localdynamic_addr() {
; CHECK-NEXT: ldr x16, [x[[TLSDESC_HI]], :tlsdesc_auth_lo12:local_dynamic_var]
; CHECK-NEXT: add x0, x[[TLSDESC_HI]], :tlsdesc_auth_lo12:local_dynamic_var
; CHECK-NEXT: .tlsauthdesccall local_dynamic_var
-; CHECK-NEXT: blraa x16, x0
+; CHECKA-NEXT: blraa x16, x0
+; CHECKB-NEXT: blrab x16, x0
; CHECK-NEXT: mrs x[[TPIDR:[0-9]+]], TPIDR_EL0
; CHECK-NEXT: add x0, x[[TPIDR]], x0
@@ -104,7 +122,8 @@ define i32 @test_extern_weak() {
; CHECK-NEXT: ldr x16, [x[[TLSDESC_HI]], :tlsdesc_auth_lo12:extern_weak_var]
; CHECK-NEXT: add x0, x[[TLSDESC_HI]], :tlsdesc_auth_lo12:extern_weak_var
; CHECK-NEXT: .tlsauthdesccall extern_weak_var
-; CHECK-NEXT: blraa x16, x0
+; CHECKA-NEXT: blraa x16, x0
+; CHECKB-NEXT: blrab x16, x0
; CHECK-NEXT: mrs x[[TPIDR:[0-9]+]], TPIDR_EL0
; CHECK-NEXT: ldr w0, [x[[TPIDR]], x0]
@@ -115,5 +134,11 @@ def...
[truncated]
</details>
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
This PR is the LLVM part of enabling a firmware team to be able to use Pointer Authentication with their choice of keys, with all A and all B the most likely choices.
I've assumed that on MachO there is no interest in changing the keys, so I've ignored the module-flag for that target. Adding Apple folk for visibility.
I've used local and global code and data keys (following a conversation in https://github.com/llvm/llvm-project/pull/237916). I started with using the Arm64e terms such as process independent and process dependent, however these don't fit a bare-metal environment that well, and aren't used in the backend. Local and Global are more neutral, and shorter!
I struggled to find a good place to put the accessor functions. I considered MachineModuleInfoELF but there are some drawbacks:
The keys are AArch64 specific and that is target neutral code.
Would need an if target is ELF if statement everywhere a key is needed.
Open to suggestions for a better place.
Would it make sense to implement support for this in lld first (or was it already landed and I've missed smth) so we would not face strange behavior when trying to feed new objects with custom key choices to lld?
It looks like that with current implementation lld just defaults to A key family: https://github.com/llvm/llvm-project/blob/30fd47f58d639a6d307c5c781858dd62311173ca/lld/ELF/SyntheticSections.cpp#L598-L609
Is there a test which checks the two errors which could be emitted by this function? Also, would it make sense to double-check that the Start is an even number and that the i/d character matches the one expected (so we do not have e.g. "dxixdyiy")?
Good question. I had originally thought that they were unrelated as the lld patch [1] uses the (platform, version) which is covered by the two existing aarch64-elf-pauthabi-platform and aarch64-elf-pauthabi-version.
However I think this points out a potential flaw in using just the platform/version for lld. What I've implemented for lld [1] works for known profiles as LLD knows where the bits are. However if there are future platforms with different signing-schemas (platform, version) then each of these will need a lld patch to be able to change the GOT signing key.
The other mechanism available to LLD is a build-attribute, something like
Tag_PAuth_GOT_Code_Key (=3) with the default of 0 being the IA key, 1 being the IB key.
Tag_PAuth_GOT_Data_Key (=4) with the default of 0 being the DA key, 1 being the DB key.
So I think I've got 3 choices here:
Restrict key changes to the (platform, version) matching the baremetal profile. Restricting them from the custom profile, for now at least. That would mean we don't need a new module flag for the keys and can just use the encoding in aarch64-elf-pauthabi-version like LLD.
Reserve part of the (platform, version) space, for communication of GOT keys with LLD (or other tools), even for custom profiles. Would most likely need an opt-in space, for example custom platforms with bit X set in the platform opt in to some part of the version encoding the keys. This could also be added at a later date, and wouldn't need a new module-flag.
My current preference is to reserve part of the private custom space [2], to opt-in to using the bottom 8-bits of the version to encode keys. That way we have one way of encoding GOT keys and we don't need a new module flag.
[1] Link to lld patch in my llvm fork https://github.com/llvm/llvm-project/commit/19ad4f44f79b702d7a69631fc2fd803b276e3dc0
[2] ABI PR private mask https://github.com/ARM-software/abi-aa/pull/448/changes#diff-d98cba9fadbc24d55e09df3d54a3185f503ab1cf7f443eade2d8ea39ed8c3bccR904
I will need to add a test. Apologies for missing that out. While thinking about where to put this I discovered that the existing module flag checks are in void Verifier::visitModuleFlags() and as this runs by default I think they could be moved to that point.
Related to your lld question I also wondered if I can just use the encoding in:aarch64-elf-pauthabi-platform and aarch64-elf-pauthabi-version, i.e. if platform == baremetal-profile then pickup the keys from the version. That doesn't involve any messy string parsing, and stops any possibility of the module flag being incompatible with the version.
I'm going to give that a go to see what it looks like.
Thanks for the comments and apologies for the delay in responding. Seems like GitHub is losing some nofifications.
I have rewritten to avoid a new module-flag. Use the existing module flags for platform, version instead. I've updated the ABI https://github.com/ARM-software/abi-aa/pull/448 so that a private platform from opt in to the key encoding, and GOT signing.
LLD patch to alter the GOT signing https://github.com/llvm/llvm-project/pull/248444
The clang test failures are due to the increased type size of the enum changing an i32 to i64 in the module flag size. I do need to make that change for both platform and version. I'll submit that part as a separate patch rather than adding to this one.
kovdan01
smithp35
Express the ptrauth keys as global or local to mirror the values in ptrauth.h.
global code (AKA process independent code in Arm64e)
global data (AKA process independent data in Arm64e)
local code (AKA process dependent code in Arm64e)
local data (AKA process dependent data in Arm64e)
Use utility functions to access these. I have used the AArch64TargetMachine following the example of getFloatABI in the ARM Backend, although the defaults are not currently Target Specific.
By default the global keys use the A key, the local keys use the B key.
If the aarch64-elf-pauthabi-platform module flag has a value encodes keys in the version then use
aarch64-elf-pauthabi-version to derive the keys. In summary:
A platform of 1 (Bare Metal) or a custom private platform with top-bit set encodes keys in version.
The bottom 8-bits of version contain the keys, 2-bits per key to represent one of IA, IB, DA, DB
The deactivation symbols irelative code path only support the DA key due to limitations of the PAC emulation library. However if given a different key it will fall back gracefully to another code path that can support DB.
Added tests based on existing implicit usage of keys in backend.