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