https://github.com/ojhunt created 
https://github.com/llvm/llvm-project/pull/153503

…thentication

The __has_extension query for the __ptrauth qualifier returns false when 
-pedantic-errors is specified by the developer, which results in ABI mismatches 
and potentially ODR violations as well.

The only way to resolve this is to to make it an error to attempt to use the 
-pedantic-errors option when targeting a platform where pointer authentication 
is present. This may cause compilation issues for some projects but is better 
than bad codegen.

>From e69b1acc7b302462548938c97392b5e72c9c43b2 Mon Sep 17 00:00:00 2001
From: Oliver Hunt <oli...@apple.com>
Date: Wed, 13 Aug 2025 15:00:00 -0700
Subject: [PATCH] [clang][PAC] Disallow -pedantic-errors in conjunction with
 pointer authentication

The __has_extension query for the __ptrauth qualifier returns false
when -pedantic-errors is specified by the developer, which results
in ABI mismatches and potentially ODR violations as well.

The only way to resolve this is to to make it an error to attempt to
use the -pedantic-errors option when targeting a platform where
pointer authentication is present. This may cause compilation issues
for some projects but is better than bad codegen.
---
 clang/lib/Frontend/CompilerInvocation.cpp     | 36 +++++++++++++------
 .../Driver/aarch64-ptrauth-pedantic-errors.c  | 26 ++++++++++++++
 2 files changed, 51 insertions(+), 11 deletions(-)
 create mode 100644 clang/test/Driver/aarch64-ptrauth-pedantic-errors.c

diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index db7c791059a32..2942d5aefe354 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3470,22 +3470,31 @@ static void GeneratePointerAuthArgs(const LangOptions 
&Opts,
 
 static void ParsePointerAuthArgs(LangOptions &Opts, ArgList &Args,
                                  DiagnosticsEngine &Diags) {
-  Opts.PointerAuthIntrinsics = Args.hasArg(OPT_fptrauth_intrinsics);
-  Opts.PointerAuthCalls = Args.hasArg(OPT_fptrauth_calls);
-  Opts.PointerAuthReturns = Args.hasArg(OPT_fptrauth_returns);
-  Opts.PointerAuthIndirectGotos = Args.hasArg(OPT_fptrauth_indirect_gotos);
-  Opts.PointerAuthAuthTraps = Args.hasArg(OPT_fptrauth_auth_traps);
+  const Arg *PedanticErrors = Args.getLastArgNoClaim(OPT_pedantic_errors);
+  auto GetAndCheckPointerAuthArg = [&](driver::options::ID Option) {
+    Arg *OptionArg = Args.getLastArg(Option);
+    if (OptionArg && PedanticErrors) {
+        Diags.Report(diag::err_drv_incompatible_options)
+            << OptionArg->getSpelling() << PedanticErrors->getSpelling();
+    }
+    return OptionArg != nullptr;
+  };
+  Opts.PointerAuthIntrinsics = 
GetAndCheckPointerAuthArg(OPT_fptrauth_intrinsics);
+  Opts.PointerAuthCalls = GetAndCheckPointerAuthArg(OPT_fptrauth_calls);
+  Opts.PointerAuthReturns = GetAndCheckPointerAuthArg(OPT_fptrauth_returns);
+  Opts.PointerAuthIndirectGotos = 
GetAndCheckPointerAuthArg(OPT_fptrauth_indirect_gotos);
+  Opts.PointerAuthAuthTraps = 
GetAndCheckPointerAuthArg(OPT_fptrauth_auth_traps);
   Opts.PointerAuthVTPtrAddressDiscrimination =
-      Args.hasArg(OPT_fptrauth_vtable_pointer_address_discrimination);
+      
GetAndCheckPointerAuthArg(OPT_fptrauth_vtable_pointer_address_discrimination);
   Opts.PointerAuthVTPtrTypeDiscrimination =
-      Args.hasArg(OPT_fptrauth_vtable_pointer_type_discrimination);
+      
GetAndCheckPointerAuthArg(OPT_fptrauth_vtable_pointer_type_discrimination);
   Opts.PointerAuthTypeInfoVTPtrDiscrimination =
-      Args.hasArg(OPT_fptrauth_type_info_vtable_pointer_discrimination);
+      
GetAndCheckPointerAuthArg(OPT_fptrauth_type_info_vtable_pointer_discrimination);
   Opts.PointerAuthFunctionTypeDiscrimination =
-      Args.hasArg(OPT_fptrauth_function_pointer_type_discrimination);
-  Opts.PointerAuthInitFini = Args.hasArg(OPT_fptrauth_init_fini);
+      
GetAndCheckPointerAuthArg(OPT_fptrauth_function_pointer_type_discrimination);
+  Opts.PointerAuthInitFini = GetAndCheckPointerAuthArg(OPT_fptrauth_init_fini);
   Opts.PointerAuthInitFiniAddressDiscrimination =
-      Args.hasArg(OPT_fptrauth_init_fini_address_discrimination);
+      GetAndCheckPointerAuthArg(OPT_fptrauth_init_fini_address_discrimination);
 }
 
 /// Check if input file kind and language standard are compatible.
@@ -4895,6 +4904,11 @@ bool CompilerInvocation::CreateFromArgsImpl(
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
   llvm::Triple T(Res.getTargetOpts().Triple);
+  if (const Arg *PedanticErrors =  Args.getLastArgNoClaim(OPT_pedantic_errors);
+     PedanticErrors && T.isArm64e()) {
+      Diags.Report(diag::err_drv_unsupported_opt_for_target)
+          << PedanticErrors->getSpelling() << T.str();
+  }
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
                         Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
diff --git a/clang/test/Driver/aarch64-ptrauth-pedantic-errors.c 
b/clang/test/Driver/aarch64-ptrauth-pedantic-errors.c
new file mode 100644
index 0000000000000..61cc68666ffd8
--- /dev/null
+++ b/clang/test/Driver/aarch64-ptrauth-pedantic-errors.c
@@ -0,0 +1,26 @@
+// REQUIRES: aarch64-registered-target
+
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-intrinsics %s 
-c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-calls %s -c  
2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-returns %s -c  
2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-indirect-gotos 
%s -c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-auth-traps %s 
-c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 
-fptrauth-vtable-pointer-address-discrimination %s -c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 
-fptrauth-vtable-pointer-type-discrimination %s -c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 
-fptrauth-function-pointer-type-discrimination %s -c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 -fptrauth-init-fini %s 
-c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=aarch64 
-fptrauth-init-fini-address-discrimination %s -c  2>&1 | FileCheck %s
+// RUN: not %clang  -pedantic-errors --target=arm64e %s -c  2>&1 | FileCheck 
%s --check-prefix ARM64E_TRIPLE
+// RUN: not %clang  -pedantic-errors --target=arm64e-apple-macosx10.0 %s -c  
2>&1 | FileCheck %s --check-prefix ARM64E_MACOS_TRIPLE
+// RUN: not %clang  -pedantic-errors -arch arm64e %s -c  2>&1 | FileCheck %s 
--check-prefix ARM64E_ARCH
+
+int i;
+
+// CHECK: error: the combination of '{{.*}}' and '-pedantic-errors' is 
incompatible
+// ARM64E_TRIPLE: error: unsupported option '-pedantic-errors' for target 
'arm64e'
+// ARM64E_MACOS_TRIPLE: error: unsupported option '-pedantic-errors' for 
target 'arm64e-apple-macosx10.0.0'
+
+// We have a trailing 'arm64e with no closing ' as the full triple is inferred 
from the host
+// which we don't care about, and don't want to specify as we're wanting to 
ensure that *just*
+// using '-arch arm64e' is sufficient
+// ARM64E_ARCH: error: unsupported option '-pedantic-errors' for target 'arm64e
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to