1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! This crate provides an experimental Rust allocator for the Windows Kernel based on [Lookaside
//! Lists](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/using-lookaside-lists).
//!
//! Given the nature of Lookaside Lists (fixed-size buffers) this Allocator is not meant to be used
//! as the Global Allocator for this reason this crate does not implement the GlobalAlloc trait,
//! on the other hand it does implement the Allocator trait (no grow nor shrink) so it can
//! be used as the allocator in `xxx_in` methods.
//! > The default implementation of grow/grow_zeroed & shrink of the Allocator API has been
//! overridden to throw a panic. This is done just to make the user aware that calling these
//! methods on this allocator is a misuse of the Lookaside API.
//!
//! Obviously, this crate requires the `allocator_api`feature is enabled (hence this being an
//! experimental/unstable crate).
//!
//! Alternatively, this can be used directly by initializing the allocator and using allocate &
//! deallocate methods to get an entry from the list as a `*mut u8` and return an entry to the
//! list respectively.
//!
//! Since this is meant to be used in the kernel, this Allocator can return `NULL` and doesn't
//! trigger the `alloc_error_handler` if an OOM condition happens. This requires using
//! fallible APIs such as [Box::try_new_in](https://doc.rust-lang.org/nightly/alloc/boxed/struct.Box.html#method.try_new_in)
//! or crates such as [fallible_vec](https://docs.rs/fallible_vec/latest/fallible_vec/index.html).
//!
//! # Usage
//!
//! If working on a Driver fully written in Rust, the following example shows how we can make use
//! of the Allocator.
//!
//! All in one function just for the sake of the example, usually we would store the Lookaside
//! Allocator in some structure or global variable initialized in the DriverEntry and destroy it
//! in the DriverUnload.
//! ```
//! #![no_std]
//! #![feature(allocator_api)]
//! #[macro_use] extern crate win_lookaside;
//!
//! extern crate alloc;
//!
//! use alloc::boxed::Box;
//! use win_lookaside::LookasideAlloc;
//! use windows_sys::Wdk::Foundation::NonPagedPool;
//!
//! fn example() {
//! // Init Lookaside List allocator with default values//!
//! let mut allocator = LookasideAlloc::default();
//!
//! // Init Lookaside List with fixed-size to hold a u32
//! // Properly handle possible InitError;
//! allocator.init(core::mem::size_of::<u32>(), NonPagedPool as i32, None, None, None).unwrap();
//!
//! // Allocate from Lookaside & Free to it on Drop
//! {
//! let Ok(ctx) = Box::try_new_in(10, &allocator) else {
//! return; // AllocError
//! };
//! }
//!
//! // Destroy Lookaside List Allocator
//! allocator.destroy();
//! }
//! ```
//!
//!
//! Another option is if we are working with a Driver written in C++ and we want to work on a
//! extensions/component in Rust. We can write a thin FFI layer on top of this crate to expose the
//! functionality to the Driver.
//!
//! A very simple implementation of how this FFI layer could look like is the following:
//! ```
//! #![no_std]
//! #![feature(allocator_api)]
//! #[macro_use] extern crate win_lookaside;
//!
//! extern crate alloc;
//!
//! use alloc::boxed::Box;
//! use windows_sys::Wdk::Foundation::PagedPool;
//! use windows_sys::Win32::Foundation::{NTSTATUS, STATUS_INSUFFICIENT_RESOURCES, STATUS_SUCCESS};
//! use win_lookaside::LookasideAlloc;
//!
//! // Interior mutability due to the way the Lookaside API works
//! static mut LOOKASIDE: LookasideAlloc = LookasideAlloc::default();
//!
//! struct Context{};
//!
//! #[no_mangle]
//! pub unsafe extern "C" fn init_lookaside(tag: u32) -> NTSTATUS {
//! LOOKASIDE.init(core::mem::size_of::<Context>(), PagedPool, Some(tag), None, None )?;
//! STATUS_SUCCESS
//! }
//!
//! #[no_mangle]
//! pub extern "C" fn create_context(context: *mut *mut Context) -> FfiResult<()> {
//! let Ok(ctx) = unsafe { Box::try_new_in(Context {}, &LOOKASIDE) } else {
//! return STATUS_INSUFFICIENT_RESOURCES;
//! };
//!
//! unsafe {
//! *context = Box::into_raw(ctx);
//! }
//!
//! STATUS_SUCCESS
//! }
//!
//! #[no_mangle]
//! pub extern "C" fn remove_context(context: *mut Context) {
//! let _ctx = unsafe { Box::from_raw_in(context, &LOOKASIDE) };
//! }
//!
//! #[no_mangle]
//! pub unsafe extern "C" fn free_lookaside() {
//! LOOKASIDE.destroy();
//! }
//! ```
//! > Here the Context is just an empty struct, but it could be something more complex that could
//! offer more functionality and the C++ driver would just need to store those as an opaque pointer.
//!
//! # Remarks
//! This crate has been developed under the 22H2 WDK meaning certain Lookaside API methods are
//! exported instead of inlined. The crate is yet to be tested in an older WDK, behavior when
//! trying to build might be different.
//!
//! At the moment the crate uses [spin](https://crates.io/crates/spin) as the synchronization
//! mechanism. Even thou this does the job, ideally at some point it should use synchronization
//! primitives native to the OS.
//!
//!
use ;
/*
TODO: Review WDK metadata https://github.com/microsoft/wdkmetadata#overview
TODO: Use bindings from windows-rs when available
use windows_sys::{
Wdk::Foundation::POOL_TYPE,
Win32::Foundation::{NTSTATUS, STATUS_SUCCESS},
};
use windows_sys::Wdk::System::SystemServices::{
ExAllocateFromLookasideListEx,
ExDeleteLookasideListEx,
ExFlushLookasideListEx,
ExFreeToLookasideListEx,
ExInitializeLookasideListEx,
};
*/
/// Default PoolTag used by the Lookaside Allocator if none passed when initializing it
pub const DEFAULT_POOL_TAG: u32 = u32from_ne_bytes;
/// Possible Errors returned by the Lookaside List Allocator
/// Lookaside List Allocator Result
pub type LookasideResult<T> = ;
/// The LookasideListAllocateEx routine allocates the storage for a new lookaside-list entry when
/// a client requests an entry from a lookaside list that is empty.
///
/// More info: <https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nc-wdm-allocate_function_ex>
pub type AllocateFunctionEx = nsafe extern "system" fn ,
>;
/// The LookasideListFreeEx routine frees the storage for a lookaside-list entry when a client
/// tries to insert the entry into a lookaside list that is full.
///
/// More info: <https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nc-wdm-free_function_ex>
pub type FreeFunctionEx =
;
// Newtype over windows-sys [NTSTATUS (i32)](https://docs.rs/windows-sys/0.48.0/windows_sys/Win32/Foundation/type.NTSTATUS.html)
// Change to windows-sys NTSTATUS when windows-sys bindings are ready
/// Newtype over i32.
;
/// Wrapper over the [_LOOKASIDE_LIST_EX](https://learn.microsoft.com/en-us/windows-hardware/drivers/kernel/eprocess#lookaside_list_ex)
/// type
///
/// See: <https://www.vergiliusproject.com/kernels/x64/Windows%2011/22H2%20(2022%20Update)/_LOOKASIDE_LIST_EX>
extern "system"
/// Lookaside List Allocator
unsafe