Skip to content

Commit 624b724

Browse files
ojhuntAaronBallman
andauthored
[clang][PAC] ptrauth_qualifier and ptrauth_intrinsic should only be available on Darwin (#153912)
For backwards compatibility reasons the `ptrauth_qualifier` and `ptrauth_intrinsic` features need to be testable with `__has_feature()` on Apple platforms, but for other platforms this backwards compatibility issue does not exist. This PR resolves these issues by making the `ptrauth_qualifier` and `ptrauth_intrinsic` tests conditional upon a darwin target. This also allows us to revert the ptrauth_qualifier check from an extension to a feature test again, as is required on these platforms. At the same time we introduce a new predefined macro `__PTRAUTH__` that answers the same question as `__has_feature(ptrauth_qualifier)` and `__has_feature(ptrauth_intrinsic)` as those tests are synonymous and only exist separately for compatibility reasons. The requirement to test for the `__PTRAUTH__` macro also resolves the hazard presented by mixing the `ptrauth_qualifier` flag (that impacts ABI and security policies) with `-pedantics-errors`, which makes `__has_extension` return false for all extensions. --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent 9617ce4 commit 624b724

File tree

8 files changed

+70
-13
lines changed

8 files changed

+70
-13
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ Non-comprehensive list of changes in this release
137137
- ``__builtin_elementwise_max`` and ``__builtin_elementwise_min`` functions for integer types can
138138
now be used in constant expressions.
139139

140+
- Use of ``__has_feature`` to detect the ``ptrauth_qualifier`` and ``ptrauth_intrinsics``
141+
features has been deprecated, and is restricted to the arm64e target only. The
142+
correct method to check for these features is to test for the ``__PTRAUTH__``
143+
macro.
144+
145+
140146
New Compiler Flags
141147
------------------
142148
- New option ``-fno-sanitize-annotate-debug-info-traps`` added to disable emitting trap reasons into the debug info when compiling with trapping UBSan (e.g. ``-fsanitize-trap=undefined``).

clang/include/clang/Basic/Features.def

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ FEATURE(type_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Type))
147147
FEATURE(thread_sanitizer, LangOpts.Sanitize.has(SanitizerKind::Thread))
148148
FEATURE(dataflow_sanitizer, LangOpts.Sanitize.has(SanitizerKind::DataFlow))
149149
FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
150-
FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics)
151-
EXTENSION(ptrauth_qualifier, LangOpts.PointerAuthIntrinsics)
150+
FEATURE(ptrauth_intrinsics, LangOpts.PointerAuthIntrinsics &&
151+
PP.getTargetInfo().getTriple().isOSDarwin())
152+
FEATURE(ptrauth_qualifier, LangOpts.PointerAuthIntrinsics &&
153+
PP.getTargetInfo().getTriple().isOSDarwin())
152154
FEATURE(ptrauth_calls, LangOpts.PointerAuthCalls)
153155
FEATURE(ptrauth_returns, LangOpts.PointerAuthReturns)
154156
FEATURE(ptrauth_vtable_pointer_address_discrimination, LangOpts.PointerAuthVTPtrAddressDiscrimination)

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
15351535
#undef TARGET_OS
15361536
}
15371537

1538+
if (LangOpts.PointerAuthIntrinsics)
1539+
Builder.defineMacro("__PTRAUTH__");
1540+
15381541
// Get other target #defines.
15391542
TI.getTargetDefines(LangOpts, Builder);
15401543
}

clang/lib/Headers/ptrauth.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
9595
__ptrauth qualifier; the compiler will perform this check
9696
automatically. */
9797

98-
#if __has_feature(ptrauth_intrinsics)
98+
#if __has_feature(ptrauth_intrinsics) || defined(__PTRAUTH__)
9999

100100
/* Strip the signature from a value without authenticating it.
101101
@@ -388,6 +388,6 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
388388
#define __ptrauth_objc_isa_uintptr
389389
#define __ptrauth_objc_super_pointer
390390

391-
#endif /* __has_feature(ptrauth_intrinsics) */
391+
#endif /* __has_feature(ptrauth_intrinsics) || defined(__PTRAUTH__) */
392392

393393
#endif /* __PTRAUTH_H */

clang/test/Preprocessor/ptrauth_extension.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,32 @@
44
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-calls | \
55
// RUN: FileCheck %s --check-prefixes=NOINTRIN
66

