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 impl<Tag, Extra> Allocation<Tag, Extra> {
115 /// Creates a read-only allocation initialized by the given bytes
116 pub fn from_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, align: Align, extra: Extra) -> Self {
117 let bytes = slice.into().into_owned();
118 let undef_mask = UndefMask::new(Size::from_bytes(bytes.len() as u64), true);
121 relocations: Relocations::new(),
124 mutability: Mutability::Immutable,
129 pub fn from_byte_aligned_bytes<'a>(slice: impl Into<Cow<'a, [u8]>>, extra: Extra) -> Self {
130 Allocation::from_bytes(slice, Align::from_bytes(1).unwrap(), extra)
133 pub fn undef(size: Size, align: Align, extra: Extra) -> Self {
134 assert_eq!(size.bytes() as usize as u64, size.bytes());
136 bytes: vec![0; size.bytes() as usize],
137 relocations: Relocations::new(),
138 undef_mask: UndefMask::new(size, false),
140 mutability: Mutability::Mutable,
146 impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
148 /// Alignment and bounds checks
149 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
150 /// Checks if the pointer is "in-bounds". Notice that a pointer pointing at the end
151 /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
152 /// in-bounds! This follows C's/LLVM's rules.
153 /// If you want to check bounds before doing a memory access, better use `check_bounds`.
157 msg: CheckInAllocMsg,
158 ) -> EvalResult<'tcx> {
159 let allocation_size = self.bytes.len() as u64;
160 ptr.check_in_alloc(Size::from_bytes(allocation_size), msg)
163 /// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
167 cx: &impl HasDataLayout,
170 msg: CheckInAllocMsg,
171 ) -> EvalResult<'tcx> {
172 // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
173 self.check_bounds_ptr(ptr.offset(size, cx)?, msg)
178 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
179 /// The last argument controls whether we error out when there are undefined
180 /// or pointer bytes. You should never call this, call `get_bytes` or
181 /// `get_bytes_with_undef_and_ptr` instead,
183 /// This function also guarantees that the resulting pointer will remain stable
184 /// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
186 fn get_bytes_internal(
188 cx: &impl HasDataLayout,
191 check_defined_and_ptr: bool,
192 msg: CheckInAllocMsg,
193 ) -> EvalResult<'tcx, &[u8]>
195 self.check_bounds(cx, ptr, size, msg)?;
197 if check_defined_and_ptr {
198 self.check_defined(ptr, size)?;
199 self.check_relocations(cx, ptr, size)?;
201 // We still don't want relocations on the *edges*
202 self.check_relocation_edges(cx, ptr, size)?;
205 AllocationExtra::memory_read(self, ptr, size)?;
207 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
208 assert_eq!(size.bytes() as usize as u64, size.bytes());
209 let offset = ptr.offset.bytes() as usize;
210 Ok(&self.bytes[offset..offset + size.bytes() as usize])
216 cx: &impl HasDataLayout,
219 ) -> EvalResult<'tcx, &[u8]>
221 self.get_bytes_internal(cx, ptr, size, true, CheckInAllocMsg::MemoryAccessTest)
224 /// It is the caller's responsibility to handle undefined and pointer bytes.
225 /// However, this still checks that there are no relocations on the *edges*.
227 pub fn get_bytes_with_undef_and_ptr(
229 cx: &impl HasDataLayout,
232 ) -> EvalResult<'tcx, &[u8]>
234 self.get_bytes_internal(cx, ptr, size, false, CheckInAllocMsg::MemoryAccessTest)
237 /// Just calling this already marks everything as defined and removes relocations,
238 /// so be sure to actually put data there!
239 pub fn get_bytes_mut(
241 cx: &impl HasDataLayout,
244 ) -> EvalResult<'tcx, &mut [u8]>
246 assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
247 self.check_bounds(cx, ptr, size, CheckInAllocMsg::MemoryAccessTest)?;
249 self.mark_definedness(ptr, size, true)?;
250 self.clear_relocations(cx, ptr, size)?;
252 AllocationExtra::memory_written(self, ptr, size)?;
254 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
255 assert_eq!(size.bytes() as usize as u64, size.bytes());
256 let offset = ptr.offset.bytes() as usize;
257 Ok(&mut self.bytes[offset..offset + size.bytes() as usize])
261 /// Reading and writing
262 impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
263 /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
264 /// before a `0` is found.
267 cx: &impl HasDataLayout,
269 ) -> EvalResult<'tcx, &[u8]>
271 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
272 let offset = ptr.offset.bytes() as usize;
273 match self.bytes[offset..].iter().position(|&c| c == 0) {
275 let size_with_null = Size::from_bytes((size + 1) as u64);
276 // Go through `get_bytes` for checks and AllocationExtra hooks.
277 // We read the null, so we include it in the request, but we want it removed
279 Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
281 None => err!(UnterminatedCString(ptr.erase_tag())),
285 /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
286 /// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
287 /// given range contains neither relocations nor undef bytes.
290 cx: &impl HasDataLayout,
293 allow_ptr_and_undef: bool,
294 ) -> EvalResult<'tcx>
296 // Check bounds and relocations on the edges
297 self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
298 // Check undef and ptr
299 if !allow_ptr_and_undef {
300 self.check_defined(ptr, size)?;
301 self.check_relocations(cx, ptr, size)?;
306 /// Writes `src` to the memory starting at `ptr.offset`.
308 /// Will do bounds checks on the allocation.
311 cx: &impl HasDataLayout,
314 ) -> EvalResult<'tcx>
316 let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
317 bytes.clone_from_slice(src);
321 /// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
324 cx: &impl HasDataLayout,
328 ) -> EvalResult<'tcx>
330 let bytes = self.get_bytes_mut(cx, ptr, count)?;
337 /// Read a *non-ZST* scalar
339 /// zsts can't be read out of two reasons:
340 /// * byteorder cannot work with zero element buffers
341 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
342 /// being valid for ZSTs
344 /// Note: This function does not do *any* alignment checks, you need to do these before calling
347 cx: &impl HasDataLayout,
350 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
352 // get_bytes_unchecked tests relocation edges
353 let bytes = self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
354 // Undef check happens *after* we established that the alignment is correct.
355 // We must not return Ok() for unaligned pointers!
356 if self.check_defined(ptr, size).is_err() {
357 // this inflates undefined bytes to the entire scalar, even if only a few
358 // bytes are undefined
359 return Ok(ScalarMaybeUndef::Undef);
361 // Now we do the actual reading
362 let bits = read_target_uint(cx.data_layout().endian, bytes).unwrap();
363 // See if we got a pointer
364 if size != cx.data_layout().pointer_size {
365 // *Now* better make sure that the inside also is free of relocations.
366 self.check_relocations(cx, ptr, size)?;
368 match self.relocations.get(&ptr.offset) {
369 Some(&(tag, alloc_id)) => {
370 let ptr = Pointer::new_with_tag(alloc_id, Size::from_bytes(bits as u64), tag);
371 return Ok(ScalarMaybeUndef::Scalar(ptr.into()))
376 // We don't. Just return the bits.
377 Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
380 /// Note: This function does not do *any* alignment checks, you need to do these before calling
381 pub fn read_ptr_sized(
383 cx: &impl HasDataLayout,
385 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
387 self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
390 /// Write a *non-ZST* scalar
392 /// zsts can't be read out of two reasons:
393 /// * byteorder cannot work with zero element buffers
394 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
395 /// being valid for ZSTs
397 /// Note: This function does not do *any* alignment checks, you need to do these before calling
400 cx: &impl HasDataLayout,
402 val: ScalarMaybeUndef<Tag>,
404 ) -> EvalResult<'tcx>
406 let val = match val {
407 ScalarMaybeUndef::Scalar(scalar) => scalar,
408 ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
411 let bytes = match val.to_bits_or_ptr(type_size, cx) {
412 Err(val) => val.offset.bytes() as u128,
416 let endian = cx.data_layout().endian;
417 let dst = self.get_bytes_mut(cx, ptr, type_size)?;
418 write_target_uint(endian, dst, bytes).unwrap();
420 // See if we have to also write a relocation
422 Scalar::Ptr(val) => {
423 self.relocations.insert(
425 (val.tag, val.alloc_id),
434 /// Note: This function does not do *any* alignment checks, you need to do these before calling
435 pub fn write_ptr_sized(
437 cx: &impl HasDataLayout,
439 val: ScalarMaybeUndef<Tag>
440 ) -> EvalResult<'tcx>
442 let ptr_size = cx.data_layout().pointer_size;
443 self.write_scalar(cx, ptr.into(), val, ptr_size)
448 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
449 /// Returns all relocations overlapping with the given ptr-offset pair.
452 cx: &impl HasDataLayout,
455 ) -> &[(Size, (Tag, AllocId))] {
456 // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
457 // the beginning of this range.
458 let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
459 let end = ptr.offset + size; // this does overflow checking
460 self.relocations.range(Size::from_bytes(start)..end)
463 /// Checks that there are no relocations overlapping with the given range.
465 fn check_relocations(
467 cx: &impl HasDataLayout,
470 ) -> EvalResult<'tcx> {
471 if self.relocations(cx, ptr, size).is_empty() {
474 err!(ReadPointerAsBytes)
478 /// Removes all relocations inside the given range.
479 /// If there are relocations overlapping with the edges, they
480 /// are removed as well *and* the bytes they cover are marked as
481 /// uninitialized. This is a somewhat odd "spooky action at a distance",
482 /// but it allows strictly more code to run than if we would just error
483 /// immediately in that case.
484 fn clear_relocations(
486 cx: &impl HasDataLayout,
489 ) -> EvalResult<'tcx> {
490 // Find the start and end of the given range and its outermost relocations.
491 let (first, last) = {
492 // Find all relocations overlapping the given range.
493 let relocations = self.relocations(cx, ptr, size);
494 if relocations.is_empty() {
498 (relocations.first().unwrap().0,
499 relocations.last().unwrap().0 + cx.data_layout().pointer_size)
501 let start = ptr.offset;
502 let end = start + size;
504 // Mark parts of the outermost relocations as undefined if they partially fall outside the
507 self.undef_mask.set_range(first, start, false);
510 self.undef_mask.set_range(end, last, false);
513 // Forget all the relocations.
514 self.relocations.remove_range(first..last);
519 /// Error if there are relocations overlapping with the edges of the
520 /// given memory range.
522 fn check_relocation_edges(
524 cx: &impl HasDataLayout,
527 ) -> EvalResult<'tcx> {
528 self.check_relocations(cx, ptr, Size::ZERO)?;
529 self.check_relocations(cx, ptr.offset(size, cx)?, Size::ZERO)?;
536 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
537 /// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
538 /// error which will report the first byte which is undefined.
540 fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> EvalResult<'tcx> {
541 self.undef_mask.is_range_defined(
544 ).or_else(|idx| err!(ReadUndefBytes(idx)))
547 pub fn mark_definedness(
552 ) -> EvalResult<'tcx> {
553 if size.bytes() == 0 {
556 self.undef_mask.set_range(
566 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
567 pub struct Relocations<Tag=(), Id=AllocId>(SortedMap<Size, (Tag, Id)>);
569 impl<Tag, Id> Relocations<Tag, Id> {
570 pub fn new() -> Self {
571 Relocations(SortedMap::new())
574 // The caller must guarantee that the given relocations are already sorted
575 // by address and contain no duplicates.
576 pub fn from_presorted(r: Vec<(Size, (Tag, Id))>) -> Self {
577 Relocations(SortedMap::from_presorted_elements(r))
581 impl<Tag> Deref for Relocations<Tag> {
582 type Target = SortedMap<Size, (Tag, AllocId)>;
584 fn deref(&self) -> &Self::Target {
589 impl<Tag> DerefMut for Relocations<Tag> {
590 fn deref_mut(&mut self) -> &mut Self::Target {
595 ////////////////////////////////////////////////////////////////////////////////
596 // Undefined byte tracking
597 ////////////////////////////////////////////////////////////////////////////////
601 /// A bitmask where each bit refers to the byte with the same index. If the bit is `true`, the byte
602 /// is defined. If it is `false` the byte is undefined.
603 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
604 pub struct UndefMask {
609 impl_stable_hash_for!(struct mir::interpret::UndefMask{blocks, len});
612 pub const BLOCK_SIZE: u64 = 64;
614 pub fn new(size: Size, state: bool) -> Self {
615 let mut m = UndefMask {
623 /// Checks whether the range `start..end` (end-exclusive) is entirely defined.
625 /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
626 /// at which the first undefined access begins.
628 pub fn is_range_defined(&self, start: Size, end: Size) -> Result<(), Size> {
630 return Err(self.len);
633 // FIXME(oli-obk): optimize this for allocations larger than a block.
634 let idx = (start.bytes()..end.bytes())
635 .map(|i| Size::from_bytes(i))
636 .find(|&i| !self.get(i));
639 Some(idx) => Err(idx),
644 pub fn set_range(&mut self, start: Size, end: Size, new_state: bool) {
647 self.grow(end - len, new_state);
649 self.set_range_inbounds(start, end, new_state);
652 pub fn set_range_inbounds(&mut self, start: Size, end: Size, new_state: bool) {
653 let (blocka, bita) = bit_index(start);
654 let (blockb, bitb) = bit_index(end);
655 if blocka == blockb {
656 // first set all bits but the first `bita`
657 // then unset the last `64 - bitb` bits
658 let range = if bitb == 0 {
659 u64::max_value() << bita
661 (u64::max_value() << bita) & (u64::max_value() >> (64 - bitb))
664 self.blocks[blocka] |= range;
666 self.blocks[blocka] &= !range;
670 // across block boundaries
673 self.blocks[blocka] |= u64::max_value() << bita;
676 self.blocks[blockb] |= u64::max_value() >> (64 - bitb);
678 // fill in all the other blocks (much faster than one bit at a time)
679 for block in (blocka + 1) .. blockb {
680 self.blocks[block] = u64::max_value();
684 self.blocks[blocka] &= !(u64::max_value() << bita);
687 self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb));
689 // fill in all the other blocks (much faster than one bit at a time)
690 for block in (blocka + 1) .. blockb {
691 self.blocks[block] = 0;
697 pub fn get(&self, i: Size) -> bool {
698 let (block, bit) = bit_index(i);
699 (self.blocks[block] & (1 << bit)) != 0
703 pub fn set(&mut self, i: Size, new_state: bool) {
704 let (block, bit) = bit_index(i);
705 self.set_bit(block, bit, new_state);
709 fn set_bit(&mut self, block: usize, bit: usize, new_state: bool) {
711 self.blocks[block] |= 1 << bit;
713 self.blocks[block] &= !(1 << bit);
717 pub fn grow(&mut self, amount: Size, new_state: bool) {
718 if amount.bytes() == 0 {
721 let unused_trailing_bits = self.blocks.len() as u64 * Self::BLOCK_SIZE - self.len.bytes();
722 if amount.bytes() > unused_trailing_bits {
723 let additional_blocks = amount.bytes() / Self::BLOCK_SIZE + 1;
724 assert_eq!(additional_blocks as usize as u64, additional_blocks);
726 // FIXME(oli-obk): optimize this by repeating `new_state as Block`
727 iter::repeat(0).take(additional_blocks as usize),
730 let start = self.len;
732 self.set_range_inbounds(start, start + amount, new_state);
737 fn bit_index(bits: Size) -> (usize, usize) {
738 let bits = bits.bytes();
739 let a = bits / UndefMask::BLOCK_SIZE;
740 let b = bits % UndefMask::BLOCK_SIZE;
741 assert_eq!(a as usize as u64, a);
742 assert_eq!(b as usize as u64, b);
743 (a as usize, b as usize)