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