Skip to content

Conversation

ojhunt
Copy link
Contributor

@ojhunt ojhunt commented Oct 14, 2025

Adds an additional helper that had not been upstreamed, and adds documentation for both ptrauth_type_discriminator and ptrauth_function_pointer_type_discriminator

@ojhunt ojhunt requested a review from kovdan01 October 14, 2025 21:31
@llvmbot llvmbot added clang Clang issues not falling into any other category backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics labels Oct 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 14, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-backend-x86

Author: Oliver Hunt (ojhunt)

Changes

Adds an additional helper that had not been upstreamed, and adds documentation for both ptrauth_type_discriminator and ptrauth_function_pointer_type_discriminator


Full diff: https://github.com/llvm/llvm-project/pull/163456.diff

2 Files Affected:

  • (modified) clang/docs/PointerAuthentication.rst (+30)
  • (modified) clang/lib/Headers/ptrauth.h (+12)
diff --git a/clang/docs/PointerAuthentication.rst b/clang/docs/PointerAuthentication.rst
index 96eb498bc48b6..7e65f4b1b4915 100644
--- a/clang/docs/PointerAuthentication.rst
+++ b/clang/docs/PointerAuthentication.rst
@@ -592,6 +592,36 @@ The result value is never zero and always within range for both the
 
 This can be used in constant expressions.
 
+``ptrauth_type_discriminator``
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+.. code-block:: c
+
+  ptrauth_type_discriminator(type)
+
+Compute the constant discriminator derived from the given type, as is computed
+for automatically type diversified schemas.
+
+``type`` must be a type. The result has the type ``ptrauth_extra_data_t``.
+
+This can be used in constant expressions.
+
+``ptrauth_function_pointer_type_discriminator``
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+.. code-block:: c
+
+  ptrauth_function_pointer_type_discriminator(function_type)
+
+Compute the constant discriminator derived from the provided function type, for
+use in contexts where the default function authentication schema. If function
+pointer type diversity is enabled, this is equivalent to
+`ptrauth_type_discriminator(function_type)`, if it is not enabled this is `0`.
+
+``function_type`` must be a function type. The result has the type ``ptrauth_extra_data_t``.
+
+This can be used in constant expressions.
+
 ``ptrauth_strip``
 ^^^^^^^^^^^^^^^^^
 
diff --git a/clang/lib/Headers/ptrauth.h b/clang/lib/Headers/ptrauth.h
index f902ca1e3bbd3..1de8324e1ec6f 100644
--- a/clang/lib/Headers/ptrauth.h
+++ b/clang/lib/Headers/ptrauth.h
@@ -241,6 +241,17 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
 #define ptrauth_type_discriminator(__type)                                     \
   __builtin_ptrauth_type_discriminator(__type)
 
+/* Compute the constant discriminator used by Clang to sign pointers with the
+   given C function pointer type.
+
+   A call to this function is an integer constant expression. */
+#if __has_feature(ptrauth_function_pointer_type_discrimination)
+#define ptrauth_function_pointer_type_discriminator(__type) \
+  __builtin_ptrauth_type_discriminator(__type)
+#else
+#define ptrauth_function_pointer_type_discriminator(__type) ((ptrauth_extra_data_t)0)
+#endif
+
 /* Compute a signature for the given pair of pointer-sized values.
    The order of the arguments is significant.
 
@@ -372,6 +383,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
   })
 
 #define ptrauth_type_discriminator(__type) ((ptrauth_extra_data_t)0)
+#define ptrauth_function_pointer_type_discriminator(__type) ((ptrauth_extra_data_t)0)
 
 #define ptrauth_sign_generic_data(__value, __data)                             \
   ({                                                                           \

Copy link

github-actions bot commented Oct 14, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@ojhunt ojhunt force-pushed the users/ojhunt/ptrauth-additional-helpers branch from de32e5b to 28965d5 Compare October 14, 2025 21:37
@ojhunt ojhunt changed the title [clang][PAC] Add ptrauth.h helpers for type diversity helpers [clang][PAC] Add ptrauth.h helpers for computing type discriminators Oct 14, 2025
@ojhunt ojhunt force-pushed the users/ojhunt/ptrauth-additional-helpers branch from 28965d5 to 03a48e8 Compare October 14, 2025 21:38
Copy link
Contributor

@kovdan01 kovdan01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!


This can be used in constant expressions.

``ptrauth_function_pointer_type_discriminator``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: maybe ptrauth_function_pointer_discriminator (w/o type) would suite better here? We either have a type-based discriminator if the feature is requested or just have zero, so it might be better to call the macro just ptrauth_function_pointer_discriminator w/o making the user think that it's a type discriminator.

Totally optional and feel free to ignore.

@kovdan01
Copy link
Contributor

@ojhunt Thanks for introducing this! It's probably worth applying the auto-formatting suggestion before merging the PR

Adds an additional helper that had not been upstreamed, and adds documentation for both ptrauth_type_discriminator and ptrauth_function_pointer_type_discriminator
@ojhunt ojhunt force-pushed the users/ojhunt/ptrauth-additional-helpers branch from 03a48e8 to 7e7dbd5 Compare October 15, 2025 00:50
@ojhunt
Copy link
Contributor Author

ojhunt commented Oct 15, 2025

@ojhunt Thanks for introducing this! It's probably worth applying the auto-formatting suggestion before merging the PR

Oh yeah, durrrr, copying code while forgetting that we have reformatted a lot over time :D

@ojhunt ojhunt enabled auto-merge (squash) October 15, 2025 00:51
@ojhunt ojhunt merged commit 23848e6 into main Oct 15, 2025
10 of 11 checks passed
@ojhunt ojhunt deleted the users/ojhunt/ptrauth-additional-helpers branch October 15, 2025 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:X86 clang:headers Headers provided by Clang, e.g. for intrinsics clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants