Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/zeroize/tests/alloc.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / zeroize / tests / alloc.rs` 74 lines ยท 1804 bytes Raw Blame History 1 //! Tests for `Zeroize` impls on heap-allocated data structures 2 3 #![allow(clippy::std_instead_of_core, clippy::undocumented_unsafe_blocks)] 4 5 use std::alloc::{GlobalAlloc, Layout, System}; 6 use zeroize::Zeroize; 7 8 // Allocator that ensures that deallocated data is zeroized. 9 struct ProxyAllocator; 10 11 unsafe impl GlobalAlloc for ProxyAllocator { 12 unsafe fn alloc(&self, layout: Layout) -> *mut u8 { 13 unsafe { System.alloc(layout) } 14 } 15 16 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { 17 if layout.size() == 160 { 18 for i in 0..layout.size() { 19 let b = unsafe { core::ptr::read(ptr.add(i)) }; 20 assert_eq!(b, 0); 21 } 22 } 23 24 unsafe { System.dealloc(ptr, layout) } 25 } 26 } 27 28 #[global_allocator] 29 static PROXY_ALLOCATOR: ProxyAllocator = ProxyAllocator; 30 31 struct SecretBox(Box); 32 33 impl SecretBox { 34 fn new(val: S) -> Self { 35 Self(Box::new(val)) 36 } 37 } 38 39 impl Drop for SecretBox { 40 fn drop(&mut self) { 41 self.0.as_mut().zeroize(); 42 } 43 } 44 45 #[test] 46 fn secret_box_alloc_test() { 47 let b1 = SecretBox::new([u128::MAX; 10]); 48 core::hint::black_box(&b1); 49 let b2 = SecretBox::new([u8::MAX; 160]); 50 core::hint::black_box(&b2); 51 } 52 53 struct ObserveSecretBox(Box); 54 55 impl ObserveSecretBox { 56 fn new(val: S) -> Self { 57 Self(Box::new(val)) 58 } 59 } 60 61 impl Drop for ObserveSecretBox { 62 fn drop(&mut self) { 63 *self.0 = Default::default(); 64 zeroize::optimization_barrier(&self); 65 } 66 } 67 68 #[test] 69 fn observe_secret_box_alloc_test() { 70 let b1 = ObserveSecretBox::new([u128::MAX; 10]); 71 core::hint::black_box(&b1); 72 let b2 = SecretBox::new([u8::MAX; 160]); 73 core::hint::black_box(&b2); 74 }