Skip to content

Commit ca60134

Browse files
committed
remove allocations from int_to_ptr_map when they get freed
1 parent 815a51c commit ca60134

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/intptrcast.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ pub type GlobalState = RefCell<GlobalStateInner>;
2626

2727
#[derive(Clone, Debug)]
2828
pub 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.
32+
/// This is not the *full* inverse of `base_addr`; dead allocations have been removed.
3133
int_to_ptr_map: Vec<(u64, AllocId)>,
3234
/// The base address for each allocation. We cannot put that into
3335
/// `AllocExtra` because function pointers also have a base address, and
@@ -102,18 +104,14 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
102104
}
103105
}?;
104106

105-
// We only use this provenance if it has been exposed, *and* is still live.
107+
// We only use this provenance if it has been exposed.
106108
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-
}
109+
// This must still be live, since we remove allocations from `int_to_ptr_map` when they get freed.
110+
debug_assert!(!matches!(ecx.get_alloc_info(alloc_id).2, AllocKind::Dead));
111+
Some(alloc_id)
112+
} else {
113+
None
114114
}
115-
116-
None
117115
}
118116

119117
fn addr_from_alloc_id(&self, alloc_id: AllocId) -> InterpResult<'tcx, u64> {
@@ -124,9 +122,13 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
124122
Ok(match global_state.base_addr.entry(alloc_id) {
125123
Entry::Occupied(entry) => *entry.get(),
126124
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);
125+
let (size, align, kind) = ecx.get_alloc_info(alloc_id);
126+
// This is either called immediately after allocation (and then cached), or when
127+
// adjusting `tcx` pointers (which never get freed). So assert that we are looking
128+
// at a live allocation. This also ensures that we never re-assign an address to an
129+
// allocation that previously had an address, but then was freed and the address
130+
// information was removed.
131+
assert!(!matches!(kind, AllocKind::Dead));
130132

131133
// This allocation does not have a base address yet, pick one.
132134
// Leave some space to the previous allocation, to give it some chance to be less aligned.
@@ -162,6 +164,7 @@ trait EvalContextExtPriv<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
162164
if global_state.next_base_addr > ecx.target_usize_max() {
163165
throw_exhaust!(AddressSpaceFull);
164166
}
167+
// Also maintain the opposite mapping in `int_to_ptr_map`.
165168
// Given that `next_base_addr` increases in each allocation, pushing the
166169
// corresponding tuple keeps `int_to_ptr_map` sorted
167170
global_state.int_to_ptr_map.push((base_addr, alloc_id));
@@ -246,7 +249,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
246249
};
247250

248251
// This cannot fail: since we already have a pointer with that provenance, rel_ptr_to_addr
249-
// must have been called in the past.
252+
// must have been called in the past, so we can just look up the address in the map.
250253
let base_addr = ecx.addr_from_alloc_id(alloc_id).unwrap();
251254

252255
// Wrapping "addr - base_addr"
@@ -260,6 +263,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
260263
}
261264
}
262265

266+
impl GlobalStateInner {
267+
pub fn free_alloc_id(&mut self, dead_id: AllocId) {
268+
// We can *not* remove this from `base_addr`, since `addr_from_alloc_id` is called on each
269+
// attempt at a memory access to determine the allocation ID and offset -- and there can
270+
// still be pointers with `dead_id` that one can attempt to use for a memory access.
271+
// However, we *can* remove it from `int_to_ptr_map`, since any wildcard pointers that exist
272+
// can 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+
// We can also remove it from `exposed`, since this allocation can anyway not be returned by
276+
// `alloc_id_from_addr` any more.
277+
self.exposed.remove(&dead_id);
278+
}
279+
}
280+
263281
#[cfg(test)]
264282
mod tests {
265283
use super::*;

src/machine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
12381238
{
12391239
*deallocated_at = Some(machine.current_span());
12401240
}
1241+
machine.intptrcast.get_mut().free_alloc_id(alloc_id);
12411242
Ok(())
12421243
}
12431244

0 commit comments

Comments
 (0)