]> git.lizzy.rs Git - rust.git/blob - src/memory.rs
4a3160607b6547e8cc2ae8aa42861df3e5cd8ec3
[rust.git] / src / memory.rs
1 use byteorder::{ByteOrder, NativeEndian, ReadBytesExt, WriteBytesExt};
2 use std::collections::{btree_map, BTreeMap, HashMap};
3 use std::collections::Bound::{Included, Excluded};
4 use std::mem;
5 use std::ptr;
6
7 use error::{EvalError, EvalResult};
8 use primval::PrimVal;
9
10 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
11 pub struct AllocId(u64);
12
13 #[derive(Debug)]
14 pub struct Allocation {
15     pub bytes: Box<[u8]>,
16     pub relocations: BTreeMap<usize, AllocId>,
17
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.
21     ///
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.
25     ///
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>>,
32 }
33
34 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
35 pub struct Pointer {
36     pub alloc_id: AllocId,
37     pub offset: usize,
38 }
39
40 impl Pointer {
41     pub fn offset(self, i: isize) -> Self {
42         Pointer { offset: (self.offset as isize + i) as usize, ..self }
43     }
44 }
45
46 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
47 pub struct FieldRepr {
48     pub offset: usize,
49     pub size: usize,
50 }
51
52 #[derive(Clone, Debug, Eq, PartialEq)]
53 pub enum Repr {
54     /// Representation for a non-aggregate type such as a boolean, integer, character or pointer.
55     Primitive {
56         size: usize
57     },
58
59     /// The representation for aggregate types including structs, enums, and tuples.
60     Aggregate {
61         /// The size of the discriminant (an integer). Should be between 0 and 8. Always 0 for
62         /// structs and tuples.
63         discr_size: usize,
64
65         /// The size of the entire aggregate, including the discriminant.
66         size: usize,
67
68         /// The representations of the contents of each variant.
69         variants: Vec<Vec<FieldRepr>>,
70     },
71
72     Array {
73         elem_size: usize,
74
75         /// Number of elements.
76         length: usize,
77     },
78 }
79
80 impl Repr {
81     pub fn size(&self) -> usize {
82         match *self {
83             Repr::Primitive { size } => size,
84             Repr::Aggregate { size, .. } => size,
85             Repr::Array { elem_size, length } => elem_size * length,
86         }
87     }
88 }
89
90 pub struct Memory {
91     alloc_map: HashMap<u64, Allocation>,
92     next_id: u64,
93     pub pointer_size: usize,
94 }
95
96 impl Memory {
97     pub fn new() -> Self {
98         Memory {
99             alloc_map: HashMap::new(),
100             next_id: 0,
101
102             // TODO(tsion): Should this be host's or target's usize?
103             pointer_size: mem::size_of::<usize>(),
104         }
105     }
106
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(),
112             undef_mask: None,
113         };
114         self.alloc_map.insert(self.next_id, alloc);
115         self.next_id += 1;
116         Pointer {
117             alloc_id: id,
118             offset: 0,
119         }
120     }
121
122     ////////////////////////////////////////////////////////////////////////////////
123     // Allocation accessors
124     ////////////////////////////////////////////////////////////////////////////////
125
126     pub fn get(&self, id: AllocId) -> EvalResult<&Allocation> {
127         self.alloc_map.get(&id.0).ok_or(EvalError::DanglingPointerDeref)
128     }
129
130     pub fn get_mut(&mut self, id: AllocId) -> EvalResult<&mut Allocation> {
131         self.alloc_map.get_mut(&id.0).ok_or(EvalError::DanglingPointerDeref)
132     }
133
134     ////////////////////////////////////////////////////////////////////////////////
135     // Byte accessors
136     ////////////////////////////////////////////////////////////////////////////////
137
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);
142         }
143         Ok(&alloc.bytes[ptr.offset..ptr.offset + size])
144     }
145
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);
150         }
151         Ok(&mut alloc.bytes[ptr.offset..ptr.offset + size])
152     }
153
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);
157         }
158         // TODO(tsion): Track and check for undef bytes.
159         self.get_bytes_unchecked(ptr, size)
160     }
161
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)
166     }
167
168     ////////////////////////////////////////////////////////////////////////////////
169     // Reading and writing
170     ////////////////////////////////////////////////////////////////////////////////
171
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));
175
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();
178
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.
182         unsafe {
183             if src.alloc_id == dest.alloc_id {
184                 ptr::copy(src_bytes, dest_bytes, size);
185             } else {
186                 ptr::copy_nonoverlapping(src_bytes, dest_bytes, size);
187             }
188         }
189
190         self.copy_relocations(src, dest, size)
191     }
192
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))
195     }
196
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),
205         }
206     }
207
208     pub fn write_ptr(&mut self, dest: Pointer, ptr: Pointer) -> EvalResult<()> {
209         {
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();
213         }
214         try!(self.get_mut(dest.alloc_id)).relocations.insert(dest.offset, ptr.alloc_id);
215         Ok(())
216     }
217
218     pub fn write_primval(&mut self, ptr: Pointer, val: PrimVal) -> EvalResult<()> {
219         let pointer_size = self.pointer_size;
220         match val {
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!(),
232         }
233     }
234
235     pub fn read_bool(&self, ptr: Pointer) -> EvalResult<bool> {
236         let bytes = try!(self.get_bytes(ptr, 1));
237         match bytes[0] {
238             0 => Ok(false),
239             1 => Ok(true),
240             _ => Err(EvalError::InvalidBool),
241         }
242     }
243
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)
246     }
247
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())
250     }
251
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())
254     }
255
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())
258     }
259
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())
262     }
263
264     pub fn read_isize(&self, ptr: Pointer) -> EvalResult<i64> {
265         self.read_int(ptr, self.pointer_size)
266     }
267
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)
271     }
272
273     pub fn read_usize(&self, ptr: Pointer) -> EvalResult<u64> {
274         self.read_uint(ptr, self.pointer_size)
275     }
276
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)
280     }
281
282     ////////////////////////////////////////////////////////////////////////////////
283     // Relocations
284     ////////////////////////////////////////////////////////////////////////////////
285
286     fn relocations(&self, ptr: Pointer, size: usize)
287         -> EvalResult<btree_map::Range<usize, AllocId>>
288     {
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)))
292     }
293
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));
297         for k in keys {
298             alloc.relocations.remove(&k);
299         }
300         Ok(())
301     }
302
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);
308         }
309         Ok(())
310     }
311
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)
317             })
318             .collect();
319         try!(self.get_mut(dest.alloc_id)).relocations.extend(relocations);
320         Ok(())
321     }
322
323     ////////////////////////////////////////////////////////////////////////////////
324     // Undefined bytes
325     ////////////////////////////////////////////////////////////////////////////////
326
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);
330         Ok(())
331     }
332 }
333
334 ////////////////////////////////////////////////////////////////////////////////
335 // Undefined byte tracking
336 ////////////////////////////////////////////////////////////////////////////////
337
338 impl Allocation {
339     /// Check whether the range `start..end` (end-exclusive) in this allocation is entirely
340     /// defined.
341     fn is_range_defined(&self, start: usize, end: usize) -> bool {
342         debug_assert!(start <= end);
343         debug_assert!(end <= self.bytes.len());
344
345         // An empty range is always fully defined.
346         if start == end {
347             return true;
348         }
349
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 };
355
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)
360             }
361             None => false,
362         }
363     }
364
365     /// Mark the range `start..end` (end-exclusive) as defined or undefined, depending on
366     /// `new_state`.
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());
370
371         // There is no need to track undef masks for zero-sized allocations.
372         let len = self.bytes.len();
373         if len == 0 {
374             return;
375         }
376
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;
381
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.
387                 Ok(j) => {
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;
393
394                     // Don't insert a new mask boundary; simply reuse or delete the matched one.
395                     should_insert = false;
396                 }
397
398                 // Region endpoint is not on a mask boundary.
399                 Err(j) => {
400                     // This is the index after the nearest mask boundary which has the same state.
401                     index = j;
402
403                     // Insert a new boundary if this endpoint's state doesn't match the state of
404                     // this position.
405                     should_insert = index_matches_new_state(j);
406                 }
407             }
408             (index, should_insert)
409         };
410
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);
417
418                 // Delete all the undef mask boundaries overwritten by the new range.
419                 undef_mask.drain(start_index..end_index);
420
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);
426                 }
427                 if insert_start && start != end {
428                     undef_mask.insert(start_index, start);
429                 }
430             }
431
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.
437                     Vec::new()
438                 } else {
439                     // 0..0 is defined, 0..start is undefined, start..end is defined.
440                     vec![0, start]
441                 };
442
443                 // Don't insert the end boundary if it's equal to the allocation length; that
444                 // boundary is implicit.
445                 if end != len {
446                     mask.push(end);
447                 }
448                 self.undef_mask = Some(mask);
449             },
450         }
451     }
452 }
453
454 #[cfg(test)]
455 mod test {
456     use memory::Allocation;
457     use std::collections::BTreeMap;
458
459     fn alloc_with_mask(len: usize, undef_mask: Option<Vec<usize>>) -> Allocation {
460         Allocation {
461             bytes: vec![0; len].into_boxed_slice(),
462             relocations: BTreeMap::new(),
463             undef_mask: undef_mask,
464         }
465     }
466
467     #[test]
468     fn large_undef_mask() {
469         let mut alloc = alloc_with_mask(20, Some(vec![4, 8, 12, 16]));
470
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));
486
487         alloc.mark_definedness(8, 11, false);
488         assert_eq!(alloc.undef_mask, Some(vec![4, 11, 12, 16]));
489
490         alloc.mark_definedness(8, 11, true);
491         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
492
493         alloc.mark_definedness(8, 12, false);
494         assert_eq!(alloc.undef_mask, Some(vec![4, 16]));
495
496         alloc.mark_definedness(8, 12, true);
497         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
498
499         alloc.mark_definedness(9, 11, true);
500         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
501
502         alloc.mark_definedness(9, 11, false);
503         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 9, 11, 12, 16]));
504
505         alloc.mark_definedness(9, 10, true);
506         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 10, 11, 12, 16]));
507
508         alloc.mark_definedness(8, 12, true);
509         assert_eq!(alloc.undef_mask, Some(vec![4, 8, 12, 16]));
510     }
511
512     #[test]
513     fn empty_undef_mask() {
514         let mut alloc = alloc_with_mask(0, None);
515         assert!(alloc.is_range_defined(0, 0));
516
517         alloc.mark_definedness(0, 0, false);
518         assert_eq!(alloc.undef_mask, None);
519         assert!(alloc.is_range_defined(0, 0));
520
521         alloc.mark_definedness(0, 0, true);
522         assert_eq!(alloc.undef_mask, None);
523         assert!(alloc.is_range_defined(0, 0));
524     }
525
526     #[test]
527     fn small_undef_mask() {
528         let mut alloc = alloc_with_mask(8, None);
529
530         alloc.mark_definedness(0, 4, false);
531         assert_eq!(alloc.undef_mask, None);
532
533         alloc.mark_definedness(0, 4, true);
534         assert_eq!(alloc.undef_mask, Some(vec![4]));
535
536         alloc.mark_definedness(4, 8, false);
537         assert_eq!(alloc.undef_mask, Some(vec![4]));
538
539         alloc.mark_definedness(4, 8, true);
540         assert_eq!(alloc.undef_mask, Some(vec![]));
541
542         alloc.mark_definedness(0, 8, true);
543         assert_eq!(alloc.undef_mask, Some(vec![]));
544
545         alloc.mark_definedness(0, 8, false);
546         assert_eq!(alloc.undef_mask, Some(vec![0]));
547
548         alloc.mark_definedness(0, 8, true);
549         assert_eq!(alloc.undef_mask, Some(vec![]));
550
551         alloc.mark_definedness(4, 8, false);
552         assert_eq!(alloc.undef_mask, Some(vec![4]));
553
554         alloc.mark_definedness(0, 8, false);
555         assert_eq!(alloc.undef_mask, Some(vec![0]));
556
557         alloc.mark_definedness(2, 5, true);
558         assert_eq!(alloc.undef_mask, Some(vec![0, 2, 5]));
559
560         alloc.mark_definedness(4, 6, false);
561         assert_eq!(alloc.undef_mask, Some(vec![0, 2, 4]));
562
563         alloc.mark_definedness(0, 3, true);
564         assert_eq!(alloc.undef_mask, Some(vec![4]));
565
566         alloc.mark_definedness(2, 6, true);
567         assert_eq!(alloc.undef_mask, Some(vec![6]));
568
569         alloc.mark_definedness(3, 7, false);
570         assert_eq!(alloc.undef_mask, Some(vec![3]));
571     }
572 }