2016-06-23 08:16:25 -05:00
|
|
|
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian, BigEndian, self};
|
2016-03-13 15:36:25 -05:00
|
|
|
use std::collections::Bound::{Included, Excluded};
|
2016-04-06 04:45:06 -05:00
|
|
|
use std::collections::{btree_map, BTreeMap, HashMap, HashSet, VecDeque};
|
2016-06-30 22:30:03 -05:00
|
|
|
use std::{fmt, iter, ptr};
|
2016-03-05 00:48:23 -06:00
|
|
|
|
2016-06-08 06:43:34 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-09-09 05:51:14 -05:00
|
|
|
use rustc::ty::{BareFnTy, ClosureTy, ClosureSubsts};
|
2016-06-08 06:43:34 -05:00
|
|
|
use rustc::ty::subst::Substs;
|
2016-06-23 08:16:25 -05:00
|
|
|
use rustc::ty::layout::{self, TargetDataLayout};
|
2016-06-08 06:43:34 -05:00
|
|
|
|
2016-03-14 22:48:00 -05:00
|
|
|
use error::{EvalError, EvalResult};
|
2016-03-13 01:43:28 -06:00
|
|
|
use primval::PrimVal;
|
2016-03-05 00:48:23 -06:00
|
|
|
|
2016-04-04 21:33:41 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Allocations and pointers
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-09 20:31:53 -05:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
|
2016-06-30 04:29:25 -05:00
|
|
|
pub struct AllocId(pub u64);
|
2016-04-04 21:33:41 -05:00
|
|
|
|
2016-04-09 20:31:53 -05:00
|
|
|
impl fmt::Display for AllocId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:33:41 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Allocation {
|
2016-09-09 10:11:43 -05:00
|
|
|
/// The actual bytes of the allocation.
|
|
|
|
/// Note that the bytes of a pointer represent the offset of the pointer
|
2016-04-06 18:29:56 -05:00
|
|
|
pub bytes: Vec<u8>,
|
2016-09-09 10:11:43 -05:00
|
|
|
/// Maps from byte addresses to allocations.
|
|
|
|
/// Only the first byte of a pointer is inserted into the map.
|
2016-04-04 21:33:41 -05:00
|
|
|
pub relocations: BTreeMap<usize, AllocId>,
|
2016-09-09 10:11:43 -05:00
|
|
|
/// Denotes undefined memory. Reading from undefined memory is forbidden in miri
|
2016-04-06 04:45:06 -05:00
|
|
|
pub undef_mask: UndefMask,
|
2016-09-09 10:11:43 -05:00
|
|
|
/// The alignment of the allocation to detect unaligned reads.
|
2016-07-05 07:27:27 -05:00
|
|
|
pub align: usize,
|
2016-09-09 10:44:04 -05:00
|
|
|
/// Whether the allocation may be modified.
|
|
|
|
/// Use the `freeze` method of `Memory` to ensure that an error occurs, if the memory of this
|
|
|
|
/// allocation is modified in the future.
|
|
|
|
pub immutable: bool,
|
2016-04-04 21:33:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct Pointer {
|
|
|
|
pub alloc_id: AllocId,
|
|
|
|
pub offset: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pointer {
|
|
|
|
pub fn offset(self, i: isize) -> Self {
|
|
|
|
Pointer { offset: (self.offset as isize + i) as usize, ..self }
|
|
|
|
}
|
2016-06-30 09:42:09 -05:00
|
|
|
pub fn points_to_zst(&self) -> bool {
|
2016-07-01 17:40:52 -05:00
|
|
|
self.alloc_id == ZST_ALLOC_ID
|
2016-06-30 09:42:09 -05:00
|
|
|
}
|
|
|
|
fn zst_ptr() -> Self {
|
|
|
|
Pointer {
|
2016-07-01 06:08:19 -05:00
|
|
|
alloc_id: ZST_ALLOC_ID,
|
2016-06-30 09:42:09 -05:00
|
|
|
offset: 0,
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 21:33:41 -05:00
|
|
|
}
|
|
|
|
|
2016-09-09 05:51:14 -05:00
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
|
|
|
struct FunctionDefinition<'tcx> {
|
2016-06-14 03:34:54 -05:00
|
|
|
pub def_id: DefId,
|
2016-09-09 05:51:14 -05:00
|
|
|
pub kind: FunctionKind<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
|
|
|
enum FunctionKind<'tcx> {
|
|
|
|
Closure {
|
|
|
|
substs: ClosureSubsts<'tcx>,
|
|
|
|
ty: ClosureTy<'tcx>,
|
|
|
|
},
|
|
|
|
Function {
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
|
|
|
ty: &'tcx BareFnTy<'tcx>,
|
|
|
|
}
|
2016-06-14 03:34:54 -05:00
|
|
|
}
|
|
|
|
|
2016-04-04 21:33:41 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Top-level interpreter memory
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-06-23 02:36:24 -05:00
|
|
|
pub struct Memory<'a, 'tcx> {
|
2016-06-13 04:39:15 -05:00
|
|
|
/// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations)
|
2016-04-09 20:31:53 -05:00
|
|
|
alloc_map: HashMap<AllocId, Allocation>,
|
2016-07-05 03:47:10 -05:00
|
|
|
/// Number of virtual bytes allocated
|
2016-07-07 04:20:46 -05:00
|
|
|
memory_usage: usize,
|
2016-07-05 03:47:10 -05:00
|
|
|
/// Maximum number of virtual bytes that may be allocated
|
2016-07-07 04:20:46 -05:00
|
|
|
memory_size: usize,
|
2016-06-13 04:39:15 -05:00
|
|
|
/// Function "allocations". They exist solely so pointers have something to point to, and
|
|
|
|
/// we can figure out what they point to.
|
2016-06-14 03:34:54 -05:00
|
|
|
functions: HashMap<AllocId, FunctionDefinition<'tcx>>,
|
2016-06-20 03:35:15 -05:00
|
|
|
/// Inverse map of `functions` so we don't allocate a new pointer every time we need one
|
2016-06-20 09:52:53 -05:00
|
|
|
function_alloc_cache: HashMap<FunctionDefinition<'tcx>, AllocId>,
|
2016-04-09 20:31:53 -05:00
|
|
|
next_id: AllocId,
|
2016-06-23 02:36:24 -05:00
|
|
|
pub layout: &'a TargetDataLayout,
|
2016-03-23 22:40:58 -05:00
|
|
|
}
|
|
|
|
|
2016-07-01 06:08:19 -05:00
|
|
|
const ZST_ALLOC_ID: AllocId = AllocId(0);
|
|
|
|
|
2016-06-23 02:36:24 -05:00
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-07-07 04:20:46 -05:00
|
|
|
pub fn new(layout: &'a TargetDataLayout, max_memory: usize) -> Self {
|
2016-06-30 09:42:09 -05:00
|
|
|
let mut mem = Memory {
|
2016-03-17 08:53:26 -05:00
|
|
|
alloc_map: HashMap::new(),
|
2016-06-08 06:43:34 -05:00
|
|
|
functions: HashMap::new(),
|
2016-06-20 09:52:53 -05:00
|
|
|
function_alloc_cache: HashMap::new(),
|
2016-06-30 09:42:09 -05:00
|
|
|
next_id: AllocId(1),
|
2016-06-23 02:36:24 -05:00
|
|
|
layout: layout,
|
2016-07-05 03:47:10 -05:00
|
|
|
memory_size: max_memory,
|
|
|
|
memory_usage: 0,
|
2016-06-30 09:42:09 -05:00
|
|
|
};
|
|
|
|
// alloc id 0 is reserved for ZSTs, this is an optimization to prevent ZST
|
2016-07-01 06:08:19 -05:00
|
|
|
// (e.g. function items, (), [], ...) from requiring memory
|
2016-06-30 09:42:09 -05:00
|
|
|
let alloc = Allocation {
|
|
|
|
bytes: Vec::new(),
|
|
|
|
relocations: BTreeMap::new(),
|
|
|
|
undef_mask: UndefMask::new(0),
|
2016-07-22 09:35:39 -05:00
|
|
|
align: 1,
|
2016-09-09 10:44:04 -05:00
|
|
|
immutable: false, // must be mutable, because sometimes we "move out" of a ZST
|
2016-06-30 09:42:09 -05:00
|
|
|
};
|
2016-07-01 06:08:19 -05:00
|
|
|
mem.alloc_map.insert(ZST_ALLOC_ID, alloc);
|
2016-06-30 09:42:09 -05:00
|
|
|
// check that additional zst allocs work
|
2016-07-22 09:35:39 -05:00
|
|
|
debug_assert!(mem.allocate(0, 1).unwrap().points_to_zst());
|
2016-07-01 06:08:19 -05:00
|
|
|
debug_assert!(mem.get(ZST_ALLOC_ID).is_ok());
|
2016-06-30 09:42:09 -05:00
|
|
|
mem
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-07-07 06:19:17 -05:00
|
|
|
pub fn allocations(&self) -> ::std::collections::hash_map::Iter<AllocId, Allocation> {
|
2016-06-30 04:29:25 -05:00
|
|
|
self.alloc_map.iter()
|
|
|
|
}
|
|
|
|
|
2016-09-09 05:51:14 -05:00
|
|
|
pub fn create_closure_ptr(&mut self, def_id: DefId, substs: ClosureSubsts<'tcx>, fn_ty: ClosureTy<'tcx>) -> Pointer {
|
|
|
|
self.create_fn_alloc(FunctionDefinition {
|
|
|
|
def_id: def_id,
|
|
|
|
kind: FunctionKind::Closure {
|
|
|
|
substs: substs,
|
|
|
|
ty: fn_ty,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn create_fn_ptr(&mut self, def_id: DefId, substs: &'tcx Substs<'tcx>, fn_ty: &'tcx BareFnTy<'tcx>) -> Pointer {
|
2016-09-09 05:51:14 -05:00
|
|
|
self.create_fn_alloc(FunctionDefinition {
|
2016-06-14 03:34:54 -05:00
|
|
|
def_id: def_id,
|
2016-09-09 05:51:14 -05:00
|
|
|
kind: FunctionKind::Function {
|
|
|
|
substs: substs,
|
|
|
|
ty: fn_ty,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_fn_alloc(&mut self, def: FunctionDefinition<'tcx>) -> Pointer {
|
2016-06-20 09:52:53 -05:00
|
|
|
if let Some(&alloc_id) = self.function_alloc_cache.get(&def) {
|
2016-06-20 03:35:15 -05:00
|
|
|
return Pointer {
|
|
|
|
alloc_id: alloc_id,
|
|
|
|
offset: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let id = self.next_id;
|
|
|
|
debug!("creating fn ptr: {}", id);
|
|
|
|
self.next_id.0 += 1;
|
2016-09-09 05:51:14 -05:00
|
|
|
self.functions.insert(id, def.clone());
|
2016-06-20 09:52:53 -05:00
|
|
|
self.function_alloc_cache.insert(def, id);
|
2016-06-08 06:43:34 -05:00
|
|
|
Pointer {
|
|
|
|
alloc_id: id,
|
|
|
|
offset: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 07:27:27 -05:00
|
|
|
pub fn allocate(&mut self, size: usize, align: usize) -> EvalResult<'tcx, Pointer> {
|
2016-06-30 09:42:09 -05:00
|
|
|
if size == 0 {
|
2016-07-05 03:47:10 -05:00
|
|
|
return Ok(Pointer::zst_ptr());
|
2016-06-30 09:42:09 -05:00
|
|
|
}
|
2016-09-13 22:31:12 -05:00
|
|
|
assert!(align != 0);
|
|
|
|
|
2016-07-07 04:20:46 -05:00
|
|
|
if self.memory_size - self.memory_usage < size {
|
2016-07-05 03:47:10 -05:00
|
|
|
return Err(EvalError::OutOfMemory {
|
2016-07-22 09:35:39 -05:00
|
|
|
allocation_size: size,
|
2016-07-05 03:47:10 -05:00
|
|
|
memory_size: self.memory_size,
|
|
|
|
memory_usage: self.memory_usage,
|
|
|
|
});
|
|
|
|
}
|
2016-07-07 04:20:46 -05:00
|
|
|
self.memory_usage += size;
|
2016-03-22 01:48:28 -05:00
|
|
|
let alloc = Allocation {
|
2016-07-22 09:35:39 -05:00
|
|
|
bytes: vec![0; size],
|
2016-03-22 01:48:28 -05:00
|
|
|
relocations: BTreeMap::new(),
|
2016-07-22 09:35:39 -05:00
|
|
|
undef_mask: UndefMask::new(size),
|
2016-07-05 07:27:27 -05:00
|
|
|
align: align,
|
2016-09-09 10:44:04 -05:00
|
|
|
immutable: false,
|
2016-03-22 01:48:28 -05:00
|
|
|
};
|
2016-04-09 20:31:53 -05:00
|
|
|
let id = self.next_id;
|
|
|
|
self.next_id.0 += 1;
|
|
|
|
self.alloc_map.insert(id, alloc);
|
2016-07-05 03:47:10 -05:00
|
|
|
Ok(Pointer {
|
2016-03-07 07:19:43 -06:00
|
|
|
alloc_id: id,
|
2016-07-22 09:35:39 -05:00
|
|
|
offset: 0,
|
2016-07-05 03:47:10 -05:00
|
|
|
})
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-05-09 21:08:37 -05:00
|
|
|
// TODO(solson): Track which allocations were returned from __rust_allocate and report an error
|
2016-04-06 18:29:56 -05:00
|
|
|
// when reallocating/deallocating any others.
|
2016-07-05 07:27:27 -05:00
|
|
|
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize, align: usize) -> EvalResult<'tcx, Pointer> {
|
2016-07-22 09:35:39 -05:00
|
|
|
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
|
|
|
if ptr.offset != 0 {
|
2016-05-30 08:27:52 -05:00
|
|
|
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
2016-04-06 18:29:56 -05:00
|
|
|
}
|
2016-07-01 06:08:19 -05:00
|
|
|
if ptr.points_to_zst() {
|
2016-07-05 07:27:27 -05:00
|
|
|
return self.allocate(new_size, align);
|
2016-07-01 06:08:19 -05:00
|
|
|
}
|
2016-04-06 18:29:56 -05:00
|
|
|
|
2016-07-05 07:27:27 -05:00
|
|
|
let size = self.get(ptr.alloc_id)?.bytes.len();
|
2016-06-13 04:24:01 -05:00
|
|
|
|
2016-07-22 09:35:39 -05:00
|
|
|
if new_size > size {
|
|
|
|
let amount = new_size - size;
|
2016-07-07 04:20:46 -05:00
|
|
|
self.memory_usage += amount;
|
2016-06-13 04:24:01 -05:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-04-06 18:29:56 -05:00
|
|
|
alloc.bytes.extend(iter::repeat(0).take(amount));
|
|
|
|
alloc.undef_mask.grow(amount, false);
|
2016-07-22 09:35:39 -05:00
|
|
|
} else if size > new_size {
|
|
|
|
self.memory_usage -= size - new_size;
|
|
|
|
self.clear_relocations(ptr.offset(new_size as isize), size - new_size)?;
|
2016-06-13 04:24:01 -05:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-07-22 09:35:39 -05:00
|
|
|
alloc.bytes.truncate(new_size);
|
2016-07-25 05:30:35 -05:00
|
|
|
alloc.bytes.shrink_to_fit();
|
2016-07-22 09:35:39 -05:00
|
|
|
alloc.undef_mask.truncate(new_size);
|
2016-04-06 18:29:56 -05:00
|
|
|
}
|
|
|
|
|
2016-07-05 07:27:27 -05:00
|
|
|
Ok(Pointer {
|
|
|
|
alloc_id: ptr.alloc_id,
|
2016-07-22 09:35:39 -05:00
|
|
|
offset: 0,
|
2016-07-05 07:27:27 -05:00
|
|
|
})
|
2016-04-06 18:29:56 -05:00
|
|
|
}
|
|
|
|
|
2016-05-09 21:08:37 -05:00
|
|
|
// TODO(solson): See comment on `reallocate`.
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn deallocate(&mut self, ptr: Pointer) -> EvalResult<'tcx, ()> {
|
2016-06-30 09:42:09 -05:00
|
|
|
if ptr.points_to_zst() {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2016-07-22 09:35:39 -05:00
|
|
|
if ptr.offset != 0 {
|
2016-05-09 21:08:37 -05:00
|
|
|
// TODO(solson): Report error about non-__rust_allocate'd pointer.
|
2016-05-30 08:27:52 -05:00
|
|
|
return Err(EvalError::Unimplemented(format!("bad pointer offset: {}", ptr.offset)));
|
2016-04-07 04:02:02 -05:00
|
|
|
}
|
|
|
|
|
2016-07-05 03:47:10 -05:00
|
|
|
if let Some(alloc) = self.alloc_map.remove(&ptr.alloc_id) {
|
2016-07-07 04:20:46 -05:00
|
|
|
self.memory_usage -= alloc.bytes.len();
|
2016-07-05 03:47:10 -05:00
|
|
|
} else {
|
2016-07-01 06:08:19 -05:00
|
|
|
debug!("deallocated a pointer twice: {}", ptr.alloc_id);
|
2016-05-09 21:08:37 -05:00
|
|
|
// TODO(solson): Report error about erroneous free. This is blocked on properly tracking
|
2016-04-07 04:02:02 -05:00
|
|
|
// already-dropped state since this if-statement is entered even in safe code without
|
|
|
|
// it.
|
|
|
|
}
|
2016-07-01 06:08:19 -05:00
|
|
|
debug!("deallocated : {}", ptr.alloc_id);
|
2016-04-07 04:02:02 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-06-23 02:59:16 -05:00
|
|
|
|
|
|
|
pub fn pointer_size(&self) -> usize {
|
|
|
|
self.layout.pointer_size.bytes() as usize
|
|
|
|
}
|
2016-06-23 08:16:25 -05:00
|
|
|
|
|
|
|
pub fn endianess(&self) -> layout::Endian {
|
|
|
|
self.layout.endian
|
|
|
|
}
|
2016-07-22 09:35:39 -05:00
|
|
|
|
|
|
|
pub fn check_align(&self, ptr: Pointer, align: usize) -> EvalResult<'tcx, ()> {
|
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
|
|
|
if alloc.align < align {
|
|
|
|
return Err(EvalError::AlignmentCheckFailed {
|
|
|
|
has: alloc.align,
|
|
|
|
required: align,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if ptr.offset % align == 0 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(EvalError::AlignmentCheckFailed {
|
|
|
|
has: ptr.offset % align,
|
|
|
|
required: align,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-06-23 02:40:01 -05:00
|
|
|
}
|
2016-04-07 04:02:02 -05:00
|
|
|
|
2016-06-23 02:40:01 -05:00
|
|
|
/// Allocation accessors
|
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
|
2016-06-13 04:39:15 -05:00
|
|
|
match self.alloc_map.get(&id) {
|
|
|
|
Some(alloc) => Ok(alloc),
|
|
|
|
None => match self.functions.get(&id) {
|
|
|
|
Some(_) => Err(EvalError::DerefFunctionPointer),
|
|
|
|
None => Err(EvalError::DanglingPointerDeref),
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn get_mut(&mut self, id: AllocId) -> EvalResult<'tcx, &mut Allocation> {
|
2016-06-13 04:39:15 -05:00
|
|
|
match self.alloc_map.get_mut(&id) {
|
2016-09-09 10:44:04 -05:00
|
|
|
Some(ref alloc) if alloc.immutable => Err(EvalError::ModifiedConstantMemory),
|
2016-06-13 04:39:15 -05:00
|
|
|
Some(alloc) => Ok(alloc),
|
|
|
|
None => match self.functions.get(&id) {
|
|
|
|
Some(_) => Err(EvalError::DerefFunctionPointer),
|
|
|
|
None => Err(EvalError::DanglingPointerDeref),
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-09-09 05:51:14 -05:00
|
|
|
pub fn get_closure(&self, id: AllocId) -> EvalResult<'tcx, (DefId, ClosureSubsts<'tcx>, ClosureTy<'tcx>)> {
|
|
|
|
debug!("reading closure fn ptr: {}", id);
|
|
|
|
match self.functions.get(&id) {
|
|
|
|
Some(&FunctionDefinition {
|
|
|
|
def_id,
|
|
|
|
kind: FunctionKind::Closure { ref substs, ref ty }
|
2016-09-13 06:03:54 -05:00
|
|
|
}) => Ok((def_id, *substs, ty.clone())),
|
2016-09-09 05:51:14 -05:00
|
|
|
Some(&FunctionDefinition {
|
|
|
|
kind: FunctionKind::Function { .. }, ..
|
|
|
|
}) => Err(EvalError::CalledClosureAsFunction),
|
|
|
|
None => match self.alloc_map.get(&id) {
|
|
|
|
Some(_) => Err(EvalError::ExecuteMemory),
|
|
|
|
None => Err(EvalError::InvalidFunctionPointer),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_fn(&self, id: AllocId) -> EvalResult<'tcx, (DefId, &'tcx Substs<'tcx>, &'tcx BareFnTy<'tcx>)> {
|
2016-06-08 06:43:34 -05:00
|
|
|
debug!("reading fn ptr: {}", id);
|
2016-06-13 04:39:15 -05:00
|
|
|
match self.functions.get(&id) {
|
2016-09-09 05:51:14 -05:00
|
|
|
Some(&FunctionDefinition {
|
|
|
|
def_id,
|
|
|
|
kind: FunctionKind::Function { substs, ty }
|
|
|
|
}) => Ok((def_id, substs, ty)),
|
|
|
|
Some(&FunctionDefinition {
|
|
|
|
kind: FunctionKind::Closure { .. }, ..
|
|
|
|
}) => Err(EvalError::CalledClosureAsFunction),
|
2016-06-13 04:39:15 -05:00
|
|
|
None => match self.alloc_map.get(&id) {
|
|
|
|
Some(_) => Err(EvalError::ExecuteMemory),
|
|
|
|
None => Err(EvalError::InvalidFunctionPointer),
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 06:43:34 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
/// Print an allocation and all allocations it points to, recursively.
|
|
|
|
pub fn dump(&self, id: AllocId) {
|
|
|
|
let mut allocs_seen = HashSet::new();
|
|
|
|
let mut allocs_to_print = VecDeque::new();
|
|
|
|
allocs_to_print.push_back(id);
|
|
|
|
|
|
|
|
while let Some(id) = allocs_to_print.pop_front() {
|
2016-04-09 20:31:53 -05:00
|
|
|
allocs_seen.insert(id);
|
|
|
|
let prefix = format!("Alloc {:<5} ", format!("{}:", id));
|
2016-04-06 04:45:06 -05:00
|
|
|
print!("{}", prefix);
|
|
|
|
let mut relocations = vec![];
|
|
|
|
|
2016-06-08 06:43:34 -05:00
|
|
|
let alloc = match (self.alloc_map.get(&id), self.functions.get(&id)) {
|
|
|
|
(Some(a), None) => a,
|
|
|
|
(None, Some(_)) => {
|
|
|
|
// FIXME: print function name
|
|
|
|
println!("function pointer");
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
(None, None) => {
|
2016-04-07 04:07:57 -05:00
|
|
|
println!("(deallocated)");
|
|
|
|
continue;
|
2016-06-08 06:43:34 -05:00
|
|
|
},
|
2016-09-06 09:16:49 -05:00
|
|
|
(Some(_), Some(_)) => bug!("miri invariant broken: an allocation id exists that points to both a function and a memory location"),
|
2016-04-07 04:07:57 -05:00
|
|
|
};
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
for i in 0..alloc.bytes.len() {
|
|
|
|
if let Some(&target_id) = alloc.relocations.get(&i) {
|
2016-04-09 20:31:53 -05:00
|
|
|
if !allocs_seen.contains(&target_id) {
|
2016-04-06 04:45:06 -05:00
|
|
|
allocs_to_print.push_back(target_id);
|
|
|
|
}
|
2016-04-09 20:31:53 -05:00
|
|
|
relocations.push((i, target_id));
|
2016-04-06 04:45:06 -05:00
|
|
|
}
|
2016-04-09 20:31:53 -05:00
|
|
|
if alloc.undef_mask.is_range_defined(i, i + 1) {
|
2016-04-06 04:45:06 -05:00
|
|
|
print!("{:02x} ", alloc.bytes[i]);
|
|
|
|
} else {
|
|
|
|
print!("__ ");
|
|
|
|
}
|
|
|
|
}
|
2016-04-06 18:33:24 -05:00
|
|
|
println!("({} bytes)", alloc.bytes.len());
|
2016-04-06 04:45:06 -05:00
|
|
|
|
|
|
|
if !relocations.is_empty() {
|
|
|
|
print!("{:1$}", "", prefix.len()); // Print spaces.
|
|
|
|
let mut pos = 0;
|
2016-06-23 03:00:31 -05:00
|
|
|
let relocation_width = (self.pointer_size() - 1) * 3;
|
2016-04-06 04:45:06 -05:00
|
|
|
for (i, target_id) in relocations {
|
|
|
|
print!("{:1$}", "", (i - pos) * 3);
|
|
|
|
print!("└{0:─^1$}┘ ", format!("({})", target_id), relocation_width);
|
2016-06-23 03:00:31 -05:00
|
|
|
pos = i + self.pointer_size();
|
2016-04-06 04:45:06 -05:00
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-23 02:40:01 -05:00
|
|
|
}
|
2016-04-06 04:45:06 -05:00
|
|
|
|
2016-06-23 02:40:01 -05:00
|
|
|
/// Byte accessors
|
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-06-14 03:34:54 -05:00
|
|
|
fn get_bytes_unchecked(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, &[u8]> {
|
2016-05-09 19:52:44 -05:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2016-03-23 20:44:05 -05:00
|
|
|
if ptr.offset + size > alloc.bytes.len() {
|
2016-05-31 05:05:25 -05:00
|
|
|
return Err(EvalError::PointerOutOfBounds {
|
2016-06-01 04:22:37 -05:00
|
|
|
ptr: ptr,
|
2016-05-31 05:05:25 -05:00
|
|
|
size: size,
|
2016-06-01 04:22:37 -05:00
|
|
|
allocation_size: alloc.bytes.len(),
|
2016-05-31 05:05:25 -05:00
|
|
|
});
|
2016-03-23 20:44:05 -05:00
|
|
|
}
|
|
|
|
Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
fn get_bytes_unchecked_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<'tcx, &mut [u8]> {
|
2016-05-09 19:52:44 -05:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-03-23 20:44:05 -05:00
|
|
|
if ptr.offset + size > alloc.bytes.len() {
|
2016-05-31 05:05:25 -05:00
|
|
|
return Err(EvalError::PointerOutOfBounds {
|
2016-06-01 04:22:37 -05:00
|
|
|
ptr: ptr,
|
2016-05-31 05:05:25 -05:00
|
|
|
size: size,
|
2016-06-01 04:22:37 -05:00
|
|
|
allocation_size: alloc.bytes.len(),
|
2016-05-31 05:05:25 -05:00
|
|
|
});
|
2016-03-23 20:44:05 -05:00
|
|
|
}
|
|
|
|
Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-07-22 09:35:39 -05:00
|
|
|
fn get_bytes(&self, ptr: Pointer, size: usize, align: usize) -> EvalResult<'tcx, &[u8]> {
|
|
|
|
self.check_align(ptr, align)?;
|
2016-05-09 19:52:44 -05:00
|
|
|
if self.relocations(ptr, size)?.count() != 0 {
|
2016-03-23 20:44:05 -05:00
|
|
|
return Err(EvalError::ReadPointerAsBytes);
|
2016-03-21 06:27:34 -05:00
|
|
|
}
|
2016-05-09 19:52:44 -05:00
|
|
|
self.check_defined(ptr, size)?;
|
2016-03-23 20:44:05 -05:00
|
|
|
self.get_bytes_unchecked(ptr, size)
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:35:39 -05:00
|
|
|
fn get_bytes_mut(&mut self, ptr: Pointer, size: usize, align: usize) -> EvalResult<'tcx, &mut [u8]> {
|
|
|
|
self.check_align(ptr, align)?;
|
2016-05-09 19:52:44 -05:00
|
|
|
self.clear_relocations(ptr, size)?;
|
|
|
|
self.mark_definedness(ptr, size, true)?;
|
2016-03-23 20:44:05 -05:00
|
|
|
self.get_bytes_unchecked_mut(ptr, size)
|
2016-03-21 06:27:34 -05:00
|
|
|
}
|
2016-06-23 02:40:01 -05:00
|
|
|
}
|
2016-03-21 06:27:34 -05:00
|
|
|
|
2016-06-23 02:40:01 -05:00
|
|
|
/// Reading and writing
|
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-09-09 10:44:04 -05:00
|
|
|
|
|
|
|
pub fn freeze(&mut self, alloc_id: AllocId) -> EvalResult<'tcx, ()> {
|
|
|
|
self.get_mut(alloc_id)?.immutable = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:35:39 -05:00
|
|
|
pub fn copy(&mut self, src: Pointer, dest: Pointer, size: usize, align: usize) -> EvalResult<'tcx, ()> {
|
2016-05-09 19:52:44 -05:00
|
|
|
self.check_relocation_edges(src, size)?;
|
2016-03-21 06:27:34 -05:00
|
|
|
|
2016-09-09 09:57:09 -05:00
|
|
|
let src_bytes = self.get_bytes_unchecked(src, size)?.as_ptr();
|
2016-07-22 09:35:39 -05:00
|
|
|
let dest_bytes = self.get_bytes_mut(dest, size, align)?.as_mut_ptr();
|
2016-03-05 00:48:23 -06: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 {
|
|
|
|
if src.alloc_id == dest.alloc_id {
|
|
|
|
ptr::copy(src_bytes, dest_bytes, size);
|
|
|
|
} else {
|
|
|
|
ptr::copy_nonoverlapping(src_bytes, dest_bytes, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 19:52:44 -05:00
|
|
|
self.copy_undef_mask(src, dest, size)?;
|
|
|
|
self.copy_relocations(src, dest, size)?;
|
2016-04-06 05:08:52 -05:00
|
|
|
|
|
|
|
Ok(())
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_bytes(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, &[u8]> {
|
2016-07-22 09:35:39 -05:00
|
|
|
self.get_bytes(ptr, size, 1)
|
2016-04-15 04:16:35 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_bytes(&mut self, ptr: Pointer, src: &[u8]) -> EvalResult<'tcx, ()> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let bytes = self.get_bytes_mut(ptr, src.len(), 1)?;
|
2016-04-07 06:56:07 -05:00
|
|
|
bytes.clone_from_slice(src);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_repeat(&mut self, ptr: Pointer, val: u8, count: usize) -> EvalResult<'tcx, ()> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let bytes = self.get_bytes_mut(ptr, count, 1)?;
|
2016-04-07 06:56:07 -05:00
|
|
|
for b in bytes { *b = val; }
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<'tcx, Pointer> {
|
2016-06-23 03:00:31 -05:00
|
|
|
let size = self.pointer_size();
|
2016-05-09 19:52:44 -05:00
|
|
|
self.check_defined(ptr, size)?;
|
2016-06-25 09:50:33 -05:00
|
|
|
let endianess = self.endianess();
|
|
|
|
let bytes = self.get_bytes_unchecked(ptr, size)?;
|
|
|
|
let offset = read_target_uint(endianess, bytes).unwrap() as usize;
|
2016-05-09 19:52:44 -05:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2016-03-17 08:24:10 -05:00
|
|
|
match alloc.relocations.get(&ptr.offset) {
|
|
|
|
Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
|
|
|
|
None => Err(EvalError::ReadBytesAsPointer),
|
|
|
|
}
|
2016-03-13 15:36:25 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<'tcx, ()> {
|
2016-06-23 06:04:05 -05:00
|
|
|
self.write_usize(dest, ptr.offset as u64)?;
|
2016-05-09 19:52:44 -05:00
|
|
|
self.get_mut(dest.alloc_id)?.relocations.insert(dest.offset, ptr.alloc_id);
|
2016-03-13 15:36:25 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<'tcx, ()> {
|
2016-06-23 03:00:31 -05:00
|
|
|
let pointer_size = self.pointer_size();
|
2016-03-13 01:14:20 -06:00
|
|
|
match val {
|
|
|
|
PrimVal::Bool(b) => self.write_bool(ptr, b),
|
2016-03-17 07:26:37 -05:00
|
|
|
PrimVal::I8(n) => self.write_int(ptr, n as i64, 1),
|
|
|
|
PrimVal::I16(n) => self.write_int(ptr, n as i64, 2),
|
|
|
|
PrimVal::I32(n) => self.write_int(ptr, n as i64, 4),
|
|
|
|
PrimVal::I64(n) => self.write_int(ptr, n as i64, 8),
|
|
|
|
PrimVal::U8(n) => self.write_uint(ptr, n as u64, 1),
|
|
|
|
PrimVal::U16(n) => self.write_uint(ptr, n as u64, 2),
|
|
|
|
PrimVal::U32(n) => self.write_uint(ptr, n as u64, 4),
|
2016-03-21 06:42:42 -05:00
|
|
|
PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
|
2016-06-21 02:43:45 -05:00
|
|
|
PrimVal::Char(c) => self.write_uint(ptr, c as u64, 4),
|
2016-03-21 06:42:42 -05:00
|
|
|
PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
|
2016-07-06 04:51:32 -05:00
|
|
|
PrimVal::F32(f) => self.write_f32(ptr, f),
|
|
|
|
PrimVal::F64(f) => self.write_f64(ptr, f),
|
2016-09-07 11:34:59 -05:00
|
|
|
PrimVal::FnPtr(p) |
|
|
|
|
PrimVal::AbstractPtr(p) => self.write_ptr(ptr, p),
|
2016-03-13 01:14:20 -06:00
|
|
|
}
|
2016-03-07 04:44:03 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_bool(&self, ptr: Pointer) -> EvalResult<'tcx, bool> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let bytes = self.get_bytes(ptr, 1, self.layout.i1_align.abi() as usize)?;
|
2016-03-07 04:44:03 -06:00
|
|
|
match bytes[0] {
|
|
|
|
0 => Ok(false),
|
|
|
|
1 => Ok(true),
|
|
|
|
_ => Err(EvalError::InvalidBool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_bool(&mut self, ptr: Pointer, b: bool) -> EvalResult<'tcx, ()> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.layout.i1_align.abi() as usize;
|
|
|
|
self.get_bytes_mut(ptr, 1, align)
|
|
|
|
.map(|bytes| bytes[0] = b as u8)
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
2016-03-13 01:14:20 -06:00
|
|
|
|
2016-07-22 09:35:39 -05:00
|
|
|
fn int_align(&self, size: usize) -> EvalResult<'tcx, usize> {
|
2016-07-06 04:12:44 -05:00
|
|
|
match size {
|
2016-07-22 09:35:39 -05:00
|
|
|
1 => Ok(self.layout.i8_align.abi() as usize),
|
|
|
|
2 => Ok(self.layout.i16_align.abi() as usize),
|
|
|
|
4 => Ok(self.layout.i32_align.abi() as usize),
|
|
|
|
8 => Ok(self.layout.i64_align.abi() as usize),
|
2016-09-06 09:16:49 -05:00
|
|
|
_ => bug!("bad integer size"),
|
2016-07-06 04:12:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, i64> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.int_align(size)?;
|
|
|
|
self.get_bytes(ptr, size, align).map(|b| read_target_int(self.endianess(), b).unwrap())
|
2016-03-17 04:12:15 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<'tcx, ()> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.int_align(size)?;
|
2016-06-23 08:16:25 -05:00
|
|
|
let endianess = self.endianess();
|
2016-07-22 09:35:39 -05:00
|
|
|
let b = self.get_bytes_mut(ptr, size, align)?;
|
2016-06-25 09:50:33 -05:00
|
|
|
write_target_int(endianess, b, n).unwrap();
|
|
|
|
Ok(())
|
2016-03-13 01:14:20 -06:00
|
|
|
}
|
2016-03-15 00:03:31 -05:00
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_uint(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, u64> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.int_align(size)?;
|
|
|
|
self.get_bytes(ptr, size, align).map(|b| read_target_uint(self.endianess(), b).unwrap())
|
2016-03-17 03:53:03 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_uint(&mut self, ptr: Pointer, n: u64, size: usize) -> EvalResult<'tcx, ()> {
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.int_align(size)?;
|
2016-06-23 08:16:25 -05:00
|
|
|
let endianess = self.endianess();
|
2016-07-22 09:35:39 -05:00
|
|
|
let b = self.get_bytes_mut(ptr, size, align)?;
|
2016-06-25 09:50:33 -05:00
|
|
|
write_target_uint(endianess, b, n).unwrap();
|
|
|
|
Ok(())
|
2016-03-17 03:53:03 -05:00
|
|
|
}
|
2016-03-21 00:24:27 -05:00
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_isize(&self, ptr: Pointer) -> EvalResult<'tcx, i64> {
|
2016-06-23 03:00:31 -05:00
|
|
|
self.read_int(ptr, self.pointer_size())
|
2016-03-21 00:24:27 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_isize(&mut self, ptr: Pointer, n: i64) -> EvalResult<'tcx, ()> {
|
2016-06-23 03:00:31 -05:00
|
|
|
let size = self.pointer_size();
|
2016-03-21 00:24:27 -05:00
|
|
|
self.write_int(ptr, n, size)
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn read_usize(&self, ptr: Pointer) -> EvalResult<'tcx, u64> {
|
2016-06-23 03:00:31 -05:00
|
|
|
self.read_uint(ptr, self.pointer_size())
|
2016-03-21 00:24:27 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
pub fn write_usize(&mut self, ptr: Pointer, n: u64) -> EvalResult<'tcx, ()> {
|
2016-06-23 03:00:31 -05:00
|
|
|
let size = self.pointer_size();
|
2016-03-21 00:24:27 -05:00
|
|
|
self.write_uint(ptr, n, size)
|
|
|
|
}
|
2016-07-06 04:51:32 -05:00
|
|
|
|
|
|
|
pub fn write_f32(&mut self, ptr: Pointer, f: f32) -> EvalResult<'tcx, ()> {
|
|
|
|
let endianess = self.endianess();
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.layout.f32_align.abi() as usize;
|
|
|
|
let b = self.get_bytes_mut(ptr, 4, align)?;
|
2016-07-06 04:51:32 -05:00
|
|
|
write_target_f32(endianess, b, f).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_f64(&mut self, ptr: Pointer, f: f64) -> EvalResult<'tcx, ()> {
|
|
|
|
let endianess = self.endianess();
|
2016-07-22 09:35:39 -05:00
|
|
|
let align = self.layout.f64_align.abi() as usize;
|
|
|
|
let b = self.get_bytes_mut(ptr, 8, align)?;
|
2016-07-06 04:51:32 -05:00
|
|
|
write_target_f64(endianess, b, f).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_f32(&self, ptr: Pointer) -> EvalResult<'tcx, f32> {
|
2016-07-22 09:35:39 -05:00
|
|
|
self.get_bytes(ptr, 4, self.layout.f32_align.abi() as usize)
|
|
|
|
.map(|b| read_target_f32(self.endianess(), b).unwrap())
|
2016-07-06 04:51:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_f64(&self, ptr: Pointer) -> EvalResult<'tcx, f64> {
|
2016-07-22 09:35:39 -05:00
|
|
|
self.get_bytes(ptr, 8, self.layout.f64_align.abi() as usize)
|
|
|
|
.map(|b| read_target_f64(self.endianess(), b).unwrap())
|
2016-07-06 04:51:32 -05:00
|
|
|
}
|
2016-06-23 02:40:01 -05:00
|
|
|
}
|
2016-03-05 00:48:23 -06:00
|
|
|
|
2016-06-23 02:40:01 -05:00
|
|
|
/// Relocations
|
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-03-23 22:40:58 -05:00
|
|
|
fn relocations(&self, ptr: Pointer, size: usize)
|
2016-06-14 03:34:54 -05:00
|
|
|
-> EvalResult<'tcx, btree_map::Range<usize, AllocId>>
|
2016-03-23 22:40:58 -05:00
|
|
|
{
|
2016-06-23 03:00:31 -05:00
|
|
|
let start = ptr.offset.saturating_sub(self.pointer_size() - 1);
|
2016-06-13 04:24:01 -05:00
|
|
|
let end = ptr.offset + size;
|
2016-05-09 19:52:44 -05:00
|
|
|
Ok(self.get(ptr.alloc_id)?.relocations.range(Included(&start), Excluded(&end)))
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
fn clear_relocations(&mut self, ptr: Pointer, size: usize) -> EvalResult<'tcx, ()> {
|
2016-03-27 01:29:02 -05:00
|
|
|
// Find all relocations overlapping the given range.
|
2016-05-09 19:52:44 -05:00
|
|
|
let keys: Vec<_> = self.relocations(ptr, size)?.map(|(&k, _)| k).collect();
|
2016-04-28 23:01:17 -05:00
|
|
|
if keys.is_empty() { return Ok(()); }
|
2016-03-27 01:29:02 -05:00
|
|
|
|
|
|
|
// Find the start and end of the given range and its outermost relocations.
|
|
|
|
let start = ptr.offset;
|
|
|
|
let end = start + size;
|
|
|
|
let first = *keys.first().unwrap();
|
2016-06-23 03:00:31 -05:00
|
|
|
let last = *keys.last().unwrap() + self.pointer_size();
|
2016-03-27 01:29:02 -05:00
|
|
|
|
2016-05-09 19:52:44 -05:00
|
|
|
let alloc = self.get_mut(ptr.alloc_id)?;
|
2016-03-27 01:29:02 -05:00
|
|
|
|
|
|
|
// Mark parts of the outermost relocations as undefined if they partially fall outside the
|
|
|
|
// given range.
|
2016-04-06 04:45:06 -05: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 01:29:02 -05:00
|
|
|
|
|
|
|
// Forget all the relocations.
|
|
|
|
for k in keys { alloc.relocations.remove(&k); }
|
|
|
|
|
2016-03-23 22:40:58 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
fn check_relocation_edges(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, ()> {
|
2016-05-09 19:52:44 -05:00
|
|
|
let overlapping_start = self.relocations(ptr, 0)?.count();
|
|
|
|
let overlapping_end = self.relocations(ptr.offset(size as isize), 0)?.count();
|
2016-03-23 22:40:58 -05:00
|
|
|
if overlapping_start + overlapping_end != 0 {
|
|
|
|
return Err(EvalError::ReadPointerAsBytes);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
fn copy_relocations(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<'tcx, ()> {
|
2016-05-09 19:52:44 -05:00
|
|
|
let relocations: Vec<_> = self.relocations(src, size)?
|
2016-03-23 22:40:58 -05:00
|
|
|
.map(|(&offset, &alloc_id)| {
|
|
|
|
// Update relocation offsets for the new positions in the destination allocation.
|
|
|
|
(offset + dest.offset - src.offset, alloc_id)
|
|
|
|
})
|
|
|
|
.collect();
|
2016-05-09 19:52:44 -05:00
|
|
|
self.get_mut(dest.alloc_id)?.relocations.extend(relocations);
|
2016-03-23 22:40:58 -05:00
|
|
|
Ok(())
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|
2016-06-23 02:40:01 -05:00
|
|
|
}
|
2016-03-26 23:25:08 -05:00
|
|
|
|
2016-06-23 02:40:01 -05:00
|
|
|
/// Undefined bytes
|
|
|
|
impl<'a, 'tcx> Memory<'a, 'tcx> {
|
2016-05-09 21:08:37 -05:00
|
|
|
// FIXME(solson): This is a very naive, slow version.
|
2016-06-14 03:34:54 -05:00
|
|
|
fn copy_undef_mask(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<'tcx, ()> {
|
2016-04-06 05:08:52 -05:00
|
|
|
// The bits have to be saved locally before writing to dest in case src and dest overlap.
|
|
|
|
let mut v = Vec::with_capacity(size);
|
|
|
|
for i in 0..size {
|
2016-05-09 19:52:44 -05:00
|
|
|
let defined = self.get(src.alloc_id)?.undef_mask.get(src.offset + i);
|
2016-04-06 05:08:52 -05:00
|
|
|
v.push(defined);
|
|
|
|
}
|
|
|
|
for (i, defined) in v.into_iter().enumerate() {
|
2016-05-09 19:52:44 -05:00
|
|
|
self.get_mut(dest.alloc_id)?.undef_mask.set(dest.offset + i, defined);
|
2016-04-06 05:08:52 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:34:54 -05:00
|
|
|
fn check_defined(&self, ptr: Pointer, size: usize) -> EvalResult<'tcx, ()> {
|
2016-05-09 19:52:44 -05:00
|
|
|
let alloc = self.get(ptr.alloc_id)?;
|
2016-04-06 04:45:06 -05:00
|
|
|
if !alloc.undef_mask.is_range_defined(ptr.offset, ptr.offset + size) {
|
2016-03-27 00:56:49 -05:00
|
|
|
return Err(EvalError::ReadUndefBytes);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:37:07 -05:00
|
|
|
pub fn mark_definedness(&mut self, ptr: Pointer, size: usize, new_state: bool)
|
2016-06-14 03:34:54 -05:00
|
|
|
-> EvalResult<'tcx, ()>
|
2016-03-28 17:37:07 -05:00
|
|
|
{
|
2016-05-09 19:52:44 -05:00
|
|
|
let mut alloc = self.get_mut(ptr.alloc_id)?;
|
2016-04-06 04:45:06 -05:00
|
|
|
alloc.undef_mask.set_range(ptr.offset, ptr.offset + size, new_state);
|
2016-03-26 23:25:08 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 09:50:33 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods to access integers in the target endianess
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u64) -> Result<(), byteorder::Error> {
|
|
|
|
let len = target.len();
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => target.write_uint::<LittleEndian>(data, len),
|
|
|
|
layout::Endian::Big => target.write_uint::<BigEndian>(data, len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i64) -> Result<(), byteorder::Error> {
|
|
|
|
let len = target.len();
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => target.write_int::<LittleEndian>(data, len),
|
|
|
|
layout::Endian::Big => target.write_int::<BigEndian>(data, len),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u64, byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => source.read_uint::<LittleEndian>(source.len()),
|
|
|
|
layout::Endian::Big => source.read_uint::<BigEndian>(source.len()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i64, byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => source.read_int::<LittleEndian>(source.len()),
|
|
|
|
layout::Endian::Big => source.read_int::<BigEndian>(source.len()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-06 04:51:32 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods to access floats in the target endianess
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
fn write_target_f32(endianess: layout::Endian, mut target: &mut [u8], data: f32) -> Result<(), byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => target.write_f32::<LittleEndian>(data),
|
|
|
|
layout::Endian::Big => target.write_f32::<BigEndian>(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn write_target_f64(endianess: layout::Endian, mut target: &mut [u8], data: f64) -> Result<(), byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => target.write_f64::<LittleEndian>(data),
|
|
|
|
layout::Endian::Big => target.write_f64::<BigEndian>(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_target_f32(endianess: layout::Endian, mut source: &[u8]) -> Result<f32, byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => source.read_f32::<LittleEndian>(),
|
|
|
|
layout::Endian::Big => source.read_f32::<BigEndian>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn read_target_f64(endianess: layout::Endian, mut source: &[u8]) -> Result<f64, byteorder::Error> {
|
|
|
|
match endianess {
|
|
|
|
layout::Endian::Little => source.read_f64::<LittleEndian>(),
|
|
|
|
layout::Endian::Big => source.read_f64::<BigEndian>(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 23:25:08 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Undefined byte tracking
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
type Block = u64;
|
|
|
|
const BLOCK_SIZE: usize = 64;
|
2016-03-27 00:25:35 -05:00
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct UndefMask {
|
|
|
|
blocks: Vec<Block>,
|
|
|
|
len: usize,
|
|
|
|
}
|
2016-03-27 00:25:35 -05:00
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
impl UndefMask {
|
2016-04-06 05:35:25 -05:00
|
|
|
fn new(size: usize) -> Self {
|
|
|
|
let mut m = UndefMask {
|
2016-04-06 04:45:06 -05:00
|
|
|
blocks: vec![],
|
|
|
|
len: 0,
|
2016-04-06 05:35:25 -05:00
|
|
|
};
|
|
|
|
m.grow(size, false);
|
|
|
|
m
|
2016-03-27 00:25:35 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
/// Check whether the range `start..end` (end-exclusive) is entirely defined.
|
2016-06-30 04:29:25 -05:00
|
|
|
pub fn is_range_defined(&self, start: usize, end: usize) -> bool {
|
2016-04-06 04:45:06 -05:00
|
|
|
if end > self.len { return false; }
|
|
|
|
for i in start..end {
|
|
|
|
if !self.get(i) { return false; }
|
2016-03-26 23:25:08 -05:00
|
|
|
}
|
2016-04-06 04:45:06 -05:00
|
|
|
true
|
2016-03-26 23:25:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn set_range(&mut self, start: usize, end: usize, new_state: bool) {
|
|
|
|
let len = self.len;
|
|
|
|
if end > len { self.grow(end - len, new_state); }
|
|
|
|
self.set_range_inbounds(start, end, new_state);
|
2016-03-26 23:25:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn set_range_inbounds(&mut self, start: usize, end: usize, new_state: bool) {
|
|
|
|
for i in start..end { self.set(i, new_state); }
|
2016-03-26 23:25:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn get(&self, i: usize) -> bool {
|
|
|
|
let (block, bit) = bit_index(i);
|
|
|
|
(self.blocks[block] & 1 << bit) != 0
|
2016-03-26 23:25:08 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn set(&mut self, i: usize, new_state: bool) {
|
|
|
|
let (block, bit) = bit_index(i);
|
|
|
|
if new_state {
|
|
|
|
self.blocks[block] |= 1 << bit;
|
|
|
|
} else {
|
|
|
|
self.blocks[block] &= !(1 << bit);
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 23:25:08 -05:00
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn grow(&mut self, amount: usize, new_state: bool) {
|
|
|
|
let unused_trailing_bits = self.blocks.len() * BLOCK_SIZE - self.len;
|
|
|
|
if amount > unused_trailing_bits {
|
|
|
|
let additional_blocks = amount / BLOCK_SIZE + 1;
|
|
|
|
self.blocks.extend(iter::repeat(0).take(additional_blocks));
|
|
|
|
}
|
|
|
|
let start = self.len;
|
|
|
|
self.len += amount;
|
|
|
|
self.set_range_inbounds(start, start + amount, new_state);
|
|
|
|
}
|
2016-03-26 23:25:08 -05:00
|
|
|
|
2016-06-13 04:24:01 -05:00
|
|
|
fn truncate(&mut self, length: usize) {
|
|
|
|
self.len = length;
|
|
|
|
self.blocks.truncate(self.len / BLOCK_SIZE + 1);
|
2016-07-07 04:21:18 -05:00
|
|
|
self.blocks.shrink_to_fit();
|
2016-06-13 04:24:01 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-26 23:25:08 -05:00
|
|
|
|
2016-04-06 04:45:06 -05:00
|
|
|
fn bit_index(bits: usize) -> (usize, usize) {
|
|
|
|
(bits / BLOCK_SIZE, bits % BLOCK_SIZE)
|
2016-03-05 00:48:23 -06:00
|
|
|
}
|