interpret: refactor allocation info query
We now have an infallible function that also tells us which kind of allocation we are talking about. Also we do longer have to distinguish between data and function allocations for liveness.
This commit is contained in:
parent
1aabd8a4a6
commit
b094116538
@ -56,15 +56,14 @@ impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used by `get_size_and_align` to indicate whether the allocation needs to be live.
|
/// The return value of `get_alloc_info` indicates the "kind" of the allocation.
|
||||||
#[derive(Debug, Copy, Clone)]
|
pub enum AllocKind {
|
||||||
pub enum AllocCheck {
|
/// A regular live data allocation.
|
||||||
/// Allocation must be live and not a function pointer.
|
LiveData,
|
||||||
Dereferenceable,
|
/// A function allocation (that fn ptrs point to).
|
||||||
/// Allocations needs to be live, but may be a function pointer.
|
Function,
|
||||||
Live,
|
/// A dead allocation.
|
||||||
/// Allocation may be dead.
|
Dead,
|
||||||
MaybeDead,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The value of a function pointer.
|
/// The value of a function pointer.
|
||||||
@ -360,8 +359,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
align,
|
align,
|
||||||
CheckInAllocMsg::MemoryAccessTest,
|
CheckInAllocMsg::MemoryAccessTest,
|
||||||
|alloc_id, offset, tag| {
|
|alloc_id, offset, tag| {
|
||||||
let (size, align) =
|
let (size, align) = self.get_live_alloc_size_and_align(alloc_id)?;
|
||||||
self.get_alloc_size_and_align(alloc_id, AllocCheck::Dereferenceable)?;
|
|
||||||
Ok((size, align, (alloc_id, offset, tag)))
|
Ok((size, align, (alloc_id, offset, tag)))
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
@ -379,15 +377,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
msg: CheckInAllocMsg,
|
msg: CheckInAllocMsg,
|
||||||
) -> InterpResult<'tcx> {
|
) -> InterpResult<'tcx> {
|
||||||
self.check_and_deref_ptr(ptr, size, Some(align), msg, |alloc_id, _, _| {
|
self.check_and_deref_ptr(ptr, size, Some(align), msg, |alloc_id, _, _| {
|
||||||
let check = match msg {
|
let (size, align) = self.get_live_alloc_size_and_align(alloc_id)?;
|
||||||
CheckInAllocMsg::DerefTest | CheckInAllocMsg::MemoryAccessTest => {
|
|
||||||
AllocCheck::Dereferenceable
|
|
||||||
}
|
|
||||||
CheckInAllocMsg::PointerArithmeticTest
|
|
||||||
| CheckInAllocMsg::OffsetFromTest
|
|
||||||
| CheckInAllocMsg::InboundsTest => AllocCheck::Live,
|
|
||||||
};
|
|
||||||
let (size, align) = self.get_alloc_size_and_align(alloc_id, check)?;
|
|
||||||
Ok((size, align, ()))
|
Ok((size, align, ()))
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -655,30 +645,19 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
|
|
||||||
/// Obtain the size and alignment of an allocation, even if that allocation has
|
/// Obtain the size and alignment of an allocation, even if that allocation has
|
||||||
/// been deallocated.
|
/// been deallocated.
|
||||||
///
|
pub fn get_alloc_info(&self, id: AllocId) -> (Size, Align, AllocKind) {
|
||||||
/// If `liveness` is `AllocCheck::MaybeDead`, this function always returns `Ok`.
|
|
||||||
pub fn get_alloc_size_and_align(
|
|
||||||
&self,
|
|
||||||
id: AllocId,
|
|
||||||
liveness: AllocCheck,
|
|
||||||
) -> InterpResult<'tcx, (Size, Align)> {
|
|
||||||
// # Regular allocations
|
// # Regular allocations
|
||||||
// Don't use `self.get_raw` here as that will
|
// Don't use `self.get_raw` here as that will
|
||||||
// a) cause cycles in case `id` refers to a static
|
// a) cause cycles in case `id` refers to a static
|
||||||
// b) duplicate a global's allocation in miri
|
// b) duplicate a global's allocation in miri
|
||||||
if let Some((_, alloc)) = self.memory.alloc_map.get(id) {
|
if let Some((_, alloc)) = self.memory.alloc_map.get(id) {
|
||||||
return Ok((alloc.size(), alloc.align));
|
return (alloc.size(), alloc.align, AllocKind::LiveData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// # Function pointers
|
// # Function pointers
|
||||||
// (both global from `alloc_map` and local from `extra_fn_ptr_map`)
|
// (both global from `alloc_map` and local from `extra_fn_ptr_map`)
|
||||||
if self.get_fn_alloc(id).is_some() {
|
if self.get_fn_alloc(id).is_some() {
|
||||||
return if let AllocCheck::Dereferenceable = liveness {
|
return (Size::ZERO, Align::ONE, AllocKind::Function);
|
||||||
// The caller requested no function pointers.
|
|
||||||
throw_ub!(DerefFunctionPointer(id))
|
|
||||||
} else {
|
|
||||||
Ok((Size::ZERO, Align::ONE))
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// # Statics
|
// # Statics
|
||||||
@ -690,32 +669,38 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
// Use size and align of the type.
|
// Use size and align of the type.
|
||||||
let ty = self.tcx.type_of(did);
|
let ty = self.tcx.type_of(did);
|
||||||
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
|
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
|
||||||
Ok((layout.size, layout.align.abi))
|
(layout.size, layout.align.abi, AllocKind::LiveData)
|
||||||
}
|
}
|
||||||
Some(GlobalAlloc::Memory(alloc)) => {
|
Some(GlobalAlloc::Memory(alloc)) => {
|
||||||
// Need to duplicate the logic here, because the global allocations have
|
// Need to duplicate the logic here, because the global allocations have
|
||||||
// different associated types than the interpreter-local ones.
|
// different associated types than the interpreter-local ones.
|
||||||
let alloc = alloc.inner();
|
let alloc = alloc.inner();
|
||||||
Ok((alloc.size(), alloc.align))
|
(alloc.size(), alloc.align, AllocKind::LiveData)
|
||||||
}
|
}
|
||||||
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
|
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
|
||||||
// The rest must be dead.
|
// The rest must be dead.
|
||||||
None => {
|
None => {
|
||||||
if let AllocCheck::MaybeDead = liveness {
|
// Deallocated pointers are allowed, we should be able to find
|
||||||
// Deallocated pointers are allowed, we should be able to find
|
// them in the map.
|
||||||
// them in the map.
|
let (size, align) = *self
|
||||||
Ok(*self
|
.memory
|
||||||
.memory
|
.dead_alloc_map
|
||||||
.dead_alloc_map
|
.get(&id)
|
||||||
.get(&id)
|
.expect("deallocated pointers should all be recorded in `dead_alloc_map`");
|
||||||
.expect("deallocated pointers should all be recorded in `dead_alloc_map`"))
|
(size, align, AllocKind::Dead)
|
||||||
} else {
|
|
||||||
throw_ub!(PointerUseAfterFree(id))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Obtain the size and alignment of a live allocation.
|
||||||
|
pub fn get_live_alloc_size_and_align(&self, id: AllocId) -> InterpResult<'tcx, (Size, Align)> {
|
||||||
|
let (size, align, kind) = self.get_alloc_info(id);
|
||||||
|
if matches!(kind, AllocKind::Dead) {
|
||||||
|
throw_ub!(PointerUseAfterFree(id))
|
||||||
|
}
|
||||||
|
Ok((size, align))
|
||||||
|
}
|
||||||
|
|
||||||
fn get_fn_alloc(&self, id: AllocId) -> Option<FnVal<'tcx, M::ExtraFnVal>> {
|
fn get_fn_alloc(&self, id: AllocId) -> Option<FnVal<'tcx, M::ExtraFnVal>> {
|
||||||
if let Some(extra) = self.memory.extra_fn_ptr_map.get(&id) {
|
if let Some(extra) = self.memory.extra_fn_ptr_map.get(&id) {
|
||||||
Some(FnVal::Other(*extra))
|
Some(FnVal::Other(*extra))
|
||||||
@ -1187,9 +1172,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
|||||||
let ptr = self.scalar_to_ptr(scalar)?;
|
let ptr = self.scalar_to_ptr(scalar)?;
|
||||||
match self.ptr_try_get_alloc_id(ptr) {
|
match self.ptr_try_get_alloc_id(ptr) {
|
||||||
Ok((alloc_id, offset, _)) => {
|
Ok((alloc_id, offset, _)) => {
|
||||||
let (size, _align) = self
|
let (size, _align, _kind) = self.get_alloc_info(alloc_id);
|
||||||
.get_alloc_size_and_align(alloc_id, AllocCheck::MaybeDead)
|
|
||||||
.expect("alloc info with MaybeDead cannot fail");
|
|
||||||
// If the pointer is out-of-bounds, it may be null.
|
// If the pointer is out-of-bounds, it may be null.
|
||||||
// Note that one-past-the-end (offset == size) is still inbounds, and never null.
|
// Note that one-past-the-end (offset == size) is still inbounds, and never null.
|
||||||
offset > size
|
offset > size
|
||||||
|
@ -23,7 +23,7 @@ pub use self::eval_context::{
|
|||||||
};
|
};
|
||||||
pub use self::intern::{intern_const_alloc_recursive, InternKind};
|
pub use self::intern::{intern_const_alloc_recursive, InternKind};
|
||||||
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
|
pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump};
|
||||||
pub use self::memory::{AllocCheck, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind};
|
pub use self::memory::{AllocRef, AllocRefMut, FnVal, Memory, MemoryKind};
|
||||||
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
|
pub use self::operand::{ImmTy, Immediate, OpTy, Operand};
|
||||||
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
|
pub use self::place::{MPlaceTy, MemPlace, MemPlaceMeta, Place, PlaceTy};
|
||||||
pub use self::validity::{CtfeValidationMode, RefTracking};
|
pub use self::validity::{CtfeValidationMode, RefTracking};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user