Skip to content

Commit 04cd235

Browse files
committed
[CodeGen] Make sure pointer authentication function attributes are added
to function definitions. The pointer authentication attributes weren't being added to the definition of std::terminate when its declaration was added to the IR as a runtime function before its definition was emitted. In that case, getDefaultFunctionAttributes doesn't get called. rdar://problem/71053329
1 parent c3f8a3a commit 04cd235

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,14 +1810,6 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
18101810
FuncAttrs.addAttribute("stackrealign");
18111811
if (CodeGenOpts.Backchain)
18121812
FuncAttrs.addAttribute("backchain");
1813-
if (CodeGenOpts.PointerAuth.ReturnAddresses)
1814-
FuncAttrs.addAttribute("ptrauth-returns");
1815-
if (CodeGenOpts.PointerAuth.FunctionPointers)
1816-
FuncAttrs.addAttribute("ptrauth-calls");
1817-
if (CodeGenOpts.PointerAuth.IndirectGotos)
1818-
FuncAttrs.addAttribute("ptrauth-indirect-gotos");
1819-
if (CodeGenOpts.PointerAuth.AuthTraps)
1820-
FuncAttrs.addAttribute("ptrauth-auth-traps");
18211813
if (CodeGenOpts.EnableSegmentedStacks)
18221814
FuncAttrs.addAttribute("split-stack");
18231815

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,17 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
791791
FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass)
792792
SanOpts.Mask &= ~SanitizerKind::Null;
793793

794+
// Add pointer authentication attributes.
795+
const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
796+
if (CodeGenOpts.PointerAuth.ReturnAddresses)
797+
Fn->addFnAttr("ptrauth-returns");
798+
if (CodeGenOpts.PointerAuth.FunctionPointers)
799+
Fn->addFnAttr("ptrauth-calls");
800+
if (CodeGenOpts.PointerAuth.IndirectGotos)
801+
Fn->addFnAttr("ptrauth-indirect-gotos");
802+
if (CodeGenOpts.PointerAuth.AuthTraps)
803+
Fn->addFnAttr("ptrauth-auth-traps");
804+
794805
// Apply xray attributes to the function (as a string, for now)
795806
bool AlwaysXRayAttr = false;
796807
if (const auto *XRayAttr = D ? D->getAttr<XRayInstrumentAttr>() : nullptr) {

clang/test/CodeGenCXX/ptrauth.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -fptrauth-returns -fptrauth-intrinsics -emit-llvm -std=c++11 -fexceptions -fcxx-exceptions -o - %s | FileCheck %s
2+
3+
void foo1();
4+
5+
void test_terminate() noexcept {
6+
foo1();
7+
}
8+
9+
// CHECK: define void @_ZSt9terminatev() #[[ATTR4:.*]] {
10+
11+
namespace std {
12+
void terminate() noexcept {
13+
}
14+
}
15+
16+
// CHECK: attributes #[[ATTR4]] = {{{.*}}"ptrauth-calls" "ptrauth-returns"{{.*}}}

0 commit comments

Comments
 (0)