Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,11 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
llvm::AttrBuilder &FuncAttrs) {
getTrivialDefaultFunctionAttributes(Name, HasOptnone, AttrOnCallSite,
FuncAttrs);

if (!AttrOnCallSite)
TargetCodeGenInfo::initPointerAuthFnAttributes(CodeGenOpts.PointerAuth,
FuncAttrs);

// If we're just getting the default, get the default values for mergeable
// attributes.
if (!AttrOnCallSite)
Expand Down
13 changes: 0 additions & 13 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions clang/lib/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,35 @@ void TargetCodeGenInfo::initBranchProtectionFnAttributes(
FuncAttrs.addAttribute("guarded-control-stack");
}

void TargetCodeGenInfo::setPointerAuthFnAttributes(
const PointerAuthOptions &Opts, llvm::Function &F) {
auto UpdateAttr = [&F](bool AttrShouldExist, StringRef AttrName) {
if (AttrShouldExist && !F.hasFnAttribute(AttrName))
F.addFnAttr(AttrName);
if (!AttrShouldExist && F.hasFnAttribute(AttrName))
F.removeFnAttr(AttrName);
};
UpdateAttr(Opts.ReturnAddresses, "ptrauth-returns");
UpdateAttr((bool)Opts.FunctionPointers, "ptrauth-calls");
UpdateAttr(Opts.AuthTraps, "ptrauth-auth-traps");
UpdateAttr(Opts.IndirectGotos, "ptrauth-indirect-gotos");
UpdateAttr(Opts.AArch64JumpTableHardening, "aarch64-jump-table-hardening");
}

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:
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,15 @@ class TargetCodeGenInfo {
initBranchProtectionFnAttributes(const TargetInfo::BranchProtectionInfo &BPI,
llvm::AttrBuilder &FuncAttrs);

// Set the ptrauth-* attributes of the Function accordingly to the Opts.
// Remove attributes that contradict with current Opts.
static void setPointerAuthFnAttributes(const PointerAuthOptions &Opts,
llvm::Function &F);

// Add the ptrauth-* Attributes to the FuncAttrs.
static void initPointerAuthFnAttributes(const PointerAuthOptions &Opts,
llvm::AttrBuilder &FuncAttrs);

protected:
static std::string qualifyWindowsLibrary(StringRef Lib);

Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/Targets/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
}
}
setBranchProtectionFnAttributes(BPI, *Fn);
setPointerAuthFnAttributes(CGM.getCodeGenOpts().PointerAuth, *Fn);
}

bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,
Expand Down
13 changes: 10 additions & 3 deletions clang/test/CodeGenCXX/arm64-generated-fn-attr.cpp
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"