xref: /xnu-10002.81.5/EXTERNAL_HEADERS/ptrcheck.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*===---- ptrcheck.h - Pointer bounds hints & specifications ----------------===
2*5e3eaea3SApple OSS Distributions  *
3*5e3eaea3SApple OSS Distributions  * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*5e3eaea3SApple OSS Distributions  * See https://llvm.org/LICENSE.txt for license information.
5*5e3eaea3SApple OSS Distributions  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*5e3eaea3SApple OSS Distributions  *
7*5e3eaea3SApple OSS Distributions  *===-----------------------------------------------------------------------===
8*5e3eaea3SApple OSS Distributions  */
9*5e3eaea3SApple OSS Distributions 
10*5e3eaea3SApple OSS Distributions #ifndef __PTRCHECK_H
11*5e3eaea3SApple OSS Distributions #define __PTRCHECK_H
12*5e3eaea3SApple OSS Distributions 
13*5e3eaea3SApple OSS Distributions /* __has_ptrcheck can be used in preprocessor macros (and other parts of the
14*5e3eaea3SApple OSS Distributions    language expecting constant expressions) to test if bounds attributes
15*5e3eaea3SApple OSS Distributions    exist. */
16*5e3eaea3SApple OSS Distributions #if defined(__has_feature) && __has_feature(bounds_attributes)
17*5e3eaea3SApple OSS Distributions   #define __has_ptrcheck 1
18*5e3eaea3SApple OSS Distributions #else
19*5e3eaea3SApple OSS Distributions   #define __has_ptrcheck 0
20*5e3eaea3SApple OSS Distributions #endif
21*5e3eaea3SApple OSS Distributions 
22*5e3eaea3SApple OSS Distributions #if __has_ptrcheck
23*5e3eaea3SApple OSS Distributions 
24*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such that its ABI is three pointer
25*5e3eaea3SApple OSS Distributions    components: the pointer value itself (the pointer value); one-past-the-end of
26*5e3eaea3SApple OSS Distributions    the object it is derived from (the upper bound); and the base address of the
27*5e3eaea3SApple OSS Distributions    object it is derived from (the lower bound). The pointer value is allowed to
28*5e3eaea3SApple OSS Distributions    lie outside the [lower bound, upper bound) interval, and it supports the
29*5e3eaea3SApple OSS Distributions    entire range of arithmetic operations that are usually applicable to
30*5e3eaea3SApple OSS Distributions    pointers. Bounds are implicitly checked only when the pointer is dereferenced
31*5e3eaea3SApple OSS Distributions    or converted to a different representation. */
32*5e3eaea3SApple OSS Distributions #define __bidi_indexable __attribute__((__bidi_indexable__))
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such that its ABI is two pointer
35*5e3eaea3SApple OSS Distributions    components: the pointer value itself (the lower bound); and one-past-the-end
36*5e3eaea3SApple OSS Distributions    of the object it is derived from (the upper bound). Indexable pointers do not
37*5e3eaea3SApple OSS Distributions    support negative arithmetic operations: it is a compile-time error to use a
38*5e3eaea3SApple OSS Distributions    subtraction or add a negative quantity to them, and it is a runtime error if
39*5e3eaea3SApple OSS Distributions    the same happens at runtime while it can't be detected at compile-time. Same
40*5e3eaea3SApple OSS Distributions    as __bidi_indexable pointers, __indexable pointers are bounds-checked when
41*5e3eaea3SApple OSS Distributions    dereferenced or converted to another representation. */
42*5e3eaea3SApple OSS Distributions #define __indexable __attribute__((__indexable__))
43*5e3eaea3SApple OSS Distributions 
44*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such than it has the ABI of a
45*5e3eaea3SApple OSS Distributions    regular C pointer, without allowing pointer arithmetic. Pointer arithmetic is
46*5e3eaea3SApple OSS Distributions    a compile-time error. A __single pointer is expected to be either NULL or
47*5e3eaea3SApple OSS Distributions    point to exactly one valid value. */
48*5e3eaea3SApple OSS Distributions #define __single __attribute__((__single__))
49*5e3eaea3SApple OSS Distributions 
50*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such than it can be used exactly
51*5e3eaea3SApple OSS Distributions    like a regular C pointer, with unchecked arithmetic and dereferencing. An
52*5e3eaea3SApple OSS Distributions    __unsafe_indexable pointer cannot convert implicitly to another type of
53*5e3eaea3SApple OSS Distributions    pointer since that would require information that is not available to the
54*5e3eaea3SApple OSS Distributions    program. You must use __unsafe_forge_bidi_indexable or __unsafe_forge_single
55*5e3eaea3SApple OSS Distributions    to convert __unsafe_indexable pointers to so-called safe pointers. */
56*5e3eaea3SApple OSS Distributions #define __unsafe_indexable __attribute__((__unsafe_indexable__))
57*5e3eaea3SApple OSS Distributions 
58*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such that it has the ABI of a
59*5e3eaea3SApple OSS Distributions    regular C pointer, but it implicitly converts to a __bidi_indexable pointer
60*5e3eaea3SApple OSS Distributions    with bounds that assume there are N valid elements starting at its address.
61*5e3eaea3SApple OSS Distributions    The conversion happens at the same point the object converts to an rvalue, or
62*5e3eaea3SApple OSS Distributions    immediately for values which cannot be lvalues (such as function calls). */
63*5e3eaea3SApple OSS Distributions 
64*5e3eaea3SApple OSS Distributions /* Assignments to the pointer object must be accompanied with an assignment to
65*5e3eaea3SApple OSS Distributions    N if it is assignable. */
66*5e3eaea3SApple OSS Distributions 
67*5e3eaea3SApple OSS Distributions /* N must either be an expression that evaluates to a constant, or an integer
68*5e3eaea3SApple OSS Distributions    declaration from the same scope, or (for structure fields) a declaration
69*5e3eaea3SApple OSS Distributions    contained in basic arithmetic. */
70*5e3eaea3SApple OSS Distributions #define __counted_by(N) __attribute__((__counted_by__(N)))
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions /* Identical to __counted_by(N), aside that N is a byte count instead of an
73*5e3eaea3SApple OSS Distributions    object count. */
74*5e3eaea3SApple OSS Distributions #define __sized_by(N) __attribute__((__sized_by__(N)))
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions /* An attribute that modifies a pointer type such that it has the ABI of a
77*5e3eaea3SApple OSS Distributions    regular C pointer, but it implicitly converts to a __bidi_indexable pointer
78*5e3eaea3SApple OSS Distributions    with bounds that assume that E is one-past-the-end of the original object.
79*5e3eaea3SApple OSS Distributions    Implicitly, referencing E in the same scope will create a pointer that
80*5e3eaea3SApple OSS Distributions    converts to a __bidi_indexable pointer one-past-the-end of the original
81*5e3eaea3SApple OSS Distributions    object, but with a lower bound set to the value of the pointer that is
82*5e3eaea3SApple OSS Distributions    attributed. */
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions /* Assignments to the pointer object must be accompanied with an assignment to
85*5e3eaea3SApple OSS Distributions    E if it is assignable. */
86*5e3eaea3SApple OSS Distributions #define __ended_by(E) __attribute__((__ended_by__(E)))
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions /* The __terminated_by(T) attribute can be applied to arrays and pointers. The
89*5e3eaea3SApple OSS Distributions    argument T specifies the terminator and must be an integer constant
90*5e3eaea3SApple OSS Distributions    expression. Even though T has to be an integer constant, __terminated_by(T)
91*5e3eaea3SApple OSS Distributions    can be applied to pointer arrays as well. For convenience, the
92*5e3eaea3SApple OSS Distributions    __null_terminated macro is provided, which is equivalent to
93*5e3eaea3SApple OSS Distributions    __terminated_by(0).
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions    The __terminated_by(T) attribute can be applied only to __single pointers. If
96*5e3eaea3SApple OSS Distributions    the pointer attribute is not specified, it is automatically set to __single.
97*5e3eaea3SApple OSS Distributions    A __terminated_by(T) pointer points to the first element of an array that is
98*5e3eaea3SApple OSS Distributions    terminated with T.
99*5e3eaea3SApple OSS Distributions 
100*5e3eaea3SApple OSS Distributions    Arithmetic on __terminated_by(T) pointers is restricted to only incrementing
101*5e3eaea3SApple OSS Distributions    the pointer by one, and must be able to be evaluated at compile-time.
102*5e3eaea3SApple OSS Distributions    Pointer arithmetic generates a runtime check to ensure that the pointer
103*5e3eaea3SApple OSS Distributions    doesn't point pass the terminator.
104*5e3eaea3SApple OSS Distributions 
105*5e3eaea3SApple OSS Distributions    A __terminated_by(T) pointer has the ABI of a regular C pointer.
106*5e3eaea3SApple OSS Distributions 
107*5e3eaea3SApple OSS Distributions    When __terminated_by(T) is applied to an array, the compiler checks if the
108*5e3eaea3SApple OSS Distributions    array is terminated with the given terminator T during the initialization.
109*5e3eaea3SApple OSS Distributions    Moreover, a __terminated_by(T) array decays to a __terminated_by(T) __single
110*5e3eaea3SApple OSS Distributions    pointer, instead of decaying to a __bidi_indexable pointer. */
111*5e3eaea3SApple OSS Distributions #define __terminated_by(T) __attribute__((__terminated_by__(T)))
112*5e3eaea3SApple OSS Distributions #define __null_terminated __terminated_by(0)
113*5e3eaea3SApple OSS Distributions 
114*5e3eaea3SApple OSS Distributions /* Directives that tells the compiler to assume that subsequent pointer types
115*5e3eaea3SApple OSS Distributions    have the ABI specified by the ABI parameter, which may be one of single,
116*5e3eaea3SApple OSS Distributions    indexable, bidi_indexable or unsafe_indexable. */
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions /* In project files, the ABI is assumed to be single by default. In headers
119*5e3eaea3SApple OSS Distributions    included from libraries or the SDK, the ABI is assumed to be unsafe_indexable
120*5e3eaea3SApple OSS Distributions    by default. */
121*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_single() \
122*5e3eaea3SApple OSS Distributions   _Pragma("clang abi_ptr_attr set(single)")
123*5e3eaea3SApple OSS Distributions 
124*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_indexable() \
125*5e3eaea3SApple OSS Distributions   _Pragma("clang abi_ptr_attr set(indexable)")
126*5e3eaea3SApple OSS Distributions 
127*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_bidi_indexable() \
128*5e3eaea3SApple OSS Distributions   _Pragma("clang abi_ptr_attr set(bidi_indexable)")
129*5e3eaea3SApple OSS Distributions 
130*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_unsafe_indexable() \
131*5e3eaea3SApple OSS Distributions   _Pragma("clang abi_ptr_attr set(unsafe_indexable)")
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions /* Create a __bidi_indexable pointer of a given pointer type (T), starting at
134*5e3eaea3SApple OSS Distributions    address P, pointing to S bytes of valid memory. T must be a pointer type. */
135*5e3eaea3SApple OSS Distributions #define __unsafe_forge_bidi_indexable(T, P, S) \
136*5e3eaea3SApple OSS Distributions   ((T __bidi_indexable)__builtin_unsafe_forge_bidi_indexable((P), (S)))
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions /* Create a __single pointer of a given type (T), starting at address P. T must
139*5e3eaea3SApple OSS Distributions    be a pointer type. */
140*5e3eaea3SApple OSS Distributions #define __unsafe_forge_single(T, P) \
141*5e3eaea3SApple OSS Distributions   ((T __single)__builtin_unsafe_forge_single((P)))
142*5e3eaea3SApple OSS Distributions 
143*5e3eaea3SApple OSS Distributions /* Create a wide pointer with the same lower bound and upper bounds as X, but
144*5e3eaea3SApple OSS Distributions    with a pointer component also equal to the lower bound. */
145*5e3eaea3SApple OSS Distributions #define __ptr_lower_bound(X) __builtin_get_pointer_lower_bound(X)
146*5e3eaea3SApple OSS Distributions 
147*5e3eaea3SApple OSS Distributions /* Create a wide pointer with the same lower bound and upper bounds as X, but
148*5e3eaea3SApple OSS Distributions    with a pointer component also equal to the upper bound. */
149*5e3eaea3SApple OSS Distributions #define __ptr_upper_bound(X) __builtin_get_pointer_upper_bound(X)
150*5e3eaea3SApple OSS Distributions 
151*5e3eaea3SApple OSS Distributions /* Convert a __terminated_by(T) pointer to an __indexable pointer. These
152*5e3eaea3SApple OSS Distributions    operations will calculate the upper bound by iterating over the memory
153*5e3eaea3SApple OSS Distributions    pointed to by P in order to find the terminator.
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions    The __terminated_by_to_indexable(P) does NOT include the terminator within
156*5e3eaea3SApple OSS Distributions    bounds of the __indexable pointer. Consequently, the terminator cannot be
157*5e3eaea3SApple OSS Distributions    erased (or even accessed) through the __indexable pointer. The address one
158*5e3eaea3SApple OSS Distributions    past the end of the array (pointing to the terminator) can be found with
159*5e3eaea3SApple OSS Distributions    __ptr_upper_bound().
160*5e3eaea3SApple OSS Distributions 
161*5e3eaea3SApple OSS Distributions    The __unsafe_terminated_by_to_indexable(P) does include the terminator within
162*5e3eaea3SApple OSS Distributions    the bounds of the __indexable pointer. This makes the operation unsafe, since
163*5e3eaea3SApple OSS Distributions    the terminator can be erased, and thus using P might result in out-of-bounds
164*5e3eaea3SApple OSS Distributions    access. */
165*5e3eaea3SApple OSS Distributions #define __terminated_by_to_indexable(P) \
166*5e3eaea3SApple OSS Distributions   __builtin_terminated_by_to_indexable(P)
167*5e3eaea3SApple OSS Distributions #define __unsafe_terminated_by_to_indexable(P) \
168*5e3eaea3SApple OSS Distributions   __builtin_unsafe_terminated_by_to_indexable(P)
169*5e3eaea3SApple OSS Distributions 
170*5e3eaea3SApple OSS Distributions #define __null_terminated_to_indexable(P)            \
171*5e3eaea3SApple OSS Distributions   ({                                                 \
172*5e3eaea3SApple OSS Distributions     __typeof__(*(P)) *__null_terminated __ptr = (P); \
173*5e3eaea3SApple OSS Distributions     __terminated_by_to_indexable(__ptr);             \
174*5e3eaea3SApple OSS Distributions   })
175*5e3eaea3SApple OSS Distributions 
176*5e3eaea3SApple OSS Distributions #define __unsafe_null_terminated_to_indexable(P)     \
177*5e3eaea3SApple OSS Distributions   ({                                                 \
178*5e3eaea3SApple OSS Distributions     __typeof__(*(P)) *__null_terminated __ptr = (P); \
179*5e3eaea3SApple OSS Distributions     __unsafe_terminated_by_to_indexable(__ptr);      \
180*5e3eaea3SApple OSS Distributions   })
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions /* __unsafe_terminated_by_from_indexable(T, PTR [, PTR_TO_TERM]) converts an
183*5e3eaea3SApple OSS Distributions    __indexable pointer to a __terminated_by(T) pointer. The operation will
184*5e3eaea3SApple OSS Distributions    check if the given terminator T occurs in the memory pointed to by PTR.
185*5e3eaea3SApple OSS Distributions    If so, the operation evaluates to __terminated_by(T) pointer. Otherwise, it
186*5e3eaea3SApple OSS Distributions    traps.
187*5e3eaea3SApple OSS Distributions 
188*5e3eaea3SApple OSS Distributions    The operation has an optional parameter PTR_TO_TERM, which changes the way
189*5e3eaea3SApple OSS Distributions    how the check for the terminator existence is generated. PTR_TO_TERM must
190*5e3eaea3SApple OSS Distributions    point to the terminator element and be within the bounds of PTR.
191*5e3eaea3SApple OSS Distributions    If PTR_TO_TERM is provided, the runtime will check if it is in fact within
192*5e3eaea3SApple OSS Distributions    the bounds and points to an element that equals to T. If PTR_TO_TERM is not
193*5e3eaea3SApple OSS Distributions    provided, the runtime will iterate over the memory pointed to by PTR to find
194*5e3eaea3SApple OSS Distributions    the terminator.
195*5e3eaea3SApple OSS Distributions 
196*5e3eaea3SApple OSS Distributions    The operation is unsafe, since the terminator can be erased through PTR after
197*5e3eaea3SApple OSS Distributions    the conversion. This can result in out-of-bounds access through the newly
198*5e3eaea3SApple OSS Distributions    created __terminated_by(T) pointer.
199*5e3eaea3SApple OSS Distributions 
200*5e3eaea3SApple OSS Distributions    For convenience, the
201*5e3eaea3SApple OSS Distributions    __unsafe_null_terminated_from_indexable(PTR [, PTR_TO_TERM]) macro is
202*5e3eaea3SApple OSS Distributions    provided, which assumes that the terminator is 0. */
203*5e3eaea3SApple OSS Distributions #define __unsafe_terminated_by_from_indexable(T, ...) \
204*5e3eaea3SApple OSS Distributions   __builtin_unsafe_terminated_by_from_indexable((T), __VA_ARGS__)
205*5e3eaea3SApple OSS Distributions #define __unsafe_null_terminated_from_indexable(...) \
206*5e3eaea3SApple OSS Distributions   __builtin_unsafe_terminated_by_from_indexable(0, __VA_ARGS__)
207*5e3eaea3SApple OSS Distributions 
208*5e3eaea3SApple OSS Distributions /* Instruct the compiler to disregard the bounds of an array used in a function
209*5e3eaea3SApple OSS Distributions    prototype and allow the decayed pointer to use __counted_by. This is a niche
210*5e3eaea3SApple OSS Distributions    capability that is only useful in limited patterns (the way that `mig` uses
211*5e3eaea3SApple OSS Distributions    arrays being one of them). */
212*5e3eaea3SApple OSS Distributions #define __array_decay_dicards_count_in_parameters \
213*5e3eaea3SApple OSS Distributions   __attribute__((__decay_discards_count_in_parameters__))
214*5e3eaea3SApple OSS Distributions 
215*5e3eaea3SApple OSS Distributions /* An attribute to indicate a variable to be effectively constant (or data const)
216*5e3eaea3SApple OSS Distributions    that it is allocated in a const section so cannot be modified after an early
217*5e3eaea3SApple OSS Distributions    stage of bootup, for example. Adding this attribute allows a global variable
218*5e3eaea3SApple OSS Distributions    to be used in __counted_by attribute of struct fields, function parameter, or
219*5e3eaea3SApple OSS Distributions    local variable just like actual constants.
220*5e3eaea3SApple OSS Distributions    Note that ensuring the value never changes once it is used is the user's
221*5e3eaea3SApple OSS Distributions    responsibility. One way to achieve this is the xnu model, in which certain
222*5e3eaea3SApple OSS Distributions    variables are placed in a segment that is remapped as read-only after
223*5e3eaea3SApple OSS Distributions    initialization. */
224*5e3eaea3SApple OSS Distributions #define __unsafe_late_const __attribute__((__unsafe_late_const__))
225*5e3eaea3SApple OSS Distributions 
226*5e3eaea3SApple OSS Distributions #else
227*5e3eaea3SApple OSS Distributions 
228*5e3eaea3SApple OSS Distributions /* We intentionally define to nothing pointer attributes which do not have an
229*5e3eaea3SApple OSS Distributions    impact on the ABI. __indexable and __bidi_indexable are not defined because
230*5e3eaea3SApple OSS Distributions    of the ABI incompatibility that makes the diagnostic preferable. */
231*5e3eaea3SApple OSS Distributions #define __single
232*5e3eaea3SApple OSS Distributions #define __unsafe_indexable
233*5e3eaea3SApple OSS Distributions #define __counted_by(N)
234*5e3eaea3SApple OSS Distributions #define __sized_by(N)
235*5e3eaea3SApple OSS Distributions #define __ended_by(E)
236*5e3eaea3SApple OSS Distributions 
237*5e3eaea3SApple OSS Distributions /* We intentionally define the terminated_by attributes to nothing. */
238*5e3eaea3SApple OSS Distributions #define __terminated_by(T)
239*5e3eaea3SApple OSS Distributions #define __null_terminated
240*5e3eaea3SApple OSS Distributions 
241*5e3eaea3SApple OSS Distributions /* Similarly, we intentionally define to nothing the
242*5e3eaea3SApple OSS Distributions    __ptrcheck_abi_assume_single and __ptrcheck_abi_assume_unsafe_indexable
243*5e3eaea3SApple OSS Distributions    macros because they do not lead to an ABI incompatibility. However, we do not
244*5e3eaea3SApple OSS Distributions    define the indexable and unsafe_indexable ones because the diagnostic is
245*5e3eaea3SApple OSS Distributions    better than the silent ABI break. */
246*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_single()
247*5e3eaea3SApple OSS Distributions #define __ptrcheck_abi_assume_unsafe_indexable()
248*5e3eaea3SApple OSS Distributions 
249*5e3eaea3SApple OSS Distributions /* __unsafe_forge intrinsics are defined as regular C casts. */
250*5e3eaea3SApple OSS Distributions #define __unsafe_forge_bidi_indexable(T, P, S) ((T)(P))
251*5e3eaea3SApple OSS Distributions #define __unsafe_forge_single(T, P) ((T)(P))
252*5e3eaea3SApple OSS Distributions 
253*5e3eaea3SApple OSS Distributions /* The conversion between terminated_by pointers just evaluates to the pointer
254*5e3eaea3SApple OSS Distributions    argument. */
255*5e3eaea3SApple OSS Distributions #define __terminated_by_to_indexable(P) (P)
256*5e3eaea3SApple OSS Distributions #define __unsafe_terminated_by_to_indexable(P) (P)
257*5e3eaea3SApple OSS Distributions #define __null_terminated_to_indexable(P) (P)
258*5e3eaea3SApple OSS Distributions #define __unsafe_null_terminated_to_indexable(P) (P)
259*5e3eaea3SApple OSS Distributions #define __unsafe_terminated_by_from_indexable(T, P, ...) (P)
260*5e3eaea3SApple OSS Distributions #define __unsafe_null_terminated_from_indexable(P, ...) (P)
261*5e3eaea3SApple OSS Distributions 
262*5e3eaea3SApple OSS Distributions /* decay operates normally; attribute is meaningless without pointer checks. */
263*5e3eaea3SApple OSS Distributions #define __array_decay_dicards_count_in_parameters
264*5e3eaea3SApple OSS Distributions 
265*5e3eaea3SApple OSS Distributions #endif /* __has_ptrcheck */
266*5e3eaea3SApple OSS Distributions 
267*5e3eaea3SApple OSS Distributions #endif /* __PTRCHECK_H */
268