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};
11 use rustc_span::def_id::DefId;
14 AllocId, Allocation, AllocationExtra, CheckInAllocMsg, Frame, ImmTy, InterpCx, InterpResult,
15 LocalValue, MemPlace, Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Scalar,
18 /// Data returned by Machine::stack_pop,
19 /// to provide further control over the popping of the stack frame
20 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
21 pub enum StackPopJump {
22 /// Indicates that no special handling should be
23 /// done - we'll either return normally or unwind
24 /// based on the terminator for the function
28 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
29 /// took care of everything.
33 /// Whether this kind of memory is allowed to leak
34 pub trait MayLeak: Copy {
35 fn may_leak(self) -> bool;
38 /// The functionality needed by memory to manage its allocations
39 pub trait AllocMap<K: Hash + Eq, V> {
40 /// Tests if the map contains the given key.
41 /// Deliberately takes `&mut` because that is sufficient, and some implementations
42 /// can be more efficient then (using `RefCell::get_mut`).
43 fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
47 /// Inserts a new entry into the map.
48 fn insert(&mut self, k: K, v: V) -> Option<V>;
50 /// Removes an entry from the map.
51 fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
55 /// Returns data based on the keys and values in the map.
56 fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
58 /// Returns a reference to entry `k`. If no such entry exists, call
59 /// `vacant` and either forward its error, or add its result to the map
60 /// and return a reference to *that*.
61 fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
63 /// Returns a mutable 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_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
69 fn get(&self, k: K) -> Option<&V> {
70 self.get_or(k, || Err(())).ok()
74 fn get_mut(&mut self, k: K) -> Option<&mut V> {
75 self.get_mut_or(k, || Err(())).ok()
79 /// Methods of this trait signifies a point where CTFE evaluation would fail
80 /// and some use case dependent behaviour can instead be applied.
81 pub trait Machine<'mir, 'tcx>: Sized {
82 /// Additional memory kinds a machine wishes to distinguish from the builtin ones
83 type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
85 /// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
86 /// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.
87 /// The `default()` is used for pointers to consts, statics, vtables and functions.
88 /// The `Debug` formatting is used for displaying pointers; we cannot use `Display`
89 /// as `()` does not implement that, but it should be "nice" output.
90 type PointerTag: Debug + Copy + Eq + Hash + 'static;
92 /// Machines can define extra (non-instance) things that represent values of function pointers.
93 /// For example, Miri uses this to return a function pointer from `dlsym`
94 /// that can later be called to execute the right thing.
95 type ExtraFnVal: Debug + Copy;
97 /// Extra data stored in every call frame.
100 /// Extra data stored in memory. A reference to this is available when `AllocExtra`
101 /// gets initialized, so you can e.g., have an `Rc` here if there is global state you
102 /// need access to in the `AllocExtra` hooks.
105 /// Extra data stored in every allocation.
106 type AllocExtra: AllocationExtra<Self::PointerTag> + 'static;
108 /// Memory's allocation map
109 type MemoryMap: AllocMap<
111 (MemoryKind<Self::MemoryKind>, Allocation<Self::PointerTag, 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 /// `tag_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 /// Whether memory accesses should be alignment-checked.
124 fn enforce_alignment(memory_extra: &Self::MemoryExtra) -> 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.
128 fn force_int_for_alignment_check(memory_extra: &Self::MemoryExtra) -> bool;
130 /// Whether to enforce the validity invariant
131 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
133 /// Entry point to all function calls.
135 /// Returns either the mir to use for the call, or `None` if execution should
136 /// just proceed (which usually means this hook did all the work that the
137 /// called function should usually have done). In the latter case, it is
138 /// this hook's responsibility to advance the instruction pointer!
139 /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
140 /// nor just jump to `ret`, but instead push their own stack frame.)
141 /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
143 fn find_mir_or_eval_fn(
144 ecx: &mut InterpCx<'mir, 'tcx, Self>,
145 instance: ty::Instance<'tcx>,
146 args: &[OpTy<'tcx, Self::PointerTag>],
147 ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
148 unwind: Option<mir::BasicBlock>,
149 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>>;
151 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
152 /// pointer as appropriate.
154 ecx: &mut InterpCx<'mir, 'tcx, Self>,
155 fn_val: Self::ExtraFnVal,
156 args: &[OpTy<'tcx, Self::PointerTag>],
157 ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
158 unwind: Option<mir::BasicBlock>,
159 ) -> InterpResult<'tcx>;
161 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
162 /// responsibility to advance the instruction pointer as appropriate.
164 ecx: &mut InterpCx<'mir, 'tcx, Self>,
165 instance: ty::Instance<'tcx>,
166 args: &[OpTy<'tcx, Self::PointerTag>],
167 ret: Option<(PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
168 unwind: Option<mir::BasicBlock>,
169 ) -> InterpResult<'tcx>;
171 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
173 ecx: &mut InterpCx<'mir, 'tcx, Self>,
174 msg: &mir::AssertMessage<'tcx>,
175 unwind: Option<mir::BasicBlock>,
176 ) -> InterpResult<'tcx>;
178 /// Called to evaluate `Abort` MIR terminator.
179 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tcx, !> {
180 use crate::const_eval::ConstEvalErrKind;
182 Err(ConstEvalErrKind::Abort(msg).into())
185 /// Called for all binary operations where the LHS has pointer type.
187 /// Returns a (value, overflowed) pair if the operation succeeded
189 ecx: &InterpCx<'mir, 'tcx, Self>,
191 left: ImmTy<'tcx, Self::PointerTag>,
192 right: ImmTy<'tcx, Self::PointerTag>,
193 ) -> InterpResult<'tcx, (Scalar<Self::PointerTag>, bool, Ty<'tcx>)>;
195 /// Heap allocations via the `box` keyword.
197 ecx: &mut InterpCx<'mir, 'tcx, Self>,
198 dest: PlaceTy<'tcx, Self::PointerTag>,
199 ) -> InterpResult<'tcx>;
201 /// Called to read the specified `local` from the `frame`.
202 /// Since reading a ZST is not actually accessing memory or locals, this is never invoked
206 _ecx: &InterpCx<'mir, 'tcx, Self>,
207 frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
209 ) -> InterpResult<'tcx, Operand<Self::PointerTag>> {
210 frame.locals[local].access()
213 /// Called to write the specified `local` from the `frame`.
214 /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
217 fn access_local_mut<'a>(
218 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
221 ) -> InterpResult<'tcx, Result<&'a mut LocalValue<Self::PointerTag>, MemPlace<Self::PointerTag>>>
225 ecx.stack_mut()[frame].locals[local].access_mut()
228 /// Called before a basic block terminator is executed.
229 /// You can use this to detect endlessly running programs.
231 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
235 /// Called before a global allocation is accessed.
236 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
238 fn before_access_global(
239 _memory_extra: &Self::MemoryExtra,
241 _allocation: &Allocation,
242 _static_def_id: Option<DefId>,
244 ) -> InterpResult<'tcx> {
248 /// Return the `AllocId` for the given thread-local static in the current thread.
249 fn thread_local_static_alloc_id(
250 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
252 ) -> InterpResult<'tcx, AllocId> {
253 throw_unsup!(ThreadLocalStatic(def_id))
256 /// Return the `AllocId` backing the given `extern static`.
257 fn extern_static_alloc_id(
258 mem: &Memory<'mir, 'tcx, Self>,
260 ) -> InterpResult<'tcx, AllocId> {
261 // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
262 Ok(mem.tcx.create_static_alloc(def_id))
265 /// Return the "base" tag for the given *global* allocation: the one that is used for direct
266 /// accesses to this static/const/fn allocation. If `id` is not a global allocation,
267 /// this will return an unusable tag (i.e., accesses will be UB)!
269 /// Called on the id returned by `thread_local_static_alloc_id` and `extern_static_alloc_id`, if needed.
270 fn tag_global_base_pointer(memory_extra: &Self::MemoryExtra, id: AllocId) -> Self::PointerTag;
272 /// Called to initialize the "extra" state of an allocation and make the pointers
273 /// it contains (in relocations) tagged. The way we construct allocations is
274 /// to always first construct it without extra and then add the extra.
275 /// This keeps uniform code paths for handling both allocations created by CTFE
276 /// for globals, and allocations created by Miri during evaluation.
278 /// `kind` is the kind of the allocation being tagged; it can be `None` when
279 /// it's a global and `GLOBAL_KIND` is `None`.
281 /// This should avoid copying if no work has to be done! If this returns an owned
282 /// allocation (because a copy had to be done to add tags or metadata), machine memory will
283 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
284 /// owned allocation to the map even when the map is shared.)
286 /// Also return the "base" tag to use for this allocation: the one that is used for direct
287 /// accesses to this allocation. If `kind == STATIC_KIND`, this tag must be consistent
288 /// with `tag_global_base_pointer`.
289 fn init_allocation_extra<'b>(
290 memory_extra: &Self::MemoryExtra,
292 alloc: Cow<'b, Allocation>,
293 kind: Option<MemoryKind<Self::MemoryKind>>,
294 ) -> (Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>, Self::PointerTag);
296 /// Called to notify the machine before a deallocation occurs.
297 fn before_deallocation(
298 _memory_extra: &mut Self::MemoryExtra,
300 ) -> InterpResult<'tcx> {
304 /// Executes a retagging operation
307 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
308 _kind: mir::RetagKind,
309 _place: PlaceTy<'tcx, Self::PointerTag>,
310 ) -> InterpResult<'tcx> {
314 /// Called immediately before a new stack frame gets pushed.
316 ecx: &mut InterpCx<'mir, 'tcx, Self>,
317 frame: Frame<'mir, 'tcx, Self::PointerTag>,
318 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>>;
320 /// Borrow the current thread's stack.
322 ecx: &'a InterpCx<'mir, 'tcx, Self>,
323 ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>];
325 /// Mutably borrow the current thread's stack.
327 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
328 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>>;
330 /// Called immediately after a stack frame got pushed and its locals got initialized.
331 fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
335 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
337 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
338 _frame: Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
340 ) -> InterpResult<'tcx, StackPopJump> {
341 // By default, we do not support unwinding from panics
342 Ok(StackPopJump::Normal)
346 _mem: &Memory<'mir, 'tcx, Self>,
348 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
350 // This is UB, seriously.
351 err_ub!(DanglingIntPointer(0, CheckInAllocMsg::InboundsTest))
353 // This is just something we cannot support during const-eval.
354 err_unsup!(ReadBytesAsPointer)
360 _mem: &Memory<'mir, 'tcx, Self>,
361 _ptr: Pointer<Self::PointerTag>,
362 ) -> InterpResult<'tcx, u64>;
365 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
366 // (CTFE and ConstProp) use the same instance. Here, we share that code.
367 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
368 type PointerTag = ();
372 type MemoryMap = rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<!>, Allocation)>;
373 const GLOBAL_KIND: Option<!> = None; // no copying of globals from `tcx` to machine memory
375 type AllocExtra = ();
376 type FrameExtra = ();
379 fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
380 // We do not check for alignment to avoid having to carry an `Align`
381 // in `ConstValue::ByRef`.
386 fn force_int_for_alignment_check(_memory_extra: &Self::MemoryExtra) -> bool {
387 // We do not support `force_int`.
392 fn enforce_validity(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
393 false // for now, we don't enforce validity
398 _ecx: &mut InterpCx<$mir, $tcx, Self>,
400 _args: &[OpTy<$tcx>],
401 _ret: Option<(PlaceTy<$tcx>, mir::BasicBlock)>,
402 _unwind: Option<mir::BasicBlock>,
403 ) -> InterpResult<$tcx> {
408 fn init_allocation_extra<'b>(
409 _memory_extra: &Self::MemoryExtra,
411 alloc: Cow<'b, Allocation>,
412 _kind: Option<MemoryKind<!>>,
413 ) -> (Cow<'b, Allocation<Self::PointerTag>>, Self::PointerTag) {
414 // We do not use a tag so we can just cheaply forward the allocation
419 fn tag_global_base_pointer(
420 _memory_extra: &Self::MemoryExtra,
422 ) -> Self::PointerTag {