rust/src/memory.rs

555 lines
19 KiB
Rust
Raw Normal View History

2016-03-21 06:27:34 -05:00
use byteorder::{ByteOrder, NativeEndian, ReadBytesExt, WriteBytesExt};
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};
use std::{fmt, iter, mem, ptr};
2016-03-05 00:48:23 -06:00
2016-03-14 22:48:00 -05:00
use error::{EvalError, EvalResult};
use primval::PrimVal;
2016-03-05 00:48:23 -06:00
2016-04-04 21:33:41 -05:00
////////////////////////////////////////////////////////////////////////////////
// Value representations
////////////////////////////////////////////////////////////////////////////////
2016-03-05 00:48:23 -06:00
2016-03-20 23:07:25 -05:00
#[derive(Clone, Debug, Eq, PartialEq)]
2016-03-05 00:48:23 -06:00
pub enum Repr {
2016-03-17 08:53:26 -05:00
/// Representation for a non-aggregate type such as a boolean, integer, character or pointer.
2016-03-17 04:36:06 -05:00
Primitive {
size: usize
},
2016-03-17 05:38:46 -05:00
/// The representation for aggregate types including structs, enums, and tuples.
Aggregate {
/// The size of the discriminant (an integer). Should be between 0 and 8. Always 0 for
/// structs and tuples.
2016-03-17 03:53:03 -05:00
discr_size: usize,
/// The size of the entire aggregate, including the discriminant.
size: usize,
2016-03-17 03:53:03 -05:00
2016-03-17 04:20:49 -05:00
/// The representations of the contents of each variant.
2016-03-17 05:38:46 -05:00
variants: Vec<Vec<FieldRepr>>,
},
2016-03-15 06:50:53 -05:00
Array {
2016-03-17 04:19:13 -05:00
elem_size: usize,
2016-03-15 06:50:53 -05:00
/// Number of elements.
length: usize,
},
2016-03-05 00:48:23 -06:00
}
2016-04-04 21:33:41 -05:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct FieldRepr {
pub offset: usize,
pub size: usize,
}
2016-03-23 22:40:58 -05:00
impl Repr {
pub fn size(&self) -> usize {
match *self {
Repr::Primitive { size } => size,
Repr::Aggregate { size, .. } => size,
Repr::Array { elem_size, length } => elem_size * length,
}
}
}
2016-04-04 21:33:41 -05:00
////////////////////////////////////////////////////////////////////////////////
// Allocations and pointers
////////////////////////////////////////////////////////////////////////////////
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
2016-04-04 21:33:41 -05:00
pub struct AllocId(u64);
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 {
pub bytes: Vec<u8>,
2016-04-04 21:33:41 -05:00
pub relocations: BTreeMap<usize, AllocId>,
2016-04-06 04:45:06 -05:00
pub undef_mask: UndefMask,
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 }
}
}
////////////////////////////////////////////////////////////////////////////////
// Top-level interpreter memory
////////////////////////////////////////////////////////////////////////////////
2016-03-23 22:40:58 -05:00
pub struct Memory {
alloc_map: HashMap<AllocId, Allocation>,
next_id: AllocId,
2016-03-23 22:40:58 -05:00
pub pointer_size: usize,
}
2016-03-05 00:48:23 -06:00
impl Memory {
pub fn new() -> Self {
2016-03-17 08:53:26 -05:00
Memory {
alloc_map: HashMap::new(),
next_id: AllocId(0),
2016-03-17 08:53:26 -05:00
// TODO(tsion): Should this be host's or target's usize?
pointer_size: mem::size_of::<usize>(),
}
2016-03-05 00:48:23 -06:00
}
2016-03-07 07:19:43 -06:00
pub fn allocate(&mut self, size: usize) -> Pointer {
let alloc = Allocation {
bytes: vec![0; size],
relocations: BTreeMap::new(),
undef_mask: UndefMask::new(size),
};
let id = self.next_id;
self.next_id.0 += 1;
self.alloc_map.insert(id, alloc);
2016-03-05 00:48:23 -06:00
Pointer {
2016-03-07 07:19:43 -06:00
alloc_id: id,
2016-03-05 00:48:23 -06:00
offset: 0,
}
}
// TODO(tsion): Track which allocations were returned from __rust_allocate and report an error
// when reallocating/deallocating any others.
pub fn reallocate(&mut self, ptr: Pointer, new_size: usize) -> EvalResult<()> {
if ptr.offset != 0 {
// TODO(tsion): Report error about non-__rust_allocate'd pointer.
panic!()
}
let alloc = try!(self.get_mut(ptr.alloc_id));
let size = alloc.bytes.len();
if new_size > size {
let amount = new_size - size;
alloc.bytes.extend(iter::repeat(0).take(amount));
alloc.undef_mask.grow(amount, false);
} else if size > new_size {
unimplemented!()
// alloc.bytes.truncate(new_size);
// alloc.undef_mask.len = new_size;
// TODO: potentially remove relocations
}
Ok(())
}
2016-04-07 04:02:02 -05:00
// TODO(tsion): See comment on `reallocate`.
pub fn deallocate(&mut self, ptr: Pointer) -> EvalResult<()> {
if ptr.offset != 0 {
// TODO(tsion): Report error about non-__rust_allocate'd pointer.
panic!()
}
if self.alloc_map.remove(&ptr.alloc_id).is_none() {
2016-04-07 04:02:02 -05:00
// TODO(tsion): Report error about erroneous free. This is blocked on properly tracking
// already-dropped state since this if-statement is entered even in safe code without
// it.
}
Ok(())
}
2016-03-23 22:40:58 -05:00
////////////////////////////////////////////////////////////////////////////////
// Allocation accessors
////////////////////////////////////////////////////////////////////////////////
2016-03-05 00:48:23 -06:00
pub fn get(&self, id: AllocId) -> EvalResult<&Allocation> {
self.alloc_map.get(&id).ok_or(EvalError::DanglingPointerDeref)
2016-03-05 00:48:23 -06:00
}
pub fn get_mut(&mut self, id: AllocId) -> EvalResult<&mut Allocation> {
self.alloc_map.get_mut(&id).ok_or(EvalError::DanglingPointerDeref)
2016-03-05 00:48:23 -06: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() {
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![];
let alloc = match self.alloc_map.get(&id) {
Some(a) => a,
None => {
println!("(deallocated)");
continue;
}
};
2016-04-06 04:45:06 -05:00
for i in 0..alloc.bytes.len() {
if let Some(&target_id) = alloc.relocations.get(&i) {
if !allocs_seen.contains(&target_id) {
2016-04-06 04:45:06 -05:00
allocs_to_print.push_back(target_id);
}
relocations.push((i, target_id));
2016-04-06 04:45:06 -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;
let relocation_width = (self.pointer_size - 1) * 3;
for (i, target_id) in relocations {
print!("{:1$}", "", (i - pos) * 3);
print!("{0:─^1$}", format!("({})", target_id), relocation_width);
pos = i + self.pointer_size;
}
println!("");
}
}
}
2016-03-23 22:40:58 -05:00
////////////////////////////////////////////////////////////////////////////////
// Byte accessors
////////////////////////////////////////////////////////////////////////////////
2016-03-23 20:44:05 -05:00
fn get_bytes_unchecked(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
let alloc = try!(self.get(ptr.alloc_id));
if ptr.offset + size > alloc.bytes.len() {
return Err(EvalError::PointerOutOfBounds);
}
Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
2016-03-05 00:48:23 -06:00
}
2016-03-23 20:44:05 -05:00
fn get_bytes_unchecked_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
let alloc = try!(self.get_mut(ptr.alloc_id));
if ptr.offset + size > alloc.bytes.len() {
return Err(EvalError::PointerOutOfBounds);
}
Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
2016-03-05 00:48:23 -06:00
}
2016-03-23 20:44:05 -05:00
fn get_bytes(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
if try!(self.relocations(ptr, size)).count() != 0 {
return Err(EvalError::ReadPointerAsBytes);
2016-03-21 06:27:34 -05:00
}
try!(self.check_defined(ptr, size));
2016-03-23 20:44:05 -05:00
self.get_bytes_unchecked(ptr, size)
}
fn get_bytes_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
try!(self.clear_relocations(ptr, size));
2016-03-26 23:25:08 -05:00
try!(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-03-23 22:40:58 -05:00
////////////////////////////////////////////////////////////////////////////////
// Reading and writing
////////////////////////////////////////////////////////////////////////////////
2016-03-21 03:37:31 -05:00
pub fn copy(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
2016-03-21 06:27:34 -05:00
try!(self.check_relocation_edges(src, size));
2016-03-23 20:44:05 -05:00
let src_bytes = try!(self.get_bytes_unchecked_mut(src, size)).as_mut_ptr();
2016-03-05 00:48:23 -06:00
let dest_bytes = try!(self.get_bytes_mut(dest, size)).as_mut_ptr();
// 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);
}
}
try!(self.copy_undef_mask(src, dest, size));
try!(self.copy_relocations(src, dest, size));
Ok(())
2016-03-05 00:48:23 -06:00
}
2016-03-19 00:19:39 -05:00
pub fn write_bytes(&mut self, ptr: Pointer, src: &[u8]) -> EvalResult<()> {
2016-04-07 06:56:07 -05:00
let bytes = try!(self.get_bytes_mut(ptr, src.len()));
bytes.clone_from_slice(src);
Ok(())
}
pub fn write_repeat(&mut self, ptr: Pointer, val: u8, count: usize) -> EvalResult<()> {
let bytes = try!(self.get_bytes_mut(ptr, count));
for b in bytes { *b = val; }
Ok(())
}
pub fn drop_fill(&mut self, ptr: Pointer, size: usize) -> EvalResult<()> {
self.write_repeat(ptr, mem::POST_DROP_U8, size)
2016-03-19 00:19:39 -05:00
}
2016-03-13 15:36:25 -05:00
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<Pointer> {
2016-03-23 20:44:05 -05:00
let size = self.pointer_size;
try!(self.check_defined(ptr, size));
2016-03-23 20:44:05 -05:00
let offset = try!(self.get_bytes_unchecked(ptr, size))
.read_uint::<NativeEndian>(size).unwrap() as usize;
2016-03-13 15:36:25 -05:00
let alloc = try!(self.get(ptr.alloc_id));
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-03-21 06:27:34 -05:00
pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<()> {
2016-03-13 15:36:25 -05:00
{
2016-03-17 08:53:26 -05:00
let size = self.pointer_size;
2016-03-21 06:27:34 -05:00
let mut bytes = try!(self.get_bytes_mut(dest, size));
bytes.write_uint::<NativeEndian>(ptr.offset as u64, size).unwrap();
2016-03-13 15:36:25 -05:00
}
2016-03-21 06:27:34 -05:00
try!(self.get_mut(dest.alloc_id)).relocations.insert(dest.offset, ptr.alloc_id);
2016-03-13 15:36:25 -05:00
Ok(())
}
2016-03-13 01:14:20 -06:00
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<()> {
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),
PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
PrimVal::AbstractPtr(_p) => unimplemented!(),
2016-03-13 01:14:20 -06:00
}
2016-03-07 04:44:03 -06:00
}
pub fn read_bool(&self, ptr: Pointer) -> EvalResult<bool> {
2016-03-07 04:44:03 -06:00
let bytes = try!(self.get_bytes(ptr, 1));
match bytes[0] {
0 => Ok(false),
1 => Ok(true),
_ => Err(EvalError::InvalidBool),
}
}
pub fn write_bool(&mut self, ptr: Pointer, b: bool) -> EvalResult<()> {
2016-03-17 07:39:29 -05:00
self.get_bytes_mut(ptr, 1).map(|bytes| bytes[0] = b as u8)
2016-03-05 00:48:23 -06:00
}
2016-03-13 01:14:20 -06:00
2016-03-17 04:12:15 -05:00
pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<i64> {
self.get_bytes(ptr, size).map(|mut b| b.read_int::<NativeEndian>(size).unwrap())
}
pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<()> {
self.get_bytes_mut(ptr, size).map(|mut b| b.write_int::<NativeEndian>(n, size).unwrap())
2016-03-13 01:14:20 -06:00
}
2016-03-15 00:03:31 -05:00
2016-03-17 03:53:03 -05:00
pub fn read_uint(&self, ptr: Pointer, size: usize) -> EvalResult<u64> {
self.get_bytes(ptr, size).map(|mut b| b.read_uint::<NativeEndian>(size).unwrap())
}
pub fn write_uint(&mut self, ptr: Pointer, n: u64, size: usize) -> EvalResult<()> {
self.get_bytes_mut(ptr, size).map(|mut b| b.write_uint::<NativeEndian>(n, size).unwrap())
}
2016-03-21 00:24:27 -05:00
pub fn read_isize(&self, ptr: Pointer) -> EvalResult<i64> {
self.read_int(ptr, self.pointer_size)
}
pub fn write_isize(&mut self, ptr: Pointer, n: i64) -> EvalResult<()> {
let size = self.pointer_size;
self.write_int(ptr, n, size)
}
pub fn read_usize(&self, ptr: Pointer) -> EvalResult<u64> {
self.read_uint(ptr, self.pointer_size)
}
pub fn write_usize(&mut self, ptr: Pointer, n: u64) -> EvalResult<()> {
let size = self.pointer_size;
self.write_uint(ptr, n, size)
}
2016-03-05 00:48:23 -06:00
2016-03-23 22:40:58 -05:00
////////////////////////////////////////////////////////////////////////////////
// Relocations
////////////////////////////////////////////////////////////////////////////////
fn relocations(&self, ptr: Pointer, size: usize)
-> EvalResult<btree_map::Range<usize, AllocId>>
{
let start = ptr.offset.saturating_sub(self.pointer_size - 1);
let end = start + size;
Ok(try!(self.get(ptr.alloc_id)).relocations.range(Included(&start), Excluded(&end)))
2016-03-05 00:48:23 -06:00
}
2016-03-23 22:40:58 -05:00
fn clear_relocations(&mut self, ptr: Pointer, size: usize) -> EvalResult<()> {
// Find all relocations overlapping the given range.
2016-03-23 22:40:58 -05:00
let keys: Vec<_> = try!(self.relocations(ptr, size)).map(|(&k, _)| k).collect();
if keys.len() == 0 { return Ok(()); }
// 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();
let last = *keys.last().unwrap() + self.pointer_size;
2016-03-23 22:40:58 -05:00
let alloc = try!(self.get_mut(ptr.alloc_id));
// 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); }
// Forget all the relocations.
for k in keys { alloc.relocations.remove(&k); }
2016-03-23 22:40:58 -05:00
Ok(())
}
fn check_relocation_edges(&self, ptr: Pointer, size: usize) -> EvalResult<()> {
let overlapping_start = try!(self.relocations(ptr, 0)).count();
let overlapping_end = try!(self.relocations(ptr.offset(size as isize), 0)).count();
if overlapping_start + overlapping_end != 0 {
return Err(EvalError::ReadPointerAsBytes);
}
Ok(())
}
fn copy_relocations(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
let relocations: Vec<_> = try!(self.relocations(src, size))
.map(|(&offset, &alloc_id)| {
// Update relocation offsets for the new positions in the destination allocation.
(offset + dest.offset - src.offset, alloc_id)
})
.collect();
try!(self.get_mut(dest.alloc_id)).relocations.extend(relocations);
Ok(())
2016-03-05 00:48:23 -06:00
}
2016-03-26 23:25:08 -05:00
////////////////////////////////////////////////////////////////////////////////
// Undefined bytes
////////////////////////////////////////////////////////////////////////////////
// FIXME(tsino): This is a very naive, slow version.
fn copy_undef_mask(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
// 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 {
let defined = try!(self.get(src.alloc_id)).undef_mask.get(src.offset + i);
v.push(defined);
}
for (i, defined) in v.into_iter().enumerate() {
try!(self.get_mut(dest.alloc_id)).undef_mask.set(dest.offset + i, defined);
}
Ok(())
}
fn check_defined(&self, ptr: Pointer, size: usize) -> EvalResult<()> {
let alloc = try!(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) {
return Err(EvalError::ReadUndefBytes);
}
Ok(())
}
pub fn mark_definedness(&mut self, ptr: Pointer, size: usize, new_state: bool)
-> EvalResult<()>
{
2016-03-26 23:25:08 -05:00
let mut alloc = try!(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(())
}
}
////////////////////////////////////////////////////////////////////////////////
// Undefined byte tracking
////////////////////////////////////////////////////////////////////////////////
2016-04-06 04:45:06 -05:00
type Block = u64;
const BLOCK_SIZE: usize = 64;
2016-04-06 04:45:06 -05:00
#[derive(Clone, Debug)]
pub struct UndefMask {
blocks: Vec<Block>,
len: usize,
}
2016-04-06 04:45:06 -05:00
impl UndefMask {
fn new(size: usize) -> Self {
let mut m = UndefMask {
2016-04-06 04:45:06 -05:00
blocks: vec![],
len: 0,
};
m.grow(size, false);
m
}
2016-04-06 04:45:06 -05:00
/// Check whether the range `start..end` (end-exclusive) is entirely defined.
fn is_range_defined(&self, start: usize, end: usize) -> bool {
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-04-06 04:45:06 -05:00
// fn uniform_block(state: bool) -> Block {
// if state { !0 } else { 0 }
// }
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
}