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
|
|
|
|
//! 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;
|
2019-10-22 16:06:30 +00:00
|
|
|
use std::ptr;
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
use rustc::ty::layout::{Align, HasDataLayout, Size, TargetDataLayout};
|
|
|
|
use rustc::ty::{self, query::TyCtxtAt, Instance, ParamEnv};
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2018-06-08 02:47:26 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ast::Mutability;
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2018-10-16 12:50:07 +00:00
|
|
|
use super::{
|
2019-12-22 22:42:04 +00:00
|
|
|
AllocId, AllocMap, Allocation, AllocationExtra, CheckInAllocMsg, ErrorHandled, GlobalAlloc,
|
|
|
|
GlobalId, InterpResult, Machine, MayLeak, Pointer, PointerArithmetic, Scalar,
|
2018-10-16 12:50:07 +00:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
/// static and looked up in the `tcx` for read access. Some machines may
|
2018-10-05 13:13:59 +00:00
|
|
|
/// have to mutate this map even on a read-only access to a static (because
|
|
|
|
/// they do pointer provenance tracking and the allocations in `tcx` have
|
|
|
|
/// the wrong type), so we let the machine override this type.
|
2018-10-05 08:23:39 +00:00
|
|
|
/// Either way, if the machine allows writing to a static, doing so will
|
|
|
|
/// create a copy of the static 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>,
|
|
|
|
|
2018-09-15 14:34:30 +00:00
|
|
|
/// To be able to compare pointers with NULL, and to check alignment for accesses
|
|
|
|
/// 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.
|
2019-06-30 11:51:18 +00:00
|
|
|
pub tcx: TyCtxtAt<'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
|
|
|
|
2018-10-22 16:21:55 +00:00
|
|
|
// FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead
|
2018-09-20 08:12:21 +00:00
|
|
|
// carefully copy only the reachable parts.
|
2019-06-11 21:11:55 +00:00
|
|
|
impl<'mir, 'tcx, M> Clone for Memory<'mir, 'tcx, M>
|
2018-11-14 15:00:52 +00:00
|
|
|
where
|
2019-12-16 14:23:42 +00:00
|
|
|
M: Machine<'mir, 'tcx, PointerTag = (), AllocExtra = ()>,
|
|
|
|
M::MemoryExtra: Copy,
|
2018-11-14 15:00:52 +00:00
|
|
|
M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation)>,
|
2018-09-20 08:12:21 +00:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Memory {
|
|
|
|
alloc_map: self.alloc_map.clone(),
|
2019-06-30 11:51:18 +00:00
|
|
|
extra_fn_ptr_map: self.extra_fn_ptr_map.clone(),
|
2018-09-20 08:12:21 +00:00
|
|
|
dead_alloc_map: self.dead_alloc_map.clone(),
|
2019-12-16 14:23:42 +00:00
|
|
|
extra: self.extra,
|
2018-09-20 08:12:21 +00:00
|
|
|
tcx: self.tcx,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
|
2019-06-26 18:13:19 +00:00
|
|
|
pub fn new(tcx: TyCtxtAt<'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
|
2019-12-01 09:02:41 +00:00
|
|
|
/// the *canonical* machine pointer to the allocation. Must never be used
|
|
|
|
/// for any other pointers!
|
|
|
|
///
|
|
|
|
/// This represents a *direct* access to that memory, as opposed to access
|
|
|
|
/// through a pointer that was created by the program.
|
2019-05-28 08:44:46 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
|
2020-02-23 18:50:34 +00:00
|
|
|
let id = M::canonical_alloc_id(self, ptr.alloc_id);
|
|
|
|
ptr.with_tag(M::tag_static_base_pointer(&self.extra, id))
|
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 {
|
|
|
|
FnVal::Instance(instance) => self.tcx.alloc_map.lock().create_fn_alloc(instance),
|
|
|
|
FnVal::Other(extra) => {
|
2019-06-30 13:35:39 +00:00
|
|
|
// FIXME(RalfJung): Should we have a cache here?
|
2019-06-30 11:51:18 +00:00
|
|
|
let id = self.tcx.alloc_map.lock().reserve();
|
|
|
|
let old = self.extra_fn_ptr_map.insert(id, extra);
|
|
|
|
assert!(old.is_none());
|
|
|
|
id
|
|
|
|
}
|
|
|
|
};
|
2019-05-28 08:44:46 +00:00
|
|
|
self.tag_static_base_pointer(Pointer::from(id))
|
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,
|
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
|
|
|
) -> Pointer<M::PointerTag> {
|
|
|
|
let alloc = Allocation::undef(size, align);
|
|
|
|
self.allocate_with(alloc, kind)
|
2017-02-10 21:35:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 08:44:46 +00:00
|
|
|
pub fn allocate_static_bytes(
|
2017-07-28 14:48:43 +00:00
|
|
|
&mut self,
|
2019-05-28 08:44:46 +00:00
|
|
|
bytes: &[u8],
|
2018-06-08 02:47:26 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
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,
|
2018-06-08 02:47:26 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2019-04-15 08:05:13 +00:00
|
|
|
) -> Pointer<M::PointerTag> {
|
2019-05-28 08:44:46 +00:00
|
|
|
let id = self.tcx.alloc_map.lock().reserve();
|
2019-12-22 22:42:04 +00:00
|
|
|
debug_assert_ne!(
|
|
|
|
Some(kind),
|
|
|
|
M::STATIC_KIND.map(MemoryKind::Machine),
|
|
|
|
"dynamically allocating static memory"
|
|
|
|
);
|
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,
|
2017-08-09 12:53:22 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
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 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(ReallocateNonBasePtr)
|
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,
|
2019-11-02 16:46:11 +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
|
|
|
}
|
|
|
|
|
2018-08-23 17:04:33 +00:00
|
|
|
/// Deallocate a local, or do nothing if that local has been made into a static
|
2019-06-07 16:56:27 +00:00
|
|
|
pub fn deallocate_local(&mut self, ptr: Pointer<M::PointerTag>) -> InterpResult<'tcx> {
|
2018-08-23 17:04:33 +00:00
|
|
|
// The allocation might be already removed by static interning.
|
|
|
|
// 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)>,
|
2017-08-09 12:53:22 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
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 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(DeallocateNonBasePtr)
|
2016-11-17 13:48:34 +00:00
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
|
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 => {
|
2018-08-23 17:04:33 +00:00
|
|
|
// Deallocating static memory -- always an error
|
2019-07-31 07:18:54 +00:00
|
|
|
return Err(match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
Some(GlobalAlloc::Function(..)) => err_unsup!(DeallocatedWrongMemoryKind(
|
2018-05-02 04:03:06 +00:00
|
|
|
"function".to_string(),
|
|
|
|
format!("{:?}", kind),
|
2019-07-29 07:36:42 +00:00
|
|
|
)),
|
2019-07-31 07:18:54 +00:00
|
|
|
Some(GlobalAlloc::Static(..)) | Some(GlobalAlloc::Memory(..)) => err_unsup!(
|
|
|
|
DeallocatedWrongMemoryKind("static".to_string(), format!("{:?}", kind))
|
|
|
|
),
|
|
|
|
None => err_unsup!(DoubleFree),
|
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 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(DeallocatedWrongMemoryKind(
|
2017-12-06 08:25:29 +00:00
|
|
|
format!("{:?}", alloc_kind),
|
2017-08-10 15:48:38 +00:00
|
|
|
format!("{:?}", kind),
|
2019-07-30 14:48:50 +00:00
|
|
|
))
|
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 {
|
2019-07-30 21:10:51 +00:00
|
|
|
if size != alloc.size || align != alloc.align {
|
|
|
|
let bytes = alloc.size;
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(IncorrectAllocationInformation(size, bytes, align, alloc.align))
|
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
|
2019-07-30 21:10:51 +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
|
2019-12-22 22:42:04 +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>>> {
|
2019-12-06 12:18:32 +00:00
|
|
|
let align = M::CHECK_ALIGN.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`
|
2019-11-04 11:28:13 +00:00
|
|
|
/// is `Some` (overriding `M::CHECK_ALIGN`). Also lets the caller control
|
|
|
|
/// 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();
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(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 {
|
|
|
|
// A "real" access, we must get a pointer.
|
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) => {
|
|
|
|
let bits = bits as u64; // it's ptr-sized
|
|
|
|
assert!(size.bytes() == 0);
|
2019-07-28 11:44:11 +00:00
|
|
|
// Must be non-NULL.
|
2019-06-23 12:26:36 +00:00
|
|
|
if bits == 0 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(InvalidNullPointerUsage)
|
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)?;
|
2019-06-23 14:42:51 +00:00
|
|
|
// Test bounds. This also ensures non-NULL.
|
|
|
|
// It is sufficient to check this for the end pointer. The addition
|
|
|
|
// checks for overflow.
|
|
|
|
let end_ptr = ptr.offset(size, self)?;
|
2019-11-04 11:28:13 +00:00
|
|
|
end_ptr.check_inbounds_alloc(allocation_size, msg)?;
|
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 {
|
2019-07-28 11:44:11 +00:00
|
|
|
if alloc_align.bytes() < align.bytes() {
|
|
|
|
// The allocation itself is not aligned enough.
|
|
|
|
// FIXME: Alignment check is too strict, depending on the base address that
|
|
|
|
// got picked we might be aligned even if this check fails.
|
|
|
|
// We instead have to fall back to converting to an integer and checking
|
|
|
|
// the "real" alignment.
|
2019-12-22 22:42:04 +00:00
|
|
|
throw_unsup!(AlignmentCheckFailed { has: alloc_align, required: align });
|
2019-07-28 11:44:11 +00:00
|
|
|
}
|
|
|
|
check_offset_align(ptr.offset.bytes(), align)?;
|
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`.
|
|
|
|
if size.bytes() == 0 { None } else { Some(ptr) }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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");
|
2019-07-28 12:19:13 +00:00
|
|
|
ptr.check_inbounds_alloc(size, CheckInAllocMsg::NullPointerTest).is_err()
|
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> {
|
2018-10-05 14:49:51 +00:00
|
|
|
/// Helper function to obtain the global (tcx) allocation for a static.
|
|
|
|
/// 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`.
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
/// `const_eval_raw` and it is the "resolved" ID.
|
|
|
|
/// The resolved ID is never used by the interpreted progrma, it is hidden.
|
|
|
|
/// 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
|
2019-05-28 17:08:14 +00:00
|
|
|
/// another static), those inner references only exist in "resolved" form.
|
2020-02-23 16:04:34 +00:00
|
|
|
///
|
|
|
|
/// Assumes `id` is already canonical.
|
2018-08-27 11:34:12 +00:00
|
|
|
fn get_static_alloc(
|
2019-07-02 10:31:17 +00:00
|
|
|
memory_extra: &M::MemoryExtra,
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxtAt<'tcx>,
|
2019-07-02 10:31:17 +00:00
|
|
|
id: AllocId,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
|
2018-08-27 11:34:12 +00:00
|
|
|
let alloc = tcx.alloc_map.lock().get(id);
|
2019-05-28 08:44:46 +00:00
|
|
|
let alloc = match alloc {
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(GlobalAlloc::Memory(mem)) => Cow::Borrowed(mem),
|
|
|
|
Some(GlobalAlloc::Function(..)) => throw_unsup!(DerefFunctionPointer),
|
|
|
|
None => throw_unsup!(DanglingPointerDeref),
|
2019-05-28 08:44:46 +00:00
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
|
|
|
// We got a "lazy" static that has not been computed yet.
|
|
|
|
if tcx.is_foreign_item(def_id) {
|
2020-02-23 16:04:34 +00:00
|
|
|
trace!("get_static_alloc: foreign item {:?}", def_id);
|
|
|
|
throw_unsup!(ReadForeignStatic)
|
2019-05-28 08:44:46 +00:00
|
|
|
}
|
2020-02-23 16:04:34 +00:00
|
|
|
trace!("get_static_alloc: Need to compute {:?}", def_id);
|
|
|
|
let instance = Instance::mono(tcx.tcx, def_id);
|
|
|
|
let gid = GlobalId { instance, promoted: None };
|
|
|
|
// use the raw query here to break validation cycles. Later uses of the static
|
|
|
|
// will call the full query anyway
|
|
|
|
let raw_const =
|
|
|
|
tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid)).map_err(|err| {
|
|
|
|
// no need to report anything, the const_eval call takes care of that
|
|
|
|
// for statics
|
|
|
|
assert!(tcx.is_static(def_id));
|
|
|
|
match err {
|
|
|
|
ErrorHandled::Reported => err_inval!(ReferencedConstant),
|
|
|
|
ErrorHandled::TooGeneric => err_inval!(TooGeneric),
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
// Make sure we use the ID of the resolved memory, not the lazy one!
|
|
|
|
let id = raw_const.alloc_id;
|
|
|
|
let allocation = tcx.alloc_map.lock().unwrap_memory(id);
|
|
|
|
|
|
|
|
M::before_access_static(memory_extra, allocation)?;
|
|
|
|
Cow::Borrowed(allocation)
|
2018-08-31 08:39:47 +00:00
|
|
|
}
|
2019-05-28 08:44:46 +00:00
|
|
|
};
|
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,
|
|
|
|
M::STATIC_KIND.map(MemoryKind::Machine),
|
2019-12-01 09:02:41 +00:00
|
|
|
);
|
|
|
|
debug_assert_eq!(tag, M::tag_static_base_pointer(memory_extra, id));
|
|
|
|
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.
|
|
|
|
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCtx` instead!
|
|
|
|
pub fn get_raw(
|
2019-06-07 16:56:27 +00:00
|
|
|
&self,
|
|
|
|
id: AllocId,
|
|
|
|
) -> InterpResult<'tcx, &Allocation<M::PointerTag, M::AllocExtra>> {
|
2020-02-23 16:04:34 +00:00
|
|
|
let id = M::canonical_alloc_id(self, id);
|
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
|
|
|
|
// `get_static_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, || {
|
2019-07-02 10:31:17 +00:00
|
|
|
let alloc = Self::get_static_alloc(&self.extra, self.tcx, id).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
|
|
|
|
let kind = M::STATIC_KIND.expect(
|
|
|
|
"I got an owned 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.
|
|
|
|
/// Use the higher-level, `PlaceTy`- and `OpTy`-based APIs in `InterpCtx` instead!
|
|
|
|
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>> {
|
2020-02-23 16:04:34 +00:00
|
|
|
let id = M::canonical_alloc_id(self, id);
|
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, || {
|
|
|
|
// Need to make a copy, even if `get_static_alloc` is able
|
|
|
|
// to give us a cheap reference.
|
2019-07-02 10:31:17 +00:00
|
|
|
let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
|
2019-12-16 16:28:40 +00:00
|
|
|
if alloc.mutability == Mutability::Not {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(ModifiedConstantMemory)
|
2018-08-26 10:59:59 +00:00
|
|
|
}
|
2018-11-19 12:49:07 +00:00
|
|
|
match M::STATIC_KIND {
|
|
|
|
Some(kind) => Ok((MemoryKind::Machine(kind), alloc.into_owned())),
|
2019-07-30 14:48:50 +00:00
|
|
|
None => throw_unsup!(ModifiedStatic),
|
2018-11-19 12:49:07 +00:00
|
|
|
}
|
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 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(ModifiedConstantMemory)
|
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)> {
|
2020-02-23 16:04:34 +00:00
|
|
|
let id = M::canonical_alloc_id(self, id);
|
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
|
|
|
|
// b) duplicate a static's allocation in miri
|
2019-07-28 10:58:39 +00:00
|
|
|
if let Some((_, alloc)) = self.alloc_map.get(id) {
|
2019-07-30 21:10:51 +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.
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(DerefFunctionPointer)
|
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.
|
|
|
|
let alloc = self.tcx.alloc_map.lock().get(id);
|
|
|
|
match alloc {
|
|
|
|
Some(GlobalAlloc::Static(did)) => {
|
|
|
|
// 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.
|
2019-12-22 22:42:04 +00:00
|
|
|
Ok((alloc.size, alloc.align))
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
Ok(*self.dead_alloc_map.get(&id).expect(
|
|
|
|
"deallocated pointers should all be recorded in \
|
|
|
|
`dead_alloc_map`",
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
throw_unsup!(DanglingPointerDeref)
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 14:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 16:04:34 +00:00
|
|
|
/// Assumes `id` is already canonical.
|
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 {
|
|
|
|
match self.tcx.alloc_map.lock().get(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 {
|
2019-07-30 14:48:50 +00:00
|
|
|
throw_unsup!(InvalidFunctionPointer)
|
2017-06-22 04:45:51 +00:00
|
|
|
}
|
2020-02-23 16:04:34 +00:00
|
|
|
let id = M::canonical_alloc_id(self, ptr.alloc_id);
|
|
|
|
self.get_fn_alloc(id).ok_or_else(|| err_unsup!(ExecuteMemory).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(())
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:12:17 +00:00
|
|
|
/// Print an allocation and all allocations it points to, recursively.
|
|
|
|
/// This prints directly to stderr, ignoring RUSTC_LOG! It is up to the caller to
|
|
|
|
/// control for this.
|
2016-12-08 06:00:46 +00:00
|
|
|
pub fn dump_alloc(&self, id: AllocId) {
|
|
|
|
self.dump_allocs(vec![id]);
|
|
|
|
}
|
|
|
|
|
2018-10-16 07:15:13 +00:00
|
|
|
fn dump_alloc_helper<Tag, Extra>(
|
2018-09-21 21:32:59 +00:00
|
|
|
&self,
|
|
|
|
allocs_seen: &mut FxHashSet<AllocId>,
|
|
|
|
allocs_to_print: &mut VecDeque<AllocId>,
|
|
|
|
mut msg: String,
|
2018-10-16 07:15:13 +00:00
|
|
|
alloc: &Allocation<Tag, Extra>,
|
2018-09-21 21:32:59 +00:00
|
|
|
extra: String,
|
|
|
|
) {
|
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
let prefix_len = msg.len();
|
|
|
|
let mut relocations = vec![];
|
|
|
|
|
2019-07-30 21:10:51 +00:00
|
|
|
for i in 0..alloc.size.bytes() {
|
2018-09-21 21:32:59 +00:00
|
|
|
let i = Size::from_bytes(i);
|
2019-08-29 16:02:51 +00:00
|
|
|
if let Some(&(_, target_id)) = alloc.relocations().get(&i) {
|
2018-09-21 21:32:59 +00:00
|
|
|
if allocs_seen.insert(target_id) {
|
|
|
|
allocs_to_print.push_back(target_id);
|
|
|
|
}
|
|
|
|
relocations.push((i, target_id));
|
|
|
|
}
|
2019-08-14 00:26:18 +00:00
|
|
|
if alloc.undef_mask().is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
|
2018-09-21 21:32:59 +00:00
|
|
|
// this `as usize` is fine, since `i` came from a `usize`
|
2019-07-30 21:10:51 +00:00
|
|
|
let i = i.bytes() as usize;
|
|
|
|
|
|
|
|
// Checked definedness (and thus range) and relocations. This access also doesn't
|
|
|
|
// influence interpreter execution but is only for debugging.
|
2019-12-22 22:42:04 +00:00
|
|
|
let bytes = alloc.inspect_with_undef_and_ptr_outside_interpreter(i..i + 1);
|
2019-07-30 21:10:51 +00:00
|
|
|
write!(msg, "{:02x} ", bytes[0]).unwrap();
|
2018-09-21 21:32:59 +00:00
|
|
|
} else {
|
|
|
|
msg.push_str("__ ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:12:17 +00:00
|
|
|
eprintln!(
|
2018-09-21 21:32:59 +00:00
|
|
|
"{}({} bytes, alignment {}){}",
|
|
|
|
msg,
|
2019-07-30 21:10:51 +00:00
|
|
|
alloc.size.bytes(),
|
2018-09-08 22:16:45 +00:00
|
|
|
alloc.align.bytes(),
|
2018-09-21 21:32:59 +00:00
|
|
|
extra
|
|
|
|
);
|
|
|
|
|
|
|
|
if !relocations.is_empty() {
|
|
|
|
msg.clear();
|
|
|
|
write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
|
|
|
|
let mut pos = Size::ZERO;
|
|
|
|
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
|
|
|
|
for (i, target_id) in relocations {
|
|
|
|
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
|
|
|
|
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
|
|
|
|
let target = format!("({})", target_id);
|
|
|
|
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
|
|
|
|
write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
|
|
|
|
pos = i + self.pointer_size();
|
|
|
|
}
|
2019-11-23 08:12:17 +00:00
|
|
|
eprintln!("{}", msg);
|
2018-09-21 21:32:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 08:12:17 +00:00
|
|
|
/// Print a list of allocations and all allocations they point to, recursively.
|
|
|
|
/// This prints directly to stderr, ignoring RUSTC_LOG! It is up to the caller to
|
|
|
|
/// control for this.
|
2016-12-08 06:00:46 +00:00
|
|
|
pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
|
|
|
|
allocs.sort();
|
|
|
|
allocs.dedup();
|
|
|
|
let mut allocs_to_print = VecDeque::from(allocs);
|
2018-03-23 09:32:27 +00:00
|
|
|
let mut allocs_seen = FxHashSet::default();
|
2016-04-06 09:45:06 +00:00
|
|
|
|
|
|
|
while let Some(id) = allocs_to_print.pop_front() {
|
2018-09-21 21:32:59 +00:00
|
|
|
let msg = format!("Alloc {:<5} ", format!("{}:", id));
|
|
|
|
|
|
|
|
// normal alloc?
|
2018-10-05 13:13:59 +00:00
|
|
|
match self.alloc_map.get_or(id, || Err(())) {
|
|
|
|
Ok((kind, alloc)) => {
|
2018-09-21 21:32:59 +00:00
|
|
|
let extra = match kind {
|
2017-12-06 08:25:29 +00:00
|
|
|
MemoryKind::Stack => " (stack)".to_owned(),
|
2018-09-21 21:32:59 +00:00
|
|
|
MemoryKind::Vtable => " (vtable)".to_owned(),
|
2019-11-28 18:15:32 +00:00
|
|
|
MemoryKind::CallerLocation => " (caller_location)".to_owned(),
|
2017-12-06 08:25:29 +00:00
|
|
|
MemoryKind::Machine(m) => format!(" ({:?})", m),
|
2018-09-21 21:32:59 +00:00
|
|
|
};
|
|
|
|
self.dump_alloc_helper(
|
2019-12-22 22:42:04 +00:00
|
|
|
&mut allocs_seen,
|
|
|
|
&mut allocs_to_print,
|
|
|
|
msg,
|
|
|
|
alloc,
|
|
|
|
extra,
|
2018-09-21 21:32:59 +00:00
|
|
|
);
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2018-10-05 13:13:59 +00:00
|
|
|
Err(()) => {
|
2018-09-21 21:32:59 +00:00
|
|
|
// static alloc?
|
|
|
|
match self.tcx.alloc_map.lock().get(id) {
|
2019-05-30 11:05:05 +00:00
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
2018-09-21 21:32:59 +00:00
|
|
|
self.dump_alloc_helper(
|
2019-12-22 22:42:04 +00:00
|
|
|
&mut allocs_seen,
|
|
|
|
&mut allocs_to_print,
|
|
|
|
msg,
|
|
|
|
alloc,
|
|
|
|
" (immutable)".to_owned(),
|
2018-09-21 21:32:59 +00:00
|
|
|
);
|
|
|
|
}
|
2019-05-30 11:05:05 +00:00
|
|
|
Some(GlobalAlloc::Function(func)) => {
|
2019-11-23 08:12:17 +00:00
|
|
|
eprintln!("{} {}", msg, func);
|
2018-09-21 21:32:59 +00:00
|
|
|
}
|
2019-05-30 11:05:05 +00:00
|
|
|
Some(GlobalAlloc::Static(did)) => {
|
2019-11-23 08:12:17 +00:00
|
|
|
eprintln!("{} {:?}", msg, did);
|
2018-09-21 21:32:59 +00:00
|
|
|
}
|
|
|
|
None => {
|
2019-11-23 08:12:17 +00:00
|
|
|
eprintln!("{} (deallocated)", msg);
|
2018-06-08 02:47:26 +00:00
|
|
|
}
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2018-09-21 21:32:59 +00:00
|
|
|
};
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-14 14:35:13 +00:00
|
|
|
|
|
|
|
pub fn leak_report(&self) -> usize {
|
2019-12-22 22:42:04 +00:00
|
|
|
let leaks: Vec<_> = self
|
|
|
|
.alloc_map
|
|
|
|
.filter_map_collect(|&id, &(kind, _)| if kind.may_leak() { 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 {
|
|
|
|
eprintln!("### LEAK REPORT ###");
|
|
|
|
self.dump_allocs(leaks);
|
|
|
|
}
|
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
|
|
|
|
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-06-30 21:49:37 +00:00
|
|
|
/// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice.
|
|
|
|
///
|
|
|
|
/// Performs appropriate bounds checks.
|
|
|
|
pub fn read_c_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, &[u8]> {
|
|
|
|
let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we *need* a ptr.
|
2019-11-02 16:46:11 +00:00
|
|
|
self.get_raw(ptr.alloc_id)?.read_c_str(self, ptr)
|
2019-06-30 21:49:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 19:29:40 +00:00
|
|
|
/// Reads a 0x0000-terminated u16-sequence from memory. Returns them as a Vec<u16>.
|
|
|
|
/// Terminator 0x0000 is not included in the returned Vec<u16>.
|
|
|
|
///
|
|
|
|
/// Performs appropriate bounds checks.
|
|
|
|
pub fn read_wide_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, Vec<u16>> {
|
|
|
|
let size_2bytes = Size::from_bytes(2);
|
|
|
|
let align_2bytes = Align::from_bytes(2).unwrap();
|
|
|
|
// We need to read at least 2 bytes, so we *need* a ptr.
|
|
|
|
let mut ptr = self.force_ptr(ptr)?;
|
|
|
|
let allocation = self.get_raw(ptr.alloc_id)?;
|
|
|
|
let mut u16_seq = Vec::new();
|
|
|
|
|
|
|
|
loop {
|
|
|
|
ptr = self
|
|
|
|
.check_ptr_access(ptr.into(), size_2bytes, align_2bytes)?
|
|
|
|
.expect("cannot be a ZST");
|
|
|
|
let single_u16 = allocation.read_scalar(self, ptr, size_2bytes)?.to_u16()?;
|
|
|
|
if single_u16 != 0x0000 {
|
|
|
|
u16_seq.push(single_u16);
|
|
|
|
ptr = ptr.offset(size_2bytes, self)?;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(u16_seq)
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-10-20 10:02:35 +00:00
|
|
|
let src = src.into_iter();
|
2019-10-22 16:06:30 +00:00
|
|
|
let size = Size::from_bytes(src.size_hint().0 as u64);
|
|
|
|
// `write_bytes` checks that this lower bound matches the upper bound matches 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,
|
|
|
|
None => return Ok(()), // zero-sized access
|
|
|
|
};
|
|
|
|
let tcx = self.tcx.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.
|
2018-08-29 08:07:27 +00:00
|
|
|
// (`get_bytes_with_undef_and_ptr` below checks that there are no
|
|
|
|
// 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
|
|
|
|
2018-11-12 12:26:53 +00:00
|
|
|
let tcx = self.tcx.tcx;
|
|
|
|
|
2019-12-27 18:50:56 +00:00
|
|
|
// The bits have to be saved locally before writing to dest in case src and dest overlap.
|
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
|
|
|
|
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 =
|
|
|
|
self.get_raw(src.alloc_id)?.get_bytes_with_undef_and_ptr(&tcx, src, size)?.as_ptr();
|
|
|
|
let dest_bytes =
|
2019-12-27 11:56:52 +00:00
|
|
|
self.get_raw_mut(dest.alloc_id)?.get_bytes_mut(&tcx, dest, size * length)?;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-12-27 18:50:56 +00:00
|
|
|
// Prepare a copy of the undef mask.
|
|
|
|
let compressed = self.get_raw(src.alloc_id)?.compress_undef_range(src, size);
|
|
|
|
|
|
|
|
if compressed.all_bytes_undef() {
|
|
|
|
// Fast path: If all bytes are `undef` then there is nothing to copy. The target range
|
|
|
|
// is marked as undef but we otherwise omit changing the byte representation which may
|
|
|
|
// be arbitrary for undef bytes.
|
|
|
|
// This also avoids writing to the target bytes so that the backing allocation is never
|
|
|
|
// touched if the bytes stay undef for the whole interpreter execution. On contemporary
|
|
|
|
// operating system this can avoid physically allocating the page.
|
|
|
|
let dest_alloc = self.get_raw_mut(dest.alloc_id)?;
|
|
|
|
dest_alloc.mark_definedness(dest, size * length, false);
|
|
|
|
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 {
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
2016-03-05 06:48:23 +00:00
|
|
|
if src.alloc_id == dest.alloc_id {
|
2017-07-04 03:27:09 +00:00
|
|
|
if nonoverlapping {
|
2019-12-22 22:42:04 +00:00
|
|
|
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,
|
|
|
|
dest_bytes.offset((size.bytes() * i) as isize),
|
|
|
|
size.bytes() as usize,
|
|
|
|
);
|
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,
|
|
|
|
dest_bytes.offset((size.bytes() * i) as isize),
|
|
|
|
size.bytes() as usize,
|
|
|
|
);
|
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
|
|
|
|
self.get_raw_mut(dest.alloc_id)?.mark_compressed_undef_range(
|
|
|
|
&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),
|
2019-12-22 22:42:04 +00:00
|
|
|
Err(ptr) => Ok(M::ptr_to_int(&self, ptr)? as u128),
|
2019-06-12 17:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-27 04:25:08 +00:00
|
|
|
}
|