xref: /xnu-8792.81.2/EXTERNAL_HEADERS/ptrauth.h (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions /*===---- ptrauth.h - Pointer authentication -------------------------------===
2*19c3b8c2SApple OSS Distributions  *
3*19c3b8c2SApple OSS Distributions  * Permission is hereby granted, free of charge, to any person obtaining a copy
4*19c3b8c2SApple OSS Distributions  * of this software and associated documentation files (the "Software"), to deal
5*19c3b8c2SApple OSS Distributions  * in the Software without restriction, including without limitation the rights
6*19c3b8c2SApple OSS Distributions  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*19c3b8c2SApple OSS Distributions  * copies of the Software, and to permit persons to whom the Software is
8*19c3b8c2SApple OSS Distributions  * furnished to do so, subject to the following conditions:
9*19c3b8c2SApple OSS Distributions  *
10*19c3b8c2SApple OSS Distributions  * The above copyright notice and this permission notice shall be included in
11*19c3b8c2SApple OSS Distributions  * all copies or substantial portions of the Software.
12*19c3b8c2SApple OSS Distributions  *
13*19c3b8c2SApple OSS Distributions  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*19c3b8c2SApple OSS Distributions  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*19c3b8c2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*19c3b8c2SApple OSS Distributions  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*19c3b8c2SApple OSS Distributions  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18*19c3b8c2SApple OSS Distributions  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19*19c3b8c2SApple OSS Distributions  * THE SOFTWARE.
20*19c3b8c2SApple OSS Distributions  *
21*19c3b8c2SApple OSS Distributions  *===-----------------------------------------------------------------------===
22*19c3b8c2SApple OSS Distributions  */
23*19c3b8c2SApple OSS Distributions 
24*19c3b8c2SApple OSS Distributions #ifndef __PTRAUTH_H
25*19c3b8c2SApple OSS Distributions #define __PTRAUTH_H
26*19c3b8c2SApple OSS Distributions 
27*19c3b8c2SApple OSS Distributions #include <stdint.h>
28*19c3b8c2SApple OSS Distributions 
29*19c3b8c2SApple OSS Distributions typedef enum {
30*19c3b8c2SApple OSS Distributions   ptrauth_key_asia = 0,
31*19c3b8c2SApple OSS Distributions   ptrauth_key_asib = 1,
32*19c3b8c2SApple OSS Distributions   ptrauth_key_asda = 2,
33*19c3b8c2SApple OSS Distributions   ptrauth_key_asdb = 3,
34*19c3b8c2SApple OSS Distributions 
35*19c3b8c2SApple OSS Distributions   /* A process-independent key which can be used to sign code pointers.
36*19c3b8c2SApple OSS Distributions      Signing and authenticating with this key is a no-op in processes
37*19c3b8c2SApple OSS Distributions      which disable ABI pointer authentication. */
38*19c3b8c2SApple OSS Distributions   ptrauth_key_process_independent_code = ptrauth_key_asia,
39*19c3b8c2SApple OSS Distributions 
40*19c3b8c2SApple OSS Distributions   /* A process-specific key which can be used to sign code pointers.
41*19c3b8c2SApple OSS Distributions      Signing and authenticating with this key is enforced even in processes
42*19c3b8c2SApple OSS Distributions      which disable ABI pointer authentication. */
43*19c3b8c2SApple OSS Distributions   ptrauth_key_process_dependent_code = ptrauth_key_asib,
44*19c3b8c2SApple OSS Distributions 
45*19c3b8c2SApple OSS Distributions   /* A process-independent key which can be used to sign data pointers.
46*19c3b8c2SApple OSS Distributions      Signing and authenticating with this key is a no-op in processes
47*19c3b8c2SApple OSS Distributions      which disable ABI pointer authentication. */
48*19c3b8c2SApple OSS Distributions   ptrauth_key_process_independent_data = ptrauth_key_asda,
49*19c3b8c2SApple OSS Distributions 
50*19c3b8c2SApple OSS Distributions   /* A process-specific key which can be used to sign data pointers.
51*19c3b8c2SApple OSS Distributions      Signing and authenticating with this key is a no-op in processes
52*19c3b8c2SApple OSS Distributions      which disable ABI pointer authentication. */
53*19c3b8c2SApple OSS Distributions   ptrauth_key_process_dependent_data = ptrauth_key_asdb,
54*19c3b8c2SApple OSS Distributions 
55*19c3b8c2SApple OSS Distributions   /* The key used to sign C function pointers.
56*19c3b8c2SApple OSS Distributions      The extra data is always 0. */
57*19c3b8c2SApple OSS Distributions   ptrauth_key_function_pointer = ptrauth_key_process_independent_code,
58*19c3b8c2SApple OSS Distributions 
59*19c3b8c2SApple OSS Distributions   /* The key used to sign return addresses on the stack.
60*19c3b8c2SApple OSS Distributions      The extra data is based on the storage address of the return address.
61*19c3b8c2SApple OSS Distributions      On ARM64, that is always the storage address of the return address plus 8
62*19c3b8c2SApple OSS Distributions      (or, in other words, the value of the stack pointer on function entry) */
63*19c3b8c2SApple OSS Distributions   ptrauth_key_return_address = ptrauth_key_process_dependent_code,
64*19c3b8c2SApple OSS Distributions 
65*19c3b8c2SApple OSS Distributions   /* The key used to sign frame pointers on the stack.
66*19c3b8c2SApple OSS Distributions      The extra data is based on the storage address of the frame pointer.
67*19c3b8c2SApple OSS Distributions      On ARM64, that is always the storage address of the frame pointer plus 16
68*19c3b8c2SApple OSS Distributions      (or, in other words, the value of the stack pointer on function entry) */
69*19c3b8c2SApple OSS Distributions   ptrauth_key_frame_pointer = ptrauth_key_process_dependent_data,
70*19c3b8c2SApple OSS Distributions 
71*19c3b8c2SApple OSS Distributions   /* The key used to sign block function pointers, including:
72*19c3b8c2SApple OSS Distributions        invocation functions,
73*19c3b8c2SApple OSS Distributions        block object copy functions,
74*19c3b8c2SApple OSS Distributions        block object destroy functions,
75*19c3b8c2SApple OSS Distributions        __block variable copy functions, and
76*19c3b8c2SApple OSS Distributions        __block variable destroy functions.
77*19c3b8c2SApple OSS Distributions      The extra data is always the address at which the function pointer
78*19c3b8c2SApple OSS Distributions      is stored.
79*19c3b8c2SApple OSS Distributions 
80*19c3b8c2SApple OSS Distributions      Note that block object pointers themselves (i.e. the direct
81*19c3b8c2SApple OSS Distributions      representations of values of block-pointer type) are not signed. */
82*19c3b8c2SApple OSS Distributions   ptrauth_key_block_function = ptrauth_key_asia,
83*19c3b8c2SApple OSS Distributions 
84*19c3b8c2SApple OSS Distributions   /* The key used to sign C++ v-table pointers.
85*19c3b8c2SApple OSS Distributions      The extra data is always 0. */
86*19c3b8c2SApple OSS Distributions   ptrauth_key_cxx_vtable_pointer = ptrauth_key_asda,
87*19c3b8c2SApple OSS Distributions 
88*19c3b8c2SApple OSS Distributions   /* Other pointers signed under the ABI use private ABI rules. */
89*19c3b8c2SApple OSS Distributions 
90*19c3b8c2SApple OSS Distributions } ptrauth_key;
91*19c3b8c2SApple OSS Distributions 
92*19c3b8c2SApple OSS Distributions /* An integer type of the appropriate size for an extra-data argument. */
93*19c3b8c2SApple OSS Distributions typedef uintptr_t ptrauth_extra_data_t;
94*19c3b8c2SApple OSS Distributions 
95*19c3b8c2SApple OSS Distributions /* An integer type of the appropriate size for a generic signature. */
96*19c3b8c2SApple OSS Distributions typedef uintptr_t ptrauth_generic_signature_t;
97*19c3b8c2SApple OSS Distributions 
98*19c3b8c2SApple OSS Distributions /* A signed pointer value embeds the original pointer together with
99*19c3b8c2SApple OSS Distributions    a signature that attests to the validity of that pointer.  Because
100*19c3b8c2SApple OSS Distributions    this signature must use only "spare" bits of the pointer, a
101*19c3b8c2SApple OSS Distributions    signature's validity is probabilistic in practice: it is unlikely
102*19c3b8c2SApple OSS Distributions    but still plausible that an invalidly-derived signature will
103*19c3b8c2SApple OSS Distributions    somehow equal the correct signature and therefore successfully
104*19c3b8c2SApple OSS Distributions    authenticate.  Nonetheless, this scheme provides a strong degree
105*19c3b8c2SApple OSS Distributions    of protection against certain kinds of attacks. */
106*19c3b8c2SApple OSS Distributions 
107*19c3b8c2SApple OSS Distributions /* Authenticating a pointer that was not signed with the given key
108*19c3b8c2SApple OSS Distributions    and extra-data value will (likely) fail.  However, an
109*19c3b8c2SApple OSS Distributions    authentication failure will not lead immediately to a trap.
110*19c3b8c2SApple OSS Distributions    Instead, it will yield a value which is guaranteed to trap
111*19c3b8c2SApple OSS Distributions    if actually dereferenced. */
112*19c3b8c2SApple OSS Distributions 
113*19c3b8c2SApple OSS Distributions /* The null function pointer is always the all-zero bit pattern.
114*19c3b8c2SApple OSS Distributions    Signing an all-zero bit pattern will embed a (likely) non-zero
115*19c3b8c2SApple OSS Distributions    signature in the result, and so the result will not seem to be
116*19c3b8c2SApple OSS Distributions    a null function pointer.  Authenticating this value will yield
117*19c3b8c2SApple OSS Distributions    a null function pointer back.  However, authenticating an
118*19c3b8c2SApple OSS Distributions    all-zero bit pattern will probably fail, because the
119*19c3b8c2SApple OSS Distributions    authentication will expect a (likely) non-zero signature to
120*19c3b8c2SApple OSS Distributions    embedded in the value.
121*19c3b8c2SApple OSS Distributions 
122*19c3b8c2SApple OSS Distributions    Because of this, if a pointer may validly be null, you should
123*19c3b8c2SApple OSS Distributions    check for null before attempting to authenticate it. */
124*19c3b8c2SApple OSS Distributions 
125*19c3b8c2SApple OSS Distributions #ifdef __PTRAUTH_INTRINSICS__
126*19c3b8c2SApple OSS Distributions 
127*19c3b8c2SApple OSS Distributions /* Strip the signature from a value without authenticating it.
128*19c3b8c2SApple OSS Distributions 
129*19c3b8c2SApple OSS Distributions    If the value is a function pointer, the result will not be a
130*19c3b8c2SApple OSS Distributions    legal function pointer because of the missing signature, and
131*19c3b8c2SApple OSS Distributions    attempting to call it will result in an authentication failure.
132*19c3b8c2SApple OSS Distributions 
133*19c3b8c2SApple OSS Distributions    The value must be an expression of pointer type.
134*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
135*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value. */
136*19c3b8c2SApple OSS Distributions #define ptrauth_strip(__value, __key) \
137*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_strip(__value, __key)
138*19c3b8c2SApple OSS Distributions 
139*19c3b8c2SApple OSS Distributions /* Blend a pointer and a small integer to form a new extra-data
140*19c3b8c2SApple OSS Distributions    discriminator.  Not all bits of the inputs are guaranteed to
141*19c3b8c2SApple OSS Distributions    contribute to the result.
142*19c3b8c2SApple OSS Distributions 
143*19c3b8c2SApple OSS Distributions    On ARM64, only the low 16 bits of the integer will be considered.
144*19c3b8c2SApple OSS Distributions 
145*19c3b8c2SApple OSS Distributions    For the purposes of ptrauth_sign_constant, the result of calling
146*19c3b8c2SApple OSS Distributions    this function is considered a constant expression if the arguments
147*19c3b8c2SApple OSS Distributions    are constant.  Some restrictions may be imposed on the pointer.
148*19c3b8c2SApple OSS Distributions 
149*19c3b8c2SApple OSS Distributions    The first argument must be an expression of pointer type.
150*19c3b8c2SApple OSS Distributions    The second argument must be an expression of integer type.
151*19c3b8c2SApple OSS Distributions    The result will have type uintptr_t. */
152*19c3b8c2SApple OSS Distributions #define ptrauth_blend_discriminator(__pointer, __integer) \
153*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_blend_discriminator(__pointer, __integer)
154*19c3b8c2SApple OSS Distributions 
155*19c3b8c2SApple OSS Distributions /* Compute the 16-bit integer discriminator of the given type.
156*19c3b8c2SApple OSS Distributions 
157*19c3b8c2SApple OSS Distributions    The argument must be a type.
158*19c3b8c2SApple OSS Distributions */
159*19c3b8c2SApple OSS Distributions #if __has_builtin(__builtin_ptrauth_type_discriminator)
160*19c3b8c2SApple OSS Distributions #define ptrauth_type_discriminator(__type) \
161*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_type_discriminator(__type)
162*19c3b8c2SApple OSS Distributions #else
163*19c3b8c2SApple OSS Distributions #define ptrauth_type_discriminator(__type) ((uintptr_t)0)
164*19c3b8c2SApple OSS Distributions #endif
165*19c3b8c2SApple OSS Distributions 
166*19c3b8c2SApple OSS Distributions /* Compute the constant discriminator used by Clang to sign pointers with the
167*19c3b8c2SApple OSS Distributions    given C function pointer type.
168*19c3b8c2SApple OSS Distributions 
169*19c3b8c2SApple OSS Distributions    A call to this function is an integer constant expression*/
170*19c3b8c2SApple OSS Distributions #if __has_feature(ptrauth_function_pointer_type_discrimination)
171*19c3b8c2SApple OSS Distributions #define ptrauth_function_pointer_type_discriminator(__type) \
172*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_type_discriminator(__type)
173*19c3b8c2SApple OSS Distributions #else
174*19c3b8c2SApple OSS Distributions #define ptrauth_function_pointer_type_discriminator(__type) ((uintptr_t)0)
175*19c3b8c2SApple OSS Distributions #endif
176*19c3b8c2SApple OSS Distributions 
177*19c3b8c2SApple OSS Distributions /* Add a signature to the given pointer value using a specific key,
178*19c3b8c2SApple OSS Distributions    using the given extra data as a salt to the signing process.
179*19c3b8c2SApple OSS Distributions 
180*19c3b8c2SApple OSS Distributions    The value must be a constant expression of pointer type.
181*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
182*19c3b8c2SApple OSS Distributions    The extra data must be a constant expression of pointer or integer type;
183*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
184*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value.
185*19c3b8c2SApple OSS Distributions 
186*19c3b8c2SApple OSS Distributions    This is a constant expression if the extra data is an integer or
187*19c3b8c2SApple OSS Distributions    null pointer constant. */
188*19c3b8c2SApple OSS Distributions #define ptrauth_sign_constant(__value, __key, __data) \
189*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_sign_constant(__value, __key, __data)
190*19c3b8c2SApple OSS Distributions 
191*19c3b8c2SApple OSS Distributions /* Add a signature to the given pointer value using a specific key,
192*19c3b8c2SApple OSS Distributions    using the given extra data as a salt to the signing process.
193*19c3b8c2SApple OSS Distributions 
194*19c3b8c2SApple OSS Distributions    This operation does not authenticate the original value and is
195*19c3b8c2SApple OSS Distributions    therefore potentially insecure if an attacker could possibly
196*19c3b8c2SApple OSS Distributions    control that value.
197*19c3b8c2SApple OSS Distributions 
198*19c3b8c2SApple OSS Distributions    The value must be an expression of pointer type.
199*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
200*19c3b8c2SApple OSS Distributions    The extra data must be an expression of pointer or integer type;
201*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
202*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value. */
203*19c3b8c2SApple OSS Distributions #define ptrauth_sign_unauthenticated(__value, __key, __data) \
204*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_sign_unauthenticated(__value, __key, __data)
205*19c3b8c2SApple OSS Distributions 
206*19c3b8c2SApple OSS Distributions /* Authenticate a pointer using one scheme and resign it using another.
207*19c3b8c2SApple OSS Distributions 
208*19c3b8c2SApple OSS Distributions    If the result is subsequently authenticated using the new scheme, that
209*19c3b8c2SApple OSS Distributions    authentication is guaranteed to fail if and only if the initial
210*19c3b8c2SApple OSS Distributions    authentication failed.
211*19c3b8c2SApple OSS Distributions 
212*19c3b8c2SApple OSS Distributions    The value must be an expression of pointer type.
213*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
214*19c3b8c2SApple OSS Distributions    The extra data must be an expression of pointer or integer type;
215*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
216*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value.
217*19c3b8c2SApple OSS Distributions 
218*19c3b8c2SApple OSS Distributions    This operation is guaranteed to not leave the intermediate value
219*19c3b8c2SApple OSS Distributions    available for attack before it is re-signed.
220*19c3b8c2SApple OSS Distributions 
221*19c3b8c2SApple OSS Distributions    Do not pass a null pointer to this function. A null pointer
222*19c3b8c2SApple OSS Distributions    will not successfully authenticate. */
223*19c3b8c2SApple OSS Distributions #define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data) \
224*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data)
225*19c3b8c2SApple OSS Distributions 
226*19c3b8c2SApple OSS Distributions /* Authenticate a pointer using one scheme and resign it as a C
227*19c3b8c2SApple OSS Distributions    function pointer.
228*19c3b8c2SApple OSS Distributions 
229*19c3b8c2SApple OSS Distributions    If the result is subsequently authenticated using the new scheme, that
230*19c3b8c2SApple OSS Distributions    authentication is guaranteed to fail if and only if the initial
231*19c3b8c2SApple OSS Distributions    authentication failed.
232*19c3b8c2SApple OSS Distributions 
233*19c3b8c2SApple OSS Distributions    The value must be an expression of function pointer type.
234*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
235*19c3b8c2SApple OSS Distributions    The extra data must be an expression of pointer or integer type;
236*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
237*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value.
238*19c3b8c2SApple OSS Distributions 
239*19c3b8c2SApple OSS Distributions    This operation is guaranteed to not leave the intermediate value
240*19c3b8c2SApple OSS Distributions    available for attack before it is re-signed. Additionally, if this
241*19c3b8c2SApple OSS Distributions    expression is used syntactically as the function expression in a
242*19c3b8c2SApple OSS Distributions    call, only a single authentication will be performed. */
243*19c3b8c2SApple OSS Distributions #define ptrauth_auth_function(__value, __old_key, __old_data) \
244*19c3b8c2SApple OSS Distributions   ptrauth_auth_and_resign(__value, __old_key, __old_data, ptrauth_key_function_pointer, 0)
245*19c3b8c2SApple OSS Distributions 
246*19c3b8c2SApple OSS Distributions /* Cast a pointer to the given type without changing any signature.
247*19c3b8c2SApple OSS Distributions 
248*19c3b8c2SApple OSS Distributions    The type must have the same size as a pointer type.
249*19c3b8c2SApple OSS Distributions    The type of value must have the same size as a pointer type, and will be
250*19c3b8c2SApple OSS Distributions    converted to an rvalue prior to the cast.
251*19c3b8c2SApple OSS Distributions    The result has type given by the first argument.
252*19c3b8c2SApple OSS Distributions 
253*19c3b8c2SApple OSS Distributions    The result has an identical bit-pattern to the input pointer. */
254*19c3b8c2SApple OSS Distributions #define ptrauth_nop_cast(__type, __value)        \
255*19c3b8c2SApple OSS Distributions   ({ union {                                     \
256*19c3b8c2SApple OSS Distributions       typeof(__value) __fptr;                    \
257*19c3b8c2SApple OSS Distributions       typeof(__type) __opaque;                   \
258*19c3b8c2SApple OSS Distributions   } __storage;                                   \
259*19c3b8c2SApple OSS Distributions   __storage.__fptr = (__value);                  \
260*19c3b8c2SApple OSS Distributions   __storage.__opaque; })
261*19c3b8c2SApple OSS Distributions 
262*19c3b8c2SApple OSS Distributions /* Authenticate a data pointer.
263*19c3b8c2SApple OSS Distributions 
264*19c3b8c2SApple OSS Distributions    The value must be an expression of non-function pointer type.
265*19c3b8c2SApple OSS Distributions    The key must be a constant expression of type ptrauth_key.
266*19c3b8c2SApple OSS Distributions    The extra data must be an expression of pointer or integer type;
267*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
268*19c3b8c2SApple OSS Distributions    The result will have the same type as the original value.
269*19c3b8c2SApple OSS Distributions 
270*19c3b8c2SApple OSS Distributions    If the authentication fails, dereferencing the resulting pointer
271*19c3b8c2SApple OSS Distributions    will fail. */
272*19c3b8c2SApple OSS Distributions #define ptrauth_auth_data(__value, __old_key, __old_data) \
273*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_auth(__value, __old_key, __old_data)
274*19c3b8c2SApple OSS Distributions 
275*19c3b8c2SApple OSS Distributions /* Return an extra-discriminator value which can validly be used
276*19c3b8c2SApple OSS Distributions    as the second argument to ptrauth_blend_discriminator or the
277*19c3b8c2SApple OSS Distributions    third argument to the __ptrauth qualifier.
278*19c3b8c2SApple OSS Distributions 
279*19c3b8c2SApple OSS Distributions    The argument must be a string literal.
280*19c3b8c2SApple OSS Distributions    A call to this function is an integer constant expression. */
281*19c3b8c2SApple OSS Distributions #define ptrauth_string_discriminator(__string) \
282*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_string_discriminator(__string)
283*19c3b8c2SApple OSS Distributions 
284*19c3b8c2SApple OSS Distributions /* Compute a full pointer-width generic signature for the given
285*19c3b8c2SApple OSS Distributions    value, using the given data as a salt.
286*19c3b8c2SApple OSS Distributions 
287*19c3b8c2SApple OSS Distributions    This generic signature is process-independent, but may not be
288*19c3b8c2SApple OSS Distributions    consistent across reboots.
289*19c3b8c2SApple OSS Distributions 
290*19c3b8c2SApple OSS Distributions    This can be used to validate the integrity of arbitrary data
291*19c3b8c2SApple OSS Distributions    by storing a signature for that data together with it.  Because
292*19c3b8c2SApple OSS Distributions    the signature is pointer-sized, if the stored signature matches
293*19c3b8c2SApple OSS Distributions    the result of re-signing the current data, a match provides very
294*19c3b8c2SApple OSS Distributions    strong evidence that the data has not been corrupted.
295*19c3b8c2SApple OSS Distributions 
296*19c3b8c2SApple OSS Distributions    The value must be an expression of pointer or integer type; if
297*19c3b8c2SApple OSS Distributions    an integer, it will be coerced to uintptr_t.
298*19c3b8c2SApple OSS Distributions    The extra data must be an expression of pointer or integer type;
299*19c3b8c2SApple OSS Distributions    if an integer, it will be coerced to ptrauth_extra_data_t.
300*19c3b8c2SApple OSS Distributions    The result will have type ptrauth_generic_signature_t.
301*19c3b8c2SApple OSS Distributions 
302*19c3b8c2SApple OSS Distributions    This operation will compute a meaningful signature even in processes
303*19c3b8c2SApple OSS Distributions    which disable ABI pointer authentication. */
304*19c3b8c2SApple OSS Distributions #define ptrauth_sign_generic_data(__value, __data) \
305*19c3b8c2SApple OSS Distributions   __builtin_ptrauth_sign_generic_data(__value, __data)
306*19c3b8c2SApple OSS Distributions 
307*19c3b8c2SApple OSS Distributions 
308*19c3b8c2SApple OSS Distributions /* Define some standard __ptrauth qualifiers used in the ABI. */
309*19c3b8c2SApple OSS Distributions #define __ptrauth_function_pointer            \
310*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,0,0)
311*19c3b8c2SApple OSS Distributions #define __ptrauth_return_address              \
312*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_return_address,1,0)
313*19c3b8c2SApple OSS Distributions #define __ptrauth_block_invocation_pointer    \
314*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
315*19c3b8c2SApple OSS Distributions #define __ptrauth_block_copy_helper           \
316*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
317*19c3b8c2SApple OSS Distributions #define __ptrauth_block_destroy_helper        \
318*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
319*19c3b8c2SApple OSS Distributions #define __ptrauth_block_byref_copy_helper     \
320*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
321*19c3b8c2SApple OSS Distributions #define __ptrauth_block_byref_destroy_helper  \
322*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
323*19c3b8c2SApple OSS Distributions #define __ptrauth_objc_method_list_imp        \
324*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0)
325*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_vtable_pointer          \
326*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_cxx_vtable_pointer,0,0)
327*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_vtt_vtable_pointer      \
328*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_cxx_vtable_pointer,0,0)
329*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_heap_object_destructor \
330*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,0xbbbf)
331*19c3b8c2SApple OSS Distributions 
332*19c3b8c2SApple OSS Distributions /* Some situations in the C++ and Swift ABIs use declaration-specific
333*19c3b8c2SApple OSS Distributions    or type-specific extra discriminators. */
334*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_virtual_function_pointer(__declkey) \
335*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,__declkey)
336*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_function_pointer(__typekey) \
337*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,0,__typekey)
338*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_class_method_pointer(__declkey) \
339*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,__declkey)
340*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_protocol_witness_function_pointer(__declkey) \
341*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,__declkey)
342*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_value_witness_function_pointer(__key) \
343*19c3b8c2SApple OSS Distributions   __ptrauth(ptrauth_key_function_pointer,1,__key)
344*19c3b8c2SApple OSS Distributions 
345*19c3b8c2SApple OSS Distributions #else
346*19c3b8c2SApple OSS Distributions 
347*19c3b8c2SApple OSS Distributions #define ptrauth_strip(__value, __key) ({ (void)__key; __value; })
348*19c3b8c2SApple OSS Distributions #define ptrauth_blend_discriminator(__pointer, __integer) ({ (void)__pointer; (void)__integer; (uintptr_t)0; })
349*19c3b8c2SApple OSS Distributions #define ptrauth_type_discriminator(__type) ((uintptr_t)0)
350*19c3b8c2SApple OSS Distributions #define ptrauth_function_pointer_type_discriminator(__type) ((uintptr_t)0)
351*19c3b8c2SApple OSS Distributions #define ptrauth_sign_constant(__value, __key, __data) ({ (void)__key; (void)__data; __value; })
352*19c3b8c2SApple OSS Distributions #define ptrauth_sign_unauthenticated(__value, __key, __data) ({ (void)__key; (void)__data; __value; })
353*19c3b8c2SApple OSS Distributions #define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data) ({ \
354*19c3b8c2SApple OSS Distributions       (void)__old_key; \
355*19c3b8c2SApple OSS Distributions       (void)__old_data; \
356*19c3b8c2SApple OSS Distributions       (void)__new_key; \
357*19c3b8c2SApple OSS Distributions       (void)__new_data; \
358*19c3b8c2SApple OSS Distributions       __value; })
359*19c3b8c2SApple OSS Distributions #define ptrauth_auth_function(__value, __old_key, __old_data) ({ (void)__old_key; (void)__old_data; __value; })
360*19c3b8c2SApple OSS Distributions #define ptrauth_nop_cast(__type, __value) ((__type)__value)
361*19c3b8c2SApple OSS Distributions #define ptrauth_auth_data(__value, __old_key, __old_data) ({ (void)__old_key; (void)__old_data; __value; })
362*19c3b8c2SApple OSS Distributions #define ptrauth_string_discriminator(__string) ({ (void)__string; (int)0; })
363*19c3b8c2SApple OSS Distributions #define ptrauth_sign_generic_data(__value, __data) ({ (void)__value; (void)__data; (ptrauth_generic_signature_t)0; })
364*19c3b8c2SApple OSS Distributions 
365*19c3b8c2SApple OSS Distributions #define __ptrauth_function_pointer
366*19c3b8c2SApple OSS Distributions #define __ptrauth_return_address
367*19c3b8c2SApple OSS Distributions #define __ptrauth_block_invocation_pointer
368*19c3b8c2SApple OSS Distributions #define __ptrauth_block_copy_helper
369*19c3b8c2SApple OSS Distributions #define __ptrauth_block_destroy_helper
370*19c3b8c2SApple OSS Distributions #define __ptrauth_block_byref_copy_helper
371*19c3b8c2SApple OSS Distributions #define __ptrauth_block_byref_destroy_helper
372*19c3b8c2SApple OSS Distributions #define __ptrauth_objc_method_list_imp
373*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_vtable_pointer
374*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_vtt_vtable_pointer
375*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_heap_object_destructor
376*19c3b8c2SApple OSS Distributions #define __ptrauth_cxx_virtual_function_pointer(__declkey)
377*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_function_pointer(__typekey)
378*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_class_method_pointer(__declkey)
379*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_protocol_witness_function_pointer(__declkey)
380*19c3b8c2SApple OSS Distributions #define __ptrauth_swift_value_witness_function_pointer(__key)
381*19c3b8c2SApple OSS Distributions 
382*19c3b8c2SApple OSS Distributions #endif /* __PTRAUTH_INTRINSICS__ */
383*19c3b8c2SApple OSS Distributions 
384*19c3b8c2SApple OSS Distributions #endif /* __PTRAUTH_H */
385