1 //! The virtual memory representation of the MIR interpreter.
4 Pointer, EvalResult, AllocId, ScalarMaybeUndef, write_target_uint, read_target_uint, Scalar,
7 use crate::ty::layout::{Size, Align};
8 use syntax::ast::Mutability;
9 use std::{iter, fmt::{self, Display}};
11 use std::ops::{Deref, DerefMut};
12 use rustc_data_structures::sorted_map::SortedMap;
13 use rustc_macros::HashStable;
14 use rustc_target::abi::HasDataLayout;
17 /// Used by `check_bounds` to indicate whether the pointer needs to be just inbounds
18 /// or also inbounds of a *live* allocation.
19 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
20 pub enum InboundsCheck {
25 /// Used by `check_in_alloc` to indicate context of check
26 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable)]
27 pub enum CheckInAllocMsg {
30 PointerArithmeticTest,
34 impl Display for CheckInAllocMsg {
35 /// When this is printed as an error the context looks like this
36 /// "{test name} failed: pointer must be in-bounds at offset..."
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 write!(f, "{}", match *self {
39 CheckInAllocMsg::MemoryAccessTest => "Memory access",
40 CheckInAllocMsg::NullPointerTest => "Null pointer test",
41 CheckInAllocMsg::PointerArithmeticTest => "Pointer arithmetic",
42 CheckInAllocMsg::InboundsTest => "Inbounds test",
47 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
48 pub struct Allocation<Tag=(),Extra=()> {
49 /// The actual bytes of the allocation.
50 /// Note that the bytes of a pointer represent the offset of the pointer
52 /// Maps from byte addresses to extra data for each pointer.
53 /// Only the first byte of a pointer is inserted into the map; i.e.,
54 /// every entry in this map applies to `pointer_size` consecutive bytes starting
55 /// at the given offset.
56 pub relocations: Relocations<Tag>,
57 /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
58 pub undef_mask: UndefMask,
59 /// The alignment of the allocation to detect unaligned reads.
61 /// Whether the allocation is mutable.
62 /// Also used by codegen to determine if a static should be put into mutable memory,
63 /// which happens for `static mut` and `static` with interior mutability.
64 pub mutability: Mutability,
65 /// Extra state for the machine.
70 pub trait AllocationExtra<Tag>: ::std::fmt::Debug + Clone {
71 // There is no constructor in here because the constructor's type depends
72 // on `MemoryKind`, and making things sufficiently generic leads to painful
75 /// Hook for performing extra checks on a memory read access.
77 /// Takes read-only access to the allocation so we can keep all the memory read
78 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
82 _alloc: &Allocation<Tag, Self>,
85 ) -> EvalResult<'tcx> {
89 /// Hook for performing extra checks on a memory write access.
92 _alloc: &mut Allocation<Tag, Self>,
95 ) -> EvalResult<'tcx> {
99 /// Hook for performing extra checks on a memory deallocation.
100 /// `size` will be the size of the allocation.
102 fn memory_deallocated(
103 _alloc: &mut Allocation<Tag, Self>,
106 ) -> EvalResult<'tcx> {
111 // For Tag=() and no extra state, we have is a trivial implementation.
112 impl AllocationExtra<()> for () { }
114 // The constructors are all without extra; the extra gets added by a machine hook later.
115 impl<Tag> Allocation<Tag> {
116 /// Creates a read-only allocation initialized by the given bytes
117 pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align) -> Self {
118 let bytes = slice.into().into_owned();
119 let undef_mask = UndefMask::new(Size::from_bytes(bytes.len() as u64), true);
122 relocations: Relocations::new(),
125 mutability: Mutability::Immutable,
130 pub fn from_byte_aligned_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>) -> Self {
131 Allocation::from_bytes(slice, Align::from_bytes(1).unwrap())
134 pub fn undef(size: Size, align: Align) -> Self {
135 assert_eq!(size.bytes() as usize as u64, size.bytes());
137 bytes: vec![0; size.bytes() as usize],
138 relocations: Relocations::new(),
139 undef_mask: UndefMask::new(size, false),
141 mutability: Mutability::Mutable,
147 impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
149 /// Alignment and bounds checks
150 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
151 /// Checks if the pointer is "in-bounds". Notice that a pointer pointing at the end
152 /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
153 /// in-bounds! This follows C's/LLVM's rules.
154 /// If you want to check bounds before doing a memory access, better use `check_bounds`.
158 msg: CheckInAllocMsg,
159 ) -> EvalResult<'tcx> {
160 let allocation_size = self.bytes.len() as u64;
161 ptr.check_in_alloc(Size::from_bytes(allocation_size), msg)
164 /// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
168 cx: &impl HasDataLayout,
171 msg: CheckInAllocMsg,
172 ) -> EvalResult<'tcx> {
173 // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
174 self.check_bounds_ptr(ptr.offset(size, cx)?, msg)
179 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
180 /// The last argument controls whether we error out when there are undefined
181 /// or pointer bytes. You should never call this, call `get_bytes` or
182 /// `get_bytes_with_undef_and_ptr` instead,
184 /// This function also guarantees that the resulting pointer will remain stable
185 /// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
187 fn get_bytes_internal(
189 cx: &impl HasDataLayout,
192 check_defined_and_ptr: bool,
193 msg: CheckInAllocMsg,
194 ) -> EvalResult<'tcx, &[u8]>
196 self.check_bounds(cx, ptr, size, msg)?;
198 if check_defined_and_ptr {
199 self.check_defined(ptr, size)?;
200 self.check_relocations(cx, ptr, size)?;
202 // We still don't want relocations on the *edges*
203 self.check_relocation_edges(cx, ptr, size)?;
206 AllocationExtra::memory_read(self, ptr, size)?;
208 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
209 assert_eq!(size.bytes() as usize as u64, size.bytes());
210 let offset = ptr.offset.bytes() as usize;
211 Ok(&self.bytes[offset..offset + size.bytes() as usize])
217 cx: &impl HasDataLayout,
220 ) -> EvalResult<'tcx, &[u8]>
222 self.get_bytes_internal(cx, ptr, size, true, CheckInAllocMsg::MemoryAccessTest)
225 /// It is the caller's responsibility to handle undefined and pointer bytes.
226 /// However, this still checks that there are no relocations on the *edges*.
228 pub fn get_bytes_with_undef_and_ptr(
230 cx: &impl HasDataLayout,
233 ) -> EvalResult<'tcx, &[u8]>
235 self.get_bytes_internal(cx, ptr, size, false, CheckInAllocMsg::MemoryAccessTest)
238 /// Just calling this already marks everything as defined and removes relocations,
239 /// so be sure to actually put data there!
240 pub fn get_bytes_mut(
242 cx: &impl HasDataLayout,
245 ) -> EvalResult<'tcx, &mut [u8]>
247 assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
248 self.check_bounds(cx, ptr, size, CheckInAllocMsg::MemoryAccessTest)?;
250 self.mark_definedness(ptr, size, true)?;
251 self.clear_relocations(cx, ptr, size)?;
253 AllocationExtra::memory_written(self, ptr, size)?;
255 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
256 assert_eq!(size.bytes() as usize as u64, size.bytes());
257 let offset = ptr.offset.bytes() as usize;
258 Ok(&mut self.bytes[offset..offset + size.bytes() as usize])
262 /// Reading and writing
263 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
264 /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
265 /// before a `0` is found.
268 cx: &impl HasDataLayout,
270 ) -> EvalResult<'tcx, &[u8]>
272 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
273 let offset = ptr.offset.bytes() as usize;
274 match self.bytes[offset..].iter().position(|&c| c == 0) {
276 let size_with_null = Size::from_bytes((size + 1) as u64);
277 // Go through `get_bytes` for checks and AllocationExtra hooks.
278 // We read the null, so we include it in the request, but we want it removed
280 Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
282 None => err!(UnterminatedCString(ptr.erase_tag())),
286 /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
287 /// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
288 /// given range contains neither relocations nor undef bytes.
291 cx: &impl HasDataLayout,
294 allow_ptr_and_undef: bool,
295 ) -> EvalResult<'tcx>
297 // Check bounds and relocations on the edges
298 self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
299 // Check undef and ptr
300 if !allow_ptr_and_undef {
301 self.check_defined(ptr, size)?;
302 self.check_relocations(cx, ptr, size)?;
307 /// Writes `src` to the memory starting at `ptr.offset`.
309 /// Will do bounds checks on the allocation.
312 cx: &impl HasDataLayout,
315 ) -> EvalResult<'tcx>
317 let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
318 bytes.clone_from_slice(src);
322 /// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
325 cx: &impl HasDataLayout,
329 ) -> EvalResult<'tcx>
331 let bytes = self.get_bytes_mut(cx, ptr, count)?;
338 /// Read a *non-ZST* scalar
340 /// zsts can't be read out of two reasons:
341 /// * byteorder cannot work with zero element buffers
342 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
343 /// being valid for ZSTs
345 /// Note: This function does not do *any* alignment checks, you need to do these before calling
348 cx: &impl HasDataLayout,
351 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
353 // get_bytes_unchecked tests relocation edges
354 let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
355 // Undef check happens *after* we established that the alignment is correct.
356 // We must not return Ok() for unaligned pointers!
357 if self.check_defined(ptr, size).is_err() {
358 // this inflates undefined bytes to the entire scalar, even if only a few
359 // bytes are undefined
360 return Ok(ScalarMaybeUndef::Undef);
362 // Now we do the actual reading
363 let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
364 // See if we got a pointer
365 if size != cx.data_layout().pointer_size {
366 // *Now* better make sure that the inside also is free of relocations.
367 self.check_relocations(cx, ptr, size)?;
369 match self.relocations.get(&ptr.offset) {
370 Some(&(tag, alloc_id)) => {
371 let ptr = Pointer::new_with_tag(alloc_id, Size::from_bytes(bits as u64), tag);
372 return Ok(ScalarMaybeUndef::Scalar(ptr.into()))
377 // We don't. Just return the bits.
378 Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
381 /// Note: This function does not do *any* alignment checks, you need to do these before calling
382 pub fn read_ptr_sized(
384 cx: &impl HasDataLayout,
386 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
388 self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
391 /// Write a *non-ZST* scalar
393 /// zsts can't be read out of two reasons:
394 /// * byteorder cannot work with zero element buffers
395 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
396 /// being valid for ZSTs
398 /// Note: This function does not do *any* alignment checks, you need to do these before calling
401 cx: &impl HasDataLayout,
403 val: ScalarMaybeUndef<Tag>,
405 ) -> EvalResult<'tcx>
407 let val = match val {
408 ScalarMaybeUndef::Scalar(scalar) => scalar,
409 ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
412 let bytes = match val.to_bits_or_ptr(type_size, cx) {
413 Err(val) => val.offset.bytes() as u128,
417 let endian = cx.data_layout().endian;
418 let dst = self.get_bytes_mut(cx, ptr, type_size)?;
419 write_target_uint(endian, dst, bytes).unwrap();
421 // See if we have to also write a relocation
423 Scalar::Ptr(val) => {
424 self.relocations.insert(
426 (val.tag, val.alloc_id),
435 /// Note: This function does not do *any* alignment checks, you need to do these before calling
436 pub fn write_ptr_sized(
438 cx: &impl HasDataLayout,
440 val: ScalarMaybeUndef<Tag>
441 ) -> EvalResult<'tcx>
443 let ptr_size = cx.data_layout().pointer_size;
444 self.write_scalar(cx, ptr.into(), val, ptr_size)
449 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
450 /// Returns all relocations overlapping with the given ptr-offset pair.
453 cx: &impl HasDataLayout,
456 ) -> &[(Size, (Tag, AllocId))] {
457 // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
458 // the beginning of this range.
459 let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
460 let end = ptr.offset + size; // this does overflow checking
461 self.relocations.range(Size::from_bytes(start)..end)
464 /// Checks that there are no relocations overlapping with the given range.
466 fn check_relocations(
468 cx: &impl HasDataLayout,
471 ) -> EvalResult<'tcx> {
472 if self.relocations(cx, ptr, size).is_empty() {
475 err!(ReadPointerAsBytes)
479 /// Removes all relocations inside the given range.
480 /// If there are relocations overlapping with the edges, they
481 /// are removed as well *and* the bytes they cover are marked as
482 /// uninitialized. This is a somewhat odd "spooky action at a distance",
483 /// but it allows strictly more code to run than if we would just error
484 /// immediately in that case.
485 fn clear_relocations(
487 cx: &impl HasDataLayout,
490 ) -> EvalResult<'tcx> {
491 // Find the start and end of the given range and its outermost relocations.
492 let (first, last) = {
493 // Find all relocations overlapping the given range.
494 let relocations = self.relocations(cx, ptr, size);
495 if relocations.is_empty() {
499 (relocations.first().unwrap().0,
500 relocations.last().unwrap().0 + cx.data_layout().pointer_size)
502 let start = ptr.offset;
503 let end = start + size;
505 // Mark parts of the outermost relocations as undefined if they partially fall outside the
508 self.undef_mask.set_range(first, start, false);
511 self.undef_mask.set_range(end, last, false);
514 // Forget all the relocations.
515 self.relocations.remove_range(first..last);
520 /// Error if there are relocations overlapping with the edges of the
521 /// given memory range.
523 fn check_relocation_edges(
525 cx: &impl HasDataLayout,
528 ) -> EvalResult<'tcx> {
529 self.check_relocations(cx, ptr, Size::ZERO)?;
530 self.check_relocations(cx, ptr.offset(size, cx)?, Size::ZERO)?;
537 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
538 /// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
539 /// error which will report the first byte which is undefined.
541 fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> EvalResult<'tcx> {
542 self.undef_mask.is_range_defined(
545 ).or_else(|idx| err!(ReadUndefBytes(idx)))
548 pub fn mark_definedness(
553 ) -> EvalResult<'tcx> {
554 if size.bytes() == 0 {
557 self.undef_mask.set_range(
567 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
568 pub struct Relocations<Tag=(), Id=AllocId>(SortedMap<Size, (Tag, Id)>);
570 impl<Tag, Id> Relocations<Tag, Id> {
571 pub fn new() -> Self {
572 Relocations(SortedMap::new())
575 // The caller must guarantee that the given relocations are already sorted
576 // by address and contain no duplicates.
577 pub fn from_presorted(r: Vec<(Size, (Tag, Id))>) -> Self {
578 Relocations(SortedMap::from_presorted_elements(r))
582 impl<Tag> Deref for Relocations<Tag> {
583 type Target = SortedMap<Size, (Tag, AllocId)>;
585 fn deref(&self) -> &Self::Target {
590 impl<Tag> DerefMut for Relocations<Tag> {
591 fn deref_mut(&mut self) -> &mut Self::Target {
596 ////////////////////////////////////////////////////////////////////////////////
597 // Undefined byte tracking
598 ////////////////////////////////////////////////////////////////////////////////
602 /// A bitmask where each bit refers to the byte with the same index. If the bit is `true`, the byte
603 /// is defined. If it is `false` the byte is undefined.
604 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
605 pub struct UndefMask {
610 impl_stable_hash_for!(struct mir::interpret::UndefMask{blocks, len});
613 pub const BLOCK_SIZE: u64 = 64;
615 pub fn new(size: Size, state: bool) -> Self {
616 let mut m = UndefMask {
624 /// Checks whether the range `start..end` (end-exclusive) is entirely defined.
626 /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
627 /// at which the first undefined access begins.
629 pub fn is_range_defined(&self, start: Size, end: Size) -> Result<(), Size> {
631 return Err(self.len);
634 // FIXME(oli-obk): optimize this for allocations larger than a block.
635 let idx = (start.bytes()..end.bytes())
636 .map(|i| Size::from_bytes(i))
637 .find(|&i| !self.get(i));
640 Some(idx) => Err(idx),
645 pub fn set_range(&mut self, start: Size, end: Size, new_state: bool) {
648 self.grow(end - len, new_state);
650 self.set_range_inbounds(start, end, new_state);
653 pub fn set_range_inbounds(&mut self, start: Size, end: Size, new_state: bool) {
654 let (blocka, bita) = bit_index(start);
655 let (blockb, bitb) = bit_index(end);
656 if blocka == blockb {
657 // first set all bits but the first `bita`
658 // then unset the last `64 - bitb` bits
659 let range = if bitb == 0 {
660 u64::max_value() << bita
662 (u64::max_value() << bita) & (u64::max_value() >> (64 - bitb))
665 self.blocks[blocka] |= range;
667 self.blocks[blocka] &= !range;
671 // across block boundaries
674 self.blocks[blocka] |= u64::max_value() << bita;
677 self.blocks[blockb] |= u64::max_value() >> (64 - bitb);
679 // fill in all the other blocks (much faster than one bit at a time)
680 for block in (blocka + 1) .. blockb {
681 self.blocks[block] = u64::max_value();
685 self.blocks[blocka] &= !(u64::max_value() << bita);
688 self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb));
690 // fill in all the other blocks (much faster than one bit at a time)
691 for block in (blocka + 1) .. blockb {
692 self.blocks[block] = 0;
698 pub fn get(&self, i: Size) -> bool {
699 let (block, bit) = bit_index(i);
700 (self.blocks[block] & (1 << bit)) != 0
704 pub fn set(&mut self, i: Size, new_state: bool) {
705 let (block, bit) = bit_index(i);
706 self.set_bit(block, bit, new_state);
710 fn set_bit(&mut self, block: usize, bit: usize, new_state: bool) {
712 self.blocks[block] |= 1 << bit;
714 self.blocks[block] &= !(1 << bit);
718 pub fn grow(&mut self, amount: Size, new_state: bool) {
719 if amount.bytes() == 0 {
722 let unused_trailing_bits = self.blocks.len() as u64 * Self::BLOCK_SIZE - self.len.bytes();
723 if amount.bytes() > unused_trailing_bits {
724 let additional_blocks = amount.bytes() / Self::BLOCK_SIZE + 1;
725 assert_eq!(additional_blocks as usize as u64, additional_blocks);
727 // FIXME(oli-obk): optimize this by repeating `new_state as Block`
728 iter::repeat(0).take(additional_blocks as usize),
731 let start = self.len;
733 self.set_range_inbounds(start, start + amount, new_state);
738 fn bit_index(bits: Size) -> (usize, usize) {
739 let bits = bits.bytes();
740 let a = bits / UndefMask::BLOCK_SIZE;
741 let b = bits % UndefMask::BLOCK_SIZE;
742 assert_eq!(a as usize as u64, a);
743 assert_eq!(b as usize as u64, b);
744 (a as usize, b as usize)