[clang] [Clang][ARM][AArch64] Add branch protection attributes to the defaults. (PR #83277)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 4 08:34:26 PST 2024
https://github.com/DanielKristofKiss updated https://github.com/llvm/llvm-project/pull/83277
>From c421b6b9c167e82cedc5db2a67f47d3ba12deba9 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/5] 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 13f68237b464d60..5b59c7735367522 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 000000000000000..8daf44abd4f91c3
--- /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 3e46bfa6bcd8a0cce142a4e1254c89ff68174117 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/5] 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 5b59c7735367522..58ccd3d181ccdf8 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");
>From 054515d8b3b50b3efc792db9dfe4c9e61fa2507d 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/5] Move TargetInfo changes over.
Dropping restrictions on the member functions.
---
clang/include/clang/Basic/TargetInfo.h | 43 +++++++++++++++++++++++---
1 file changed, 39 insertions(+), 4 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 7682f84e491c7bc..9eeeecd68147f87 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -31,7 +31,9 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Frontend/OpenMP/OMPGridValues.h"
+#include "llvm/IR/Attributes.h"
#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Function.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/VersionTuple.h"
@@ -1368,15 +1370,14 @@ class TargetInfo : public TransferrableTargetInfo,
return StringRef();
}
- struct BranchProtectionInfo {
+ class BranchProtectionInfo {
+ public:
LangOptions::SignReturnAddressScopeKind SignReturnAddr;
LangOptions::SignReturnAddressKeyKind SignKey;
bool BranchTargetEnforcement;
bool BranchProtectionPAuthLR;
bool GuardedControlStack;
- BranchProtectionInfo() = default;
-
const char *getSignReturnAddrStr() const {
switch (SignReturnAddr) {
case LangOptions::SignReturnAddressScopeKind::None:
@@ -1388,7 +1389,6 @@ class TargetInfo : public TransferrableTargetInfo,
}
llvm_unreachable("Unexpected SignReturnAddressScopeKind");
}
-
const char *getSignKeyStr() const {
switch (SignKey) {
case LangOptions::SignReturnAddressKeyKind::AKey:
@@ -1398,6 +1398,41 @@ class TargetInfo : public TransferrableTargetInfo,
}
llvm_unreachable("Unexpected SignReturnAddressKeyKind");
}
+
+ BranchProtectionInfo() = default;
+ BranchProtectionInfo(const LangOptions &LangOpts) {
+ SignReturnAddr =
+ LangOpts.hasSignReturnAddress()
+ ? (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;
+ }
+
+ void setFnAttributes(llvm::Function &F) {
+ llvm::AttrBuilder FuncAttrs(F.getContext());
+ setFnAttributes(FuncAttrs);
+ F.addFnAttrs(FuncAttrs);
+ }
+
+ void setFnAttributes(llvm::AttrBuilder &FuncAttrs) {
+ if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
+ FuncAttrs.addAttribute("sign-return-address", getSignReturnAddrStr());
+ FuncAttrs.addAttribute("sign-return-address-key", getSignKeyStr());
+ }
+ if (BranchTargetEnforcement)
+ FuncAttrs.addAttribute("branch-target-enforcement", "true");
+ if (BranchProtectionPAuthLR)
+ FuncAttrs.addAttribute("branch-protection-pauth-lr", "true");
+ if (GuardedControlStack)
+ FuncAttrs.addAttribute("guarded-control-stack", "true");
+ }
};
/// Determine if the Architecture in this TargetInfo supports branch
>From 23490c21d4c49b8ec9ba653380af90b33989dfa4 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/5] 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 58ccd3d181ccdf8..8f13b84908805bf 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2024,17 +2024,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 e216b669622662cf28ecbd3f597fb8bb7cca9bf9 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/5] 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 8daf44abd4f91c3..de35c32e9c18afc 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
More information about the cfe-commits
mailing list