[llvm] [llvm-exegesis][AArch64] Check for PAC keys before disabling them (PR #138643)

Abhilash Majumder via llvm-commits llvm-commits at lists.llvm.org
Tue May 13 09:11:16 PDT 2025


https://github.com/abhilash1910 updated https://github.com/llvm/llvm-project/pull/138643

>From a88910dccb1d13d5945d5d4ca09135c268461205 Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Tue, 6 May 2025 10:35:24 +0530
Subject: [PATCH 01/12] fix pac/pag

---
 .../llvm-exegesis/lib/AArch64/Target.cpp      | 24 +++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a1eb5a46f21fc..d1da6f41461a7 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -207,16 +207,26 @@ class ExegesisAArch64Target : public ExegesisTarget {
 
     if (isPointerAuth(Opcode)) {
 #if defined(__aarch64__) && defined(__linux__)
+      
+      // Fix for some systems where PAC/PAG keys are present but is 
+      // explicitly getting disabled
+      unsigned long pac_keys = 0;
+      if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
+        return "Failed to get PAC key status";
+      }
+      
       // Disable all PAC keys. Note that while we expect the measurements to
       // be the same with PAC keys disabled, they could potentially be lower
       // since authentication checks are bypassed.
-      if (prctl(PR_PAC_SET_ENABLED_KEYS,
-                PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
-                    PR_PAC_APDBKEY, // all keys
-                0,                  // disable all
-                0, 0) < 0) {
-        return "Failed to disable PAC keys";
-      }
+      if (pac_keys != 0) {
+        if (prctl(PR_PAC_SET_ENABLED_KEYS,
+                  PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
+                      PR_PAC_APDBKEY, // all keys
+                  0,                  // disable all
+                  0, 0) < 0) {
+          return "Failed to disable PAC keys";
+        }
+      } 
 #else
       return "Unsupported opcode: isPointerAuth";
 #endif

>From 009d3ba11c4e19da9c0bede0a0979d2f7f03611a Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Tue, 6 May 2025 16:12:57 +0530
Subject: [PATCH 02/12] review comments

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index d1da6f41461a7..33082621c11e4 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -207,14 +207,17 @@ class ExegesisAArch64Target : public ExegesisTarget {
 
     if (isPointerAuth(Opcode)) {
 #if defined(__aarch64__) && defined(__linux__)
-      
-      // Fix for some systems where PAC/PAG keys are present but is 
-      // explicitly getting disabled
+
+      // For some systems with existing PAC keys set, it is better to
+      // check the existing state of the key before setting it.
+      // For systems without PAC, this is a No-op but with PAC, it is
+      // safer to check the existing key state and then disable/enable them.
+      // Hence the guard placed for switching.
       unsigned long pac_keys = 0;
       if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
         return "Failed to get PAC key status";
       }
-      
+
       // Disable all PAC keys. Note that while we expect the measurements to
       // be the same with PAC keys disabled, they could potentially be lower
       // since authentication checks are bypassed.
@@ -226,7 +229,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
                   0, 0) < 0) {
           return "Failed to disable PAC keys";
         }
-      } 
+      }
 #else
       return "Unsupported opcode: isPointerAuth";
 #endif

>From 2c80855603699c6b37bcf6eab1cab98d7ec241e1 Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Wed, 7 May 2025 17:12:59 +0530
Subject: [PATCH 03/12] review comments

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 33082621c11e4..93c13dca31e5c 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -213,7 +213,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // For systems without PAC, this is a No-op but with PAC, it is
       // safer to check the existing key state and then disable/enable them.
       // Hence the guard placed for switching.
-      unsigned long pac_keys = 0;
+      unsigned long PacKeys = 0;
       if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
         return "Failed to get PAC key status";
       }
@@ -221,7 +221,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // Disable all PAC keys. Note that while we expect the measurements to
       // be the same with PAC keys disabled, they could potentially be lower
       // since authentication checks are bypassed.
