Skip to content

Commit d61d58c

Browse files
ahmedbougachadtapuska
authored andcommitted
[clang] Add __ptrauth macros to ptrauth.h for the arm64e ABI.
FIXME: the c++ vtable cases should depend on vtable discrimination.
1 parent a3bdd61 commit d61d58c

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

clang/lib/Headers/ptrauth.h

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,38 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
106106
__ptrauth qualifier; the compiler will perform this check
107107
automatically. */
108108

109+
#if __has_feature(ptrauth_qualifier_authentication_mode)
110+
/* We define these macros including the comma so we can no-op them in the future
111+
without version locking with downstream projects
112+
*/
113+
#if __has_feature(ptrauth_objc_isa_signs)
114+
#if __has_feature(ptrauth_objc_isa_authenticates)
115+
#define __ptrauth_isa_signing_mode , "sign-and-auth"
116+
#elif __has_feature(ptrauth_objc_isa_strips)
117+
#define __ptrauth_isa_signing_mode , "sign-and-strip"
118+
#endif
119+
#else
120+
#if __has_feature(ptrauth_objc_isa_strips)
121+
#define __ptrauth_isa_signing_mode , "strip"
122+
#endif
123+
#endif
124+
#endif
125+
126+
#ifdef __ptrauth_isa_signing_mode
127+
#if __has_feature(ptrauth_objc_isa_masking)
128+
#define __ptrauth_isa_masking_mode ",isa-pointer"
129+
#else
130+
#define __ptrauth_isa_masking_mode
131+
#endif
132+
#else
133+
#define __ptrauth_isa_signing_mode
134+
#if __has_feature(ptrauth_objc_isa_masking)
135+
#define __ptrauth_isa_masking_mode "isa-pointer"
136+
#else
137+
#define __ptrauth_isa_masking_mode
138+
#endif
139+
#endif
140+
109141
#if __has_feature(ptrauth_intrinsics)
110142

111143
/* Strip the signature from a value without authenticating it.
@@ -315,6 +347,77 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
315347
/* The value is ptrauth_string_discriminator("init_fini") */
316348
#define __ptrauth_init_fini_discriminator 0xd9d4
317349

350+
/* Define some standard __ptrauth qualifiers used in the ABI. */
351+
#define __ptrauth_function_pointer(__typekey) \
352+
__ptrauth(ptrauth_key_function_pointer,0,__typekey)
353+
#define __ptrauth_return_address \
354+
__ptrauth(ptrauth_key_return_address,1,0)
355+
#define __ptrauth_block_invocation_pointer \
356+
__ptrauth(ptrauth_key_function_pointer,1,0)
357+
#define __ptrauth_block_copy_helper \
358+
__ptrauth(ptrauth_key_function_pointer,1,0)
359+
#define __ptrauth_block_destroy_helper \
360+
__ptrauth(ptrauth_key_function_pointer,1,0)
361+
#define __ptrauth_block_byref_copy_helper \
362+
__ptrauth(ptrauth_key_function_pointer,1,0)
363+
#define __ptrauth_block_byref_destroy_helper \
364+
__ptrauth(ptrauth_key_function_pointer,1,0)
365+
#if __has_feature(ptrauth_signed_block_descriptors)
366+
#define __ptrauth_block_descriptor_pointer \
367+
__ptrauth(ptrauth_key_block_descriptor_pointer,1,0xC0BB)
368+
#else
369+
#define __ptrauth_block_descriptor_pointer
370+
#endif
371+
#define __ptrauth_objc_method_list_imp \
372+
__ptrauth(ptrauth_key_function_pointer,1,0)
373+
#if __has_feature(ptrauth_objc_method_list_pointer)
374+
#define __ptrauth_objc_method_list_pointer \
375+
__ptrauth(ptrauth_key_method_list_pointer,1,0xC310)
376+
#else
377+
#define __ptrauth_objc_method_list_pointer
378+
#endif
379+
#if !__has_feature(ptrauth_qualifier_authentication_mode) || \
380+
(__has_feature(ptrauth_objc_isa_signs) || \
381+
__has_feature(ptrauth_objc_isa_strips))
382+
#define __ptrauth_isa_discriminator 0x6AE1
383+
#define __ptrauth_super_discriminator 0xB5AB
384+
#define __ptrauth_objc_isa_pointer \
385+
__ptrauth(ptrauth_key_objc_isa_pointer, 1, \
386+
__ptrauth_isa_discriminator __ptrauth_isa_signing_mode \
387+
__ptrauth_isa_masking_mode)
388+
#define __ptrauth_objc_isa_uintptr \
389+
__ptrauth_restricted_intptr( \
390+
ptrauth_key_objc_isa_pointer, 1, \
391+
__ptrauth_isa_discriminator __ptrauth_isa_signing_mode \
392+
__ptrauth_isa_masking_mode)
393+
#define __ptrauth_objc_super_pointer \
394+
__ptrauth(ptrauth_key_objc_super_pointer, 1, \
395+
__ptrauth_super_discriminator __ptrauth_isa_signing_mode)
396+
#else
397+
#define __ptrauth_objc_isa_pointer
398+
#define __ptrauth_objc_isa_uintptr
399+
#define __ptrauth_objc_super_pointer
400+
#endif
401+
#define __ptrauth_cxx_vtable_pointer \
402+
__ptrauth(ptrauth_key_cxx_vtable_pointer,0,0)
403+
#define __ptrauth_cxx_vtt_vtable_pointer \
404+
__ptrauth(ptrauth_key_cxx_vtable_pointer,0,0)
405+
#define __ptrauth_swift_heap_object_destructor \
406+
__ptrauth(ptrauth_key_function_pointer,1,0xbbbf)
407+
408+
/* Some situations in the C++ and Swift ABIs use declaration-specific
409+
or type-specific extra discriminators. */
410+
#define __ptrauth_cxx_virtual_function_pointer(__declkey) \
411+
__ptrauth(ptrauth_key_function_pointer,1,__declkey)
412+
#define __ptrauth_swift_function_pointer(__typekey) \
413+
__ptrauth(ptrauth_key_function_pointer,0,__typekey)
414+
#define __ptrauth_swift_class_method_pointer(__declkey) \
415+
__ptrauth(ptrauth_key_function_pointer,1,__declkey)
416+
#define __ptrauth_swift_protocol_witness_function_pointer(__declkey) \
417+
__ptrauth(ptrauth_key_function_pointer,1,__declkey)
418+
#define __ptrauth_swift_value_witness_function_pointer(__key) \
419+
__ptrauth(ptrauth_key_function_pointer,1,__key)
420+
318421
#else
319422

