[clang] [Clang][ARM][AArch64] Add branch protection attributes to the defaults. (PR #83277)

Daniel Kiss via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 02:05:13 PDT 2024


https://github.com/DanielKristofKiss updated https://github.com/llvm/llvm-project/pull/83277

>From 0c3118713387246dc1c503f3792ba5af82e6b5eb Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Wed, 28 Feb 2024 15:18:31 +0100
Subject: [PATCH 1/8] Add branch protection attributes to the defaults.

These attributes are no longer inherited from the module flags,
therefore need to be added for synthetic functions.
---
 clang/lib/CodeGen/CGCall.cpp                  | 16 ++++++++++
 .../CodeGenCXX/arm64-generated-fn-attr.cpp    | 30 +++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 7e7b2b395f7d6..7b93f6d2dded1 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2030,6 +2030,22 @@ static void getTrivialDefaultFunctionAttributes(
     std::tie(Var, Value) = Attr.split('=');
     FuncAttrs.addAttribute(Var, Value);
   }
+
+  TargetInfo::BranchProtectionInfo BPI(LangOpts);
+
+  if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
+    FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
+    FuncAttrs.addAttribute(
+        "sign-return-address-key",
+        BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey ? "a_key"
+                                                                   : "b_key");
+  }
+  if (BPI.BranchTargetEnforcement)
+    FuncAttrs.addAttribute("branch-target-enforcement", "true");
+  if (BPI.BranchProtectionPAuthLR)
+    FuncAttrs.addAttribute("branch-protection-pauth-lr", "true");
+  if (BPI.GuardedControlStack)
+    FuncAttrs.addAttribute("guarded-control-stack", "true");
 }
 
 /// Merges `target-features` from \TargetOpts and \F, and sets the result in
diff --git a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
new file mode 100644
index 0000000000000..8daf44abd4f91
--- /dev/null
+++ b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -triple aarch64-none-none -mbranch-target-enforce -msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK
+
+// Check that functions generated by clang have the correct attributes
+
+class Example {
+public:
+  Example();
+  int fn();
+};
+
+// Initialization of var1 causes __cxx_global_var_init and __tls_init to be generated
+thread_local Example var1;
+extern thread_local Example var2;
+extern void fn();
+
+int testfn() noexcept {
+  // Calling fn in a noexcept function causes __clang_call_terminate to be generated
+  fn();
+  // Use of var1 and var2 causes TLS wrapper functions to be generated
+  return var1.fn() + var2.fn();
+}
+
+// CHECK: define {{.*}} @__cxx_global_var_init() [[ATTR1:#[0-9]+]]
+// CHECK: define {{.*}} @__clang_call_terminate({{.*}}) [[ATTR2:#[0-9]+]]
+// CHECK: define {{.*}} @_ZTW4var1() [[ATTR1]]
+// CHECK: define {{.*}} @_ZTW4var2() [[ATTR1]]
+// CHECK: define {{.*}} @__tls_init() [[ATTR1]]
+
+// CHECK: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"

>From 0e2a9e129bcb4f569a443494b9266ae482c73c94 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Wed, 28 Feb 2024 17:38:49 +0100
Subject: [PATCH 2/8] fixup! [Clang][Arm][AArch64] Add branch protection
 attributes to the defaults.

---
 clang/lib/CodeGen/CGCall.cpp | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 7b93f6d2dded1..131666d732b3d 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2035,10 +2035,7 @@ static void getTrivialDefaultFunctionAttributes(
 
   if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
     FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
-    FuncAttrs.addAttribute(
-        "sign-return-address-key",
-        BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey ? "a_key"
-                                                                   : "b_key");
+    FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());
   }
   if (BPI.BranchTargetEnforcement)
     FuncAttrs.addAttribute("branch-target-enforcement", "true");

>From 735d24d5fe8bb4f37d87cb2345cbe45d53b1ba42 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Mon, 4 Mar 2024 17:31:31 +0100
Subject: [PATCH 3/8] Move TargetInfo changes over.

