X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fintptrcast.rs;h=188ff94861bdd194a223f092f5c24149c276b725;hb=2670839e1af540a496a0d889fce9ad42529ecc11;hp=8eb28e4f4700a6d29e3c97e13a520097d72425e0;hpb=8948a29a4cb3c6940e3863886e99bacfa1f325ae;p=rust.git diff --git a/src/intptrcast.rs b/src/intptrcast.rs index 8eb28e4f470..188ff94861b 100644 --- a/src/intptrcast.rs +++ b/src/intptrcast.rs @@ -6,11 +6,9 @@ use rand::Rng; use rustc_data_structures::fx::FxHashMap; -use rustc_middle::ty::layout::HasDataLayout; -use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic}; -use rustc_target::abi::Size; +use rustc_target::abi::{Size, HasDataLayout}; -use crate::{Evaluator, Tag, STACK_ADDR}; +use crate::*; pub type MemoryExtra = RefCell; @@ -42,11 +40,13 @@ fn default() -> Self { impl<'mir, 'tcx> GlobalState { pub fn int_to_ptr( int: u64, - memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>, + memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>, ) -> InterpResult<'tcx, Pointer> { 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]; @@ -54,7 +54,7 @@ pub fn int_to_ptr( // 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) @@ -66,7 +66,7 @@ pub fn int_to_ptr( // 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)) } } }) @@ -74,11 +74,11 @@ pub fn int_to_ptr( pub fn ptr_to_int( ptr: Pointer, - memory: &Memory<'mir, 'tcx, Evaluator<'tcx>>, + memory: &Memory<'mir, 'tcx, Evaluator<'mir, 'tcx>>, ) -> 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`.