Skip to content

Commit 71d8526

Browse files
committed
[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.
1 parent 23a674d commit 71d8526

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,8 @@ static void getTrivialDefaultFunctionAttributes(
21002100

21012101
TargetInfo::BranchProtectionInfo BPI(LangOpts);
21022102
TargetCodeGenInfo::initBranchProtectionFnAttributes(BPI, FuncAttrs);
2103+
TargetCodeGenInfo::initPointerAuthFnAttributes(CodeGenOpts.PointerAuth,
2104+
FuncAttrs);
21032105
}
21042106

21052107
/// Merges `target-features` from \TargetOpts and \F, and sets the result in

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -890,19 +890,6 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
890890
FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
891891
SanOpts.Mask &= ~SanitizerKind::Null;
892892

893-
// Add pointer authentication attributes.
894-
const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
895-
if (CodeGenOpts.PointerAuth.ReturnAddresses)
896-
Fn->addFnAttr("ptrauth-returns");
897-
if (CodeGenOpts.PointerAuth.FunctionPointers)
898-
Fn->addFnAttr("ptrauth-calls");
899-
if (CodeGenOpts.PointerAuth.AuthTraps)
900-
Fn->addFnAttr("ptrauth-auth-traps");
901-
if (CodeGenOpts.PointerAuth.IndirectGotos)
902-
Fn->addFnAttr("ptrauth-indirect-gotos");
903-
if (CodeGenOpts.PointerAuth.AArch64JumpTableHardening)
904-
Fn->addFnAttr("aarch64-jump-table-hardening");
905-
906893
// Apply xray attributes to the function (as a string, for now)
907894
bool AlwaysXRayAttr = false;
908895
if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ void TargetCodeGenInfo::initBranchProtectionFnAttributes(
258258
FuncAttrs.addAttribute("guarded-control-stack");
259259
}
260260

261+
void TargetCodeGenInfo::initPointerAuthFnAttributes(
262+
const PointerAuthOptions &Opts, llvm::AttrBuilder &FuncAttrs) {
263+
if (Opts.ReturnAddresses)
264+
FuncAttrs.addAttribute("ptrauth-returns");
265+
if (Opts.FunctionPointers)
266+
FuncAttrs.addAttribute("ptrauth-calls");
267+
if (Opts.AuthTraps)
268+
FuncAttrs.addAttribute("ptrauth-auth-traps");
269+
if (Opts.IndirectGotos)
270+
FuncAttrs.addAttribute("ptrauth-indirect-gotos");
271+
if (Opts.AArch64JumpTableHardening)
272+
FuncAttrs.addAttribute("aarch64-jump-table-hardening");
273+
}
274+
261275
namespace {
262276
class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
263277
public:

clang/lib/CodeGen/TargetInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ class TargetCodeGenInfo {
455455
initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
456456
llvm::AttrBuilder &FuncAttrs);
457457

458+
// Add the ptrauth-* Attributes to the FuncAttrs.
459+
static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts,
460+
llvm::AttrBuilder &FuncAttrs);
461+
458462
protected:
459463
static std::string qualifyWindowsLibrary(StringRef Lib);
460464

clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// RUN: %clang_cc1 -triple aarch64 -mbranch-target-enforce -msign-return-address=all -fcxx-exceptions -fexceptions -emit-llvm %s -o - | FileCheck %s
1+
// RUN: %clang_cc1 -triple aarch64 -mbranch-target-enforce -msign-return-address=all \
2+
// RUN: -fcxx-exceptions -fexceptions -emit-llvm %s -o - \
3+
// RUN: | FileCheck --check-prefixes=CHECK,BTE-SIGNRA %s
4+
// RUN: %clang_cc1 -triple aarch64 -fptrauth-calls -fptrauth-returns -fptrauth-auth-traps -fptrauth-indirect-gotos \
5+
// RUN: -fcxx-exceptions -fexceptions -emit-llvm %s -o - \
6+
// RUN: | FileCheck --check-prefixes=CHECK,PAUTHTEST %s
27

38
// Check that functions generated by clang have the correct attributes
49

@@ -26,5 +31,7 @@ int testfn() noexcept {
2631
// CHECK: define {{.*}} @_ZTW4var2() [[ATTR1]]
2732
// CHECK: define {{.*}} @__tls_init() [[ATTR1]]
2833

29-
// CHECK: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
30-
// CHECK: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
34+
// BTE-SIGNRA: attributes [[ATTR1]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
35+
// BTE-SIGNRA: attributes [[ATTR2]] = { {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
36+
// PAUTHTEST: attributes [[ATTR1]] = { {{.*}}"ptrauth-auth-traps" "ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"
37+
// PAUTHTEST: attributes [[ATTR2]] = { {{.*}}"ptrauth-auth-traps" "ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"

0 commit comments

Comments
 (0)