xref: /xnu-11417.101.15/libkern/os/alloc_util.h (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * Copyright (c) 2021 Apple Inc. All rights reserved.
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * @APPLE_LICENSE_HEADER_START@
5*e3723e1fSApple OSS Distributions  *
6*e3723e1fSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*e3723e1fSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*e3723e1fSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*e3723e1fSApple OSS Distributions  * compliance with the License. Please obtain a copy of the License at
10*e3723e1fSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this
11*e3723e1fSApple OSS Distributions  * file.
12*e3723e1fSApple OSS Distributions  *
13*e3723e1fSApple OSS Distributions  * The Original Code and all software distributed under the License are
14*e3723e1fSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*e3723e1fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*e3723e1fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*e3723e1fSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*e3723e1fSApple OSS Distributions  * Please see the License for the specific language governing rights and
19*e3723e1fSApple OSS Distributions  * limitations under the License.
20*e3723e1fSApple OSS Distributions  *
21*e3723e1fSApple OSS Distributions  * @APPLE_LICENSE_HEADER_END@
22*e3723e1fSApple OSS Distributions  */
23*e3723e1fSApple OSS Distributions 
24*e3723e1fSApple OSS Distributions #ifndef _OS_ALLOC_UTIL_H
25*e3723e1fSApple OSS Distributions #define _OS_ALLOC_UTIL_H
26*e3723e1fSApple OSS Distributions 
27*e3723e1fSApple OSS Distributions #include <sys/cdefs.h>
28*e3723e1fSApple OSS Distributions #if defined(__cplusplus) && __cplusplus >= 201103L
29*e3723e1fSApple OSS Distributions extern "C++" {
30*e3723e1fSApple OSS Distributions #include <os/cpp_util.h>
31*e3723e1fSApple OSS Distributions }
32*e3723e1fSApple OSS Distributions #endif
33*e3723e1fSApple OSS Distributions 
34*e3723e1fSApple OSS Distributions /*!
35*e3723e1fSApple OSS Distributions  * @macro os_is_ptr_like
36*e3723e1fSApple OSS Distributions  *
37*e3723e1fSApple OSS Distributions  * @abstract
38*e3723e1fSApple OSS Distributions  * Tell whether the given expression resembles a pointer.
39*e3723e1fSApple OSS Distributions  *
40*e3723e1fSApple OSS Distributions  * @discussion
41*e3723e1fSApple OSS Distributions  * When pointer bounds are enabled, only types that are actually classified
42*e3723e1fSApple OSS Distributions  * as pointers will be considered pointer-like. Otherwise, any pointer-sized
43*e3723e1fSApple OSS Distributions  * type will be considered pointer-like.
44*e3723e1fSApple OSS Distributions  *
45*e3723e1fSApple OSS Distributions  * @param P             the expression to be checked
46*e3723e1fSApple OSS Distributions  */
47*e3723e1fSApple OSS Distributions #if __has_ptrcheck
48*e3723e1fSApple OSS Distributions #define os_is_ptr_like(P) (__builtin_classify_type(P) == 5)
49*e3723e1fSApple OSS Distributions #else  /* __has_ptrcheck */
50*e3723e1fSApple OSS Distributions #define os_is_ptr_like(P) (sizeof(P) == sizeof(void *))
51*e3723e1fSApple OSS Distributions #endif /* __has_ptrcheck */
52*e3723e1fSApple OSS Distributions 
53*e3723e1fSApple OSS Distributions /*!
54*e3723e1fSApple OSS Distributions  * @macro os_ptr_load_and_erase
55*e3723e1fSApple OSS Distributions  *
56*e3723e1fSApple OSS Distributions  * @abstract
57*e3723e1fSApple OSS Distributions  * Load the value of @c elem into a temporary, set @c elem to NULL, and
58*e3723e1fSApple OSS Distributions  * return the value.
59*e3723e1fSApple OSS Distributions  *
60*e3723e1fSApple OSS Distributions  * @param elem          the pointer whose value will be taken, and which will
61*e3723e1fSApple OSS Distributions  *                      be set to NULL.
62*e3723e1fSApple OSS Distributions  */
63*e3723e1fSApple OSS Distributions #define os_ptr_load_and_erase(elem) ({                        \
64*e3723e1fSApple OSS Distributions 	_Static_assert(os_is_ptr_like(elem),                      \
65*e3723e1fSApple OSS Distributions 	    "elem isn't pointer sized");                          \
66*e3723e1fSApple OSS Distributions 	__auto_type *__single __eptr = &(elem);                   \
67*e3723e1fSApple OSS Distributions 	__auto_type __elem = *__eptr;                             \
68*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic push")                          \
69*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic ignored \"-Wold-style-cast\"")  \
70*e3723e1fSApple OSS Distributions 	*__eptr = (__typeof__(__elem))NULL;                       \
71*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic pop")                           \
72*e3723e1fSApple OSS Distributions 	__elem;                                                   \
73*e3723e1fSApple OSS Distributions })
74*e3723e1fSApple OSS Distributions 
75*e3723e1fSApple OSS Distributions /*!
76*e3723e1fSApple OSS Distributions  * @macro os_get_pointee_type
77*e3723e1fSApple OSS Distributions  *
78*e3723e1fSApple OSS Distributions  * @abstract
79*e3723e1fSApple OSS Distributions  * Get the type pointed to by @c ptr.
80*e3723e1fSApple OSS Distributions  *
81*e3723e1fSApple OSS Distributions  * @discussion
82*e3723e1fSApple OSS Distributions  * C++ forbids dereferencing a void pointer.
83*e3723e1fSApple OSS Distributions  * This macro provides an abstraction that is safe to use with any pointer,
84*e3723e1fSApple OSS Distributions  * both from C, and from C++.
85*e3723e1fSApple OSS Distributions  *
86*e3723e1fSApple OSS Distributions  * @param ptr           the pointer we want to get the pointee's type for
87*e3723e1fSApple OSS Distributions  */
88*e3723e1fSApple OSS Distributions 
89*e3723e1fSApple OSS Distributions #if defined(__cplusplus) && __has_builtin(__remove_pointer) && \
90*e3723e1fSApple OSS Distributions         __has_builtin(__remove_reference_t)
91*e3723e1fSApple OSS Distributions /*
92*e3723e1fSApple OSS Distributions  * The reason we're using the compiler builtins is that those are able to
93*e3723e1fSApple OSS Distributions  * properly deal with __ptrauth-qualified pointers, unlike template
94*e3723e1fSApple OSS Distributions  * specializations. Moreover, template specializations would require forwarding
95*e3723e1fSApple OSS Distributions  * type definitions, and currently __attribute__((xnu_usage_semantics(...)))
96*e3723e1fSApple OSS Distributions  * annotations are not carried over across typedefs.
97*e3723e1fSApple OSS Distributions  *
98*e3723e1fSApple OSS Distributions  * It is safe to assume that if the compiler does not support such builtins,
99*e3723e1fSApple OSS Distributions  * it also does not implement D150875, and we can therefore safely dereference
100*e3723e1fSApple OSS Distributions  * void pointers.
101*e3723e1fSApple OSS Distributions  */
102*e3723e1fSApple OSS Distributions #define os_get_pointee_type(ptr)                               \
103*e3723e1fSApple OSS Distributions 	__remove_pointer(__remove_reference_t(__typeof__(ptr)))
104*e3723e1fSApple OSS Distributions #else
105*e3723e1fSApple OSS Distributions #define os_get_pointee_type(ptr)                                      \
106*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic push")                               \
107*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic ignored \"-Wvoid-ptr-dereference\"") \
108*e3723e1fSApple OSS Distributions 	__typeof__(*(ptr))                                             \
109*e3723e1fSApple OSS Distributions 	_Pragma("clang diagnostic pop")
110*e3723e1fSApple OSS Distributions #endif
111*e3723e1fSApple OSS Distributions 
112*e3723e1fSApple OSS Distributions #endif /* _OS_ALLOC_UTIL_H */
113