Skip to content

Commit 2eb6e30

Browse files
[clang] Wire -fptrauth-returns to "ptrauth-returns" fn attribute. (#102416)
We already ended up with -fptrauth-returns, the feature macro, the lang opt, and the actual backend lowering. The only part left is threading it all through PointerAuthOptions, to drive the addition of the "ptrauth-returns" attribute to generated functions. While there, do minor cleanup on ptrauth-function-attributes.c. This also adds ptrauth_key_return_address to ptrauth.h.
1 parent ca7ad38 commit 2eb6e30

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

clang/include/clang/Basic/PointerAuthOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ class PointerAuthSchema {
163163
};
164164

165165
struct PointerAuthOptions {
166+
/// Should return addresses be authenticated?
167+
bool ReturnAddresses = false;
168+
166169
/// Do indirect goto label addresses need to be authenticated?
167170
bool IndirectGotos = false;
168171

clang/lib/CodeGen/CodeGenFunction.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,8 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
880880

881881
// Add pointer authentication attributes.
882882
const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();
883+
if (CodeGenOpts.PointerAuth.ReturnAddresses)
884+
Fn->addFnAttr("ptrauth-returns");
883885
if (CodeGenOpts.PointerAuth.FunctionPointers)
884886
Fn->addFnAttr("ptrauth-calls");
885887
if (CodeGenOpts.PointerAuth.IndirectGotos)

clang/lib/Frontend/CompilerInvocation.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1511,13 +1511,15 @@ void CompilerInvocation::setDefaultPointerAuthOptions(
15111511
}
15121512
}
15131513
Opts.IndirectGotos = LangOpts.PointerAuthIndirectGotos;
1514+
Opts.ReturnAddresses = LangOpts.PointerAuthReturns;
15141515
}
15151516

15161517
static void parsePointerAuthOptions(PointerAuthOptions &Opts,
15171518
const LangOptions &LangOpts,
15181519
const llvm::Triple &Triple,
15191520
DiagnosticsEngine &Diags) {
1520-
if (!LangOpts.PointerAuthCalls && !LangOpts.PointerAuthIndirectGotos)
1521+
if (!LangOpts.PointerAuthCalls && !LangOpts.PointerAuthIndirectGotos &&
1522+
!LangOpts.PointerAuthReturns)
15211523
return;
15221524

15231525
CompilerInvocation::setDefaultPointerAuthOptions(Opts, LangOpts, Triple);

clang/lib/Headers/ptrauth.h

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ typedef enum {
2828
/* A process-specific key which can be used to sign data pointers. */
2929
ptrauth_key_process_dependent_data = ptrauth_key_asdb,
3030

31+
/* The key used to sign return addresses on the stack.
32+
The extra data is based on the storage address of the return address.
33+
On AArch64, that is always the storage address of the return address + 8
34+
(or, in other words, the value of the stack pointer on function entry) */
35+
ptrauth_key_return_address = ptrauth_key_process_dependent_code,
36+
3137
/* The key used to sign C function pointers.
3238
The extra data is always 0. */
3339
ptrauth_key_function_pointer = ptrauth_key_process_independent_code,
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
// RUN: %clang_cc1 -triple arm64-apple-ios -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,OFF
12
// RUN: %clang_cc1 -triple arm64e-apple-ios -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,OFF
23
// RUN: %clang_cc1 -triple aarch64-linux-gnu -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,OFF
34

4-
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CALLS
5+
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-calls -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CALLS
56
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-calls -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,CALLS
67

8+
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-returns -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,RETS
9+
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-returns -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,RETS
10+
711
// RUN: %clang_cc1 -triple arm64-apple-ios -fptrauth-indirect-gotos -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,GOTOS
8-
// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-indirect-gotos -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,GOTOS
912
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fptrauth-indirect-gotos -emit-llvm %s -o - | FileCheck %s --check-prefixes=ALL,GOTOS
1013

1114
// ALL: define {{(dso_local )?}}void @test() #0
@@ -14,6 +17,8 @@ void test() {
1417

1518
// CALLS: attributes #0 = {{{.*}} "ptrauth-calls" {{.*}}}
1619

20+
// RETS: attributes #0 = {{{.*}} "ptrauth-returns" {{.*}}}
21+
1722
// GOTOS: attributes #0 = {{{.*}} "ptrauth-indirect-gotos" {{.*}}}
1823

1924
// OFF-NOT: attributes {{.*}} "ptrauth-

0 commit comments

Comments
 (0)