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