use rand::Rng;
use rustc_data_structures::fx::FxHashMap;
-use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic};
use rustc_target::abi::{Size, HasDataLayout};
-use crate::{Evaluator, Tag, STACK_ADDR};
+use crate::*;
pub type MemoryExtra = RefCell<GlobalState>;
let global_state = memory.extra.intptrcast.borrow();
let pos = global_state.int_to_ptr_map.binary_search_by_key(&int, |(addr, _)| *addr);
+ // The int must be in-bounds after being cast to a pointer, so we error
+ // with `CheckInAllocMsg::InboundsTest`.
Ok(match pos {
Ok(pos) => {
let (_, alloc_id) = global_state.int_to_ptr_map[pos];
// zero. The pointer is untagged because it was created from a cast
Pointer::new_with_tag(alloc_id, Size::from_bytes(0), Tag::Untagged)
}
- Err(0) => throw_ub!(InvalidIntPointerUsage(int)),
+ Err(0) => throw_ub!(DanglingIntPointer(int, CheckInAllocMsg::InboundsTest)),
Err(pos) => {
// This is the largest of the adresses smaller than `int`,
// i.e. the greatest lower bound (glb)
// This pointer is untagged because it was created from a cast
Pointer::new_with_tag(alloc_id, Size::from_bytes(offset), Tag::Untagged)
} else {
- throw_ub!(InvalidIntPointerUsage(int))
+ throw_ub!(DanglingIntPointer(int, CheckInAllocMsg::InboundsTest))
}
}
})
) -> InterpResult<'tcx, u64> {
let mut global_state = memory.extra.intptrcast.borrow_mut();
let global_state = &mut *global_state;
- let id = Evaluator::canonical_alloc_id(memory, ptr.alloc_id);
+ let id = ptr.alloc_id;
// There is nothing wrong with a raw pointer being cast to an integer only after
// it became dangling. Hence `MaybeDead`.
let slack = {
let mut rng = memory.extra.rng.borrow_mut();
// This means that `(global_state.next_base_addr + slack) % 16` is uniformly distributed.
- rng.gen_range(0, 16)
+ rng.gen_range(0..16)
};
// From next_base_addr + slack, round up to adjust for alignment.
let base_addr = global_state.next_base_addr.checked_add(slack).unwrap();