1 use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian, BigEndian};
2 use std::collections::{btree_map, BTreeMap, HashMap, HashSet, VecDeque};
3 use std::{fmt, iter, ptr, mem, io, ops};
6 use rustc::ty::layout::{self, TargetDataLayout, HasDataLayout};
7 use syntax::ast::Mutability;
8 use rustc::middle::region::CodeExtent;
10 use error::{EvalError, EvalResult};
11 use value::{PrimVal, Pointer};
12 use eval_context::EvalContext;
14 ////////////////////////////////////////////////////////////////////////////////
16 ////////////////////////////////////////////////////////////////////////////////
21 // The derived `Ord` impl sorts first by the first field, then, if the fields are the same,
22 // by the second field.
23 // This is exactly what we need for our purposes, since a range query on a BTReeSet/BTreeMap will give us all
24 // `MemoryRange`s whose `start` is <= than the one we're looking for, but not > the end of the range we're checking.
25 // At the same time the `end` is irrelevant for the sorting and range searching, but used for the check.
26 // This kind of search breaks, if `end < start`, so don't do that!
27 #[derive(Eq, PartialEq, Ord, PartialOrd, Debug)]
28 pub struct MemoryRange {
34 pub fn new(offset: u64, len: u64) -> MemoryRange {
42 pub fn range(offset: u64, len: u64) -> ops::Range<MemoryRange> {
44 // We select all elements that are within
45 // the range given by the offset into the allocation and the length.
46 // This is sound if "self.contains() || self.overlaps() == true" implies that self is in-range.
47 let left = MemoryRange {
51 let right = MemoryRange {
52 start: offset + len + 1,
58 pub fn contains(&self, offset: u64, len: u64) -> bool {
60 self.start <= offset && (offset + len) <= self.end
63 pub fn overlaps(&self, offset: u64, len: u64) -> bool {
65 //let non_overlap = (offset + len) <= self.start || self.end <= offset;
66 (offset + len) > self.start && self.end > offset
72 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
78 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
79 pub struct DynamicLifetime {
81 region: Option<CodeExtent>, // "None" indicates "until the function ends"
84 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
87 RecoverAfter(CodeExtent), // the frame is given by the surrounding LockInfo's lifetime.
90 /// Information about a lock that is or will be held.
91 #[derive(Copy, Clone, Debug)]
94 lifetime: DynamicLifetime,
99 fn access_permitted(&self, frame: usize, access: AccessKind) -> bool {
100 use self::AccessKind::*;
101 match (self.kind, access) {
102 (Read, Read) => true, // Read access to read-locked region is okay, no matter who's holding the read lock.
103 (Write, _) if self.lifetime.frame == frame => true, // All access is okay when we hold the write lock.
104 _ => false, // Somebody else holding the write lock is not okay
109 ////////////////////////////////////////////////////////////////////////////////
110 // Allocations and pointers
111 ////////////////////////////////////////////////////////////////////////////////
113 #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
114 pub struct AllocId(pub u64);
116 impl fmt::Display for AllocId {
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 write!(f, "{}", self.0)
123 pub struct Allocation {
124 /// The actual bytes of the allocation.
125 /// Note that the bytes of a pointer represent the offset of the pointer
127 /// Maps from byte addresses to allocations.
128 /// Only the first byte of a pointer is inserted into the map.
129 pub relocations: BTreeMap<u64, AllocId>,
130 /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
131 pub undef_mask: UndefMask,
132 /// The alignment of the allocation to detect unaligned reads.
134 /// Whether the allocation may be modified.
135 pub mutable: Mutability,
136 /// Use the `mark_static_initalized` method of `Memory` to ensure that an error occurs, if the memory of this
137 /// allocation is modified or deallocated in the future.
138 /// Helps guarantee that stack allocations aren't deallocated via `rust_deallocate`
140 /// Memory regions that are locked by some function
141 locks: BTreeMap<MemoryRange, Vec<LockInfo>>,
144 #[derive(Debug, PartialEq, Copy, Clone)]
146 /// Error if deallocated any other way than `rust_deallocate`
148 /// Error if deallocated any other way than `free`
150 /// Error if deallocated except during a stack pop
152 /// Static in the process of being initialized.
153 /// The difference is important: An immutable static referring to a
154 /// mutable initialized static will freeze immutably and would not
155 /// be able to distinguish already initialized statics from uninitialized ones
157 /// May never be deallocated
159 /// Part of env var emulation
163 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
164 pub struct MemoryPointer {
165 pub alloc_id: AllocId,
169 impl<'tcx> MemoryPointer {
170 pub fn new(alloc_id: AllocId, offset: u64) -> Self {
171 MemoryPointer { alloc_id, offset }
174 pub(crate) fn wrapping_signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> Self {
175 MemoryPointer::new(self.alloc_id, cx.data_layout().wrapping_signed_offset(self.offset, i))
178 pub(crate) fn overflowing_signed_offset<C: HasDataLayout>(self, i: i128, cx: C) -> (Self, bool) {
179 let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset, i);
180 (MemoryPointer::new(self.alloc_id, res), over)
183 pub(crate) fn signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> EvalResult<'tcx, Self> {
184 Ok(MemoryPointer::new(self.alloc_id, cx.data_layout().signed_offset(self.offset, i)?))
187 pub(crate) fn overflowing_offset<C: HasDataLayout>(self, i: u64, cx: C) -> (Self, bool) {
188 let (res, over) = cx.data_layout().overflowing_offset(self.offset, i);
189 (MemoryPointer::new(self.alloc_id, res), over)
192 pub(crate) fn offset<C: HasDataLayout>(self, i: u64, cx: C) -> EvalResult<'tcx, Self> {
193 Ok(MemoryPointer::new(self.alloc_id, cx.data_layout().offset(self.offset, i)?))
197 ////////////////////////////////////////////////////////////////////////////////
198 // Top-level interpreter memory
199 ////////////////////////////////////////////////////////////////////////////////
201 pub type TlsKey = usize;
203 #[derive(Copy, Clone, Debug)]
204 pub struct TlsEntry<'tcx> {
205 data: Pointer, // Will eventually become a map from thread IDs to `Pointer`s, if we ever support more than one thread.
206 dtor: Option<ty::Instance<'tcx>>,
209 pub struct Memory<'a, 'tcx> {
210 /// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations).
211 alloc_map: HashMap<AllocId, Allocation>,
213 /// The AllocId to assign to the next new allocation. Always incremented, never gets smaller.
216 /// Set of statics, constants, promoteds, vtables, ... to prevent `mark_static_initalized` from
217 /// stepping out of its own allocations. This set only contains statics backed by an
218 /// allocation. If they are ByVal or ByValPair they are not here, but will be inserted once
219 /// they become ByRef.
220 static_alloc: HashSet<AllocId>,
222 /// Number of virtual bytes allocated.
225 /// Maximum number of virtual bytes that may be allocated.
228 /// Function "allocations". They exist solely so pointers have something to point to, and
229 /// we can figure out what they point to.
230 functions: HashMap<AllocId, ty::Instance<'tcx>>,
232 /// Inverse map of `functions` so we don't allocate a new pointer every time we need one
233 function_alloc_cache: HashMap<ty::Instance<'tcx>, AllocId>,
235 /// Target machine data layout to emulate.
236 pub layout: &'a TargetDataLayout,
238 /// A cache for basic byte allocations keyed by their contents. This is used to deduplicate
239 /// allocations for string and bytestring literals.
240 literal_alloc_cache: HashMap<Vec<u8>, AllocId>,
242 /// pthreads-style thread-local storage.
243 thread_local: BTreeMap<TlsKey, TlsEntry<'tcx>>,
245 /// The Key to use for the next thread-local allocation.
246 next_thread_local: TlsKey,
248 /// To avoid having to pass flags to every single memory access, we have some global state saying whether
249 /// alignment checking is currently enforced for read and/or write accesses.
250 reads_are_aligned: bool,
251 writes_are_aligned: bool,
253 /// The current stack frame. Used to check accesses against locks.
257 impl<'a, 'tcx> Memory<'a, 'tcx> {
258 pub fn new(layout: &'a TargetDataLayout, max_memory: u64) -> Self {
260 alloc_map: HashMap::new(),
261 functions: HashMap::new(),
262 function_alloc_cache: HashMap::new(),
265 memory_size: max_memory,
267 static_alloc: HashSet::new(),
268 literal_alloc_cache: HashMap::new(),
269 thread_local: BTreeMap::new(),
270 next_thread_local: 0,
271 reads_are_aligned: true,
272 writes_are_aligned: true,
273 cur_frame: usize::max_value(),
277 pub fn allocations(&self) -> ::std::collections::hash_map::Iter<AllocId, Allocation> {
278 self.alloc_map.iter()
281 pub fn create_fn_alloc(&mut self, instance: ty::Instance<'tcx>) -> MemoryPointer {
282 if let Some(&alloc_id) = self.function_alloc_cache.get(&instance) {
283 return MemoryPointer::new(alloc_id, 0);
285 let id = self.next_id;
286 debug!("creating fn ptr: {}", id);
288 self.functions.insert(id, instance);
289 self.function_alloc_cache.insert(instance, id);
290 MemoryPointer::new(id, 0)
293 pub fn allocate_cached(&mut self, bytes: &[u8]) -> EvalResult<'tcx, MemoryPointer> {
294 if let Some(&alloc_id) = self.literal_alloc_cache.get(bytes) {
295 return Ok(MemoryPointer::new(alloc_id, 0));
298 let ptr = self.allocate(bytes.len() as u64, 1, Kind::UninitializedStatic)?;
299 self.write_bytes(ptr.into(), bytes)?;
300 self.mark_static_initalized(ptr.alloc_id, Mutability::Immutable)?;
301 self.literal_alloc_cache.insert(bytes.to_vec(), ptr.alloc_id);
305 pub fn allocate(&mut self, size: u64, align: u64, kind: Kind) -> EvalResult<'tcx, MemoryPointer> {
306 assert_ne!(align, 0);
307 assert!(align.is_power_of_two());
309 if self.memory_size - self.memory_usage < size {
310 return Err(EvalError::OutOfMemory {
311 allocation_size: size,
312 memory_size: self.memory_size,
313 memory_usage: self.memory_usage,
316 self.memory_usage += size;
317 assert_eq!(size as usize as u64, size);
318 let alloc = Allocation {
319 bytes: vec![0; size as usize],
320 relocations: BTreeMap::new(),
321 undef_mask: UndefMask::new(size),
324 mutable: Mutability::Mutable,
325 locks: BTreeMap::new(),
327 let id = self.next_id;
329 self.alloc_map.insert(id, alloc);
330 Ok(MemoryPointer::new(id, 0))
333 pub fn reallocate(&mut self, ptr: MemoryPointer, old_size: u64, old_align: u64, new_size: u64, new_align: u64, kind: Kind) -> EvalResult<'tcx, MemoryPointer> {
337 return Err(EvalError::ReallocateNonBasePtr);
339 if let Ok(alloc) = self.get(ptr.alloc_id) {
340 if alloc.kind != kind {
341 return Err(EvalError::ReallocatedWrongMemoryKind(alloc.kind, kind));
345 // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc"
346 let new_ptr = self.allocate(new_size, new_align, kind)?;
347 self.copy(ptr.into(), new_ptr.into(), min(old_size, new_size), min(old_align, new_align), /*nonoverlapping*/true)?;
348 self.deallocate(ptr, Some((old_size, old_align)), kind)?;
353 pub fn deallocate(&mut self, ptr: MemoryPointer, size_and_align: Option<(u64, u64)>, kind: Kind) -> EvalResult<'tcx> {
355 return Err(EvalError::DeallocateNonBasePtr);
358 let alloc = match self.alloc_map.remove(&ptr.alloc_id) {
359 Some(alloc) => alloc,
360 None => return Err(EvalError::DoubleFree),
363 if alloc.kind != kind {
364 return Err(EvalError::DeallocatedWrongMemoryKind(alloc.kind, kind));
366 if !alloc.locks.is_empty() {
367 return Err(EvalError::DeallocatedLockedMemory);
369 if let Some((size, align)) = size_and_align {
370 if size != alloc.bytes.len() as u64 || align != alloc.align {
371 return Err(EvalError::IncorrectAllocationInformation);
375 self.memory_usage -= alloc.bytes.len() as u64;
376 debug!("deallocated : {}", ptr.alloc_id);
381 pub fn pointer_size(&self) -> u64 {
382 self.layout.pointer_size.bytes()
385 pub fn endianess(&self) -> layout::Endian {
389 /// Check that the pointer is aligned and non-NULL
390 pub fn check_align(&self, ptr: Pointer, align: u64) -> EvalResult<'tcx> {
391 let offset = match ptr.into_inner_primval() {
392 PrimVal::Ptr(ptr) => {
393 let alloc = self.get(ptr.alloc_id)?;
394 if alloc.align < align {
395 return Err(EvalError::AlignmentCheckFailed {
402 PrimVal::Bytes(bytes) => {
403 let v = ((bytes as u128) % (1 << self.pointer_size())) as u64;
405 return Err(EvalError::InvalidNullPointerUsage);
409 PrimVal::Undef => return Err(EvalError::ReadUndefBytes),
411 if offset % align == 0 {
414 Err(EvalError::AlignmentCheckFailed {
421 pub(crate) fn check_bounds(&self, ptr: MemoryPointer, access: bool) -> EvalResult<'tcx> {
422 let alloc = self.get(ptr.alloc_id)?;
423 let allocation_size = alloc.bytes.len() as u64;
424 if ptr.offset > allocation_size {
425 return Err(EvalError::PointerOutOfBounds { ptr, access, allocation_size });
430 pub(crate) fn set_cur_frame(&mut self, cur_frame: usize) {
431 self.cur_frame = cur_frame;
434 pub(crate) fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey {
435 let new_key = self.next_thread_local;
436 self.next_thread_local += 1;
437 self.thread_local.insert(new_key, TlsEntry { data: Pointer::null(), dtor });
438 trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
442 pub(crate) fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
443 return match self.thread_local.remove(&key) {
445 trace!("TLS key {} removed", key);
448 None => Err(EvalError::TlsOutOfBounds)
452 pub(crate) fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Pointer> {
453 return match self.thread_local.get(&key) {
454 Some(&TlsEntry { data, .. }) => {
455 trace!("TLS key {} loaded: {:?}", key, data);
458 None => Err(EvalError::TlsOutOfBounds)
462 pub(crate) fn store_tls(&mut self, key: TlsKey, new_data: Pointer) -> EvalResult<'tcx> {
463 return match self.thread_local.get_mut(&key) {
464 Some(&mut TlsEntry { ref mut data, .. }) => {
465 trace!("TLS key {} stored: {:?}", key, new_data);
469 None => Err(EvalError::TlsOutOfBounds)
473 /// Returns a dtor, its argument and its index, if one is supposed to run
475 /// An optional destructor function may be associated with each key value.
476 /// At thread exit, if a key value has a non-NULL destructor pointer,
477 /// and the thread has a non-NULL value associated with that key,
478 /// the value of the key is set to NULL, and then the function pointed
479 /// to is called with the previously associated value as its sole argument.
480 /// The order of destructor calls is unspecified if more than one destructor
481 /// exists for a thread when it exits.
483 /// If, after all the destructors have been called for all non-NULL values
484 /// with associated destructors, there are still some non-NULL values with
485 /// associated destructors, then the process is repeated.
486 /// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
487 /// calls for outstanding non-NULL values, there are still some non-NULL values
488 /// with associated destructors, implementations may stop calling destructors,
489 /// or they may continue calling destructors until no non-NULL values with
490 /// associated destructors exist, even though this might result in an infinite loop.
491 pub(crate) fn fetch_tls_dtor(&mut self, key: Option<TlsKey>) -> EvalResult<'tcx, Option<(ty::Instance<'tcx>, Pointer, TlsKey)>> {
492 use std::collections::Bound::*;
493 let start = match key {
494 Some(key) => Excluded(key),
497 for (&key, &mut TlsEntry { ref mut data, dtor }) in self.thread_local.range_mut((start, Unbounded)) {
498 if !data.is_null()? {
499 if let Some(dtor) = dtor {
500 let ret = Some((dtor, *data, key));
501 *data = Pointer::null();
511 impl<'a, 'tcx> Memory<'a, 'tcx> {
512 pub(crate) fn check_locks(&self, ptr: MemoryPointer, len: u64, access: AccessKind) -> EvalResult<'tcx> {
513 let alloc = self.get(ptr.alloc_id)?;
514 for (range, locks) in alloc.locks.range(MemoryRange::range(ptr.offset, len)) {
516 // Check if the lock is active, overlaps this access, and is in conflict with the access.
517 if lock.status == LockStatus::Held && range.overlaps(ptr.offset, len) && !lock.access_permitted(self.cur_frame, access) {
518 return Err(EvalError::MemoryLockViolation { ptr, len, access, lock: *lock });
525 /// Acquire the lock for the given lifetime
526 pub(crate) fn acquire_lock(&mut self, ptr: MemoryPointer, len: u64, region: Option<CodeExtent>, kind: AccessKind) -> EvalResult<'tcx> {
527 self.check_bounds(ptr.offset(len, self.layout)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
528 self.check_locks(ptr, len, kind)?; // make sure we have the access we are acquiring
529 let lifetime = DynamicLifetime { frame: self.cur_frame, region };
530 let alloc = self.get_mut(ptr.alloc_id)?;
531 alloc.locks.entry(MemoryRange::new(ptr.offset, len)).or_insert_with(|| Vec::new()).push(LockInfo { lifetime, kind, status: LockStatus::Held });
535 /// Release a lock prematurely
536 pub(crate) fn release_lock_until(&mut self, ptr: MemoryPointer, len: u64, release_until: Option<CodeExtent>) -> EvalResult<'tcx> {
537 // Make sure there are no read locks and no *other* write locks here
538 if let Err(_) = self.check_locks(ptr, len, AccessKind::Write) {
539 return Err(EvalError::InvalidMemoryLockRelease { ptr, len });
541 let cur_frame = self.cur_frame;
542 let alloc = self.get_mut(ptr.alloc_id)?;
544 let lock_infos = alloc.locks.get_mut(&MemoryRange::new(ptr.offset, len)).ok_or(EvalError::InvalidMemoryLockRelease { ptr, len })?;
545 let lock_info = match lock_infos.len() {
546 0 => return Err(EvalError::InvalidMemoryLockRelease { ptr, len }),
547 1 => &mut lock_infos[0],
548 _ => bug!("There can not be overlapping locks when write access is possible."),
550 assert_eq!(lock_info.lifetime.frame, cur_frame);
551 if let Some(ce) = release_until {
552 lock_info.status = LockStatus::RecoverAfter(ce);
556 // Falling through to here means we want to entirely remove the lock. The control-flow is somewhat weird because of lexical lifetimes.
557 alloc.locks.remove(&MemoryRange::new(ptr.offset, len));
561 pub(crate) fn locks_lifetime_ended(&mut self, ending_region: Option<CodeExtent>) {
562 let cur_frame = self.cur_frame;
563 let has_ended = |lock: &LockInfo| -> bool {
564 if lock.lifetime.frame != cur_frame {
567 match ending_region {
568 None => true, // When a function ends, we end *all* its locks. It's okay for a function to still have lifetime-related locks
569 // when it returns, that can happen e.g. with NLL when a lifetime can, but does not have to, extend beyond the
570 // end of a function.
571 Some(ending_region) => lock.lifetime.region == Some(ending_region),
575 for alloc in self.alloc_map.values_mut() {
576 for (_range, locks) in alloc.locks.iter_mut() {
577 // Delete everything that ends now -- i.e., keep only all the other lifeimes.
578 locks.retain(|lock| !has_ended(lock));
579 // Activate locks that get recovered now
580 if let Some(ending_region) = ending_region {
581 for lock in locks.iter_mut() {
582 if lock.lifetime.frame == cur_frame && lock.status == LockStatus::RecoverAfter(ending_region) {
583 lock.status = LockStatus::Held;
589 // TODO: It may happen now that we leave empty vectors in the map. Is it worth getting rid of them?
593 /// Allocation accessors
594 impl<'a, 'tcx> Memory<'a, 'tcx> {
595 pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
596 match self.alloc_map.get(&id) {
597 Some(alloc) => Ok(alloc),
598 None => match self.functions.get(&id) {
599 Some(_) => Err(EvalError::DerefFunctionPointer),
600 None => Err(EvalError::DanglingPointerDeref),
605 pub fn get_mut(&mut self, id: AllocId) -> EvalResult<'tcx, &mut Allocation> {
606 match self.alloc_map.get_mut(&id) {
607 Some(alloc) => if alloc.mutable == Mutability::Mutable {
610 Err(EvalError::ModifiedConstantMemory)
612 None => match self.functions.get(&id) {
613 Some(_) => Err(EvalError::DerefFunctionPointer),
614 None => Err(EvalError::DanglingPointerDeref),
619 pub fn get_fn(&self, ptr: MemoryPointer) -> EvalResult<'tcx, ty::Instance<'tcx>> {
621 return Err(EvalError::InvalidFunctionPointer);
623 debug!("reading fn ptr: {}", ptr.alloc_id);
624 match self.functions.get(&ptr.alloc_id) {
625 Some(&fndef) => Ok(fndef),
626 None => match self.alloc_map.get(&ptr.alloc_id) {
627 Some(_) => Err(EvalError::ExecuteMemory),
628 None => Err(EvalError::InvalidFunctionPointer),
633 /// For debugging, print an allocation and all allocations it points to, recursively.
634 pub fn dump_alloc(&self, id: AllocId) {
635 self.dump_allocs(vec![id]);
638 /// For debugging, print a list of allocations and all allocations they point to, recursively.
639 pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
643 let mut allocs_to_print = VecDeque::from(allocs);
644 let mut allocs_seen = HashSet::new();
646 while let Some(id) = allocs_to_print.pop_front() {
647 let mut msg = format!("Alloc {:<5} ", format!("{}:", id));
648 let prefix_len = msg.len();
649 let mut relocations = vec![];
651 let alloc = match (self.alloc_map.get(&id), self.functions.get(&id)) {
652 (Some(a), None) => a,
653 (None, Some(instance)) => {
654 trace!("{} {}", msg, instance);
658 trace!("{} (deallocated)", msg);
661 (Some(_), Some(_)) => bug!("miri invariant broken: an allocation id exists that points to both a function and a memory location"),
664 for i in 0..(alloc.bytes.len() as u64) {
665 if let Some(&target_id) = alloc.relocations.get(&i) {
666 if allocs_seen.insert(target_id) {
667 allocs_to_print.push_back(target_id);
669 relocations.push((i, target_id));
671 if alloc.undef_mask.is_range_defined(i, i + 1) {
672 // this `as usize` is fine, since `i` came from a `usize`
673 write!(msg, "{:02x} ", alloc.bytes[i as usize]).unwrap();
679 let immutable = match (alloc.kind, alloc.mutable) {
680 (Kind::UninitializedStatic, _) => " (static in the process of initialization)",
681 (Kind::Static, Mutability::Mutable) => " (static mut)",
682 (Kind::Static, Mutability::Immutable) => " (immutable)",
683 (Kind::Env, _) => " (env var)",
684 (Kind::C, _) => " (malloc)",
685 (Kind::Rust, _) => " (heap)",
686 (Kind::Stack, _) => " (stack)",
688 trace!("{}({} bytes, alignment {}){}", msg, alloc.bytes.len(), alloc.align, immutable);
690 if !relocations.is_empty() {
692 write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
694 let relocation_width = (self.pointer_size() - 1) * 3;
695 for (i, target_id) in relocations {
696 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
697 write!(msg, "{:1$}", "", ((i - pos) * 3) as usize).unwrap();
698 let target = format!("({})", target_id);
699 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
700 write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
701 pos = i + self.pointer_size();
708 pub fn leak_report(&self) -> usize {
709 trace!("### LEAK REPORT ###");
710 let leaks: Vec<_> = self.alloc_map
712 .filter_map(|(&key, val)| {
713 if val.kind != Kind::Static {
721 self.dump_allocs(leaks);
727 impl<'a, 'tcx> Memory<'a, 'tcx> {
728 fn get_bytes_unchecked(&self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &[u8]> {
729 // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
730 if self.reads_are_aligned {
731 self.check_align(ptr.into(), align)?;
736 self.check_locks(ptr, size, AccessKind::Read)?;
737 self.check_bounds(ptr.offset(size, self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
738 let alloc = self.get(ptr.alloc_id)?;
739 assert_eq!(ptr.offset as usize as u64, ptr.offset);
740 assert_eq!(size as usize as u64, size);
741 let offset = ptr.offset as usize;
742 Ok(&alloc.bytes[offset..offset + size as usize])
745 fn get_bytes_unchecked_mut(&mut self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &mut [u8]> {
746 // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
747 if self.writes_are_aligned {
748 self.check_align(ptr.into(), align)?;
753 self.check_locks(ptr, size, AccessKind::Write)?;
754 self.check_bounds(ptr.offset(size, self.layout)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
755 let alloc = self.get_mut(ptr.alloc_id)?;
756 assert_eq!(ptr.offset as usize as u64, ptr.offset);
757 assert_eq!(size as usize as u64, size);
758 let offset = ptr.offset as usize;
759 Ok(&mut alloc.bytes[offset..offset + size as usize])
762 fn get_bytes(&self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &[u8]> {
764 if self.relocations(ptr, size)?.count() != 0 {
765 return Err(EvalError::ReadPointerAsBytes);
767 self.check_defined(ptr, size)?;
768 self.get_bytes_unchecked(ptr, size, align)
771 fn get_bytes_mut(&mut self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &mut [u8]> {
773 self.clear_relocations(ptr, size)?;
774 self.mark_definedness(ptr.into(), size, true)?;
775 self.get_bytes_unchecked_mut(ptr, size, align)
779 /// Reading and writing
780 impl<'a, 'tcx> Memory<'a, 'tcx> {
781 /// mark an allocation as being the entry point to a static (see `static_alloc` field)
782 pub fn mark_static(&mut self, alloc_id: AllocId) {
783 trace!("mark_static: {:?}", alloc_id);
784 if !self.static_alloc.insert(alloc_id) {
785 bug!("tried to mark an allocation ({:?}) as static twice", alloc_id);
789 /// mark an allocation pointed to by a static as static and initialized
790 pub fn mark_inner_allocation(&mut self, alloc: AllocId, mutability: Mutability) -> EvalResult<'tcx> {
791 // relocations into other statics are not "inner allocations"
792 if !self.static_alloc.contains(&alloc) {
793 self.mark_static_initalized(alloc, mutability)?;
798 /// mark an allocation as static and initialized, either mutable or not
799 pub fn mark_static_initalized(&mut self, alloc_id: AllocId, mutability: Mutability) -> EvalResult<'tcx> {
800 trace!("mark_static_initalized {:?}, mutability: {:?}", alloc_id, mutability);
801 // do not use `self.get_mut(alloc_id)` here, because we might have already marked a
802 // sub-element or have circular pointers (e.g. `Rc`-cycles)
803 let relocations = match self.alloc_map.get_mut(&alloc_id) {
804 Some(&mut Allocation { ref mut relocations, ref mut kind, ref mut mutable, .. }) => {
806 // const eval results can refer to "locals".
807 // E.g. `const Foo: &u32 = &1;` refers to the temp local that stores the `1`
809 // The entire point of this function
810 Kind::UninitializedStatic |
811 // In the future const eval will allow heap allocations so we'll need to protect them
812 // from deallocation, too
816 trace!("mark_static_initalized: skipping already initialized static referred to by static currently being initialized");
819 // FIXME: This could be allowed, but not for env vars set during miri execution
820 Kind::Env => return Err(EvalError::Unimplemented("statics can't refer to env vars".to_owned())),
822 *kind = Kind::Static;
823 *mutable = mutability;
824 // take out the relocations vector to free the borrow on self, so we can call
826 mem::replace(relocations, Default::default())
828 None if !self.functions.contains_key(&alloc_id) => return Err(EvalError::DanglingPointerDeref),
831 // recurse into inner allocations
832 for &alloc in relocations.values() {
833 self.mark_inner_allocation(alloc, mutability)?;
835 // put back the relocations
836 self.alloc_map.get_mut(&alloc_id).expect("checked above").relocations = relocations;
840 pub fn copy(&mut self, src: Pointer, dest: Pointer, size: u64, align: u64, nonoverlapping: bool) -> EvalResult<'tcx> {
842 // Empty accesses don't need to be valid pointers, but they should still be aligned
843 if self.reads_are_aligned {
844 self.check_align(src, align)?;
846 if self.writes_are_aligned {
847 self.check_align(dest, align)?;
851 let src = src.to_ptr()?;
852 let dest = dest.to_ptr()?;
853 self.check_relocation_edges(src, size)?;
855 let src_bytes = self.get_bytes_unchecked(src, size, align)?.as_ptr();
856 let dest_bytes = self.get_bytes_mut(dest, size, align)?.as_mut_ptr();
858 // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
859 // behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
860 // `dest` could possibly overlap.
862 assert_eq!(size as usize as u64, size);
863 if src.alloc_id == dest.alloc_id {
865 if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
866 (dest.offset <= src.offset && dest.offset + size > src.offset) {
867 return Err(EvalError::Intrinsic(format!("copy_nonoverlapping called on overlapping ranges")));
870 ptr::copy(src_bytes, dest_bytes, size as usize);
872 ptr::copy_nonoverlapping(src_bytes, dest_bytes, size as usize);
876 self.copy_undef_mask(src, dest, size)?;
877 self.copy_relocations(src, dest, size)?;
882 pub fn read_c_str(&self, ptr: MemoryPointer) -> EvalResult<'tcx, &[u8]> {
883 let alloc = self.get(ptr.alloc_id)?;
884 assert_eq!(ptr.offset as usize as u64, ptr.offset);
885 let offset = ptr.offset as usize;
886 match alloc.bytes[offset..].iter().position(|&c| c == 0) {
888 if self.relocations(ptr, (size + 1) as u64)?.count() != 0 {
889 return Err(EvalError::ReadPointerAsBytes);
891 self.check_defined(ptr, (size + 1) as u64)?;
892 self.check_locks(ptr, (size + 1) as u64, AccessKind::Read)?;
893 Ok(&alloc.bytes[offset..offset + size])
895 None => Err(EvalError::UnterminatedCString(ptr)),
899 pub fn read_bytes(&self, ptr: Pointer, size: u64) -> EvalResult<'tcx, &[u8]> {
901 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
902 if self.reads_are_aligned {
903 self.check_align(ptr, 1)?;
907 self.get_bytes(ptr.to_ptr()?, size, 1)
910 pub fn write_bytes(&mut self, ptr: Pointer, src: &[u8]) -> EvalResult<'tcx> {
912 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
913 if self.writes_are_aligned {
914 self.check_align(ptr, 1)?;
918 let bytes = self.get_bytes_mut(ptr.to_ptr()?, src.len() as u64, 1)?;
919 bytes.clone_from_slice(src);
923 pub fn write_repeat(&mut self, ptr: Pointer, val: u8, count: u64) -> EvalResult<'tcx> {
925 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
926 if self.writes_are_aligned {
927 self.check_align(ptr, 1)?;
931 let bytes = self.get_bytes_mut(ptr.to_ptr()?, count, 1)?;
932 for b in bytes { *b = val; }
936 pub fn read_ptr(&self, ptr: MemoryPointer) -> EvalResult<'tcx, Pointer> {
937 let size = self.pointer_size();
938 self.check_relocation_edges(ptr, size)?; // Make sure we don't read part of a pointer as a pointer
939 let endianess = self.endianess();
940 let bytes = self.get_bytes_unchecked(ptr, size, size)?;
941 // Undef check happens *after* we established that the alignment is correct.
942 // We must not return Ok() for unaligned pointers!
943 if self.check_defined(ptr, size).is_err() {
944 return Ok(PrimVal::Undef.into());
946 let offset = read_target_uint(endianess, bytes).unwrap();
947 assert_eq!(offset as u64 as u128, offset);
948 let offset = offset as u64;
949 let alloc = self.get(ptr.alloc_id)?;
950 match alloc.relocations.get(&ptr.offset) {
951 Some(&alloc_id) => Ok(PrimVal::Ptr(MemoryPointer::new(alloc_id, offset)).into()),
952 None => Ok(PrimVal::Bytes(offset as u128).into()),
956 pub fn write_ptr(&mut self, dest: MemoryPointer, ptr: MemoryPointer) -> EvalResult<'tcx> {
957 self.write_usize(dest, ptr.offset as u64)?;
958 self.get_mut(dest.alloc_id)?.relocations.insert(dest.offset, ptr.alloc_id);
962 pub fn write_primval(
967 ) -> EvalResult<'tcx> {
969 PrimVal::Ptr(ptr) => {
970 assert_eq!(size, self.pointer_size());
971 self.write_ptr(dest.to_ptr()?, ptr)
974 PrimVal::Bytes(bytes) => {
975 // We need to mask here, or the byteorder crate can die when given a u64 larger
976 // than fits in an integer of the requested size.
977 let mask = match size {
983 n => bug!("unexpected PrimVal::Bytes size: {}", n),
985 self.write_uint(dest.to_ptr()?, bytes & mask, size)
988 PrimVal::Undef => self.mark_definedness(dest, size, false),
992 pub fn read_bool(&self, ptr: MemoryPointer) -> EvalResult<'tcx, bool> {
993 let bytes = self.get_bytes(ptr, 1, self.layout.i1_align.abi())?;
997 _ => Err(EvalError::InvalidBool),
1001 pub fn write_bool(&mut self, ptr: MemoryPointer, b: bool) -> EvalResult<'tcx> {
1002 let align = self.layout.i1_align.abi();
1003 self.get_bytes_mut(ptr, 1, align)
1004 .map(|bytes| bytes[0] = b as u8)
1007 fn int_align(&self, size: u64) -> EvalResult<'tcx, u64> {
1009 1 => Ok(self.layout.i8_align.abi()),
1010 2 => Ok(self.layout.i16_align.abi()),
1011 4 => Ok(self.layout.i32_align.abi()),
1012 8 => Ok(self.layout.i64_align.abi()),
1013 16 => Ok(self.layout.i128_align.abi()),
1014 _ => bug!("bad integer size: {}", size),
1018 pub fn read_int(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx, i128> {
1019 let align = self.int_align(size)?;
1020 self.get_bytes(ptr, size, align).map(|b| read_target_int(self.endianess(), b).unwrap())
1023 pub fn write_int(&mut self, ptr: MemoryPointer, n: i128, size: u64) -> EvalResult<'tcx> {
1024 let align = self.int_align(size)?;
1025 let endianess = self.endianess();
1026 let b = self.get_bytes_mut(ptr, size, align)?;
1027 write_target_int(endianess, b, n).unwrap();
1031 pub fn read_uint(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx, u128> {
1032 let align = self.int_align(size)?;
1033 self.get_bytes(ptr, size, align).map(|b| read_target_uint(self.endianess(), b).unwrap())
1036 pub fn write_uint(&mut self, ptr: MemoryPointer, n: u128, size: u64) -> EvalResult<'tcx> {
1037 let align = self.int_align(size)?;
1038 let endianess = self.endianess();
1039 let b = self.get_bytes_mut(ptr, size, align)?;
1040 write_target_uint(endianess, b, n).unwrap();
1044 pub fn read_isize(&self, ptr: MemoryPointer) -> EvalResult<'tcx, i64> {
1045 self.read_int(ptr, self.pointer_size()).map(|i| i as i64)
1048 pub fn write_isize(&mut self, ptr: MemoryPointer, n: i64) -> EvalResult<'tcx> {
1049 let size = self.pointer_size();
1050 self.write_int(ptr, n as i128, size)
1053 pub fn read_usize(&self, ptr: MemoryPointer) -> EvalResult<'tcx, u64> {
1054 self.read_uint(ptr, self.pointer_size()).map(|i| i as u64)
1057 pub fn write_usize(&mut self, ptr: MemoryPointer, n: u64) -> EvalResult<'tcx> {
1058 let size = self.pointer_size();
1059 self.write_uint(ptr, n as u128, size)
1062 pub fn write_f32(&mut self, ptr: MemoryPointer, f: f32) -> EvalResult<'tcx> {
1063 let endianess = self.endianess();
1064 let align = self.layout.f32_align.abi();
1065 let b = self.get_bytes_mut(ptr, 4, align)?;
1066 write_target_f32(endianess, b, f).unwrap();
1070 pub fn write_f64(&mut self, ptr: MemoryPointer, f: f64) -> EvalResult<'tcx> {
1071 let endianess = self.endianess();
1072 let align = self.layout.f64_align.abi();
1073 let b = self.get_bytes_mut(ptr, 8, align)?;
1074 write_target_f64(endianess, b, f).unwrap();
1078 pub fn read_f32(&self, ptr: MemoryPointer) -> EvalResult<'tcx, f32> {
1079 self.get_bytes(ptr, 4, self.layout.f32_align.abi())
1080 .map(|b| read_target_f32(self.endianess(), b).unwrap())
1083 pub fn read_f64(&self, ptr: MemoryPointer) -> EvalResult<'tcx, f64> {
1084 self.get_bytes(ptr, 8, self.layout.f64_align.abi())
1085 .map(|b| read_target_f64(self.endianess(), b).unwrap())
1090 impl<'a, 'tcx> Memory<'a, 'tcx> {
1091 fn relocations(&self, ptr: MemoryPointer, size: u64)
1092 -> EvalResult<'tcx, btree_map::Range<u64, AllocId>>
1094 let start = ptr.offset.saturating_sub(self.pointer_size() - 1);
1095 let end = ptr.offset + size;
1096 Ok(self.get(ptr.alloc_id)?.relocations.range(start..end))
1099 fn clear_relocations(&mut self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1100 // Find all relocations overlapping the given range.
1101 let keys: Vec<_> = self.relocations(ptr, size)?.map(|(&k, _)| k).collect();
1102 if keys.is_empty() { return Ok(()); }
1104 // Find the start and end of the given range and its outermost relocations.
1105 let start = ptr.offset;
1106 let end = start + size;
1107 let first = *keys.first().unwrap();
1108 let last = *keys.last().unwrap() + self.pointer_size();
1110 let alloc = self.get_mut(ptr.alloc_id)?;
1112 // Mark parts of the outermost relocations as undefined if they partially fall outside the
1114 if first < start { alloc.undef_mask.set_range(first, start, false); }
1115 if last > end { alloc.undef_mask.set_range(end, last, false); }
1117 // Forget all the relocations.
1118 for k in keys { alloc.relocations.remove(&k); }
1123 fn check_relocation_edges(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1124 let overlapping_start = self.relocations(ptr, 0)?.count();
1125 let overlapping_end = self.relocations(ptr.offset(size, self.layout)?, 0)?.count();
1126 if overlapping_start + overlapping_end != 0 {
1127 return Err(EvalError::ReadPointerAsBytes);
1132 fn copy_relocations(&mut self, src: MemoryPointer, dest: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1133 let relocations: Vec<_> = self.relocations(src, size)?
1134 .map(|(&offset, &alloc_id)| {
1135 // Update relocation offsets for the new positions in the destination allocation.
1136 (offset + dest.offset - src.offset, alloc_id)
1139 self.get_mut(dest.alloc_id)?.relocations.extend(relocations);
1145 impl<'a, 'tcx> Memory<'a, 'tcx> {
1146 // FIXME(solson): This is a very naive, slow version.
1147 fn copy_undef_mask(&mut self, src: MemoryPointer, dest: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1148 // The bits have to be saved locally before writing to dest in case src and dest overlap.
1149 assert_eq!(size as usize as u64, size);
1150 let mut v = Vec::with_capacity(size as usize);
1152 let defined = self.get(src.alloc_id)?.undef_mask.get(src.offset + i);
1155 for (i, defined) in v.into_iter().enumerate() {
1156 self.get_mut(dest.alloc_id)?.undef_mask.set(dest.offset + i as u64, defined);
1161 fn check_defined(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1162 let alloc = self.get(ptr.alloc_id)?;
1163 if !alloc.undef_mask.is_range_defined(ptr.offset, ptr.offset + size) {
1164 return Err(EvalError::ReadUndefBytes);
1169 pub fn mark_definedness(
1174 ) -> EvalResult<'tcx> {
1178 let ptr = ptr.to_ptr()?;
1179 let mut alloc = self.get_mut(ptr.alloc_id)?;
1180 alloc.undef_mask.set_range(ptr.offset, ptr.offset + size, new_state);
1185 ////////////////////////////////////////////////////////////////////////////////
1186 // Methods to access integers in the target endianess
1187 ////////////////////////////////////////////////////////////////////////////////
1189 fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u128) -> Result<(), io::Error> {
1190 let len = target.len();
1192 layout::Endian::Little => target.write_uint128::<LittleEndian>(data, len),
1193 layout::Endian::Big => target.write_uint128::<BigEndian>(data, len),
1196 fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i128) -> Result<(), io::Error> {
1197 let len = target.len();
1199 layout::Endian::Little => target.write_int128::<LittleEndian>(data, len),
1200 layout::Endian::Big => target.write_int128::<BigEndian>(data, len),
1204 fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u128, io::Error> {
1206 layout::Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
1207 layout::Endian::Big => source.read_uint128::<BigEndian>(source.len()),
1210 fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i128, io::Error> {
1212 layout::Endian::Little => source.read_int128::<LittleEndian>(source.len()),
1213 layout::Endian::Big => source.read_int128::<BigEndian>(source.len()),
1217 ////////////////////////////////////////////////////////////////////////////////
1218 // Methods to access floats in the target endianess
1219 ////////////////////////////////////////////////////////////////////////////////
1221 fn write_target_f32(endianess: layout::Endian, mut target: &mut [u8], data: f32) -> Result<(), io::Error> {
1223 layout::Endian::Little => target.write_f32::<LittleEndian>(data),
1224 layout::Endian::Big => target.write_f32::<BigEndian>(data),
1227 fn write_target_f64(endianess: layout::Endian, mut target: &mut [u8], data: f64) -> Result<(), io::Error> {
1229 layout::Endian::Little => target.write_f64::<LittleEndian>(data),
1230 layout::Endian::Big => target.write_f64::<BigEndian>(data),
1234 fn read_target_f32(endianess: layout::Endian, mut source: &[u8]) -> Result<f32, io::Error> {
1236 layout::Endian::Little => source.read_f32::<LittleEndian>(),
1237 layout::Endian::Big => source.read_f32::<BigEndian>(),
1240 fn read_target_f64(endianess: layout::Endian, mut source: &[u8]) -> Result<f64, io::Error> {
1242 layout::Endian::Little => source.read_f64::<LittleEndian>(),
1243 layout::Endian::Big => source.read_f64::<BigEndian>(),
1247 ////////////////////////////////////////////////////////////////////////////////
1248 // Undefined byte tracking
1249 ////////////////////////////////////////////////////////////////////////////////
1252 const BLOCK_SIZE: u64 = 64;
1254 #[derive(Clone, Debug)]
1255 pub struct UndefMask {
1261 fn new(size: u64) -> Self {
1262 let mut m = UndefMask {
1266 m.grow(size, false);
1270 /// Check whether the range `start..end` (end-exclusive) is entirely defined.
1271 pub fn is_range_defined(&self, start: u64, end: u64) -> bool {
1272 if end > self.len { return false; }
1273 for i in start..end {
1274 if !self.get(i) { return false; }
1279 fn set_range(&mut self, start: u64, end: u64, new_state: bool) {
1281 if end > len { self.grow(end - len, new_state); }
1282 self.set_range_inbounds(start, end, new_state);
1285 fn set_range_inbounds(&mut self, start: u64, end: u64, new_state: bool) {
1286 for i in start..end { self.set(i, new_state); }
1289 fn get(&self, i: u64) -> bool {
1290 let (block, bit) = bit_index(i);
1291 (self.blocks[block] & 1 << bit) != 0
1294 fn set(&mut self, i: u64, new_state: bool) {
1295 let (block, bit) = bit_index(i);
1297 self.blocks[block] |= 1 << bit;
1299 self.blocks[block] &= !(1 << bit);
1303 fn grow(&mut self, amount: u64, new_state: bool) {
1304 let unused_trailing_bits = self.blocks.len() as u64 * BLOCK_SIZE - self.len;
1305 if amount > unused_trailing_bits {
1306 let additional_blocks = amount / BLOCK_SIZE + 1;
1307 assert_eq!(additional_blocks as usize as u64, additional_blocks);
1308 self.blocks.extend(iter::repeat(0).take(additional_blocks as usize));
1310 let start = self.len;
1312 self.set_range_inbounds(start, start + amount, new_state);
1316 fn bit_index(bits: u64) -> (usize, usize) {
1317 let a = bits / BLOCK_SIZE;
1318 let b = bits % BLOCK_SIZE;
1319 assert_eq!(a as usize as u64, a);
1320 assert_eq!(b as usize as u64, b);
1321 (a as usize, b as usize)
1324 ////////////////////////////////////////////////////////////////////////////////
1325 // Unaligned accesses
1326 ////////////////////////////////////////////////////////////////////////////////
1328 pub(crate) trait HasMemory<'a, 'tcx> {
1329 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx>;
1330 fn memory(&self) -> &Memory<'a, 'tcx>;
1332 // These are not supposed to be overriden.
1333 fn read_maybe_aligned<F, T>(&mut self, aligned: bool, f: F) -> EvalResult<'tcx, T>
1334 where F: FnOnce(&mut Self) -> EvalResult<'tcx, T>
1336 assert!(self.memory_mut().reads_are_aligned, "Unaligned reads must not be nested");
1337 self.memory_mut().reads_are_aligned = aligned;
1339 self.memory_mut().reads_are_aligned = true;
1343 fn write_maybe_aligned<F, T>(&mut self, aligned: bool, f: F) -> EvalResult<'tcx, T>
1344 where F: FnOnce(&mut Self) -> EvalResult<'tcx, T>
1346 assert!(self.memory_mut().writes_are_aligned, "Unaligned writes must not be nested");
1347 self.memory_mut().writes_are_aligned = aligned;
1349 self.memory_mut().writes_are_aligned = true;
1354 impl<'a, 'tcx> HasMemory<'a, 'tcx> for Memory<'a, 'tcx> {
1356 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx> {
1361 fn memory(&self) -> &Memory<'a, 'tcx> {
1366 impl<'a, 'tcx> HasMemory<'a, 'tcx> for EvalContext<'a, 'tcx> {
1368 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx> {
1373 fn memory(&self) -> &Memory<'a, 'tcx> {
1378 ////////////////////////////////////////////////////////////////////////////////
1379 // Pointer arithmetic
1380 ////////////////////////////////////////////////////////////////////////////////
1382 pub trait PointerArithmetic : layout::HasDataLayout {
1383 // These are not supposed to be overriden.
1385 //// Trunace the given value to the pointer size; also return whether there was an overflow
1386 fn truncate_to_ptr(self, val: u128) -> (u64, bool) {
1387 let max_ptr_plus_1 = 1u128 << self.data_layout().pointer_size.bits();
1388 ((val % max_ptr_plus_1) as u64, val >= max_ptr_plus_1)
1391 // Overflow checking only works properly on the range from -u64 to +u64.
1392 fn overflowing_signed_offset(self, val: u64, i: i128) -> (u64, bool) {
1393 // FIXME: is it possible to over/underflow here?
1395 // trickery to ensure that i64::min_value() works fine
1396 // this formula only works for true negative values, it panics for zero!
1397 let n = u64::max_value() - (i as u64) + 1;
1398 val.overflowing_sub(n)
1400 self.overflowing_offset(val, i as u64)
1404 fn overflowing_offset(self, val: u64, i: u64) -> (u64, bool) {
1405 let (res, over1) = val.overflowing_add(i);
1406 let (res, over2) = self.truncate_to_ptr(res as u128);
1407 (res, over1 || over2)
1410 fn signed_offset<'tcx>(self, val: u64, i: i64) -> EvalResult<'tcx, u64> {
1411 let (res, over) = self.overflowing_signed_offset(val, i as i128);
1413 Err(EvalError::OverflowingMath)
1419 fn offset<'tcx>(self, val: u64, i: u64) -> EvalResult<'tcx, u64> {
1420 let (res, over) = self.overflowing_offset(val, i);
1422 Err(EvalError::OverflowingMath)
1428 fn wrapping_signed_offset(self, val: u64, i: i64) -> u64 {
1429 self.overflowing_signed_offset(val, i as i128).0
1433 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
1435 impl<'a, 'tcx> layout::HasDataLayout for &'a Memory<'a, 'tcx> {
1437 fn data_layout(&self) -> &TargetDataLayout {
1441 impl<'a, 'tcx> layout::HasDataLayout for &'a EvalContext<'a, 'tcx> {
1443 fn data_layout(&self) -> &TargetDataLayout {
1444 self.memory().layout
1448 impl<'c, 'b, 'a, 'tcx> layout::HasDataLayout for &'c &'b mut EvalContext<'a, 'tcx> {
1450 fn data_layout(&self) -> &TargetDataLayout {
1451 self.memory().layout