pub struct MAProper;
Expand description
The MAProper
memory allocator
This memory allocator is an extension around std::alloc::System
which ensures that the
allocated memory is always erased before it is deallocated.
§Using MAProper
as global allocator
#[global_allocator]
static MA_PROPER: MAProper = MAProper;
fn main() {
// This `Vec` will allocate memory through `MA_PROPER` above
let mut v = Vec::new();
v.push(1);
}
§How it works
§Allocation
To ensure that we have enough information to erase everything, we allocate slightly more memory than requested and prepend some checksummed metadata to it. So a final chunk looks like this:
Layout: [ metadata | alignment padding | requested memory ]
Length: META_LEN | dynamic | user specified
Then we increment the pointer so that it points to requested memory
and return it.
§Deallocation
Once the pointer is to be deallocated, we rewind the pointer so that it points to
metadata/length info
again to read and verify it. Once we know the length, we overwrite the
entire allocated space using one of
memset_s
/SecureZeroMemory
/explicit_bzero
/explicit_memset
.
Then we deallocate it.
§Important
Please note that MAProper
only erases memory that is deallocated properly. This especially
means that:
- stack items are not overwritten by this allocator – to erase stack memory, we expose
MAProper::erase_slice
andMAProper::erase_ptr<T>
so that you can erase them manually if necessary - depending on your panic-policy and your
Rc
/Arc
use (retain-cycles), the destructor (and thus the deallocator) may never be called
Trait Implementations§
Source§impl GlobalAlloc for MAProper
impl GlobalAlloc for MAProper
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout
. Read more