@@ -64,6 +64,7 @@ impl MachineAlloc {
6464 fn add_page ( & mut self ) {
6565 let page_layout =
6666 unsafe { Layout :: from_size_align_unchecked ( self . page_size , self . page_size ) } ;
67+ // We don't overwrite the bytes we hand out so make sure they're zeroed by default!
6768 let page_ptr = unsafe { alloc:: alloc ( page_layout) } ;
6869 self . allocated . push ( vec ! [ 0u8 ; self . page_size / 8 ] . into_boxed_slice ( ) ) ;
6970 self . pages . push ( page_ptr) ;
@@ -90,7 +91,7 @@ impl MachineAlloc {
9091 let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
9192 unsafe {
9293 if alloc. enabled {
93- ( alloc. alloc_inner ( layout, alloc :: alloc ) , false )
94+ ( alloc. alloc_inner ( layout, false ) , false )
9495 } else {
9596 ( alloc:: alloc ( layout) , true )
9697 }
@@ -105,7 +106,7 @@ impl MachineAlloc {
105106 pub unsafe fn alloc_zeroed ( layout : Layout ) -> ( * mut u8 , bool ) {
106107 let mut alloc = ALLOCATOR . lock ( ) . unwrap ( ) ;
107108 if alloc. enabled {
108- let ptr = unsafe { alloc. alloc_inner ( layout, alloc :: alloc_zeroed ) } ;
109+ let ptr = unsafe { alloc. alloc_inner ( layout, true ) } ;
109110 ( ptr, false )
110111 } else {
111112 unsafe { ( alloc:: alloc_zeroed ( layout) , true ) }
@@ -114,15 +115,11 @@ impl MachineAlloc {
114115
115116 /// SAFETY: The allocator must have been `enable()`d already and
116117 /// the `layout` must be valid.
117- unsafe fn alloc_inner (
118- & mut self ,
119- layout : Layout ,
120- sys_allocator : unsafe fn ( Layout ) -> * mut u8 ,
121- ) -> * mut u8 {
118+ unsafe fn alloc_inner ( & mut self , layout : Layout , zeroed : bool ) -> * mut u8 {
122119 let ( size, align) = MachineAlloc :: normalized_layout ( layout) ;
123120
124121 if align > self . page_size || size > self . page_size {
125- unsafe { self . alloc_multi_page ( layout, sys_allocator ) }
122+ unsafe { self . alloc_multi_page ( layout, zeroed ) }
126123 } else {
127124 for ( page, pinfo) in std:: iter:: zip ( & mut self . pages , & mut self . allocated ) {
128125 for idx in ( 0 ..self . page_size ) . step_by ( align) {
@@ -138,26 +135,27 @@ impl MachineAlloc {
138135 if ret. addr ( ) >= page. addr ( ) + self . page_size {
139136 panic ! ( "Returing {} from page {}" , ret. addr( ) , page. addr( ) ) ;
140137 }
141- return page. offset ( idx. try_into ( ) . unwrap ( ) ) ;
138+ if zeroed {
139+ // No need to zero out more than was specifically requested
140+ ret. write_bytes ( 0 , layout. size ( ) ) ;
141+ }
142+ return ret;
142143 }
143144 }
144145 }
145146 }
146147
147148 // We get here only if there's no space in our existing pages
148149 self . add_page ( ) ;
149- unsafe { self . alloc_inner ( layout, sys_allocator ) }
150+ unsafe { self . alloc_inner ( layout, zeroed ) }
150151 }
151152 }
152153
153154 /// SAFETY: Same as `alloc_inner()` with the added requirement that `layout`
154155 /// must ask for a size larger than the host pagesize.
155- unsafe fn alloc_multi_page (
156- & mut self ,
157- layout : Layout ,
158- sys_allocator : unsafe fn ( Layout ) -> * mut u8 ,
159- ) -> * mut u8 {
160- let ret = unsafe { sys_allocator ( layout) } ;
156+ unsafe fn alloc_multi_page ( & mut self , layout : Layout , zeroed : bool ) -> * mut u8 {
157+ let ret =
158+ unsafe { if zeroed { alloc:: alloc_zeroed ( layout) } else { alloc:: alloc ( layout) } } ;
161159 self . huge_allocs . push ( ( ret, layout. size ( ) ) ) ;
162160 ret
163161 }
@@ -197,6 +195,7 @@ impl MachineAlloc {
197195 let size_pinfo = size / 8 ;
198196 // Everything is always aligned to at least 8 bytes so this is ok
199197 pinfo[ ptr_idx_pinfo..ptr_idx_pinfo + size_pinfo] . fill ( 0 ) ;
198+ // And also zero out the page contents!
200199 }
201200
202201 let mut free = vec ! [ ] ;
@@ -219,11 +218,10 @@ impl MachineAlloc {
219218 /// SAFETY: Same as `dealloc()` with the added requirement that `layout`
220219 /// must ask for a size larger than the host pagesize.
221220 unsafe fn dealloc_multi_page ( & mut self , ptr : * mut u8 , layout : Layout ) {
222- let ( idx, _ ) = self
221+ let idx = self
223222 . huge_allocs
224223 . iter ( )
225- . enumerate ( )
226- . find ( |pg| ptr. addr ( ) == pg. 1 . 0 . addr ( ) )
224+ . position ( |pg| ptr. addr ( ) == pg. 0 . addr ( ) )
227225 . expect ( "Freeing unallocated pages" ) ;
228226 let ptr = self . huge_allocs . remove ( idx) . 0 ;
229227 unsafe {
0 commit comments