Dropping restrictions on the member functions.
---
 clang/include/clang/Basic/TargetInfo.h | 15 ---------------
 1 file changed, 15 deletions(-)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 1f208b40f92cb..d8075ee97972e 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1415,14 +1415,10 @@ class TargetInfo : public TransferrableTargetInfo,
       switch (SignReturnAddr) {
       case LangOptions::SignReturnAddressScopeKind::None:
         return "none";
-      case LangOptions::SignReturnAddressScopeKind::NonLeaf:
         return "non-leaf";
-      case LangOptions::SignReturnAddressScopeKind::All:
-        return "all";
       }
       llvm_unreachable("Unexpected SignReturnAddressScopeKind");
     }
-
     const char *getSignKeyStr() const {
       switch (SignKey) {
       case LangOptions::SignReturnAddressKeyKind::AKey:
@@ -1441,10 +1437,7 @@ class TargetInfo : public TransferrableTargetInfo,
               ? (LangOpts.isSignReturnAddressScopeAll()
                      ? LangOptions::SignReturnAddressScopeKind::All
                      : LangOptions::SignReturnAddressScopeKind::NonLeaf)
-              : LangOptions::SignReturnAddressScopeKind::None;
       SignKey = LangOpts.isSignReturnAddressWithAKey()
-                    ? LangOptions::SignReturnAddressKeyKind::AKey
-                    : LangOptions::SignReturnAddressKeyKind::BKey;
       BranchTargetEnforcement = LangOpts.BranchTargetEnforcement;
       BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
       GuardedControlStack = LangOpts.GuardedControlStack;
@@ -1468,19 +1461,11 @@ class TargetInfo : public TransferrableTargetInfo,
       if (GuardedControlStack)
         FuncAttrs.addAttribute("guarded-control-stack");
     }
-  };
 
   /// Determine if the Architecture in this TargetInfo supports branch
   /// protection
   virtual bool isBranchProtectionSupportedArch(StringRef Arch) const {
     return false;
-  }
-
-  /// Determine if this TargetInfo supports the given branch protection
-  /// specification
-  virtual bool validateBranchProtection(StringRef Spec, StringRef Arch,
-                                        BranchProtectionInfo &BPI,
-                                        StringRef &Err) const {
     Err = "";
     return false;
   }

>From 911273af8542f94546646d398cfa674b98edd517 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Mon, 4 Mar 2024 17:32:41 +0100
Subject: [PATCH 4/8] Simplify the code.

---
 clang/lib/CodeGen/CGCall.cpp | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 131666d732b3d..8404954577417 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2032,17 +2032,7 @@ static void getTrivialDefaultFunctionAttributes(
   }
 
   TargetInfo::BranchProtectionInfo BPI(LangOpts);
-
-  if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
-    FuncAttrs.addAttribute("sign-return-address", BPI.getSignReturnAddrStr());
-    FuncAttrs.addAttribute("sign-return-address-key", BPI.getSignKeyStr());
-  }
-  if (BPI.BranchTargetEnforcement)
-    FuncAttrs.addAttribute("branch-target-enforcement", "true");
-  if (BPI.BranchProtectionPAuthLR)
-    FuncAttrs.addAttribute("branch-protection-pauth-lr", "true");
-  if (BPI.GuardedControlStack)
-    FuncAttrs.addAttribute("guarded-control-stack", "true");
+  BPI.setFnAttributes(FuncAttrs);
 }
 
 /// Merges `target-features` from \TargetOpts and \F, and sets the result in

>From 30c29d2d024b9dae1757ed04da95ba9583f3f763 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Mon, 4 Mar 2024 17:32:59 +0100
Subject: [PATCH 5/8] Simplify the test invocation

---
 clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
index 8daf44abd4f91..de35c32e9c18a 100644
--- a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
+++ b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple aarch64-none-none -mbranch-target-enforce -msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK
+// RUN: %clang_cc1 -triple aarch64 -mbranch-target-enforce -msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | FileCheck %s
 
 // Check that functions generated by clang have the correct attributes
 

