1 //! This module contains everything needed to instantiate an interpreter.
2 //! This separation exists to ensure that no fancy miri features like
3 //! interpreting common C functions leak into CTFE.
5 use std::borrow::{Borrow, Cow};
10 use rustc_middle::ty::{self, Ty, TyCtxt};
11 use rustc_span::def_id::DefId;
12 use rustc_target::abi::Size;
13 use rustc_target::spec::abi::Abi as CallAbi;
16 AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
17 MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
20 /// Data returned by Machine::stack_pop,
21 /// to provide further control over the popping of the stack frame
22 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
23 pub enum StackPopJump {
24 /// Indicates that no special handling should be
25 /// done - we'll either return normally or unwind
26 /// based on the terminator for the function
30 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
31 /// took care of everything.
35 /// Whether this kind of memory is allowed to leak
36 pub trait MayLeak: Copy {
37 fn may_leak(self) -> bool;
40 /// The functionality needed by memory to manage its allocations
41 pub trait AllocMap<K: Hash + Eq, V> {
42 /// Tests if the map contains the given key.
43 /// Deliberately takes `&mut` because that is sufficient, and some implementations
44 /// can be more efficient then (using `RefCell::get_mut`).
45 fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
49 /// Inserts a new entry into the map.
50 fn insert(&mut self, k: K, v: V) -> Option<V>;
52 /// Removes an entry from the map.
53 fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
57 /// Returns data based on the keys and values in the map.
58 fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
60 /// Returns a reference to entry `k`. If no such entry exists, call
61 /// `vacant` and either forward its error, or add its result to the map
62 /// and return a reference to *that*.
63 fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
65 /// Returns a mutable reference to entry `k`. If no such entry exists, call
66 /// `vacant` and either forward its error, or add its result to the map
67 /// and return a reference to *that*.
68 fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
71 fn get(&self, k: K) -> Option<&V> {
72 self.get_or(k, || Err(())).ok()
76 fn get_mut(&mut self, k: K) -> Option<&mut V> {
77 self.get_mut_or(k, || Err(())).ok()
81 /// Methods of this trait signifies a point where CTFE evaluation would fail
82 /// and some use case dependent behaviour can instead be applied.
83 pub trait Machine<'mir, 'tcx>: Sized {
84 /// Additional memory kinds a machine wishes to distinguish from the builtin ones
85 type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
87 /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
88 type Provenance: Provenance + Eq + Hash + 'static;
90 /// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
91 /// that is passed to memory access hooks so they can do things with it.
92 type ProvenanceExtra: Copy + 'static;
94 /// Machines can define extra (non-instance) things that represent values of function pointers.
95 /// For example, Miri uses this to return a function pointer from `dlsym`
96 /// that can later be called to execute the right thing.
97 type ExtraFnVal: Debug + Copy;
99 /// Extra data stored in every call frame.
102 /// Extra data stored in every allocation.
103 type AllocExtra: Debug + Clone + 'static;
105 /// Memory's allocation map
106 type MemoryMap: AllocMap<
108 (MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
112 /// The memory kind to use for copied global memory (held in `tcx`) --
113 /// or None if such memory should not be mutated and thus any such attempt will cause
114 /// a `ModifiedStatic` error to be raised.
115 /// Statics are copied under two circumstances: When they are mutated, and when
116 /// `adjust_allocation` (see below) returns an owned allocation
117 /// that is added to the memory so that the work is not done twice.
118 const GLOBAL_KIND: Option<Self::MemoryKind>;
120 /// Should the machine panic on allocation failures?
121 const PANIC_ON_ALLOC_FAIL: bool;
123 /// Whether memory accesses should be alignment-checked.
124 fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
126 /// Whether, when checking alignment, we should `force_int` and thus support
127 /// custom alignment logic based on whatever the integer address happens to be.
129 /// Requires Provenance::OFFSET_IS_ADDR to be true.
130 fn force_int_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
132 /// Whether to enforce the validity invariant
133 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
135 /// Whether to enforce integers and floats being initialized.
136 fn enforce_number_init(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
138 /// Whether function calls should be [ABI](CallAbi)-checked.
139 fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
143 /// Whether CheckedBinOp MIR statements should actually check for overflow.
144 fn checked_binop_checks_overflow(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
146 /// Entry point for obtaining the MIR of anything that should get evaluated.
147 /// So not just functions and shims, but also const/static initializers, anonymous
150 ecx: &InterpCx<'mir, 'tcx, Self>,
151 instance: ty::InstanceDef<'tcx>,
152 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
153 Ok(ecx.tcx.instance_mir(instance))
156 /// Entry point to all function calls.
158 /// Returns either the mir to use for the call, or `None` if execution should
159 /// just proceed (which usually means this hook did all the work that the
160 /// called function should usually have done). In the latter case, it is
161 /// this hook's responsibility to advance the instruction pointer!
162 /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
163 /// nor just jump to `ret`, but instead push their own stack frame.)
164 /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
166 fn find_mir_or_eval_fn(
167 ecx: &mut InterpCx<'mir, 'tcx, Self>,
168 instance: ty::Instance<'tcx>,
170 args: &[OpTy<'tcx, Self::Provenance>],
171 destination: &PlaceTy<'tcx, Self::Provenance>,
172 target: Option<mir::BasicBlock>,
173 unwind: StackPopUnwind,
174 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
176 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
177 /// pointer as appropriate.
179 ecx: &mut InterpCx<'mir, 'tcx, Self>,
180 fn_val: Self::ExtraFnVal,
182 args: &[OpTy<'tcx, Self::Provenance>],
183 destination: &PlaceTy<'tcx, Self::Provenance>,
184 target: Option<mir::BasicBlock>,
185 unwind: StackPopUnwind,
186 ) -> InterpResult<'tcx>;
188 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
189 /// responsibility to advance the instruction pointer as appropriate.
191 ecx: &mut InterpCx<'mir, 'tcx, Self>,
192 instance: ty::Instance<'tcx>,
193 args: &[OpTy<'tcx, Self::Provenance>],
194 destination: &PlaceTy<'tcx, Self::Provenance>,
195 target: Option<mir::BasicBlock>,
196 unwind: StackPopUnwind,
197 ) -> InterpResult<'tcx>;
199 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
201 ecx: &mut InterpCx<'mir, 'tcx, Self>,
202 msg: &mir::AssertMessage<'tcx>,
203 unwind: Option<mir::BasicBlock>,
204 ) -> InterpResult<'tcx>;
206 /// Called to evaluate `Abort` MIR terminator.
207 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
208 throw_unsup_format!("aborting execution is not supported")
211 /// Called for all binary operations where the LHS has pointer type.
213 /// Returns a (value, overflowed) pair if the operation succeeded
215 ecx: &InterpCx<'mir, 'tcx, Self>,
217 left: &ImmTy<'tcx, Self::Provenance>,
218 right: &ImmTy<'tcx, Self::Provenance>,
219 ) -> InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)>;
221 /// Called to read the specified `local` from the `frame`.
222 /// Since reading a ZST is not actually accessing memory or locals, this is never invoked
226 frame: &'a Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
228 ) -> InterpResult<'tcx, &'a Operand<Self::Provenance>>
232 frame.locals[local].access()
235 /// Called to write the specified `local` from the `frame`.
236 /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
239 fn access_local_mut<'a>(
240 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
243 ) -> InterpResult<'tcx, &'a mut Operand<Self::Provenance>>
247 ecx.stack_mut()[frame].locals[local].access_mut()
250 /// Called before a basic block terminator is executed.
251 /// You can use this to detect endlessly running programs.
253 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
257 /// Called before a global allocation is accessed.
258 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
260 fn before_access_global(
264 _allocation: ConstAllocation<'tcx>,
265 _static_def_id: Option<DefId>,
267 ) -> InterpResult<'tcx> {
271 /// Return the `AllocId` for the given thread-local static in the current thread.
272 fn thread_local_static_base_pointer(
273 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
275 ) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
276 throw_unsup!(ThreadLocalStatic(def_id))
279 /// Return the root pointer for the given `extern static`.
280 fn extern_static_base_pointer(
281 ecx: &InterpCx<'mir, 'tcx, Self>,
283 ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
285 /// Return a "base" pointer for the given allocation: the one that is used for direct
286 /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
288 /// Not called on `extern` or thread-local statics (those use the methods above).
289 fn adjust_alloc_base_pointer(
290 ecx: &InterpCx<'mir, 'tcx, Self>,
292 ) -> Pointer<Self::Provenance>;
294 /// "Int-to-pointer cast"
295 fn ptr_from_addr_cast(
296 ecx: &InterpCx<'mir, 'tcx, Self>,
298 ) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
300 /// Marks a pointer as exposed, allowing it's provenance
301 /// to be recovered. "Pointer-to-int cast"
303 ecx: &mut InterpCx<'mir, 'tcx, Self>,
304 ptr: Pointer<Self::Provenance>,
305 ) -> InterpResult<'tcx>;
307 /// Convert a pointer with provenance into an allocation-offset pair
308 /// and extra provenance info.
310 /// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
312 /// When this fails, that means the pointer does not point to a live allocation.
314 ecx: &InterpCx<'mir, 'tcx, Self>,
315 ptr: Pointer<Self::Provenance>,
316 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
318 /// Called to adjust allocations to the Provenance and AllocExtra of this machine.
320 /// The way we construct allocations is to always first construct it without extra and then add
321 /// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
322 /// globals, and allocations created by Miri during evaluation.
324 /// `kind` is the kind of the allocation being adjusted; it can be `None` when
325 /// it's a global and `GLOBAL_KIND` is `None`.
327 /// This should avoid copying if no work has to be done! If this returns an owned
328 /// allocation (because a copy had to be done to adjust things), machine memory will
329 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
330 /// owned allocation to the map even when the map is shared.)
332 /// This must only fail if `alloc` contains relocations.
333 fn adjust_allocation<'b>(
334 ecx: &InterpCx<'mir, 'tcx, Self>,
336 alloc: Cow<'b, Allocation>,
337 kind: Option<MemoryKind<Self::MemoryKind>>,
338 ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
340 /// Hook for performing extra checks on a memory read access.
342 /// Takes read-only access to the allocation so we can keep all the memory read
343 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
349 _alloc_extra: &Self::AllocExtra,
350 _prov: (AllocId, Self::ProvenanceExtra),
352 ) -> InterpResult<'tcx> {
356 /// Hook for performing extra checks on a memory write access.
361 _alloc_extra: &mut Self::AllocExtra,
362 _prov: (AllocId, Self::ProvenanceExtra),
364 ) -> InterpResult<'tcx> {
368 /// Hook for performing extra operations on a memory deallocation.
370 fn memory_deallocated(
373 _alloc_extra: &mut Self::AllocExtra,
374 _prov: (AllocId, Self::ProvenanceExtra),
376 ) -> InterpResult<'tcx> {
380 /// Executes a retagging operation.
383 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
384 _kind: mir::RetagKind,
385 _place: &PlaceTy<'tcx, Self::Provenance>,
386 ) -> InterpResult<'tcx> {
390 /// Called immediately before a new stack frame gets pushed.
392 ecx: &mut InterpCx<'mir, 'tcx, Self>,
393 frame: Frame<'mir, 'tcx, Self::Provenance>,
394 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
396 /// Borrow the current thread's stack.
398 ecx: &'a InterpCx<'mir, 'tcx, Self>,
399 ) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>];
401 /// Mutably borrow the current thread's stack.
403 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
404 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
406 /// Called immediately after a stack frame got pushed and its locals got initialized.
407 fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
411 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
412 /// The `locals` have already been destroyed!
414 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
415 _frame: Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
417 ) -> InterpResult<'tcx, StackPopJump> {
418 // By default, we do not support unwinding from panics
420 Ok(StackPopJump::Normal)
424 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
425 // (CTFE and ConstProp) use the same instance. Here, we share that code.
426 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
427 type Provenance = AllocId;
428 type ProvenanceExtra = ();
433 rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
434 const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
436 type AllocExtra = ();
437 type FrameExtra = ();
440 fn force_int_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
441 // We do not support `force_int`.
446 fn enforce_number_init(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
451 fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
457 _ecx: &mut InterpCx<$mir, $tcx, Self>,
460 _args: &[OpTy<$tcx>],
461 _destination: &PlaceTy<$tcx, Self::Provenance>,
462 _target: Option<mir::BasicBlock>,
463 _unwind: StackPopUnwind,
464 ) -> InterpResult<$tcx> {
469 fn adjust_allocation<'b>(
470 _ecx: &InterpCx<$mir, $tcx, Self>,
472 alloc: Cow<'b, Allocation>,
473 _kind: Option<MemoryKind<Self::MemoryKind>>,
474 ) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
478 fn extern_static_base_pointer(
479 ecx: &InterpCx<$mir, $tcx, Self>,
481 ) -> InterpResult<$tcx, Pointer> {
482 // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
483 Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
487 fn adjust_alloc_base_pointer(
488 _ecx: &InterpCx<$mir, $tcx, Self>,
489 ptr: Pointer<AllocId>,
490 ) -> Pointer<AllocId> {
495 fn ptr_from_addr_cast(
496 _ecx: &InterpCx<$mir, $tcx, Self>,
498 ) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
499 // Allow these casts, but make the pointer not dereferenceable.
500 // (I.e., they behave like transmutation.)
501 Ok(Pointer::from_addr(addr))
506 _ecx: &InterpCx<$mir, $tcx, Self>,
507 ptr: Pointer<AllocId>,
508 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
509 // We know `offset` is relative to the allocation, so we can use `into_parts`.
510 let (alloc_id, offset) = ptr.into_parts();
511 Some((alloc_id, offset, ()))