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