@@ -157,36 +157,44 @@ impl<'mir, 'tcx> GlobalStateInner {
157157 }
158158
159159 fn alloc_base_addr ( ecx : & MiriEvalContext < ' mir , ' tcx > , alloc_id : AllocId ) -> u64 {
160- // TODO avoid leaked address hack
161- let mut is_global = false ;
162- let base_addr: u64 = match ecx. get_alloc_base_addr ( alloc_id) {
163- Ok ( addr) => {
164- assert ! ( addr. bytes( ) % 16 == 0 ) ;
165- addr. bytes ( )
166- }
167- // Grabbing u128 for max alignment
160+ // With our hack, base_addr should always be fully aligned
161+ let mut global_state = match ecx. machine . intptrcast . try_borrow_mut ( ) {
162+ Ok ( gstate) => gstate,
168163 Err ( _) => {
169- is_global = true ;
170- Box :: leak ( Box :: new ( 0u128 ) ) as * const u128 as u64
164+ // we're recursing
165+ let new_addr = Box :: leak ( Box :: new ( 0u128 ) ) as * const u128 as u64 ;
166+ unsafe {
167+ ( * ecx. machine . intptrcast . as_ptr ( ) ) . base_addr . insert ( alloc_id, new_addr) ;
168+ ( * ecx. machine . intptrcast . as_ptr ( ) ) . int_to_ptr_map . insert ( new_addr, alloc_id) ;
169+ }
170+ trace ! (
171+ "Recursive case: Assigning base address {:#x} to allocation {:?}" ,
172+ new_addr,
173+ alloc_id,
174+ ) ;
175+ return new_addr;
171176 }
172177 } ;
173- // With our hack, base_addr should always be fully aligned
174- let mut global_state = ecx. machine . intptrcast . borrow_mut ( ) ;
175178 let global_state = & mut * global_state;
176179
177180 match global_state. base_addr . entry ( alloc_id) {
178181 Entry :: Occupied ( entry) => * entry. get ( ) ,
179182 Entry :: Vacant ( entry) => {
183+ let base_addr = match ecx. get_alloc_base_addr ( alloc_id) {
184+ Ok ( addr) => {
185+ assert ! ( addr. bytes( ) % 16 == 0 ) ;
186+ addr. bytes ( )
187+ }
188+ // Grabbing u128 for max alignment
189+ Err ( _) => {
190+ // TODO avoid leaked address hack
191+ Box :: leak ( Box :: new ( 0u128 ) ) as * const u128 as u64
192+ }
193+ } ;
180194 // There is nothing wrong with a raw pointer being cast to an integer only after
181195 // it became dangling. Hence we allow dead allocations.
182196 let ( size, align, _kind) = ecx. get_alloc_info ( alloc_id) ;
183197
184- // println!("REE: {:?}, {:?}, {:?}", align, base_addr % align.bytes(), is_global);
185- let what = Self :: align_addr ( base_addr, align. bytes ( ) ) ;
186- if ( what != base_addr) {
187- // println!("REEE: {:?}, {:?}, {:?}", what, base_addr, alloc_id);
188- }
189-
190198 // This allocation does not have a base address yet, assign its bytes base.
191199 entry. insert ( base_addr) ;
192200 trace ! (
@@ -200,7 +208,7 @@ impl<'mir, 'tcx> GlobalStateInner {
200208 // Map has no duplicates so no need to remove copies.
201209 // Map is always sorted.
202210 global_state. int_to_ptr_map . insert ( base_addr, alloc_id) ;
203-
211+
204212 base_addr
205213 }
206214 }
0 commit comments