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::Size;
14 use rustc_target::spec::abi::Abi as CallAbi;
17 AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
18 MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
21 /// Data returned by Machine::stack_pop,
22 /// to provide further control over the popping of the stack frame
23 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
24 pub enum StackPopJump {
25 /// Indicates that no special handling should be
26 /// done - we'll either return normally or unwind
27 /// based on the terminator for the function
31 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
32 /// took care of everything.
36 /// Whether this kind of memory is allowed to leak
37 pub trait MayLeak: Copy {
38 fn may_leak(self) -> bool;
41 /// The functionality needed by memory to manage its allocations
42 pub trait AllocMap<K: Hash + Eq, V> {
43 /// Tests if the map contains the given key.
44 /// Deliberately takes `&mut` because that is sufficient, and some implementations
45 /// can be more efficient then (using `RefCell::get_mut`).
46 fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
50 /// Inserts a new entry into the map.
51 fn insert(&mut self, k: K, v: V) -> Option<V>;
53 /// Removes an entry from the map.
54 fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
58 /// Returns data based on the keys and values in the map.
59 fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
61 /// Returns a reference to entry `k`. If no such entry exists, call
62 /// `vacant` and either forward its error, or add its result to the map
63 /// and return a reference to *that*.
64 fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
66 /// Returns a mutable reference to entry `k`. If no such entry exists, call
67 /// `vacant` and either forward its error, or add its result to the map
68 /// and return a reference to *that*.
69 fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
72 fn get(&self, k: K) -> Option<&V> {
73 self.get_or(k, || Err(())).ok()
77 fn get_mut(&mut self, k: K) -> Option<&mut V> {
78 self.get_mut_or(k, || Err(())).ok()
82 /// Methods of this trait signifies a point where CTFE evaluation would fail
83 /// and some use case dependent behaviour can instead be applied.
84 pub trait Machine<'mir, 'tcx>: Sized {
85 /// Additional memory kinds a machine wishes to distinguish from the builtin ones
86 type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
88 /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
89 type Provenance: Provenance + Eq + Hash + 'static;
91 /// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
92 /// that is passed to memory access hooks so they can do things with it.
93 type ProvenanceExtra: Copy + 'static;
95 /// Machines can define extra (non-instance) things that represent values of function pointers.
96 /// For example, Miri uses this to return a function pointer from `dlsym`
97 /// that can later be called to execute the right thing.
98 type ExtraFnVal: Debug + Copy;
100 /// Extra data stored in every call frame.
103 /// Extra data stored in every allocation.
104 type AllocExtra: Debug + Clone + 'static;
106 /// Memory's allocation map
107 type MemoryMap: AllocMap<
109 (MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
113 /// The memory kind to use for copied global memory (held in `tcx`) --
114 /// or None if such memory should not be mutated and thus any such attempt will cause
115 /// a `ModifiedStatic` error to be raised.
116 /// Statics are copied under two circumstances: When they are mutated, and when
117 /// `adjust_allocation` (see below) returns an owned allocation
118 /// that is added to the memory so that the work is not done twice.
119 const GLOBAL_KIND: Option<Self::MemoryKind>;
121 /// Should the machine panic on allocation failures?
122 const PANIC_ON_ALLOC_FAIL: bool;
124 /// Whether memory accesses should be alignment-checked.
125 fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
127 /// Whether, when checking alignment, we should look at the actual address and thus support
128 /// custom alignment logic based on whatever the integer address happens to be.
130 /// If this returns true, Provenance::OFFSET_IS_ADDR must be true.
131 fn use_addr_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
133 /// Whether to enforce the validity invariant
134 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
136 /// Whether function calls should be [ABI](CallAbi)-checked.
137 fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
141 /// Whether CheckedBinOp MIR statements should actually check for overflow.
142 fn checked_binop_checks_overflow(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
144 /// Entry point for obtaining the MIR of anything that should get evaluated.
145 /// So not just functions and shims, but also const/static initializers, anonymous
148 ecx: &InterpCx<'mir, 'tcx, Self>,
149 instance: ty::InstanceDef<'tcx>,
150 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
151 Ok(ecx.tcx.instance_mir(instance))
154 /// Entry point to all function calls.
156 /// Returns either the mir to use for the call, or `None` if execution should
157 /// just proceed (which usually means this hook did all the work that the
158 /// called function should usually have done). In the latter case, it is
159 /// this hook's responsibility to advance the instruction pointer!
160 /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
161 /// nor just jump to `ret`, but instead push their own stack frame.)
162 /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
164 fn find_mir_or_eval_fn(
165 ecx: &mut InterpCx<'mir, 'tcx, Self>,
166 instance: ty::Instance<'tcx>,
168 args: &[OpTy<'tcx, Self::Provenance>],
169 destination: &PlaceTy<'tcx, Self::Provenance>,
170 target: Option<mir::BasicBlock>,
171 unwind: StackPopUnwind,
172 ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
174 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
175 /// pointer as appropriate.
177 ecx: &mut InterpCx<'mir, 'tcx, Self>,
178 fn_val: Self::ExtraFnVal,
180 args: &[OpTy<'tcx, Self::Provenance>],
181 destination: &PlaceTy<'tcx, Self::Provenance>,
182 target: Option<mir::BasicBlock>,
183 unwind: StackPopUnwind,
184 ) -> InterpResult<'tcx>;
186 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
187 /// responsibility to advance the instruction pointer as appropriate.
189 ecx: &mut InterpCx<'mir, 'tcx, Self>,
190 instance: ty::Instance<'tcx>,
191 args: &[OpTy<'tcx, Self::Provenance>],
192 destination: &PlaceTy<'tcx, Self::Provenance>,
193 target: Option<mir::BasicBlock>,
194 unwind: StackPopUnwind,
195 ) -> InterpResult<'tcx>;
197 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
199 ecx: &mut InterpCx<'mir, 'tcx, Self>,
200 msg: &mir::AssertMessage<'tcx>,
201 unwind: Option<mir::BasicBlock>,
202 ) -> InterpResult<'tcx>;
204 /// Called to evaluate `Abort` MIR terminator.
205 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
206 throw_unsup_format!("aborting execution is not supported")
209 /// Called for all binary operations where the LHS has pointer type.
211 /// Returns a (value, overflowed) pair if the operation succeeded
213 ecx: &InterpCx<'mir, 'tcx, Self>,
215 left: &ImmTy<'tcx, Self::Provenance>,
216 right: &ImmTy<'tcx, Self::Provenance>,
217 ) -> InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)>;
219 /// Called to write the specified `local` from the `frame`.
220 /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
223 /// Due to borrow checker trouble, we indicate the `frame` as an index rather than an `&mut
226 fn access_local_mut<'a>(
227 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
230 ) -> InterpResult<'tcx, &'a mut Operand<Self::Provenance>>
234 ecx.stack_mut()[frame].locals[local].access_mut()
237 /// Called before a basic block terminator is executed.
238 /// You can use this to detect endlessly running programs.
240 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
244 /// Called before a global allocation is accessed.
245 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
247 fn before_access_global(
251 _allocation: ConstAllocation<'tcx>,
252 _static_def_id: Option<DefId>,
254 ) -> InterpResult<'tcx> {
258 /// Return the `AllocId` for the given thread-local static in the current thread.
259 fn thread_local_static_base_pointer(
260 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
262 ) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
263 throw_unsup!(ThreadLocalStatic(def_id))
266 /// Return the root pointer for the given `extern static`.
267 fn extern_static_base_pointer(
268 ecx: &InterpCx<'mir, 'tcx, Self>,
270 ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
272 /// Return a "base" pointer for the given allocation: the one that is used for direct
273 /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
275 /// Not called on `extern` or thread-local statics (those use the methods above).
276 fn adjust_alloc_base_pointer(
277 ecx: &InterpCx<'mir, 'tcx, Self>,
279 ) -> Pointer<Self::Provenance>;
281 /// "Int-to-pointer cast"
282 fn ptr_from_addr_cast(
283 ecx: &InterpCx<'mir, 'tcx, Self>,
285 ) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
287 /// Marks a pointer as exposed, allowing it's provenance
288 /// to be recovered. "Pointer-to-int cast"
290 ecx: &mut InterpCx<'mir, 'tcx, Self>,
291 ptr: Pointer<Self::Provenance>,
292 ) -> InterpResult<'tcx>;
294 /// Convert a pointer with provenance into an allocation-offset pair
295 /// and extra provenance info.
297 /// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
299 /// When this fails, that means the pointer does not point to a live allocation.
301 ecx: &InterpCx<'mir, 'tcx, Self>,
302 ptr: Pointer<Self::Provenance>,
303 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
305 /// Called to adjust allocations to the Provenance and AllocExtra of this machine.
307 /// The way we construct allocations is to always first construct it without extra and then add
308 /// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
309 /// globals, and allocations created by Miri during evaluation.
311 /// `kind` is the kind of the allocation being adjusted; it can be `None` when
312 /// it's a global and `GLOBAL_KIND` is `None`.
314 /// This should avoid copying if no work has to be done! If this returns an owned
315 /// allocation (because a copy had to be done to adjust things), machine memory will
316 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
317 /// owned allocation to the map even when the map is shared.)
319 /// This must only fail if `alloc` contains provenance.
320 fn adjust_allocation<'b>(
321 ecx: &InterpCx<'mir, 'tcx, Self>,
323 alloc: Cow<'b, Allocation>,
324 kind: Option<MemoryKind<Self::MemoryKind>>,
325 ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
328 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
329 _template: &'tcx [InlineAsmTemplatePiece],
330 _operands: &[mir::InlineAsmOperand<'tcx>],
331 _options: InlineAsmOptions,
332 ) -> InterpResult<'tcx> {
333 throw_unsup_format!("inline assembly is not supported")
336 /// Hook for performing extra checks on a memory read access.
338 /// Takes read-only access to the allocation so we can keep all the memory read
339 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
342 fn before_memory_read(
345 _alloc_extra: &Self::AllocExtra,
346 _prov: (AllocId, Self::ProvenanceExtra),
348 ) -> InterpResult<'tcx> {
352 /// Hook for performing extra checks on a memory write access.
354 fn before_memory_write(
357 _alloc_extra: &mut Self::AllocExtra,
358 _prov: (AllocId, Self::ProvenanceExtra),
360 ) -> InterpResult<'tcx> {
364 /// Hook for performing extra operations on a memory deallocation.
366 fn before_memory_deallocation(
369 _alloc_extra: &mut Self::AllocExtra,
370 _prov: (AllocId, Self::ProvenanceExtra),
372 ) -> InterpResult<'tcx> {
376 /// Executes a retagging operation.
379 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
380 _kind: mir::RetagKind,
381 _place: &PlaceTy<'tcx, Self::Provenance>,
382 ) -> InterpResult<'tcx> {
386 /// Called immediately before a new stack frame gets pushed.
388 ecx: &mut InterpCx<'mir, 'tcx, Self>,
389 frame: Frame<'mir, 'tcx, Self::Provenance>,
390 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
392 /// Borrow the current thread's stack.
394 ecx: &'a InterpCx<'mir, 'tcx, Self>,
395 ) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>];
397 /// Mutably borrow the current thread's stack.
399 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
400 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
402 /// Called immediately after a stack frame got pushed and its locals got initialized.
403 fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
407 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
408 /// The `locals` have already been destroyed!
410 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
411 _frame: Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
413 ) -> InterpResult<'tcx, StackPopJump> {
414 // By default, we do not support unwinding from panics
416 Ok(StackPopJump::Normal)
420 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
421 // (CTFE and ConstProp) use the same instance. Here, we share that code.
422 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
423 type Provenance = AllocId;
424 type ProvenanceExtra = ();
429 rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
430 const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
432 type AllocExtra = ();
433 type FrameExtra = ();
436 fn use_addr_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
437 // We do not support `use_addr`.
442 fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
448 _ecx: &mut InterpCx<$mir, $tcx, Self>,
451 _args: &[OpTy<$tcx>],
452 _destination: &PlaceTy<$tcx, Self::Provenance>,
453 _target: Option<mir::BasicBlock>,
454 _unwind: StackPopUnwind,
455 ) -> InterpResult<$tcx> {
460 fn adjust_allocation<'b>(
461 _ecx: &InterpCx<$mir, $tcx, Self>,
463 alloc: Cow<'b, Allocation>,
464 _kind: Option<MemoryKind<Self::MemoryKind>>,
465 ) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
469 fn extern_static_base_pointer(
470 ecx: &InterpCx<$mir, $tcx, Self>,
472 ) -> InterpResult<$tcx, Pointer> {
473 // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
474 Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
478 fn adjust_alloc_base_pointer(
479 _ecx: &InterpCx<$mir, $tcx, Self>,
480 ptr: Pointer<AllocId>,
481 ) -> Pointer<AllocId> {
486 fn ptr_from_addr_cast(
487 _ecx: &InterpCx<$mir, $tcx, Self>,
489 ) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
490 // Allow these casts, but make the pointer not dereferenceable.
491 // (I.e., they behave like transmutation.)
492 // This is correct because no pointers can ever be exposed in compile-time evaluation.
493 Ok(Pointer::from_addr(addr))
498 _ecx: &InterpCx<$mir, $tcx, Self>,
499 ptr: Pointer<AllocId>,
500 ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
501 // We know `offset` is relative to the allocation, so we can use `into_parts`.
502 let (alloc_id, offset) = ptr.into_parts();
503 Some((alloc_id, offset, ()))