From e31206a391dae40807e09b5899ee39647570a0c9 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Mon, 4 Dec 2023 10:54:42 +0300 Subject: [PATCH 1/8] [AArch64][PAC] Emit a fatal error when ptrauth target feature is missing Previously, in `expandPtrAuthPseudo` we expanded `MOVaddrPAC`, `LOADgotPAC` and `LOADauthptrgot` pseudo-instructions without taking presense of PAuth subtarget feature into account. In case of `LOADauthptrgot`, it resulted in undesired so-called `$auth_ptr$` stub and a corresponding AUTH relocation. In case `MOVaddrPAC` and `LOADgotPAC`, it resulted in pac-specific instructions (e.g. `paciza`) emitted which was only caught via assertion during machine instructions verification (and not caught at all when assertions are disabled). This patch makes us emit a fatal error and fail fast in such cases. --- .../AArch64/AArch64ExpandHardenedPseudos.cpp | 6 ++ .../GlobalISel/ptrauth-lowering-err.ll | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll diff --git a/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp b/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp index 394691e0a1a2..6ee9b3b66ab5 100644 --- a/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp +++ b/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp @@ -171,6 +171,9 @@ bool AArch64ExpandHardenedPseudos::expandPtrAuthPseudo(MachineInstr &MI) { if (MI.getOpcode() == AArch64::LOADauthptrgot) { LLVM_DEBUG(dbgs() << "Expanding: " << MI << "\n"); + if (!STI.hasPAuth()) + report_fatal_error("pac instructions require ptrauth target feature"); + const TargetMachine &TM = MF.getTarget(); MachineModuleInfo &MMI = MF.getMMI(); @@ -223,6 +226,9 @@ bool AArch64ExpandHardenedPseudos::expandPtrAuthPseudo(MachineInstr &MI) { LLVM_DEBUG(dbgs() << "Expanding: " << MI << "\n"); + if (!STI.hasPAuth()) + report_fatal_error("pac instructions require ptrauth target feature"); + const bool IsGOTLoad = MI.getOpcode() == AArch64::LOADgotPAC; MachineOperand GAOp = MI.getOperand(0); auto Key = (AArch64PACKey::ID)MI.getOperand(1).getImm(); diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll new file mode 100644 index 000000000000..aeb99f2a7331 --- /dev/null +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -0,0 +1,57 @@ +; RUN: split-file %s %t && cd %t + +;--- MOVaddrPAC.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ +; RUN: FileCheck MOVaddrPAC.ll + +; CHECK: Expanding: MOVaddrPAC @foo +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +@foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 + +define ptr @bar() #0 { + ret ptr @foo.ptrauth +} + +define private void @foo() { + ret void +} + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- LOADgotPAC.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ +; RUN: FileCheck LOADgotPAC.ll + +; CHECK: Expanding: LOADgotPAC @foo +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +@foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 + +define ptr @bar() #0 { + ret ptr @foo.ptrauth +} + +declare void @foo() + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- LOADauthptrgot.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ +; RUN: FileCheck LOADauthptrgot.ll + +; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +define i8* @foo() #0 { + %tmp = bitcast { i8*, i32, i64, i64 }* @g_weak.ptrauth to i8* + ret i8* %tmp +} + +@g_weak = extern_weak global i32 +@g_weak.ptrauth = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_weak to i8*), i32 0, i64 0, i64 0 }, section "llvm.ptrauth" + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } From ccfa70eb879773d70a2762e607f9a92b4ba452e9 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Wed, 6 Dec 2023 03:28:20 +0300 Subject: [PATCH 2/8] Pass `-debug` option to llc only on assertion-enabled builds The option is not present on assertion-disabled builds. A test without the option (and without verifying debug output ensuring which particular pseudo-instructions are expanded) is also added - it looks reasonable since the PR itself is intended to add proper error handling for assertion-disabled builds. --- .../GlobalISel/ptrauth-lowering-err-debug.ll | 58 +++++++++++++++++++ .../GlobalISel/ptrauth-lowering-err.ll | 9 +-- 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll new file mode 100644 index 000000000000..b0a372748c0d --- /dev/null +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll @@ -0,0 +1,58 @@ +; REQUIRES: asserts +; RUN: split-file %s %t && cd %t + +;--- MOVaddrPAC.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ +; RUN: FileCheck MOVaddrPAC.ll + +; CHECK: Expanding: MOVaddrPAC @foo +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +@foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 + +define ptr @bar() #0 { + ret ptr @foo.ptrauth +} + +define private void @foo() { + ret void +} + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- LOADgotPAC.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ +; RUN: FileCheck LOADgotPAC.ll + +; CHECK: Expanding: LOADgotPAC @foo +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +@foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 + +define ptr @bar() #0 { + ret ptr @foo.ptrauth +} + +declare void @foo() + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- LOADauthptrgot.ll + +; RUN: not --crash llc -debug -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ +; RUN: FileCheck LOADauthptrgot.ll + +; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +define i8* @foo() #0 { + %tmp = bitcast { i8*, i32, i64, i64 }* @g_weak.ptrauth to i8* + ret i8* %tmp +} + +@g_weak = extern_weak global i32 +@g_weak.ptrauth = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_weak to i8*), i32 0, i64 0, i64 0 }, section "llvm.ptrauth" + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll index aeb99f2a7331..e549d5cdee0a 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -2,10 +2,9 @@ ;--- MOVaddrPAC.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ +; RUN: not --crash llc -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ ; RUN: FileCheck MOVaddrPAC.ll -; CHECK: Expanding: MOVaddrPAC @foo ; CHECK: LLVM ERROR: pac instructions require ptrauth target feature @foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 @@ -22,10 +21,9 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ;--- LOADgotPAC.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ +; RUN: not --crash llc -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ ; RUN: FileCheck LOADgotPAC.ll -; CHECK: Expanding: LOADgotPAC @foo ; CHECK: LLVM ERROR: pac instructions require ptrauth target feature @foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 @@ -40,10 +38,9 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ;--- LOADauthptrgot.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ +; RUN: not --crash llc -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ ; RUN: FileCheck LOADauthptrgot.ll -; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak ; CHECK: LLVM ERROR: pac instructions require ptrauth target feature define i8* @foo() #0 { From 52afe428f699b1eddcd6b649058d1e21c2f21b1b Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Thu, 7 Dec 2023 03:42:28 +0300 Subject: [PATCH 3/8] Use `-debug-only=aarch64-expand-hardened-pseudos` instead of `-debug` --- .../AArch64/GlobalISel/ptrauth-lowering-err-debug.ll | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll index b0a372748c0d..fb4b5446a74c 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll @@ -3,7 +3,7 @@ ;--- MOVaddrPAC.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ +; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ ; RUN: FileCheck MOVaddrPAC.ll ; CHECK: Expanding: MOVaddrPAC @foo @@ -23,7 +23,7 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ;--- LOADgotPAC.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ +; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ ; RUN: FileCheck LOADgotPAC.ll ; CHECK: Expanding: LOADgotPAC @foo @@ -41,7 +41,7 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ;--- LOADauthptrgot.ll -; RUN: not --crash llc -debug -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ +; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ ; RUN: FileCheck LOADauthptrgot.ll ; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak From 81cb742c38224309aa29bde914bd2fcf164d74b1 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Thu, 7 Dec 2023 05:02:48 +0300 Subject: [PATCH 4/8] Ensure that ptrauth subtarget feature is checked for ptrauth-returns --- .../Target/AArch64/AArch64FrameLowering.cpp | 8 ++++++-- .../AArch64/GlobalISel/ptrauth-lowering-err.ll | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp index 05cf6b4c2ac5..092522ef5e1b 100644 --- a/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64FrameLowering.cpp @@ -1511,7 +1511,9 @@ void AArch64FrameLowering::emitPrologue(MachineFunction &MF, // If we're saving LR, sign it first. if (shouldAuthenticateLR(MF)) { if (LLVM_UNLIKELY(!Subtarget.hasPAuth())) - report_fatal_error("arm64e LR authentication requires ptrauth"); + report_fatal_error( + StringRef(Subtarget.isTargetMachO() ? "arm64e" : "aarch64") + + " LR authentication requires ptrauth"); for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) { if (Info.getReg() != AArch64::LR) continue; @@ -2051,7 +2053,9 @@ void AArch64FrameLowering::emitEpilogue(MachineFunction &MF, auto InsertAuthLROnExit = make_scope_exit([&]() { if (shouldAuthenticateLR(MF)) { if (LLVM_UNLIKELY(!Subtarget.hasPAuth())) - report_fatal_error("arm64e LR authentication requires ptrauth"); + report_fatal_error( + StringRef(Subtarget.isTargetMachO() ? "arm64e" : "aarch64") + + " LR authentication requires ptrauth"); for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo()) { if (Info.getReg() != AArch64::LR) continue; diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll index e549d5cdee0a..aae0075eebb8 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -52,3 +52,21 @@ define i8* @foo() #0 { @g_weak.ptrauth = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_weak to i8*), i32 0, i64 0, i64 0 }, section "llvm.ptrauth" attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- ptrauth-returns.ll + +; RUN: not --crash llc -mtriple aarch64-elf ptrauth-returns.ll 2>&1 | \ +; RUN: FileCheck ptrauth-returns.ll + +; CHECK: LLVM ERROR: aarch64 LR authentication requires ptrauth + +define i32 @bar() #0 { + ret i32 42 +} + +define i32 @foo() { + %tmp = call i32 @bar() + ret i32 %tmp +} + +attributes #0 = { "ptrauth-returns" "target-cpu"="generic" } From 38214453def27a7d89f9ebc025ff1b154ef18fd3 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Thu, 7 Dec 2023 05:24:40 +0300 Subject: [PATCH 5/8] Ensure AUTH_CALL is not emitted when ptrauth subtarget feature is missing --- .../AArch64/GlobalISel/ptrauth-lowering-err.ll | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll index aae0075eebb8..23ffc8cdd392 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -70,3 +70,17 @@ define i32 @foo() { } attributes #0 = { "ptrauth-returns" "target-cpu"="generic" } + +;--- auth-call.ll + +; RUN: not --crash llc -mtriple aarch64-elf auth-call.ll 2>&1 | \ +; RUN: FileCheck auth-call.ll + +; CHECK: LLVM ERROR: Cannot select:{{.*}}AArch64ISD::AUTH_CALL + +define void @bar(ptr %foo) #0 { + call void %foo() [ "ptrauth"(i32 0, i64 0) ] + ret void +} + +attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } From 9ed8fc69b0d66138610bf74c8cf066b21356f5ba Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Thu, 7 Dec 2023 05:27:01 +0300 Subject: [PATCH 6/8] Use CHECK-NEXT instead of CHECK when suitable --- .../GlobalISel/ptrauth-lowering-err-debug.ll | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll index fb4b5446a74c..823640680ed7 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll @@ -6,8 +6,9 @@ ; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf MOVaddrPAC.ll 2>&1 | \ ; RUN: FileCheck MOVaddrPAC.ll -; CHECK: Expanding: MOVaddrPAC @foo -; CHECK: LLVM ERROR: pac instructions require ptrauth target feature +; CHECK: Expanding: MOVaddrPAC @foo +; CHECK-NEXT: {{^$}} +; CHECK-NEXT: LLVM ERROR: pac instructions require ptrauth target feature @foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 @@ -26,8 +27,9 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf LOADgotPAC.ll 2>&1 | \ ; RUN: FileCheck LOADgotPAC.ll -; CHECK: Expanding: LOADgotPAC @foo -; CHECK: LLVM ERROR: pac instructions require ptrauth target feature +; CHECK: Expanding: LOADgotPAC @foo +; CHECK-NEXT: {{^$}} +; CHECK-NEXT: LLVM ERROR: pac instructions require ptrauth target feature @foo.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @foo, i32 0, i64 0, i64 0 }, section "llvm.ptrauth", align 8 @@ -44,8 +46,9 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ; RUN: not --crash llc -debug-only=aarch64-expand-hardened-pseudos -mtriple aarch64-elf LOADauthptrgot.ll 2>&1 | \ ; RUN: FileCheck LOADauthptrgot.ll -; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak -; CHECK: LLVM ERROR: pac instructions require ptrauth target feature +; CHECK: Expanding: {{.*}}LOADauthptrgot @g_weak +; CHECK-NEXT: {{^$}} +; CHECK-NEXT: LLVM ERROR: pac instructions require ptrauth target feature define i8* @foo() #0 { %tmp = bitcast { i8*, i32, i64, i64 }* @g_weak.ptrauth to i8* From bffc609c13d5f077e29a5bb1ca1cb4f7022520dc Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sat, 9 Dec 2023 00:12:06 +0300 Subject: [PATCH 7/8] Add error messages for emitting LDRA* pseudos without PAuth subtarget feature --- .../AArch64/AArch64ExpandHardenedPseudos.cpp | 5 +++++ llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp | 3 +++ .../GISel/AArch64InstructionSelector.cpp | 3 +++ .../AArch64/GlobalISel/ptrauth-lowering-err.ll | 17 +++++++++++++++++ 4 files changed, 28 insertions(+) diff --git a/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp b/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp index 6ee9b3b66ab5..76e1417ed206 100644 --- a/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp +++ b/llvm/lib/Target/AArch64/AArch64ExpandHardenedPseudos.cpp @@ -338,6 +338,11 @@ bool AArch64ExpandHardenedPseudos::expandAuthLoad(MachineInstr &MI) { LLVM_DEBUG(dbgs() << "Expanding: " << MI << "\n"); + // LDRA and LDRApre preudos are emitted in AArch64DAGToDAGISel::tryAuthLoad + // and AArch64InstructionSelector::selectAuthLoad with prior checks against + // ptrauth subtarget feature + assert(STI.hasPAuth()); + bool IsPre = MI.getOpcode() == AArch64::LDRApre; MachineOperand DstOp = MI.getOperand(0); diff --git a/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp b/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp index 9a9426e9245d..f2c0e8df2024 100644 --- a/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelDAGToDAG.cpp @@ -1723,6 +1723,9 @@ bool AArch64DAGToDAGISel::tryAuthLoad(SDNode *N) { return false; } + if (!Subtarget->hasPAuth()) + report_fatal_error("pac instructions require ptrauth target feature"); + // We have 2 main isel alternatives: // - LDRAA/LDRAB, writeback or indexed. Zero disc, small offsets, D key. // - LDRA/LDRApre. Pointer needs to be in X16. diff --git a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp index 744bdd8543c7..3d510199128d 100644 --- a/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp +++ b/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp @@ -2856,6 +2856,9 @@ bool AArch64InstructionSelector::select(MachineInstr &I) { case AArch64::G_LDRA: case AArch64::G_LDRApre: + // TODO: implement a test case for this error + if (!STI.hasPAuth()) + report_fatal_error("pac instructions require ptrauth target feature"); return selectAuthLoad(I, MRI); case TargetOpcode::G_ZEXTLOAD: diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll index 23ffc8cdd392..7a51f09175b0 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -84,3 +84,20 @@ define void @bar(ptr %foo) #0 { } attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } + +;--- tryAuthLoad.ll + +; RUN: not --crash llc -mtriple aarch64-elf tryAuthLoad.ll 2>&1 | \ +; RUN: FileCheck tryAuthLoad.ll + +; CHECK: LLVM ERROR: pac instructions require ptrauth target feature + +define i64 @test(i64* %ptr) { + %tmp0 = ptrtoint i64* %ptr to i64 + %tmp1 = call i64 @llvm.ptrauth.auth(i64 %tmp0, i32 2, i64 0) + %tmp2 = inttoptr i64 %tmp1 to i64* + %tmp3 = load i64, i64* %tmp2 + ret i64 %tmp3 +} + +declare i64 @llvm.ptrauth.auth(i64, i32, i64) From 8e30a83d8afda9d3ac963717f1047d14e54247c1 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev Date: Sat, 9 Dec 2023 00:22:45 +0300 Subject: [PATCH 8/8] Use opaque pointers --- .../GlobalISel/ptrauth-lowering-err-debug.ll | 7 +++---- .../AArch64/GlobalISel/ptrauth-lowering-err.ll | 15 +++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll index 823640680ed7..e11edbb606b9 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err-debug.ll @@ -50,12 +50,11 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ; CHECK-NEXT: {{^$}} ; CHECK-NEXT: LLVM ERROR: pac instructions require ptrauth target feature -define i8* @foo() #0 { - %tmp = bitcast { i8*, i32, i64, i64 }* @g_weak.ptrauth to i8* - ret i8* %tmp +define ptr @foo() #0 { + ret ptr @g_weak.ptrauth } @g_weak = extern_weak global i32 -@g_weak.ptrauth = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_weak to i8*), i32 0, i64 0, i64 0 }, section "llvm.ptrauth" +@g_weak.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @g_weak, i32 0, i64 0, i64 0 }, section "llvm.ptrauth" attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll index 7a51f09175b0..ad43910c5ad5 100644 --- a/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll +++ b/llvm/test/CodeGen/AArch64/GlobalISel/ptrauth-lowering-err.ll @@ -43,13 +43,12 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ; CHECK: LLVM ERROR: pac instructions require ptrauth target feature -define i8* @foo() #0 { - %tmp = bitcast { i8*, i32, i64, i64 }* @g_weak.ptrauth to i8* - ret i8* %tmp +define ptr @foo() #0 { + ret ptr @g_weak.ptrauth } @g_weak = extern_weak global i32 -@g_weak.ptrauth = private constant { i8*, i32, i64, i64 } { i8* bitcast (i32* @g_weak to i8*), i32 0, i64 0, i64 0 }, section "llvm.ptrauth" +@g_weak.ptrauth = private constant { ptr, i32, i64, i64 } { ptr @g_weak, i32 0, i64 0, i64 0 }, section "llvm.ptrauth" attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } @@ -92,11 +91,11 @@ attributes #0 = { "ptrauth-calls" "target-cpu"="generic" } ; CHECK: LLVM ERROR: pac instructions require ptrauth target feature -define i64 @test(i64* %ptr) { - %tmp0 = ptrtoint i64* %ptr to i64 +define i64 @test(ptr %ptr) { + %tmp0 = ptrtoint ptr %ptr to i64 %tmp1 = call i64 @llvm.ptrauth.auth(i64 %tmp0, i32 2, i64 0) - %tmp2 = inttoptr i64 %tmp1 to i64* - %tmp3 = load i64, i64* %tmp2 + %tmp2 = inttoptr i64 %tmp1 to ptr + %tmp3 = load i64, ptr %tmp2 ret i64 %tmp3 }