xref: /xnu-10063.121.3/doc/allocators/api-basics.md (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions# XNU Allocators best practices
2*2c2f96dcSApple OSS Distributions
3*2c2f96dcSApple OSS DistributionsThe right way to allocate memory in the kernel.
4*2c2f96dcSApple OSS Distributions
5*2c2f96dcSApple OSS Distributions## Introduction
6*2c2f96dcSApple OSS Distributions
7*2c2f96dcSApple OSS DistributionsXNU proposes two ways to allocate memory:
8*2c2f96dcSApple OSS Distributions
9*2c2f96dcSApple OSS Distributions- the VM subsystem that provides allocations at the granularity of pages (with
10*2c2f96dcSApple OSS Distributions  `kmem_alloc` and similar interfaces);
11*2c2f96dcSApple OSS Distributions- the zone allocator subsystem (`<kern/zalloc.h>`) which is a slab-allocator of
12*2c2f96dcSApple OSS Distributions  objects of fixed size.
13*2c2f96dcSApple OSS Distributions
14*2c2f96dcSApple OSS DistributionsIn addition to that, `<kern/kalloc.h>` provides a variable-size general purpose
15*2c2f96dcSApple OSS Distributionsallocator implemented as a collection of zones of fixed size, and overflowing to
16*2c2f96dcSApple OSS Distributions`kmem_alloc` for allocations larger than a few pages (32KB when this
17*2c2f96dcSApple OSS Distributionsdocument was being written but this is subject to change/tuning in the future).
18*2c2f96dcSApple OSS Distributions
19*2c2f96dcSApple OSS Distributions
20*2c2f96dcSApple OSS DistributionsThe Core Kernel allocators rely on the following headers:
21*2c2f96dcSApple OSS Distributions
22*2c2f96dcSApple OSS Distributions- `<kern/zalloc.h>` and `<kern/kalloc.h>` for its API surface, which most
23*2c2f96dcSApple OSS Distributions  clients should find sufficient,
24*2c2f96dcSApple OSS Distributions- `<kern/zalloc_internal.h>` for interfaces that need to be exported
25*2c2f96dcSApple OSS Distributions  for introspection and implementation purposes, and is not meant
26*2c2f96dcSApple OSS Distributions  for general consumption.
27*2c2f96dcSApple OSS Distributions
28*2c2f96dcSApple OSS DistributionsThis document will present the best practices to allocate memory
29*2c2f96dcSApple OSS Distributionsin the kernel, from a security perspective.
30*2c2f96dcSApple OSS Distributions
31*2c2f96dcSApple OSS Distributions## Permanent allocations
32*2c2f96dcSApple OSS Distributions
33*2c2f96dcSApple OSS DistributionsThe kernel sometimes needs to provide persistent allocations that depend on
34*2c2f96dcSApple OSS Distributionsparameters that aren't compile time constants, but will not vary over time (NCPU
35*2c2f96dcSApple OSS Distributionsis an obvious example here).
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS DistributionsThe zone subsystem provides a `zalloc_permanent*` family of functions that help
38*2c2f96dcSApple OSS Distributionsallocating memory in such a fashion in a very compact way.
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS DistributionsUnlike the typical zone allocators, this allows for arbitrary sizes, in a
41*2c2f96dcSApple OSS Distributionssimilar fashion to `kalloc`. These functions will never fail (if the allocation
42*2c2f96dcSApple OSS Distributionsfails, the kernel will panic), and always return zeroed memory. Trying to free
43*2c2f96dcSApple OSS Distributionsthese allocations results in a kernel panic.
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions## Allocation flags
46*2c2f96dcSApple OSS Distributions
47*2c2f96dcSApple OSS DistributionsMost `zalloc` or `kalloc` functions take `zalloc_flags_t` typed flags.
48*2c2f96dcSApple OSS DistributionsWhen flags are expected, exactly one of `Z_WAITOK`, `Z_NOWAIT` or `Z_NOPAGEWAIT`
49*2c2f96dcSApple OSS Distributionsis to be passed:
50*2c2f96dcSApple OSS Distributions
51*2c2f96dcSApple OSS Distributions- `Z_WAITOK` means that the zone allocator can wait and block,
52*2c2f96dcSApple OSS Distributions- `Z_NOWAIT` can be used to require a fully non blocking behavior, which can be
53*2c2f96dcSApple OSS Distributions  used for allocations under spinlock and other preemption disabled contexts;
54*2c2f96dcSApple OSS Distributions- `Z_NOPAGEWAIT` allows for the allocator to block (typically on mutexes),
55*2c2f96dcSApple OSS Distributions  but not to wait for available pages if there are none, this is only useful
56*2c2f96dcSApple OSS Distributions  for the buffer cache, and most client should either use `Z_NOWAIT` or `Z_WAITOK`.
57*2c2f96dcSApple OSS Distributions
58*2c2f96dcSApple OSS DistributionsOther important flags:
59*2c2f96dcSApple OSS Distributions
60*2c2f96dcSApple OSS Distributions- `Z_ZERO` if zeroed memory is expected (nowadays most of the allocations will
61*2c2f96dcSApple OSS Distributions  be zeroed regardless, but it's always clearer to specify it), note that it is
62*2c2f96dcSApple OSS Distributions  often more efficient than calling bzero as the allocator tends to maintain
63*2c2f96dcSApple OSS Distributions  freed memory as zeroed in the first place,
64*2c2f96dcSApple OSS Distributions- `Z_NOFAIL` if the caller knows the allocation can't fail: allocations that are
65*2c2f96dcSApple OSS Distributions   made with `Z_WAITOK` from regular (non exhaustible) zones, or from `kalloc*`
66*2c2f96dcSApple OSS Distributions   interfaces with a size smaller than `KALLOC_SAFE_ALLOC_SIZE`,
67*2c2f96dcSApple OSS Distributions  will never fail (the kernel will instead panic if no memory can be found).
68*2c2f96dcSApple OSS Distributions  `Z_NOFAIL` can be used to denote that the caller knows about this.
69*2c2f96dcSApple OSS Distributions  If `Z_NOFAIL` is incorrectly used, then the zone allocator will panic at runtime.
70*2c2f96dcSApple OSS Distributions
71*2c2f96dcSApple OSS Distributions## Zones (`zalloc`)
72*2c2f96dcSApple OSS Distributions
73*2c2f96dcSApple OSS DistributionsThe first blessed way to allocate memory in the kernel is by using zones.
74*2c2f96dcSApple OSS DistributionsZones are mostly meant to be used in Core XNU and some "BSD" kexts.
75*2c2f96dcSApple OSS Distributions
76*2c2f96dcSApple OSS DistributionsIt is generally recommended to create zones early and to store the `zone_t`
77*2c2f96dcSApple OSS Distributionspointer in read-only memory (using `SECURITY_READ_ONLY_LATE` storage).
78*2c2f96dcSApple OSS Distributions
79*2c2f96dcSApple OSS DistributionsZones are more feature-rich than `kalloc`, and some features can only be
80*2c2f96dcSApple OSS Distributionsused when making a zone:
81*2c2f96dcSApple OSS Distributions
82*2c2f96dcSApple OSS Distributions- the object type being allocated requires extremely strong segregation
83*2c2f96dcSApple OSS Distributions  from other types (typically `zone_require` will be used with this zone),
84*2c2f96dcSApple OSS Distributions- the object type implements some form of security boundary and wants to adopt
85*2c2f96dcSApple OSS Distributions  the read-only allocator (See `ZC_READONLY`),
86*2c2f96dcSApple OSS Distributions- the allocation must be per-cpu,
87*2c2f96dcSApple OSS Distributions- ...
88*2c2f96dcSApple OSS Distributions
89*2c2f96dcSApple OSS DistributionsIn the vast majority of cases however, using `kalloc_type` (or `IOMallocType`)
90*2c2f96dcSApple OSS Distributionsis preferred.
91*2c2f96dcSApple OSS Distributions
92*2c2f96dcSApple OSS Distributions
93*2c2f96dcSApple OSS Distributions## The Typed allocator
94*2c2f96dcSApple OSS Distributions
95*2c2f96dcSApple OSS DistributionsIgnoring VM allocations (or wrappers like `IOMemoryDescriptor`), the only
96*2c2f96dcSApple OSS Distributionsblessed way to allocate typed memory in XNU is using the typed allocator
97*2c2f96dcSApple OSS Distributions`kalloc_type` or one of its variants (like IOKit's `IOMallocType`) and untyped
98*2c2f96dcSApple OSS Distributionsmemory that doesn't contain pointers is using the data API `kalloc_data` or
99*2c2f96dcSApple OSS Distributionsone of its variants (like IOKit's `IOMallocData`). However, this comes with
100*2c2f96dcSApple OSS Distributionsadditional requirements.
101*2c2f96dcSApple OSS Distributions
102*2c2f96dcSApple OSS DistributionsNote that at this time, those interfaces aren't exported to third parties,
103*2c2f96dcSApple OSS Distributionsas its ABI has not yet converged.
104*2c2f96dcSApple OSS Distributions
105*2c2f96dcSApple OSS Distributions### A word about types
106*2c2f96dcSApple OSS Distributions
107*2c2f96dcSApple OSS DistributionsThe typed allocators assume that allocated types fit a very precise model.
108*2c2f96dcSApple OSS DistributionsIf the allocations you perform do not fit the model, then your types
109*2c2f96dcSApple OSS Distributionsmust be restructured to fit, for security reasons.
110*2c2f96dcSApple OSS Distributions
111*2c2f96dcSApple OSS DistributionsA general theme will be the separation of data/primitive types from pointers,
112*2c2f96dcSApple OSS Distributionsas attackers tend to use data/pointer overlaps to carry out their exploitations.
113*2c2f96dcSApple OSS Distributions
114*2c2f96dcSApple OSS DistributionsThe typed allocators use compiler support to infer signatures
115*2c2f96dcSApple OSS Distributionsof the types being allocated. Because some scalars actually represent
116*2c2f96dcSApple OSS Distributionskernel pointers (like `vm_offset_t`,`vm_address_t`, `uintptr_t`, ...),
117*2c2f96dcSApple OSS Distributionstypes or structure members can be decorated with `__kernel_ptr_semantics`
118*2c2f96dcSApple OSS Distributionsto denote when a data-looking type is actually a pointer.
119*2c2f96dcSApple OSS Distributions
120*2c2f96dcSApple OSS DistributionsDo note that `__kernel_data_semantics` and `__kernel_dual_semantics`
121*2c2f96dcSApple OSS Distributionsare also provided but should typically rarely be used.
122*2c2f96dcSApple OSS Distributions
123*2c2f96dcSApple OSS Distributions#### fixed-sized types
124*2c2f96dcSApple OSS Distributions
125*2c2f96dcSApple OSS DistributionsThe first case is fixed size types, this is typically a `struct`, `union`
126*2c2f96dcSApple OSS Distributionsor C++ `class`. Fixed-size types must follow certain rules:
127*2c2f96dcSApple OSS Distributions
128*2c2f96dcSApple OSS Distributions- types should be small enough to fit in the zone allocator:
129*2c2f96dcSApple OSS Distributions  smaller than `KALLOC_SAFE_ALLOC_SIZE`. When this is not the case,
130*2c2f96dcSApple OSS Distributions  we have typically found that there is a large array of data,
131*2c2f96dcSApple OSS Distributions  or some buffer in that type, the solution is to outline this allocation.
132*2c2f96dcSApple OSS Distributions  kernel extensions must define `KALLOC_TYPE_STRICT_SIZE_CHECK` to turn
133*2c2f96dcSApple OSS Distributions  misuse of `kalloc_type()` relative to size at compile time, it's default in XNU.
134*2c2f96dcSApple OSS Distributions- for union types, data/pointer overlaps should be avoided if possible.
135*2c2f96dcSApple OSS Distributions  when this isn't possible, a zone should be considered.
136*2c2f96dcSApple OSS Distributions
137*2c2f96dcSApple OSS Distributions#### Variable-sized types
138*2c2f96dcSApple OSS Distributions
139*2c2f96dcSApple OSS DistributionsThese come in two variants: arrays, and arrays prefixed with a header.
140*2c2f96dcSApple OSS DistributionsAny other case must be reduced to those, by possibly making more allocations.
141*2c2f96dcSApple OSS Distributions
142*2c2f96dcSApple OSS DistributionsAn array is simply an allocation of several fixed-size types,
143*2c2f96dcSApple OSS Distributionsand the rules of "fixed-sized types" above apply to them.
144*2c2f96dcSApple OSS Distributions
145*2c2f96dcSApple OSS DistributionsThe following rules are expected when dealing with variable sized allocations:
146*2c2f96dcSApple OSS Distributions
147*2c2f96dcSApple OSS Distributions- variable sized allocations should have a single owner and not be refcounted;
148*2c2f96dcSApple OSS Distributions- under the header-prefixed form, if the header contains pointers,
149*2c2f96dcSApple OSS Distributions  then the array element type **must not** be only data.
150*2c2f96dcSApple OSS Distributions
151*2c2f96dcSApple OSS DistributionsIf those rules can't be followed, then the allocation must be split with
152*2c2f96dcSApple OSS Distributionsthe header becoming a fixed-sized type becoming the single owner
153*2c2f96dcSApple OSS Distributionsof an array.
154*2c2f96dcSApple OSS Distributions
155*2c2f96dcSApple OSS Distributions#### Untyped memory
156*2c2f96dcSApple OSS Distributions
157*2c2f96dcSApple OSS DistributionsWhen allocating untyped memory with the data APIs ensure that it doesn't
158*2c2f96dcSApple OSS Distributionscontain kernel pointers. If your untyped allocation contains kernel pointers
159*2c2f96dcSApple OSS Distributionsconsider splitting the allocation into two: one part that is typed and contains
160*2c2f96dcSApple OSS Distributionsthe kernel pointers and the second that is untyped and data-only.
161*2c2f96dcSApple OSS Distributions
162*2c2f96dcSApple OSS Distributions### API surface
163*2c2f96dcSApple OSS Distributions
164*2c2f96dcSApple OSS Distributions<table>
165*2c2f96dcSApple OSS Distributions  <tr>
166*2c2f96dcSApple OSS Distributions    <th>Interface</th>
167*2c2f96dcSApple OSS Distributions    <th>API</th>
168*2c2f96dcSApple OSS Distributions    <th>Notes</th>
169*2c2f96dcSApple OSS Distributions  </tr>
170*2c2f96dcSApple OSS Distributions  <tr>
171*2c2f96dcSApple OSS Distributions    <td>Data/Primitive types</td>
172*2c2f96dcSApple OSS Distributions    <td>
173*2c2f96dcSApple OSS Distributions      <p>
174*2c2f96dcSApple OSS Distributions      <b>Core Kernel</b>:<br/>
175*2c2f96dcSApple OSS Distributions      <tt>kalloc_data(size, flags)</tt><br/>
176*2c2f96dcSApple OSS Distributions      <tt>krealloc_data(ptr, old_size, new_size, flags)</tt><br/>
177*2c2f96dcSApple OSS Distributions      <tt>kfree_data(ptr, size)</tt><br/>
178*2c2f96dcSApple OSS Distributions      <tt>kfree_data_counted_by(ptr_var, count_var)</tt><br/>
179*2c2f96dcSApple OSS Distributions      <tt>kfree_data_sized_by(ptr_var, byte_count_var)</tt><br/>
180*2c2f96dcSApple OSS Distributions      <tt>kfree_data_addr(ptr)</tt>
181*2c2f96dcSApple OSS Distributions      </p>
182*2c2f96dcSApple OSS Distributions      <p>
183*2c2f96dcSApple OSS Distributions      <b>IOKit untyped variant (returns <tt>void *</tt>)</b>:<br/>
184*2c2f96dcSApple OSS Distributions      <tt>IOMallocData(size)</tt><br/>
185*2c2f96dcSApple OSS Distributions      <tt>IOMallocZeroData(size)</tt><br/>
186*2c2f96dcSApple OSS Distributions      <tt>IOFreeData(ptr, size)</tt>
187*2c2f96dcSApple OSS Distributions      </p>
188*2c2f96dcSApple OSS Distributions      <p>
189*2c2f96dcSApple OSS Distributions      <b>IOKit typed variant (returns <tt>type_t *</tt>)</b>:<br/>
190*2c2f96dcSApple OSS Distributions      <tt>IONewData(type_t, count)</tt><br/>
191*2c2f96dcSApple OSS Distributions      <tt>IONewZeroData(type_t, count)</tt><br/>
192*2c2f96dcSApple OSS Distributions      <tt>IODeleteData(ptr, type_t, count)</tt>
193*2c2f96dcSApple OSS Distributions      </p>
194*2c2f96dcSApple OSS Distributions    </td>
195*2c2f96dcSApple OSS Distributions    <td>This should be used when the allocated type contains no kernel pointer only</td>
196*2c2f96dcSApple OSS Distributions  </tr>
197*2c2f96dcSApple OSS Distributions  <tr>
198*2c2f96dcSApple OSS Distributions    <td>Fixed-sized type</td>
199*2c2f96dcSApple OSS Distributions    <td>
200*2c2f96dcSApple OSS Distributions      <p>
201*2c2f96dcSApple OSS Distributions      <b>Core Kernel</b>:<br/>
202*2c2f96dcSApple OSS Distributions      <tt>kalloc_type(type_t, flags)</tt><br/>
203*2c2f96dcSApple OSS Distributions      <tt>kfree_type(type_t, ptr)</tt>
204*2c2f96dcSApple OSS Distributions      </p>
205*2c2f96dcSApple OSS Distributions      <p>
206*2c2f96dcSApple OSS Distributions      <b>IOKit:</b><br/>
207*2c2f96dcSApple OSS Distributions      <tt>IOMallocType(type_t)</tt><br/>
208*2c2f96dcSApple OSS Distributions      <tt>IOFreeType(ptr, type_t)</tt>
209*2c2f96dcSApple OSS Distributions      </p>
210*2c2f96dcSApple OSS Distributions    </td>
211*2c2f96dcSApple OSS Distributions    <td>
212*2c2f96dcSApple OSS Distributions      <p>
213*2c2f96dcSApple OSS Distributions      Note that this is absolutely OK to use this variant
214*2c2f96dcSApple OSS Distributions      for data/primitive types, it will be redirected to <tt>kalloc_data</tt>
215*2c2f96dcSApple OSS Distributions      (or <tt>IOMallocData</tt>).
216*2c2f96dcSApple OSS Distributions      </p>
217*2c2f96dcSApple OSS Distributions    </td>
218*2c2f96dcSApple OSS Distributions  </tr>
219*2c2f96dcSApple OSS Distributions  <tr>
220*2c2f96dcSApple OSS Distributions    <td>Arrays of fixed-sized type</td>
221*2c2f96dcSApple OSS Distributions    <td>
222*2c2f96dcSApple OSS Distributions      <p>
223*2c2f96dcSApple OSS Distributions      <b>Core Kernel</b>:<br/>
224*2c2f96dcSApple OSS Distributions      <tt>kalloc_type(type_t, count, flags)</tt><br/>
225*2c2f96dcSApple OSS Distributions      <tt>kfree_type(type_t, count, ptr)</tt>
226*2c2f96dcSApple OSS Distributions      </p>
227*2c2f96dcSApple OSS Distributions      <p>
228*2c2f96dcSApple OSS Distributions      <b>IOKit:</b><br/>
229*2c2f96dcSApple OSS Distributions      <tt>IONew(type_t, count)</tt><br/>
230*2c2f96dcSApple OSS Distributions      <tt>IONewZero(type_t, count)</tt><br/>
231*2c2f96dcSApple OSS Distributions      <tt>IODelete(ptr, type_t, count)</tt>
232*2c2f96dcSApple OSS Distributions      </p>
233*2c2f96dcSApple OSS Distributions    </td>
234*2c2f96dcSApple OSS Distributions    <td>
235*2c2f96dcSApple OSS Distributions      <p>
236*2c2f96dcSApple OSS Distributions      <tt>kalloc_type(type_t, ...)</tt> (resp. <tt>IONew(type_t, 1)</tt>)
237*2c2f96dcSApple OSS Distributions      <b>isn't</b> equivalent to <tt>kalloc_type(type_t, 1, ...)</tt>
238*2c2f96dcSApple OSS Distributions      (resp. <tt>IOMallocType(type_t)</tt>). Mix-and-matching interfaces
239*2c2f96dcSApple OSS Distributions      will result in panics.
240*2c2f96dcSApple OSS Distributions      </p>
241*2c2f96dcSApple OSS Distributions      <p>
242*2c2f96dcSApple OSS Distributions      Note that this is absolutely OK to use this variant
243*2c2f96dcSApple OSS Distributions      for data/primitive types, it will be redirected to <tt>kalloc_data</tt>.
244*2c2f96dcSApple OSS Distributions      </p>
245*2c2f96dcSApple OSS Distributions    </td>
246*2c2f96dcSApple OSS Distributions  </tr>
247*2c2f96dcSApple OSS Distributions  <tr>
248*2c2f96dcSApple OSS Distributions    <td>Header-prefixed arrays of fixed-sized type</td>
249*2c2f96dcSApple OSS Distributions    <td>
250*2c2f96dcSApple OSS Distributions      <p>
251*2c2f96dcSApple OSS Distributions      <b>Core Kernel</b>:<br/>
252*2c2f96dcSApple OSS Distributions      <tt>kalloc_type(hdr_type_t, type_t, count, flags)</tt><br/>
253*2c2f96dcSApple OSS Distributions      <tt>kfree_type(hdr_type_t, type_t, count, ptr)</tt>
254*2c2f96dcSApple OSS Distributions      </p>
255*2c2f96dcSApple OSS Distributions      <p>
256*2c2f96dcSApple OSS Distributions      <b>IOKit:</b><br/>
257*2c2f96dcSApple OSS Distributions      <tt>IONew(hdr_type_t, type_t, count)</tt><br/>
258*2c2f96dcSApple OSS Distributions      <tt>IONewZero(hdr_type_t, type_t, count)</tt><br/>
259*2c2f96dcSApple OSS Distributions      <tt>IODelete(ptr, hdr_type_t, type_t, count)</tt>
260*2c2f96dcSApple OSS Distributions      </p>
261*2c2f96dcSApple OSS Distributions    </td>
262*2c2f96dcSApple OSS Distributions    <td>
263*2c2f96dcSApple OSS Distributions      <p>
264*2c2f96dcSApple OSS Distributions      <tt>hdr_type_t</tt> can't contain a refcount,
265*2c2f96dcSApple OSS Distributions      and <tt>type_t</tt> can't be a primitive type.
266*2c2f96dcSApple OSS Distributions      </p>
267*2c2f96dcSApple OSS Distributions    </td>
268*2c2f96dcSApple OSS Distributions  </tr>
269*2c2f96dcSApple OSS Distributions</table>
270*2c2f96dcSApple OSS Distributions
271*2c2f96dcSApple OSS Distributions`kfree_data_counted_by` and `kfree_data_sized_by` are used when working with
272*2c2f96dcSApple OSS Distributions-fbounds-safety and pointers with __counted_by and __sized_by modifiers,
273*2c2f96dcSApple OSS Distributionsrespectively. They expect both their pointer and size arguments to be
274*2c2f96dcSApple OSS Distributionsmodifiable, and the pointer and size will be set to 0 together, in accordance
275*2c2f96dcSApple OSS Distributionswith -fbounds-safety semantics. Please note that arguments are evaluated
276*2c2f96dcSApple OSS Distributionsmultiple times. When -fbounds-safety is enabled, the compiler can help ensuring
277*2c2f96dcSApple OSS Distributionscorrect usage of these macros; with -fbounds-safety disabled, engineers are on
278*2c2f96dcSApple OSS Distributionstheir own to ensure proper usage.
279*2c2f96dcSApple OSS Distributions
280*2c2f96dcSApple OSS Distributions## C++ classes and operator new.
281*2c2f96dcSApple OSS Distributions
282*2c2f96dcSApple OSS DistributionsThis section covers how typed allocators should be adopted to use
283*2c2f96dcSApple OSS Distributions`operator new/delete` in C++. For C++ classes, the approach required
284*2c2f96dcSApple OSS Distributionsdiffers based on whether the class inherits from `OSObject` or not.
285*2c2f96dcSApple OSS Distributions
286*2c2f96dcSApple OSS DistributionsMost, if not all, C++ objects used in conjuction with IOKit APIs
287*2c2f96dcSApple OSS Distributionsshould probably use OSObject as a base class. C++ operators
288*2c2f96dcSApple OSS Distributionsand non-POD types should be used seldomly.
289*2c2f96dcSApple OSS Distributions
290*2c2f96dcSApple OSS Distributions### `OSObject` subclasses
291*2c2f96dcSApple OSS Distributions
292*2c2f96dcSApple OSS DistributionsAll subclasses of `OSObject` must declare and define one of IOKit's
293*2c2f96dcSApple OSS Distributions`OSDeclare*` and `OSDefine*` macros. As part of those, an `operator new` and
294*2c2f96dcSApple OSS Distributions`operator delete` are injected that force objects to enroll into `kalloc_type`.
295*2c2f96dcSApple OSS Distributions
296*2c2f96dcSApple OSS DistributionsNote that idiomatic IOKit is supposed to use `OSTypeAlloc(Class)`.
297*2c2f96dcSApple OSS Distributions
298*2c2f96dcSApple OSS Distributions### Other classes
299*2c2f96dcSApple OSS Distributions
300*2c2f96dcSApple OSS DistributionsUnlike `OSObject` subclasses, regular C++ classes must adopt typed allocators
301*2c2f96dcSApple OSS Distributionsmanually. If your struct or class is POD (Plain Old Data), then replacing usage of
302*2c2f96dcSApple OSS Distributions`new/delete` (resp. `new[]/delete[]`) with `IOMallocType/IOFreeType` (resp.
303*2c2f96dcSApple OSS Distributions`IONew/IODelete`) is safe.
304*2c2f96dcSApple OSS Distributions
305*2c2f96dcSApple OSS DistributionsHowever, if you have non default structors, or members of your class/struct
306*2c2f96dcSApple OSS Distributionshave non default structors, you will need to manually enroll it into `kalloc_type`.
307*2c2f96dcSApple OSS DistributionsThis can be accomplished through one of the following approaches, and it lets you
308*2c2f96dcSApple OSS Distributionsto continue to use C++'s new and delete keywords to allocate/deallocate instances.
309*2c2f96dcSApple OSS Distributions
310*2c2f96dcSApple OSS DistributionsThe first approach is to subclass the IOTypedOperatorsMixin struct. This will
311*2c2f96dcSApple OSS Distributionsadopt typed allocators for your class/struct by providing the appropriate
312*2c2f96dcSApple OSS Distributionsimplementations for `operator new/delete`:
313*2c2f96dcSApple OSS Distributions
314*2c2f96dcSApple OSS Distributions```cpp
315*2c2f96dcSApple OSS Distributionsstruct Type : public IOTypedOperatorsMixin<Type> {
316*2c2f96dcSApple OSS Distributions    ...
317*2c2f96dcSApple OSS Distributions};
318*2c2f96dcSApple OSS Distributions```
319*2c2f96dcSApple OSS Distributions
320*2c2f96dcSApple OSS DistributionsAlternatively, if you cannot use the mixin approach, you can use the
321*2c2f96dcSApple OSS Distributions`IOOverrideTypedOperators` macro to override `operator new/delete`
322*2c2f96dcSApple OSS Distributionswithin your class/struct declaration:
323*2c2f96dcSApple OSS Distributions
324*2c2f96dcSApple OSS Distributions```cpp
325*2c2f96dcSApple OSS Distributionsstruct Type {
326*2c2f96dcSApple OSS Distributions    IOOverrideTypedOperators(Type);
327*2c2f96dcSApple OSS Distributions    ...
328*2c2f96dcSApple OSS Distributions};
329*2c2f96dcSApple OSS Distributions```
330*2c2f96dcSApple OSS Distributions
331*2c2f96dcSApple OSS DistributionsFinally, if you need to decouple the declaration of the operators from
332*2c2f96dcSApple OSS Distributionstheir implementation, you can use `IODeclareTypedOperators` paired with
333*2c2f96dcSApple OSS Distributions`IODefineTypedOperators`, to declare the operators within your class/struct
334*2c2f96dcSApple OSS Distributionsdeclaration and then provide their definition out of line:
335*2c2f96dcSApple OSS Distributions
336*2c2f96dcSApple OSS Distributions```cpp
337*2c2f96dcSApple OSS Distributions// declaration
338*2c2f96dcSApple OSS Distributionsstruct Type {
339*2c2f96dcSApple OSS Distributions    IODeclareTypedOperators(Type);
340*2c2f96dcSApple OSS Distributions    ...
341*2c2f96dcSApple OSS Distributions};
342*2c2f96dcSApple OSS Distributions
343*2c2f96dcSApple OSS Distributions// definition
344*2c2f96dcSApple OSS DistributionsIODefineTypedOperators(Type)
345*2c2f96dcSApple OSS Distributions```
346*2c2f96dcSApple OSS Distributions
347*2c2f96dcSApple OSS DistributionsWhen a class/struct adopts typed allocators through one of those approaches,
348*2c2f96dcSApple OSS Distributionsall its subclasses must also explicitly adopt typed allocators. It is not
349*2c2f96dcSApple OSS Distributionssufficient for a common parent within the class hierarchy to enroll, in order to
350*2c2f96dcSApple OSS Distributionsautomatically provide the implementation of the operators for all of its children:
351*2c2f96dcSApple OSS Distributionseach and every subclass in the class hierarchy must also explicitly do the same.
352*2c2f96dcSApple OSS Distributions
353*2c2f96dcSApple OSS Distributions### The case of `operator new[]`
354*2c2f96dcSApple OSS Distributions
355*2c2f96dcSApple OSS DistributionsThe ABI of `operator new[]` is unfortunate, as it denormalizes
356*2c2f96dcSApple OSS Distributionsdata that we prefer to be known by the owning object
357*2c2f96dcSApple OSS Distributions(the element sizes and array element count).
358*2c2f96dcSApple OSS Distributions
359*2c2f96dcSApple OSS DistributionsIt also makes those allocations ripe for abuse in an adversarial
360*2c2f96dcSApple OSS Distributionscontext as this denormalized information is at the begining
361*2c2f96dcSApple OSS Distributionsof the structure, making it relatively easy to attack with
362*2c2f96dcSApple OSS Distributionsout-of-bounds bugs.
363*2c2f96dcSApple OSS Distributions
364*2c2f96dcSApple OSS DistributionsFor this reason, the default variants of the mixin and the macros
365*2c2f96dcSApple OSS Distributionspresented above will delete the implementation of `operator new[]`
366*2c2f96dcSApple OSS Distributionsfrom the class they are applied to.
367*2c2f96dcSApple OSS Distributions
368*2c2f96dcSApple OSS DistributionsHowever, if those must be used, you can add adopt the typed
369*2c2f96dcSApple OSS Distributionsallocators on your class by using the appropriate variant
370*2c2f96dcSApple OSS Distributionswhich explicitly implements the support for array operators:
371*2c2f96dcSApple OSS Distributions- `IOTypedOperatorsMixinSupportingArrayOperators`
372*2c2f96dcSApple OSS Distributions- `IOOverrideTypedOperatorsSupportingArrayOperators`
373*2c2f96dcSApple OSS Distributions- `IO{Declare, Define}TypedOperatorsSupportingArrayOperators`
374*2c2f96dcSApple OSS Distributions
375*2c2f96dcSApple OSS Distributions### Scalar types
376*2c2f96dcSApple OSS Distributions
377*2c2f96dcSApple OSS DistributionsThe only accepted ways of using `operator new/delete` and their variants are the ones
378*2c2f96dcSApple OSS Distributionsdescribed above. You should never use the operators on scalar types. Instead, you
379*2c2f96dcSApple OSS Distributionsshould use the appropriate typed allocator API based on the semantics of the memory
380*2c2f96dcSApple OSS Distributionsbeing allocated (i.e. `IOMallocData` for data only buffers, and `IOMallocType`/`IONew`
381*2c2f96dcSApple OSS Distributionsfor any other type).
382*2c2f96dcSApple OSS Distributions
383*2c2f96dcSApple OSS Distributions### Wrapping C++ type allocation in container OSObjects
384*2c2f96dcSApple OSS DistributionsThe blessed way of wrapping and passing a C++ type allocation for use in the
385*2c2f96dcSApple OSS Distributionslibkern collection is using `OSValueObject`. Please do no use OSData for this
386*2c2f96dcSApple OSS Distributionspurpose as its backing store should not contain kernel pointers.
387*2c2f96dcSApple OSS Distributions
388