2018-05-18 14:06:20 +00:00
|
|
|
use std::collections::VecDeque;
|
2018-06-22 04:40:14 +00:00
|
|
|
use std::hash::{Hash, Hasher};
|
2018-04-26 07:18:19 +00:00
|
|
|
use std::ptr;
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2018-05-03 16:29:14 +00:00
|
|
|
use rustc::hir::def_id::DefId;
|
2018-02-06 17:33:59 +00:00
|
|
|
use rustc::ty::Instance;
|
2018-05-03 16:29:14 +00:00
|
|
|
use rustc::ty::ParamEnv;
|
2018-06-13 13:44:43 +00:00
|
|
|
use rustc::ty::query::TyCtxtAt;
|
2018-05-19 14:37:29 +00:00
|
|
|
use rustc::ty::layout::{self, Align, TargetDataLayout, Size};
|
2018-07-24 16:28:53 +00:00
|
|
|
use rustc::mir::interpret::{Pointer, AllocId, Allocation, AccessKind, Value, ScalarMaybeUndef,
|
2018-05-20 21:43:16 +00:00
|
|
|
EvalResult, Scalar, EvalErrorKind, GlobalId, AllocType};
|
2018-04-26 07:18:19 +00:00
|
|
|
pub use rustc::mir::interpret::{write_target_uint, write_target_int, read_target_uint};
|
2018-06-22 04:40:14 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxHashMap, FxHasher};
|
2018-06-08 02:47:26 +00:00
|
|
|
|
|
|
|
use syntax::ast::Mutability;
|
2017-12-12 16:14:49 +00:00
|
|
|
|
2017-12-14 10:36:28 +00:00
|
|
|
use super::{EvalContext, Machine};
|
2017-06-17 00:58:18 +00:00
|
|
|
|
2016-04-05 02:33:41 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Allocations and pointers
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-22 04:40:14 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
2017-08-09 12:53:22 +00:00
|
|
|
pub enum MemoryKind<T> {
|
2017-07-13 14:58:36 +00:00
|
|
|
/// Error if deallocated except during a stack pop
|
2017-07-12 12:51:47 +00:00
|
|
|
Stack,
|
2017-07-28 14:48:43 +00:00
|
|
|
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
|
|
|
|
Machine(T),
|
2016-04-05 02:33:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 00:58:18 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Top-level interpreter memory
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-06-22 19:36:54 +00:00
|
|
|
#[derive(Clone)]
|
2018-01-16 08:31:48 +00:00
|
|
|
pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
|
2017-07-21 15:25:30 +00:00
|
|
|
/// Additional data required by the Machine
|
|
|
|
pub data: M::MemoryData,
|
2017-05-26 05:38:07 +00:00
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
/// Helps guarantee that stack allocations aren't deallocated via `rust_deallocate`
|
2018-03-23 09:32:27 +00:00
|
|
|
alloc_kind: FxHashMap<AllocId, MemoryKind<M::MemoryKinds>>,
|
2017-12-06 08:25:29 +00:00
|
|
|
|
2017-02-10 21:35:45 +00:00
|
|
|
/// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations).
|
2018-03-23 09:32:27 +00:00
|
|
|
alloc_map: FxHashMap<AllocId, Allocation>,
|
2017-02-10 21:35:45 +00:00
|
|
|
|
2017-06-17 00:58:18 +00:00
|
|
|
/// The current stack frame. Used to check accesses against locks.
|
2017-12-14 10:36:28 +00:00
|
|
|
pub cur_frame: usize,
|
2017-12-06 08:25:29 +00:00
|
|
|
|
2018-02-06 17:33:59 +00:00
|
|
|
pub tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
2016-03-24 03:40:58 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 04:40:14 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M> Eq for Memory<'a, 'mir, 'tcx, M>
|
|
|
|
where M: Machine<'mir, 'tcx>,
|
|
|
|
'tcx: 'a + 'mir,
|
|
|
|
{}
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx, M> PartialEq for Memory<'a, 'mir, 'tcx, M>
|
|
|
|
where M: Machine<'mir, 'tcx>,
|
|
|
|
'tcx: 'a + 'mir,
|
|
|
|
{
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
let Memory {
|
|
|
|
data,
|
|
|
|
alloc_kind,
|
|
|
|
alloc_map,
|
|
|
|
cur_frame,
|
2018-06-22 20:31:25 +00:00
|
|
|
tcx: _,
|
2018-06-22 04:40:14 +00:00
|
|
|
} = self;
|
|
|
|
|
|
|
|
*data == other.data
|
|
|
|
&& *alloc_kind == other.alloc_kind
|
|
|
|
&& *alloc_map == other.alloc_map
|
|
|
|
&& *cur_frame == other.cur_frame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'mir, 'tcx, M> Hash for Memory<'a, 'mir, 'tcx, M>
|
|
|
|
where M: Machine<'mir, 'tcx>,
|
|
|
|
'tcx: 'a + 'mir,
|
|
|
|
{
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
let Memory {
|
|
|
|
data,
|
|
|
|
alloc_kind: _,
|
|
|
|
alloc_map: _,
|
|
|
|
cur_frame,
|
2018-06-22 20:31:25 +00:00
|
|
|
tcx: _,
|
2018-06-22 04:40:14 +00:00
|
|
|
} = self;
|
|
|
|
|
|
|
|
data.hash(state);
|
|
|
|
cur_frame.hash(state);
|
|
|
|
|
|
|
|
// We ignore some fields which don't change between evaluation steps.
|
|
|
|
|
|
|
|
// Since HashMaps which contain the same items may have different
|
|
|
|
// iteration orders, we use a commutative operation (in this case
|
|
|
|
// addition, but XOR would also work), to combine the hash of each
|
|
|
|
// `Allocation`.
|
|
|
|
self.allocations()
|
|
|
|
.map(|allocs| {
|
|
|
|
let mut h = FxHasher::default();
|
|
|
|
allocs.hash(&mut h);
|
|
|
|
h.finish()
|
|
|
|
})
|
|
|
|
.fold(0u64, |hash, x| hash.wrapping_add(x))
|
|
|
|
.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-02-06 17:33:59 +00:00
|
|
|
pub fn new(tcx: TyCtxtAt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
|
2016-09-22 13:22:00 +00:00
|
|
|
Memory {
|
2017-07-21 15:25:30 +00:00
|
|
|
data,
|
2018-03-23 09:32:27 +00:00
|
|
|
alloc_kind: FxHashMap::default(),
|
|
|
|
alloc_map: FxHashMap::default(),
|
2017-12-06 08:25:29 +00:00
|
|
|
tcx,
|
2017-06-17 00:58:18 +00:00
|
|
|
cur_frame: usize::max_value(),
|
2016-09-22 13:22:00 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn allocations<'x>(
|
|
|
|
&'x self,
|
2017-12-06 08:25:29 +00:00
|
|
|
) -> impl Iterator<Item = (AllocId, &'x Allocation)> {
|
2018-01-05 03:12:38 +00:00
|
|
|
self.alloc_map.iter().map(|(&id, alloc)| (id, alloc))
|
2016-06-30 09:29:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> Pointer {
|
2018-05-20 22:46:06 +00:00
|
|
|
self.tcx.alloc_map.lock().create_fn_alloc(instance).into()
|
2016-06-08 11:43:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
pub fn allocate_bytes(&mut self, bytes: &[u8]) -> Pointer {
|
2018-05-20 22:46:06 +00:00
|
|
|
self.tcx.allocate_bytes(bytes).into()
|
2017-02-10 21:35:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
/// kind is `None` for statics
|
2018-04-26 07:18:19 +00:00
|
|
|
pub fn allocate_value(
|
2017-07-28 14:48:43 +00:00
|
|
|
&mut self,
|
2018-04-26 07:18:19 +00:00
|
|
|
alloc: Allocation,
|
2018-06-08 02:47:26 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-04-26 07:18:19 +00:00
|
|
|
) -> EvalResult<'tcx, AllocId> {
|
2018-05-02 04:03:06 +00:00
|
|
|
let id = self.tcx.alloc_map.lock().reserve();
|
2017-12-14 10:36:28 +00:00
|
|
|
M::add_lock(self, id);
|
2018-06-08 02:47:26 +00:00
|
|
|
self.alloc_map.insert(id, alloc);
|
|
|
|
self.alloc_kind.insert(id, kind);
|
2018-04-26 07:18:19 +00:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// kind is `None` for statics
|
|
|
|
pub fn allocate(
|
|
|
|
&mut self,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2018-04-26 07:18:19 +00:00
|
|
|
align: Align,
|
2018-06-08 02:47:26 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-05-20 22:37:44 +00:00
|
|
|
) -> EvalResult<'tcx, Pointer> {
|
2018-05-20 22:46:06 +00:00
|
|
|
self.allocate_value(Allocation::undef(size, align), kind).map(Pointer::from)
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 14:48:43 +00:00
|
|
|
pub fn reallocate(
|
|
|
|
&mut self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
old_size: Size,
|
2017-12-17 06:47:22 +00:00
|
|
|
old_align: Align,
|
2018-05-19 14:37:29 +00:00
|
|
|
new_size: Size,
|
2017-12-17 06:47:22 +00:00
|
|
|
new_align: Align,
|
2017-08-09 12:53:22 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2018-05-20 22:37:44 +00:00
|
|
|
) -> EvalResult<'tcx, Pointer> {
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(ReallocateNonBasePtr);
|
2016-04-06 23:29:56 +00:00
|
|
|
}
|
2018-01-05 03:12:38 +00:00
|
|
|
if self.alloc_map.contains_key(&ptr.alloc_id) {
|
|
|
|
let alloc_kind = self.alloc_kind[&ptr.alloc_id];
|
2017-12-06 08:25:29 +00:00
|
|
|
if alloc_kind != kind {
|
2017-08-10 15:48:38 +00:00
|
|
|
return err!(ReallocatedWrongMemoryKind(
|
2017-12-06 08:25:29 +00:00
|
|
|
format!("{:?}", alloc_kind),
|
2017-08-10 15:48:38 +00:00
|
|
|
format!("{:?}", kind),
|
|
|
|
));
|
2017-07-12 12:51:47 +00:00
|
|
|
}
|
2016-11-17 13:48:34 +00:00
|
|
|
}
|
2016-04-06 23:29:56 +00:00
|
|
|
|
2017-07-10 20:34:54 +00:00
|
|
|
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc"
|
2018-06-08 02:47:26 +00:00
|
|
|
let new_ptr = self.allocate(new_size, new_align, kind)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
self.copy(
|
|
|
|
ptr.into(),
|
2017-12-17 06:47:22 +00:00
|
|
|
old_align,
|
2017-08-10 15:48:38 +00:00
|
|
|
new_ptr.into(),
|
2017-12-17 06:47:22 +00:00
|
|
|
new_align,
|
|
|
|
old_size.min(new_size),
|
2017-08-10 15:48:38 +00:00
|
|
|
/*nonoverlapping*/
|
|
|
|
true,
|
|
|
|
)?;
|
2017-07-12 12:51:47 +00:00
|
|
|
self.deallocate(ptr, Some((old_size, old_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-05-20 22:37:44 +00:00
|
|
|
pub fn deallocate_local(&mut self, ptr: Pointer) -> EvalResult<'tcx> {
|
2018-01-05 03:12:38 +00:00
|
|
|
match self.alloc_kind.get(&ptr.alloc_id).cloned() {
|
2017-12-06 08:25:29 +00:00
|
|
|
Some(MemoryKind::Stack) => self.deallocate(ptr, None, MemoryKind::Stack),
|
|
|
|
// Happens if the memory was interned into immutable memory
|
|
|
|
None => Ok(()),
|
|
|
|
other => bug!("local contained non-stack memory: {:?}", other),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 14:48:43 +00:00
|
|
|
pub fn deallocate(
|
|
|
|
&mut self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size_and_align: Option<(Size, Align)>,
|
2017-08-09 12:53:22 +00:00
|
|
|
kind: MemoryKind<M::MemoryKinds>,
|
2017-07-28 14:48:43 +00:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(DeallocateNonBasePtr);
|
2016-11-17 13:48:34 +00:00
|
|
|
}
|
2016-04-07 09:02:02 +00:00
|
|
|
|
2018-01-05 03:12:38 +00:00
|
|
|
let 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-05-02 04:03:06 +00:00
|
|
|
return match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
Some(AllocType::Function(..)) => err!(DeallocatedWrongMemoryKind(
|
|
|
|
"function".to_string(),
|
|
|
|
format!("{:?}", kind),
|
|
|
|
)),
|
|
|
|
Some(AllocType::Static(..)) |
|
|
|
|
Some(AllocType::Memory(..)) => err!(DeallocatedWrongMemoryKind(
|
|
|
|
"static".to_string(),
|
|
|
|
format!("{:?}", kind),
|
|
|
|
)),
|
|
|
|
None => err!(DoubleFree)
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 11:06:14 +00:00
|
|
|
};
|
|
|
|
|
2018-01-05 03:12:38 +00:00
|
|
|
let alloc_kind = self.alloc_kind.remove(&ptr.alloc_id).expect("alloc_map out of sync with alloc_kind");
|
2017-07-14 03:50:16 +00:00
|
|
|
|
2017-07-14 03:08:35 +00:00
|
|
|
// It is okay for us to still holds locks on deallocation -- for example, we could store data we own
|
|
|
|
// in a local, and the local could be deallocated (from StorageDead) before the function returns.
|
2017-07-14 03:50:16 +00:00
|
|
|
// However, we should check *something*. For now, we make sure that there is no conflicting write
|
|
|
|
// lock by another frame. We *have* to permit deallocation if we hold a read lock.
|
|
|
|
// TODO: Figure out the exact rules here.
|
2018-01-05 03:12:38 +00:00
|
|
|
M::free_lock(self, ptr.alloc_id, alloc.bytes.len() as u64)?;
|
2017-07-12 12:51:47 +00:00
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
if alloc_kind != kind {
|
2017-08-10 15:48:38 +00:00
|
|
|
return err!(DeallocatedWrongMemoryKind(
|
2017-12-06 08:25:29 +00:00
|
|
|
format!("{:?}", alloc_kind),
|
2017-08-10 15:48:38 +00:00
|
|
|
format!("{:?}", kind),
|
|
|
|
));
|
2017-07-12 12:51:47 +00:00
|
|
|
}
|
|
|
|
if let Some((size, align)) = size_and_align {
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
|
|
|
|
return err!(IncorrectAllocationInformation(size, Size::from_bytes(alloc.bytes.len() as u64), 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
|
|
|
|
2016-07-01 11:08:19 +00:00
|
|
|
debug!("deallocated : {}", ptr.alloc_id);
|
2016-04-07 09:02:02 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-23 07:59:16 +00:00
|
|
|
|
2018-05-19 14:37:29 +00:00
|
|
|
pub fn pointer_size(&self) -> Size {
|
|
|
|
self.tcx.data_layout.pointer_size
|
2016-06-23 07:59:16 +00:00
|
|
|
}
|
2016-06-23 13:16:25 +00:00
|
|
|
|
2018-02-10 23:56:24 +00:00
|
|
|
pub fn endianness(&self) -> layout::Endian {
|
2017-12-06 08:25:29 +00:00
|
|
|
self.tcx.data_layout.endian
|
2016-06-23 13:16:25 +00:00
|
|
|
}
|
2016-07-22 14:35:39 +00:00
|
|
|
|
2017-07-14 00:38:31 +00:00
|
|
|
/// Check that the pointer is aligned AND non-NULL.
|
2018-05-20 22:30:00 +00:00
|
|
|
pub fn check_align(&self, ptr: Scalar, required_align: Align) -> EvalResult<'tcx> {
|
2017-08-25 12:41:59 +00:00
|
|
|
// Check non-NULL/Undef, extract offset
|
2018-05-20 22:30:00 +00:00
|
|
|
let (offset, alloc_align) = match ptr {
|
2018-05-20 21:43:16 +00:00
|
|
|
Scalar::Ptr(ptr) => {
|
2017-07-03 12:16:11 +00:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
(ptr.offset.bytes(), alloc.align)
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2018-07-24 16:28:53 +00:00
|
|
|
Scalar::Bits { bits, size } => {
|
|
|
|
assert_eq!(size as u64, self.pointer_size().bytes());
|
2018-05-22 08:28:46 +00:00
|
|
|
// FIXME: what on earth does this line do? docs or fix needed!
|
|
|
|
let v = ((bits as u128) % (1 << self.pointer_size().bytes())) as u64;
|
2017-07-03 12:16:11 +00:00
|
|
|
if v == 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(InvalidNullPointerUsage);
|
2017-07-03 12:16:11 +00:00
|
|
|
}
|
2017-12-17 06:47:22 +00:00
|
|
|
// the base address if the "integer allocation" is 0 and hence always aligned
|
|
|
|
(v, required_align)
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2017-01-31 09:36:27 +00:00
|
|
|
};
|
2017-08-25 12:41:59 +00:00
|
|
|
// Check alignment
|
2017-12-17 06:47:22 +00:00
|
|
|
if alloc_align.abi() < required_align.abi() {
|
2017-08-25 12:41:59 +00:00
|
|
|
return err!(AlignmentCheckFailed {
|
2018-05-19 14:37:29 +00:00
|
|
|
has: alloc_align,
|
|
|
|
required: required_align,
|
2017-08-25 12:41:59 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-17 06:47:22 +00:00
|
|
|
if offset % required_align.abi() == 0 {
|
2016-07-22 14:35:39 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2018-05-19 14:37:29 +00:00
|
|
|
let has = offset % required_align.abi();
|
2017-08-02 14:59:01 +00:00
|
|
|
err!(AlignmentCheckFailed {
|
2018-05-19 14:37:29 +00:00
|
|
|
has: Align::from_bytes(has, has).unwrap(),
|
|
|
|
required: required_align,
|
2016-07-22 14:35:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-01-30 08:44:52 +00:00
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
pub fn check_bounds(&self, ptr: Pointer, access: bool) -> EvalResult<'tcx> {
|
2017-06-05 02:31:34 +00:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
|
|
|
let allocation_size = alloc.bytes.len() as u64;
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() > allocation_size {
|
2017-08-10 15:48:38 +00:00
|
|
|
return err!(PointerOutOfBounds {
|
|
|
|
ptr,
|
|
|
|
access,
|
2018-05-19 14:37:29 +00:00
|
|
|
allocation_size: Size::from_bytes(allocation_size),
|
2017-08-10 15:48:38 +00:00
|
|
|
});
|
2017-06-05 02:31:34 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
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
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2018-05-03 16:29:14 +00:00
|
|
|
fn const_eval_static(&self, def_id: DefId) -> EvalResult<'tcx, &'tcx Allocation> {
|
2018-05-29 00:38:18 +00:00
|
|
|
if self.tcx.is_foreign_item(def_id) {
|
|
|
|
return err!(ReadForeignStatic);
|
|
|
|
}
|
2018-05-03 16:29:14 +00:00
|
|
|
let instance = Instance::mono(self.tcx.tcx, def_id);
|
|
|
|
let gid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
};
|
2018-06-19 14:40:53 +00:00
|
|
|
self.tcx.const_eval(ParamEnv::reveal_all().and(gid)).map_err(|err| {
|
2018-06-03 01:01:06 +00:00
|
|
|
// no need to report anything, the const_eval call takes care of that for statics
|
|
|
|
assert!(self.tcx.is_static(def_id).is_some());
|
2018-06-19 14:40:53 +00:00
|
|
|
EvalErrorKind::ReferencedConstant(err).into()
|
2018-05-03 16:29:14 +00:00
|
|
|
}).map(|val| {
|
2018-06-25 18:53:02 +00:00
|
|
|
self.tcx.const_value_to_allocation(val)
|
2018-05-03 16:29:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
|
|
|
|
// normal alloc?
|
2018-01-05 03:12:38 +00:00
|
|
|
match self.alloc_map.get(&id) {
|
2018-06-08 02:47:26 +00:00
|
|
|
Some(alloc) => Ok(alloc),
|
2017-12-06 08:25:29 +00:00
|
|
|
// uninitialized static alloc?
|
2018-06-08 02:47:26 +00:00
|
|
|
None => {
|
|
|
|
// static alloc?
|
|
|
|
let alloc = self.tcx.alloc_map.lock().get(id);
|
|
|
|
match alloc {
|
|
|
|
Some(AllocType::Memory(mem)) => Ok(mem),
|
|
|
|
Some(AllocType::Function(..)) => {
|
|
|
|
Err(EvalErrorKind::DerefFunctionPointer.into())
|
|
|
|
}
|
|
|
|
Some(AllocType::Static(did)) => {
|
|
|
|
self.const_eval_static(did)
|
2018-05-03 16:29:14 +00:00
|
|
|
}
|
2018-06-08 02:47:26 +00:00
|
|
|
None => Err(EvalErrorKind::DanglingPointerDeref.into()),
|
|
|
|
}
|
2017-12-06 08:25:29 +00:00
|
|
|
},
|
2016-06-13 09:39:15 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
2017-08-10 15:48:38 +00:00
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
fn get_mut(
|
2017-08-10 15:48:38 +00:00
|
|
|
&mut self,
|
|
|
|
id: AllocId,
|
2017-12-06 08:25:29 +00:00
|
|
|
) -> EvalResult<'tcx, &mut Allocation> {
|
|
|
|
// normal alloc?
|
2018-01-05 03:12:38 +00:00
|
|
|
match self.alloc_map.get_mut(&id) {
|
2017-12-06 08:25:29 +00:00
|
|
|
Some(alloc) => Ok(alloc),
|
|
|
|
// uninitialized static alloc?
|
2018-06-08 02:47:26 +00:00
|
|
|
None => {
|
|
|
|
// no alloc or immutable alloc? produce an error
|
|
|
|
match self.tcx.alloc_map.lock().get(id) {
|
|
|
|
Some(AllocType::Memory(..)) |
|
|
|
|
Some(AllocType::Static(..)) => err!(ModifiedConstantMemory),
|
|
|
|
Some(AllocType::Function(..)) => err!(DerefFunctionPointer),
|
|
|
|
None => err!(DanglingPointerDeref),
|
|
|
|
}
|
2017-12-06 08:25:29 +00:00
|
|
|
},
|
2017-07-14 03:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
pub fn get_fn(&self, ptr: Pointer) -> EvalResult<'tcx, Instance<'tcx>> {
|
2018-05-19 14:37:29 +00:00
|
|
|
if ptr.offset.bytes() != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(InvalidFunctionPointer);
|
2017-06-22 04:45:51 +00:00
|
|
|
}
|
|
|
|
debug!("reading fn ptr: {}", ptr.alloc_id);
|
2018-05-02 04:03:06 +00:00
|
|
|
match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
|
|
|
|
Some(AllocType::Function(instance)) => Ok(instance),
|
|
|
|
_ => Err(EvalErrorKind::ExecuteMemory.into()),
|
|
|
|
}
|
2016-06-08 11:43:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-03 15:50:31 +00:00
|
|
|
pub fn get_alloc_kind(&self, id: AllocId) -> Option<MemoryKind<M::MemoryKinds>> {
|
|
|
|
self.alloc_kind.get(&id).cloned()
|
|
|
|
}
|
|
|
|
|
2016-12-08 06:00:46 +00:00
|
|
|
/// For debugging, print an allocation and all allocations it points to, recursively.
|
|
|
|
pub fn dump_alloc(&self, id: AllocId) {
|
2018-04-26 05:35:24 +00:00
|
|
|
if !log_enabled!(::log::Level::Trace) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-08 06:00:46 +00:00
|
|
|
self.dump_allocs(vec![id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For debugging, print a list of allocations and all allocations they point to, recursively.
|
|
|
|
pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
|
2018-04-26 05:35:24 +00:00
|
|
|
if !log_enabled!(::log::Level::Trace) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-22 11:01:08 +00:00
|
|
|
use std::fmt::Write;
|
2016-12-08 06:00:46 +00:00
|
|
|
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() {
|
2016-09-22 11:01:08 +00:00
|
|
|
let mut msg = format!("Alloc {:<5} ", format!("{}:", id));
|
|
|
|
let prefix_len = msg.len();
|
2016-04-06 09:45:06 +00:00
|
|
|
let mut relocations = vec![];
|
|
|
|
|
2017-12-06 08:25:29 +00:00
|
|
|
let (alloc, immutable) =
|
|
|
|
// normal alloc?
|
2018-01-05 03:12:38 +00:00
|
|
|
match self.alloc_map.get(&id) {
|
|
|
|
Some(a) => (a, match self.alloc_kind[&id] {
|
2017-12-06 08:25:29 +00:00
|
|
|
MemoryKind::Stack => " (stack)".to_owned(),
|
|
|
|
MemoryKind::Machine(m) => format!(" ({:?})", m),
|
|
|
|
}),
|
2018-06-08 02:47:26 +00:00
|
|
|
None => {
|
|
|
|
// static alloc?
|
|
|
|
match self.tcx.alloc_map.lock().get(id) {
|
|
|
|
Some(AllocType::Memory(a)) => (a, "(immutable)".to_owned()),
|
|
|
|
Some(AllocType::Function(func)) => {
|
|
|
|
trace!("{} {}", msg, func);
|
|
|
|
continue;
|
2018-01-16 08:31:48 +00:00
|
|
|
}
|
2018-06-08 02:47:26 +00:00
|
|
|
Some(AllocType::Static(did)) => {
|
|
|
|
trace!("{} {:?}", msg, did);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
trace!("{} (deallocated)", msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 08:25:29 +00:00
|
|
|
},
|
2018-01-16 08:31:48 +00:00
|
|
|
};
|
2016-04-07 09:07:57 +00:00
|
|
|
|
2016-11-18 11:55:14 +00:00
|
|
|
for i in 0..(alloc.bytes.len() as u64) {
|
2018-05-19 14:37:29 +00:00
|
|
|
let i = Size::from_bytes(i);
|
2016-04-06 09:45:06 +00:00
|
|
|
if let Some(&target_id) = alloc.relocations.get(&i) {
|
2017-02-06 17:26:01 +00:00
|
|
|
if allocs_seen.insert(target_id) {
|
2016-04-06 09:45:06 +00:00
|
|
|
allocs_to_print.push_back(target_id);
|
|
|
|
}
|
2016-04-10 01:31:53 +00:00
|
|
|
relocations.push((i, target_id));
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
2018-05-19 14:37:29 +00:00
|
|
|
if alloc.undef_mask.is_range_defined(i, i + Size::from_bytes(1)) {
|
2016-11-18 11:55:14 +00:00
|
|
|
// this `as usize` is fine, since `i` came from a `usize`
|
2018-05-19 14:37:29 +00:00
|
|
|
write!(msg, "{:02x} ", alloc.bytes[i.bytes() as usize]).unwrap();
|
2016-04-06 09:45:06 +00:00
|
|
|
} else {
|
2016-09-22 11:01:08 +00:00
|
|
|
msg.push_str("__ ");
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-19 10:10:51 +00:00
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
trace!(
|
|
|
|
"{}({} bytes, alignment {}){}",
|
|
|
|
msg,
|
|
|
|
alloc.bytes.len(),
|
2017-12-17 06:47:22 +00:00
|
|
|
alloc.align.abi(),
|
2017-08-10 15:48:38 +00:00
|
|
|
immutable
|
|
|
|
);
|
2016-04-06 09:45:06 +00:00
|
|
|
|
|
|
|
if !relocations.is_empty() {
|
2016-09-22 11:01:08 +00:00
|
|
|
msg.clear();
|
|
|
|
write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
|
2018-05-20 12:14:39 +00:00
|
|
|
let mut pos = Size::ZERO;
|
2018-05-19 14:37:29 +00:00
|
|
|
let relocation_width = (self.pointer_size().bytes() - 1) * 3;
|
2016-04-06 09:45:06 +00:00
|
|
|
for (i, target_id) in relocations {
|
2016-11-18 11:55:14 +00:00
|
|
|
// this `as usize` is fine, since we can't print more chars than `usize::MAX`
|
2018-05-19 14:37:29 +00:00
|
|
|
write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
|
2017-06-23 10:55:49 +00:00
|
|
|
let target = format!("({})", target_id);
|
2016-11-18 11:55:14 +00:00
|
|
|
// 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();
|
2016-06-23 08:00:31 +00:00
|
|
|
pos = i + self.pointer_size();
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
2016-09-22 11:01:08 +00:00
|
|
|
trace!("{}", msg);
|
2016-04-06 09:45:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-14 14:35:13 +00:00
|
|
|
|
|
|
|
pub fn leak_report(&self) -> usize {
|
|
|
|
trace!("### LEAK REPORT ###");
|
|
|
|
let leaks: Vec<_> = self.alloc_map
|
2017-12-06 08:25:29 +00:00
|
|
|
.keys()
|
2018-01-16 08:31:48 +00:00
|
|
|
.cloned()
|
2017-02-14 14:35:13 +00:00
|
|
|
.collect();
|
|
|
|
let n = leaks.len();
|
|
|
|
self.dump_allocs(leaks);
|
|
|
|
n
|
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-04-06 09:45:06 +00:00
|
|
|
|
2016-06-23 07:40:01 +00:00
|
|
|
/// Byte accessors
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2017-08-10 15:48:38 +00:00
|
|
|
fn get_bytes_unchecked(
|
|
|
|
&self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-12-17 06:47:22 +00:00
|
|
|
align: Align,
|
2017-08-10 15:48:38 +00:00
|
|
|
) -> EvalResult<'tcx, &[u8]> {
|
2017-07-20 20:20:33 +00:00
|
|
|
// Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
|
2017-12-17 06:47:22 +00:00
|
|
|
self.check_align(ptr.into(), align)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() == 0 {
|
2017-07-20 20:20:33 +00:00
|
|
|
return Ok(&[]);
|
|
|
|
}
|
2017-12-14 10:36:28 +00:00
|
|
|
M::check_locks(self, ptr, size, AccessKind::Read)?;
|
2017-07-22 18:28:14 +00:00
|
|
|
self.check_bounds(ptr.offset(size, self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
|
2016-05-10 00:52:44 +00:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
|
|
|
Ok(&alloc.bytes[offset..offset + size.bytes() as usize])
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
fn get_bytes_unchecked_mut(
|
|
|
|
&mut self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-12-17 06:47:22 +00:00
|
|
|
align: Align,
|
2017-08-10 15:48:38 +00:00
|
|
|
) -> EvalResult<'tcx, &mut [u8]> {
|
2017-07-20 20:20:33 +00:00
|
|
|
// Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
|
2017-12-17 06:47:22 +00:00
|
|
|
self.check_align(ptr.into(), align)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() == 0 {
|
2017-07-20 20:20:33 +00:00
|
|
|
return Ok(&mut []);
|
|
|
|
}
|
2017-12-14 10:36:28 +00:00
|
|
|
M::check_locks(self, ptr, size, AccessKind::Write)?;
|
2017-12-06 08:25:29 +00:00
|
|
|
self.check_bounds(ptr.offset(size, &*self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
|
2016-05-10 00:52:44 +00:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
|
|
|
Ok(&mut alloc.bytes[offset..offset + size.bytes() as usize])
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
fn get_bytes(&self, ptr: Pointer, size: Size, align: Align) -> EvalResult<'tcx, &[u8]> {
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_ne!(size.bytes(), 0);
|
2018-05-18 14:06:20 +00:00
|
|
|
if self.relocations(ptr, size)?.len() != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(ReadPointerAsBytes);
|
2016-03-21 11:27:34 +00:00
|
|
|
}
|
2016-05-10 00:52:44 +00:00
|
|
|
self.check_defined(ptr, size)?;
|
2017-01-31 09:36:46 +00:00
|
|
|
self.get_bytes_unchecked(ptr, size, align)
|
2016-03-24 01:44:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
fn get_bytes_mut(
|
|
|
|
&mut self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-12-17 06:47:22 +00:00
|
|
|
align: Align,
|
2017-08-10 15:48:38 +00:00
|
|
|
) -> EvalResult<'tcx, &mut [u8]> {
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_ne!(size.bytes(), 0);
|
2016-05-10 00:52:44 +00:00
|
|
|
self.clear_relocations(ptr, size)?;
|
2017-07-04 12:33:15 +00:00
|
|
|
self.mark_definedness(ptr.into(), size, true)?;
|
2017-01-31 09:36:46 +00:00
|
|
|
self.get_bytes_unchecked_mut(ptr, size, align)
|
2016-03-21 11:27:34 +00:00
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-03-21 11:27:34 +00:00
|
|
|
|
2016-06-23 07:40:01 +00:00
|
|
|
/// Reading and writing
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2017-02-14 09:59:38 +00:00
|
|
|
/// mark an allocation pointed to by a static as static and initialized
|
2017-08-25 12:41:59 +00:00
|
|
|
fn mark_inner_allocation_initialized(
|
2017-08-10 15:48:38 +00:00
|
|
|
&mut self,
|
|
|
|
alloc: AllocId,
|
|
|
|
mutability: Mutability,
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-01-05 03:12:38 +00:00
|
|
|
match self.alloc_kind.get(&alloc) {
|
2018-01-16 08:31:48 +00:00
|
|
|
// do not go into statics
|
2018-01-22 09:21:49 +00:00
|
|
|
None => Ok(()),
|
2017-12-06 08:25:29 +00:00
|
|
|
// just locals and machine allocs
|
2018-01-16 09:16:38 +00:00
|
|
|
Some(_) => self.mark_static_initialized(alloc, mutability),
|
2017-02-14 09:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 15:27:28 +00:00
|
|
|
/// mark an allocation as static and initialized, either mutable or not
|
2018-01-16 09:16:38 +00:00
|
|
|
pub fn mark_static_initialized(
|
2017-08-10 15:48:38 +00:00
|
|
|
&mut self,
|
|
|
|
alloc_id: AllocId,
|
|
|
|
mutability: Mutability,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
trace!(
|
2018-01-16 09:16:38 +00:00
|
|
|
"mark_static_initialized {:?}, mutability: {:?}",
|
2017-08-10 15:48:38 +00:00
|
|
|
alloc_id,
|
|
|
|
mutability
|
|
|
|
);
|
2018-01-16 08:31:48 +00:00
|
|
|
// The machine handled it
|
|
|
|
if M::mark_static_initialized(self, alloc_id, mutability)? {
|
|
|
|
return Ok(())
|
2017-12-06 08:25:29 +00:00
|
|
|
}
|
2018-01-16 08:31:48 +00:00
|
|
|
let alloc = self.alloc_map.remove(&alloc_id);
|
|
|
|
match self.alloc_kind.remove(&alloc_id) {
|
|
|
|
None => {},
|
|
|
|
Some(MemoryKind::Machine(_)) => bug!("machine didn't handle machine alloc"),
|
|
|
|
Some(MemoryKind::Stack) => {},
|
2017-12-06 08:25:29 +00:00
|
|
|
}
|
2018-06-08 02:47:26 +00:00
|
|
|
if let Some(mut alloc) = alloc {
|
2018-01-16 08:31:48 +00:00
|
|
|
// ensure llvm knows not to put this into immutable memroy
|
2018-01-26 13:28:58 +00:00
|
|
|
alloc.runtime_mutability = mutability;
|
2018-01-16 08:31:48 +00:00
|
|
|
let alloc = self.tcx.intern_const_alloc(alloc);
|
2018-05-02 04:03:06 +00:00
|
|
|
self.tcx.alloc_map.lock().set_id_memory(alloc_id, alloc);
|
2018-01-16 08:31:48 +00:00
|
|
|
// recurse into inner allocations
|
|
|
|
for &alloc in alloc.relocations.values() {
|
|
|
|
self.mark_inner_allocation_initialized(alloc, mutability)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2018-01-22 09:21:49 +00:00
|
|
|
} else {
|
|
|
|
bug!("no allocation found for {:?}", alloc_id);
|
2016-09-19 10:10:18 +00:00
|
|
|
}
|
2016-09-09 15:44:04 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-08-10 15:48:38 +00:00
|
|
|
pub fn copy(
|
|
|
|
&mut self,
|
2018-05-20 22:30:00 +00:00
|
|
|
src: Scalar,
|
2017-12-17 06:47:22 +00:00
|
|
|
src_align: Align,
|
2018-05-20 22:30:00 +00:00
|
|
|
dest: Scalar,
|
2017-12-17 06:47:22 +00:00
|
|
|
dest_align: Align,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-08-10 15:48:38 +00:00
|
|
|
nonoverlapping: bool,
|
2018-06-27 02:59:10 +00:00
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
self.copy_repeatedly(src, src_align, dest, dest_align, size, 1, nonoverlapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn copy_repeatedly(
|
|
|
|
&mut self,
|
|
|
|
src: Scalar,
|
|
|
|
src_align: Align,
|
|
|
|
dest: Scalar,
|
|
|
|
dest_align: Align,
|
|
|
|
size: Size,
|
|
|
|
length: u64,
|
|
|
|
nonoverlapping: bool,
|
2017-08-10 15:48:38 +00:00
|
|
|
) -> EvalResult<'tcx> {
|
2017-08-25 12:41:59 +00:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be aligned
|
2017-12-17 06:47:22 +00:00
|
|
|
self.check_align(src, src_align)?;
|
|
|
|
self.check_align(dest, dest_align)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() == 0 {
|
2016-09-22 13:22:00 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2017-06-20 12:26:50 +00:00
|
|
|
let src = src.to_ptr()?;
|
|
|
|
let dest = dest.to_ptr()?;
|
2016-05-10 00:52:44 +00:00
|
|
|
self.check_relocation_edges(src, size)?;
|
2016-03-21 11:27:34 +00:00
|
|
|
|
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-06-30 04:44:58 +00:00
|
|
|
let relocations = {
|
|
|
|
let relocations = self.relocations(src, size)?;
|
|
|
|
let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
|
|
|
|
for i in 0..length {
|
|
|
|
new_relocations.extend(
|
|
|
|
relocations
|
|
|
|
.iter()
|
|
|
|
.map(|&(offset, alloc_id)| {
|
|
|
|
(offset + dest.offset - src.offset + (i * size * relocations.len() as u64), alloc_id)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_relocations
|
|
|
|
};
|
2017-08-28 12:08:55 +00:00
|
|
|
|
2017-12-17 06:47:22 +00:00
|
|
|
let src_bytes = self.get_bytes_unchecked(src, size, src_align)?.as_ptr();
|
2018-06-27 02:59:10 +00:00
|
|
|
let dest_bytes = self.get_bytes_mut(dest, size * length, dest_align)?.as_mut_ptr();
|
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.
|
|
|
|
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 {
|
|
|
|
if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
|
2017-08-10 15:48:38 +00:00
|
|
|
(dest.offset <= src.offset && dest.offset + size > src.offset)
|
|
|
|
{
|
|
|
|
return err!(Intrinsic(
|
2018-07-28 12:40:32 +00:00
|
|
|
"copy_nonoverlapping called on overlapping ranges".to_string(),
|
2017-08-10 15:48:38 +00:00
|
|
|
));
|
2017-07-04 03:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 02:59:10 +00:00
|
|
|
|
|
|
|
for i in 0..length {
|
|
|
|
ptr::copy(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize);
|
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
} else {
|
2018-06-27 02:59:10 +00:00
|
|
|
for i in 0..length {
|
|
|
|
ptr::copy_nonoverlapping(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize);
|
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 18:23:41 +00:00
|
|
|
self.copy_undef_mask(src, dest, size, length)?;
|
2017-08-28 12:08:55 +00:00
|
|
|
// copy back the relocations
|
2018-05-18 14:06:20 +00:00
|
|
|
self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations);
|
2016-04-06 10:08:52 +00:00
|
|
|
|
|
|
|
Ok(())
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
pub fn read_c_str(&self, ptr: Pointer) -> EvalResult<'tcx, &[u8]> {
|
2016-12-17 01:10:16 +00:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
|
|
|
|
let offset = ptr.offset.bytes() as usize;
|
2016-12-17 01:10:16 +00:00
|
|
|
match alloc.bytes[offset..].iter().position(|&c| c == 0) {
|
|
|
|
Some(size) => {
|
2018-05-19 14:37:29 +00:00
|
|
|
let p1 = Size::from_bytes((size + 1) as u64);
|
2018-05-18 14:06:20 +00:00
|
|
|
if self.relocations(ptr, p1)?.len() != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(ReadPointerAsBytes);
|
2016-12-17 01:10:16 +00:00
|
|
|
}
|
2018-05-19 14:37:29 +00:00
|
|
|
self.check_defined(ptr, p1)?;
|
|
|
|
M::check_locks(self, ptr, p1, AccessKind::Read)?;
|
2016-12-17 01:10:16 +00:00
|
|
|
Ok(&alloc.bytes[offset..offset + size])
|
2017-08-10 15:48:38 +00:00
|
|
|
}
|
2017-08-02 14:59:01 +00:00
|
|
|
None => err!(UnterminatedCString(ptr)),
|
2016-12-17 01:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:30:00 +00:00
|
|
|
pub fn read_bytes(&self, ptr: Scalar, size: Size) -> EvalResult<'tcx, &[u8]> {
|
2017-08-25 12:41:59 +00:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 06:47:22 +00:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
|
|
|
self.check_align(ptr, align)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() == 0 {
|
2017-06-20 14:26:53 +00:00
|
|
|
return Ok(&[]);
|
|
|
|
}
|
2017-12-17 06:47:22 +00:00
|
|
|
self.get_bytes(ptr.to_ptr()?, size, align)
|
2016-04-15 09:16:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:30:00 +00:00
|
|
|
pub fn write_bytes(&mut self, ptr: Scalar, src: &[u8]) -> EvalResult<'tcx> {
|
2017-08-25 12:41:59 +00:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 06:47:22 +00:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
|
|
|
self.check_align(ptr, align)?;
|
2017-06-20 14:26:53 +00:00
|
|
|
if src.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2018-05-19 14:37:29 +00:00
|
|
|
let bytes = self.get_bytes_mut(ptr.to_ptr()?, Size::from_bytes(src.len() as u64), align)?;
|
2016-04-07 11:56:07 +00:00
|
|
|
bytes.clone_from_slice(src);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:30:00 +00:00
|
|
|
pub fn write_repeat(&mut self, ptr: Scalar, val: u8, count: Size) -> EvalResult<'tcx> {
|
2017-08-25 12:41:59 +00:00
|
|
|
// Empty accesses don't need to be valid pointers, but they should still be non-NULL
|
2017-12-17 06:47:22 +00:00
|
|
|
let align = Align::from_bytes(1, 1).unwrap();
|
|
|
|
self.check_align(ptr, align)?;
|
2018-05-19 14:37:29 +00:00
|
|
|
if count.bytes() == 0 {
|
2017-06-20 14:26:53 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2017-12-17 06:47:22 +00:00
|
|
|
let bytes = self.get_bytes_mut(ptr.to_ptr()?, count, align)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
for b in bytes {
|
|
|
|
*b = val;
|
|
|
|
}
|
2016-04-07 11:56:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
pub fn read_scalar(&self, ptr: Pointer, ptr_align: Align, size: Size) -> EvalResult<'tcx, ScalarMaybeUndef> {
|
2017-07-13 02:29:16 +00:00
|
|
|
self.check_relocation_edges(ptr, size)?; // Make sure we don't read part of a pointer as a pointer
|
2018-02-10 23:56:24 +00:00
|
|
|
let endianness = self.endianness();
|
2017-12-17 06:47:22 +00:00
|
|
|
let bytes = self.get_bytes_unchecked(ptr, size, ptr_align.min(self.int_align(size)))?;
|
2017-07-20 03:24:09 +00:00
|
|
|
// Undef check happens *after* we established that the alignment is correct.
|
|
|
|
// We must not return Ok() for unaligned pointers!
|
|
|
|
if self.check_defined(ptr, size).is_err() {
|
2018-06-04 16:32:06 +00:00
|
|
|
// this inflates undefined bytes to the entire scalar, even if only a few bytes are undefined
|
2018-07-24 16:28:53 +00:00
|
|
|
return Ok(ScalarMaybeUndef::Undef);
|
2017-07-20 03:24:09 +00:00
|
|
|
}
|
2017-08-25 14:20:13 +00:00
|
|
|
// Now we do the actual reading
|
2018-05-22 08:28:46 +00:00
|
|
|
let bits = read_target_uint(endianness, bytes).unwrap();
|
2017-08-25 14:20:13 +00:00
|
|
|
// See if we got a pointer
|
|
|
|
if size != self.pointer_size() {
|
2018-05-18 14:06:20 +00:00
|
|
|
if self.relocations(ptr, size)?.len() != 0 {
|
2017-08-25 14:20:13 +00:00
|
|
|
return err!(ReadPointerAsBytes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
|
|
|
match alloc.relocations.get(&ptr.offset) {
|
2018-07-24 16:28:53 +00:00
|
|
|
Some(&alloc_id) => return Ok(ScalarMaybeUndef::Scalar(Pointer::new(alloc_id, Size::from_bytes(bits as u64)).into())),
|
2017-08-25 14:20:13 +00:00
|
|
|
None => {},
|
|
|
|
}
|
2016-03-17 13:24:10 +00:00
|
|
|
}
|
2018-05-22 08:28:46 +00:00
|
|
|
// We don't. Just return the bits.
|
2018-07-24 16:28:53 +00:00
|
|
|
Ok(ScalarMaybeUndef::Scalar(Scalar::Bits {
|
2018-05-22 08:28:46 +00:00
|
|
|
bits,
|
2018-07-24 16:28:53 +00:00
|
|
|
size: size.bytes() as u8,
|
|
|
|
}))
|
2017-08-25 14:20:13 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
pub fn read_ptr_sized(&self, ptr: Pointer, ptr_align: Align) -> EvalResult<'tcx, ScalarMaybeUndef> {
|
2018-05-22 17:30:16 +00:00
|
|
|
self.read_scalar(ptr, ptr_align, self.pointer_size())
|
2016-03-13 20:36:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
pub fn write_scalar(&mut self, ptr: Scalar, ptr_align: Align, val: ScalarMaybeUndef, type_size: Size, signed: bool) -> EvalResult<'tcx> {
|
2018-02-10 23:56:24 +00:00
|
|
|
let endianness = self.endianness();
|
2016-03-13 20:36:25 +00:00
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
let val = match val {
|
|
|
|
ScalarMaybeUndef::Scalar(scalar) => scalar,
|
|
|
|
ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
|
|
|
|
};
|
|
|
|
|
2017-08-25 16:25:05 +00:00
|
|
|
let bytes = match val {
|
2018-05-20 21:43:16 +00:00
|
|
|
Scalar::Ptr(val) => {
|
2018-07-24 16:28:53 +00:00
|
|
|
assert_eq!(type_size, self.pointer_size());
|
2018-05-19 14:37:29 +00:00
|
|
|
val.offset.bytes() as u128
|
2016-12-17 09:36:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
Scalar::Bits { size: 0, .. } => {
|
|
|
|
// nothing to do for ZSTs
|
|
|
|
assert_eq!(type_size.bytes(), 0);
|
2017-08-25 16:25:05 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
2018-07-24 16:28:53 +00:00
|
|
|
|
|
|
|
Scalar::Bits { bits, size } => {
|
|
|
|
assert_eq!(size as u64, type_size.bytes());
|
|
|
|
bits
|
|
|
|
},
|
2017-08-25 16:25:05 +00:00
|
|
|
};
|
|
|
|
|
2018-04-13 14:05:54 +00:00
|
|
|
let ptr = ptr.to_ptr()?;
|
|
|
|
|
2017-08-25 16:25:05 +00:00
|
|
|
{
|
2018-07-24 16:28:53 +00:00
|
|
|
let align = self.int_align(type_size);
|
|
|
|
let dst = self.get_bytes_mut(ptr, type_size, ptr_align.min(align))?;
|
2017-08-25 16:25:05 +00:00
|
|
|
if signed {
|
2018-02-10 23:56:24 +00:00
|
|
|
write_target_int(endianness, dst, bytes as i128).unwrap();
|
2017-08-25 16:25:05 +00:00
|
|
|
} else {
|
2018-02-10 23:56:24 +00:00
|
|
|
write_target_uint(endianness, dst, bytes).unwrap();
|
2017-08-25 16:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we have to also write a relocation
|
|
|
|
match val {
|
2018-05-20 21:43:16 +00:00
|
|
|
Scalar::Ptr(val) => {
|
2017-08-25 16:25:05 +00:00
|
|
|
self.get_mut(ptr.alloc_id)?.relocations.insert(
|
|
|
|
ptr.offset,
|
|
|
|
val.alloc_id,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => {}
|
2016-12-17 09:36:02 +00:00
|
|
|
}
|
2017-08-25 16:25:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-07-24 16:28:53 +00:00
|
|
|
pub fn write_ptr_sized_unsigned(&mut self, ptr: Pointer, ptr_align: Align, val: ScalarMaybeUndef) -> EvalResult<'tcx> {
|
2017-08-25 16:25:05 +00:00
|
|
|
let ptr_size = self.pointer_size();
|
2018-05-22 17:30:16 +00:00
|
|
|
self.write_scalar(ptr.into(), ptr_align, val, ptr_size, false)
|
2016-03-07 10:44:03 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 14:37:29 +00:00
|
|
|
fn int_align(&self, size: Size) -> Align {
|
2017-08-25 14:20:13 +00:00
|
|
|
// We assume pointer-sized integers have the same alignment as pointers.
|
2017-08-28 13:58:58 +00:00
|
|
|
// We also assume signed and unsigned integers of the same size have the same alignment.
|
2018-05-19 14:37:29 +00:00
|
|
|
let ity = match size.bytes() {
|
2017-12-17 06:47:22 +00:00
|
|
|
1 => layout::I8,
|
|
|
|
2 => layout::I16,
|
|
|
|
4 => layout::I32,
|
|
|
|
8 => layout::I64,
|
|
|
|
16 => layout::I128,
|
2018-05-19 14:37:29 +00:00
|
|
|
_ => bug!("bad integer size: {}", size.bytes()),
|
2017-12-17 06:47:22 +00:00
|
|
|
};
|
|
|
|
ity.align(self)
|
2016-07-06 09:12:44 +00:00
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-03-05 06:48:23 +00:00
|
|
|
|
2016-06-23 07:40:01 +00:00
|
|
|
/// Relocations
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2017-08-10 15:48:38 +00:00
|
|
|
fn relocations(
|
|
|
|
&self,
|
2018-05-20 22:37:44 +00:00
|
|
|
ptr: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2018-05-18 14:06:20 +00:00
|
|
|
) -> EvalResult<'tcx, &[(Size, AllocId)]> {
|
2018-05-19 14:37:29 +00:00
|
|
|
let start = ptr.offset.bytes().saturating_sub(self.pointer_size().bytes() - 1);
|
2016-06-13 09:24:01 +00:00
|
|
|
let end = ptr.offset + size;
|
2018-05-19 14:37:29 +00:00
|
|
|
Ok(self.get(ptr.alloc_id)?.relocations.range(Size::from_bytes(start)..end))
|
2016-03-05 06:48:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
fn clear_relocations(&mut self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2016-03-27 06:29:02 +00:00
|
|
|
// Find the start and end of the given range and its outermost relocations.
|
2018-05-18 14:06:20 +00:00
|
|
|
let (first, last) = {
|
|
|
|
// Find all relocations overlapping the given range.
|
|
|
|
let relocations = self.relocations(ptr, size)?;
|
|
|
|
if relocations.is_empty() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
(relocations.first().unwrap().0,
|
|
|
|
relocations.last().unwrap().0 + self.pointer_size())
|
|
|
|
};
|
2016-03-27 06:29:02 +00:00
|
|
|
let start = ptr.offset;
|
|
|
|
let end = start + size;
|
|
|
|
|
2016-05-10 00:52:44 +00:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-03-27 06:29:02 +00:00
|
|
|
|
|
|
|
// Mark parts of the outermost relocations as undefined if they partially fall outside the
|
|
|
|
// given range.
|
2017-08-10 15:48:38 +00:00
|
|
|
if first < start {
|
|
|
|
alloc.undef_mask.set_range(first, start, false);
|
|
|
|
}
|
|
|
|
if last > end {
|
|
|
|
alloc.undef_mask.set_range(end, last, false);
|
|
|
|
}
|
2016-03-27 06:29:02 +00:00
|
|
|
|
|
|
|
// Forget all the relocations.
|
2018-05-30 13:59:42 +00:00
|
|
|
alloc.relocations.remove_range(first..last);
|
2016-03-27 06:29:02 +00:00
|
|
|
|
2016-03-24 03:40:58 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
fn check_relocation_edges(&self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2018-05-20 12:14:39 +00:00
|
|
|
let overlapping_start = self.relocations(ptr, Size::ZERO)?.len();
|
|
|
|
let overlapping_end = self.relocations(ptr.offset(size, self)?, Size::ZERO)?.len();
|
2016-03-24 03:40:58 +00:00
|
|
|
if overlapping_start + overlapping_end != 0 {
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(ReadPointerAsBytes);
|
2016-03-24 03:40:58 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-23 07:40:01 +00:00
|
|
|
}
|
2016-03-27 04:25:08 +00:00
|
|
|
|
2016-06-23 07:40:01 +00:00
|
|
|
/// Undefined bytes
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
|
2016-05-10 02:08:37 +00:00
|
|
|
// FIXME(solson): This is a very naive, slow version.
|
2017-08-10 15:48:38 +00:00
|
|
|
fn copy_undef_mask(
|
|
|
|
&mut self,
|
2018-05-20 22:37:44 +00:00
|
|
|
src: Pointer,
|
|
|
|
dest: Pointer,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2018-06-30 18:23:41 +00:00
|
|
|
repeat: u64,
|
2017-08-10 15:48:38 +00:00
|
|
|
) -> EvalResult<'tcx> {
|
2016-04-06 10:08:52 +00:00
|
|
|
// The bits have to be saved locally before writing to dest in case src and dest overlap.
|
2018-05-19 14:37:29 +00:00
|
|
|
assert_eq!(size.bytes() as usize as u64, size.bytes());
|
2018-06-30 00:22:35 +00:00
|
|
|
|
2018-06-30 02:26:15 +00:00
|
|
|
let undef_mask = self.get(src.alloc_id)?.undef_mask.clone();
|
|
|
|
let dest_allocation = self.get_mut(dest.alloc_id)?;
|
2018-06-30 00:22:35 +00:00
|
|
|
|
2018-05-19 14:37:29 +00:00
|
|
|
for i in 0..size.bytes() {
|
2018-06-30 02:26:15 +00:00
|
|
|
let defined = undef_mask.get(src.offset + Size::from_bytes(i));
|
2018-06-22 04:40:14 +00:00
|
|
|
|
2018-06-30 18:23:41 +00:00
|
|
|
for j in 0..repeat {
|
|
|
|
dest_allocation.undef_mask.set(
|
|
|
|
dest.offset + Size::from_bytes(i + (size.bytes() * j)),
|
|
|
|
defined
|
|
|
|
);
|
|
|
|
}
|
2016-04-06 10:08:52 +00:00
|
|
|
}
|
2018-06-30 00:22:35 +00:00
|
|
|
|
2016-04-06 10:08:52 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:37:44 +00:00
|
|
|
fn check_defined(&self, ptr: Pointer, size: Size) -> EvalResult<'tcx> {
|
2016-05-10 00:52:44 +00:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
if !alloc.undef_mask.is_range_defined(
|
|
|
|
ptr.offset,
|
|
|
|
ptr.offset + size,
|
|
|
|
)
|
|
|
|
{
|
2017-08-02 14:59:01 +00:00
|
|
|
return err!(ReadUndefBytes);
|
2016-03-27 05:56:49 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-02-04 21:09:10 +00:00
|
|
|
pub fn mark_definedness(
|
|
|
|
&mut self,
|
2018-05-20 22:30:00 +00:00
|
|
|
ptr: Scalar,
|
2018-05-19 14:37:29 +00:00
|
|
|
size: Size,
|
2017-08-10 15:48:38 +00:00
|
|
|
new_state: bool,
|
2017-02-04 21:09:10 +00:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-19 14:37:29 +00:00
|
|
|
if size.bytes() == 0 {
|
2017-08-10 15:48:38 +00:00
|
|
|
return Ok(());
|
2016-09-22 13:22:00 +00:00
|
|
|
}
|
2017-06-20 12:26:50 +00:00
|
|
|
let ptr = ptr.to_ptr()?;
|
2017-08-12 16:45:44 +00:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2017-08-10 15:48:38 +00:00
|
|
|
alloc.undef_mask.set_range(
|
|
|
|
ptr.offset,
|
|
|
|
ptr.offset + size,
|
|
|
|
new_state,
|
|
|
|
);
|
2016-03-27 04:25:08 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 17:29:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Unaligned accesses
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-16 08:31:48 +00:00
|
|
|
pub trait HasMemory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
|
|
|
|
fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M>;
|
|
|
|
fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M>;
|
2017-07-13 17:29:11 +00:00
|
|
|
|
2017-12-12 16:14:49 +00:00
|
|
|
/// Convert the value into a pointer (or a pointer-sized integer). If the value is a ByRef,
|
|
|
|
/// this may have to perform a load.
|
|
|
|
fn into_ptr(
|
|
|
|
&self,
|
|
|
|
value: Value,
|
2018-07-24 16:28:53 +00:00
|
|
|
) -> EvalResult<'tcx, ScalarMaybeUndef> {
|
2017-12-12 16:14:49 +00:00
|
|
|
Ok(match value {
|
2017-12-16 21:34:43 +00:00
|
|
|
Value::ByRef(ptr, align) => {
|
2018-02-22 16:29:39 +00:00
|
|
|
self.memory().read_ptr_sized(ptr.to_ptr()?, align)?
|
2017-12-12 16:14:49 +00:00
|
|
|
}
|
2018-05-20 21:46:30 +00:00
|
|
|
Value::Scalar(ptr) |
|
|
|
|
Value::ScalarPair(ptr, _) => ptr,
|
2017-12-12 16:14:49 +00:00
|
|
|
}.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_ptr_vtable_pair(
|
|
|
|
&self,
|
|
|
|
value: Value,
|
2018-07-24 16:28:53 +00:00
|
|
|
) -> EvalResult<'tcx, (ScalarMaybeUndef, Pointer)> {
|
2017-12-12 16:14:49 +00:00
|
|
|
match value {
|
2017-12-16 21:34:43 +00:00
|
|
|
Value::ByRef(ref_ptr, align) => {
|
2017-12-17 06:47:22 +00:00
|
|
|
let mem = self.memory();
|
2018-02-22 16:29:39 +00:00
|
|
|
let ptr = mem.read_ptr_sized(ref_ptr.to_ptr()?, align)?.into();
|
|
|
|
let vtable = mem.read_ptr_sized(
|
2018-05-20 22:30:00 +00:00
|
|
|
ref_ptr.ptr_offset(mem.pointer_size(), &mem.tcx.data_layout)?.to_ptr()?,
|
2017-12-17 06:47:22 +00:00
|
|
|
align
|
2018-07-30 13:59:00 +00:00
|
|
|
)?.unwrap_or_err()?.to_ptr()?;
|
2017-12-17 06:47:22 +00:00
|
|
|
Ok((ptr, vtable))
|
2017-12-12 16:14:49 +00:00
|
|
|
}
|
|
|
|
|
2018-07-30 13:59:00 +00:00
|
|
|
Value::ScalarPair(ptr, vtable) => Ok((ptr, vtable.unwrap_or_err()?.to_ptr()?)),
|
2017-12-12 16:14:49 +00:00
|
|
|
_ => bug!("expected ptr and vtable, got {:?}", value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_slice(
|
|
|
|
&self,
|
|
|
|
value: Value,
|
2018-07-24 16:28:53 +00:00
|
|
|
) -> EvalResult<'tcx, (ScalarMaybeUndef, u64)> {
|
2017-12-12 16:14:49 +00:00
|
|
|
match value {
|
2017-12-16 21:34:43 +00:00
|
|
|
Value::ByRef(ref_ptr, align) => {
|
2017-12-17 06:47:22 +00:00
|
|
|
let mem = self.memory();
|
2018-02-22 16:29:39 +00:00
|
|
|
let ptr = mem.read_ptr_sized(ref_ptr.to_ptr()?, align)?.into();
|
|
|
|
let len = mem.read_ptr_sized(
|
2018-05-20 22:30:00 +00:00
|
|
|
ref_ptr.ptr_offset(mem.pointer_size(), &mem.tcx.data_layout)?.to_ptr()?,
|
2017-12-17 06:47:22 +00:00
|
|
|
align
|
2018-07-30 13:59:00 +00:00
|
|
|
)?.unwrap_or_err()?.to_bits(mem.pointer_size())? as u64;
|
2017-12-17 06:47:22 +00:00
|
|
|
Ok((ptr, len))
|
2017-12-12 16:14:49 +00:00
|
|
|
}
|
2018-05-20 21:46:30 +00:00
|
|
|
Value::ScalarPair(ptr, val) => {
|
2018-07-30 13:59:00 +00:00
|
|
|
let len = val.unwrap_or_err()?.to_bits(self.memory().pointer_size())?;
|
2018-07-24 16:28:53 +00:00
|
|
|
Ok((ptr, len as u64))
|
2017-12-12 16:14:49 +00:00
|
|
|
}
|
2018-05-20 21:46:30 +00:00
|
|
|
Value::Scalar(_) => bug!("expected ptr and length, got {:?}", value),
|
2017-12-12 16:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-13 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for Memory<'a, 'mir, 'tcx, M> {
|
2017-07-13 17:29:11 +00:00
|
|
|
#[inline]
|
2018-01-16 08:31:48 +00:00
|
|
|
fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> {
|
2017-07-13 17:29:11 +00:00
|
|
|
self
|
|
|
|
}
|
2017-07-22 18:28:14 +00:00
|
|
|
|
|
|
|
#[inline]
|
2018-01-16 08:31:48 +00:00
|
|
|
fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M> {
|
2017-07-22 18:28:14 +00:00
|
|
|
self
|
|
|
|
}
|
2017-07-13 17:29:11 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for EvalContext<'a, 'mir, 'tcx, M> {
|
2017-07-13 17:29:11 +00:00
|
|
|
#[inline]
|
2018-01-16 08:31:48 +00:00
|
|
|
fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> {
|
2018-06-22 19:36:54 +00:00
|
|
|
&mut self.memory
|
2017-07-13 17:29:11 +00:00
|
|
|
}
|
2017-07-22 18:28:14 +00:00
|
|
|
|
|
|
|
#[inline]
|
2018-01-16 08:31:48 +00:00
|
|
|
fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M> {
|
2018-06-22 19:36:54 +00:00
|
|
|
&self.memory
|
2017-07-22 18:28:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 08:31:48 +00:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> layout::HasDataLayout for &'a Memory<'a, 'mir, 'tcx, M> {
|
2017-07-22 18:28:14 +00:00
|
|
|
#[inline]
|
2017-07-23 13:00:22 +00:00
|
|
|
fn data_layout(&self) -> &TargetDataLayout {
|
2017-12-06 08:25:29 +00:00
|
|
|
&self.tcx.data_layout
|
2017-07-24 07:56:02 +00:00
|
|
|
}
|
|
|
|
}
|