Skip to content

Commit 57925ab

Browse files
committed
Improved error message, updated tests, fixed message duplication
1 parent 20ac8d5 commit 57925ab

File tree

4 files changed

+78
-44
lines changed

4 files changed

+78
-44
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def err_stack_tagging_requires_hardware_feature : Error<
533533
"Armv9, try compiling with -march=armv8a+memtag or -march=armv9a+memtag">;
534534

535535
def err_pauth_cpu_feature_missing : Error<
536-
"neither FEAT_PAUTH nor -fptrauth-soft is enabled. Most of PAuth features are unavailable">;
536+
"%0 or -mbranch-protection=pauthabi is passed that require either enabling PAuth CPU feature or passing -fptrauth-soft option">;
537537

538538
def warn_pauth_option_ignored : Warning<
539539
"%0 is ignored because neither -fptrauth-calls nor -mbranch-protection=pauthabi is specified">,

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,26 @@ AArch64TargetInfo::AArch64TargetInfo(const llvm::Triple &Triple,
201201
}
202202

203203
void AArch64TargetInfo::validatePAuthOptions(DiagnosticsEngine &Diags,
204-
const LangOptions &Opts) const {
204+
LangOptions &Opts) const {
205+
// AArch64TargetInfo::adjust can be called multiple times during
206+
// compilation, so unset unsupported options to prevent printing
207+
// multiple identical diagnostics.
208+
auto UnsetIgnoredOptions = [&]() {
209+
assert(!Opts.PointerAuthCalls && "Will change the behavior");
210+
Opts.PointerAuthInitFini = false;
211+
Opts.FunctionPointerTypeDiscrimination = false;
212+
Opts.PointerAuthVTPtrAddressDiscrimination = false;
213+
Opts.PointerAuthVTPtrTypeDiscrimination = false;
214+
Opts.PointerAuthBlockDescriptorPointers = false;
215+
};
216+
auto UnsetUnsupportedOptions = [&]() {
217+
assert(!HasPAuth && !Opts.SoftPointerAuth && "Will change the behavior");
218+
Opts.PointerAuthIntrinsics = false;
219+
Opts.PointerAuthCalls = false;
220+
Opts.PointerAuthReturns = false;
221+
Opts.setPointerAuthObjcIsaAuthentication(PointerAuthenticationMode::None);
222+
};
223+
205224
if (!Opts.PointerAuthCalls) {
206225
if (Opts.PointerAuthInitFini)
207226
Diags.Report(diag::warn_pauth_option_ignored) << "-fptrauth-init-fini";
@@ -217,26 +236,29 @@ void AArch64TargetInfo::validatePAuthOptions(DiagnosticsEngine &Diags,
217236
if (Opts.PointerAuthBlockDescriptorPointers)
218237
Diags.Report(diag::warn_pauth_option_ignored)
219238
<< "-fptrauth-block-descriptor-pointers";
239+
UnsetIgnoredOptions();
220240
}
221241

222242
if (HasPAuth || Opts.SoftPointerAuth)
223243
return;
224244

225-
bool NeedsFeatPAuth = false;
226245
// FIXME: Some ptrauth_* intrinsics can be implemented by only using
227246
// HINT-encoded instructions, thus not requiring FEAT_PAUTH.
228247
// At now, checking conservatively.
229-
NeedsFeatPAuth |= Opts.PointerAuthIntrinsics;
230-
NeedsFeatPAuth |= Opts.PointerAuthCalls;
231-
NeedsFeatPAuth |= Opts.PointerAuthReturns;
232-
NeedsFeatPAuth |= Opts.getPointerAuthObjcIsaAuthentication() !=
233-
PointerAuthenticationMode::None;
234-
235-
// FIXME The particular command line option is not printed in error message,
236-
// as it would misleadingly mention various -fptrauth-<something>
237-
// options instead of -mbranch-protection=pauthabi.
238-
if (NeedsFeatPAuth)
239-
Diags.Report(diag::err_pauth_cpu_feature_missing);
248+
if (Opts.PointerAuthIntrinsics)
249+
Diags.Report(diag::err_pauth_cpu_feature_missing) << "-fptrauth-intrinsics";
250+
if (Opts.PointerAuthCalls)
251+
Diags.Report(diag::err_pauth_cpu_feature_missing) << "-fptrauth-calls";
252+
if (Opts.PointerAuthReturns)
253+
Diags.Report(diag::err_pauth_cpu_feature_missing) << "-fptrauth-returns";
254+
if (Opts.getPointerAuthObjcIsaAuthentication() !=
255+
PointerAuthenticationMode::None)
256+
Diags.Report(diag::err_pauth_cpu_feature_missing)
257+
<< "-fptrauth-objc-isa=...";
258+
259+
UnsetUnsupportedOptions();
260+
// Opts.PointerAuthCalls was unset - prevent unexpected warnings, too.
261+
UnsetIgnoredOptions();
240262
}
241263

242264
void AArch64TargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {

clang/lib/Basic/Targets/AArch64.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ class LLVM_LIBRARY_VISIBILITY AArch64TargetInfo : public TargetInfo {
9191
public:
9292
AArch64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts);
9393

94-
void validatePAuthOptions(DiagnosticsEngine &Diags,
95-
const LangOptions &Opts) const;
94+
void validatePAuthOptions(DiagnosticsEngine &Diags, LangOptions &Opts) const;
9695
void adjust(DiagnosticsEngine &Diags, LangOptions &Opts) override;
9796

9897
StringRef getABI() const override;

clang/test/CodeGen/ptrauth-cpu-feature.cpp

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
// Test that features requiring FEAT_PAuth fail early if the requirement is not met:
44
//
5-
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pauthabi 2>&1 | FileCheck %s --check-prefix=FAIL
6-
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-calls 2>&1 | FileCheck %s --check-prefix=FAIL
7-
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-returns 2>&1 | FileCheck %s --check-prefix=FAIL
8-
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-objc-isa 2>&1 | FileCheck %s --check-prefix=FAIL
5+
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pauthabi 2>&1 \
6+
// RUN: | FileCheck %s --check-prefixes=FAIL-INTRINSICS,FAIL-CALLS,FAIL-RETURNS,NO-UNEXPECTED
7+
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-intrinsics 2>&1 \
8+
// RUN: | FileCheck %s --check-prefixes=FAIL-INTRINSICS,NO-UNEXPECTED
9+
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-calls 2>&1 \
10+
// RUN: | FileCheck %s --check-prefixes=FAIL-CALLS,NO-UNEXPECTED
11+
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-returns 2>&1 \
12+
// RUN: | FileCheck %s --check-prefixes=FAIL-RETURNS,NO-UNEXPECTED
13+
// RUN: not %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-objc-isa 2>&1 \
14+
// RUN: | FileCheck %s --check-prefixes=FAIL-OBJC-ISA,NO-UNEXPECTED
915
//
1016
// Test that no errors and warnings are generated if FEAT_PAUTH is supported:
1117
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=apple-a12 -mbranch-protection=pauthabi 2>&1 \
@@ -17,47 +23,52 @@
1723
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -march=armv8.2-a+pauth -mbranch-protection=pauthabi 2>&1 \
1824
// RUN: | FileCheck %s --check-prefix=PAUTH --implicit-check-not=error --implicit-check-not=warning
1925
//
20-
// Test a few combinations of options that should not generate warnings (technically, prefix is CHECK):
26+
// Test a few combinations of options that should not generate warnings (technically, the prefix is CHECK):
2127
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -march=armv8.3-a -fptrauth-returns 2>&1 \
22-
// RUN: | FileCheck %s --allow-unused-prefixes --implicit-check-not=error --implicit-check-not=warning
28+
// RUN: | FileCheck %s --check-prefix=NO-UNEXPECTED
2329
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -march=armv8.3-a -fptrauth-objc-isa 2>&1 \
24-
// RUN: | FileCheck %s --allow-unused-prefixes --implicit-check-not=error --implicit-check-not=warning
30+
// RUN: | FileCheck %s --check-prefix=NO-UNEXPECTED
2531
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -march=armv8.3-a -fptrauth-init-fini -fptrauth-calls 2>&1 \
26-
// RUN: | FileCheck %s --allow-unused-prefixes --implicit-check-not=error --implicit-check-not=warning
32+
// RUN: | FileCheck %s --check-prefix=NO-UNEXPECTED
2733
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -march=armv8.3-a -fptrauth-init-fini -mbranch-protection=pauthabi 2>&1 \
28-
// RUN: | FileCheck %s --allow-unused-prefixes --implicit-check-not=error --implicit-check-not=warning
34+
// RUN: | FileCheck %s --check-prefix=NO-UNEXPECTED
2935

3036
// Test that the following options are still gated on -fptrauth-calls.
3137
// If they are not, in assertion builds they would usually fail at asm printing time:
3238
//
3339
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-init-fini 2>&1 \
34-
// RUN: | FileCheck %s --check-prefix=WARN -DOPTION=-fptrauth-init-fini
40+
// RUN: | FileCheck %s --check-prefixes=NO-PTRAUTH-CALLS,NO-UNEXPECTED -DOPTION=-fptrauth-init-fini
3541
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-function-pointer-type-discrimination 2>&1 \
36-
// RUN: | FileCheck %s --check-prefix=WARN -DOPTION=-fptrauth-function-pointer-type-discrimination
42+
// RUN: | FileCheck %s --check-prefixes=NO-PTRAUTH-CALLS,NO-UNEXPECTED -DOPTION=-fptrauth-function-pointer-type-discrimination
3743
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-vtable-pointer-address-discrimination 2>&1 \
38-
// RUN: | FileCheck %s --check-prefix=WARN -DOPTION=-fptrauth-vtable-pointer-address-discrimination
44+
// RUN: | FileCheck %s --check-prefixes=NO-PTRAUTH-CALLS,NO-UNEXPECTED -DOPTION=-fptrauth-vtable-pointer-address-discrimination
3945
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-vtable-pointer-type-discrimination 2>&1 \
40-
// RUN: | FileCheck %s --check-prefix=WARN -DOPTION=-fptrauth-vtable-pointer-type-discrimination
46+
// RUN: | FileCheck %s --check-prefixes=NO-PTRAUTH-CALLS,NO-UNEXPECTED -DOPTION=-fptrauth-vtable-pointer-type-discrimination
4147
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -fptrauth-block-descriptor-pointers -fblocks -DBLOCKS 2>&1 \
42-
// RUN: | FileCheck %s --check-prefix=WARN -DOPTION=-fptrauth-block-descriptor-pointers
48+
// RUN: | FileCheck %s --check-prefixes=NO-PTRAUTH-CALLS,NO-UNEXPECTED -DOPTION=-fptrauth-block-descriptor-pointers
4349

4450
// Test that v8.2-compatible code is generated, if possible:
4551
//
46-
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -msign-return-address=all 2>&1 | FileCheck %s --check-prefix=COMPAT
47-
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pac-ret 2>&1 | FileCheck %s --check-prefix=COMPAT
48-
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pac-ret+b-key 2>&1 | FileCheck %s --check-prefix=COMPAT
52+
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -msign-return-address=all 2>&1 \
53+
// RUN: | FileCheck %s --check-prefix=COMPAT
54+
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pac-ret 2>&1 \
55+
// RUN: | FileCheck %s --check-prefix=COMPAT
56+
// RUN: %clang %s -S -o - -target aarch64-linux-gnu -mcpu=cortex-a72 -mbranch-protection=pac-ret+b-key 2>&1 \
57+
// RUN: | FileCheck %s --check-prefix=COMPAT
4958

5059
// arm64e has ptrauth enabled and assumes modern enough CPU by default:
5160
//
52-
// RUN: %clang %s -S -o - -target arm64e-apple-ios 2>&1 | FileCheck %s --check-prefix=PAUTH
53-
// RUN: not %clang %s -S -o - -target arm64e-apple-ios -mcpu=cortex-a72 2>&1 | FileCheck %s --check-prefix=FAIL
61+
// RUN: %clang %s -S -o - -target arm64e-apple-ios 2>&1 \
62+
// RUN: | FileCheck %s --check-prefix=PAUTH
63+
// RUN: not %clang %s -S -o - -target arm64e-apple-ios -mcpu=cortex-a72 2>&1 \
64+
// RUN: | FileCheck %s --check-prefixes=FAIL-INTRINSICS,FAIL-CALLS,FAIL-RETURNS,FAIL-OBJC-ISA,NO-UNEXPECTED
5465

5566
volatile int counter;
5667

5768
void ext(void);
5869

5970
// Basically check the code generated for `caller`, other functions and classes
60-
// are provided just to ensure that assertion-enabled builds do not crash when
71+
// are provided just to check that assertion-enabled builds do not crash when
6172
// generating code for constructors, vtable, etc.
6273

6374
extern "C" int caller(void) {
@@ -103,11 +114,13 @@ extern "C" void *create(bool f) {
103114
return new Derived();
104115
}
105116

106-
// FIXME At now, the error message is printed twice.
107-
// Ideally, this should be fixed, but it seems rather harmless.
108-
//
109-
// FAIL-COUNT-2: error: neither FEAT_PAUTH nor -fptrauth-soft is enabled. Most of PAuth features are unavailable
110-
// WARN-COUNT-2: warning: [[OPTION]] is ignored because neither -fptrauth-calls nor -mbranch-protection=pauthabi is specified
117+
// NO-PTRAUTH-CALLS: warning: [[OPTION]] is ignored because neither -fptrauth-calls nor -mbranch-protection=pauthabi is specified
118+
// FAIL-INTRINSICS: error: -fptrauth-intrinsics or -mbranch-protection=pauthabi is passed that require either enabling PAuth CPU feature or passing -fptrauth-soft option
119+
// FAIL-CALLS: error: -fptrauth-calls or -mbranch-protection=pauthabi is passed that require either enabling PAuth CPU feature or passing -fptrauth-soft option
120+
// FAIL-RETURNS: error: -fptrauth-returns or -mbranch-protection=pauthabi is passed that require either enabling PAuth CPU feature or passing -fptrauth-soft option
121+
// FAIL-OBJC-ISA: error: -fptrauth-objc-isa=... or -mbranch-protection=pauthabi is passed that require either enabling PAuth CPU feature or passing -fptrauth-soft option
122+
// NO-UNEXPECTED-NOT: error:
123+
// NO-UNEXPECTED-NOT: warning:
111124

112125
// COMPAT: caller:
113126
// COMPAT: hint {{#25|#27}}
@@ -125,8 +138,8 @@ extern "C" void *create(bool f) {
125138
// Just check that some assembler output is printed and -fptrauth-init-fini
126139
// is disabled.
127140
//
128-
// WARN-NOT: @AUTH
141+
// NO-PTRAUTH-CALLS-NOT: @AUTH
129142
//
130-
// WARN: caller:
143+
// NO-PTRAUTH-CALLS: caller:
131144
//
132-
// WARN-NOT: @AUTH
145+
// NO-PTRAUTH-CALLS-NOT: @AUTH

0 commit comments

Comments
 (0)