https://github.com/atrosinenko created 
https://github.com/llvm/llvm-project/pull/140277

Move the initialization of ptrauth-* function attributes near the 
initialization of branch protection attributes. The semantics of these groups 
of attributes partially overlaps, so initialize them both in 
getTrivialDefaultFunctionAttributes() function to prevent getting them out of 
sync. This fixes C++ TLS wrappers.

>From 71d852614cecab901709977b4a5f134145c2ac62 Mon Sep 17 00:00:00 2001
From: Anatoly Trosinenko <atrosine...@accesssoftek.com>
Date: Fri, 16 May 2025 16:32:42 +0300
Subject: [PATCH] [clang][AArch64] Move initialization of ptrauth-* function
 attrs

Move the initialization of ptrauth-* function attributes near the
initialization of branch protection attributes. The semantics of these
groups of attributes partially overlaps, so initialize them both in
getTrivialDefaultFunctionAttributes() function to prevent getting them
out of sync. This fixes C++ TLS wrappers.
---
 clang/lib/CodeGen/CGCall.cpp                      |  2 ++
 clang/lib/CodeGen/CodeGenFunction.cpp             | 13 -------------
 clang/lib/CodeGen/TargetInfo.cpp                  | 14 ++++++++++++++
 clang/lib/CodeGen/TargetInfo.h                    |  4 ++++
 clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp | 13 ++++++++++---
 5 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index bcd579454413e..8064c935de216 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -2100,6 +2100,8 @@ static void getTrivialDefaultFunctionAttributes(
 
   TargetInfo::BranchProtectionInfo BPI(LangOpts);
   TargetCodeGenInfo::initBranchProtectionFnAttributes(BPI, FuncAttrs);
+  TargetCodeGenInfo::initPointerAuthFnAttributes(CodeGenOpts.PointerAuth,
+                                                 FuncAttrs);
 }
 
 /// Merges `target-features` from \TargetOpts and \F, and sets the result in
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp 
b/clang/lib/CodeGen/CodeGenFunction.cpp
index ac40aab97820d..4e79cdf0ef089 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -890,19 +890,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, 
QualType RetTy,
         FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
       SanOpts.Mask &= ~SanitizerKind::Null;
 
-  // Add pointer authentication attributes.
-  const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
-  if (CodeGenOpts.PointerAuth.ReturnAddresses)
-    Fn->addFnAttr("ptrauth-returns");
-  if (CodeGenOpts.PointerAuth.FunctionPointers)
-    Fn->addFnAttr("ptrauth-calls");
-  if (CodeGenOpts.PointerAuth.AuthTraps)
-    Fn->addFnAttr("ptrauth-auth-traps");
-  if (CodeGenOpts.PointerAuth.IndirectGotos)
-    Fn->addFnAttr("ptrauth-indirect-gotos");
-  if (CodeGenOpts.PointerAuth.AArch64JumpTableHardening)
-    Fn->addFnAttr("aarch64-jump-table-hardening");
-
   // Apply xray attributes to the function (as a string, for now)
   bool AlwaysXRayAttr = false;
   if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {
diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index 75a7d3c7c73f0..564413228e34a 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -258,6 +258,20 @@ void TargetCodeGenInfo::initBranchProtectionFnAttributes(
     FuncAttrs.addAttribute("guarded-control-stack");
 }
 
+void TargetCodeGenInfo::initPointerAuthFnAttributes(
+    const PointerAuthOptions &Opts, llvm::AttrBuilder &FuncAttrs) {
+  if (Opts.ReturnAddresses)
+    FuncAttrs.addAttribute("ptrauth-returns");
+  if (Opts.FunctionPointers)
+    FuncAttrs.addAttribute("ptrauth-calls");
+  if (Opts.AuthTraps)
+    FuncAttrs.addAttribute("ptrauth-auth-traps");
+  if (Opts.IndirectGotos)
+    FuncAttrs.addAttribute("ptrauth-indirect-gotos");
+  if (Opts.AArch64JumpTableHardening)
+    FuncAttrs.addAttribute("aarch64-jump-table-hardening");
+}
+
 namespace {
 class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
 public:
diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h
index dd5dc0c32bd71..7415747c4ffd7 100644
--- a/clang/lib/CodeGen/TargetInfo.h
+++ b/clang/lib/CodeGen/TargetInfo.h
@@ -455,6 +455,10 @@ class TargetCodeGenInfo {
   initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
                                    llvm::AttrBuilder &FuncAttrs);
 
+  // Add the ptrauth-* Attributes to the FuncAttrs.
+  static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts,
+                                          llvm::AttrBuilder &FuncAttrs);
+
 protected:
   static std::string qualifyWindowsLibrary(StringRef Lib);
 
diff --git a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp 
b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
index 18d9da40d469b..630b5edbbc1d7 100644
--- a/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
+++ b/clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
@@ -1,4 +1,9 @@
-// RUN: %clang_cc1 -triple aarch64 -mbranch-target-enforce 
-msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | 
FileCheck %s
+// RUN: %clang_cc1 -triple aarch64 -mbranch-target-enforce 
-msign-return-address=all \
+// RUN:            -fcxx-exceptions -fexceptions -emit-llvm %s -o - \
+// RUN:   | FileCheck --check-prefixes=CHECK,BTE-SIGNRA %s
+// RUN: %clang_cc1 -triple aarch64 -fptrauth-calls -fptrauth-returns 
-fptrauth-auth-traps -fptrauth-indirect-gotos \
+// RUN:            -fcxx-exceptions -fexceptions -emit-llvm %s -o - \
+// RUN:   | FileCheck --check-prefixes=CHECK,PAUTHTEST %s
 
 // Check that functions generated by clang have the correct attributes
 
@@ -26,5 +31,7 @@ int testfn() noexcept {
 // CHECK: define {{.*}} @_ZTW4var2() [[ATTR1]]
 // CHECK: define {{.*}} @__tls_init() [[ATTR1]]
 
-// 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"
+// BTE-SIGNRA: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement" 
{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// BTE-SIGNRA: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement" 
{{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
+// PAUTHTEST: attributes [[ATTR1]] = { {{.*}}"ptrauth-auth-traps" 
"ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"
+// PAUTHTEST: attributes [[ATTR2]] = { {{.*}}"ptrauth-auth-traps" 
"ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to