]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/interpret/memory.rs
code review fixes
[rust.git] / src / librustc_mir / interpret / memory.rs
1 //! The memory subsystem.
2 //!
3 //! Generally, we use `Pointer` to denote memory addresses. However, some operations
4 //! have a "size"-like parameter, and they take `Scalar` for the address because
5 //! if the size is 0, then the pointer can also be a (properly aligned, non-NULL)
6 //! integer. It is crucial that these operations call `check_align` *before*
7 //! short-circuiting the empty case!
8
9 use std::collections::VecDeque;
10 use std::ptr;
11 use std::borrow::Cow;
12
13 use rustc::ty::{self, Instance, ParamEnv, query::TyCtxtAt};
14 use rustc::ty::layout::{Align, TargetDataLayout, Size, HasDataLayout};
15 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
16
17 use syntax::ast::Mutability;
18
19 use super::{
20     Pointer, AllocId, Allocation, GlobalId, AllocationExtra,
21     InterpResult, Scalar, InterpError, GlobalAlloc, PointerArithmetic,
22     Machine, AllocMap, MayLeak, ErrorHandled, CheckInAllocMsg, InvalidProgramInfo,
23 };
24
25 #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
26 pub enum MemoryKind<T> {
27     /// Error if deallocated except during a stack pop
28     Stack,
29     /// Error if ever deallocated
30     Vtable,
31     /// Additional memory kinds a machine wishes to distinguish from the builtin ones
32     Machine(T),
33 }
34
35 impl<T: MayLeak> MayLeak for MemoryKind<T> {
36     #[inline]
37     fn may_leak(self) -> bool {
38         match self {
39             MemoryKind::Stack => false,
40             MemoryKind::Vtable => true,
41             MemoryKind::Machine(k) => k.may_leak()
42         }
43     }
44 }
45
46 /// Used by `get_size_and_align` to indicate whether the allocation needs to be live.
47 #[derive(Debug, Copy, Clone)]
48 pub enum AllocCheck {
49     /// Allocation must be live and not a function pointer.
50     Dereferencable,
51     /// Allocations needs to be live, but may be a function pointer.
52     Live,
53     /// Allocation may be dead.
54     MaybeDead,
55 }
56
57 /// The value of a function pointer.
58 #[derive(Debug, Copy, Clone)]
59 pub enum FnVal<'tcx, Other> {
60     Instance(Instance<'tcx>),
61     Other(Other),
62 }
63
64 impl<'tcx, Other> FnVal<'tcx, Other> {
65     pub fn as_instance(self) -> InterpResult<'tcx, Instance<'tcx>> {
66         match self {
67             FnVal::Instance(instance) =>
68                 Ok(instance),
69             FnVal::Other(_) => err!(MachineError(format!(
70                 "Expected instance function pointer, got 'other' pointer"
71             ))),
72         }
73     }
74 }
75
76 // `Memory` has to depend on the `Machine` because some of its operations
77 // (e.g., `get`) call a `Machine` hook.
78 pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
79     /// Allocations local to this instance of the miri engine. The kind
80     /// helps ensure that the same mechanism is used for allocation and
81     /// deallocation. When an allocation is not found here, it is a
82     /// static and looked up in the `tcx` for read access. Some machines may
83     /// have to mutate this map even on a read-only access to a static (because
84     /// they do pointer provenance tracking and the allocations in `tcx` have
85     /// the wrong type), so we let the machine override this type.
86     /// Either way, if the machine allows writing to a static, doing so will
87     /// create a copy of the static allocation here.
88     // FIXME: this should not be public, but interning currently needs access to it
89     pub(super) alloc_map: M::MemoryMap,
90
91     /// Map for "extra" function pointers.
92     extra_fn_ptr_map: FxHashMap<AllocId, M::ExtraFnVal>,
93
94     /// To be able to compare pointers with NULL, and to check alignment for accesses
95     /// to ZSTs (where pointers may dangle), we keep track of the size even for allocations
96     /// that do not exist any more.
97     // FIXME: this should not be public, but interning currently needs access to it
98     pub(super) dead_alloc_map: FxHashMap<AllocId, (Size, Align)>,
99
100     /// Extra data added by the machine.
101     pub extra: M::MemoryExtra,
102
103     /// Lets us implement `HasDataLayout`, which is awfully convenient.
104     pub tcx: TyCtxtAt<'tcx>,
105 }
106
107 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for Memory<'mir, 'tcx, M> {
108     #[inline]
109     fn data_layout(&self) -> &TargetDataLayout {
110         &self.tcx.data_layout
111     }
112 }
113
114 // FIXME: Really we shouldn't clone memory, ever. Snapshot machinery should instead
115 // carefully copy only the reachable parts.
116 impl<'mir, 'tcx, M> Clone for Memory<'mir, 'tcx, M>
117 where
118     M: Machine<'mir, 'tcx, PointerTag = (), AllocExtra = (), MemoryExtra = ()>,
119     M::MemoryMap: AllocMap<AllocId, (MemoryKind<M::MemoryKinds>, Allocation)>,
120 {
121     fn clone(&self) -> Self {
122         Memory {
123             alloc_map: self.alloc_map.clone(),
124             extra_fn_ptr_map: self.extra_fn_ptr_map.clone(),
125             dead_alloc_map: self.dead_alloc_map.clone(),
126             extra: (),
127             tcx: self.tcx,
128         }
129     }
130 }
131
132 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
133     pub fn new(tcx: TyCtxtAt<'tcx>, extra: M::MemoryExtra) -> Self {
134         Memory {
135             alloc_map: M::MemoryMap::default(),
136             extra_fn_ptr_map: FxHashMap::default(),
137             dead_alloc_map: FxHashMap::default(),
138             extra,
139             tcx,
140         }
141     }
142
143     #[inline]
144     pub fn tag_static_base_pointer(&self, ptr: Pointer) -> Pointer<M::PointerTag> {
145         ptr.with_tag(M::tag_static_base_pointer(&self.extra, ptr.alloc_id))
146     }
147
148     pub fn create_fn_alloc(
149         &mut self,
150         fn_val: FnVal<'tcx, M::ExtraFnVal>,
151     ) -> Pointer<M::PointerTag>
152     {
153         let id = match fn_val {
154             FnVal::Instance(instance) => self.tcx.alloc_map.lock().create_fn_alloc(instance),
155             FnVal::Other(extra) => {
156                 // FIXME(RalfJung): Should we have a cache here?
157                 let id = self.tcx.alloc_map.lock().reserve();
158                 let old = self.extra_fn_ptr_map.insert(id, extra);
159                 assert!(old.is_none());
160                 id
161             }
162         };
163         self.tag_static_base_pointer(Pointer::from(id))
164     }
165
166     pub fn allocate(
167         &mut self,
168         size: Size,
169         align: Align,
170         kind: MemoryKind<M::MemoryKinds>,
171     ) -> Pointer<M::PointerTag> {
172         let alloc = Allocation::undef(size, align);
173         self.allocate_with(alloc, kind)
174     }
175
176     pub fn allocate_static_bytes(
177         &mut self,
178         bytes: &[u8],
179         kind: MemoryKind<M::MemoryKinds>,
180     ) -> Pointer<M::PointerTag> {
181         let alloc = Allocation::from_byte_aligned_bytes(bytes);
182         self.allocate_with(alloc, kind)
183     }
184
185     pub fn allocate_with(
186         &mut self,
187         alloc: Allocation,
188         kind: MemoryKind<M::MemoryKinds>,
189     ) -> Pointer<M::PointerTag> {
190         let id = self.tcx.alloc_map.lock().reserve();
191         let (alloc, tag) = M::tag_allocation(&self.extra, id, Cow::Owned(alloc), Some(kind));
192         self.alloc_map.insert(id, (kind, alloc.into_owned()));
193         Pointer::from(id).with_tag(tag)
194     }
195
196     pub fn reallocate(
197         &mut self,
198         ptr: Pointer<M::PointerTag>,
199         old_size_and_align: Option<(Size, Align)>,
200         new_size: Size,
201         new_align: Align,
202         kind: MemoryKind<M::MemoryKinds>,
203     ) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
204         if ptr.offset.bytes() != 0 {
205             return err!(ReallocateNonBasePtr);
206         }
207
208         // For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
209         // This happens so rarely, the perf advantage is outweighed by the maintenance cost.
210         let new_ptr = self.allocate(new_size, new_align, kind);
211         let old_size = match old_size_and_align {
212             Some((size, _align)) => size,
213             None => Size::from_bytes(self.get(ptr.alloc_id)?.bytes.len() as u64),
214         };
215         self.copy(
216             ptr,
217             new_ptr,
218             old_size.min(new_size),
219             /*nonoverlapping*/ true,
220         )?;
221         self.deallocate(ptr, old_size_and_align, kind)?;
222
223         Ok(new_ptr)
224     }
225
226     /// Deallocate a local, or do nothing if that local has been made into a static
227     pub fn deallocate_local(&mut self, ptr: Pointer<M::PointerTag>) -> InterpResult<'tcx> {
228         // The allocation might be already removed by static interning.
229         // This can only really happen in the CTFE instance, not in miri.
230         if self.alloc_map.contains_key(&ptr.alloc_id) {
231             self.deallocate(ptr, None, MemoryKind::Stack)
232         } else {
233             Ok(())
234         }
235     }
236
237     pub fn deallocate(
238         &mut self,
239         ptr: Pointer<M::PointerTag>,
240         old_size_and_align: Option<(Size, Align)>,
241         kind: MemoryKind<M::MemoryKinds>,
242     ) -> InterpResult<'tcx> {
243         trace!("deallocating: {}", ptr.alloc_id);
244
245         if ptr.offset.bytes() != 0 {
246             return err!(DeallocateNonBasePtr);
247         }
248
249         let (alloc_kind, mut alloc) = match self.alloc_map.remove(&ptr.alloc_id) {
250             Some(alloc) => alloc,
251             None => {
252                 // Deallocating static memory -- always an error
253                 return match self.tcx.alloc_map.lock().get(ptr.alloc_id) {
254                     Some(GlobalAlloc::Function(..)) => err!(DeallocatedWrongMemoryKind(
255                         "function".to_string(),
256                         format!("{:?}", kind),
257                     )),
258                     Some(GlobalAlloc::Static(..)) |
259                     Some(GlobalAlloc::Memory(..)) => err!(DeallocatedWrongMemoryKind(
260                         "static".to_string(),
261                         format!("{:?}", kind),
262                     )),
263                     None => err!(DoubleFree)
264                 }
265             }
266         };
267
268         if alloc_kind != kind {
269             return err!(DeallocatedWrongMemoryKind(
270                 format!("{:?}", alloc_kind),
271                 format!("{:?}", kind),
272             ));
273         }
274         if let Some((size, align)) = old_size_and_align {
275             if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
276                 let bytes = Size::from_bytes(alloc.bytes.len() as u64);
277                 return err!(IncorrectAllocationInformation(size, bytes, align, alloc.align));
278             }
279         }
280
281         // Let the machine take some extra action
282         let size = Size::from_bytes(alloc.bytes.len() as u64);
283         AllocationExtra::memory_deallocated(&mut alloc, ptr, size)?;
284
285         // Don't forget to remember size and align of this now-dead allocation
286         let old = self.dead_alloc_map.insert(
287             ptr.alloc_id,
288             (Size::from_bytes(alloc.bytes.len() as u64), alloc.align)
289         );
290         if old.is_some() {
291             bug!("Nothing can be deallocated twice");
292         }
293
294         Ok(())
295     }
296
297     /// Check if the given scalar is allowed to do a memory access of given `size`
298     /// and `align`. On success, returns `None` for zero-sized accesses (where
299     /// nothing else is left to do) and a `Pointer` to use for the actual access otherwise.
300     /// Crucially, if the input is a `Pointer`, we will test it for liveness
301     /// *even of* the size is 0.
302     ///
303     /// Everyone accessing memory based on a `Scalar` should use this method to get the
304     /// `Pointer` they need. And even if you already have a `Pointer`, call this method
305     /// to make sure it is sufficiently aligned and not dangling.  Not doing that may
306     /// cause ICEs.
307     ///
308     /// Most of the time you should use `check_mplace_access`, but when you just have a pointer,
309     /// this method is still appropriate.
310     pub fn check_ptr_access(
311         &self,
312         sptr: Scalar<M::PointerTag>,
313         size: Size,
314         align: Align,
315     ) -> InterpResult<'tcx, Option<Pointer<M::PointerTag>>> {
316         fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
317             if offset % align.bytes() == 0 {
318                 Ok(())
319             } else {
320                 // The biggest power of two through which `offset` is divisible.
321                 let offset_pow2 = 1 << offset.trailing_zeros();
322                 err!(AlignmentCheckFailed {
323                     has: Align::from_bytes(offset_pow2).unwrap(),
324                     required: align,
325                 })
326             }
327         }
328
329         // Normalize to a `Pointer` if we definitely need one.
330         let normalized = if size.bytes() == 0 {
331             // Can be an integer, just take what we got.  We do NOT `force_bits` here;
332             // if this is already a `Pointer` we want to do the bounds checks!
333             sptr
334         } else {
335             // A "real" access, we must get a pointer.
336             Scalar::Ptr(self.force_ptr(sptr)?)
337         };
338         Ok(match normalized.to_bits_or_ptr(self.pointer_size(), self) {
339             Ok(bits) => {
340                 let bits = bits as u64; // it's ptr-sized
341                 assert!(size.bytes() == 0);
342                 // Must be non-NULL and aligned.
343                 if bits == 0 {
344                     return err!(InvalidNullPointerUsage);
345                 }
346                 check_offset_align(bits, align)?;
347                 None
348             }
349             Err(ptr) => {
350                 let (allocation_size, alloc_align) =
351                     self.get_size_and_align(ptr.alloc_id, AllocCheck::Dereferencable)?;
352                 // Test bounds. This also ensures non-NULL.
353                 // It is sufficient to check this for the end pointer. The addition
354                 // checks for overflow.
355                 let end_ptr = ptr.offset(size, self)?;
356                 end_ptr.check_in_alloc(allocation_size, CheckInAllocMsg::MemoryAccessTest)?;
357                 // Test align. Check this last; if both bounds and alignment are violated
358                 // we want the error to be about the bounds.
359                 if alloc_align.bytes() < align.bytes() {
360                     // The allocation itself is not aligned enough.
361                     // FIXME: Alignment check is too strict, depending on the base address that
362                     // got picked we might be aligned even if this check fails.
363                     // We instead have to fall back to converting to an integer and checking
364                     // the "real" alignment.
365                     return err!(AlignmentCheckFailed {
366                         has: alloc_align,
367                         required: align,
368                     });
369                 }
370                 check_offset_align(ptr.offset.bytes(), align)?;
371
372                 // We can still be zero-sized in this branch, in which case we have to
373                 // return `None`.
374                 if size.bytes() == 0 { None } else { Some(ptr) }
375             }
376         })
377     }
378
379     /// Test if the pointer might be NULL.
380     pub fn ptr_may_be_null(
381         &self,
382         ptr: Pointer<M::PointerTag>,
383     ) -> bool {
384         let (size, _align) = self.get_size_and_align(ptr.alloc_id, AllocCheck::MaybeDead)
385             .expect("alloc info with MaybeDead cannot fail");
386         ptr.check_in_alloc(size, CheckInAllocMsg::NullPointerTest).is_err()
387     }
388 }
389
390 /// Allocation accessors
391 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
392     /// Helper function to obtain the global (tcx) allocation for a static.
393     /// This attempts to return a reference to an existing allocation if
394     /// one can be found in `tcx`. That, however, is only possible if `tcx` and
395     /// this machine use the same pointer tag, so it is indirected through
396     /// `M::tag_allocation`.
397     ///
398     /// Notice that every static has two `AllocId` that will resolve to the same
399     /// thing here: one maps to `GlobalAlloc::Static`, this is the "lazy" ID,
400     /// and the other one is maps to `GlobalAlloc::Memory`, this is returned by
401     /// `const_eval_raw` and it is the "resolved" ID.
402     /// The resolved ID is never used by the interpreted progrma, it is hidden.
403     /// The `GlobalAlloc::Memory` branch here is still reachable though; when a static
404     /// contains a reference to memory that was created during its evaluation (i.e., not to
405     /// another static), those inner references only exist in "resolved" form.
406     fn get_static_alloc(
407         memory_extra: &M::MemoryExtra,
408         tcx: TyCtxtAt<'tcx>,
409         id: AllocId,
410     ) -> InterpResult<'tcx, Cow<'tcx, Allocation<M::PointerTag, M::AllocExtra>>> {
411         let alloc = tcx.alloc_map.lock().get(id);
412         let alloc = match alloc {
413             Some(GlobalAlloc::Memory(mem)) =>
414                 Cow::Borrowed(mem),
415             Some(GlobalAlloc::Function(..)) =>
416                 return err!(DerefFunctionPointer),
417             None =>
418                 return err!(DanglingPointerDeref),
419             Some(GlobalAlloc::Static(def_id)) => {
420                 // We got a "lazy" static that has not been computed yet.
421                 if tcx.is_foreign_item(def_id) {
422                     trace!("static_alloc: foreign item {:?}", def_id);
423                     M::find_foreign_static(tcx.tcx, def_id)?
424                 } else {
425                     trace!("static_alloc: Need to compute {:?}", def_id);
426                     let instance = Instance::mono(tcx.tcx, def_id);
427                     let gid = GlobalId {
428                         instance,
429                         promoted: None,
430                     };
431                     // use the raw query here to break validation cycles. Later uses of the static
432                     // will call the full query anyway
433                     let raw_const = tcx.const_eval_raw(ty::ParamEnv::reveal_all().and(gid))
434                         .map_err(|err| {
435                             // no need to report anything, the const_eval call takes care of that
436                             // for statics
437                             assert!(tcx.is_static(def_id));
438                             match err {
439                                 ErrorHandled::Reported =>
440                                     InterpError::InvalidProgram(
441                                         InvalidProgramInfo::ReferencedConstant
442                                     ),
443                                 ErrorHandled::TooGeneric =>
444                                     InterpError::InvalidProgram(InvalidProgramInfo::TooGeneric),
445                             }
446                         })?;
447                     // Make sure we use the ID of the resolved memory, not the lazy one!
448                     let id = raw_const.alloc_id;
449                     let allocation = tcx.alloc_map.lock().unwrap_memory(id);
450                     Cow::Borrowed(allocation)
451                 }
452             }
453         };
454         // We got tcx memory. Let the machine figure out whether and how to
455         // turn that into memory with the right pointer tag.
456         Ok(M::tag_allocation(
457             memory_extra,
458             id, // always use the ID we got as input, not the "hidden" one.
459             alloc,
460             M::STATIC_KIND.map(MemoryKind::Machine),
461         ).0)
462     }
463
464     pub fn get(
465         &self,
466         id: AllocId,
467     ) -> InterpResult<'tcx, &Allocation<M::PointerTag, M::AllocExtra>> {
468         // The error type of the inner closure here is somewhat funny.  We have two
469         // ways of "erroring": An actual error, or because we got a reference from
470         // `get_static_alloc` that we can actually use directly without inserting anything anywhere.
471         // So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
472         let a = self.alloc_map.get_or(id, || {
473             let alloc = Self::get_static_alloc(&self.extra, self.tcx, id).map_err(Err)?;
474             match alloc {
475                 Cow::Borrowed(alloc) => {
476                     // We got a ref, cheaply return that as an "error" so that the
477                     // map does not get mutated.
478                     Err(Ok(alloc))
479                 }
480                 Cow::Owned(alloc) => {
481                     // Need to put it into the map and return a ref to that
482                     let kind = M::STATIC_KIND.expect(
483                         "I got an owned allocation that I have to copy but the machine does \
484                             not expect that to happen"
485                     );
486                     Ok((MemoryKind::Machine(kind), alloc))
487                 }
488             }
489         });
490         // Now unpack that funny error type
491         match a {
492             Ok(a) => Ok(&a.1),
493             Err(a) => a
494         }
495     }
496
497     pub fn get_mut(
498         &mut self,
499         id: AllocId,
500     ) -> InterpResult<'tcx, &mut Allocation<M::PointerTag, M::AllocExtra>> {
501         let tcx = self.tcx;
502         let memory_extra = &self.extra;
503         let a = self.alloc_map.get_mut_or(id, || {
504             // Need to make a copy, even if `get_static_alloc` is able
505             // to give us a cheap reference.
506             let alloc = Self::get_static_alloc(memory_extra, tcx, id)?;
507             if alloc.mutability == Mutability::Immutable {
508                 return err!(ModifiedConstantMemory);
509             }
510             match M::STATIC_KIND {
511                 Some(kind) => Ok((MemoryKind::Machine(kind), alloc.into_owned())),
512                 None => err!(ModifiedStatic),
513             }
514         });
515         // Unpack the error type manually because type inference doesn't
516         // work otherwise (and we cannot help it because `impl Trait`)
517         match a {
518             Err(e) => Err(e),
519             Ok(a) => {
520                 let a = &mut a.1;
521                 if a.mutability == Mutability::Immutable {
522                     return err!(ModifiedConstantMemory);
523                 }
524                 Ok(a)
525             }
526         }
527     }
528
529     /// Obtain the size and alignment of an allocation, even if that allocation has
530     /// been deallocated.
531     ///
532     /// If `liveness` is `AllocCheck::MaybeDead`, this function always returns `Ok`.
533     pub fn get_size_and_align(
534         &self,
535         id: AllocId,
536         liveness: AllocCheck,
537     ) -> InterpResult<'static, (Size, Align)> {
538         // # Regular allocations
539         // Don't use `self.get` here as that will
540         // a) cause cycles in case `id` refers to a static
541         // b) duplicate a static's allocation in miri
542         if let Some((_, alloc)) = self.alloc_map.get(id) {
543             return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
544         }
545
546         // # Function pointers
547         // (both global from `alloc_map` and local from `extra_fn_ptr_map`)
548         if let Ok(_) = self.get_fn_alloc(id) {
549             return if let AllocCheck::Dereferencable = liveness {
550                 // The caller requested no function pointers.
551                 err!(DerefFunctionPointer)
552             } else {
553                 Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
554             };
555         }
556
557         // # Statics
558         // Can't do this in the match argument, we may get cycle errors since the lock would
559         // be held throughout the match.
560         let alloc = self.tcx.alloc_map.lock().get(id);
561         match alloc {
562             Some(GlobalAlloc::Static(did)) => {
563                 // Use size and align of the type.
564                 let ty = self.tcx.type_of(did);
565                 let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
566                 Ok((layout.size, layout.align.abi))
567             },
568             Some(GlobalAlloc::Memory(alloc)) =>
569                 // Need to duplicate the logic here, because the global allocations have
570                 // different associated types than the interpreter-local ones.
571                 Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align)),
572             Some(GlobalAlloc::Function(_)) =>
573                 bug!("We already checked function pointers above"),
574             // The rest must be dead.
575             None => if let AllocCheck::MaybeDead = liveness {
576                 // Deallocated pointers are allowed, we should be able to find
577                 // them in the map.
578                 Ok(*self.dead_alloc_map.get(&id)
579                     .expect("deallocated pointers should all be recorded in \
580                             `dead_alloc_map`"))
581             } else {
582                 err!(DanglingPointerDeref)
583             },
584         }
585     }
586
587     fn get_fn_alloc(&self, id: AllocId) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
588         trace!("reading fn ptr: {}", id);
589         if let Some(extra) = self.extra_fn_ptr_map.get(&id) {
590             Ok(FnVal::Other(*extra))
591         } else {
592             match self.tcx.alloc_map.lock().get(id) {
593                 Some(GlobalAlloc::Function(instance)) => Ok(FnVal::Instance(instance)),
594                 _ => err!(ExecuteMemory),
595             }
596         }
597     }
598
599     pub fn get_fn(
600         &self,
601         ptr: Scalar<M::PointerTag>,
602     ) -> InterpResult<'tcx, FnVal<'tcx, M::ExtraFnVal>> {
603         let ptr = self.force_ptr(ptr)?; // We definitely need a pointer value.
604         if ptr.offset.bytes() != 0 {
605             return err!(InvalidFunctionPointer);
606         }
607         self.get_fn_alloc(ptr.alloc_id)
608     }
609
610     pub fn mark_immutable(&mut self, id: AllocId) -> InterpResult<'tcx> {
611         self.get_mut(id)?.mutability = Mutability::Immutable;
612         Ok(())
613     }
614
615     /// For debugging, print an allocation and all allocations it points to, recursively.
616     pub fn dump_alloc(&self, id: AllocId) {
617         self.dump_allocs(vec![id]);
618     }
619
620     fn dump_alloc_helper<Tag, Extra>(
621         &self,
622         allocs_seen: &mut FxHashSet<AllocId>,
623         allocs_to_print: &mut VecDeque<AllocId>,
624         mut msg: String,
625         alloc: &Allocation<Tag, Extra>,
626         extra: String,
627     ) {
628         use std::fmt::Write;
629
630         let prefix_len = msg.len();
631         let mut relocations = vec![];
632
633         for i in 0..(alloc.bytes.len() as u64) {
634             let i = Size::from_bytes(i);
635             if let Some(&(_, target_id)) = alloc.relocations.get(&i) {
636                 if allocs_seen.insert(target_id) {
637                     allocs_to_print.push_back(target_id);
638                 }
639                 relocations.push((i, target_id));
640             }
641             if alloc.undef_mask.is_range_defined(i, i + Size::from_bytes(1)).is_ok() {
642                 // this `as usize` is fine, since `i` came from a `usize`
643                 write!(msg, "{:02x} ", alloc.bytes[i.bytes() as usize]).unwrap();
644             } else {
645                 msg.push_str("__ ");
646             }
647         }
648
649         trace!(
650             "{}({} bytes, alignment {}){}",
651             msg,
652             alloc.bytes.len(),
653             alloc.align.bytes(),
654             extra
655         );
656
657         if !relocations.is_empty() {
658             msg.clear();
659             write!(msg, "{:1$}", "", prefix_len).unwrap(); // Print spaces.
660             let mut pos = Size::ZERO;
661             let relocation_width = (self.pointer_size().bytes() - 1) * 3;
662             for (i, target_id) in relocations {
663                 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
664                 write!(msg, "{:1$}", "", ((i - pos) * 3).bytes() as usize).unwrap();
665                 let target = format!("({})", target_id);
666                 // this `as usize` is fine, since we can't print more chars than `usize::MAX`
667                 write!(msg, "â””{0:─^1$}┘ ", target, relocation_width as usize).unwrap();
668                 pos = i + self.pointer_size();
669             }
670             trace!("{}", msg);
671         }
672     }
673
674     /// For debugging, print a list of allocations and all allocations they point to, recursively.
675     pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
676         if !log_enabled!(::log::Level::Trace) {
677             return;
678         }
679         allocs.sort();
680         allocs.dedup();
681         let mut allocs_to_print = VecDeque::from(allocs);
682         let mut allocs_seen = FxHashSet::default();
683
684         while let Some(id) = allocs_to_print.pop_front() {
685             let msg = format!("Alloc {:<5} ", format!("{}:", id));
686
687             // normal alloc?
688             match self.alloc_map.get_or(id, || Err(())) {
689                 Ok((kind, alloc)) => {
690                     let extra = match kind {
691                         MemoryKind::Stack => " (stack)".to_owned(),
692                         MemoryKind::Vtable => " (vtable)".to_owned(),
693                         MemoryKind::Machine(m) => format!(" ({:?})", m),
694                     };
695                     self.dump_alloc_helper(
696                         &mut allocs_seen, &mut allocs_to_print,
697                         msg, alloc, extra
698                     );
699                 },
700                 Err(()) => {
701                     // static alloc?
702                     match self.tcx.alloc_map.lock().get(id) {
703                         Some(GlobalAlloc::Memory(alloc)) => {
704                             self.dump_alloc_helper(
705                                 &mut allocs_seen, &mut allocs_to_print,
706                                 msg, alloc, " (immutable)".to_owned()
707                             );
708                         }
709                         Some(GlobalAlloc::Function(func)) => {
710                             trace!("{} {}", msg, func);
711                         }
712                         Some(GlobalAlloc::Static(did)) => {
713                             trace!("{} {:?}", msg, did);
714                         }
715                         None => {
716                             trace!("{} (deallocated)", msg);
717                         }
718                     }
719                 },
720             };
721
722         }
723     }
724
725     pub fn leak_report(&self) -> usize {
726         trace!("### LEAK REPORT ###");
727         let leaks: Vec<_> = self.alloc_map.filter_map_collect(|&id, &(kind, _)| {
728             if kind.may_leak() { None } else { Some(id) }
729         });
730         let n = leaks.len();
731         self.dump_allocs(leaks);
732         n
733     }
734
735     /// This is used by [priroda](https://github.com/oli-obk/priroda)
736     pub fn alloc_map(&self) -> &M::MemoryMap {
737         &self.alloc_map
738     }
739 }
740
741 /// Reading and writing.
742 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
743     /// Reads the given number of bytes from memory. Returns them as a slice.
744     ///
745     /// Performs appropriate bounds checks.
746     pub fn read_bytes(
747         &self,
748         ptr: Scalar<M::PointerTag>,
749         size: Size,
750     ) -> InterpResult<'tcx, &[u8]> {
751         let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
752             Some(ptr) => ptr,
753             None => return Ok(&[]), // zero-sized access
754         };
755         self.get(ptr.alloc_id)?.get_bytes(self, ptr, size)
756     }
757
758     /// Reads a 0-terminated sequence of bytes from memory. Returns them as a slice.
759     ///
760     /// Performs appropriate bounds checks.
761     pub fn read_c_str(&self, ptr: Scalar<M::PointerTag>) -> InterpResult<'tcx, &[u8]> {
762         let ptr = self.force_ptr(ptr)?; // We need to read at least 1 byte, so we *need* a ptr.
763         self.get(ptr.alloc_id)?.read_c_str(self, ptr)
764     }
765
766     /// Expects the caller to have checked bounds and alignment.
767     pub fn copy(
768         &mut self,
769         src: Pointer<M::PointerTag>,
770         dest: Pointer<M::PointerTag>,
771         size: Size,
772         nonoverlapping: bool,
773     ) -> InterpResult<'tcx> {
774         self.copy_repeatedly(src, dest, size, 1, nonoverlapping)
775     }
776
777     /// Expects the caller to have checked bounds and alignment.
778     pub fn copy_repeatedly(
779         &mut self,
780         src: Pointer<M::PointerTag>,
781         dest: Pointer<M::PointerTag>,
782         size: Size,
783         length: u64,
784         nonoverlapping: bool,
785     ) -> InterpResult<'tcx> {
786         // first copy the relocations to a temporary buffer, because
787         // `get_bytes_mut` will clear the relocations, which is correct,
788         // since we don't want to keep any relocations at the target.
789         // (`get_bytes_with_undef_and_ptr` below checks that there are no
790         // relocations overlapping the edges; those would not be handled correctly).
791         let relocations = {
792             let relocations = self.get(src.alloc_id)?.relocations(self, src, size);
793             if relocations.is_empty() {
794                 // nothing to copy, ignore even the `length` loop
795                 Vec::new()
796             } else {
797                 let mut new_relocations = Vec::with_capacity(relocations.len() * (length as usize));
798                 for i in 0..length {
799                     new_relocations.extend(
800                         relocations
801                         .iter()
802                         .map(|&(offset, reloc)| {
803                             // compute offset for current repetition
804                             let dest_offset = dest.offset + (i * size);
805                             (
806                                 // shift offsets from source allocation to destination allocation
807                                 offset + dest_offset - src.offset,
808                                 reloc,
809                             )
810                         })
811                     );
812                 }
813
814                 new_relocations
815             }
816         };
817
818         let tcx = self.tcx.tcx;
819
820         // This checks relocation edges on the src.
821         let src_bytes = self.get(src.alloc_id)?
822             .get_bytes_with_undef_and_ptr(&tcx, src, size)?
823             .as_ptr();
824         let dest_bytes = self.get_mut(dest.alloc_id)?
825             .get_bytes_mut(&tcx, dest, size * length)?
826             .as_mut_ptr();
827
828         // SAFE: The above indexing would have panicked if there weren't at least `size` bytes
829         // behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and
830         // `dest` could possibly overlap.
831         // The pointers above remain valid even if the `HashMap` table is moved around because they
832         // point into the `Vec` storing the bytes.
833         unsafe {
834             assert_eq!(size.bytes() as usize as u64, size.bytes());
835             if src.alloc_id == dest.alloc_id {
836                 if nonoverlapping {
837                     if (src.offset <= dest.offset && src.offset + size > dest.offset) ||
838                         (dest.offset <= src.offset && dest.offset + size > src.offset)
839                     {
840                         return err!(Intrinsic(
841                             "copy_nonoverlapping called on overlapping ranges".to_string(),
842                         ));
843                     }
844                 }
845
846                 for i in 0..length {
847                     ptr::copy(src_bytes,
848                               dest_bytes.offset((size.bytes() * i) as isize),
849                               size.bytes() as usize);
850                 }
851             } else {
852                 for i in 0..length {
853                     ptr::copy_nonoverlapping(src_bytes,
854                                              dest_bytes.offset((size.bytes() * i) as isize),
855                                              size.bytes() as usize);
856                 }
857             }
858         }
859
860         // copy definedness to the destination
861         self.copy_undef_mask(src, dest, size, length)?;
862         // copy the relocations to the destination
863         self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations);
864
865         Ok(())
866     }
867 }
868
869 /// Undefined bytes
870 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
871     // FIXME: Add a fast version for the common, nonoverlapping case
872     fn copy_undef_mask(
873         &mut self,
874         src: Pointer<M::PointerTag>,
875         dest: Pointer<M::PointerTag>,
876         size: Size,
877         repeat: u64,
878     ) -> InterpResult<'tcx> {
879         // The bits have to be saved locally before writing to dest in case src and dest overlap.
880         assert_eq!(size.bytes() as usize as u64, size.bytes());
881
882         let undef_mask = &self.get(src.alloc_id)?.undef_mask;
883
884         // Since we are copying `size` bytes from `src` to `dest + i * size` (`for i in 0..repeat`),
885         // a naive undef mask copying algorithm would repeatedly have to read the undef mask from
886         // the source and write it to the destination. Even if we optimized the memory accesses,
887         // we'd be doing all of this `repeat` times.
888         // Therefor we precompute a compressed version of the undef mask of the source value and
889         // then write it back `repeat` times without computing any more information from the source.
890
891         // a precomputed cache for ranges of defined/undefined bits
892         // 0000010010001110 will become
893         // [5, 1, 2, 1, 3, 3, 1]
894         // where each element toggles the state
895         let mut ranges = smallvec::SmallVec::<[u64; 1]>::new();
896         let first = undef_mask.get(src.offset);
897         let mut cur_len = 1;
898         let mut cur = first;
899         for i in 1..size.bytes() {
900             // FIXME: optimize to bitshift the current undef block's bits and read the top bit
901             if undef_mask.get(src.offset + Size::from_bytes(i)) == cur {
902                 cur_len += 1;
903             } else {
904                 ranges.push(cur_len);
905                 cur_len = 1;
906                 cur = !cur;
907             }
908         }
909
910         // now fill in all the data
911         let dest_allocation = self.get_mut(dest.alloc_id)?;
912         // an optimization where we can just overwrite an entire range of definedness bits if
913         // they are going to be uniformly `1` or `0`.
914         if ranges.is_empty() {
915             dest_allocation.undef_mask.set_range_inbounds(
916                 dest.offset,
917                 dest.offset + size * repeat,
918                 first,
919             );
920             return Ok(())
921         }
922
923         // remember to fill in the trailing bits
924         ranges.push(cur_len);
925
926         for mut j in 0..repeat {
927             j *= size.bytes();
928             j += dest.offset.bytes();
929             let mut cur = first;
930             for range in &ranges {
931                 let old_j = j;
932                 j += range;
933                 dest_allocation.undef_mask.set_range_inbounds(
934                     Size::from_bytes(old_j),
935                     Size::from_bytes(j),
936                     cur,
937                 );
938                 cur = !cur;
939             }
940         }
941         Ok(())
942     }
943
944     pub fn force_ptr(
945         &self,
946         scalar: Scalar<M::PointerTag>,
947     ) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
948         match scalar {
949             Scalar::Ptr(ptr) => Ok(ptr),
950             _ => M::int_to_ptr(&self, scalar.to_usize(self)?)
951         }
952     }
953
954     pub fn force_bits(
955         &self,
956         scalar: Scalar<M::PointerTag>,
957         size: Size
958     ) -> InterpResult<'tcx, u128> {
959         match scalar.to_bits_or_ptr(size, self) {
960             Ok(bits) => Ok(bits),
961             Err(ptr) => Ok(M::ptr_to_int(&self, ptr)? as u128)
962         }
963     }
964 }