2018-08-18 09:53:15 +00:00
|
|
|
//! The memory subsystem.
|
|
|
|
//!
|
|
|
|
//! Generally, we use `Pointer` to denote memory addresses. However, some operations
|
|
|
|
//! have a "size"-like parameter, and they take `Scalar` for the address because
|
2021-05-02 21:55:22 +00:00
|
|
|
//! if the size is 0, then the pointer can also be a (properly aligned, non-null)
|
2019-02-08 13:53:55 +00:00
|
|
|
//! integer. It is crucial that these operations call `check_align` *before*
|
2018-08-18 09:53:15 +00:00
|
|
|
//! short-circuiting the empty case!
|
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use std::borrow::Cow;
|
2018-05-18 14:06:20 +00:00
|
|
|
use std::collections::VecDeque;
|
2020-08-16 15:36:46 +00:00
|
|
|
use std::convert::{TryFrom, TryInto};
|
2020-04-04 11:34:18 +00:00
|
|
|
use std::fmt;
|
2019-10-22 16:06:30 +00:00
|
|
|
use std::ptr;
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::Mutability;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2020-08-11 09:35:50 +00:00
|
|
|
use rustc_middle::ty::{Instance, ParamEnv, TyCtxt};
|
2020-03-31 16:16:47 +00:00
|
|
|
use rustc_target::abi::{Align, HasDataLayout, Size, TargetDataLayout};
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2018-10-16 12:50:07 +00:00
|
|
|
use super::{
|
2020-08-11 09:35:50 +00:00
|
|
|
AllocId, AllocMap, Allocation, AllocationExtra, CheckInAllocMsg, GlobalAlloc, InterpResult,
|
|
|
|
Machine, MayLeak, Pointer, PointerArithmetic, Scalar,
|
2018-10-16 12:50:07 +00:00
|
|
|
};
|
2020-04-04 11:21:25 +00:00
|
|
|
use crate::util::pretty;
|
2016-04-05 02:33:41 +00:00
|
|
|
|
2019-10-20 04:54:53 +00:00
|
|
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
2017-08-09 12:53:22 +00:00
|
|
|
pub enum MemoryKind<T> {
|
2019-11-28 18:15:32 +00:00
|
|
|
/// Stack memory. Error if deallocated except during a stack pop.
|
2017-07-12 12:51:47 +00:00
|
|
|
Stack,
|
2019-11-28 18:15:32 +00:00
|
|
|
/// Memory backing vtables. Error if ever deallocated.
|
2018-09-21 21:32:59 +00:00
|
|
|
Vtable,
|
2019-11-28 18:15:32 +00:00
|
|
|
/// Memory allocated by `caller_location` intrinsic. Error if ever deallocated.
|
|
|
|
CallerLocation,
|
|
|
|
/// Additional memory kinds a machine wishes to distinguish from the builtin ones.
|
2017-07-28 14:48:43 +00:00
|
|
|
Machine(T),
|
2016-04-05 02:33:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:45:44 +00:00
|
|
|
impl<T: MayLeak> MayLeak for MemoryKind<T> {
|
|
|
|
#[inline]
|
|
|
|
fn may_leak(self) -> bool {
|
|
|
|
match self {
|
|
|
|
MemoryKind::Stack => false,
|
|
|
|
MemoryKind::Vtable => true,
|
2019-11-28 18:15:32 +00:00
|
|
|
MemoryKind::CallerLocation => true,
|
2019-12-22 22:42:04 +00:00
|
|
|
MemoryKind::Machine(k) => k.may_leak(),
|
2018-10-16 10:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 11:34:18 +00:00
|
|
|
impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
MemoryKind::Stack => write!(f, "stack variable"),
|
|
|
|
MemoryKind::Vtable => write!(f, "vtable"),
|
|
|
|
MemoryKind::CallerLocation => write!(f, "caller location"),
|
|
|
|
MemoryKind::Machine(m) => write!(f, "{}", m),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 14:42:51 +00:00
|
|
|
/// Used by `get_size_and_align` to indicate whether the allocation needs to be live.
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum AllocCheck {
|
|
|
|
/// Allocation must be live and not a function pointer.
|
2019-11-22 17:11:28 +00:00
|
|
|
Dereferenceable,
|
2019-06-23 14:42:51 +00:00
|
|
|
/// Allocations needs to be live, but may be a function pointer.
|
|
|
|
Live,
|
|
|
|
/// Allocation may be dead.
|
|
|
|
MaybeDead,
|
|
|
|
}
|
|
|
|
|
2019-06-30 11:51:18 +00:00
|
|
|
/// The value of a function pointer.
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum FnVal<'tcx, Other> {
|
|
|
|
Instance(Instance<'tcx>),
|
|
|
|
Other(Other),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, Other> FnVal<'tcx, Other> {
|
|
|
|
pub fn as_instance(self) -> InterpResult<'tcx, Instance<'tcx>> {
|
|
|
|
match self {
|
2019-12-22 22:42:04 +00:00
|
|
|
FnVal::Instance(instance) => Ok(instance),
|
|
|
|
FnVal::Other(_) => {
|
|
|
|
throw_unsup_format!("'foreign' function pointers are not supported in this context")
|
|
|
|
}
|
2019-06-30 11:51:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 08:12:21 +00:00
|
|
|
// `Memory` has to depend on the `Machine` because some of its operations
|
2018-11-27 02:59:49 +00:00
|
|
|
// (e.g., `get`) call a `Machine` hook.
|
2019-06-11 19:03:44 +00:00
|
|
|
pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Allocations local to this instance of the miri engine. The kind
|
2018-08-23 17:04:33 +00:00
|
|
|
/// helps ensure that the same mechanism is used for allocation and
|
2019-02-08 13:53:55 +00:00
|
|
|
/// deallocation. When an allocation is not found here, it is a
|
2020-03-21 18:19:10 +00:00
|
|
|
/// global and looked up in the `tcx` for read access. Some machines may
|
|
|
|
/// have to mutate this map even on a read-only access to a global (because
|
2018-10-05 13:13:59 +00:00
|
|
|
/// they do pointer provenance tracking and the allocations in `tcx` have
|
|
|
|
/// the wrong type), so we let the machine override this type.
|
2020-03-21 18:19:10 +00:00
|
|
|
/// Either way, if the machine allows writing to a global, doing so will
|
|
|
|
/// create a copy of the global allocation here.
|
2019-06-19 08:03:53 +00:00
|
|
|
// FIXME: this should not be public, but interning currently needs access to it
|
2019-02-10 13:59:13 +00:00
|
|
|
pub(super) alloc_map: M::MemoryMap,
|
2017-02-10 21:35:45 +00:00
|
|
|
|
2019-06-30 11:51:18 +00:00
|
|
|
/// Map for "extra" function pointers.
|
|
|
|
extra_fn_ptr_map: FxHashMap<AllocId, M::ExtraFnVal>,
|
|
|
|
|
2021-05-02 21:55:22 +00:00
|
|
|
/// To be able to compare pointers with null, and to check alignment for accesses
|
2018-09-15 14:34:30 +00:00
|
|
|
/// to ZSTs (where pointers may dangle), we keep track of the size even for allocations
|
|
|
|
/// that do not exist any more.
|
2019-06-30 11:51:18 +00:00
|
|
|
// FIXME: this should not be public, but interning currently needs access to it
|
2019-02-10 13:59:13 +00:00
|
|
|
pub(super) dead_alloc_map: FxHashMap<AllocId, (Size, Align)>,
|
2018-09-15 14:34:30 +00:00
|
|
|
|
2018-11-14 15:00:52 +00:00
|
|
|
/// Extra data added by the machine.
|
|
|
|
pub extra: M::MemoryExtra,
|
|
|
|
|
2018-09-20 08:22:11 +00:00
|
|
|
/// Lets us implement `HasDataLayout`, which is awfully convenient.
|
2020-06-01 06:42:40 +00:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2016-03-24 03:40:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 21:11:55 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> {
|
2018-08-26 18:42:52 +00:00
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 19:22:27 +00:00
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
2020-06-01 06:42:40 +00:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, extra: M::MemoryExtra) -> Self {
|
2016-09-22 13:22:00 +00:00
|
|
|
Memory {
|
2018-11-14 15:00:52 +00:00
|
|
|
alloc_map: M::MemoryMap::default(),
|
2019-06-30 11:51:18 +00:00
|
|
|
extra_fn_ptr_map: FxHashMap::default(),
|
2018-09-15 14:34:30 +00:00
|
|
|
dead_alloc_map: FxHashMap::default(),
|
2019-06-26 18:13:19 +00:00
|
|
|
extra,
|
2017-12-06 08:25:29 +00:00
|
|
|
tcx,
|
2016-09-22 13:22:00 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 18:42:37 +00:00
|
|
|
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
|
2020-07-26 09:11:17 +00:00
|
|
|
/// the machine pointer to the allocation. Must never be used
|
|
|
|
/// for any other pointers, nor for TLS statics.
|
2019-12-01 09:02:41 +00:00
|
|
|
///
|
2020-07-26 09:11:17 +00:00
|
|
|
/// Using the resulting pointer represents a *direct* access to that memory
|
|
|
|
/// (e.g. by directly using a `static`),
|
|
|
|
/// as opposed to access through a pointer that was created by the program.
|
2019-12-01 09:02:41 +00:00
|
|
|
///
|
2020-07-26 09:11:17 +00:00
|
|
|
/// This function can fail only if `ptr` points to an `extern static`.
|
2019-05-28 08:44:46 +00:00
|
|
|
#[inline]
|
2020-07-26 13:45:09 +00:00
|
|
|
pub fn global_base_pointer(
|
|
|
|
&self,
|
|
|
|
mut ptr: Pointer,
|
|
|
|
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
|
2020-07-26 09:11:17 +00:00
|
|
|
// We need to handle `extern static`.
|
|
|
|
let ptr = match self.tcx.get_global_alloc(ptr.alloc_id) {
|
|
|
|
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_thread_local_static(def_id) => {
|
|
|
|
bug!("global memory cannot point to thread-local static")
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_foreign_item(def_id) => {
|
|
|
|
ptr.alloc_id = M::extern_static_alloc_id(self, def_id)?;
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// No need to change the `AllocId`.
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// And we need to get the tag.
|
|
|
|
let tag = M::tag_global_base_pointer(&self.extra, ptr.alloc_id);
|
|
|
|
Ok(ptr.with_tag(tag))
|
2019-05-28 08:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 11:51:18 +00:00
|
|
|
pub fn create_fn_alloc(
|
|
|
|
&mut self,
|
|
|
|
fn_val: FnVal<'tcx, M::ExtraFnVal>,
|
2019-12-22 22:42:04 +00:00
|
|
|
) -> Pointer<M::PointerTag> {
|
2019-06-30 11:51:18 +00:00
|
|
|
let id = match fn_val {
|
2020-04-24 10:53:18 +00:00
|
|
|
FnVal::Instance(instance) => self.tcx.create_fn_alloc(instance),
|
2019-06-30 11:51:18 +00:00
|
|
|
FnVal::Other(extra) => {
|
2019-06-30 13:35:39 +00:00
|
|
|
// FIXME(RalfJung): Should we have a cache here?
|
2020-04-24 10:53:18 +00:00
|
|
|
let id = self.tcx.reserve_alloc_id();
|
2019-06-30 11:51:18 +00:00
|
|
|
let old = self.extra_fn_ptr_map.insert(id, extra);
|
|
|
|
assert!(old.is_none());
|
|
|
|
id
|
|
|
|
}
|
|
|
|
};
|
2020-07-26 09:11:17 +00:00
|
|
|
// Functions are global allocations, so make sure we get the right base pointer.
|
2020-07-26 13:45:09 +00:00
|
|
|
// We know this is not an `extern static` so this cannot fail.
|
2020-07-26 09:11:17 +00:00
|
|
|
self.global_base_pointer(Pointer::from(id)).unwrap()
|
2016-06-08 11:43:34 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 08:44:46 +00:00
|
|
|
pub fn allocate(
|
|
|
|
&mut self,
|
|
|
|
size: Size,
|
|
|
|
align: Align,
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: MemoryKind<M::MemoryKind>,
|
2019-05-28 08:44:46 +00:00
|
|
|
) -> Pointer<M::PointerTag> {
|
2020-07-22 15:08:59 +00:00
|
|
|
let alloc = Allocation::uninit(size, align);
|
2019-05-28 08:44:46 +00:00
|
|
|
self.allocate_with(alloc, kind)
|
2017-02-10 21:35:33 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 18:19:10 +00:00
|
|
|
pub fn allocate_bytes(
|
2017-07-28 14:48:43 +00:00
|
|
|
&mut self,
|
2019-05-28 08:44:46 +00:00
|
|
|
bytes: &[u8],
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: MemoryKind<M::MemoryKind>,
|
2019-05-28 08:44:46 +00:00
|
|
|
) -> Pointer<M::PointerTag> {
|
|
|
|
let alloc = Allocation::from_byte_aligned_bytes(bytes);
|
|
|
|
self.allocate_with(alloc, kind)
|
2018-04-26 07:18:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 08:44:46 +00:00
|
|
|
pub fn allocate_with(
|
2018-04-26 07:18:19 +00:00
|
|
|
&mut self,
|
2019-05-28 08:44:46 +00:00
|
|
|
alloc: Allocation,
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: MemoryKind<M::MemoryKind>,
|
2019-04-15 08:05:13 +00:00
|
|
|
) -> Pointer<M::PointerTag> {
|
2020-04-24 10:53:18 +00:00
|
|
|
let id = self.tcx.reserve_alloc_id();
|
2019-12-22 22:42:04 +00:00
|
|
|
debug_assert_ne!(
|
|
|
|
Some(kind),
|
2020-03-21 18:19:10 +00:00
|
|
|
M::GLOBAL_KIND.map(MemoryKind::Machine),
|
|
|
|
"dynamically allocating global memory"
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2020-07-26 09:11:17 +00:00
|
|
|
// This is a new allocation, not a new global one, so no `global_base_ptr`.
|
2019-12-01 09:02:41 +00:00
|
|
|
let (alloc, tag) = M::init_allocation_extra(&self.extra, id, Cow::Owned(alloc), Some(kind));
|
2019-05-28 08:44:46 +00:00
|
|
|
self.alloc_map.insert(id, (kind, alloc.into_owned()));
|
2019-12-01 09:02:41 +00:00
|
|
|
Pointer::from(id).with_tag(tag)
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 14:48:43 +00:00
|
|
|
pub fn reallocate(
|
|
|
|
&mut self,
|
2018-09-21 21:32:59 +00:00
|
|
|
ptr: Pointer<M::PointerTag>,
|
2019-07-01 21:48:58 +00:00
|
|
|
old_size_and_align: Option<(Size, Align)>,
|
2018-05-19 14:37:29 +00:00
|
|
|
new_size: Size,
|
2018-09-08 22:16:45 +00:00
|
|
|
new_align: Align,
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: MemoryKind<M::MemoryKind>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2020-03-08 18:44:09 +00:00
|
|
|
throw_ub_format!(
|
|
|
|
"reallocating {:?} which does not point to the beginning of an object",
|
|
|
|
ptr
|
|
|
|
);
|
2016-04-06 23:29:56 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:15:42 +00:00
|
|
|
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
|
2018-10-22 17:17:37 +00:00
|
|
|
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
|
2018-12-19 13:11:01 +00:00
|
|
|
let new_ptr = self.allocate(new_size, new_align, kind);
|
2019-07-01 21:48:58 +00:00
|
|
|
let old_size = match old_size_and_align {
|
|
|
|
Some((size, _align)) => size,
|
2021-05-17 11:30:16 +00:00
|
|
|
None => self.get_raw(ptr.alloc_id)?.size(),
|
2019-07-01 21:48:58 +00:00
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
self.copy(ptr, new_ptr, old_size.min(new_size), /*nonoverlapping*/ true)?;
|
2019-07-01 21:48:58 +00:00
|
|
|
self.deallocate(ptr, old_size_and_align, kind)?;
|
2016-06-13 09:24:01 +00:00
|
|
|
|
2017-07-10 20:34:54 +00:00
|
|
|
Ok(new_ptr)
|
2016-04-06 23:29:56 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 18:19:10 +00:00
|
|
|
/// Deallocate a local, or do nothing if that local has been made into a global.
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn deallocate_local(&mut self, ptr: Pointer<M::PointerTag>) -> InterpResult<'tcx> {
|
2020-03-21 18:19:10 +00:00
|
|
|
// The allocation might be already removed by global interning.
|
2018-08-23 17:04:33 +00:00
|
|
|
// This can only really happen in the CTFE instance, not in miri.
|
|
|
|
if self.alloc_map.contains_key(&ptr.alloc_id) {
|
|
|
|
self.deallocate(ptr, None, MemoryKind::Stack)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2017-12-06 08:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 14:48:43 +00:00
|
|
|
pub fn deallocate(
|
|
|
|
&mut self,
|
2018-09-21 21:32:59 +00:00
|
|
|
ptr: Pointer<M::PointerTag>,
|
2019-07-01 21:48:58 +00:00
|
|
|
old_size_and_align: Option<(Size, Align)>,
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: MemoryKind<M::MemoryKind>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-10-17 10:36:18 +00:00
|
|
|
trace!("deallocating: {}", ptr.alloc_id);
|
2018-09-15 14:34:30 +00:00
|
|
|
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2020-03-08 18:44:09 +00:00
|
|
|
throw_ub_format!(
|
|
|
|
"deallocating {:?} which does not point to the beginning of an object",
|
|
|
|
ptr
|
|
|
|
);
|
2016-11-17 13:48:34 +00:00
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
|
2020-04-09 18:48:06 +00:00
|
|
|
M::before_deallocation(&mut self.extra, ptr.alloc_id)?;
|
|
|
|
|
2018-10-16 12:50:07 +00:00
|
|
|
let (alloc_kind, mut alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
|
2017-12-06 08:25:29 +00:00
|
|
|
Some(alloc) => alloc,
|
2018-06-08 02:47:26 +00:00
|
|
|
None => {
|
2020-03-21 18:19:10 +00:00
|
|
|
// Deallocating global memory -- always an error
|
2020-04-24 10:53:18 +00:00
|
|
|
return Err(match self.tcx.get_global_alloc(ptr.alloc_id) {
|
2020-09-22 07:04:30 +00:00
|
|
|
Some(GlobalAlloc::Function(..)) => {
|
|
|
|
err_ub_format!("deallocating {}, which is a function", ptr.alloc_id)
|
|
|
|
}
|
2020-04-17 00:38:52 +00:00
|
|
|
Some(GlobalAlloc::Static(..) | GlobalAlloc::Memory(..)) => {
|
2020-09-22 07:04:30 +00:00
|
|
|
err_ub_format!("deallocating {}, which is static memory", ptr.alloc_id)
|
2020-03-08 18:44:09 +00:00
|
|
|
}
|
2020-03-08 17:52:30 +00:00
|
|
|
None => err_ub!(PointerUseAfterFree(ptr.alloc_id)),
|
2018-05-02 04:03:06 +00:00
|
|
|
}
|
2019-07-31 07:18:54 +00:00
|
|
|
.into());
|
2018-05-02 04:03:06 +00:00
|
|
|
}
|
2017-08-08 11:06:14 +00:00
|
|
|
};
|
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
if alloc_kind != kind {
|
2020-03-08 18:44:09 +00:00
|
|
|
throw_ub_format!(
|
2020-09-22 07:04:30 +00:00
|
|
|
"deallocating {}, which is {} memory, using {} deallocation operation",
|
|
|
|
ptr.alloc_id,
|
2020-03-08 18:44:09 +00:00
|
|
|
alloc_kind,
|
|
|
|
kind
|
|
|
|
);
|
2017-07-12 12:51:47 +00:00
|
|
|
}
|
2019-07-01 21:48:58 +00:00
|
|
|
if let Some((size, align)) = old_size_and_align {
|
2021-05-17 11:30:16 +00:00
|
|
|
if size != alloc.size() || align != alloc.align {
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub_format!(
|
2020-09-22 07:04:30 +00:00
|
|
|
"incorrect layout on deallocation: {} has size {} and alignment {}, but gave size {} and alignment {}",
|
|
|
|
ptr.alloc_id,
|
2021-05-17 11:30:16 +00:00
|
|
|
alloc.size().bytes(),
|
2020-03-08 18:44:09 +00:00
|
|
|
alloc.align.bytes(),
|
|
|
|
size.bytes(),
|
|
|
|
align.bytes(),
|
2020-03-08 17:52:30 +00:00
|
|
|
)
|
2017-07-03 23:47:58 +00:00
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
}
|
2017-07-03 23:47:58 +00:00
|
|
|
|
2018-10-16 12:50:07 +00:00
|
|
|
// Let the machine take some extra action
|
2021-05-17 11:30:16 +00:00
|
|
|
let size = alloc.size();
|
2018-11-12 07:39:13 +00:00
|
|
|
AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;
|
2018-10-16 12:50:07 +00:00
|
|
|
|
2018-09-15 14:34:30 +00:00
|
|
|
// Don't forget to remember size and align of this now-dead allocation
|
2021-05-17 11:30:16 +00:00
|
|
|
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
|
2018-09-15 14:34:30 +00:00
|
|
|
if old.is_some() {
|
|
|
|
bug!("Nothing can be deallocated twice");
|
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-23 07:59:16 +00:00
|
|
|
|
2019-06-23 12:26:36 +00:00
|
|
|
/// Check if the given scalar is allowed to do a memory access of given `size`
|
2019-06-23 16:00:07 +00:00
|
|
|
/// and `align`. On success, returns `None` for zero-sized accesses (where
|
2019-06-23 12:26:36 +00:00
|
|
|
/// nothing else is left to do) and a `Pointer` to use for the actual access otherwise.
|
|
|
|
/// Crucially, if the input is a `Pointer`, we will test it for liveness
|
2019-08-17 19:33:44 +00:00
|
|
|
/// *even if* the size is 0.
|
2019-06-23 12:26:36 +00:00
|
|
|
///
|
|
|
|
/// Everyone accessing memory based on a `Scalar` should use this method to get the
|
2019-06-23 16:00:07 +00:00
|
|
|
/// `Pointer` they need. And even if you already have a `Pointer`, call this method
|
2019-06-23 12:26:36 +00:00
|
|
|
/// to make sure it is sufficiently aligned and not dangling. Not doing that may
|
|
|
|
/// cause ICEs.
|
2019-07-06 10:46:08 +00:00
|
|
|
///
|
|
|
|
/// Most of the time you should use `check_mplace_access`, but when you just have a pointer,
|
|
|
|
/// this method is still appropriate.
|
2019-07-29 13:27:01 +00:00
|
|
|
#[inline(always)]
|
2019-06-23 12:26:36 +00:00
|
|
|
pub fn check_ptr_access(
|
|
|
|
&self,
|
|
|
|
sptr: Scalar<M::PointerTag>,
|
|
|
|
size: Size,
|
|
|
|
align: Align,
|
2019-07-29 13:27:01 +00:00
|
|
|
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
|
2020-04-13 15:59:12 +00:00
|
|
|
let align = M::enforce_alignment(&self.extra).then_some(align);
|
2019-11-04 11:28:13 +00:00
|
|
|
self.check_ptr_access_align(sptr, size, align, CheckInAllocMsg::MemoryAccessTest)
|
2019-07-29 13:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Like `check_ptr_access`, but *definitely* checks alignment when `align`
|
2020-04-13 15:59:12 +00:00
|
|
|
/// is `Some` (overriding `M::enforce_alignment`). Also lets the caller control
|
2019-11-04 11:28:13 +00:00
|
|
|
/// the error message for the out-of-bounds case.
|
|
|
|
pub fn check_ptr_access_align(
|
2019-07-29 13:27:01 +00:00
|
|
|
&self,
|
|
|
|
sptr: Scalar<M::PointerTag>,
|
|
|
|
size: Size,
|
|
|
|
align: Option<Align>,
|
2019-11-04 11:28:13 +00:00
|
|
|
msg: CheckInAllocMsg,
|
2019-06-23 12:26:36 +00:00
|
|
|
) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
|
2019-06-23 18:08:55 +00:00
|
|
|
fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
|
|
|
|
if offset % align.bytes() == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
// The biggest power of two through which `offset` is divisible.
|
|
|
|
let offset_pow2 = 1 << offset.trailing_zeros();
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(AlignmentCheckFailed {
|
2019-06-23 18:08:55 +00:00
|
|
|
has: Align::from_bytes(offset_pow2).unwrap(),
|
|
|
|
required: align,
|
2019-07-29 07:36:42 +00:00
|
|
|
})
|
2019-06-23 18:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 12:26:36 +00:00
|
|
|
// Normalize to a `Pointer` if we definitely need one.
|
|
|
|
let normalized = if size.bytes() == 0 {
|
2019-06-23 18:18:30 +00:00
|
|
|
// Can be an integer, just take what we got. We do NOT `force_bits` here;
|
|
|
|
// if this is already a `Pointer` we want to do the bounds checks!
|
2019-06-23 12:26:36 +00:00
|
|
|
sptr
|
|
|
|
} else {
|
2020-08-16 15:36:46 +00:00
|
|
|
// A "real" access, we must get a pointer to be able to check the bounds.
|
2019-11-29 10:09:26 +00:00
|
|
|
Scalar::from(self.force_ptr(sptr)?)
|
2019-06-23 12:26:36 +00:00
|
|
|
};
|
|
|
|
Ok(match normalized.to_bits_or_ptr(self.pointer_size(), self) {
|
|
|
|
Ok(bits) => {
|
2020-03-21 12:49:02 +00:00
|
|
|
let bits = u64::try_from(bits).unwrap(); // it's ptr-sized
|
2019-06-23 12:26:36 +00:00
|
|
|
assert!(size.bytes() == 0);
|
2021-05-02 21:55:22 +00:00
|
|
|
// Must be non-null.
|
2019-06-23 12:26:36 +00:00
|
|
|
if bits == 0 {
|
2020-04-30 18:37:58 +00:00
|
|
|
throw_ub!(DanglingIntPointer(0, msg))
|
2019-06-23 12:26:36 +00:00
|
|
|
}
|
2019-07-28 11:44:11 +00:00
|
|
|
// Must be aligned.
|
2019-07-29 13:27:01 +00:00
|
|
|
if let Some(align) = align {
|
2019-07-28 11:44:11 +00:00
|
|
|
check_offset_align(bits, align)?;
|
|
|
|
}
|
2019-06-23 12:26:36 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
Err(ptr) => {
|
2019-06-23 14:42:51 +00:00
|
|
|
let (allocation_size, alloc_align) =
|
2019-11-22 17:11:28 +00:00
|
|
|
self.get_size_and_align(ptr.alloc_id, AllocCheck::Dereferenceable)?;
|
2021-05-02 21:55:22 +00:00
|
|
|
// Test bounds. This also ensures non-null.
|
2019-06-23 14:42:51 +00:00
|
|
|
// It is sufficient to check this for the end pointer. The addition
|
|
|
|
// checks for overflow.
|
|
|
|
let end_ptr = ptr.offset(size, self)?;
|
2020-03-08 18:44:09 +00:00
|
|
|
if end_ptr.offset > allocation_size {
|
|
|
|
// equal is okay!
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(PointerOutOfBounds { ptr: end_ptr.erase_tag(), msg, allocation_size })
|
|
|
|
}
|
2019-06-23 16:00:07 +00:00
|
|
|
// Test align. Check this last; if both bounds and alignment are violated
|
2019-06-23 14:42:51 +00:00
|
|
|
// we want the error to be about the bounds.
|
2019-07-29 13:27:01 +00:00
|
|
|
if let Some(align) = align {
|
2020-08-16 15:36:46 +00:00
|
|
|
if M::force_int_for_alignment_check(&self.extra) {
|
|
|
|
let bits = self
|
|
|
|
.force_bits(ptr.into(), self.pointer_size())
|
|
|
|
.expect("ptr-to-int cast for align check should never fail");
|
|
|
|
check_offset_align(bits.try_into().unwrap(), align)?;
|
|
|
|
} else {
|
|
|
|
// Check allocation alignment and offset alignment.
|
|
|
|
if alloc_align.bytes() < align.bytes() {
|
|
|
|
throw_ub!(AlignmentCheckFailed { has: alloc_align, required: align });
|
|
|
|
}
|
|
|
|
check_offset_align(ptr.offset.bytes(), align)?;
|
2019-07-28 11:44:11 +00:00
|
|
|
}
|
2019-06-23 14:42:51 +00:00
|
|
|
}
|
2019-06-23 12:26:36 +00:00
|
|
|
|
|
|
|
// We can still be zero-sized in this branch, in which case we have to
|
|
|
|
// return `None`.
|
2020-05-01 13:52:08 +00:00
|
|
|
if size.bytes() == 0 { None } else { Some(ptr) }
|
2019-06-23 12:26:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:55:22 +00:00
|
|
|
/// Test if the pointer might be null.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn ptr_may_be_null(&self, ptr: Pointer<M::PointerTag>) -> bool {
|
|
|
|
let (size, _align) = self
|
|
|
|
.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)
|
2019-06-23 14:42:51 +00:00
|
|
|
.expect("alloc info with MaybeDead cannot fail");
|
2020-03-09 20:25:45 +00:00
|
|
|
// 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.
|
|
|
|
ptr.offset > size
|
2019-06-23 12:26:36 +00:00
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
|
2016-06-23 07:40:01 +00:00
|
|
|
/// Allocation accessors
|
2019-06-11 19:03:44 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
2020-03-21 18:19:10 +00:00
|
|
|
/// Helper function to obtain a global (tcx) allocation.
|
2018-10-05 14:49:51 +00:00
|
|
|
/// This attempts to return a reference to an existing allocation if
|
|
|
|
/// one can be found in `tcx`. That, however, is only possible if `tcx` and
|
|
|
|
/// this machine use the same pointer tag, so it is indirected through
|
2019-05-28 08:44:46 +00:00
|
|
|
/// `M::tag_allocation`.
|
2020-03-21 18:19:10 +00:00
|
|
|
fn get_global_alloc(
|
2019-07-02 10:31:17 +00:00
|
|
|
memory_extra: &M::MemoryExtra,
|
2020-06-01 06:42:40 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-07-02 10:31:17 +00:00
|
|
|
id: AllocId,
|
2020-03-21 18:19:10 +00:00
|
|
|
is_write: bool,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
|
2020-04-24 10:53:18 +00:00
|
|
|
let (alloc, def_id) = match tcx.get_global_alloc(id) {
|
2020-03-22 08:23:19 +00:00
|
|
|
Some(GlobalAlloc::Memory(mem)) => {
|
|
|
|
// Memory of a constant or promoted or anonymous memory referenced by a static.
|
|
|
|
(mem, None)
|
|
|
|
}
|
2020-03-08 17:52:30 +00:00
|
|
|
Some(GlobalAlloc::Function(..)) => throw_ub!(DerefFunctionPointer(id)),
|
|
|
|
None => throw_ub!(PointerUseAfterFree(id)),
|
2019-05-28 08:44:46 +00:00
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
2020-07-26 09:11:17 +00:00
|
|
|
assert!(tcx.is_static(def_id));
|
2020-05-02 19:44:25 +00:00
|
|
|
assert!(!tcx.is_thread_local_static(def_id));
|
2020-03-21 18:19:10 +00:00
|
|
|
// Notice that every static has two `AllocId` that will resolve to the same
|
|
|
|
// thing here: one maps to `GlobalAlloc::Static`, this is the "lazy" ID,
|
|
|
|
// and the other one is maps to `GlobalAlloc::Memory`, this is returned by
|
2020-09-07 15:30:38 +00:00
|
|
|
// `eval_static_initializer` and it is the "resolved" ID.
|
2020-04-29 12:56:40 +00:00
|
|
|
// The resolved ID is never used by the interpreted program, it is hidden.
|
|
|
|
// This is relied upon for soundness of const-patterns; a pointer to the resolved
|
|
|
|
// ID would "sidestep" the checks that make sure consts do not point to statics!
|
2020-03-21 18:19:10 +00:00
|
|
|
// The `GlobalAlloc::Memory` branch here is still reachable though; when a static
|
|
|
|
// contains a reference to memory that was created during its evaluation (i.e., not
|
|
|
|
// to another static), those inner references only exist in "resolved" form.
|
2019-05-28 08:44:46 +00:00
|
|
|
if tcx.is_foreign_item(def_id) {
|
2020-07-26 09:11:17 +00:00
|
|
|
throw_unsup!(ReadExternStatic(def_id));
|
2019-05-28 08:44:46 +00:00
|
|
|
}
|
2020-07-26 10:02:03 +00:00
|
|
|
|
2020-08-11 09:35:50 +00:00
|
|
|
(tcx.eval_static_initializer(def_id)?, Some(def_id))
|
2018-08-31 08:39:47 +00:00
|
|
|
}
|
2019-05-28 08:44:46 +00:00
|
|
|
};
|
2020-03-21 19:44:39 +00:00
|
|
|
M::before_access_global(memory_extra, id, alloc, def_id, is_write)?;
|
2020-03-21 18:19:10 +00:00
|
|
|
let alloc = Cow::Borrowed(alloc);
|
2019-11-29 18:42:37 +00:00
|
|
|
// We got tcx memory. Let the machine initialize its "extra" stuff.
|
2019-12-01 09:02:41 +00:00
|
|
|
let (alloc, tag) = M::init_allocation_extra(
|
2019-07-02 10:31:17 +00:00
|
|
|
memory_extra,
|
2019-05-28 08:44:46 +00:00
|
|
|
id, // always use the ID we got as input, not the "hidden" one.
|
|
|
|
alloc,
|
2020-03-21 18:19:10 +00:00
|
|
|
M::GLOBAL_KIND.map(MemoryKind::Machine),
|
2019-12-01 09:02:41 +00:00
|
|
|
);
|
2020-07-26 09:11:17 +00:00
|
|
|
// Sanity check that this is the same pointer we would have gotten via `global_base_pointer`.
|
2020-03-21 18:19:10 +00:00
|
|
|
debug_assert_eq!(tag, M::tag_global_base_pointer(memory_extra, id));
|
2019-12-01 09:02:41 +00:00
|
|
|
Ok(alloc)
|
2018-08-27 11:34:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 16:46:11 +00:00
|
|
|
/// Gives raw access to the `Allocation`, without bounds or alignment checks.
|
2020-03-25 01:34:36 +00:00
|
|
|
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCx` instead!
|
2019-11-02 16:46:11 +00:00
|
|
|
pub fn get_raw(
|
2019-06-07 16:56:27 +00:00
|
|
|
&self,
|
|
|
|
id: AllocId,
|
|
|
|
) -> InterpResult<'tcx, &Allocation<M::PointerTag, M::AllocExtra>> {
|
2018-09-21 21:32:59 +00:00
|
|
|
// The error type of the inner closure here is somewhat funny. We have two
|
|
|
|
// ways of "erroring": An actual error, or because we got a reference from
|
2020-03-21 18:19:10 +00:00
|
|
|
// `get_global_alloc` that we can actually use directly without inserting anything anywhere.
|
2019-06-07 16:56:27 +00:00
|
|
|
// So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
|
2018-09-21 21:32:59 +00:00
|
|
|
let a = self.alloc_map.get_or(id, || {
|
2020-03-21 18:19:10 +00:00
|
|
|
let alloc = Self::get_global_alloc(&self.extra, self.tcx, id, /*is_write*/ false)
|
|
|
|
.map_err(Err)?;
|
2018-09-21 21:32:59 +00:00
|
|
|
match alloc {
|
|
|
|
Cow::Borrowed(alloc) => {
|
2018-10-05 14:49:51 +00:00
|
|
|
// We got a ref, cheaply return that as an "error" so that the
|
|
|
|
// map does not get mutated.
|
2018-09-21 21:32:59 +00:00
|
|
|
Err(Ok(alloc))
|
|
|
|
}
|
|
|
|
Cow::Owned(alloc) => {
|
|
|
|
// Need to put it into the map and return a ref to that
|
2020-03-21 18:19:10 +00:00
|
|
|
let kind = M::GLOBAL_KIND.expect(
|
|
|
|
"I got a global allocation that I have to copy but the machine does \
|
2019-12-22 22:42:04 +00:00
|
|
|
not expect that to happen",
|
2018-09-21 21:32:59 +00:00
|
|
|
);
|
|
|
|
Ok((MemoryKind::Machine(kind), alloc))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// Now unpack that funny error type
|
|
|
|
match a {
|
|
|
|
Ok(a) => Ok(&a.1),
|
2019-12-22 22:42:04 +00:00
|
|
|
Err(a) => a,
|
2016-06-13 09:39:15 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
2017-08-10 15:48:38 +00:00
|
|
|
|
2019-11-02 16:46:11 +00:00
|
|
|
/// Gives raw mutable access to the `Allocation`, without bounds or alignment checks.
|
2020-03-25 01:34:36 +00:00
|
|
|
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCx` instead!
|
2019-11-02 16:46:11 +00:00
|
|
|
pub fn get_raw_mut(
|
2017-08-10 15:48:38 +00:00
|
|
|
&mut self,
|
|
|
|
id: AllocId,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
|
2018-10-05 13:13:59 +00:00
|
|
|
let tcx = self.tcx;
|
2019-07-02 10:31:17 +00:00
|
|
|
let memory_extra = &self.extra;
|
2018-10-05 13:13:59 +00:00
|
|
|
let a = self.alloc_map.get_mut_or(id, || {
|
2020-03-21 18:19:10 +00:00
|
|
|
// Need to make a copy, even if `get_global_alloc` is able
|
2018-10-05 13:13:59 +00:00
|
|
|
// to give us a cheap reference.
|
2020-03-21 18:19:10 +00:00
|
|
|
let alloc = Self::get_global_alloc(memory_extra, tcx, id, /*is_write*/ true)?;
|
2019-12-16 16:28:40 +00:00
|
|
|
if alloc.mutability == Mutability::Not {
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(WriteToReadOnly(id))
|
2018-08-26 10:59:59 +00:00
|
|
|
}
|
2020-03-21 18:19:10 +00:00
|
|
|
let kind = M::GLOBAL_KIND.expect(
|
|
|
|
"I got a global allocation that I have to copy but the machine does \
|
|
|
|
not expect that to happen",
|
|
|
|
);
|
|
|
|
Ok((MemoryKind::Machine(kind), alloc.into_owned()))
|
2018-10-05 13:13:59 +00:00
|
|
|
});
|
|
|
|
// Unpack the error type manually because type inference doesn't
|
|
|
|
// work otherwise (and we cannot help it because `impl Trait`)
|
|
|
|
match a {
|
|
|
|
Err(e) => Err(e),
|
|
|
|
Ok(a) => {
|
|
|
|
let a = &mut a.1;
|
2019-12-16 16:28:40 +00:00
|
|
|
if a.mutability == Mutability::Not {
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(WriteToReadOnly(id))
|
2018-09-21 21:32:59 +00:00
|
|
|
}
|
2018-10-05 13:13:59 +00:00
|
|
|
Ok(a)
|
2018-09-21 21:32:59 +00:00
|
|
|
}
|
2018-10-05 13:13:59 +00:00
|
|
|
}
|
2017-07-14 03:33:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 14:42:51 +00:00
|
|
|
/// Obtain the size and alignment of an allocation, even if that allocation has
|
|
|
|
/// been deallocated.
|
2018-12-20 14:14:25 +00:00
|
|
|
///
|
2019-06-23 14:42:51 +00:00
|
|
|
/// If `liveness` is `AllocCheck::MaybeDead`, this function always returns `Ok`.
|
2018-12-20 14:14:25 +00:00
|
|
|
pub fn get_size_and_align(
|
|
|
|
&self,
|
|
|
|
id: AllocId,
|
2019-06-23 14:42:51 +00:00
|
|
|
liveness: AllocCheck,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'static, (Size, Align)> {
|
2019-07-28 11:02:01 +00:00
|
|
|
// # Regular allocations
|
2019-11-02 16:46:11 +00:00
|
|
|
// Don't use `self.get_raw` here as that will
|
2019-07-26 08:34:54 +00:00
|
|
|
// a) cause cycles in case `id` refers to a static
|
2020-03-21 18:19:10 +00:00
|
|
|
// b) duplicate a global's allocation in miri
|
2019-07-28 10:58:39 +00:00
|
|
|
if let Some((_, alloc)) = self.alloc_map.get(id) {
|
2021-05-17 11:30:16 +00:00
|
|
|
return Ok((alloc.size(), alloc.align));
|
2019-07-28 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-28 11:02:01 +00:00
|
|
|
// # Function pointers
|
|
|
|
// (both global from `alloc_map` and local from `extra_fn_ptr_map`)
|
2020-03-04 14:53:14 +00:00
|
|
|
if self.get_fn_alloc(id).is_some() {
|
2019-11-22 17:11:28 +00:00
|
|
|
return if let AllocCheck::Dereferenceable = liveness {
|
2019-07-28 11:02:01 +00:00
|
|
|
// The caller requested no function pointers.
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(DerefFunctionPointer(id))
|
2019-07-28 11:02:01 +00:00
|
|
|
} else {
|
|
|
|
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// # Statics
|
2019-07-28 10:58:39 +00:00
|
|
|
// Can't do this in the match argument, we may get cycle errors since the lock would
|
|
|
|
// be held throughout the match.
|
2020-04-24 10:53:18 +00:00
|
|
|
match self.tcx.get_global_alloc(id) {
|
2019-07-28 10:58:39 +00:00
|
|
|
Some(GlobalAlloc::Static(did)) => {
|
2020-05-02 19:44:25 +00:00
|
|
|
assert!(!self.tcx.is_thread_local_static(did));
|
2019-07-28 10:58:39 +00:00
|
|
|
// Use size and align of the type.
|
|
|
|
let ty = self.tcx.type_of(did);
|
|
|
|
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
|
|
|
|
Ok((layout.size, layout.align.abi))
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2020-01-03 12:33:28 +00:00
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
2020-01-03 12:31:56 +00:00
|
|
|
// Need to duplicate the logic here, because the global allocations have
|
|
|
|
// different associated types than the interpreter-local ones.
|
2021-05-17 11:30:16 +00:00
|
|
|
Ok((alloc.size(), alloc.align))
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
|
2019-07-28 10:58:39 +00:00
|
|
|
// The rest must be dead.
|
2019-12-22 22:42:04 +00:00
|
|
|
None => {
|
|
|
|
if let AllocCheck::MaybeDead = liveness {
|
|
|
|
// Deallocated pointers are allowed, we should be able to find
|
|
|
|
// them in the map.
|
2020-03-08 17:52:30 +00:00
|
|
|
Ok(*self
|
|
|
|
.dead_alloc_map
|
|
|
|
.get(&id)
|
|
|
|
.expect("deallocated pointers should all be recorded in `dead_alloc_map`"))
|
2019-12-22 22:42:04 +00:00
|
|
|
} else {
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(PointerUseAfterFree(id))
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-05 14:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:59:21 +00:00
|
|
|
fn get_fn_alloc(&self, id: AllocId) -> Option<FnVal<'tcx, M::ExtraFnVal>> {
|
2019-06-30 11:51:18 +00:00
|
|
|
trace!("reading fn ptr: {}", id);
|
|
|
|
if let Some(extra) = self.extra_fn_ptr_map.get(&id) {
|
2020-02-19 12:59:21 +00:00
|
|
|
Some(FnVal::Other(*extra))
|
2019-06-30 11:51:18 +00:00
|
|
|
} else {
|
2020-04-24 10:53:18 +00:00
|
|
|
match self.tcx.get_global_alloc(id) {
|
2020-02-19 12:59:21 +00:00
|
|
|
Some(GlobalAlloc::Function(instance)) => Some(FnVal::Instance(instance)),
|
|
|
|
_ => None,
|
2018-10-05 14:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 11:51:18 +00:00
|
|
|
pub fn get_fn(
|
|
|
|
&self,
|
2019-07-01 09:16:18 +00:00
|
|
|
ptr: Scalar<M::PointerTag>,
|
2019-06-30 11:51:18 +00:00
|
|
|
) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
|
2019-07-01 09:16:18 +00:00
|
|
|
let ptr = self.force_ptr(ptr)?; // We definitely need a pointer value.
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2020-03-08 17:52:30 +00:00
|
|
|
throw_ub!(InvalidFunctionPointer(ptr.erase_tag()))
|
2017-06-22 04:45:51 +00:00
|
|
|
}
|
2020-07-26 13:45:09 +00:00
|
|
|
self.get_fn_alloc(ptr.alloc_id)
|
|
|
|
.ok_or_else(|| err_ub!(InvalidFunctionPointer(ptr.erase_tag())).into())
|
2016-06-08 11:43:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn mark_immutable(&mut self, id: AllocId) -> InterpResult<'tcx> {
|
2019-12-16 16:28:40 +00:00
|
|
|
self.get_raw_mut(id)?.mutability = Mutability::Not;
|
2018-09-21 21:32:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-28 14:15:40 +00:00
|
|
|
/// Create a lazy debug printer that prints the given allocation and all allocations it points
|
|
|
|
/// to, recursively.
|
|
|
|
#[must_use]
|
|
|
|
pub fn dump_alloc<'a>(&'a self, id: AllocId) -> DumpAllocs<'a, 'mir, 'tcx, M> {
|
|
|
|
self.dump_allocs(vec![id])
|
2016-12-08 06:00:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 14:15:40 +00:00
|
|
|
/// Create a lazy debug printer for a list of allocations and all allocations they point to,
|
|
|
|
/// recursively.
|
|
|
|
#[must_use]
|
|
|
|
pub fn dump_allocs<'a>(&'a self, mut allocs: Vec<AllocId>) -> DumpAllocs<'a, 'mir, 'tcx, M> {
|
2016-12-08 06:00:46 +00:00
|
|
|
allocs.sort();
|
|
|
|
allocs.dedup();
|
2020-07-28 14:15:40 +00:00
|
|
|
DumpAllocs { mem: self, allocs }
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
2017-02-14 14:35:13 +00:00
|
|
|
|
2020-07-23 13:49:39 +00:00
|
|
|
/// Print leaked memory. Allocations reachable from `static_roots` or a `Global` allocation
|
|
|
|
/// are not considered leaked. Leaks whose kind `may_leak()` returns true are not reported.
|
|
|
|
pub fn leak_report(&self, static_roots: &[AllocId]) -> usize {
|
2020-04-04 10:03:06 +00:00
|
|
|
// Collect the set of allocations that are *reachable* from `Global` allocations.
|
|
|
|
let reachable = {
|
|
|
|
let mut reachable = FxHashSet::default();
|
|
|
|
let global_kind = M::GLOBAL_KIND.map(MemoryKind::Machine);
|
|
|
|
let mut todo: Vec<_> = self.alloc_map.filter_map_collect(move |&id, &(kind, _)| {
|
|
|
|
if Some(kind) == global_kind { Some(id) } else { None }
|
|
|
|
});
|
2020-07-23 13:49:39 +00:00
|
|
|
todo.extend(static_roots);
|
2020-04-04 10:03:06 +00:00
|
|
|
while let Some(id) = todo.pop() {
|
|
|
|
if reachable.insert(id) {
|
2020-04-04 10:19:10 +00:00
|
|
|
// This is a new allocation, add its relocations to `todo`.
|
2020-04-04 10:03:06 +00:00
|
|
|
if let Some((_, alloc)) = self.alloc_map.get(id) {
|
|
|
|
todo.extend(alloc.relocations().values().map(|&(_, target_id)| target_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reachable
|
|
|
|
};
|
|
|
|
|
|
|
|
// All allocations that are *not* `reachable` and *not* `may_leak` are considered leaking.
|
|
|
|
let leaks: Vec<_> = self.alloc_map.filter_map_collect(|&id, &(kind, _)| {
|
|
|
|
if kind.may_leak() || reachable.contains(&id) { None } else { Some(id) }
|
|
|
|
});
|
2017-02-14 14:35:13 +00:00
|
|
|
let n = leaks.len();
|
2019-11-23 08:24:47 +00:00
|
|
|
if n > 0 {
|
2020-07-28 14:15:40 +00:00
|
|
|
eprintln!("The following memory was leaked: {:?}", self.dump_allocs(leaks));
|
2019-11-23 08:24:47 +00:00
|
|
|
}
|
2017-02-14 14:35:13 +00:00
|
|
|
n
|
|
|
|
}
|
2018-10-20 09:09:44 +00:00
|
|
|
|
|
|
|
/// This is used by [priroda](https://github.com/oli-obk/priroda)
|
2018-10-20 11:42:25 +00:00
|
|
|
pub fn alloc_map(&self) -> &M::MemoryMap {
|
2018-10-20 09:09:44 +00:00
|
|
|
&self.alloc_map
|
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-04-06 09:45:06 +00:00
|
|
|
|
2020-07-28 14:15:40 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// There's no way to use this directly, it's just a helper struct for the `dump_alloc(s)` methods.
|
|
|
|
pub struct DumpAllocs<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> {
|
|
|
|
mem: &'a Memory<'mir, 'tcx, M>,
|
|
|
|
allocs: Vec<AllocId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a, 'mir, 'tcx, M> {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
// Cannot be a closure because it is generic in `Tag`, `Extra`.
|
|
|
|
fn write_allocation_track_relocs<'tcx, Tag: Copy + fmt::Debug, Extra>(
|
|
|
|
fmt: &mut std::fmt::Formatter<'_>,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
allocs_to_print: &mut VecDeque<AllocId>,
|
|
|
|
alloc: &Allocation<Tag, Extra>,
|
|
|
|
) -> std::fmt::Result {
|
|
|
|
for &(_, target_id) in alloc.relocations().values() {
|
|
|
|
allocs_to_print.push_back(target_id);
|
|
|
|
}
|
2020-07-28 17:16:09 +00:00
|
|
|
write!(fmt, "{}", pretty::display_allocation(tcx, alloc))
|
2020-07-28 14:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut allocs_to_print: VecDeque<_> = self.allocs.iter().copied().collect();
|
|
|
|
// `allocs_printed` contains all allocations that we have already printed.
|
|
|
|
let mut allocs_printed = FxHashSet::default();
|
|
|
|
|
|
|
|
while let Some(id) = allocs_to_print.pop_front() {
|
|
|
|
if !allocs_printed.insert(id) {
|
|
|
|
// Already printed, so skip this.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(fmt, "{}", id)?;
|
|
|
|
match self.mem.alloc_map.get(id) {
|
|
|
|
Some(&(kind, ref alloc)) => {
|
|
|
|
// normal alloc
|
|
|
|
write!(fmt, " ({}, ", kind)?;
|
|
|
|
write_allocation_track_relocs(
|
|
|
|
&mut *fmt,
|
|
|
|
self.mem.tcx,
|
|
|
|
&mut allocs_to_print,
|
|
|
|
alloc,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// global alloc
|
|
|
|
match self.mem.tcx.get_global_alloc(id) {
|
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
|
|
|
write!(fmt, " (unchanged global, ")?;
|
|
|
|
write_allocation_track_relocs(
|
|
|
|
&mut *fmt,
|
|
|
|
self.mem.tcx,
|
|
|
|
&mut allocs_to_print,
|
|
|
|
alloc,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function(func)) => {
|
|
|
|
write!(fmt, " (fn: {})", func)?;
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Static(did)) => {
|
|
|
|
write!(fmt, " (static: {})", self.mem.tcx.def_path_str(did))?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
write!(fmt, " (deallocated)")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeln!(fmt)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-23 12:26:36 +00:00
|
|
|
/// Reading and writing.
|
2019-06-11 19:03:44 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
2019-06-30 21:49:37 +00:00
|
|
|
/// Reads the given number of bytes from memory. Returns them as a slice.
|
|
|
|
///
|
2019-06-23 12:26:36 +00:00
|
|
|
/// Performs appropriate bounds checks.
|
2019-12-22 22:42:04 +00:00
|
|
|
pub fn read_bytes(&self, ptr: Scalar<M::PointerTag>, size: Size) -> InterpResult<'tcx, &[u8]> {
|
2019-06-23 12:26:36 +00:00
|
|
|
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
|
|
|
|
Some(ptr) => ptr,
|
|
|
|
None => return Ok(&[]), // zero-sized access
|
|
|
|
};
|
2019-11-02 16:46:11 +00:00
|
|
|
self.get_raw(ptr.alloc_id)?.get_bytes(self, ptr, size)
|
2018-11-13 15:13:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 10:02:35 +00:00
|
|
|
/// Writes the given stream of bytes into memory.
|
|
|
|
///
|
|
|
|
/// Performs appropriate bounds checks.
|
|
|
|
pub fn write_bytes(
|
|
|
|
&mut self,
|
|
|
|
ptr: Scalar<M::PointerTag>,
|
2019-12-22 22:42:04 +00:00
|
|
|
src: impl IntoIterator<Item = u8>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-25 17:07:08 +00:00
|
|
|
let mut src = src.into_iter();
|
2020-03-22 16:48:11 +00:00
|
|
|
let size = Size::from_bytes(src.size_hint().0);
|
2020-03-21 12:49:02 +00:00
|
|
|
// `write_bytes` checks that this lower bound `size` matches the upper bound and reality.
|
2019-10-20 10:02:35 +00:00
|
|
|
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
|
|
|
|
Some(ptr) => ptr,
|
2020-03-25 17:07:08 +00:00
|
|
|
None => {
|
|
|
|
// zero-sized access
|
2021-03-04 12:06:01 +00:00
|
|
|
assert_matches!(
|
|
|
|
src.next(),
|
|
|
|
None,
|
|
|
|
"iterator said it was empty but returned an element"
|
|
|
|
);
|
2020-03-25 17:07:08 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2019-10-20 10:02:35 +00:00
|
|
|
};
|
2020-06-01 06:42:40 +00:00
|
|
|
let tcx = self.tcx;
|
2019-11-02 16:46:11 +00:00
|
|
|
self.get_raw_mut(ptr.alloc_id)?.write_bytes(&tcx, ptr, src)
|
2019-10-20 10:02:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 10:46:08 +00:00
|
|
|
/// Expects the caller to have checked bounds and alignment.
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn copy(
|
|
|
|
&mut self,
|
2019-07-06 10:46:08 +00:00
|
|
|
src: Pointer<M::PointerTag>,
|
|
|
|
dest: Pointer<M::PointerTag>,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-08-10 15:48:38 +00:00
|
|
|
nonoverlapping: bool,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-07-06 10:46:08 +00:00
|
|
|
self.copy_repeatedly(src, dest, size, 1, nonoverlapping)
|
2018-06-27 02:59:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-06 10:46:08 +00:00
|
|
|
/// Expects the caller to have checked bounds and alignment.
|
2018-06-27 02:59:10 +00:00
|
|
|
pub fn copy_repeatedly(
|
|
|
|
&mut self,
|
2019-07-06 10:46:08 +00:00
|
|
|
src: Pointer<M::PointerTag>,
|
|
|
|
dest: Pointer<M::PointerTag>,
|
2018-06-27 02:59:10 +00:00
|
|
|
size: Size,
|
|
|
|
length: u64,
|
|
|
|
nonoverlapping: bool,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2017-08-28 12:08:55 +00:00
|
|
|
// first copy the relocations to a temporary buffer, because
|
|
|
|
// `get_bytes_mut` will clear the relocations, which is correct,
|
|
|
|
// since we don't want to keep any relocations at the target.
|
2020-08-08 13:53:47 +00:00
|
|
|
// (`get_bytes_with_uninit_and_ptr` below checks that there are no
|
2018-08-29 08:07:27 +00:00
|
|
|
// relocations overlapping the edges; those would not be handled correctly).
|
2019-12-22 22:42:04 +00:00
|
|
|
let relocations =
|
|
|
|
self.get_raw(src.alloc_id)?.prepare_relocation_copy(self, src, size, dest, length);
|
2017-08-28 12:08:55 +00:00
|
|
|
|
2020-06-01 06:42:40 +00:00
|
|
|
let tcx = self.tcx;
|
2018-11-12 12:26:53 +00:00
|
|
|
|
2018-11-15 13:43:58 +00:00
|
|
|
// This checks relocation edges on the src.
|
2019-12-22 22:42:04 +00:00
|
|
|
let src_bytes =
|
2020-08-08 13:53:47 +00:00
|
|
|
self.get_raw(src.alloc_id)?.get_bytes_with_uninit_and_ptr(&tcx, src, size)?.as_ptr();
|
2019-12-22 22:42:04 +00:00
|
|
|
let dest_bytes =
|
2020-03-24 15:43:50 +00:00
|
|
|
self.get_raw_mut(dest.alloc_id)?.get_bytes_mut(&tcx, dest, size * length)?; // `Size` multiplication
|
2019-12-27 11:56:52 +00:00
|
|
|
|
|
|
|
// If `dest_bytes` is empty we just optimize to not run anything for zsts.
|
|
|
|
// See #67539
|
|
|
|
if dest_bytes.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let dest_bytes = dest_bytes.as_mut_ptr();
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2020-07-22 15:08:59 +00:00
|
|
|
// Prepare a copy of the initialization mask.
|
2020-08-08 13:53:47 +00:00
|
|
|
let compressed = self.get_raw(src.alloc_id)?.compress_uninit_range(src, size);
|
2019-12-27 18:50:56 +00:00
|
|
|
|
2020-07-22 15:08:59 +00:00
|
|
|
if compressed.no_bytes_init() {
|
|
|
|
// Fast path: If all bytes are `uninit` then there is nothing to copy. The target range
|
2020-08-04 15:01:32 +00:00
|
|
|
// is marked as uninitialized but we otherwise omit changing the byte representation which may
|
2020-07-22 15:08:59 +00:00
|
|
|
// be arbitrary for uninitialized bytes.
|
2019-12-27 18:50:56 +00:00
|
|
|
// This also avoids writing to the target bytes so that the backing allocation is never
|
2020-07-22 15:08:59 +00:00
|
|
|
// touched if the bytes stay uninitialized for the whole interpreter execution. On contemporary
|
2019-12-27 18:50:56 +00:00
|
|
|
// operating system this can avoid physically allocating the page.
|
|
|
|
let dest_alloc = self.get_raw_mut(dest.alloc_id)?;
|
2020-07-22 15:08:59 +00:00
|
|
|
dest_alloc.mark_init(dest, size * length, false); // `Size` multiplication
|
2019-12-27 18:50:56 +00:00
|
|
|
dest_alloc.mark_relocation_range(relocations);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2016-03-05 06:48:23 +00:00
|
|
|
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes
|
|
|
|
// behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
|
|
|
|
// `dest` could possibly overlap.
|
2018-10-05 08:28:33 +00:00
|
|
|
// The pointers above remain valid even if the `HashMap` table is moved around because they
|
2018-09-29 07:51:38 +00:00
|
|
|
// point into the `Vec` storing the bytes.
|
2016-03-05 06:48:23 +00:00
|
|
|
unsafe {
|
|
|
|
if src.alloc_id == dest.alloc_id {
|
2017-07-04 03:27:09 +00:00
|
|
|
if nonoverlapping {
|
2020-03-24 15:43:50 +00:00
|
|
|
// `Size` additions
|
|
|
|
if (src.offset <= dest.offset && src.offset + size > dest.offset)
|
|
|
|
|| (dest.offset <= src.offset && dest.offset + size > src.offset)
|
2017-08-10 15:48:38 +00:00
|
|
|
{
|
2019-12-22 22:42:04 +00:00
|
|
|
throw_ub_format!("copy_nonoverlapping called on overlapping ranges")
|
2017-07-04 03:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 02:59:10 +00:00
|
|
|
|
|
|
|
for i in 0..length {
|
2019-12-22 22:42:04 +00:00
|
|
|
ptr::copy(
|
|
|
|
src_bytes,
|
2020-03-24 16:13:26 +00:00
|
|
|
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
|
|
|
|
size.bytes_usize(),
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2018-06-27 02:59:10 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
} else {
|
2018-06-27 02:59:10 +00:00
|
|
|
for i in 0..length {
|
2019-12-22 22:42:04 +00:00
|
|
|
ptr::copy_nonoverlapping(
|
|
|
|
src_bytes,
|
2020-03-24 16:13:26 +00:00
|
|
|
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
|
|
|
|
size.bytes_usize(),
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2018-06-27 02:59:10 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 18:50:56 +00:00
|
|
|
// now fill in all the data
|
2020-07-22 15:08:59 +00:00
|
|
|
self.get_raw_mut(dest.alloc_id)?.mark_compressed_init_range(
|
2019-12-27 18:50:56 +00:00
|
|
|
&compressed,
|
|
|
|
dest,
|
|
|
|
size,
|
|
|
|
length,
|
|
|
|
);
|
|
|
|
|
2018-08-29 08:07:27 +00:00
|
|
|
// copy the relocations to the destination
|
2019-11-02 16:46:11 +00:00
|
|
|
self.get_raw_mut(dest.alloc_id)?.mark_relocation_range(relocations);
|
2016-04-06 10:08:52 +00:00
|
|
|
|
|
|
|
Ok(())
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2019-12-27 18:50:56 +00:00
|
|
|
/// Machine pointer introspection.
|
2019-06-11 19:03:44 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
2019-06-12 17:49:46 +00:00
|
|
|
pub fn force_ptr(
|
|
|
|
&self,
|
|
|
|
scalar: Scalar<M::PointerTag>,
|
|
|
|
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
|
|
|
|
match scalar {
|
|
|
|
Scalar::Ptr(ptr) => Ok(ptr),
|
2019-12-22 22:42:04 +00:00
|
|
|
_ => M::int_to_ptr(&self, scalar.to_machine_usize(self)?),
|
2019-06-12 17:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:26:10 +00:00
|
|
|
pub fn force_bits(
|
|
|
|
&self,
|
|
|
|
scalar: Scalar<M::PointerTag>,
|
2019-12-22 22:42:04 +00:00
|
|
|
size: Size,
|
2019-06-13 17:26:10 +00:00
|
|
|
) -> InterpResult<'tcx, u128> {
|
|
|
|
match scalar.to_bits_or_ptr(size, self) {
|
2019-06-12 17:49:46 +00:00
|
|
|
Ok(bits) => Ok(bits),
|
2020-03-21 12:49:02 +00:00
|
|
|
Err(ptr) => Ok(M::ptr_to_int(&self, ptr)?.into()),
|
2019-06-12 17:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 04:25:08 +00:00
|
|
|
}
|