@@ -26,8 +26,9 @@ pub type GlobalState = RefCell<GlobalStateInner>;
2626
2727#[ derive( Clone , Debug ) ]
2828pub struct GlobalStateInner {
29- /// This is used as a map between the address of each allocation and its `AllocId`.
30- /// It is always sorted
29+ /// This is used as a map between the address of each allocation and its `AllocId`. It is always
30+ /// sorted. We cannot use a `HashMap` since we can be given an address that is offset from the
31+ /// base address, and we need to find the `AllocId` it belongs to.
3132 int_to_ptr_map : Vec < ( u64 , AllocId ) > ,
3233 /// The base address for each allocation. We cannot put that into
3334 /// `AllocExtra` because function pointers also have a base address, and
@@ -102,18 +103,14 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
102103 }
103104 } ?;
104105
105- // We only use this provenance if it has been exposed, *and* is still live .
106+ // We only use this provenance if it has been exposede .
106107 if global_state. exposed . contains ( & alloc_id) {
107- let ( _size, _align, kind) = ecx. get_alloc_info ( alloc_id) ;
108- match kind {
109- AllocKind :: LiveData | AllocKind :: Function | AllocKind :: VTable => {
110- return Some ( alloc_id) ;
111- }
112- AllocKind :: Dead => { }
113- }
108+ // This must still be live, since we remove allocations from `int_to_ptr_map` when they get freed.
109+ debug_assert ! ( !matches!( ecx. get_alloc_info( alloc_id) . 2 , AllocKind :: Dead ) ) ;
110+ Some ( alloc_id)
111+ } else {
112+ None
114113 }
115-
116- None
117114 }
118115
119116 fn addr_from_alloc_id ( & self , alloc_id : AllocId ) -> InterpResult < ' tcx , u64 > {
@@ -124,9 +121,12 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
124121 Ok ( match global_state. base_addr . entry ( alloc_id) {
125122 Entry :: Occupied ( entry) => * entry. get ( ) ,
126123 Entry :: Vacant ( entry) => {
127- // There is nothing wrong with a raw pointer being cast to an integer only after
128- // it became dangling. Hence we allow dead allocations.
129- let ( size, align, _kind) = ecx. get_alloc_info ( alloc_id) ;
124+ let ( size, align, kind) = ecx. get_alloc_info ( alloc_id) ;
125+ // This is only called just after allocation, and when adjusting `tcx` pointers. So
126+ // the allocation can never be dead here. This also ensures that we never re-assign
127+ // an address to an allocation that previously had an address, but then was freed
128+ // and the address information was removed.
129+ assert ! ( !matches!( kind, AllocKind :: Dead ) ) ;
130130
131131 // This allocation does not have a base address yet, pick one.
132132 // Leave some space to the previous allocation, to give it some chance to be less aligned.
@@ -162,6 +162,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
162162 if global_state. next_base_addr > ecx. target_usize_max ( ) {
163163 throw_exhaust ! ( AddressSpaceFull ) ;
164164 }
165+ // Also maintain the opposite mapping in `int_to_ptr_map`.
165166 // Given that `next_base_addr` increases in each allocation, pushing the
166167 // corresponding tuple keeps `int_to_ptr_map` sorted
167168 global_state. int_to_ptr_map . push ( ( base_addr, alloc_id) ) ;
@@ -246,7 +247,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
246247 } ;
247248
248249 // This cannot fail: since we already have a pointer with that provenance, rel_ptr_to_addr
249- // must have been called in the past.
250+ // must have been called in the past, so we can just look up the address in the map .
250251 let base_addr = ecx. addr_from_alloc_id ( alloc_id) . unwrap ( ) ;
251252
252253 // Wrapping "addr - base_addr"
@@ -260,6 +261,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
260261 }
261262}
262263
264+ impl GlobalStateInner {
265+ pub fn free_alloc_id ( & mut self , dead_id : AllocId ) {
266+ // We can remove this from `base_addr`, since `addr_from_alloc_id` is only called right
267+ // after the allocation was created (to construct the first pointer with the absolute
268+ // address), and when converting `tcx` pointers (which point to things that can never be
269+ // freed anyway).
270+ self . base_addr . remove ( & dead_id) ;
271+ // We can also remove it from `int_to_ptr_map`, since any wildcard pointers that exist can
272+ // no longer actually be accessing that address. This ensures `alloc_id_from_addr` never
273+ // returns a dead allocation.
274+ self . int_to_ptr_map . retain ( |& ( _, id) | id != dead_id) ;
275+ }
276+ }
277+
263278#[ cfg( test) ]
264279mod tests {
265280 use super :: * ;
0 commit comments