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