Skip to content

Commit f10a905

Browse files
authored
[clang][AArch64] Move initialization of ptrauth-* function attrs (#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 handle both groups in getDefaultFunctionAttributes() and setTargetAttributes() functions to prevent getting them out of sync. This fixes C++ TLS wrappers.
1 parent c9d6249 commit f10a905

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,11 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
22162216
llvm::AttrBuilder &FuncAttrs) {
22172217
getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,
22182218
FuncAttrs);
2219+
2220+
if (!AttrOnCallSite)
2221+
TargetCodeGenInfo::initPointerAuthFnAttributes(CodeGenOpts.PointerAuth,
2222+
FuncAttrs);
2223+
22192224
// If we're just getting the default, get the default values for mergeable
22202225
// attributes.
22212226
if (!AttrOnCallSite)

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,35 @@ void TargetCodeGenInfo::initBranchProtectionFnAttributes(
258258
FuncAttrs.addAttribute("guarded-control-stack");
259259
}
260260

261+
void TargetCodeGenInfo::setPointerAuthFnAttributes(
262+
const PointerAuthOptions &Opts, llvm::Function &F) {
263+
auto UpdateAttr = [&F](bool AttrShouldExist, StringRef AttrName) {
264+
if (AttrShouldExist && !F.hasFnAttribute(AttrName))
265+
F.addFnAttr(AttrName);
266+
if (!AttrShouldExist && F.hasFnAttribute(AttrName))
267+
F.removeFnAttr(AttrName);
268+
};
269+
UpdateAttr(Opts.ReturnAddresses, "ptrauth-returns");
270+
UpdateAttr((bool)Opts.FunctionPointers, "ptrauth-calls");
271+
UpdateAttr(Opts.AuthTraps, "ptrauth-auth-traps");
272+
UpdateAttr(Opts.IndirectGotos, "ptrauth-indirect-gotos");
273+
UpdateAttr(Opts.AArch64JumpTableHardening, "aarch64-jump-table-hardening");
274+
}
275+
276+
void TargetCodeGenInfo::initPointerAuthFnAttributes(
277+
const PointerAuthOptions &Opts, llvm::AttrBuilder &FuncAttrs) {
278+
if (Opts.ReturnAddresses)
279+
FuncAttrs.addAttribute("ptrauth-returns");
280+
if (Opts.FunctionPointers)
281+
FuncAttrs.addAttribute("ptrauth-calls");
282+
if (Opts.AuthTraps)
283+
FuncAttrs.addAttribute("ptrauth-auth-traps");
284+
if (Opts.IndirectGotos)
285+
FuncAttrs.addAttribute("ptrauth-indirect-gotos");
286+
if (Opts.AArch64JumpTableHardening)
287+
FuncAttrs.addAttribute("aarch64-jump-table-hardening");
288+
}
289+
261290
namespace {
262291
class DefaultTargetCodeGenInfo : public TargetCodeGenInfo {
263292
public:

clang/lib/CodeGen/TargetInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,15 @@ class TargetCodeGenInfo {
459459
initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
460460
llvm::AttrBuilder &FuncAttrs);
461461

462+
// Set the ptrauth-* attributes of the Function accordingly to the Opts.
463+
// Remove attributes that contradict with current Opts.
464+
static void setPointerAuthFnAttributes(const PointerAuthOptions &Opts,
465+
llvm::Function &F);
466+
467+
// Add the ptrauth-* Attributes to the FuncAttrs.
468+
static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts,
469+
llvm::AttrBuilder &FuncAttrs);
470+
462471
protected:
463472
static std::string qualifyWindowsLibrary(StringRef Lib);
464473

clang/lib/CodeGen/Targets/AArch64.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
155155
}
156156
}
157157
setBranchProtectionFnAttributes(BPI, *Fn);
158+
setPointerAuthFnAttributes(CGM.getCodeGenOpts().PointerAuth, *Fn);
158159
}
159160

160161
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

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)