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};
9 use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
10 use rustc_middle::mir;
11 use rustc_middle::ty::{self, Ty, TyCtxt};
12 use rustc_span::def_id::DefId;
13 use rustc_target::abi::{Align, Size};
14 use rustc_target::spec::abi::Abi as CallAbi;
16 use crate::const_eval::CheckAlignment;
19 AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
20 MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
23 /// Data returned by Machine::stack_pop,
24 /// to provide further control over the popping of the stack frame
25 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
26 pub enum StackPopJump {
27 /// Indicates that no special handling should be
28 /// done - we'll either return normally or unwind
29 /// based on the terminator for the function
33 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
34 /// took care of everything.
38 /// Whether this kind of memory is allowed to leak
39 pub trait MayLeak: Copy {
40 fn may_leak(self) -> bool;
43 /// The functionality needed by memory to manage its allocations
44 pub trait AllocMap<K: Hash + Eq, V> {
45 /// Tests if the map contains the given key.
46 /// Deliberately takes `&mut` because that is sufficient, and some implementations
47 /// can be more efficient then (using `RefCell::get_mut`).
48 fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
52 /// Inserts a new entry into the map.
53 fn insert(&mut self, k: K, v: V) -> Option<V>;
55 /// Removes an entry from the map.
56 fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
60 /// Returns data based on the keys and values in the map.
61 fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
63 /// Returns a reference to entry `k`. If no such entry exists, call
64 /// `vacant` and either forward its error, or add its result to the map
65 /// and return a reference to *that*.
66 fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
68 /// Returns a mutable reference to entry `k`. If no such entry exists, call
69 /// `vacant` and either forward its error, or add its result to the map
70 /// and return a reference to *that*.
71 fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
74 fn get(&self, k: K) -> Option<&V> {
75 self.get_or(k, || Err(())).ok()
79 fn get_mut(&mut self, k: K) -> Option<&mut V> {
80 self.get_mut_or(k, || Err(())).ok()
84 /// Methods of this trait signifies a point where CTFE evaluation would fail
85 /// and some use case dependent behaviour can instead be applied.
86 pub trait Machine<'mir, 'tcx>: Sized {
87 /// Additional memory kinds a machine wishes to distinguish from the builtin ones
88 type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
90 /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
91 type Provenance: Provenance + Eq + Hash + 'static;
93 /// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
94 /// that is passed to memory access hooks so they can do things with it.
95 type ProvenanceExtra: Copy + 'static;
97 /// Machines can define extra (non-instance) things that represent values of function pointers.
98 /// For example, Miri uses this to return a function pointer from `dlsym`
99 /// that can later be called to execute the right thing.
100 type ExtraFnVal: Debug + Copy;
102 /// Extra data stored in every call frame.
105 /// Extra data stored in every allocation.
106 type AllocExtra: Debug + Clone + 'static;
108 /// Memory's allocation map
109 type MemoryMap: AllocMap<
111 (MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
115 /// The memory kind to use for copied global memory (held in `tcx`) --
116 /// or None if such memory should not be mutated and thus any such attempt will cause
117 /// a `ModifiedStatic` error to be raised.
118 /// Statics are copied under two circumstances: When they are mutated, and when
119 /// `adjust_allocation` (see below) returns an owned allocation
120 /// that is added to the memory so that the work is not done twice.
121 const GLOBAL_KIND: Option<Self::MemoryKind>;
123 /// Should the machine panic on allocation failures?
124 const PANIC_ON_ALLOC_FAIL: bool;
126 /// Whether memory accesses should be alignment-checked.
127 fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> CheckAlignment;
129 /// Whether, when checking alignment, we should look at the actual address and thus support
130 /// custom alignment logic based on whatever the integer address happens to be.
132 /// If this returns true, Provenance::OFFSET_IS_ADDR must be true.
133 fn use_addr_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
135 fn alignment_check_failed(
136 ecx: &InterpCx<'mir, 'tcx, Self>,
139 check: CheckAlignment,
140 ) -> InterpResult<'tcx, ()>;
142 /// Whether to enforce the validity invariant
143 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
145 /// Whether function calls should be [ABI](CallAbi)-checked.
146 fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
150 /// Whether CheckedBinOp MIR statements should actually check for overflow.
151 fn checked_binop_checks_overflow(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
153 /// Entry point for obtaining the MIR of anything that should get evaluated.
154 /// So not just functions and shims, but also const/static initializers, anonymous
157 ecx: &InterpCx<'mir, 'tcx, Self>,
158 instance: ty::InstanceDef<'tcx>,
159 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
160 Ok(ecx.tcx.instance_mir(instance))
163 /// Entry point to all function calls.
165 /// Returns either the mir to use for the call, or `None` if execution should
166 /// just proceed (which usually means this hook did all the work that the
167 /// called function should usually have done). In the latter case, it is
168 /// this hook's responsibility to advance the instruction pointer!
169 /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
170 /// nor just jump to `ret`, but instead push their own stack frame.)
171 /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
173 fn find_mir_or_eval_fn(
174 ecx: &mut InterpCx<'mir, 'tcx, Self>,
175 instance: ty::Instance<'tcx>,
177 args: &[OpTy<'tcx, Self::Provenance>],
178 destination: &PlaceTy<'tcx, Self::Provenance>,
179 target: Option<mir::BasicBlock>,
180 unwind: StackPopUnwind,
181 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
183 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
184 /// pointer as appropriate.
186 ecx: &mut InterpCx<'mir, 'tcx, Self>,
187 fn_val: Self::ExtraFnVal,
189 args: &[OpTy<'tcx, Self::Provenance>],
190 destination: &PlaceTy<'tcx, Self::Provenance>,
191 target: Option<mir::BasicBlock>,
192 unwind: StackPopUnwind,
193 ) -> InterpResult<'tcx>;
195 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
196 /// responsibility to advance the instruction pointer as appropriate.
198 ecx: &mut InterpCx<'mir, 'tcx, Self>,
199 instance: ty::Instance<'tcx>,
200 args: &[OpTy<'tcx, Self::Provenance>],
201 destination: &PlaceTy<'tcx, Self::Provenance>,
202 target: Option<mir::BasicBlock>,
203 unwind: StackPopUnwind,
204 ) -> InterpResult<'tcx>;
206 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
208 ecx: &mut InterpCx<'mir, 'tcx, Self>,
209 msg: &mir::AssertMessage<'tcx>,
210 unwind: Option<mir::BasicBlock>,
211 ) -> InterpResult<'tcx>;
213 /// Called to evaluate `Abort` MIR terminator.
214 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
215 throw_unsup_format!("aborting execution is not supported")
218 /// Called for all binary operations where the LHS has pointer type.
220 /// Returns a (value, overflowed) pair if the operation succeeded
222 ecx: &InterpCx<'mir, 'tcx, Self>,
224 left: &ImmTy<'tcx, Self::Provenance>,
225 right: &ImmTy<'tcx, Self::Provenance>,
226 ) -> InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)>;
228 /// Called to write the specified `local` from the `frame`.
229 /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
232 /// Due to borrow checker trouble, we indicate the `frame` as an index rather than an `&mut
235 fn access_local_mut<'a>(
236 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
239 ) -> InterpResult<'tcx, &'a mut Operand<Self::Provenance>>
243 ecx.stack_mut()[frame].locals[local].access_mut()
246 /// Called before a basic block terminator is executed.
248 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
252 /// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
253 /// You can use this to detect long or endlessly running programs.
255 fn increment_const_eval_counter(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
259 /// Called before a global allocation is accessed.
260 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
262 fn before_access_global(
266 _allocation: ConstAllocation<'tcx>,
267 _static_def_id: Option<DefId>,
269 ) -> InterpResult<'tcx> {
273 /// Return the `AllocId` for the given thread-local static in the current thread.
274 fn thread_local_static_base_pointer(
275 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
277 ) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
278 throw_unsup!(ThreadLocalStatic(def_id))
281 /// Return the root pointer for the given `extern static`.
282 fn extern_static_base_pointer(
283 ecx: &InterpCx<'mir, 'tcx, Self>,
285 ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
287 /// Return a "base" pointer for the given allocation: the one that is used for direct
288 /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
290 /// Not called on `extern` or thread-local statics (those use the methods above).
291 fn adjust_alloc_base_pointer(
292 ecx: &InterpCx<'mir, 'tcx, Self>,
294 ) -> Pointer<Self::Provenance>;
296 /// "Int-to-pointer cast"
297 fn ptr_from_addr_cast(
298 ecx: &InterpCx<'mir, 'tcx, Self>,
300 ) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
302 /// Marks a pointer as exposed, allowing it's provenance
303 /// to be recovered. "Pointer-to-int cast"
305 ecx: &mut InterpCx<'mir, 'tcx, Self>,
306 ptr: Pointer<Self::Provenance>,
307 ) -> InterpResult<'tcx>;
309 /// Convert a pointer with provenance into an allocation-offset pair
310 /// and extra provenance info.
312 /// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
314 /// When this fails, that means the pointer does not point to a live allocation.
316 ecx: &InterpCx<'mir, 'tcx, Self>,
317 ptr: Pointer<Self::Provenance>,
318 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
320 /// Called to adjust allocations to the Provenance and AllocExtra of this machine.
322 /// The way we construct allocations is to always first construct it without extra and then add
323 /// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
324 /// globals, and allocations created by Miri during evaluation.
326 /// `kind` is the kind of the allocation being adjusted; it can be `None` when
327 /// it's a global and `GLOBAL_KIND` is `None`.
329 /// This should avoid copying if no work has to be done! If this returns an owned
330 /// allocation (because a copy had to be done to adjust things), machine memory will
331 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
332 /// owned allocation to the map even when the map is shared.)
334 /// This must only fail if `alloc` contains provenance.
335 fn adjust_allocation<'b>(
336 ecx: &InterpCx<'mir, 'tcx, Self>,
338 alloc: Cow<'b, Allocation>,
339 kind: Option<MemoryKind<Self::MemoryKind>>,
340 ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
343 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
344 _template: &'tcx [InlineAsmTemplatePiece],
345 _operands: &[mir::InlineAsmOperand<'tcx>],
346 _options: InlineAsmOptions,
347 ) -> InterpResult<'tcx> {
348 throw_unsup_format!("inline assembly is not supported")
351 /// Hook for performing extra checks on a memory read access.
353 /// Takes read-only access to the allocation so we can keep all the memory read
354 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
357 fn before_memory_read(
360 _alloc_extra: &Self::AllocExtra,
361 _prov: (AllocId, Self::ProvenanceExtra),
363 ) -> InterpResult<'tcx> {
367 /// Hook for performing extra checks on a memory write access.
369 fn before_memory_write(
372 _alloc_extra: &mut Self::AllocExtra,
373 _prov: (AllocId, Self::ProvenanceExtra),
375 ) -> InterpResult<'tcx> {
379 /// Hook for performing extra operations on a memory deallocation.
381 fn before_memory_deallocation(
384 _alloc_extra: &mut Self::AllocExtra,
385 _prov: (AllocId, Self::ProvenanceExtra),
387 ) -> InterpResult<'tcx> {
391 /// Executes a retagging operation for a single pointer.
392 /// Returns the possibly adjusted pointer.
395 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
396 _kind: mir::RetagKind,
397 val: &ImmTy<'tcx, Self::Provenance>,
398 ) -> InterpResult<'tcx, ImmTy<'tcx, Self::Provenance>> {
402 /// Executes a retagging operation on a compound value.
403 /// Replaces all pointers stored in the given place.
405 fn retag_place_contents(
406 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
407 _kind: mir::RetagKind,
408 _place: &PlaceTy<'tcx, Self::Provenance>,
409 ) -> InterpResult<'tcx> {
413 /// Called immediately before a new stack frame gets pushed.
415 ecx: &mut InterpCx<'mir, 'tcx, Self>,
416 frame: Frame<'mir, 'tcx, Self::Provenance>,
417 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
419 /// Borrow the current thread's stack.
421 ecx: &'a InterpCx<'mir, 'tcx, Self>,
422 ) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>];
424 /// Mutably borrow the current thread's stack.
426 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
427 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
429 /// Called immediately after a stack frame got pushed and its locals got initialized.
430 fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
434 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
435 /// The `locals` have already been destroyed!
437 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
438 _frame: Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
440 ) -> InterpResult<'tcx, StackPopJump> {
441 // By default, we do not support unwinding from panics
443 Ok(StackPopJump::Normal)
447 /// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
448 /// (CTFE and ConstProp) use the same instance. Here, we share that code.
449 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
450 type Provenance = AllocId;
451 type ProvenanceExtra = ();
456 rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
457 const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
459 type AllocExtra = ();
460 type FrameExtra = ();
463 fn use_addr_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
464 // We do not support `use_addr`.
469 fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
475 _ecx: &mut InterpCx<$mir, $tcx, Self>,
478 _args: &[OpTy<$tcx>],
479 _destination: &PlaceTy<$tcx, Self::Provenance>,
480 _target: Option<mir::BasicBlock>,
481 _unwind: StackPopUnwind,
482 ) -> InterpResult<$tcx> {
487 fn adjust_allocation<'b>(
488 _ecx: &InterpCx<$mir, $tcx, Self>,
490 alloc: Cow<'b, Allocation>,
491 _kind: Option<MemoryKind<Self::MemoryKind>>,
492 ) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
496 fn extern_static_base_pointer(
497 ecx: &InterpCx<$mir, $tcx, Self>,
499 ) -> InterpResult<$tcx, Pointer> {
500 // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
501 Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
505 fn adjust_alloc_base_pointer(
506 _ecx: &InterpCx<$mir, $tcx, Self>,
507 ptr: Pointer<AllocId>,
508 ) -> Pointer<AllocId> {
513 fn ptr_from_addr_cast(
514 _ecx: &InterpCx<$mir, $tcx, Self>,
516 ) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
517 // Allow these casts, but make the pointer not dereferenceable.
518 // (I.e., they behave like transmutation.)
519 // This is correct because no pointers can ever be exposed in compile-time evaluation.
520 Ok(Pointer::from_addr(addr))
525 _ecx: &InterpCx<$mir, $tcx, Self>,
526 ptr: Pointer<AllocId>,
527 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
528 // We know `offset` is relative to the allocation, so we can use `into_parts`.
529 let (alloc_id, offset) = ptr.into_parts();
530 Some((alloc_id, offset, ()))