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