320423
#define ptrauth_strip(__value, __key) \
@@ -392,6 +495,29 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
392495
#define ptrauth_cxx_vtable_pointer(key, address_discrimination, \
393496
extra_discrimination...)
394497

498+
499+
#define __ptrauth_function_pointer(__typekey)
500+
#define __ptrauth_return_address
501+
#define __ptrauth_block_invocation_pointer
502+
#define __ptrauth_block_copy_helper
503+
#define __ptrauth_block_destroy_helper
504+
#define __ptrauth_block_byref_copy_helper
505+
#define __ptrauth_block_byref_destroy_helper
506+
#define __ptrauth_block_descriptor_pointer
507+
#define __ptrauth_objc_method_list_imp
508+
#define __ptrauth_objc_method_list_pointer
509+
#define __ptrauth_objc_isa_pointer
510+
#define __ptrauth_objc_isa_uintptr
511+
#define __ptrauth_objc_super_pointer
512+
#define __ptrauth_cxx_vtable_pointer
513+
#define __ptrauth_cxx_vtt_vtable_pointer
514+
#define __ptrauth_swift_heap_object_destructor
515+
#define __ptrauth_cxx_virtual_function_pointer(__declkey)
516+
#define __ptrauth_swift_function_pointer(__typekey)
517+
#define __ptrauth_swift_class_method_pointer(__declkey)
518+
#define __ptrauth_swift_protocol_witness_function_pointer(__declkey)
519+
#define __ptrauth_swift_value_witness_function_pointer(__key)
520+
395521
#endif /* __has_feature(ptrauth_intrinsics) */
396522

397523
#endif /* __PTRAUTH_H */

clang/test/Sema/ptrauth-intrinsics-macro.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,40 @@ void test(int *dp, int value) {
3131

3232
int t2 = ptrauth_sign_generic_data(dp, 0);
3333
(void)t2;
34+
void * __ptrauth_function_pointer(0) p0;
35+
(void)p0;
36+
void * __ptrauth_return_address p1;
37+
(void)p1;
38+
void * __ptrauth_block_invocation_pointer p2;
39+
(void)p2;
40+
void * __ptrauth_block_copy_helper p3;
41+
(void)p3;
42+
void * __ptrauth_block_destroy_helper p4;
43+
(void)p4;
44+
void * __ptrauth_block_byref_copy_helper p5;
45+
(void)p5;
46+
void * __ptrauth_block_byref_destroy_helper p6;
47+
(void)p6;
48+
void * __ptrauth_objc_method_list_imp p7;
49+
(void)p7;
50+
void * __ptrauth_cxx_vtable_pointer p8;
51+
(void)p8;
52+
void * __ptrauth_cxx_vtt_vtable_pointer p9;
53+
(void)p9;
54+
void * __ptrauth_swift_heap_object_destructor p10;
55+
(void)p10;
56+
void * __ptrauth_swift_function_pointer(VALID_CODE_KEY) p11;
57+
(void)p11;
58+
void * __ptrauth_swift_class_method_pointer(VALID_CODE_KEY) p12;
59+
(void)p12;
60+
void * __ptrauth_swift_protocol_witness_function_pointer(VALID_CODE_KEY) p13;
61+
(void)p13;
62+
void * __ptrauth_swift_value_witness_function_pointer(VALID_CODE_KEY) p14;
63+
(void)p14;
64+
void * __ptrauth_objc_method_list_pointer p15;
65+
(void)p15;
66+
void * __ptrauth_block_descriptor_pointer p16;
67+
(void)p16;
3468
}
3569

3670
void test_string_discriminator(int *dp) {

0 commit comments

Comments
 (0)