Lookaside Lists Allocator for Rust
This crate provides an experimental Rust allocator for the Windows Kernel based on 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) hence this being an experimental/unstable crate.
Usage
The crate can be used by means of the allocator_api or directly by using the LookasideAlloc struct.
Rust Driver
If working on a Driver fully written in Rust, the following example shows how we can make use of the crate by means of the Allocator API.
extern crate win_lookaside;
extern crate alloc;
use Box;
use LookasideAlloc;
use NonPagedPool;
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.
The above example would generate the following simplified pseudocode when looking at it with a decompiler:
void
C++ Driver
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.
A very simple implementation of how this FFI layer could look like is the following:
extern crate win_lookaside;
extern crate alloc;
use Box;
use PagedPool;
use ;
use LookasideAlloc;
// Interior mutability due to the way the Lookaside API works
static mut LOOKASIDE: LookasideAlloc = default;
;
pub unsafe extern "C"
pub extern "C"
pub extern "C"
pub unsafe extern "C"
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.
We could then use this FFI layer from our C++ Driver in the following fashion:
// No error handling
We can inspect the state of the Lookaside List just before the call to free_lookaside with the WinDBG extension !lookaside to get an output likeso:
4: kd> !lookaside fffff80474866030
Lookaside "" @ 0xfffff80474866030 Tag(hex): 0x61616262 "bbaa"
Type = 0000 NonPagedPool
Current Depth = 0 Max Depth = 4
Size = 8 Max Alloc = 32
AllocateMisses = 4 FreeMisses = 0
TotalAllocates = 4 TotalFrees = 4
Hit Rate = 0% Hit Rate = 100%
Remarks
- This crate has been written with love but is still experimental!! Please keep that in mind before making use of it 😄.
- 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.
- No benchmark nor performance test has been done, although the crate has been tested running under driver verifier with standard settings.
- Default implementation of methods grow, grow_zeroed & shrink of the Allocator API have been overridden and will panic if used since this represents a misuse of the Lookaside API.
TODO
- Use Lookaside API bindings from windows-sys when available (Look into WDK metadata).
- Use Native MS synchronization primitives.
- Test the crate with a WDK older than 22H2.