1 //! The virtual memory representation of the MIR interpreter.
4 Pointer, EvalResult, AllocId, ScalarMaybeUndef, write_target_uint, read_target_uint, Scalar,
8 use crate::ty::layout::{Size, Align};
9 use syntax::ast::Mutability;
12 use std::ops::{Deref, DerefMut};
13 use rustc_data_structures::sorted_map::SortedMap;
14 use rustc_target::abi::HasDataLayout;
16 /// Used by `check_bounds` to indicate whether the pointer needs to be just inbounds
17 /// or also inbounds of a *live* allocation.
18 #[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable)]
19 pub enum InboundsCheck {
24 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
25 pub struct Allocation<Tag=(),Extra=()> {
26 /// The actual bytes of the allocation.
27 /// Note that the bytes of a pointer represent the offset of the pointer
29 /// Maps from byte addresses to extra data for each pointer.
30 /// Only the first byte of a pointer is inserted into the map; i.e.,
31 /// every entry in this map applies to `pointer_size` consecutive bytes starting
32 /// at the given offset.
33 pub relocations: Relocations<Tag>,
34 /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
35 pub undef_mask: UndefMask,
36 /// The alignment of the allocation to detect unaligned reads.
38 /// Whether the allocation is mutable.
39 /// Also used by codegen to determine if a static should be put into mutable memory,
40 /// which happens for `static mut` and `static` with interior mutability.
41 pub mutability: Mutability,
42 /// Extra state for the machine.
47 pub trait AllocationExtra<Tag, MemoryExtra>: ::std::fmt::Debug + Clone {
48 /// Hook to initialize the extra data when an allocation gets created.
51 _memory_extra: &MemoryExtra
54 /// Hook for performing extra checks on a memory read access.
56 /// Takes read-only access to the allocation so we can keep all the memory read
57 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
61 _alloc: &Allocation<Tag, Self>,
64 ) -> EvalResult<'tcx> {
68 /// Hook for performing extra checks on a memory write access.
71 _alloc: &mut Allocation<Tag, Self>,
74 ) -> EvalResult<'tcx> {
78 /// Hook for performing extra checks on a memory deallocation.
79 /// `size` will be the size of the allocation.
81 fn memory_deallocated(
82 _alloc: &mut Allocation<Tag, Self>,
85 ) -> EvalResult<'tcx> {
90 impl AllocationExtra<(), ()> for () {
100 impl<Tag, Extra> Allocation<Tag, Extra> {
101 /// Creates a read-only allocation initialized by the given bytes
102 pub fn from_bytes(slice: &[u8], align: Align, extra: Extra) -> Self {
103 let undef_mask = UndefMask::new(Size::from_bytes(slice.len() as u64), true);
105 bytes: slice.to_owned(),
106 relocations: Relocations::new(),
109 mutability: Mutability::Immutable,
114 pub fn from_byte_aligned_bytes(slice: &[u8], extra: Extra) -> Self {
115 Allocation::from_bytes(slice, Align::from_bytes(1).unwrap(), extra)
118 pub fn undef(size: Size, align: Align, extra: Extra) -> Self {
119 assert_eq!(size.bytes() as usize as u64, size.bytes());
121 bytes: vec![0; size.bytes() as usize],
122 relocations: Relocations::new(),
123 undef_mask: UndefMask::new(size, false),
125 mutability: Mutability::Mutable,
131 impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
133 /// Alignment and bounds checks
134 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
135 /// Checks if the pointer is "in-bounds". Notice that a pointer pointing at the end
136 /// of an allocation (i.e., at the first *inaccessible* location) *is* considered
137 /// in-bounds! This follows C's/LLVM's rules.
138 /// If you want to check bounds before doing a memory access, better use `check_bounds`.
142 ) -> EvalResult<'tcx> {
143 let allocation_size = self.bytes.len() as u64;
144 ptr.check_in_alloc(Size::from_bytes(allocation_size), InboundsCheck::Live)
147 /// Checks if the memory range beginning at `ptr` and of size `Size` is "in-bounds".
151 cx: &impl HasDataLayout,
154 ) -> EvalResult<'tcx> {
155 // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
156 self.check_bounds_ptr(ptr.offset(size, cx)?)
161 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
162 /// The last argument controls whether we error out when there are undefined
163 /// or pointer bytes. You should never call this, call `get_bytes` or
164 /// `get_bytes_with_undef_and_ptr` instead,
166 /// This function also guarantees that the resulting pointer will remain stable
167 /// even when new allocations are pushed to the `HashMap`. `copy_repeatedly` relies
169 fn get_bytes_internal<MemoryExtra>(
171 cx: &impl HasDataLayout,
174 check_defined_and_ptr: bool,
175 ) -> EvalResult<'tcx, &[u8]>
176 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
177 where Extra: AllocationExtra<Tag, MemoryExtra>
179 self.check_bounds(cx, ptr, size)?;
181 if check_defined_and_ptr {
182 self.check_defined(ptr, size)?;
183 self.check_relocations(cx, ptr, size)?;
185 // We still don't want relocations on the *edges*
186 self.check_relocation_edges(cx, ptr, size)?;
189 AllocationExtra::memory_read(self, ptr, size)?;
191 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
192 assert_eq!(size.bytes() as usize as u64, size.bytes());
193 let offset = ptr.offset.bytes() as usize;
194 Ok(&self.bytes[offset..offset + size.bytes() as usize])
198 pub fn get_bytes<MemoryExtra>(
200 cx: &impl HasDataLayout,
203 ) -> EvalResult<'tcx, &[u8]>
204 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
205 where Extra: AllocationExtra<Tag, MemoryExtra>
207 self.get_bytes_internal(cx, ptr, size, true)
210 /// It is the caller's responsibility to handle undefined and pointer bytes.
211 /// However, this still checks that there are no relocations on the *edges*.
213 pub fn get_bytes_with_undef_and_ptr<MemoryExtra>(
215 cx: &impl HasDataLayout,
218 ) -> EvalResult<'tcx, &[u8]>
219 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
220 where Extra: AllocationExtra<Tag, MemoryExtra>
222 self.get_bytes_internal(cx, ptr, size, false)
225 /// Just calling this already marks everything as defined and removes relocations,
226 /// so be sure to actually put data there!
227 pub fn get_bytes_mut<MemoryExtra>(
229 cx: &impl HasDataLayout,
232 ) -> EvalResult<'tcx, &mut [u8]>
233 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
234 where Extra: AllocationExtra<Tag, MemoryExtra>
236 assert_ne!(size.bytes(), 0, "0-sized accesses should never even get a `Pointer`");
237 self.check_bounds(cx, ptr, size)?;
239 self.mark_definedness(ptr, size, true)?;
240 self.clear_relocations(cx, ptr, size)?;
242 AllocationExtra::memory_written(self, ptr, size)?;
244 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
245 assert_eq!(size.bytes() as usize as u64, size.bytes());
246 let offset = ptr.offset.bytes() as usize;
247 Ok(&mut self.bytes[offset..offset + size.bytes() as usize])
251 /// Reading and writing
252 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
253 /// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
254 /// before a `0` is found.
255 pub fn read_c_str<MemoryExtra>(
257 cx: &impl HasDataLayout,
259 ) -> EvalResult<'tcx, &[u8]>
260 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
261 where Extra: AllocationExtra<Tag, MemoryExtra>
263 assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes());
264 let offset = ptr.offset.bytes() as usize;
265 match self.bytes[offset..].iter().position(|&c| c == 0) {
267 let size_with_null = Size::from_bytes((size + 1) as u64);
268 // Go through `get_bytes` for checks and AllocationExtra hooks.
269 // We read the null, so we include it in the request, but we want it removed
271 Ok(&self.get_bytes(cx, ptr, size_with_null)?[..size])
273 None => err!(UnterminatedCString(ptr.erase_tag())),
277 /// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
278 /// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
279 /// given range contains neither relocations nor undef bytes.
280 pub fn check_bytes<MemoryExtra>(
282 cx: &impl HasDataLayout,
285 allow_ptr_and_undef: bool,
286 ) -> EvalResult<'tcx>
287 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
288 where Extra: AllocationExtra<Tag, MemoryExtra>
290 // Check bounds and relocations on the edges
291 self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
292 // Check undef and ptr
293 if !allow_ptr_and_undef {
294 self.check_defined(ptr, size)?;
295 self.check_relocations(cx, ptr, size)?;
300 /// Writes `src` to the memory starting at `ptr.offset`.
302 /// Will do bounds checks on the allocation.
303 pub fn write_bytes<MemoryExtra>(
305 cx: &impl HasDataLayout,
308 ) -> EvalResult<'tcx>
309 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
310 where Extra: AllocationExtra<Tag, MemoryExtra>
312 let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
313 bytes.clone_from_slice(src);
317 /// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
318 pub fn write_repeat<MemoryExtra>(
320 cx: &impl HasDataLayout,
324 ) -> EvalResult<'tcx>
325 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
326 where Extra: AllocationExtra<Tag, MemoryExtra>
328 let bytes = self.get_bytes_mut(cx, ptr, count)?;
335 /// Read a *non-ZST* scalar
337 /// zsts can't be read out of two reasons:
338 /// * byteorder cannot work with zero element buffers
339 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
340 /// being valid for ZSTs
342 /// Note: This function does not do *any* alignment checks, you need to do these before calling
343 pub fn read_scalar<MemoryExtra>(
345 cx: &impl HasDataLayout,
348 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
349 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
350 where Extra: AllocationExtra<Tag, MemoryExtra>
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<MemoryExtra>(
383 cx: &impl HasDataLayout,
385 ) -> EvalResult<'tcx, ScalarMaybeUndef<Tag>>
386 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
387 where Extra: AllocationExtra<Tag, MemoryExtra>
389 self.read_scalar(cx, ptr, cx.data_layout().pointer_size)
392 /// Write a *non-ZST* scalar
394 /// zsts can't be read out of two reasons:
395 /// * byteorder cannot work with zero element buffers
396 /// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
397 /// being valid for ZSTs
399 /// Note: This function does not do *any* alignment checks, you need to do these before calling
400 pub fn write_scalar<MemoryExtra>(
402 cx: &impl HasDataLayout,
404 val: ScalarMaybeUndef<Tag>,
406 ) -> EvalResult<'tcx>
407 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
408 where Extra: AllocationExtra<Tag, MemoryExtra>
410 let val = match val {
411 ScalarMaybeUndef::Scalar(scalar) => scalar,
412 ScalarMaybeUndef::Undef => return self.mark_definedness(ptr, type_size, false),
415 let bytes = match val {
416 Scalar::Ptr(val) => {
417 assert_eq!(type_size, cx.data_layout().pointer_size);
418 val.offset.bytes() as u128
421 Scalar::Bits { bits, size } => {
422 assert_eq!(size as u64, type_size.bytes());
423 debug_assert_eq!(truncate(bits, Size::from_bytes(size.into())), bits,
424 "Unexpected value of size {} when writing to memory", size);
429 let endian = cx.data_layout().endian;
430 let dst = self.get_bytes_mut(cx, ptr, type_size)?;
431 write_target_uint(endian, dst, bytes).unwrap();
433 // See if we have to also write a relocation
435 Scalar::Ptr(val) => {
436 self.relocations.insert(
438 (val.tag, val.alloc_id),
447 /// Note: This function does not do *any* alignment checks, you need to do these before calling
448 pub fn write_ptr_sized<MemoryExtra>(
450 cx: &impl HasDataLayout,
452 val: ScalarMaybeUndef<Tag>
453 ) -> EvalResult<'tcx>
454 // FIXME: Working around https://github.com/rust-lang/rust/issues/56209
455 where Extra: AllocationExtra<Tag, MemoryExtra>
457 let ptr_size = cx.data_layout().pointer_size;
458 self.write_scalar(cx, ptr.into(), val, ptr_size)
463 impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
464 /// Returns all relocations overlapping with the given ptr-offset pair.
467 cx: &impl HasDataLayout,
470 ) -> &[(Size, (Tag, AllocId))] {
471 // We have to go back `pointer_size - 1` bytes, as that one would still overlap with
472 // the beginning of this range.
473 let start = ptr.offset.bytes().saturating_sub(cx.data_layout().pointer_size.bytes() - 1);
474 let end = ptr.offset + size; // this does overflow checking
475 self.relocations.range(Size::from_bytes(start)..end)
478 /// Checks that there are no relocations overlapping with the given range.
480 fn check_relocations(
482 cx: &impl HasDataLayout,
485 ) -> EvalResult<'tcx> {
486 if self.relocations(cx, ptr, size).is_empty() {
489 err!(ReadPointerAsBytes)
493 /// Removes all relocations inside the given range.
494 /// If there are relocations overlapping with the edges, they
495 /// are removed as well *and* the bytes they cover are marked as
496 /// uninitialized. This is a somewhat odd "spooky action at a distance",
497 /// but it allows strictly more code to run than if we would just error
498 /// immediately in that case.
499 fn clear_relocations(
501 cx: &impl HasDataLayout,
504 ) -> EvalResult<'tcx> {
505 // Find the start and end of the given range and its outermost relocations.
506 let (first, last) = {
507 // Find all relocations overlapping the given range.
508 let relocations = self.relocations(cx, ptr, size);
509 if relocations.is_empty() {
513 (relocations.first().unwrap().0,
514 relocations.last().unwrap().0 + cx.data_layout().pointer_size)
516 let start = ptr.offset;
517 let end = start + size;
519 // Mark parts of the outermost relocations as undefined if they partially fall outside the
522 self.undef_mask.set_range(first, start, false);
525 self.undef_mask.set_range(end, last, false);
528 // Forget all the relocations.
529 self.relocations.remove_range(first..last);
534 /// Error if there are relocations overlapping with the edges of the
535 /// given memory range.
537 fn check_relocation_edges(
539 cx: &impl HasDataLayout,
542 ) -> EvalResult<'tcx> {
543 self.check_relocations(cx, ptr, Size::ZERO)?;
544 self.check_relocations(cx, ptr.offset(size, cx)?, Size::ZERO)?;
551 impl<'tcx, Tag, Extra> Allocation<Tag, Extra> {
552 /// Checks that a range of bytes is defined. If not, returns the `ReadUndefBytes`
553 /// error which will report the first byte which is undefined.
555 fn check_defined(&self, ptr: Pointer<Tag>, size: Size) -> EvalResult<'tcx> {
556 self.undef_mask.is_range_defined(
559 ).or_else(|idx| err!(ReadUndefBytes(idx)))
562 pub fn mark_definedness(
567 ) -> EvalResult<'tcx> {
568 if size.bytes() == 0 {
571 self.undef_mask.set_range(
581 #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, RustcEncodable, RustcDecodable)]
582 pub struct Relocations<Tag=(), Id=AllocId>(SortedMap<Size, (Tag, Id)>);
584 impl<Tag, Id> Relocations<Tag, Id> {
585 pub fn new() -> Self {
586 Relocations(SortedMap::new())
589 // The caller must guarantee that the given relocations are already sorted
590 // by address and contain no duplicates.
591 pub fn from_presorted(r: Vec<(Size, (Tag, Id))>) -> Self {
592 Relocations(SortedMap::from_presorted_elements(r))
596 impl<Tag> Deref for Relocations<Tag> {
597 type Target = SortedMap<Size, (Tag, AllocId)>;
599 fn deref(&self) -> &Self::Target {
604 impl<Tag> DerefMut for Relocations<Tag> {
605 fn deref_mut(&mut self) -> &mut Self::Target {
610 ////////////////////////////////////////////////////////////////////////////////
611 // Undefined byte tracking
612 ////////////////////////////////////////////////////////////////////////////////
616 #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
617 pub struct UndefMask {
622 impl_stable_hash_for!(struct mir::interpret::UndefMask{blocks, len});
625 pub const BLOCK_SIZE: u64 = 64;
627 pub fn new(size: Size, state: bool) -> Self {
628 let mut m = UndefMask {
636 /// Checks whether the range `start..end` (end-exclusive) is entirely defined.
638 /// Returns `Ok(())` if it's defined. Otherwise returns the index of the byte
639 /// at which the first undefined access begins.
641 pub fn is_range_defined(&self, start: Size, end: Size) -> Result<(), Size> {
643 return Err(self.len);
646 // FIXME(oli-obk): optimize this for allocations larger than a block.
647 let idx = (start.bytes()..end.bytes())
648 .map(|i| Size::from_bytes(i))
649 .find(|&i| !self.get(i));
652 Some(idx) => Err(idx),
657 pub fn set_range(&mut self, start: Size, end: Size, new_state: bool) {
660 self.grow(end - len, new_state);
662 self.set_range_inbounds(start, end, new_state);
665 pub fn set_range_inbounds(&mut self, start: Size, end: Size, new_state: bool) {
666 let (blocka, bita) = bit_index(start);
667 let (blockb, bitb) = bit_index(end);
668 if blocka == blockb {
669 // first set all bits but the first `bita`
670 // then unset the last `64 - bitb` bits
671 let range = if bitb == 0 {
672 u64::max_value() << bita
674 (u64::max_value() << bita) & (u64::max_value() >> (64 - bitb))
677 self.blocks[blocka] |= range;
679 self.blocks[blocka] &= !range;
683 // across block boundaries
686 self.blocks[blocka] |= u64::max_value() << bita;
689 self.blocks[blockb] |= u64::max_value() >> (64 - bitb);
691 // fill in all the other blocks (much faster than one bit at a time)
692 for block in (blocka + 1) .. blockb {
693 self.blocks[block] = u64::max_value();
697 self.blocks[blocka] &= !(u64::max_value() << bita);
700 self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb));
702 // fill in all the other blocks (much faster than one bit at a time)
703 for block in (blocka + 1) .. blockb {
704 self.blocks[block] = 0;
710 pub fn get(&self, i: Size) -> bool {
711 let (block, bit) = bit_index(i);
712 (self.blocks[block] & (1 << bit)) != 0
716 pub fn set(&mut self, i: Size, new_state: bool) {
717 let (block, bit) = bit_index(i);
718 self.set_bit(block, bit, new_state);
722 fn set_bit(&mut self, block: usize, bit: usize, new_state: bool) {
724 self.blocks[block] |= 1 << bit;
726 self.blocks[block] &= !(1 << bit);
730 pub fn grow(&mut self, amount: Size, new_state: bool) {
731 if amount.bytes() == 0 {
734 let unused_trailing_bits = self.blocks.len() as u64 * Self::BLOCK_SIZE - self.len.bytes();
735 if amount.bytes() > unused_trailing_bits {
736 let additional_blocks = amount.bytes() / Self::BLOCK_SIZE + 1;
737 assert_eq!(additional_blocks as usize as u64, additional_blocks);
739 // FIXME(oli-obk): optimize this by repeating `new_state as Block`
740 iter::repeat(0).take(additional_blocks as usize),
743 let start = self.len;
745 self.set_range_inbounds(start, start + amount, new_state);
750 fn bit_index(bits: Size) -> (usize, usize) {
751 let bits = bits.bytes();
752 let a = bits / UndefMask::BLOCK_SIZE;
753 let b = bits % UndefMask::BLOCK_SIZE;
754 assert_eq!(a as usize as u64, a);
755 assert_eq!(b as usize as u64, b);
756 (a as usize, b as usize)