>From 287fab911e67f1c1423282a38b77b77baecc1903 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Wed, 10 Jul 2024 16:44:01 +0200
Subject: [PATCH 6/8] drop TargetInfo changes

---
 clang/include/clang/Basic/TargetInfo.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index d8075ee97972e..1f208b40f92cb 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -1415,10 +1415,14 @@ class TargetInfo : public TransferrableTargetInfo,
       switch (SignReturnAddr) {
       case LangOptions::SignReturnAddressScopeKind::None:
         return "none";
+      case LangOptions::SignReturnAddressScopeKind::NonLeaf:
         return "non-leaf";
+      case LangOptions::SignReturnAddressScopeKind::All:
+        return "all";
       }
       llvm_unreachable("Unexpected SignReturnAddressScopeKind");
     }
+
     const char *getSignKeyStr() const {
       switch (SignKey) {
       case LangOptions::SignReturnAddressKeyKind::AKey:
@@ -1437,7 +1441,10 @@ class TargetInfo : public TransferrableTargetInfo,
               ? (LangOpts.isSignReturnAddressScopeAll()
                      ? LangOptions::SignReturnAddressScopeKind::All
                      : LangOptions::SignReturnAddressScopeKind::NonLeaf)
+              : LangOptions::SignReturnAddressScopeKind::None;
       SignKey = LangOpts.isSignReturnAddressWithAKey()
+                    ? LangOptions::SignReturnAddressKeyKind::AKey
+                    : LangOptions::SignReturnAddressKeyKind::BKey;
       BranchTargetEnforcement = LangOpts.BranchTargetEnforcement;
       BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
       GuardedControlStack = LangOpts.GuardedControlStack;
@@ -1461,11 +1468,19 @@ class TargetInfo : public TransferrableTargetInfo,
       if (GuardedControlStack)
         FuncAttrs.addAttribute("guarded-control-stack");
     }
+  };
 
   /// Determine if the Architecture in this TargetInfo supports branch
   /// protection
   virtual bool isBranchProtectionSupportedArch(StringRef Arch) const {
     return false;
+  }
+
+  /// Determine if this TargetInfo supports the given branch protection
+  /// specification
+  virtual bool validateBranchProtection(StringRef Spec, StringRef Arch,
+                                        BranchProtectionInfo &BPI,
+                                        StringRef &Err) const {
     Err = "";
     return false;
   }

>From e64847d840b27e287162a574aec281b016362966 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Fri, 12 Jul 2024 11:02:11 +0200
Subject: [PATCH 7/8] update test accorindg to upstream changes

---
 clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
index de35c32e9c18a..18d9da40d469b 100644
--- a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
+++ b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
@@ -26,5 +26,5 @@ int testfn() noexcept {
 // CHECK: define {{.*}} @_ZTW4var2() [[ATTR1]]
 // CHECK: define {{.*}} @__tls_init() [[ATTR1]]
 
-// CHECK: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
-// CHECK: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement"="true"{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// CHECK: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"

>From 12b7656d95b9397df3cab87dc78e1e4c4befc9d0 Mon Sep 17 00:00:00 2001
From: Daniel Kiss <daniel.kiss at arm.com>
Date: Thu, 11 Jul 2024 21:05:46 +0200
Subject: [PATCH 8/8] update as BranchProtectionInfo changes

---
 clang/lib/CodeGen/CGCall.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 8404954577417..a38484941ba24 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2032,7 +2032,7 @@ static void getTrivialDefaultFunctionAttributes(
   }
 
   TargetInfo::BranchProtectionInfo BPI(LangOpts);
-  BPI.setFnAttributes(FuncAttrs);
+  TargetCodeGenInfo::setBranchProtectionFnAttributes(BPI, FuncAttrs);
 }
 
 /// Merges `target-features` from \TargetOpts and \F, and sets the result in



More information about the cfe-commits mailing list