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