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

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 28 08:47:46 PST 2024


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

>From 46f0334eb5c5edd15b657429f39f588cc7726072 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/2] 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 d05cf1c6e1814e..d677a2e64be77e 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2022,6 +2022,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 00000000000000..8daf44abd4f91c
--- /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 97382a9ff63100e357e56e7f08ad2b5ac70d1e89 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/2] 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 d677a2e64be77e..9bcf8801bc8df6 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2027,10 +2027,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");



More information about the cfe-commits mailing list