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 offset(&self) -> u64 {
46 pub fn len(&self) -> u64 {
50 pub fn range(offset: u64, len: u64) -> ops::Range<MemoryRange> {
52 // We select all elements that are within
53 // the range given by the offset into the allocation and the length.
54 // This is sound if "self.contains() || self.overlaps() == true" implies that self is in-range.
55 let left = MemoryRange {
59 let right = MemoryRange {
60 start: offset + len + 1,
66 pub fn contains(&self, offset: u64, len: u64) -> bool {
68 self.start <= offset && (offset + len) <= self.end
71 pub fn overlaps(&self, offset: u64, len: u64) -> bool {
73 //let non_overlap = (offset + len) <= self.start || self.end <= offset;
74 (offset + len) > self.start && self.end > offset
80 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
86 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
87 struct DynamicLifetime {
89 pub region: Option<CodeExtent>, // "None" indicates "until the function ends"
92 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
95 RecoverAfter(CodeExtent), // the frame is given by the surrounding LockInfo's lifetime.
98 /// Information about a lock that is or will be held.
99 #[derive(Copy, Clone, Debug)]
100 pub struct LockInfo {
102 lifetime: DynamicLifetime,
107 fn access_permitted(&self, frame: usize, access: AccessKind) -> bool {
108 use self::AccessKind::*;
109 match (self.kind, access) {
110 (Read, Read) => true, // Read access to read-locked region is okay, no matter who's holding the read lock.
111 (Write, _) if self.lifetime.frame == frame => true, // All access is okay when we hold the write lock.
112 _ => false, // Somebody else holding the write lock is not okay
117 ////////////////////////////////////////////////////////////////////////////////
118 // Allocations and pointers
119 ////////////////////////////////////////////////////////////////////////////////
121 #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
122 pub struct AllocId(pub u64);
124 impl fmt::Display for AllocId {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "{}", self.0)
131 pub struct Allocation {
132 /// The actual bytes of the allocation.
133 /// Note that the bytes of a pointer represent the offset of the pointer
135 /// Maps from byte addresses to allocations.
136 /// Only the first byte of a pointer is inserted into the map.
137 pub relocations: BTreeMap<u64, AllocId>,
138 /// Denotes undefined memory. Reading from undefined memory is forbidden in miri
139 pub undef_mask: UndefMask,
140 /// The alignment of the allocation to detect unaligned reads.
142 /// Whether the allocation may be modified.
143 pub mutable: Mutability,
144 /// Use the `mark_static_initalized` method of `Memory` to ensure that an error occurs, if the memory of this
145 /// allocation is modified or deallocated in the future.
146 /// Helps guarantee that stack allocations aren't deallocated via `rust_deallocate`
148 /// Memory regions that are locked by some function
149 locks: BTreeMap<MemoryRange, Vec<LockInfo>>,
153 fn iter_locks<'a>(&'a self, offset: u64, len: u64) -> impl Iterator<Item=&'a LockInfo> + 'a {
154 self.locks.range(MemoryRange::range(offset, len))
155 .filter(move |&(range, _)| range.overlaps(offset, len))
156 .flat_map(|(_, locks)| locks.iter())
159 fn iter_lock_vecs_mut<'a>(&'a mut self, offset: u64, len: u64) -> impl Iterator<Item=(&'a MemoryRange, &'a mut Vec<LockInfo>)> + 'a {
160 self.locks.range_mut(MemoryRange::range(offset, len))
161 .filter(move |&(range, _)| range.overlaps(offset, len))
164 fn check_locks<'tcx>(&self, frame: usize, offset: u64, len: u64, access: AccessKind) -> Result<(), LockInfo> {
168 for lock in self.iter_locks(offset, len) {
169 // Check if the lock is active, and is in conflict with the access.
170 if lock.status == LockStatus::Held && !lock.access_permitted(frame, access) {
178 #[derive(Debug, PartialEq, Copy, Clone)]
180 /// Error if deallocated any other way than `rust_deallocate`
182 /// Error if deallocated any other way than `free`
184 /// Error if deallocated except during a stack pop
186 /// Static in the process of being initialized.
187 /// The difference is important: An immutable static referring to a
188 /// mutable initialized static will freeze immutably and would not
189 /// be able to distinguish already initialized statics from uninitialized ones
191 /// May never be deallocated
193 /// Part of env var emulation
197 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
198 pub struct MemoryPointer {
199 pub alloc_id: AllocId,
203 impl<'tcx> MemoryPointer {
204 pub fn new(alloc_id: AllocId, offset: u64) -> Self {
205 MemoryPointer { alloc_id, offset }
208 pub(crate) fn wrapping_signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> Self {
209 MemoryPointer::new(self.alloc_id, cx.data_layout().wrapping_signed_offset(self.offset, i))
212 pub(crate) fn overflowing_signed_offset<C: HasDataLayout>(self, i: i128, cx: C) -> (Self, bool) {
213 let (res, over) = cx.data_layout().overflowing_signed_offset(self.offset, i);
214 (MemoryPointer::new(self.alloc_id, res), over)
217 pub(crate) fn signed_offset<C: HasDataLayout>(self, i: i64, cx: C) -> EvalResult<'tcx, Self> {
218 Ok(MemoryPointer::new(self.alloc_id, cx.data_layout().signed_offset(self.offset, i)?))
221 pub(crate) fn overflowing_offset<C: HasDataLayout>(self, i: u64, cx: C) -> (Self, bool) {
222 let (res, over) = cx.data_layout().overflowing_offset(self.offset, i);
223 (MemoryPointer::new(self.alloc_id, res), over)
226 pub(crate) fn offset<C: HasDataLayout>(self, i: u64, cx: C) -> EvalResult<'tcx, Self> {
227 Ok(MemoryPointer::new(self.alloc_id, cx.data_layout().offset(self.offset, i)?))
231 ////////////////////////////////////////////////////////////////////////////////
232 // Top-level interpreter memory
233 ////////////////////////////////////////////////////////////////////////////////
235 pub type TlsKey = usize;
237 #[derive(Copy, Clone, Debug)]
238 pub struct TlsEntry<'tcx> {
239 data: Pointer, // Will eventually become a map from thread IDs to `Pointer`s, if we ever support more than one thread.
240 dtor: Option<ty::Instance<'tcx>>,
243 pub struct Memory<'a, 'tcx> {
244 /// Actual memory allocations (arbitrary bytes, may contain pointers into other allocations).
245 alloc_map: HashMap<AllocId, Allocation>,
247 /// The AllocId to assign to the next new allocation. Always incremented, never gets smaller.
250 /// Set of statics, constants, promoteds, vtables, ... to prevent `mark_static_initalized` from
251 /// stepping out of its own allocations. This set only contains statics backed by an
252 /// allocation. If they are ByVal or ByValPair they are not here, but will be inserted once
253 /// they become ByRef.
254 static_alloc: HashSet<AllocId>,
256 /// Number of virtual bytes allocated.
259 /// Maximum number of virtual bytes that may be allocated.
262 /// Function "allocations". They exist solely so pointers have something to point to, and
263 /// we can figure out what they point to.
264 functions: HashMap<AllocId, ty::Instance<'tcx>>,
266 /// Inverse map of `functions` so we don't allocate a new pointer every time we need one
267 function_alloc_cache: HashMap<ty::Instance<'tcx>, AllocId>,
269 /// Target machine data layout to emulate.
270 pub layout: &'a TargetDataLayout,
272 /// A cache for basic byte allocations keyed by their contents. This is used to deduplicate
273 /// allocations for string and bytestring literals.
274 literal_alloc_cache: HashMap<Vec<u8>, AllocId>,
276 /// pthreads-style thread-local storage.
277 thread_local: BTreeMap<TlsKey, TlsEntry<'tcx>>,
279 /// The Key to use for the next thread-local allocation.
280 next_thread_local: TlsKey,
282 /// To avoid having to pass flags to every single memory access, we have some global state saying whether
283 /// alignment checking is currently enforced for read and/or write accesses.
284 reads_are_aligned: bool,
285 writes_are_aligned: bool,
287 /// The current stack frame. Used to check accesses against locks.
291 impl<'a, 'tcx> Memory<'a, 'tcx> {
292 pub fn new(layout: &'a TargetDataLayout, max_memory: u64) -> Self {
294 alloc_map: HashMap::new(),
295 functions: HashMap::new(),
296 function_alloc_cache: HashMap::new(),
299 memory_size: max_memory,
301 static_alloc: HashSet::new(),
302 literal_alloc_cache: HashMap::new(),
303 thread_local: BTreeMap::new(),
304 next_thread_local: 0,
305 reads_are_aligned: true,
306 writes_are_aligned: true,
307 cur_frame: usize::max_value(),
311 pub fn allocations(&self) -> ::std::collections::hash_map::Iter<AllocId, Allocation> {
312 self.alloc_map.iter()
315 pub fn create_fn_alloc(&mut self, instance: ty::Instance<'tcx>) -> MemoryPointer {
316 if let Some(&alloc_id) = self.function_alloc_cache.get(&instance) {
317 return MemoryPointer::new(alloc_id, 0);
319 let id = self.next_id;
320 debug!("creating fn ptr: {}", id);
322 self.functions.insert(id, instance);
323 self.function_alloc_cache.insert(instance, id);
324 MemoryPointer::new(id, 0)
327 pub fn allocate_cached(&mut self, bytes: &[u8]) -> EvalResult<'tcx, MemoryPointer> {
328 if let Some(&alloc_id) = self.literal_alloc_cache.get(bytes) {
329 return Ok(MemoryPointer::new(alloc_id, 0));
332 let ptr = self.allocate(bytes.len() as u64, 1, Kind::UninitializedStatic)?;
333 self.write_bytes(ptr.into(), bytes)?;
334 self.mark_static_initalized(ptr.alloc_id, Mutability::Immutable)?;
335 self.literal_alloc_cache.insert(bytes.to_vec(), ptr.alloc_id);
339 pub fn allocate(&mut self, size: u64, align: u64, kind: Kind) -> EvalResult<'tcx, MemoryPointer> {
340 assert_ne!(align, 0);
341 assert!(align.is_power_of_two());
343 if self.memory_size - self.memory_usage < size {
344 return Err(EvalError::OutOfMemory {
345 allocation_size: size,
346 memory_size: self.memory_size,
347 memory_usage: self.memory_usage,
350 self.memory_usage += size;
351 assert_eq!(size as usize as u64, size);
352 let alloc = Allocation {
353 bytes: vec![0; size as usize],
354 relocations: BTreeMap::new(),
355 undef_mask: UndefMask::new(size),
358 mutable: Mutability::Mutable,
359 locks: BTreeMap::new(),
361 let id = self.next_id;
363 self.alloc_map.insert(id, alloc);
364 Ok(MemoryPointer::new(id, 0))
367 pub fn reallocate(&mut self, ptr: MemoryPointer, old_size: u64, old_align: u64, new_size: u64, new_align: u64, kind: Kind) -> EvalResult<'tcx, MemoryPointer> {
371 return Err(EvalError::ReallocateNonBasePtr);
373 if let Ok(alloc) = self.get(ptr.alloc_id) {
374 if alloc.kind != kind {
375 return Err(EvalError::ReallocatedWrongMemoryKind(alloc.kind, kind));
379 // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc"
380 let new_ptr = self.allocate(new_size, new_align, kind)?;
381 self.copy(ptr.into(), new_ptr.into(), min(old_size, new_size), min(old_align, new_align), /*nonoverlapping*/true)?;
382 self.deallocate(ptr, Some((old_size, old_align)), kind)?;
387 pub fn deallocate(&mut self, ptr: MemoryPointer, size_and_align: Option<(u64, u64)>, kind: Kind) -> EvalResult<'tcx> {
389 return Err(EvalError::DeallocateNonBasePtr);
392 let alloc = match self.alloc_map.remove(&ptr.alloc_id) {
393 Some(alloc) => alloc,
394 None => return Err(EvalError::DoubleFree),
397 // It is okay for us to still holds locks on deallocation -- for example, we could store data we own
398 // in a local, and the local could be deallocated (from StorageDead) before the function returns.
399 // However, we should check *something*. For now, we make sure that there is no conflicting write
400 // lock by another frame. We *have* to permit deallocation if we hold a read lock.
401 // TODO: Figure out the exact rules here.
402 alloc.check_locks(self.cur_frame, 0, alloc.bytes.len() as u64, AccessKind::Read)
403 .map_err(|lock| EvalError::DeallocatedLockedMemory { ptr, lock })?;
405 if alloc.kind != kind {
406 return Err(EvalError::DeallocatedWrongMemoryKind(alloc.kind, kind));
408 if let Some((size, align)) = size_and_align {
409 if size != alloc.bytes.len() as u64 || align != alloc.align {
410 return Err(EvalError::IncorrectAllocationInformation);
414 self.memory_usage -= alloc.bytes.len() as u64;
415 debug!("deallocated : {}", ptr.alloc_id);
420 pub fn pointer_size(&self) -> u64 {
421 self.layout.pointer_size.bytes()
424 pub fn endianess(&self) -> layout::Endian {
428 /// Check that the pointer is aligned AND non-NULL.
429 pub fn check_align(&self, ptr: Pointer, align: u64) -> EvalResult<'tcx> {
430 let offset = match ptr.into_inner_primval() {
431 PrimVal::Ptr(ptr) => {
432 let alloc = self.get(ptr.alloc_id)?;
433 if alloc.align < align {
434 return Err(EvalError::AlignmentCheckFailed {
441 PrimVal::Bytes(bytes) => {
442 let v = ((bytes as u128) % (1 << self.pointer_size())) as u64;
444 return Err(EvalError::InvalidNullPointerUsage);
448 PrimVal::Undef => return Err(EvalError::ReadUndefBytes),
450 if offset % align == 0 {
453 Err(EvalError::AlignmentCheckFailed {
460 pub(crate) fn check_bounds(&self, ptr: MemoryPointer, access: bool) -> EvalResult<'tcx> {
461 let alloc = self.get(ptr.alloc_id)?;
462 let allocation_size = alloc.bytes.len() as u64;
463 if ptr.offset > allocation_size {
464 return Err(EvalError::PointerOutOfBounds { ptr, access, allocation_size });
469 pub(crate) fn set_cur_frame(&mut self, cur_frame: usize) {
470 self.cur_frame = cur_frame;
473 pub(crate) fn create_tls_key(&mut self, dtor: Option<ty::Instance<'tcx>>) -> TlsKey {
474 let new_key = self.next_thread_local;
475 self.next_thread_local += 1;
476 self.thread_local.insert(new_key, TlsEntry { data: Pointer::null(), dtor });
477 trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
481 pub(crate) fn delete_tls_key(&mut self, key: TlsKey) -> EvalResult<'tcx> {
482 return match self.thread_local.remove(&key) {
484 trace!("TLS key {} removed", key);
487 None => Err(EvalError::TlsOutOfBounds)
491 pub(crate) fn load_tls(&mut self, key: TlsKey) -> EvalResult<'tcx, Pointer> {
492 return match self.thread_local.get(&key) {
493 Some(&TlsEntry { data, .. }) => {
494 trace!("TLS key {} loaded: {:?}", key, data);
497 None => Err(EvalError::TlsOutOfBounds)
501 pub(crate) fn store_tls(&mut self, key: TlsKey, new_data: Pointer) -> EvalResult<'tcx> {
502 return match self.thread_local.get_mut(&key) {
503 Some(&mut TlsEntry { ref mut data, .. }) => {
504 trace!("TLS key {} stored: {:?}", key, new_data);
508 None => Err(EvalError::TlsOutOfBounds)
512 /// Returns a dtor, its argument and its index, if one is supposed to run
514 /// An optional destructor function may be associated with each key value.
515 /// At thread exit, if a key value has a non-NULL destructor pointer,
516 /// and the thread has a non-NULL value associated with that key,
517 /// the value of the key is set to NULL, and then the function pointed
518 /// to is called with the previously associated value as its sole argument.
519 /// The order of destructor calls is unspecified if more than one destructor
520 /// exists for a thread when it exits.
522 /// If, after all the destructors have been called for all non-NULL values
523 /// with associated destructors, there are still some non-NULL values with
524 /// associated destructors, then the process is repeated.
525 /// If, after at least {PTHREAD_DESTRUCTOR_ITERATIONS} iterations of destructor
526 /// calls for outstanding non-NULL values, there are still some non-NULL values
527 /// with associated destructors, implementations may stop calling destructors,
528 /// or they may continue calling destructors until no non-NULL values with
529 /// associated destructors exist, even though this might result in an infinite loop.
530 pub(crate) fn fetch_tls_dtor(&mut self, key: Option<TlsKey>) -> EvalResult<'tcx, Option<(ty::Instance<'tcx>, Pointer, TlsKey)>> {
531 use std::collections::Bound::*;
532 let start = match key {
533 Some(key) => Excluded(key),
536 for (&key, &mut TlsEntry { ref mut data, dtor }) in self.thread_local.range_mut((start, Unbounded)) {
537 if !data.is_null()? {
538 if let Some(dtor) = dtor {
539 let ret = Some((dtor, *data, key));
540 *data = Pointer::null();
550 impl<'a, 'tcx> Memory<'a, 'tcx> {
551 pub(crate) fn check_locks(&self, ptr: MemoryPointer, len: u64, access: AccessKind) -> EvalResult<'tcx> {
555 let alloc = self.get(ptr.alloc_id)?;
556 alloc.check_locks(self.cur_frame, ptr.offset, len, access)
557 .map_err(|lock| EvalError::MemoryLockViolation { ptr, len, access, lock })
560 /// Acquire the lock for the given lifetime
561 pub(crate) fn acquire_lock(&mut self, ptr: MemoryPointer, len: u64, region: Option<CodeExtent>, kind: AccessKind) -> EvalResult<'tcx> {
563 trace!("Acquiring {:?} lock at {:?}, size {} for region {:?}", kind, ptr, len, region);
564 self.check_bounds(ptr.offset(len, self.layout)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
565 self.check_locks(ptr, len, kind)?; // make sure we have the access we are acquiring
566 let lifetime = DynamicLifetime { frame: self.cur_frame, region };
567 let alloc = self.get_mut_unchecked(ptr.alloc_id)?;
568 alloc.locks.entry(MemoryRange::new(ptr.offset, len)).or_insert_with(|| Vec::new()).push(LockInfo { lifetime, kind, status: LockStatus::Held });
572 /// Release a write lock prematurely. If there's just read locks, do nothing.
573 pub(crate) fn release_write_lock_until(&mut self, ptr: MemoryPointer, len: u64, release_until: Option<CodeExtent>) -> EvalResult<'tcx> {
575 let cur_frame = self.cur_frame;
576 let alloc = self.get_mut_unchecked(ptr.alloc_id)?;
578 for (range, locks) in alloc.iter_lock_vecs_mut(ptr.offset, len) {
579 if !range.contains(ptr.offset, len) {
580 return Err(EvalError::Unimplemented(format!("miri does not support release part of a write-locked region")));
583 // Check all locks in this region; make sure there are no conflicting write locks of other frames.
584 // Also, if we will recover later, perform our release by changing the lock status.
585 for lock in locks.iter_mut() {
586 if lock.kind == AccessKind::Read || lock.status != LockStatus::Held { continue; }
587 if lock.lifetime.frame != cur_frame {
588 return Err(EvalError::InvalidMemoryLockRelease { ptr, len });
590 let ptr = MemoryPointer { alloc_id : ptr.alloc_id, offset: range.offset() };
591 trace!("Releasing write lock at {:?}, size {} until {:?}", ptr, range.len(), release_until);
592 if let Some(region) = release_until {
593 lock.status = LockStatus::RecoverAfter(region);
597 // If we will not recove, we did not do anything above except for some checks. Now, erase the locks from the list.
598 if let None = release_until {
599 // Delete everything that's a held write lock. We already checked above that these are ours.
600 // Unfortunately, this duplicates the condition from above. Is there anything we can do about this?
601 locks.retain(|lock| lock.kind == AccessKind::Read || lock.status != LockStatus::Held);
608 pub(crate) fn locks_lifetime_ended(&mut self, ending_region: Option<CodeExtent>) {
609 trace!("Releasing locks that expire at {:?}", ending_region);
610 let cur_frame = self.cur_frame;
611 let has_ended = |lock: &LockInfo| -> bool {
612 if lock.lifetime.frame != cur_frame {
615 match ending_region {
616 None => true, // When a function ends, we end *all* its locks. It's okay for a function to still have lifetime-related locks
617 // when it returns, that can happen e.g. with NLL when a lifetime can, but does not have to, extend beyond the
618 // end of a function.
619 Some(ending_region) => lock.lifetime.region == Some(ending_region),
623 for alloc in self.alloc_map.values_mut() {
624 for (_range, locks) in alloc.locks.iter_mut() {
625 // Delete everything that ends now -- i.e., keep only all the other lifetimes.
626 locks.retain(|lock| !has_ended(lock));
627 // Activate locks that get recovered now
628 if let Some(ending_region) = ending_region {
629 for lock in locks.iter_mut() {
630 if lock.lifetime.frame == cur_frame && lock.status == LockStatus::RecoverAfter(ending_region) {
631 // FIXME: Check if this triggers a conflict between active locks
632 lock.status = LockStatus::Held;
638 // TODO: It may happen now that we leave empty vectors in the map. Is it worth getting rid of them?
642 /// Allocation accessors
643 impl<'a, 'tcx> Memory<'a, 'tcx> {
644 pub fn get(&self, id: AllocId) -> EvalResult<'tcx, &Allocation> {
645 match self.alloc_map.get(&id) {
646 Some(alloc) => Ok(alloc),
647 None => match self.functions.get(&id) {
648 Some(_) => Err(EvalError::DerefFunctionPointer),
649 None => Err(EvalError::DanglingPointerDeref),
654 fn get_mut_unchecked(&mut self, id: AllocId) -> EvalResult<'tcx, &mut Allocation> {
655 match self.alloc_map.get_mut(&id) {
656 Some(alloc) => Ok(alloc),
657 None => match self.functions.get(&id) {
658 Some(_) => Err(EvalError::DerefFunctionPointer),
659 None => Err(EvalError::DanglingPointerDeref),
664 pub fn get_mut(&mut self, id: AllocId) -> EvalResult<'tcx, &mut Allocation> {
665 let alloc = self.get_mut_unchecked(id)?;
666 if alloc.mutable == Mutability::Mutable {
669 Err(EvalError::ModifiedConstantMemory)
673 pub fn get_fn(&self, ptr: MemoryPointer) -> EvalResult<'tcx, ty::Instance<'tcx>> {
675 return Err(EvalError::InvalidFunctionPointer);
677 debug!("reading fn ptr: {}", ptr.alloc_id);
678 match self.functions.get(&ptr.alloc_id) {
679 Some(&fndef) => Ok(fndef),
680 None => match self.alloc_map.get(&ptr.alloc_id) {
681 Some(_) => Err(EvalError::ExecuteMemory),
682 None => Err(EvalError::InvalidFunctionPointer),
687 /// For debugging, print an allocation and all allocations it points to, recursively.
688 pub fn dump_alloc(&self, id: AllocId) {
689 self.dump_allocs(vec![id]);
692 /// For debugging, print a list of allocations and all allocations they point to, recursively.
693 pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
697 let mut allocs_to_print = VecDeque::from(allocs);
698 let mut allocs_seen = HashSet::new();
700 while let Some(id) = allocs_to_print.pop_front() {
701 let mut msg = format!("Alloc {:<5} ", format!("{}:", id));
702 let prefix_len = msg.len();
703 let mut relocations = vec![];
705 let alloc = match (self.alloc_map.get(&id), self.functions.get(&id)) {
706 (Some(a), None) => a,
707 (None, Some(instance)) => {
708 trace!("{} {}", msg, instance);
712 trace!("{} (deallocated)", msg);
715 (Some(_), Some(_)) => bug!("miri invariant broken: an allocation id exists that points to both a function and a memory location"),
718 for i in 0..(alloc.bytes.len() as u64) {
719 if let Some(&target_id) = alloc.relocations.get(&i) {
720 if allocs_seen.insert(target_id) {
721 allocs_to_print.push_back(target_id);
723 relocations.push((i, target_id));
725 if alloc.undef_mask.is_range_defined(i, i + 1) {
726 // this `as usize` is fine, since `i` came from a `usize`
727 write!(msg, "{:02x} ", alloc.bytes[i as usize]).unwrap();
733 let immutable = match (alloc.kind, alloc.mutable) {
734 (Kind::UninitializedStatic, _) => " (static in the process of initialization)",
735 (Kind::Static, Mutability::Mutable) => " (static mut)",
736 (Kind::Static, Mutability::Immutable) => " (immutable)",
737 (Kind::Env, _) => " (env var)",
738 (Kind::C, _) => " (malloc)",
739 (Kind::Rust, _) => " (heap)",
740 (Kind::Stack, _) => " (stack)",
742 trace!("{}({} bytes, alignment {}){}", msg, alloc.bytes.len(), alloc.align, immutable);
744 if !relocations.is_empty() {
746 write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
748 let relocation_width = (self.pointer_size() - 1) * 3;
749 for (i, target_id) in relocations {
750 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
751 write!(msg, "{:1$}", "", ((i - pos) * 3) as usize).unwrap();
752 let target = format!("({})", target_id);
753 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
754 write!(msg, "└{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
755 pos = i + self.pointer_size();
762 pub fn leak_report(&self) -> usize {
763 trace!("### LEAK REPORT ###");
764 let leaks: Vec<_> = self.alloc_map
766 .filter_map(|(&key, val)| {
767 if val.kind != Kind::Static {
775 self.dump_allocs(leaks);
781 impl<'a, 'tcx> Memory<'a, 'tcx> {
782 fn get_bytes_unchecked(&self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &[u8]> {
783 // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
784 if self.reads_are_aligned {
785 self.check_align(ptr.into(), align)?;
790 self.check_locks(ptr, size, AccessKind::Read)?;
791 self.check_bounds(ptr.offset(size, self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
792 let alloc = self.get(ptr.alloc_id)?;
793 assert_eq!(ptr.offset as usize as u64, ptr.offset);
794 assert_eq!(size as usize as u64, size);
795 let offset = ptr.offset as usize;
796 Ok(&alloc.bytes[offset..offset + size as usize])
799 fn get_bytes_unchecked_mut(&mut self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &mut [u8]> {
800 // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL
801 if self.writes_are_aligned {
802 self.check_align(ptr.into(), align)?;
807 self.check_locks(ptr, size, AccessKind::Write)?;
808 self.check_bounds(ptr.offset(size, self.layout)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow)
809 let alloc = self.get_mut(ptr.alloc_id)?;
810 assert_eq!(ptr.offset as usize as u64, ptr.offset);
811 assert_eq!(size as usize as u64, size);
812 let offset = ptr.offset as usize;
813 Ok(&mut alloc.bytes[offset..offset + size as usize])
816 fn get_bytes(&self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &[u8]> {
818 if self.relocations(ptr, size)?.count() != 0 {
819 return Err(EvalError::ReadPointerAsBytes);
821 self.check_defined(ptr, size)?;
822 self.get_bytes_unchecked(ptr, size, align)
825 fn get_bytes_mut(&mut self, ptr: MemoryPointer, size: u64, align: u64) -> EvalResult<'tcx, &mut [u8]> {
827 self.clear_relocations(ptr, size)?;
828 self.mark_definedness(ptr.into(), size, true)?;
829 self.get_bytes_unchecked_mut(ptr, size, align)
833 /// Reading and writing
834 impl<'a, 'tcx> Memory<'a, 'tcx> {
835 /// mark an allocation as being the entry point to a static (see `static_alloc` field)
836 pub fn mark_static(&mut self, alloc_id: AllocId) {
837 trace!("mark_static: {:?}", alloc_id);
838 if !self.static_alloc.insert(alloc_id) {
839 bug!("tried to mark an allocation ({:?}) as static twice", alloc_id);
843 /// mark an allocation pointed to by a static as static and initialized
844 pub fn mark_inner_allocation(&mut self, alloc: AllocId, mutability: Mutability) -> EvalResult<'tcx> {
845 // relocations into other statics are not "inner allocations"
846 if !self.static_alloc.contains(&alloc) {
847 self.mark_static_initalized(alloc, mutability)?;
852 /// mark an allocation as static and initialized, either mutable or not
853 pub fn mark_static_initalized(&mut self, alloc_id: AllocId, mutability: Mutability) -> EvalResult<'tcx> {
854 trace!("mark_static_initalized {:?}, mutability: {:?}", alloc_id, mutability);
855 // do not use `self.get_mut(alloc_id)` here, because we might have already marked a
856 // sub-element or have circular pointers (e.g. `Rc`-cycles)
857 let relocations = match self.alloc_map.get_mut(&alloc_id) {
858 Some(&mut Allocation { ref mut relocations, ref mut kind, ref mut mutable, .. }) => {
860 // const eval results can refer to "locals".
861 // E.g. `const Foo: &u32 = &1;` refers to the temp local that stores the `1`
863 // The entire point of this function
864 Kind::UninitializedStatic |
865 // In the future const eval will allow heap allocations so we'll need to protect them
866 // from deallocation, too
870 trace!("mark_static_initalized: skipping already initialized static referred to by static currently being initialized");
873 // FIXME: This could be allowed, but not for env vars set during miri execution
874 Kind::Env => return Err(EvalError::Unimplemented("statics can't refer to env vars".to_owned())),
876 *kind = Kind::Static;
877 *mutable = mutability;
878 // take out the relocations vector to free the borrow on self, so we can call
880 mem::replace(relocations, Default::default())
882 None if !self.functions.contains_key(&alloc_id) => return Err(EvalError::DanglingPointerDeref),
885 // recurse into inner allocations
886 for &alloc in relocations.values() {
887 self.mark_inner_allocation(alloc, mutability)?;
889 // put back the relocations
890 self.alloc_map.get_mut(&alloc_id).expect("checked above").relocations = relocations;
894 pub fn copy(&mut self, src: Pointer, dest: Pointer, size: u64, align: u64, nonoverlapping: bool) -> EvalResult<'tcx> {
896 // Empty accesses don't need to be valid pointers, but they should still be aligned
897 if self.reads_are_aligned {
898 self.check_align(src, align)?;
900 if self.writes_are_aligned {
901 self.check_align(dest, align)?;
905 let src = src.to_ptr()?;
906 let dest = dest.to_ptr()?;
907 self.check_relocation_edges(src, size)?;
909 let src_bytes = self.get_bytes_unchecked(src, size, align)?.as_ptr();
910 let dest_bytes = self.get_bytes_mut(dest, size, align)?.as_mut_ptr();
912 // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
913 // behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
914 // `dest` could possibly overlap.
916 assert_eq!(size as usize as u64, size);
917 if src.alloc_id == dest.alloc_id {
919 if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
920 (dest.offset <= src.offset && dest.offset + size > src.offset) {
921 return Err(EvalError::Intrinsic(format!("copy_nonoverlapping called on overlapping ranges")));
924 ptr::copy(src_bytes, dest_bytes, size as usize);
926 ptr::copy_nonoverlapping(src_bytes, dest_bytes, size as usize);
930 self.copy_undef_mask(src, dest, size)?;
931 self.copy_relocations(src, dest, size)?;
936 pub fn read_c_str(&self, ptr: MemoryPointer) -> EvalResult<'tcx, &[u8]> {
937 let alloc = self.get(ptr.alloc_id)?;
938 assert_eq!(ptr.offset as usize as u64, ptr.offset);
939 let offset = ptr.offset as usize;
940 match alloc.bytes[offset..].iter().position(|&c| c == 0) {
942 if self.relocations(ptr, (size + 1) as u64)?.count() != 0 {
943 return Err(EvalError::ReadPointerAsBytes);
945 self.check_defined(ptr, (size + 1) as u64)?;
946 self.check_locks(ptr, (size + 1) as u64, AccessKind::Read)?;
947 Ok(&alloc.bytes[offset..offset + size])
949 None => Err(EvalError::UnterminatedCString(ptr)),
953 pub fn read_bytes(&self, ptr: Pointer, size: u64) -> EvalResult<'tcx, &[u8]> {
955 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
956 if self.reads_are_aligned {
957 self.check_align(ptr, 1)?;
961 self.get_bytes(ptr.to_ptr()?, size, 1)
964 pub fn write_bytes(&mut self, ptr: Pointer, src: &[u8]) -> EvalResult<'tcx> {
966 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
967 if self.writes_are_aligned {
968 self.check_align(ptr, 1)?;
972 let bytes = self.get_bytes_mut(ptr.to_ptr()?, src.len() as u64, 1)?;
973 bytes.clone_from_slice(src);
977 pub fn write_repeat(&mut self, ptr: Pointer, val: u8, count: u64) -> EvalResult<'tcx> {
979 // Empty accesses don't need to be valid pointers, but they should still be non-NULL
980 if self.writes_are_aligned {
981 self.check_align(ptr, 1)?;
985 let bytes = self.get_bytes_mut(ptr.to_ptr()?, count, 1)?;
986 for b in bytes { *b = val; }
990 pub fn read_ptr(&self, ptr: MemoryPointer) -> EvalResult<'tcx, Pointer> {
991 let size = self.pointer_size();
992 self.check_relocation_edges(ptr, size)?; // Make sure we don't read part of a pointer as a pointer
993 let endianess = self.endianess();
994 let bytes = self.get_bytes_unchecked(ptr, size, size)?;
995 // Undef check happens *after* we established that the alignment is correct.
996 // We must not return Ok() for unaligned pointers!
997 if self.check_defined(ptr, size).is_err() {
998 return Ok(PrimVal::Undef.into());
1000 let offset = read_target_uint(endianess, bytes).unwrap();
1001 assert_eq!(offset as u64 as u128, offset);
1002 let offset = offset as u64;
1003 let alloc = self.get(ptr.alloc_id)?;
1004 match alloc.relocations.get(&ptr.offset) {
1005 Some(&alloc_id) => Ok(PrimVal::Ptr(MemoryPointer::new(alloc_id, offset)).into()),
1006 None => Ok(PrimVal::Bytes(offset as u128).into()),
1010 pub fn write_ptr(&mut self, dest: MemoryPointer, ptr: MemoryPointer) -> EvalResult<'tcx> {
1011 self.write_usize(dest, ptr.offset as u64)?;
1012 self.get_mut(dest.alloc_id)?.relocations.insert(dest.offset, ptr.alloc_id);
1016 pub fn write_primval(
1021 ) -> EvalResult<'tcx> {
1023 PrimVal::Ptr(ptr) => {
1024 assert_eq!(size, self.pointer_size());
1025 self.write_ptr(dest.to_ptr()?, ptr)
1028 PrimVal::Bytes(bytes) => {
1029 // We need to mask here, or the byteorder crate can die when given a u64 larger
1030 // than fits in an integer of the requested size.
1031 let mask = match size {
1037 n => bug!("unexpected PrimVal::Bytes size: {}", n),
1039 self.write_uint(dest.to_ptr()?, bytes & mask, size)
1042 PrimVal::Undef => self.mark_definedness(dest, size, false),
1046 pub fn read_bool(&self, ptr: MemoryPointer) -> EvalResult<'tcx, bool> {
1047 let bytes = self.get_bytes(ptr, 1, self.layout.i1_align.abi())?;
1051 _ => Err(EvalError::InvalidBool),
1055 pub fn write_bool(&mut self, ptr: MemoryPointer, b: bool) -> EvalResult<'tcx> {
1056 let align = self.layout.i1_align.abi();
1057 self.get_bytes_mut(ptr, 1, align)
1058 .map(|bytes| bytes[0] = b as u8)
1061 fn int_align(&self, size: u64) -> EvalResult<'tcx, u64> {
1063 1 => Ok(self.layout.i8_align.abi()),
1064 2 => Ok(self.layout.i16_align.abi()),
1065 4 => Ok(self.layout.i32_align.abi()),
1066 8 => Ok(self.layout.i64_align.abi()),
1067 16 => Ok(self.layout.i128_align.abi()),
1068 _ => bug!("bad integer size: {}", size),
1072 pub fn read_int(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx, i128> {
1073 let align = self.int_align(size)?;
1074 self.get_bytes(ptr, size, align).map(|b| read_target_int(self.endianess(), b).unwrap())
1077 pub fn write_int(&mut self, ptr: MemoryPointer, n: i128, size: u64) -> EvalResult<'tcx> {
1078 let align = self.int_align(size)?;
1079 let endianess = self.endianess();
1080 let b = self.get_bytes_mut(ptr, size, align)?;
1081 write_target_int(endianess, b, n).unwrap();
1085 pub fn read_uint(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx, u128> {
1086 let align = self.int_align(size)?;
1087 self.get_bytes(ptr, size, align).map(|b| read_target_uint(self.endianess(), b).unwrap())
1090 pub fn write_uint(&mut self, ptr: MemoryPointer, n: u128, size: u64) -> EvalResult<'tcx> {
1091 let align = self.int_align(size)?;
1092 let endianess = self.endianess();
1093 let b = self.get_bytes_mut(ptr, size, align)?;
1094 write_target_uint(endianess, b, n).unwrap();
1098 pub fn read_isize(&self, ptr: MemoryPointer) -> EvalResult<'tcx, i64> {
1099 self.read_int(ptr, self.pointer_size()).map(|i| i as i64)
1102 pub fn write_isize(&mut self, ptr: MemoryPointer, n: i64) -> EvalResult<'tcx> {
1103 let size = self.pointer_size();
1104 self.write_int(ptr, n as i128, size)
1107 pub fn read_usize(&self, ptr: MemoryPointer) -> EvalResult<'tcx, u64> {
1108 self.read_uint(ptr, self.pointer_size()).map(|i| i as u64)
1111 pub fn write_usize(&mut self, ptr: MemoryPointer, n: u64) -> EvalResult<'tcx> {
1112 let size = self.pointer_size();
1113 self.write_uint(ptr, n as u128, size)
1116 pub fn write_f32(&mut self, ptr: MemoryPointer, f: f32) -> EvalResult<'tcx> {
1117 let endianess = self.endianess();
1118 let align = self.layout.f32_align.abi();
1119 let b = self.get_bytes_mut(ptr, 4, align)?;
1120 write_target_f32(endianess, b, f).unwrap();
1124 pub fn write_f64(&mut self, ptr: MemoryPointer, f: f64) -> EvalResult<'tcx> {
1125 let endianess = self.endianess();
1126 let align = self.layout.f64_align.abi();
1127 let b = self.get_bytes_mut(ptr, 8, align)?;
1128 write_target_f64(endianess, b, f).unwrap();
1132 pub fn read_f32(&self, ptr: MemoryPointer) -> EvalResult<'tcx, f32> {
1133 self.get_bytes(ptr, 4, self.layout.f32_align.abi())
1134 .map(|b| read_target_f32(self.endianess(), b).unwrap())
1137 pub fn read_f64(&self, ptr: MemoryPointer) -> EvalResult<'tcx, f64> {
1138 self.get_bytes(ptr, 8, self.layout.f64_align.abi())
1139 .map(|b| read_target_f64(self.endianess(), b).unwrap())
1144 impl<'a, 'tcx> Memory<'a, 'tcx> {
1145 fn relocations(&self, ptr: MemoryPointer, size: u64)
1146 -> EvalResult<'tcx, btree_map::Range<u64, AllocId>>
1148 let start = ptr.offset.saturating_sub(self.pointer_size() - 1);
1149 let end = ptr.offset + size;
1150 Ok(self.get(ptr.alloc_id)?.relocations.range(start..end))
1153 fn clear_relocations(&mut self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1154 // Find all relocations overlapping the given range.
1155 let keys: Vec<_> = self.relocations(ptr, size)?.map(|(&k, _)| k).collect();
1156 if keys.is_empty() { return Ok(()); }
1158 // Find the start and end of the given range and its outermost relocations.
1159 let start = ptr.offset;
1160 let end = start + size;
1161 let first = *keys.first().unwrap();
1162 let last = *keys.last().unwrap() + self.pointer_size();
1164 let alloc = self.get_mut(ptr.alloc_id)?;
1166 // Mark parts of the outermost relocations as undefined if they partially fall outside the
1168 if first < start { alloc.undef_mask.set_range(first, start, false); }
1169 if last > end { alloc.undef_mask.set_range(end, last, false); }
1171 // Forget all the relocations.
1172 for k in keys { alloc.relocations.remove(&k); }
1177 fn check_relocation_edges(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1178 let overlapping_start = self.relocations(ptr, 0)?.count();
1179 let overlapping_end = self.relocations(ptr.offset(size, self.layout)?, 0)?.count();
1180 if overlapping_start + overlapping_end != 0 {
1181 return Err(EvalError::ReadPointerAsBytes);
1186 fn copy_relocations(&mut self, src: MemoryPointer, dest: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1187 let relocations: Vec<_> = self.relocations(src, size)?
1188 .map(|(&offset, &alloc_id)| {
1189 // Update relocation offsets for the new positions in the destination allocation.
1190 (offset + dest.offset - src.offset, alloc_id)
1193 self.get_mut(dest.alloc_id)?.relocations.extend(relocations);
1199 impl<'a, 'tcx> Memory<'a, 'tcx> {
1200 // FIXME(solson): This is a very naive, slow version.
1201 fn copy_undef_mask(&mut self, src: MemoryPointer, dest: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1202 // The bits have to be saved locally before writing to dest in case src and dest overlap.
1203 assert_eq!(size as usize as u64, size);
1204 let mut v = Vec::with_capacity(size as usize);
1206 let defined = self.get(src.alloc_id)?.undef_mask.get(src.offset + i);
1209 for (i, defined) in v.into_iter().enumerate() {
1210 self.get_mut(dest.alloc_id)?.undef_mask.set(dest.offset + i as u64, defined);
1215 fn check_defined(&self, ptr: MemoryPointer, size: u64) -> EvalResult<'tcx> {
1216 let alloc = self.get(ptr.alloc_id)?;
1217 if !alloc.undef_mask.is_range_defined(ptr.offset, ptr.offset + size) {
1218 return Err(EvalError::ReadUndefBytes);
1223 pub fn mark_definedness(
1228 ) -> EvalResult<'tcx> {
1232 let ptr = ptr.to_ptr()?;
1233 let mut alloc = self.get_mut(ptr.alloc_id)?;
1234 alloc.undef_mask.set_range(ptr.offset, ptr.offset + size, new_state);
1239 ////////////////////////////////////////////////////////////////////////////////
1240 // Methods to access integers in the target endianess
1241 ////////////////////////////////////////////////////////////////////////////////
1243 fn write_target_uint(endianess: layout::Endian, mut target: &mut [u8], data: u128) -> Result<(), io::Error> {
1244 let len = target.len();
1246 layout::Endian::Little => target.write_uint128::<LittleEndian>(data, len),
1247 layout::Endian::Big => target.write_uint128::<BigEndian>(data, len),
1250 fn write_target_int(endianess: layout::Endian, mut target: &mut [u8], data: i128) -> Result<(), io::Error> {
1251 let len = target.len();
1253 layout::Endian::Little => target.write_int128::<LittleEndian>(data, len),
1254 layout::Endian::Big => target.write_int128::<BigEndian>(data, len),
1258 fn read_target_uint(endianess: layout::Endian, mut source: &[u8]) -> Result<u128, io::Error> {
1260 layout::Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
1261 layout::Endian::Big => source.read_uint128::<BigEndian>(source.len()),
1264 fn read_target_int(endianess: layout::Endian, mut source: &[u8]) -> Result<i128, io::Error> {
1266 layout::Endian::Little => source.read_int128::<LittleEndian>(source.len()),
1267 layout::Endian::Big => source.read_int128::<BigEndian>(source.len()),
1271 ////////////////////////////////////////////////////////////////////////////////
1272 // Methods to access floats in the target endianess
1273 ////////////////////////////////////////////////////////////////////////////////
1275 fn write_target_f32(endianess: layout::Endian, mut target: &mut [u8], data: f32) -> Result<(), io::Error> {
1277 layout::Endian::Little => target.write_f32::<LittleEndian>(data),
1278 layout::Endian::Big => target.write_f32::<BigEndian>(data),
1281 fn write_target_f64(endianess: layout::Endian, mut target: &mut [u8], data: f64) -> Result<(), io::Error> {
1283 layout::Endian::Little => target.write_f64::<LittleEndian>(data),
1284 layout::Endian::Big => target.write_f64::<BigEndian>(data),
1288 fn read_target_f32(endianess: layout::Endian, mut source: &[u8]) -> Result<f32, io::Error> {
1290 layout::Endian::Little => source.read_f32::<LittleEndian>(),
1291 layout::Endian::Big => source.read_f32::<BigEndian>(),
1294 fn read_target_f64(endianess: layout::Endian, mut source: &[u8]) -> Result<f64, io::Error> {
1296 layout::Endian::Little => source.read_f64::<LittleEndian>(),
1297 layout::Endian::Big => source.read_f64::<BigEndian>(),
1301 ////////////////////////////////////////////////////////////////////////////////
1302 // Undefined byte tracking
1303 ////////////////////////////////////////////////////////////////////////////////
1306 const BLOCK_SIZE: u64 = 64;
1308 #[derive(Clone, Debug)]
1309 pub struct UndefMask {
1315 fn new(size: u64) -> Self {
1316 let mut m = UndefMask {
1320 m.grow(size, false);
1324 /// Check whether the range `start..end` (end-exclusive) is entirely defined.
1325 pub fn is_range_defined(&self, start: u64, end: u64) -> bool {
1326 if end > self.len { return false; }
1327 for i in start..end {
1328 if !self.get(i) { return false; }
1333 fn set_range(&mut self, start: u64, end: u64, new_state: bool) {
1335 if end > len { self.grow(end - len, new_state); }
1336 self.set_range_inbounds(start, end, new_state);
1339 fn set_range_inbounds(&mut self, start: u64, end: u64, new_state: bool) {
1340 for i in start..end { self.set(i, new_state); }
1343 fn get(&self, i: u64) -> bool {
1344 let (block, bit) = bit_index(i);
1345 (self.blocks[block] & 1 << bit) != 0
1348 fn set(&mut self, i: u64, new_state: bool) {
1349 let (block, bit) = bit_index(i);
1351 self.blocks[block] |= 1 << bit;
1353 self.blocks[block] &= !(1 << bit);
1357 fn grow(&mut self, amount: u64, new_state: bool) {
1358 let unused_trailing_bits = self.blocks.len() as u64 * BLOCK_SIZE - self.len;
1359 if amount > unused_trailing_bits {
1360 let additional_blocks = amount / BLOCK_SIZE + 1;
1361 assert_eq!(additional_blocks as usize as u64, additional_blocks);
1362 self.blocks.extend(iter::repeat(0).take(additional_blocks as usize));
1364 let start = self.len;
1366 self.set_range_inbounds(start, start + amount, new_state);
1370 fn bit_index(bits: u64) -> (usize, usize) {
1371 let a = bits / BLOCK_SIZE;
1372 let b = bits % BLOCK_SIZE;
1373 assert_eq!(a as usize as u64, a);
1374 assert_eq!(b as usize as u64, b);
1375 (a as usize, b as usize)
1378 ////////////////////////////////////////////////////////////////////////////////
1379 // Unaligned accesses
1380 ////////////////////////////////////////////////////////////////////////////////
1382 pub(crate) trait HasMemory<'a, 'tcx> {
1383 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx>;
1384 fn memory(&self) -> &Memory<'a, 'tcx>;
1386 // These are not supposed to be overriden.
1387 fn read_maybe_aligned<F, T>(&mut self, aligned: bool, f: F) -> EvalResult<'tcx, T>
1388 where F: FnOnce(&mut Self) -> EvalResult<'tcx, T>
1390 assert!(self.memory_mut().reads_are_aligned, "Unaligned reads must not be nested");
1391 self.memory_mut().reads_are_aligned = aligned;
1393 self.memory_mut().reads_are_aligned = true;
1397 fn write_maybe_aligned<F, T>(&mut self, aligned: bool, f: F) -> EvalResult<'tcx, T>
1398 where F: FnOnce(&mut Self) -> EvalResult<'tcx, T>
1400 assert!(self.memory_mut().writes_are_aligned, "Unaligned writes must not be nested");
1401 self.memory_mut().writes_are_aligned = aligned;
1403 self.memory_mut().writes_are_aligned = true;
1408 impl<'a, 'tcx> HasMemory<'a, 'tcx> for Memory<'a, 'tcx> {
1410 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx> {
1415 fn memory(&self) -> &Memory<'a, 'tcx> {
1420 impl<'a, 'tcx> HasMemory<'a, 'tcx> for EvalContext<'a, 'tcx> {
1422 fn memory_mut(&mut self) -> &mut Memory<'a, 'tcx> {
1427 fn memory(&self) -> &Memory<'a, 'tcx> {
1432 ////////////////////////////////////////////////////////////////////////////////
1433 // Pointer arithmetic
1434 ////////////////////////////////////////////////////////////////////////////////
1436 pub trait PointerArithmetic : layout::HasDataLayout {
1437 // These are not supposed to be overriden.
1439 //// Trunace the given value to the pointer size; also return whether there was an overflow
1440 fn truncate_to_ptr(self, val: u128) -> (u64, bool) {
1441 let max_ptr_plus_1 = 1u128 << self.data_layout().pointer_size.bits();
1442 ((val % max_ptr_plus_1) as u64, val >= max_ptr_plus_1)
1445 // Overflow checking only works properly on the range from -u64 to +u64.
1446 fn overflowing_signed_offset(self, val: u64, i: i128) -> (u64, bool) {
1447 // FIXME: is it possible to over/underflow here?
1449 // trickery to ensure that i64::min_value() works fine
1450 // this formula only works for true negative values, it panics for zero!
1451 let n = u64::max_value() - (i as u64) + 1;
1452 val.overflowing_sub(n)
1454 self.overflowing_offset(val, i as u64)
1458 fn overflowing_offset(self, val: u64, i: u64) -> (u64, bool) {
1459 let (res, over1) = val.overflowing_add(i);
1460 let (res, over2) = self.truncate_to_ptr(res as u128);
1461 (res, over1 || over2)
1464 fn signed_offset<'tcx>(self, val: u64, i: i64) -> EvalResult<'tcx, u64> {
1465 let (res, over) = self.overflowing_signed_offset(val, i as i128);
1467 Err(EvalError::OverflowingMath)
1473 fn offset<'tcx>(self, val: u64, i: u64) -> EvalResult<'tcx, u64> {
1474 let (res, over) = self.overflowing_offset(val, i);
1476 Err(EvalError::OverflowingMath)
1482 fn wrapping_signed_offset(self, val: u64, i: i64) -> u64 {
1483 self.overflowing_signed_offset(val, i as i128).0
1487 impl<T: layout::HasDataLayout> PointerArithmetic for T {}
1489 impl<'a, 'tcx> layout::HasDataLayout for &'a Memory<'a, 'tcx> {
1491 fn data_layout(&self) -> &TargetDataLayout {
1495 impl<'a, 'tcx> layout::HasDataLayout for &'a EvalContext<'a, 'tcx> {
1497 fn data_layout(&self) -> &TargetDataLayout {
1498 self.memory().layout
1502 impl<'c, 'b, 'a, 'tcx> layout::HasDataLayout for &'c &'b mut EvalContext<'a, 'tcx> {
1504 fn data_layout(&self) -> &TargetDataLayout {
1505 self.memory().layout