-
Notifications
You must be signed in to change notification settings - Fork 140
Add unsafe test to check that dropped values are zeroized #1180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,3 +208,60 @@ fn asref() { | |
let _asmut: &mut [u8] = buffer.as_mut(); | ||
let _asref: &[u8] = buffer.as_ref(); | ||
} | ||
|
||
#[cfg(not(miri))] | ||
#[cfg(feature = "test-allocator")] | ||
mod zeroization_with_custom_allocator { | ||
newpavlov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use super::*; | ||
use core::ptr; | ||
use std::alloc::{GlobalAlloc, Layout, System}; | ||
// Allocator that leaks all memory it allocates, thus leaving the memory open for inspection. | ||
struct UnfreeAllocator; | ||
unsafe impl GlobalAlloc for UnfreeAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
unsafe { System.alloc(layout) } | ||
} | ||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
// Do nothing, leak memory | ||
let _ = (ptr, layout); | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static UNFREE_ALLOCATOR: UnfreeAllocator = UnfreeAllocator; | ||
|
||
#[test] | ||
#[allow(unsafe_code, unused_assignments)] | ||
fn clears_memory_when_scope_ends() { | ||
struct SecretBox<S: Zeroize + ?Sized>(Box<S>); | ||
impl<S: Zeroize + ?Sized> Drop for SecretBox<S> { | ||
fn drop(&mut self) { | ||
self.0.as_mut().zeroize() | ||
} | ||
} | ||
|
||
let mut ptr: *const u128 = ptr::null(); | ||
|
||
unsafe { | ||
{ | ||
let secret = SecretBox(Box::new(0xdeadbeef_u128)); | ||
let boxptr = &secret as *const SecretBox<u128>; | ||
let boxptr = boxptr as *const *const u128; | ||
ptr = *boxptr; | ||
assert!(!ptr.is_null(), "ptr is null before drop, not ok"); | ||
let bytes: &[u8] = core::slice::from_raw_parts(ptr as *const u8, size_of::<u128>()); | ||
assert!( | ||
!bytes.iter().all(|&b| b == 0), | ||
"Expected non-zero data, instead found 0s: {:X?}", | ||
bytes | ||
); | ||
} | ||
// Check that the memory is cleared after the scope ends | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw those tests but felt that, given how magic |
||
for _ in 0..size_of::<u128>() { | ||
// This is UB but proooobably fine given the leaking allocator. | ||
let byte = *(ptr as *const u8).add(1); | ||
assert_eq!(byte, 0); | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.