mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Zeroizing allocator (#154)
* Add a zeroizing allocator * Also implement the allocator API * Add misisng license file to zalloc * Slight change to zalloc description
This commit is contained in:
46
common/zalloc/src/lib.rs
Normal file
46
common/zalloc/src/lib.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
||||
#![cfg_attr(feature = "allocator", feature(allocator_api))]
|
||||
|
||||
//! Implementation of a Zeroizing Allocator, enabling zeroizing memory on deallocation.
|
||||
//! This can either be used with Box (requires nightly and the "allocator" feature) to provide the
|
||||
//! functionality of zeroize on types which don't implement zeroize, or used as a wrapper around
|
||||
//! the global allocator to ensure *all* memory is zeroized.
|
||||
|
||||
use core::{
|
||||
slice,
|
||||
alloc::{Layout, GlobalAlloc},
|
||||
};
|
||||
|
||||
use zeroize::Zeroize;
|
||||
|
||||
/// An allocator wrapper which zeroizes its memory on dealloc.
|
||||
pub struct ZeroizingAlloc<T>(pub T);
|
||||
|
||||
#[cfg(feature = "allocator")]
|
||||
use core::{
|
||||
ptr::NonNull,
|
||||
alloc::{AllocError, Allocator},
|
||||
};
|
||||
#[cfg(feature = "allocator")]
|
||||
unsafe impl<T: Allocator> Allocator for ZeroizingAlloc<T> {
|
||||
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
|
||||
self.0.allocate(layout)
|
||||
}
|
||||
|
||||
unsafe fn deallocate(&self, mut ptr: NonNull<u8>, layout: Layout) {
|
||||
slice::from_raw_parts_mut(ptr.as_mut(), layout.size()).zeroize();
|
||||
self.0.deallocate(ptr, layout);
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl<T: GlobalAlloc> GlobalAlloc for ZeroizingAlloc<T> {
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
self.0.alloc(layout)
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
slice::from_raw_parts_mut(ptr, layout.size()).zeroize();
|
||||
self.0.dealloc(ptr, layout);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user