File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ use std:: alloc:: { GlobalAlloc , Layout , System } ;
2+
3+ use zeroize:: Zeroize ;
4+
5+ // Allocator that ensures that deallocated data is zeroized.
6+ struct ProxyAllocator ;
7+
8+ unsafe impl GlobalAlloc for ProxyAllocator {
9+ unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
10+ unsafe { System . alloc ( layout) }
11+ }
12+
13+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
14+ if layout. size ( ) == 160 {
15+ for i in 0 ..layout. size ( ) {
16+ let b = unsafe { core:: ptr:: read ( ptr. add ( i) ) } ;
17+ if b != 0 {
18+ panic ! ( )
19+ }
20+ }
21+ }
22+
23+ unsafe { System . dealloc ( ptr, layout) }
24+ }
25+ }
26+
27+ #[ global_allocator]
28+ static PROXY_ALLOCATOR : ProxyAllocator = ProxyAllocator ;
29+
30+ struct SecretBox < S : Zeroize > ( Box < S > ) ;
31+
32+ impl < S : Zeroize > SecretBox < S > {
33+ fn new ( val : S ) -> Self {
34+ Self ( Box :: new ( val) )
35+ }
36+ }
37+
38+ impl < S : Zeroize > Drop for SecretBox < S > {
39+ fn drop ( & mut self ) {
40+ self . 0 . as_mut ( ) . zeroize ( )
41+ }
42+ }
43+
44+ #[ test]
45+ fn proxy_alloc_test ( ) {
46+ let _b1 = SecretBox :: new ( [ u128:: MAX ; 10 ] ) ;
47+ let _b2 = SecretBox :: new ( [ u8:: MAX ; 160 ] ) ;
48+ }
You can’t perform that action at this time.
0 commit comments