Skip to content

Commit 6bc5400

Browse files
committed
[clang][PAC][NFC] Provide addition support macros to ptrauth.h
This introduces a number of additional macros to support the use of manual pointer authentication.
1 parent 378b6d5 commit 6bc5400

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

clang/docs/PointerAuthentication.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,40 @@ type. Implementations are not required to make all bits of the result equally
722722
significant; in particular, some implementations are known to not leave
723723
meaningful data in the low bits.
724724

725+
``ptrauth_nop_cast``
726+
^^^^^^^^^^^^^^^^^^^^
727+
728+
.. code-block:: c
729+
730+
ptrauth_nop_cast(__type, __value)
731+
732+
Cast a pointer to the given type without changing any signature.
733+
734+
This operation can be used to convert a value from one type to another without
735+
attempting to re-sign the value. This makes it possible to view a signed value
736+
of one type as another type signed with the same schema. This can be used to
737+
convert implicit schemas to explicit schemas, to convert to or from opaque
738+
types, or simply to change the effective underlying type of a signed value.
739+
740+
The `__type` must be a pointer sized value compatible with the `__ptrauth`
741+
qualifier. The authentication schema must not include address diversity.
742+
743+
The result is a bitwise identical value with the type passed as the `__type`
744+
argument.
745+
746+
``ptrauth_function_pointer_type_discriminator``
747+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
748+
749+
.. code-block:: c
750+
751+
ptrauth_function_pointer_type_discriminator(__type)
752+
753+
Compute the constant discriminator used by Clang to sign pointers with the
754+
given C function pointer type.
755+
756+
A call to this function is an integer constant expression.
757+
758+
725759
Standard ``__ptrauth`` qualifiers
726760
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
727761

clang/lib/Headers/ptrauth.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
199199
ptrauth_auth_and_resign(__value, __old_key, __old_data, \
200200
ptrauth_key_function_pointer, 0)
201201

202+
/* Cast a value to the given type without changing any signature.
203+
204+
The type must be a pointer sized type compatible with the __ptrauth
205+
qualifier.
206+
The value must be an expression with a non-address diversified pointer
207+
authentication schema, and will be converted to an rvalue prior to the cast.
208+
The result has type given by the first argument.
209+
210+
The result has an identical bit-pattern to the input pointer. */
211+
#define ptrauth_nop_cast(__type, __value) \
212+
({ \
213+
union { \
214+
typeof(*(__value)) *__fptr; \
215+
typeof(__type) __opaque; \
216+
} __storage; \
217+
__storage.__fptr = (__value); \
218+
__storage.__opaque; \
219+
})
220+
202221
/* Authenticate a data pointer.
203222
204223
The value must be an expression of non-function pointer type.
@@ -241,6 +260,18 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
241260
#define ptrauth_type_discriminator(__type) \
242261
__builtin_ptrauth_type_discriminator(__type)
243262

263+
/* Compute the constant discriminator used by Clang to sign pointers with the
264+
given C function pointer type.
265+
266+
A call to this function is an integer constant expression. */
267+
#if __has_feature(ptrauth_function_pointer_type_discrimination)
268+
#define ptrauth_function_pointer_type_discriminator(__type) \
269+
__builtin_ptrauth_type_discriminator(__type)
270+
#else
271+
#define ptrauth_function_pointer_type_discriminator(__type) \
272+
((ptrauth_extra_data_t)0)
273+
#endif
274+
244275
/* Compute a signature for the given pair of pointer-sized values.
245276
The order of the arguments is significant.
246277
@@ -263,6 +294,32 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
263294
#define ptrauth_sign_generic_data(__value, __data) \
264295
__builtin_ptrauth_sign_generic_data(__value, __data)
265296

297+
/* Define some standard __ptrauth qualifiers used in the ABI. */
298+
#define __ptrauth_function_pointer(__typekey) \
299+
__ptrauth(ptrauth_key_function_pointer, 0, __typekey)
300+
#define __ptrauth_return_address __ptrauth(ptrauth_key_return_address, 1, 0)
301+
#define __ptrauth_block_invocation_pointer \
302+
__ptrauth(ptrauth_key_function_pointer, 1, 0)
303+
#define __ptrauth_block_copy_helper \
304+
__ptrauth(ptrauth_key_function_pointer, 1, 0)
305+
#define __ptrauth_block_destroy_helper \
306+
__ptrauth(ptrauth_key_function_pointer, 1, 0)
307+
#define __ptrauth_block_byref_copy_helper \
308+
__ptrauth(ptrauth_key_function_pointer, 1, 0)
309+
#define __ptrauth_block_byref_destroy_helper \
310+
__ptrauth(ptrauth_key_function_pointer, 1, 0)
311+
#if __has_feature(ptrauth_signed_block_descriptors)
312+
#define __ptrauth_block_descriptor_pointer \
313+
__ptrauth(ptrauth_key_block_descriptor_pointer, 1, 0xC0BB)
314+
#else
315+
#define __ptrauth_block_descriptor_pointer
316+
#endif
317+
318+
#define __ptrauth_cxx_vtable_pointer \
319+
__ptrauth(ptrauth_key_cxx_vtable_pointer, 0, 0)
320+
#define __ptrauth_cxx_vtt_vtable_pointer \
321+
__ptrauth(ptrauth_key_cxx_vtable_pointer, 0, 0)
322+
266323
/* C++ vtable pointer signing class attribute */
267324
#define ptrauth_cxx_vtable_pointer(key, address_discrimination, \
268325
extra_discrimination...) \
@@ -371,7 +428,10 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
371428
((ptrauth_extra_data_t)0); \
372429
})
373430

431+
#define ptrauth_nop_cast(__type, __value) (__type)(__value)
374432
#define ptrauth_type_discriminator(__type) ((ptrauth_extra_data_t)0)
433+
#define ptrauth_function_pointer_type_discriminator(__type) \
434+
((ptrauth_extra_data_t)0)
375435

376436
#define ptrauth_sign_generic_data(__value, __data) \
377437
({ \
@@ -384,9 +444,23 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
384444
#define ptrauth_cxx_vtable_pointer(key, address_discrimination, \
385445
extra_discrimination...)
386446

447+
#define __ptrauth_function_pointer(__typekey)
448+
#define __ptrauth_return_address
449+
#define __ptrauth_block_invocation_pointer
450+
#define __ptrauth_block_copy_helper
451+
#define __ptrauth_block_destroy_helper
452+
#define __ptrauth_block_byref_copy_helper
453+
#define __ptrauth_block_byref_destroy_helper
454+
#define __ptrauth_block_descriptor_pointer
455+
#define __ptrauth_objc_method_list_imp
456+
#define __ptrauth_objc_method_list_pointer
387457
#define __ptrauth_objc_isa_pointer
388458
#define __ptrauth_objc_isa_uintptr
389459
#define __ptrauth_objc_super_pointer
460+
#define __ptrauth_cxx_vtable_pointer
461+
#define __ptrauth_cxx_vtt_vtable_pointer
462+
#define __ptrauth_objc_sel
463+
#define __ptrauth_objc_class_ro
390464

391465
#endif /* __has_feature(ptrauth_intrinsics) || defined(__PTRAUTH__) */
392466

0 commit comments

Comments
 (0)