7-
#if __has_extension(ptrauth_qualifier)
8-
// INTRIN: has_ptrauth_qualifier
9-
void has_ptrauth_qualifier() {}
10-
#else
7+
// RUN: %clang_cc1 -E %s -DIS_DARWIN -triple=arm64e-apple-darwin -fptrauth-intrinsics | \
8+
// RUN: FileCheck %s --check-prefixes=INTRIN,INTRIN_MAC
9+
10+
// RUN: %clang_cc1 -E %s -DIS_DARWIN -triple=arm64e-apple-darwin -fptrauth-calls | \
11+
// RUN: FileCheck %s --check-prefixes=NOINTRIN
12+
13+
#if defined(IS_DARWIN) && __has_extension(ptrauth_qualifier)
14+
// INTRIN_MAC: has_ptrauth_qualifier1
15+
void has_ptrauth_qualifier1() {}
16+
#ifndef __PTRAUTH__
17+
#error ptrauth_qualifier extension present without predefined test macro
18+
#endif
19+
#endif
20+
#if defined(IS_DARWIN) && __has_feature(ptrauth_qualifier)
21+
// INTRIN_MAC: has_ptrauth_qualifier2
22+
void has_ptrauth_qualifier2() {}
23+
#ifndef __PTRAUTH__
24+
#error ptrauth_qualifier extension present without predefined test macro
25+
#endif
26+
#endif
27+
#if defined(__PTRAUTH__)
28+
// INTRIN: has_ptrauth_qualifier3
29+
void has_ptrauth_qualifier3() {}
30+
#endif
31+
32+
#if !defined(__PTRAUTH__) && !__has_feature(ptrauth_qualifier) && !__has_extension(ptrauth_qualifier)
1133
// NOINTRIN: no_ptrauth_qualifier
1234
void no_ptrauth_qualifier() {}
1335
#endif

clang/test/Preprocessor/ptrauth_feature.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// RUN: %clang_cc1 -E %s -triple=aarch64 -fptrauth-elf-got | \
3535
// RUN: FileCheck %s --check-prefixes=NOINTRIN,NOCALLS,NORETS,NOVPTR_ADDR_DISCR,NOVPTR_TYPE_DISCR,NOTYPE_INFO_DISCR,NOFUNC,NOINITFINI,NOINITFINI_ADDR_DISCR,NOGOTOS,ELFGOT
3636

37-
#if __has_feature(ptrauth_intrinsics)
37+
#if defined(__PTRAUTH__)
3838
// INTRIN: has_ptrauth_intrinsics
3939
void has_ptrauth_intrinsics() {}
4040
#else

clang/test/Sema/ptrauth-qualifier.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
// RUN: %clang_cc1 -triple arm64-apple-ios -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
1+
// RUN: %clang_cc1 -triple arm64-apple-ios -DIS_DARWIN -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
22
// RUN: %clang_cc1 -triple aarch64-linux-gnu -std=c23 -fsyntax-only -verify -fptrauth-intrinsics %s
33

4-
#if !__has_extension(ptrauth_qualifier)
4+
#if defined(IS_DARWIN) && !__has_extension(ptrauth_qualifier)
55
// This error means that the __ptrauth qualifier availability test says that it
66
// is not available. This error is not expected in the output, if it is seen
77
// there is a feature detection regression.
88
#error __ptrauth qualifier not enabled
99
#endif
1010

11+
#if defined(IS_DARWIN) && !__has_feature(ptrauth_qualifier)
12+
// This error means that the __has_feature test for ptrauth_qualifier has
13+
// failed, despite it being expected on darwin.
14+
#error __ptrauth qualifier not enabled
15+
#elif !defined(IS_DARWIN) && (__has_feature(ptrauth_qualifier) || __has_extension(ptrauth_qualifier))
16+
#error ptrauth_qualifier labeled a feature on a non-darwin platform
17+
#endif
18+
19+
#if !defined (__PTRAUTH__)
20+
#error __PTRAUTH__ test macro not defined when ptrauth is enabled
21+
#endif
22+
1123
#if __aarch64__
1224
#define VALID_CODE_KEY 0
1325
#define VALID_DATA_KEY 2

clang/test/SemaObjC/ptrauth-qualifier.m

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
// RUN: %clang_cc1 -triple arm64-apple-ios -fsyntax-only -verify -fptrauth-intrinsics %s
1+
// RUN: %clang_cc1 -triple arm64-apple-ios -DIS_DARWIN -fsyntax-only -verify -fptrauth-intrinsics %s
22
// RUN: %clang_cc1 -triple aarch64-linux-gnu -fsyntax-only -verify -fptrauth-intrinsics %s
33

4-
#if !__has_extension(ptrauth_qualifier)
4+
#if defined(IS_DARWIN) && !__has_extension(ptrauth_qualifier)
55
// This error means that the __ptrauth qualifier availability test says that it
66
// is not available. This error is not expected in the output, if it is seen
77
// there is a feature detection regression.
88
#error __ptrauth qualifier not enabled
99
#endif
1010

11+
#if defined(IS_DARWIN) && !__has_feature(ptrauth_qualifier)
12+
// This error means that the __has_feature test for ptrauth_qualifier has
13+
// failed, despite it being expected on darwin.
14+
#error __ptrauth qualifier not enabled
15+
#elif !defined(IS_DARWIN) && (__has_feature(ptrauth_qualifier) || __has_extension(ptrauth_qualifier))
16+
#error ptrauth_qualifier labeled a feature on a non-darwin platform
17+
#endif
18+
19+
#if !defined (__PTRAUTH__)
20+
#error __PTRAUTH__ test macro not defined when ptrauth is enabled
21+
#endif
22+
1123
@interface Foo
1224
// expected-warning@-1 {{class 'Foo' defined without specifying a base class}}
1325
// expected-note@-2 {{add a super class to fix this problem}}

0 commit comments

Comments
 (0)