]> git.lizzy.rs Git - rust.git/blob - src/librustc_middle/mir/interpret/allocation.rs
Remove outdated reference to interpreter snapshotting
[rust.git] / src / librustc_middle / mir / interpret / allocation.rs
1 //! The virtual memory representation of the MIR interpreter.
2
3 use std::borrow::Cow;
4 use std::convert::TryFrom;
5 use std::iter;
6 use std::ops::{Deref, DerefMut, Range};
7
8 use rustc_ast::ast::Mutability;
9 use rustc_data_structures::sorted_map::SortedMap;
10 use rustc_target::abi::{Align, HasDataLayout, Size};
11
12 use super::{
13     read_target_uint, write_target_uint, AllocId, InterpResult, Pointer, Scalar, ScalarMaybeUndef,
14 };
15
16 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
17 #[derive(HashStable)]
18 pub struct Allocation<Tag = (), Extra = ()> {
19     /// The actual bytes of the allocation.
20     /// Note that the bytes of a pointer represent the offset of the pointer.
21     bytes: Vec<u8>,
22     /// Maps from byte addresses to extra data for each pointer.
23     /// Only the first byte of a pointer is inserted into the map; i.e.,
24     /// every entry in this map applies to `pointer_size` consecutive bytes starting
25     /// at the given offset.
26     relocations: Relocations<Tag>,
27     /// Denotes which part of this allocation is initialized.
28     undef_mask: UndefMask,
29     /// The size of the allocation. Currently, must always equal `bytes.len()`.
30     pub size: Size,
31     /// The alignment of the allocation to detect unaligned reads.
32     /// (`Align` guarantees that this is a power of two.)
33     pub align: Align,
34     /// `true` if the allocation is mutable.
35     /// Also used by codegen to determine if a static should be put into mutable memory,
36     /// which happens for `static mut` and `static` with interior mutability.
37     pub mutability: Mutability,
38     /// Extra state for the machine.
39     pub extra: Extra,
40 }
41
42 pub trait AllocationExtra<Tag>: ::std::fmt::Debug + Clone {
43     // There is no constructor in here because the constructor's type depends
44     // on `MemoryKind`, and making things sufficiently generic leads to painful
45     // inference failure.
46
47     /// Hook for performing extra checks on a memory read access.
48     ///
49     /// Takes read-only access to the allocation so we can keep all the memory read
50     /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
51     /// need to mutate.
52     #[inline(always)]
53     fn memory_read(
54         _alloc: &Allocation<Tag, Self>,
55         _ptr: Pointer<Tag>,
56         _size: Size,
57     ) -> InterpResult<'tcx> {
58         Ok(())
59     }
60
61     /// Hook for performing extra checks on a memory write access.
62     #[inline(always)]
63     fn memory_written(
64         _alloc: &mut Allocation<Tag, Self>,
65         _ptr: Pointer<Tag>,
66         _size: Size,
67     ) -> InterpResult<'tcx> {
68         Ok(())
69     }
70
71     /// Hook for performing extra checks on a memory deallocation.
72     /// `size` will be the size of the allocation.
73     #[inline(always)]
74     fn memory_deallocated(
75         _alloc: &mut Allocation<Tag, Self>,
76         _ptr: Pointer<Tag>,
77         _size: Size,
78     ) -> InterpResult<'tcx> {
79         Ok(())
80     }
81 }
82
83 // For `Tag = ()` and no extra state, we have a trivial implementation.
84 impl AllocationExtra<()> for () {}
85
86 // The constructors are all without extra; the extra gets added by a machine hook later.
87 impl<Tag> Allocation<Tag> {
88     /// Creates a read-only allocation initialized by the given bytes
89     pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
90         let bytes = slice.into().into_owned();
91         let size = Size::from_bytes(bytes.len());
92         Self {
93             bytes,
94             relocations: Relocations::new(),
95             undef_mask: UndefMask::new(size, true),
96             size,
97             align,
98             mutability: Mutability::Not,
99             extra: (),
100         }
101     }
102
103     pub fn from_byte_aligned_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>) -> Self {
104         Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
105     }
106
107     pub fn undef(size: Size, align: Align) -> Self {
108         Allocation {
109             bytes: vec![0; size.bytes_usize()],
110             relocations: Relocations::new(),
111             undef_mask: UndefMask::new(size, false),
112             size,
113             align,
114             mutability: Mutability::Mut,
115             extra: (),
116         }
117     }
118 }
119
120 impl Allocation<(), ()> {
121     /// Add Tag and Extra fields
122     pub fn with_tags_and_extra<T, E>(
123         self,
124         mut tagger: impl FnMut(AllocId) -> T,
125         extra: E,
126     ) -> Allocation<T, E> {
127         Allocation {
128             bytes: self.bytes,
129             size: self.size,
130             relocations: Relocations::from_presorted(
131                 self.relocations
132                     .iter()
133                     // The allocations in the relocations (pointers stored *inside* this allocation)
134                     // all get the base pointer tag.
135                     .map(|&(offset, ((), alloc))| {
136                         let tag = tagger(alloc);
137                         (offset, (tag, alloc))
138                     })
139                     .collect(),
140             ),
141             undef_mask: self.undef_mask,
142             align: self.align,
143             mutability: self.mutability,
144             extra,
145         }
146     }
147 }
148
149 /// Raw accessors. Provide access to otherwise private bytes.
150 impl<Tag, Extra> Allocation<Tag, Extra> {
151     pub fn len(&self) -> usize {
152         self.size.bytes_usize()
153     }
154
155     /// Looks at a slice which may describe undefined bytes or describe a relocation. This differs
156     /// from `get_bytes_with_undef_and_ptr` in that it does no relocation checks (even on the
157     /// edges) at all. It further ignores `AllocationExtra` callbacks.
158     /// This must not be used for reads affecting the interpreter execution.
159     pub fn inspect_with_undef_and_ptr_outside_interpreter(&self, range: Range<usize>) -> &[u8] {
160         &self.bytes[range]
161     }
162
163     /// Returns the undef mask.
164     pub fn undef_mask(&self) -> &UndefMask {
165         &self.undef_mask
166     }
167
168     /// Returns the relocation list.
169     pub fn relocations(&self) -> &Relocations<Tag> {
170         &self.relocations
171     }
172 }
173
174 impl<'tcx> rustc_serialize::UseSpecializedDecodable for &'tcx Allocation {}
175
176 /// Byte accessors.
177 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
178     /// Just a small local helper function to avoid a bit of code repetition.
179     /// Returns the range of this allocation that was meant.
180     #[inline]
181     fn check_bounds(&self, offset: Size, size: Size) -> Range<usize> {
182         let end = offset + size; // This does overflow checking.
183         let end = usize::try_from(end.bytes()).expect("access too big for this host architecture");
184         assert!(
185             end <= self.len(),
186             "Out-of-bounds access at offset {}, size {} in allocation of size {}",
187             offset.bytes(),
188             size.bytes(),
189             self.len()
190         );
191         offset.bytes_usize()..end
192     }
193
194     /// The last argument controls whether we error out when there are undefined
195     /// or pointer bytes. You should never call this, call `get_bytes` or
196     /// `get_bytes_with_undef_and_ptr` instead,
197     ///
198     /// This function also guarantees that the resulting pointer will remain stable
199     /// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
200     /// on that.
201     ///
202     /// It is the caller's responsibility to check bounds and alignment beforehand.
203     fn get_bytes_internal(
204         &self,
205         cx: &impl HasDataLayout,
206         ptr: Pointer<Tag>,
207         size: Size,
208         check_defined_and_ptr: bool,
209     ) -> InterpResult<'tcx, &[u8]> {
210         let range = self.check_bounds(ptr.offset, size);
211
212         if check_defined_and_ptr {
213             self.check_defined(ptr, size)?;
214             self.check_relocations(cx, ptr, size)?;
215         } else {
216             // We still don't want relocations on the *edges*.
217             self.check_relocation_edges(cx, ptr, size)?;
218         }
219
220         AllocationExtra::memory_read(self, ptr, size)?;
221
222         Ok(&self.bytes[range])
223     }
224
225     /// Checks that these bytes are initialized and not pointer bytes, and then return them
226     /// as a slice.
227     ///
228     /// It is the caller's responsibility to check bounds and alignment beforehand.
229     /// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
230     /// on `InterpCx` instead.
231     #[inline]
232     pub fn get_bytes(
233         &self,
234         cx: &impl HasDataLayout,
235         ptr: Pointer<Tag>,
236         size: Size,
237     ) -> InterpResult<'tcx, &[u8]> {
238         self.get_bytes_internal(cx, ptr, size, true)
239     }
240
241     /// It is the caller's responsibility to handle undefined and pointer bytes.
242     /// However, this still checks that there are no relocations on the *edges*.
243     ///
244     /// It is the caller's responsibility to check bounds and alignment beforehand.
245     #[inline]
246     pub fn get_bytes_with_undef_and_ptr(
247         &self,
248         cx: &impl HasDataLayout,
249         ptr: Pointer<Tag>,
250         size: Size,
251     ) -> InterpResult<'tcx, &[u8]> {
252         self.get_bytes_internal(cx, ptr, size, false)
253     }
254
255     /// Just calling this already marks everything as defined and removes relocations,
256     /// so be sure to actually put data there!
257     ///
258     /// It is the caller's responsibility to check bounds and alignment beforehand.
259     /// Most likely, you want to use the `PlaceTy` and `OperandTy`-based methods
260     /// on `InterpCx` instead.
261     pub fn get_bytes_mut(
262         &mut self,
263         cx: &impl HasDataLayout,
264         ptr: Pointer<Tag>,
265         size: Size,
266     ) -> InterpResult<'tcx, &mut [u8]> {
267         let range = self.check_bounds(ptr.offset, size);
268
269         self.mark_definedness(ptr, size, true);
270         self.clear_relocations(cx, ptr, size)?;
271
272         AllocationExtra::memory_written(self, ptr, size)?;
273
274         Ok(&mut self.bytes[range])
275     }
276 }
277
278 /// Reading and writing.
279 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
280     /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
281     /// before a `0` is found.
282     ///
283     /// Most likely, you want to call `Memory::read_c_str` instead of this method.
284     pub fn read_c_str(
285         &self,
286         cx: &impl HasDataLayout,
287         ptr: Pointer<Tag>,
288     ) -> InterpResult<'tcx, &[u8]> {
289         let offset = ptr.offset.bytes_usize();
290         Ok(match self.bytes[offset..].iter().position(|&c| c == 0) {
291             Some(size) => {
292                 let size_with_null = Size::from_bytes(size) + Size::from_bytes(1);
293                 // Go through `get_bytes` for checks and AllocationExtra hooks.
294                 // We read the null, so we include it in the request, but we want it removed
295                 // from the result, so we do subslicing.
296                 &self.get_bytes(cx, ptr, size_with_null)?[..size]
297             }
298             // This includes the case where `offset` is out-of-bounds to begin with.
299             None => throw_ub!(UnterminatedCString(ptr.erase_tag())),
300         })
301     }
302
303     /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
304     /// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
305     /// given range contains neither relocations nor undef bytes.
306     pub fn check_bytes(
307         &self,
308         cx: &impl HasDataLayout,
309         ptr: Pointer<Tag>,
310         size: Size,
311         allow_ptr_and_undef: bool,
312     ) -> InterpResult<'tcx> {
313         // Check bounds and relocations on the edges.
314         self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
315         // Check undef and ptr.
316         if !allow_ptr_and_undef {
317             self.check_defined(ptr, size)?;
318             self.check_relocations(cx, ptr, size)?;
319         }
320         Ok(())
321     }
322
323     /// Writes `src` to the memory starting at `ptr.offset`.
324     ///
325     /// It is the caller's responsibility to check bounds and alignment beforehand.
326     /// Most likely, you want to call `Memory::write_bytes` instead of this method.
327     pub fn write_bytes(
328         &mut self,
329         cx: &impl HasDataLayout,
330         ptr: Pointer<Tag>,
331         src: impl IntoIterator<Item = u8>,
332     ) -> InterpResult<'tcx> {
333         let mut src = src.into_iter();
334         let (lower, upper) = src.size_hint();
335         let len = upper.expect("can only write bounded iterators");
336         assert_eq!(lower, len, "can only write iterators with a precise length");
337         let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(len))?;
338         // `zip` would stop when the first iterator ends; we want to definitely
339         // cover all of `bytes`.
340         for dest in bytes {
341             *dest = src.next().expect("iterator was shorter than it said it would be");
342         }
343         src.next().expect_none("iterator was longer than it said it would be");
344         Ok(())
345     }
346
347     /// Reads a *non-ZST* scalar.
348     ///
349     /// ZSTs can't be read for two reasons:
350     /// * byte-order cannot work with zero-element buffers;
351     /// * in order to obtain a `Pointer`, we need to check for ZSTness anyway due to integer
352     ///   pointers being valid for ZSTs.
353     ///
354     /// It is the caller's responsibility to check bounds and alignment beforehand.
355     /// Most likely, you want to call `InterpCx::read_scalar` instead of this method.
356     pub fn read_scalar(
357         &self,
358         cx: &impl HasDataLayout,
359         ptr: Pointer<Tag>,
360         size: Size,
361     ) -> InterpResult<'tcx, ScalarMaybeUndef<Tag>> {
362         // `get_bytes_unchecked` tests relocation edges.
363         let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
364         // Undef check happens *after* we established that the alignment is correct.
365         // We must not return `Ok()` for unaligned pointers!
366         if self.is_defined(ptr, size).is_err() {
367             // This inflates undefined bytes to the entire scalar, even if only a few
368             // bytes are undefined.
369             return Ok(ScalarMaybeUndef::Undef);
370         }
371         // Now we do the actual reading.
372         let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
373         // See if we got a pointer.
374         if size != cx.data_layout().pointer_size {
375             // *Now*, we better make sure that the inside is free of relocations too.
376             self.check_relocations(cx, ptr, size)?;
377         } else {
378             if let Some(&(tag, alloc_id)) = self.relocations.get(&ptr.offset) {
379                 let ptr = Pointer::new_with_tag(alloc_id, Size::from_bytes(bits), tag);
380                 return Ok(ScalarMaybeUndef::Scalar(ptr.into()));
381             }
382         }
383         // We don't. Just return the bits.
384         Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
385     }
386
387     /// Reads a pointer-sized scalar.
388     ///
389     /// It is the caller's responsibility to check bounds and alignment beforehand.
390     /// Most likely, you want to call `InterpCx::read_scalar` instead of this method.
391     pub fn read_ptr_sized(
392         &self,
393         cx: &impl HasDataLayout,
394         ptr: Pointer<Tag>,
395     ) -> InterpResult<'tcx, ScalarMaybeUndef<Tag>> {
396         self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
397     }
398
399     /// Writes a *non-ZST* scalar.
400     ///
401     /// ZSTs can't be read for two reasons:
402     /// * byte-order cannot work with zero-element buffers;
403     /// * in order to obtain a `Pointer`, we need to check for ZSTness anyway due to integer
404     ///   pointers being valid for ZSTs.
405     ///
406     /// It is the caller's responsibility to check bounds and alignment beforehand.
407     /// Most likely, you want to call `InterpCx::write_scalar` instead of this method.
408     pub fn write_scalar(
409         &mut self,
410         cx: &impl HasDataLayout,
411         ptr: Pointer<Tag>,
412         val: ScalarMaybeUndef<Tag>,
413         type_size: Size,
414     ) -> InterpResult<'tcx> {
415         let val = match val {
416             ScalarMaybeUndef::Scalar(scalar) => scalar,
417             ScalarMaybeUndef::Undef => {
418                 self.mark_definedness(ptr, type_size, false);
419                 return Ok(());
420             }
421         };
422
423         let bytes = match val.to_bits_or_ptr(type_size, cx) {
424             Err(val) => u128::from(val.offset.bytes()),
425             Ok(data) => data,
426         };
427
428         let endian = cx.data_layout().endian;
429         let dst = self.get_bytes_mut(cx, ptr, type_size)?;
430         write_target_uint(endian, dst, bytes).unwrap();
431
432         // See if we have to also write a relocation.
433         if let Scalar::Ptr(val) = val {
434             self.relocations.insert(ptr.offset, (val.tag, val.alloc_id));
435         }
436
437         Ok(())
438     }
439
440     /// Writes a pointer-sized scalar.
441     ///
442     /// It is the caller's responsibility to check bounds and alignment beforehand.
443     /// Most likely, you want to call `InterpCx::write_scalar` instead of this method.
444     pub fn write_ptr_sized(
445         &mut self,
446         cx: &impl HasDataLayout,
447         ptr: Pointer<Tag>,
448         val: ScalarMaybeUndef<Tag>,
449     ) -> InterpResult<'tcx> {
450         let ptr_size = cx.data_layout().pointer_size;
451         self.write_scalar(cx, ptr, val, ptr_size)
452     }
453 }
454
455 /// Relocations.
456 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
457     /// Returns all relocations overlapping with the given pointer-offset pair.
458     pub fn get_relocations(
459         &self,
460         cx: &impl HasDataLayout,
461         ptr: Pointer<Tag>,
462         size: Size,
463     ) -> &[(Size, (Tag, AllocId))] {
464         // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
465         // the beginning of this range.
466         let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
467         let end = ptr.offset + size; // This does overflow checking.
468         self.relocations.range(Size::from_bytes(start)..end)
469     }
470
471     /// Checks that there are no relocations overlapping with the given range.
472     #[inline(always)]
473     fn check_relocations(
474         &self,
475         cx: &impl HasDataLayout,
476         ptr: Pointer<Tag>,
477         size: Size,
478     ) -> InterpResult<'tcx> {
479         if self.get_relocations(cx, ptr, size).is_empty() {
480             Ok(())
481         } else {
482             throw_unsup!(ReadPointerAsBytes)
483         }
484     }
485
486     /// Removes all relocations inside the given range.
487     /// If there are relocations overlapping with the edges, they
488     /// are removed as well *and* the bytes they cover are marked as
489     /// uninitialized. This is a somewhat odd "spooky action at a distance",
490     /// but it allows strictly more code to run than if we would just error
491     /// immediately in that case.
492     fn clear_relocations(
493         &mut self,
494         cx: &impl HasDataLayout,
495         ptr: Pointer<Tag>,
496         size: Size,
497     ) -> InterpResult<'tcx> {
498         // Find the start and end of the given range and its outermost relocations.
499         let (first, last) = {
500             // Find all relocations overlapping the given range.
501             let relocations = self.get_relocations(cx, ptr, size);
502             if relocations.is_empty() {
503                 return Ok(());
504             }
505
506             (
507                 relocations.first().unwrap().0,
508                 relocations.last().unwrap().0 + cx.data_layout().pointer_size,
509             )
510         };
511         let start = ptr.offset;
512         let end = start + size; // `Size` addition
513
514         // Mark parts of the outermost relocations as undefined if they partially fall outside the
515         // given range.
516         if first < start {
517             self.undef_mask.set_range(first, start, false);
518         }
519         if last > end {
520             self.undef_mask.set_range(end, last, false);
521         }
522
523         // Forget all the relocations.
524         self.relocations.remove_range(first..last);
525
526         Ok(())
527     }
528
529     /// Errors if there are relocations overlapping with the edges of the
530     /// given memory range.
531     #[inline]
532     fn check_relocation_edges(
533         &self,
534         cx: &impl HasDataLayout,
535         ptr: Pointer<Tag>,
536         size: Size,
537     ) -> InterpResult<'tcx> {
538         self.check_relocations(cx, ptr, Size::ZERO)?;
539         self.check_relocations(cx, ptr.offset(size, cx)?, Size::ZERO)?;
540         Ok(())
541     }
542 }
543
544 /// Undefined bytes.
545 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
546     /// Checks whether the given range  is entirely defined.
547     ///
548     /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
549     /// at which the first undefined access begins.
550     fn is_defined(&self, ptr: Pointer<Tag>, size: Size) -> Result<(), Size> {
551         self.undef_mask.is_range_defined(ptr.offset, ptr.offset + size) // `Size` addition
552     }
553
554     /// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
555     /// error which will report the first byte which is undefined.
556     fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> InterpResult<'tcx> {
557         self.is_defined(ptr, size)
558             .or_else(|idx| throw_ub!(InvalidUndefBytes(Some(Pointer::new(ptr.alloc_id, idx)))))
559     }
560
561     pub fn mark_definedness(&mut self, ptr: Pointer<Tag>, size: Size, new_state: bool) {
562         if size.bytes() == 0 {
563             return;
564         }
565         self.undef_mask.set_range(ptr.offset, ptr.offset + size, new_state);
566     }
567 }
568
569 /// Run-length encoding of the undef mask.
570 /// Used to copy parts of a mask multiple times to another allocation.
571 pub struct AllocationDefinedness {
572     /// The definedness of the first range.
573     initial: bool,
574     /// The lengths of ranges that are run-length encoded.
575     /// The definedness of the ranges alternate starting with `initial`.
576     ranges: smallvec::SmallVec<[u64; 1]>,
577 }
578
579 impl AllocationDefinedness {
580     pub fn all_bytes_undef(&self) -> bool {
581         // The `ranges` are run-length encoded and of alternating definedness.
582         // So if `ranges.len() > 1` then the second block is a range of defined.
583         !self.initial && self.ranges.len() == 1
584     }
585 }
586
587 /// Transferring the definedness mask to other allocations.
588 impl<Tag, Extra> Allocation<Tag, Extra> {
589     /// Creates a run-length encoding of the undef mask.
590     pub fn compress_undef_range(&self, src: Pointer<Tag>, size: Size) -> AllocationDefinedness {
591         // Since we are copying `size` bytes from `src` to `dest + i * size` (`for i in 0..repeat`),
592         // a naive undef mask copying algorithm would repeatedly have to read the undef mask from
593         // the source and write it to the destination. Even if we optimized the memory accesses,
594         // we'd be doing all of this `repeat` times.
595         // Therefore we precompute a compressed version of the undef mask of the source value and
596         // then write it back `repeat` times without computing any more information from the source.
597
598         // A precomputed cache for ranges of defined/undefined bits
599         // 0000010010001110 will become
600         // `[5, 1, 2, 1, 3, 3, 1]`,
601         // where each element toggles the state.
602
603         let mut ranges = smallvec::SmallVec::<[u64; 1]>::new();
604         let initial = self.undef_mask.get(src.offset);
605         let mut cur_len = 1;
606         let mut cur = initial;
607
608         for i in 1..size.bytes() {
609             // FIXME: optimize to bitshift the current undef block's bits and read the top bit.
610             if self.undef_mask.get(src.offset + Size::from_bytes(i)) == cur {
611                 cur_len += 1;
612             } else {
613                 ranges.push(cur_len);
614                 cur_len = 1;
615                 cur = !cur;
616             }
617         }
618
619         ranges.push(cur_len);
620
621         AllocationDefinedness { ranges, initial }
622     }
623
624     /// Applies multiple instances of the run-length encoding to the undef mask.
625     pub fn mark_compressed_undef_range(
626         &mut self,
627         defined: &AllocationDefinedness,
628         dest: Pointer<Tag>,
629         size: Size,
630         repeat: u64,
631     ) {
632         // An optimization where we can just overwrite an entire range of definedness bits if
633         // they are going to be uniformly `1` or `0`.
634         if defined.ranges.len() <= 1 {
635             self.undef_mask.set_range_inbounds(
636                 dest.offset,
637                 dest.offset + size * repeat, // `Size` operations
638                 defined.initial,
639             );
640             return;
641         }
642
643         for mut j in 0..repeat {
644             j *= size.bytes();
645             j += dest.offset.bytes();
646             let mut cur = defined.initial;
647             for range in &defined.ranges {
648                 let old_j = j;
649                 j += range;
650                 self.undef_mask.set_range_inbounds(
651                     Size::from_bytes(old_j),
652                     Size::from_bytes(j),
653                     cur,
654                 );
655                 cur = !cur;
656             }
657         }
658     }
659 }
660
661 /// Relocations.
662 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
663 pub struct Relocations<Tag = (), Id = AllocId>(SortedMap<Size, (Tag, Id)>);
664
665 impl<Tag, Id> Relocations<Tag, Id> {
666     pub fn new() -> Self {
667         Relocations(SortedMap::new())
668     }
669
670     // The caller must guarantee that the given relocations are already sorted
671     // by address and contain no duplicates.
672     pub fn from_presorted(r: Vec<(Size, (Tag, Id))>) -> Self {
673         Relocations(SortedMap::from_presorted_elements(r))
674     }
675 }
676
677 impl<Tag> Deref for Relocations<Tag> {
678     type Target = SortedMap<Size, (Tag, AllocId)>;
679
680     fn deref(&self) -> &Self::Target {
681         &self.0
682     }
683 }
684
685 impl<Tag> DerefMut for Relocations<Tag> {
686     fn deref_mut(&mut self) -> &mut Self::Target {
687         &mut self.0
688     }
689 }
690
691 /// A partial, owned list of relocations to transfer into another allocation.
692 pub struct AllocationRelocations<Tag> {
693     relative_relocations: Vec<(Size, (Tag, AllocId))>,
694 }
695
696 impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
697     pub fn prepare_relocation_copy(
698         &self,
699         cx: &impl HasDataLayout,
700         src: Pointer<Tag>,
701         size: Size,
702         dest: Pointer<Tag>,
703         length: u64,
704     ) -> AllocationRelocations<Tag> {
705         let relocations = self.get_relocations(cx, src, size);
706         if relocations.is_empty() {
707             return AllocationRelocations { relative_relocations: Vec::new() };
708         }
709
710         let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
711
712         for i in 0..length {
713             new_relocations.extend(relocations.iter().map(|&(offset, reloc)| {
714                 // compute offset for current repetition
715                 let dest_offset = dest.offset + size * i; // `Size` operations
716                 (
717                     // shift offsets from source allocation to destination allocation
718                     (offset + dest_offset) - src.offset, // `Size` operations
719                     reloc,
720                 )
721             }));
722         }
723
724         AllocationRelocations { relative_relocations: new_relocations }
725     }
726
727     /// Applies a relocation copy.
728     /// The affected range, as defined in the parameters to `prepare_relocation_copy` is expected
729     /// to be clear of relocations.
730     pub fn mark_relocation_range(&mut self, relocations: AllocationRelocations<Tag>) {
731         self.relocations.insert_presorted(relocations.relative_relocations);
732     }
733 }
734
735 ////////////////////////////////////////////////////////////////////////////////
736 // Undefined byte tracking
737 ////////////////////////////////////////////////////////////////////////////////
738
739 type Block = u64;
740
741 /// A bitmask where each bit refers to the byte with the same index. If the bit is `true`, the byte
742 /// is defined. If it is `false` the byte is undefined.
743 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
744 #[derive(HashStable)]
745 pub struct UndefMask {
746     blocks: Vec<Block>,
747     len: Size,
748 }
749
750 impl UndefMask {
751     pub const BLOCK_SIZE: u64 = 64;
752
753     pub fn new(size: Size, state: bool) -> Self {
754         let mut m = UndefMask { blocks: vec![], len: Size::ZERO };
755         m.grow(size, state);
756         m
757     }
758
759     /// Checks whether the range `start..end` (end-exclusive) is entirely defined.
760     ///
761     /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
762     /// at which the first undefined access begins.
763     #[inline]
764     pub fn is_range_defined(&self, start: Size, end: Size) -> Result<(), Size> {
765         if end > self.len {
766             return Err(self.len);
767         }
768
769         // FIXME(oli-obk): optimize this for allocations larger than a block.
770         let idx = (start.bytes()..end.bytes()).map(Size::from_bytes).find(|&i| !self.get(i));
771
772         match idx {
773             Some(idx) => Err(idx),
774             None => Ok(()),
775         }
776     }
777
778     pub fn set_range(&mut self, start: Size, end: Size, new_state: bool) {
779         let len = self.len;
780         if end > len {
781             self.grow(end - len, new_state);
782         }
783         self.set_range_inbounds(start, end, new_state);
784     }
785
786     pub fn set_range_inbounds(&mut self, start: Size, end: Size, new_state: bool) {
787         let (blocka, bita) = bit_index(start);
788         let (blockb, bitb) = bit_index(end);
789         if blocka == blockb {
790             // First set all bits except the first `bita`,
791             // then unset the last `64 - bitb` bits.
792             let range = if bitb == 0 {
793                 u64::MAX << bita
794             } else {
795                 (u64::MAX << bita) & (u64::MAX >> (64 - bitb))
796             };
797             if new_state {
798                 self.blocks[blocka] |= range;
799             } else {
800                 self.blocks[blocka] &= !range;
801             }
802             return;
803         }
804         // across block boundaries
805         if new_state {
806             // Set `bita..64` to `1`.
807             self.blocks[blocka] |= u64::MAX << bita;
808             // Set `0..bitb` to `1`.
809             if bitb != 0 {
810                 self.blocks[blockb] |= u64::MAX >> (64 - bitb);
811             }
812             // Fill in all the other blocks (much faster than one bit at a time).
813             for block in (blocka + 1)..blockb {
814                 self.blocks[block] = u64::MAX;
815             }
816         } else {
817             // Set `bita..64` to `0`.
818             self.blocks[blocka] &= !(u64::MAX << bita);
819             // Set `0..bitb` to `0`.
820             if bitb != 0 {
821                 self.blocks[blockb] &= !(u64::MAX >> (64 - bitb));
822             }
823             // Fill in all the other blocks (much faster than one bit at a time).
824             for block in (blocka + 1)..blockb {
825                 self.blocks[block] = 0;
826             }
827         }
828     }
829
830     #[inline]
831     pub fn get(&self, i: Size) -> bool {
832         let (block, bit) = bit_index(i);
833         (self.blocks[block] & (1 << bit)) != 0
834     }
835
836     #[inline]
837     pub fn set(&mut self, i: Size, new_state: bool) {
838         let (block, bit) = bit_index(i);
839         self.set_bit(block, bit, new_state);
840     }
841
842     #[inline]
843     fn set_bit(&mut self, block: usize, bit: usize, new_state: bool) {
844         if new_state {
845             self.blocks[block] |= 1 << bit;
846         } else {
847             self.blocks[block] &= !(1 << bit);
848         }
849     }
850
851     pub fn grow(&mut self, amount: Size, new_state: bool) {
852         if amount.bytes() == 0 {
853             return;
854         }
855         let unused_trailing_bits =
856             u64::try_from(self.blocks.len()).unwrap() * Self::BLOCK_SIZE - self.len.bytes();
857         if amount.bytes() > unused_trailing_bits {
858             let additional_blocks = amount.bytes() / Self::BLOCK_SIZE + 1;
859             self.blocks.extend(
860                 // FIXME(oli-obk): optimize this by repeating `new_state as Block`.
861                 iter::repeat(0).take(usize::try_from(additional_blocks).unwrap()),
862             );
863         }
864         let start = self.len;
865         self.len += amount;
866         self.set_range_inbounds(start, start + amount, new_state); // `Size` operation
867     }
868 }
869
870 #[inline]
871 fn bit_index(bits: Size) -> (usize, usize) {
872     let bits = bits.bytes();
873     let a = bits / UndefMask::BLOCK_SIZE;
874     let b = bits % UndefMask::BLOCK_SIZE;
875     (usize::try_from(a).unwrap(), usize::try_from(b).unwrap())
876 }