rust/src/memory.rs

298 lines
10 KiB
Rust
Raw Normal View History

2016-03-17 03:53:03 -05:00
use byteorder::{self, ByteOrder, NativeEndian, ReadBytesExt, WriteBytesExt};
use rustc::middle::ty;
2016-03-13 15:36:25 -05:00
use std::collections::{BTreeMap, HashMap};
use std::collections::Bound::{Included, Excluded};
2016-03-15 00:03:31 -05:00
use std::mem;
2016-03-05 00:48:23 -06:00
use std::ptr;
2016-03-14 22:48:00 -05:00
use error::{EvalError, EvalResult};
use primval::PrimVal;
2016-03-05 00:48:23 -06:00
// TODO(tsion): How should this get set? Host or target pointer size?
2016-03-13 15:36:25 -05:00
const POINTER_SIZE: usize = 8;
2016-03-05 00:48:23 -06:00
pub struct Memory {
next_id: u64,
alloc_map: HashMap<u64, Allocation>,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AllocId(u64);
2016-03-05 00:50:53 -06:00
#[derive(Debug)]
2016-03-05 00:48:23 -06:00
pub struct Allocation {
pub bytes: Vec<u8>,
2016-03-13 15:36:25 -05:00
pub relocations: BTreeMap<usize, AllocId>,
2016-03-05 00:48:23 -06:00
// TODO(tsion): undef mask
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2016-03-05 00:48:23 -06:00
pub struct Pointer {
pub alloc_id: AllocId,
pub offset: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FieldRepr {
pub offset: usize,
2016-03-17 04:11:40 -05:00
pub size: usize,
2016-03-05 00:48:23 -06:00
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Repr {
2016-03-17 04:36:06 -05:00
/// Representation for a primitive type such as a boolean, integer, or character.
Primitive {
size: usize
},
Pointer,
FatPointer,
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
}
impl Memory {
pub fn new() -> Self {
Memory { next_id: 0, alloc_map: HashMap::new() }
}
2016-03-07 07:19:43 -06:00
pub fn allocate(&mut self, size: usize) -> Pointer {
2016-03-05 00:48:23 -06:00
let id = AllocId(self.next_id);
2016-03-13 15:36:25 -05:00
let alloc = Allocation { bytes: vec![0; size], relocations: BTreeMap::new() };
2016-03-05 00:48:23 -06:00
self.alloc_map.insert(self.next_id, alloc);
self.next_id += 1;
Pointer {
2016-03-07 07:19:43 -06:00
alloc_id: id,
2016-03-05 00:48:23 -06:00
offset: 0,
}
}
pub fn get(&self, id: AllocId) -> EvalResult<&Allocation> {
self.alloc_map.get(&id.0).ok_or(EvalError::DanglingPointerDeref)
}
pub fn get_mut(&mut self, id: AllocId) -> EvalResult<&mut Allocation> {
self.alloc_map.get_mut(&id.0).ok_or(EvalError::DanglingPointerDeref)
}
fn get_bytes(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
2016-03-05 00:48:23 -06:00
let alloc = try!(self.get(ptr.alloc_id));
2016-03-13 15:36:25 -05:00
try!(alloc.check_no_relocations(ptr.offset, ptr.offset + size));
2016-03-05 00:48:23 -06:00
Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
}
fn get_bytes_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
2016-03-05 00:48:23 -06:00
let alloc = try!(self.get_mut(ptr.alloc_id));
2016-03-13 15:36:25 -05:00
try!(alloc.check_no_relocations(ptr.offset, ptr.offset + size));
2016-03-05 00:48:23 -06:00
Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
}
pub fn copy(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
2016-03-13 15:36:25 -05:00
let (src_bytes, relocations) = {
let alloc = try!(self.get_mut(src.alloc_id));
try!(alloc.check_relocation_edges(src.offset, src.offset + size));
let bytes = alloc.bytes[src.offset..src.offset + size].as_mut_ptr();
let mut relocations: Vec<(usize, AllocId)> = alloc.relocations
.range(Included(&src.offset), Excluded(&(src.offset + size)))
.map(|(&k, &v)| (k, v))
.collect();
for &mut (ref mut offset, _) in &mut relocations {
alloc.relocations.remove(offset);
*offset += dest.offset - src.offset;
}
(bytes, relocations)
};
2016-03-05 00:48:23 -06:00
let dest_bytes = try!(self.get_bytes_mut(dest, size)).as_mut_ptr();
2016-03-13 15:36:25 -05:00
// TODO(tsion): Clear the destination range's existing relocations.
try!(self.get_mut(dest.alloc_id)).relocations.extend(relocations);
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);
}
}
Ok(())
}
2016-03-13 15:36:25 -05:00
pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<Pointer> {
let alloc = try!(self.get(ptr.alloc_id));
try!(alloc.check_relocation_edges(ptr.offset, ptr.offset + POINTER_SIZE));
let bytes = &alloc.bytes[ptr.offset..ptr.offset + POINTER_SIZE];
let offset = byteorder::NativeEndian::read_u64(bytes) as usize;
// TODO(tsion): Return an EvalError here instead of panicking.
let alloc_id = *alloc.relocations.get(&ptr.offset).unwrap();
Ok(Pointer { alloc_id: alloc_id, offset: offset })
}
// TODO(tsion): Detect invalid writes here and elsewhere.
pub fn write_ptr(&mut self, dest: Pointer, ptr_val: Pointer) -> EvalResult<()> {
{
let bytes = try!(self.get_bytes_mut(dest, POINTER_SIZE));
byteorder::NativeEndian::write_u64(bytes, ptr_val.offset as u64);
}
let alloc = try!(self.get_mut(dest.alloc_id));
alloc.relocations.insert(dest.offset, ptr_val.alloc_id);
Ok(())
}
2016-03-17 03:53:03 -05:00
pub fn read_primval(&self, ptr: Pointer, ty: ty::Ty) -> EvalResult<PrimVal> {
use syntax::ast::{IntTy, UintTy};
match ty.sty {
ty::TyBool => self.read_bool(ptr).map(PrimVal::Bool),
2016-03-17 07:26:37 -05:00
ty::TyInt(IntTy::I8) => self.read_int(ptr, 1).map(|n| PrimVal::I8(n as i8)),
ty::TyInt(IntTy::I16) => self.read_int(ptr, 2).map(|n| PrimVal::I16(n as i16)),
ty::TyInt(IntTy::I32) => self.read_int(ptr, 4).map(|n| PrimVal::I32(n as i32)),
ty::TyInt(IntTy::I64) => self.read_int(ptr, 8).map(|n| PrimVal::I64(n as i64)),
ty::TyUint(UintTy::U8) => self.read_uint(ptr, 1).map(|n| PrimVal::U8(n as u8)),
ty::TyUint(UintTy::U16) => self.read_uint(ptr, 2).map(|n| PrimVal::U16(n as u16)),
ty::TyUint(UintTy::U32) => self.read_uint(ptr, 4).map(|n| PrimVal::U32(n as u32)),
ty::TyUint(UintTy::U64) => self.read_uint(ptr, 8).map(|n| PrimVal::U64(n as u64)),
2016-03-17 04:12:15 -05:00
// TODO(tsion): Pick the PrimVal dynamically.
ty::TyInt(IntTy::Is) => self.read_int(ptr, POINTER_SIZE).map(PrimVal::I64),
ty::TyUint(UintTy::Us) => self.read_uint(ptr, POINTER_SIZE).map(PrimVal::U64),
2016-03-17 03:53:03 -05:00
_ => panic!("primitive read of non-primitive type: {:?}", ty),
2016-03-13 01:14:20 -06:00
}
2016-03-05 00:48:23 -06:00
}
2016-03-13 01:14:20 -06:00
pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<()> {
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),
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-05 00:48:23 -06:00
}
impl Allocation {
2016-03-13 15:36:25 -05:00
fn check_bounds(&self, start: usize, end: usize) -> EvalResult<()> {
if start <= self.bytes.len() && end <= self.bytes.len() {
2016-03-07 04:44:03 -06:00
Ok(())
} else {
Err(EvalError::PointerOutOfBounds)
2016-03-05 00:48:23 -06:00
}
}
2016-03-13 15:36:25 -05:00
fn count_overlapping_relocations(&self, start: usize, end: usize) -> usize {
self.relocations.range(
Included(&start.saturating_sub(POINTER_SIZE - 1)),
Excluded(&end)
).count()
}
fn check_relocation_edges(&self, start: usize, end: usize) -> EvalResult<()> {
try!(self.check_bounds(start, end));
let n =
self.count_overlapping_relocations(start, start) +
self.count_overlapping_relocations(end, end);
if n == 0 {
Ok(())
} else {
Err(EvalError::InvalidPointerAccess)
}
}
fn check_no_relocations(&self, start: usize, end: usize) -> EvalResult<()> {
try!(self.check_bounds(start, end));
if self.count_overlapping_relocations(start, end) == 0 {
Ok(())
} else {
Err(EvalError::InvalidPointerAccess)
}
}
2016-03-05 00:48:23 -06:00
}
impl Pointer {
pub fn offset(self, i: isize) -> Self {
Pointer { offset: (self.offset as isize + i) as usize, ..self }
2016-03-05 00:48:23 -06:00
}
}
impl Repr {
2016-03-15 00:03:31 -05:00
// TODO(tsion): Choice is based on host machine's type size. Should this be how miri works?
pub fn isize() -> Self {
2016-03-17 04:36:06 -05:00
Repr::Primitive { size: mem::size_of::<isize>() }
2016-03-15 00:03:31 -05:00
}
// TODO(tsion): Choice is based on host machine's type size. Should this be how miri works?
pub fn usize() -> Self {
2016-03-17 04:36:06 -05:00
Repr::Primitive { size: mem::size_of::<usize>() }
2016-03-15 00:03:31 -05:00
}
2016-03-05 00:48:23 -06:00
pub fn size(&self) -> usize {
match *self {
2016-03-17 04:36:06 -05:00
Repr::Primitive { size } => size,
Repr::Aggregate { size, .. } => size,
2016-03-17 04:19:13 -05:00
Repr::Array { elem_size, length } => elem_size * length,
Repr::Pointer => POINTER_SIZE,
Repr::FatPointer => POINTER_SIZE * 2,
2016-03-05 00:48:23 -06:00
}
}
}