-      if (pac_keys != 0) {
+      if (PacKeys != 0) {
         if (prctl(PR_PAC_SET_ENABLED_KEYS,
                   PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
                       PR_PAC_APDBKEY, // all keys

>From 2a9af0f62741e43dd3d6f760a47c0a60833a04bd Mon Sep 17 00:00:00 2001
From: abmajumder <abmajumder at nvidia.com>
Date: Wed, 7 May 2025 20:25:25 +0530
Subject: [PATCH 04/12] typo build error

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 93c13dca31e5c..7e6382693c3e9 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -212,9 +212,9 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // check the existing state of the key before setting it.
       // For systems without PAC, this is a No-op but with PAC, it is
       // safer to check the existing key state and then disable/enable them.
-      // Hence the guard placed for switching.
+      // Hence the guard for switching.
       unsigned long PacKeys = 0;
-      if (prctl(PR_PAC_GET_ENABLED_KEYS, &pac_keys, 0, 0, 0) < 0) {
+      if (prctl(PR_PAC_GET_ENABLED_KEYS, &PacKeys, 0, 0, 0) < 0) {
         return "Failed to get PAC key status";
       }
 

>From 7dde4a4a1d0f559d093405680587f9652b7e6174 Mon Sep 17 00:00:00 2001
From: abmajumder <debabhi1396 at gmail.com>
Date: Fri, 9 May 2025 14:19:33 +0530
Subject: [PATCH 05/12] add einval condition and reviews

---
 .../llvm-exegesis/lib/AArch64/Target.cpp      | 32 ++++++++++++++-----
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 7e6382693c3e9..a432eb57026d2 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -10,6 +10,7 @@
 #include "AArch64RegisterInfo.h"
 
 #if defined(__aarch64__) && defined(__linux__)
+#include <errno.h>
 #include <linux/prctl.h> // For PR_PAC_* constants
 #include <sys/prctl.h>
 #ifndef PR_PAC_SET_ENABLED_KEYS
@@ -199,6 +200,11 @@ class ExegesisAArch64Target : public ExegesisTarget {
     PM.add(createAArch64ExpandPseudoPass());
   }
 
+  static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0, long arg4 = 0,
+                            long arg5 = 0) {
+    return prctl(op, arg2, arg3, arg4, arg5);
+  }
+
   const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,
                                            unsigned Opcode) const override {
     if (const char *Reason =
@@ -213,20 +219,30 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // For systems without PAC, this is a No-op but with PAC, it is
       // safer to check the existing key state and then disable/enable them.
       // Hence the guard for switching.
-      unsigned long PacKeys = 0;
-      if (prctl(PR_PAC_GET_ENABLED_KEYS, &PacKeys, 0, 0, 0) < 0) {
+      errno = 0;
+      unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS,
+                                            0,  // unused
+                                            0,  // unused
+                                            0,  // unused
+                                            0); // unused
+      if ((long)PacKeys < 0) {
+        if (errno == EINVAL) {
+          return "PAuth not supported on this system";
+        }
         return "Failed to get PAC key status";
       }
 
       // Disable all PAC keys. Note that while we expect the measurements to
       // be the same with PAC keys disabled, they could potentially be lower
       // since authentication checks are bypassed.
-      if (PacKeys != 0) {
-        if (prctl(PR_PAC_SET_ENABLED_KEYS,
-                  PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
-                      PR_PAC_APDBKEY, // all keys
-                  0,                  // disable all
-                  0, 0) < 0) {
+      if ((long)PacKeys != 0) {
+        if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS,
+                          PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
+                              PR_PAC_APDBKEY, // all keys
+                          0,                  // disable all
+                          0,                  // unused
+                          0)                  // unused
+            < 0) {
           return "Failed to disable PAC keys";
         }
       }

>From 079d268eda9d7d6acb000c71812b14f33a1a46f7 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <30946547+abhilash1910 at users.noreply.github.com>
Date: Mon, 12 May 2025 18:15:47 +0530
Subject: [PATCH 06/12] Update llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp

Co-authored-by: Anatoly Trosinenko <anatoly.trosinenko at gmail.com>
---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a432eb57026d2..a1583d13fe4ac 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -220,11 +220,7 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // safer to check the existing key state and then disable/enable them.
       // Hence the guard for switching.
       errno = 0;
-      unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS,
-                                            0,  // unused
-                                            0,  // unused
-                                            0,  // unused
-                                            0); // unused
+      unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
       if ((long)PacKeys < 0) {
         if (errno == EINVAL) {
           return "PAuth not supported on this system";

>From 635c646010aa773c8e9372181d6e6d87838adf7c Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <30946547+abhilash1910 at users.noreply.github.com>
Date: Mon, 12 May 2025 18:15:57 +0530
Subject: [PATCH 07/12] Update llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp

Co-authored-by: Anatoly Trosinenko <anatoly.trosinenko at gmail.com>
---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index a1583d13fe4ac..2f4c191c11517 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -232,13 +232,13 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // be the same with PAC keys disabled, they could potentially be lower
       // since authentication checks are bypassed.
       if ((long)PacKeys != 0) {
-        if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS,
-                          PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY |
-                              PR_PAC_APDBKEY, // all keys
-                          0,                  // disable all
-                          0,                  // unused
-                          0)                  // unused
-            < 0) {
+        // Operate on all keys.
+        const long KeysToControl = 
+            PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
+        // Disable all.
+        const long EnabledBitMask = 0;
+        if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl, 
+                          EnabledBitMask) < 0) {
           return "Failed to disable PAC keys";
         }
       }

>From 7ca6ee78aeb00c84c25158062ceb2593631fb1d3 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:18:07 +0530
Subject: [PATCH 08/12] review updates

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 2f4c191c11517..2fe3ddf12c08a 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -200,9 +200,10 @@ class ExegesisAArch64Target : public ExegesisTarget {
     PM.add(createAArch64ExpandPseudoPass());
   }
 
-  static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0, long arg4 = 0,
-                            long arg5 = 0) {
-    return prctl(op, arg2, arg3, arg4, arg5);
+  // Converts variadic arguments to `long` and passes zeros for the unused
+  // arg2-arg5, as tested by the Linux kernel.
+  static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0) {
+    return prctl(op, arg2, arg3, /*arg4=*/0L, /*arg5=*/0L);
   }
 
   const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,

>From e1b6523da8d07025c1da1e42069884ac8a703bce Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:19:58 +0530
Subject: [PATCH 09/12] format fix

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index 2fe3ddf12c08a..bc4faedd0b806 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -234,11 +234,11 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // since authentication checks are bypassed.
       if ((long)PacKeys != 0) {
         // Operate on all keys.
-        const long KeysToControl = 
+        const long KeysToControl =
             PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY;
         // Disable all.
         const long EnabledBitMask = 0;
-        if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl, 
+        if (prctl_wrapper(PR_PAC_SET_ENABLED_KEYS, KeysToControl,
                           EnabledBitMask) < 0) {
           return "Failed to disable PAC keys";
         }

>From fd254b057fb34686d1f425592d57fe70a894fb44 Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Mon, 12 May 2025 18:22:20 +0530
Subject: [PATCH 10/12] format fix

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index bc4faedd0b806..feb798c8ea8d8 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -214,7 +214,6 @@ class ExegesisAArch64Target : public ExegesisTarget {
 
     if (isPointerAuth(Opcode)) {
 #if defined(__aarch64__) && defined(__linux__)
-
       // For some systems with existing PAC keys set, it is better to
       // check the existing state of the key before setting it.
       // For systems without PAC, this is a No-op but with PAC, it is

>From 42075c69605df3aaff814ce1f9ee7c3894e1c1ac Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 13 May 2025 21:07:24 +0530
Subject: [PATCH 11/12] use nullptr as silentcondition

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index feb798c8ea8d8..f9396cfdaaca7 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -200,11 +200,13 @@ class ExegesisAArch64Target : public ExegesisTarget {
     PM.add(createAArch64ExpandPseudoPass());
   }
 
+#if defined(__aarch64__) && defined(__linux__)
   // Converts variadic arguments to `long` and passes zeros for the unused
   // arg2-arg5, as tested by the Linux kernel.
   static long prctl_wrapper(int op, long arg2 = 0, long arg3 = 0) {
     return prctl(op, arg2, arg3, /*arg4=*/0L, /*arg5=*/0L);
   }
+#endif
 
   const char *getIgnoredOpcodeReasonOrNull(const LLVMState &State,
                                            unsigned Opcode) const override {
@@ -222,9 +224,8 @@ class ExegesisAArch64Target : public ExegesisTarget {
       errno = 0;
       unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
       if ((long)PacKeys < 0) {
-        if (errno == EINVAL) {
-          return "PAuth not supported on this system";
-        }
+        if (errno == EINVAL)
+          return nullptr;
         return "Failed to get PAC key status";
       }
 
@@ -241,9 +242,12 @@ class ExegesisAArch64Target : public ExegesisTarget {
                           EnabledBitMask) < 0) {
           return "Failed to disable PAC keys";
         }
+        llvm::errs() << "llvm-exegesis: PAC keys were disabled at runtime for "
+                        "benchmarking.\n";
       }
 #else
-      return "Unsupported opcode: isPointerAuth";
+      // Silently return nullptr to ensure forward progress
+      return nullptr;
 #endif
     }
 

>From 2b5c69ae09679bf0c7dc17f7876ead21a5a224da Mon Sep 17 00:00:00 2001
From: Abhilash Majumder <abmajumder at nvidia.com>
Date: Tue, 13 May 2025 21:40:41 +0530
Subject: [PATCH 12/12] use nullptr as silentcondition

---
 llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
index f9396cfdaaca7..96e0c9ec23a9d 100644
--- a/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/AArch64/Target.cpp
@@ -223,11 +223,8 @@ class ExegesisAArch64Target : public ExegesisTarget {
       // Hence the guard for switching.
       errno = 0;
       unsigned long PacKeys = prctl_wrapper(PR_PAC_GET_ENABLED_KEYS);
-      if ((long)PacKeys < 0) {
-        if (errno == EINVAL)
-          return nullptr;
-        return "Failed to get PAC key status";
-      }
+      if ((long)PacKeys < 0 || errno == EINVAL)
+        return nullptr;
 
       // Disable all PAC keys. Note that while we expect the measurements to
       // be the same with PAC keys disabled, they could potentially be lower



More information about the llvm-commits mailing list