1 use byteorder::{ByteOrder, NativeEndian, ReadBytesExt, WriteBytesExt};
2 use std::collections::{btree_map, BTreeMap, HashMap};
3 use std::collections::Bound::{Included, Excluded};
7 use error::{EvalError, EvalResult};
10 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
11 pub struct AllocId(u64);
14 pub struct Allocation {
16 pub relocations: BTreeMap<usize, AllocId>,
18 /// Stores a list of indices `[a_0, a_1, ..., a_n]`. Bytes in the range `0..a_0` are considered
19 /// defined, `a_0..a_1` are undefined, `a_1..a_2` are defined and so on until
20 /// `a_n..bytes.len()`. These ranges are all end-exclusive.
22 /// In general a byte's definedness can be found by binary searching this list of indices,
23 /// finding where the byte would fall, and taking the position of nearest index mod 2. This
24 /// yields 0 for defined and 1 for undefined.
26 /// Some noteworthy cases:
27 /// * `[]` represents a fully-defined allocation.
28 /// * `[0]` represents a fully-undefined allocation. (The empty `0..0` is defined and
29 /// `0..bytes.len()` is undefined.)
30 /// * However, to avoid allocation, fully-undefined allocations can be represented as `None`.
31 pub undef_mask: Option<Vec<usize>>,
34 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
36 pub alloc_id: AllocId,
41 pub fn offset(self, i: isize) -> Self {
42 Pointer { offset: (self.offset as isize + i) as usize, ..self }
46 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
47 pub struct FieldRepr {
52 #[derive(Clone, Debug, Eq, PartialEq)]
54 /// Representation for a non-aggregate type such as a boolean, integer, character or pointer.
59 /// The representation for aggregate types including structs, enums, and tuples.
61 /// The size of the discriminant (an integer). Should be between 0 and 8. Always 0 for
62 /// structs and tuples.
65 /// The size of the entire aggregate, including the discriminant.
68 /// The representations of the contents of each variant.
69 variants: Vec<Vec<FieldRepr>>,
75 /// Number of elements.
81 pub fn size(&self) -> usize {
83 Repr::Primitive { size } => size,
84 Repr::Aggregate { size, .. } => size,
85 Repr::Array { elem_size, length } => elem_size * length,
91 alloc_map: HashMap<u64, Allocation>,
93 pub pointer_size: usize,
97 pub fn new() -> Self {
99 alloc_map: HashMap::new(),
102 // TODO(tsion): Should this be host's or target's usize?
103 pointer_size: mem::size_of::<usize>(),
107 pub fn allocate(&mut self, size: usize) -> Pointer {
108 let id = AllocId(self.next_id);
109 let alloc = Allocation {
110 bytes: vec![0; size].into_boxed_slice(),
111 relocations: BTreeMap::new(),
114 self.alloc_map.insert(self.next_id, alloc);
122 ////////////////////////////////////////////////////////////////////////////////
123 // Allocation accessors
124 ////////////////////////////////////////////////////////////////////////////////
126 pub fn get(&self, id: AllocId) -> EvalResult<&Allocation> {
127 self.alloc_map.get(&id.0).ok_or(EvalError::DanglingPointerDeref)
130 pub fn get_mut(&mut self, id: AllocId) -> EvalResult<&mut Allocation> {
131 self.alloc_map.get_mut(&id.0).ok_or(EvalError::DanglingPointerDeref)
134 ////////////////////////////////////////////////////////////////////////////////
136 ////////////////////////////////////////////////////////////////////////////////
138 fn get_bytes_unchecked(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
139 let alloc = try!(self.get(ptr.alloc_id));
140 if ptr.offset + size > alloc.bytes.len() {
141 return Err(EvalError::PointerOutOfBounds);
143 Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
146 fn get_bytes_unchecked_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
147 let alloc = try!(self.get_mut(ptr.alloc_id));
148 if ptr.offset + size > alloc.bytes.len() {
149 return Err(EvalError::PointerOutOfBounds);
151 Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
154 fn get_bytes(&self, ptr: Pointer, size: usize) -> EvalResult<&[u8]> {
155 if try!(self.relocations(ptr, size)).count() != 0 {
156 return Err(EvalError::ReadPointerAsBytes);
158 // TODO(tsion): Track and check for undef bytes.
159 self.get_bytes_unchecked(ptr, size)
162 fn get_bytes_mut(&mut self, ptr: Pointer, size: usize) -> EvalResult<&mut [u8]> {
163 try!(self.clear_relocations(ptr, size));
164 try!(self.mark_definedness(ptr, size, true));
165 self.get_bytes_unchecked_mut(ptr, size)
168 ////////////////////////////////////////////////////////////////////////////////
169 // Reading and writing
170 ////////////////////////////////////////////////////////////////////////////////
172 pub fn copy(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
173 // TODO(tsion): Track and check for undef bytes.
174 try!(self.check_relocation_edges(src, size));
176 let src_bytes = try!(self.get_bytes_unchecked_mut(src, size)).as_mut_ptr();
177 let dest_bytes = try!(self.get_bytes_mut(dest, size)).as_mut_ptr();
179 // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
180 // behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
181 // `dest` could possibly overlap.
183 if src.alloc_id == dest.alloc_id {
184 ptr::copy(src_bytes, dest_bytes, size);
186 ptr::copy_nonoverlapping(src_bytes, dest_bytes, size);
190 self.copy_relocations(src, dest, size)
193 pub fn write_bytes(&mut self, ptr: Pointer, src: &[u8]) -> EvalResult<()> {
194 self.get_bytes_mut(ptr, src.len()).map(|dest| dest.clone_from_slice(src))
197 pub fn read_ptr(&self, ptr: Pointer) -> EvalResult<Pointer> {
198 let size = self.pointer_size;
199 let offset = try!(self.get_bytes_unchecked(ptr, size))
200 .read_uint::<NativeEndian>(size).unwrap() as usize;
201 let alloc = try!(self.get(ptr.alloc_id));
202 match alloc.relocations.get(&ptr.offset) {
203 Some(&alloc_id) => Ok(Pointer { alloc_id: alloc_id, offset: offset }),
204 None => Err(EvalError::ReadBytesAsPointer),
208 pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<()> {
210 let size = self.pointer_size;
211 let mut bytes = try!(self.get_bytes_mut(dest, size));
212 bytes.write_uint::<NativeEndian>(ptr.offset as u64, size).unwrap();
214 try!(self.get_mut(dest.alloc_id)).relocations.insert(dest.offset, ptr.alloc_id);
218 pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<()> {
219 let pointer_size = self.pointer_size;
221 PrimVal::Bool(b) => self.write_bool(ptr, b),
222 PrimVal::I8(n) => self.write_int(ptr, n as i64, 1),
223 PrimVal::I16(n) => self.write_int(ptr, n as i64, 2),
224 PrimVal::I32(n) => self.write_int(ptr, n as i64, 4),
225 PrimVal::I64(n) => self.write_int(ptr, n as i64, 8),
226 PrimVal::U8(n) => self.write_uint(ptr, n as u64, 1),
227 PrimVal::U16(n) => self.write_uint(ptr, n as u64, 2),
228 PrimVal::U32(n) => self.write_uint(ptr, n as u64, 4),
229 PrimVal::U64(n) => self.write_uint(ptr, n as u64, 8),
230 PrimVal::IntegerPtr(n) => self.write_uint(ptr, n as u64, pointer_size),
231 PrimVal::AbstractPtr(_p) => unimplemented!(),
235 pub fn read_bool(&self, ptr: Pointer) -> EvalResult<bool> {
236 let bytes = try!(self.get_bytes(ptr, 1));
240 _ => Err(EvalError::InvalidBool),
244 pub fn write_bool(&mut self, ptr: Pointer, b: bool) -> EvalResult<()> {
245 self.get_bytes_mut(ptr, 1).map(|bytes| bytes[0] = b as u8)
248 pub fn read_int(&self, ptr: Pointer, size: usize) -> EvalResult<i64> {
249 self.get_bytes(ptr, size).map(|mut b| b.read_int::<NativeEndian>(size).unwrap())
252 pub fn write_int(&mut self, ptr: Pointer, n: i64, size: usize) -> EvalResult<()> {
253 self.get_bytes_mut(ptr, size).map(|mut b| b.write_int::<NativeEndian>(n, size).unwrap())
256 pub fn read_uint(&self, ptr: Pointer, size: usize) -> EvalResult<u64> {
257 self.get_bytes(ptr, size).map(|mut b| b.read_uint::<NativeEndian>(size).unwrap())
260 pub fn write_uint(&mut self, ptr: Pointer, n: u64, size: usize) -> EvalResult<()> {
261 self.get_bytes_mut(ptr, size).map(|mut b| b.write_uint::<NativeEndian>(n, size).unwrap())
264 pub fn read_isize(&self, ptr: Pointer) -> EvalResult<i64> {
265 self.read_int(ptr, self.pointer_size)
268 pub fn write_isize(&mut self, ptr: Pointer, n: i64) -> EvalResult<()> {
269 let size = self.pointer_size;
270 self.write_int(ptr, n, size)
273 pub fn read_usize(&self, ptr: Pointer) -> EvalResult<u64> {
274 self.read_uint(ptr, self.pointer_size)
277 pub fn write_usize(&mut self, ptr: Pointer, n: u64) -> EvalResult<()> {
278 let size = self.pointer_size;
279 self.write_uint(ptr, n, size)
282 ////////////////////////////////////////////////////////////////////////////////
284 ////////////////////////////////////////////////////////////////////////////////
286 fn relocations(&self, ptr: Pointer, size: usize)
287 -> EvalResult<btree_map::Range<usize, AllocId>>
289 let start = ptr.offset.saturating_sub(self.pointer_size - 1);
290 let end = start + size;
291 Ok(try!(self.get(ptr.alloc_id)).relocations.range(Included(&start), Excluded(&end)))
294 fn clear_relocations(&mut self, ptr: Pointer, size: usize) -> EvalResult<()> {
295 let keys: Vec<_> = try!(self.relocations(ptr, size)).map(|(&k, _)| k).collect();
296 let alloc = try!(self.get_mut(ptr.alloc_id));
298 alloc.relocations.remove(&k);
303 fn check_relocation_edges(&self, ptr: Pointer, size: usize) -> EvalResult<()> {
304 let overlapping_start = try!(self.relocations(ptr, 0)).count();
305 let overlapping_end = try!(self.relocations(ptr.offset(size as isize), 0)).count();
306 if overlapping_start + overlapping_end != 0 {
307 return Err(EvalError::ReadPointerAsBytes);
312 fn copy_relocations(&mut self, src: Pointer, dest: Pointer, size: usize) -> EvalResult<()> {
313 let relocations: Vec<_> = try!(self.relocations(src, size))
314 .map(|(&offset, &alloc_id)| {
315 // Update relocation offsets for the new positions in the destination allocation.
316 (offset + dest.offset - src.offset, alloc_id)
319 try!(self.get_mut(dest.alloc_id)).relocations.extend(relocations);
323 ////////////////////////////////////////////////////////////////////////////////
325 ////////////////////////////////////////////////////////////////////////////////
327 fn mark_definedness(&mut self, ptr: Pointer, size: usize, new_state: bool) -> EvalResult<()> {
328 let mut alloc = try!(self.get_mut(ptr.alloc_id));
329 alloc.mark_definedness(ptr.offset, ptr.offset + size, new_state);
334 ////////////////////////////////////////////////////////////////////////////////
335 // Undefined byte tracking
336 ////////////////////////////////////////////////////////////////////////////////
339 /// Check whether the range `start..end` (end-exclusive) in this allocation is entirely
341 fn is_range_defined(&self, start: usize, end: usize) -> bool {
342 debug_assert!(start <= end);
343 debug_assert!(end <= self.bytes.len());
345 // An empty range is always fully defined.
350 match self.undef_mask {
351 Some(ref undef_mask) => {
352 // If `start` lands directly on a boundary, it belongs to the range after the
353 // boundary, hence the increment in the `Ok` arm.
354 let i = match undef_mask.binary_search(&start) { Ok(j) => j + 1, Err(j) => j };
356 // The range is fully defined if and only if both:
357 // 1. The start value falls into a defined range (with even parity).
358 // 2. The end value is in the same range as the start value.
359 i % 2 == 0 && undef_mask.get(i).map(|&x| end <= x).unwrap_or(true)
365 /// Mark the range `start..end` (end-exclusive) as defined or undefined, depending on
367 fn mark_definedness(&mut self, start: usize, end: usize, new_state: bool) {
368 debug_assert!(start <= end);
369 debug_assert!(end <= self.bytes.len());
371 // There is no need to track undef masks for zero-sized allocations.
372 let len = self.bytes.len();
377 // Returns whether the new state matches the state of a given undef mask index. The way
378 // undef masks are represented, boundaries at even indices are undefined and those at odd
379 // indices are defined.
380 let index_matches_new_state = |i| i % 2 == new_state as usize;
382 // Lookup the undef mask index where the given endpoint `i` is or should be inserted.
383 let lookup_endpoint = |undef_mask: &[usize], i: usize| -> (usize, bool) {
384 let (index, should_insert);
385 match undef_mask.binary_search(&i) {
386 // Region endpoint is on an undef mask boundary.
388 // This endpoint's index must be incremented if the boundary's state matches
389 // the region's new state so that the boundary is:
390 // 1. Excluded from deletion when handling the inclusive left-hand endpoint.
391 // 2. Included for deletion when handling the exclusive right-hand endpoint.
392 index = j + index_matches_new_state(j) as usize;
394 // Don't insert a new mask boundary; simply reuse or delete the matched one.
395 should_insert = false;
398 // Region endpoint is not on a mask boundary.
400 // This is the index after the nearest mask boundary which has the same state.
403 // Insert a new boundary if this endpoint's state doesn't match the state of
405 should_insert = index_matches_new_state(j);
408 (index, should_insert)
411 match self.undef_mask {
412 // There is an existing undef mask, with arbitrary existing boundaries.
413 Some(ref mut undef_mask) => {
414 // Determine where the new range's endpoints fall within the current undef mask.
415 let (start_index, insert_start) = lookup_endpoint(undef_mask, start);
416 let (end_index, insert_end) = lookup_endpoint(undef_mask, end);
418 // Delete all the undef mask boundaries overwritten by the new range.
419 undef_mask.drain(start_index..end_index);
421 // Insert any new boundaries deemed necessary with two exceptions:
422 // 1. Never insert an endpoint equal to the allocation length; it's implicit.
423 // 2. Never insert a start boundary equal to the end boundary.
424 if insert_end && end != len {
425 undef_mask.insert(start_index, end);
427 if insert_start && start != end {
428 undef_mask.insert(start_index, start);
432 // There is no existing undef mask. This is taken as meaning the entire allocation is
433 // currently undefined. If the new state is false, meaning undefined, do nothing.
434 None => if new_state {
435 let mut mask = if start == 0 {
436 // 0..end is defined.
439 // 0..0 is defined, 0..start is undefined, start..end is defined.
443 // Don't insert the end boundary if it's equal to the allocation length; that
444 // boundary is implicit.
448 self.undef_mask = Some(mask);
456 use memory::Allocation;
457 use std::collections::BTreeMap;
459 fn alloc_with_mask(len: usize, undef_mask: Option<Vec<usize>>) -> Allocation {
461 bytes: vec![0; len].into_boxed_slice(),
462 relocations: BTreeMap::new(),
463 undef_mask: undef_mask,
468 fn large_undef_mask() {
469 let mut alloc = alloc_with_mask(20, Some(vec![4, 8, 12, 16]));
471 assert!(alloc.is_range_defined(0, 0));
472 assert!(alloc.is_range_defined(0, 3));
473 assert!(alloc.is_range_defined(0, 4));
474 assert!(alloc.is_range_defined(1, 3));
475 assert!(alloc.is_range_defined(1, 4));
476 assert!(alloc.is_range_defined(4, 4));
477 assert!(!alloc.is_range_defined(0, 5));
478 assert!(!alloc.is_range_defined(1, 5));
479 assert!(!alloc.is_range_defined(4, 5));
480 assert!(!alloc.is_range_defined(4, 8));
481 assert!(alloc.is_range_defined(8, 12));
482 assert!(!alloc.is_range_defined(12, 16));
483 assert!(alloc.is_range_defined(16, 20));
484 assert!(!alloc.is_range_defined(15, 20));
485 assert!(!alloc.is_range_defined(0, 20));
487 alloc.mark_definedness(8, 11, false);
488 assert_eq!(alloc.undef_mask, Some(vec![4, 11, 12, 16]));
490 alloc.mark_definedness(8, 11, true);
491 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
493 alloc.mark_definedness(8, 12, false);
494 assert_eq!(alloc.undef_mask, Some(vec![4, 16]));
496 alloc.mark_definedness(8, 12, true);
497 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
499 alloc.mark_definedness(9, 11, true);
500 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
502 alloc.mark_definedness(9, 11, false);
503 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 9, 11, 12, 16]));
505 alloc.mark_definedness(9, 10, true);
506 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 10, 11, 12, 16]));
508 alloc.mark_definedness(8, 12, true);
509 assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
513 fn empty_undef_mask() {
514 let mut alloc = alloc_with_mask(0, None);
515 assert!(alloc.is_range_defined(0, 0));
517 alloc.mark_definedness(0, 0, false);
518 assert_eq!(alloc.undef_mask, None);
519 assert!(alloc.is_range_defined(0, 0));
521 alloc.mark_definedness(0, 0, true);
522 assert_eq!(alloc.undef_mask, None);
523 assert!(alloc.is_range_defined(0, 0));
527 fn small_undef_mask() {
528 let mut alloc = alloc_with_mask(8, None);
530 alloc.mark_definedness(0, 4, false);
531 assert_eq!(alloc.undef_mask, None);
533 alloc.mark_definedness(0, 4, true);
534 assert_eq!(alloc.undef_mask, Some(vec![4]));
536 alloc.mark_definedness(4, 8, false);
537 assert_eq!(alloc.undef_mask, Some(vec![4]));
539 alloc.mark_definedness(4, 8, true);
540 assert_eq!(alloc.undef_mask, Some(vec![]));
542 alloc.mark_definedness(0, 8, true);
543 assert_eq!(alloc.undef_mask, Some(vec![]));
545 alloc.mark_definedness(0, 8, false);
546 assert_eq!(alloc.undef_mask, Some(vec![0]));
548 alloc.mark_definedness(0, 8, true);
549 assert_eq!(alloc.undef_mask, Some(vec![]));
551 alloc.mark_definedness(4, 8, false);
552 assert_eq!(alloc.undef_mask, Some(vec![4]));
554 alloc.mark_definedness(0, 8, false);
555 assert_eq!(alloc.undef_mask, Some(vec![0]));
557 alloc.mark_definedness(2, 5, true);
558 assert_eq!(alloc.undef_mask, Some(vec![0, 2, 5]));
560 alloc.mark_definedness(4, 6, false);
561 assert_eq!(alloc.undef_mask, Some(vec![0, 2, 4]));
563 alloc.mark_definedness(0, 3, true);
564 assert_eq!(alloc.undef_mask, Some(vec![4]));
566 alloc.mark_definedness(2, 6, true);
567 assert_eq!(alloc.undef_mask, Some(vec![6]));
569 alloc.mark_definedness(3, 7, false);
570 assert_eq!(alloc.undef_mask, Some(vec![3]));