rustc_const_eval/interpret/
machine.rs

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.
4
5use std::borrow::{Borrow, Cow};
6use std::fmt::Debug;
7use std::hash::Hash;
8
9use rustc_abi::{Align, Size};
10use rustc_apfloat::{Float, FloatConvert};
11use rustc_middle::query::TyCtxtAt;
12use rustc_middle::ty::Ty;
13use rustc_middle::ty::layout::TyAndLayout;
14use rustc_middle::{mir, ty};
15use rustc_span::Span;
16use rustc_span::def_id::DefId;
17use rustc_target::callconv::FnAbi;
18
19use super::{
20    AllocBytes, AllocId, AllocKind, AllocRange, Allocation, CTFE_ALLOC_SALT, ConstAllocation,
21    CtfeProvenance, FnArg, Frame, ImmTy, InterpCx, InterpResult, MPlaceTy, MemoryKind,
22    Misalignment, OpTy, PlaceTy, Pointer, Provenance, RangeSet, interp_ok, throw_unsup,
23};
24
25/// Data returned by [`Machine::after_stack_pop`], and consumed by
26/// [`InterpCx::return_from_current_stack_frame`] to determine what actions should be done when
27/// returning from a stack frame.
28#[derive(Eq, PartialEq, Debug, Copy, Clone)]
29pub enum ReturnAction {
30    /// Indicates that no special handling should be
31    /// done - we'll either return normally or unwind
32    /// based on the terminator for the function
33    /// we're leaving.
34    Normal,
35
36    /// Indicates that we should *not* jump to the return/unwind address, as the callback already
37    /// took care of everything.
38    NoJump,
39
40    /// Returned by [`InterpCx::pop_stack_frame_raw`] when no cleanup should be done.
41    NoCleanup,
42}
43
44/// Whether this kind of memory is allowed to leak
45pub trait MayLeak: Copy {
46    fn may_leak(self) -> bool;
47}
48
49/// The functionality needed by memory to manage its allocations
50pub trait AllocMap<K: Hash + Eq, V> {
51    /// Tests if the map contains the given key.
52    /// Deliberately takes `&mut` because that is sufficient, and some implementations
53    /// can be more efficient then (using `RefCell::get_mut`).
54    fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
55    where
56        K: Borrow<Q>;
57
58    /// Callers should prefer [`AllocMap::contains_key`] when it is possible to call because it may
59    /// be more efficient. This function exists for callers that only have a shared reference
60    /// (which might make it slightly less efficient than `contains_key`, e.g. if
61    /// the data is stored inside a `RefCell`).
62    fn contains_key_ref<Q: ?Sized + Hash + Eq>(&self, k: &Q) -> bool
63    where
64        K: Borrow<Q>;
65
66    /// Inserts a new entry into the map.
67    fn insert(&mut self, k: K, v: V) -> Option<V>;
68
69    /// Removes an entry from the map.
70    fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
71    where
72        K: Borrow<Q>;
73
74    /// Returns data based on the keys and values in the map.
75    fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
76
77    /// Returns a reference to entry `k`. If no such entry exists, call
78    /// `vacant` and either forward its error, or add its result to the map
79    /// and return a reference to *that*.
80    fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
81
82    /// Returns a mutable reference to entry `k`. If no such entry exists, call
83    /// `vacant` and either forward its error, or add its result to the map
84    /// and return a reference to *that*.
85    fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
86
87    /// Read-only lookup.
88    fn get(&self, k: K) -> Option<&V> {
89        self.get_or(k, || Err(())).ok()
90    }
91
92    /// Mutable lookup.
93    fn get_mut(&mut self, k: K) -> Option<&mut V> {
94        self.get_mut_or(k, || Err(())).ok()
95    }
96}
97
98/// Methods of this trait signifies a point where CTFE evaluation would fail
99/// and some use case dependent behaviour can instead be applied.
100pub trait Machine<'tcx>: Sized {
101    /// Additional memory kinds a machine wishes to distinguish from the builtin ones
102    type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
103
104    /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
105    type Provenance: Provenance + Eq + Hash + 'static;
106
107    /// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
108    /// that is passed to memory access hooks so they can do things with it.
109    type ProvenanceExtra: Copy + 'static;
110
111    /// Machines can define extra (non-instance) things that represent values of function pointers.
112    /// For example, Miri uses this to return a function pointer from `dlsym`
113    /// that can later be called to execute the right thing.
114    type ExtraFnVal: Debug + Copy;
115
116    /// Extra data stored in every call frame.
117    type FrameExtra;
118
119    /// Extra data stored in every allocation.
120    type AllocExtra: Debug + Clone + 'tcx;
121
122    /// Type for the bytes of the allocation.
123    type Bytes: AllocBytes + 'static;
124
125    /// Memory's allocation map
126    type MemoryMap: AllocMap<
127            AllocId,
128            (
129                MemoryKind<Self::MemoryKind>,
130                Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>,
131            ),
132        > + Default
133        + Clone;
134
135    /// The memory kind to use for copied global memory (held in `tcx`) --
136    /// or None if such memory should not be mutated and thus any such attempt will cause
137    /// a `ModifiedStatic` error to be raised.
138    /// Statics are copied under two circumstances: When they are mutated, and when
139    /// `adjust_allocation` (see below) returns an owned allocation
140    /// that is added to the memory so that the work is not done twice.
141    const GLOBAL_KIND: Option<Self::MemoryKind>;
142
143    /// Should the machine panic on allocation failures?
144    const PANIC_ON_ALLOC_FAIL: bool;
145
146    /// Determines whether `eval_mir_constant` can never fail because all required consts have
147    /// already been checked before.
148    const ALL_CONSTS_ARE_PRECHECKED: bool = true;
149
150    /// Whether memory accesses should be alignment-checked.
151    fn enforce_alignment(ecx: &InterpCx<'tcx, Self>) -> bool;
152
153    /// Gives the machine a chance to detect more misalignment than the built-in checks would catch.
154    #[inline(always)]
155    fn alignment_check(
156        _ecx: &InterpCx<'tcx, Self>,
157        _alloc_id: AllocId,
158        _alloc_align: Align,
159        _alloc_kind: AllocKind,
160        _offset: Size,
161        _align: Align,
162    ) -> Option<Misalignment> {
163        None
164    }
165
166    /// Whether to enforce the validity invariant for a specific layout.
167    fn enforce_validity(ecx: &InterpCx<'tcx, Self>, layout: TyAndLayout<'tcx>) -> bool;
168    /// Whether to enforce the validity invariant *recursively*.
169    fn enforce_validity_recursively(
170        _ecx: &InterpCx<'tcx, Self>,
171        _layout: TyAndLayout<'tcx>,
172    ) -> bool {
173        false
174    }
175
176    /// Whether Assert(OverflowNeg) and Assert(Overflow) MIR terminators should actually
177    /// check for overflow.
178    fn ignore_optional_overflow_checks(_ecx: &InterpCx<'tcx, Self>) -> bool;
179
180    /// Entry point for obtaining the MIR of anything that should get evaluated.
181    /// So not just functions and shims, but also const/static initializers, anonymous
182    /// constants, ...
183    fn load_mir(
184        ecx: &InterpCx<'tcx, Self>,
185        instance: ty::InstanceKind<'tcx>,
186    ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
187        interp_ok(ecx.tcx.instance_mir(instance))
188    }
189
190    /// Entry point to all function calls.
191    ///
192    /// Returns either the mir to use for the call, or `None` if execution should
193    /// just proceed (which usually means this hook did all the work that the
194    /// called function should usually have done). In the latter case, it is
195    /// this hook's responsibility to advance the instruction pointer!
196    /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
197    /// nor just jump to `ret`, but instead push their own stack frame.)
198    /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
199    /// was used.
200    fn find_mir_or_eval_fn(
201        ecx: &mut InterpCx<'tcx, Self>,
202        instance: ty::Instance<'tcx>,
203        abi: &FnAbi<'tcx, Ty<'tcx>>,
204        args: &[FnArg<'tcx, Self::Provenance>],
205        destination: &MPlaceTy<'tcx, Self::Provenance>,
206        target: Option<mir::BasicBlock>,
207        unwind: mir::UnwindAction,
208    ) -> InterpResult<'tcx, Option<(&'tcx mir::Body<'tcx>, ty::Instance<'tcx>)>>;
209
210    /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
211    /// pointer as appropriate.
212    fn call_extra_fn(
213        ecx: &mut InterpCx<'tcx, Self>,
214        fn_val: Self::ExtraFnVal,
215        abi: &FnAbi<'tcx, Ty<'tcx>>,
216        args: &[FnArg<'tcx, Self::Provenance>],
217        destination: &MPlaceTy<'tcx, Self::Provenance>,
218        target: Option<mir::BasicBlock>,
219        unwind: mir::UnwindAction,
220    ) -> InterpResult<'tcx>;
221
222    /// Directly process an intrinsic without pushing a stack frame. It is the hook's
223    /// responsibility to advance the instruction pointer as appropriate.
224    ///
225    /// Returns `None` if the intrinsic was fully handled.
226    /// Otherwise, returns an `Instance` of the function that implements the intrinsic.
227    fn call_intrinsic(
228        ecx: &mut InterpCx<'tcx, Self>,
229        instance: ty::Instance<'tcx>,
230        args: &[OpTy<'tcx, Self::Provenance>],
231        destination: &MPlaceTy<'tcx, Self::Provenance>,
232        target: Option<mir::BasicBlock>,
233        unwind: mir::UnwindAction,
234    ) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>>;
235
236    /// Check whether the given function may be executed on the current machine, in terms of the
237    /// target features is requires.
238    fn check_fn_target_features(
239        _ecx: &InterpCx<'tcx, Self>,
240        _instance: ty::Instance<'tcx>,
241    ) -> InterpResult<'tcx>;
242
243    /// Called to evaluate `Assert` MIR terminators that trigger a panic.
244    fn assert_panic(
245        ecx: &mut InterpCx<'tcx, Self>,
246        msg: &mir::AssertMessage<'tcx>,
247        unwind: mir::UnwindAction,
248    ) -> InterpResult<'tcx>;
249
250    /// Called to trigger a non-unwinding panic.
251    fn panic_nounwind(_ecx: &mut InterpCx<'tcx, Self>, msg: &str) -> InterpResult<'tcx>;
252
253    /// Called when unwinding reached a state where execution should be terminated.
254    fn unwind_terminate(
255        ecx: &mut InterpCx<'tcx, Self>,
256        reason: mir::UnwindTerminateReason,
257    ) -> InterpResult<'tcx>;
258
259    /// Called for all binary operations where the LHS has pointer type.
260    ///
261    /// Returns a (value, overflowed) pair if the operation succeeded
262    fn binary_ptr_op(
263        ecx: &InterpCx<'tcx, Self>,
264        bin_op: mir::BinOp,
265        left: &ImmTy<'tcx, Self::Provenance>,
266        right: &ImmTy<'tcx, Self::Provenance>,
267    ) -> InterpResult<'tcx, ImmTy<'tcx, Self::Provenance>>;
268
269    /// Generate the NaN returned by a float operation, given the list of inputs.
270    /// (This is all inputs, not just NaN inputs!)
271    fn generate_nan<F1: Float + FloatConvert<F2>, F2: Float>(
272        _ecx: &InterpCx<'tcx, Self>,
273        _inputs: &[F1],
274    ) -> F2 {
275        // By default we always return the preferred NaN.
276        F2::NAN
277    }
278
279    /// Determines the result of `min`/`max` on floats when the arguments are equal.
280    fn equal_float_min_max<F: Float>(_ecx: &InterpCx<'tcx, Self>, a: F, _b: F) -> F {
281        // By default, we pick the left argument.
282        a
283    }
284
285    /// Called before a basic block terminator is executed.
286    #[inline]
287    fn before_terminator(_ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
288        interp_ok(())
289    }
290
291    /// Determines the result of a `NullaryOp::UbChecks` invocation.
292    fn ub_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
293
294    /// Determines the result of a `NullaryOp::ContractChecks` invocation.
295    fn contract_checks(_ecx: &InterpCx<'tcx, Self>) -> InterpResult<'tcx, bool>;
296
297    /// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
298    /// You can use this to detect long or endlessly running programs.
299    #[inline]
300    fn increment_const_eval_counter(_ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
301        interp_ok(())
302    }
303
304    /// Called before a global allocation is accessed.
305    /// `def_id` is `Some` if this is the "lazy" allocation of a static.
306    #[inline]
307    fn before_access_global(
308        _tcx: TyCtxtAt<'tcx>,
309        _machine: &Self,
310        _alloc_id: AllocId,
311        _allocation: ConstAllocation<'tcx>,
312        _static_def_id: Option<DefId>,
313        _is_write: bool,
314    ) -> InterpResult<'tcx> {
315        interp_ok(())
316    }
317
318    /// Return the `AllocId` for the given thread-local static in the current thread.
319    fn thread_local_static_pointer(
320        _ecx: &mut InterpCx<'tcx, Self>,
321        def_id: DefId,
322    ) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
323        throw_unsup!(ThreadLocalStatic(def_id))
324    }
325
326    /// Return the `AllocId` for the given `extern static`.
327    fn extern_static_pointer(
328        ecx: &InterpCx<'tcx, Self>,
329        def_id: DefId,
330    ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
331
332    /// "Int-to-pointer cast"
333    fn ptr_from_addr_cast(
334        ecx: &InterpCx<'tcx, Self>,
335        addr: u64,
336    ) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
337
338    /// Marks a pointer as exposed, allowing its provenance
339    /// to be recovered. "Pointer-to-int cast"
340    fn expose_provenance(
341        ecx: &InterpCx<'tcx, Self>,
342        provenance: Self::Provenance,
343    ) -> InterpResult<'tcx>;
344
345    /// Convert a pointer with provenance into an allocation-offset pair and extra provenance info.
346    /// `size` says how many bytes of memory are expected at that pointer. The *sign* of `size` can
347    /// be used to disambiguate situations where a wildcard pointer sits right in between two
348    /// allocations.
349    ///
350    /// If `ptr.provenance.get_alloc_id()` is `Some(p)`, the returned `AllocId` must be `p`.
351    /// The resulting `AllocId` will just be used for that one step and the forgotten again
352    /// (i.e., we'll never turn the data returned here back into a `Pointer` that might be
353    /// stored in machine state).
354    ///
355    /// When this fails, that means the pointer does not point to a live allocation.
356    fn ptr_get_alloc(
357        ecx: &InterpCx<'tcx, Self>,
358        ptr: Pointer<Self::Provenance>,
359        size: i64,
360    ) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
361
362    /// Return a "root" pointer for the given allocation: the one that is used for direct
363    /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
364    ///
365    /// Not called on `extern` or thread-local statics (those use the methods above).
366    ///
367    /// `kind` is the kind of the allocation the pointer points to; it can be `None` when
368    /// it's a global and `GLOBAL_KIND` is `None`.
369    fn adjust_alloc_root_pointer(
370        ecx: &InterpCx<'tcx, Self>,
371        ptr: Pointer,
372        kind: Option<MemoryKind<Self::MemoryKind>>,
373    ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
374
375    /// Called to adjust global allocations to the Provenance and AllocExtra of this machine.
376    ///
377    /// If `alloc` contains pointers, then they are all pointing to globals.
378    ///
379    /// This should avoid copying if no work has to be done! If this returns an owned
380    /// allocation (because a copy had to be done to adjust things), machine memory will
381    /// cache the result. (This relies on `AllocMap::get_or` being able to add the
382    /// owned allocation to the map even when the map is shared.)
383    fn adjust_global_allocation<'b>(
384        ecx: &InterpCx<'tcx, Self>,
385        id: AllocId,
386        alloc: &'b Allocation,
387    ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
388
389    /// Initialize the extra state of an allocation local to this machine.
390    ///
391    /// This is guaranteed to be called exactly once on all allocations local to this machine.
392    /// It will not be called automatically for global allocations; `adjust_global_allocation`
393    /// has to do that itself if that is desired.
394    fn init_local_allocation(
395        ecx: &InterpCx<'tcx, Self>,
396        id: AllocId,
397        kind: MemoryKind<Self::MemoryKind>,
398        size: Size,
399        align: Align,
400    ) -> InterpResult<'tcx, Self::AllocExtra>;
401
402    /// Hook for performing extra checks on a memory read access.
403    /// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
404    /// `range`.
405    ///
406    /// This will *not* be called during validation!
407    ///
408    /// Takes read-only access to the allocation so we can keep all the memory read
409    /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
410    /// need to mutate.
411    ///
412    /// This is not invoked for ZST accesses, as no read actually happens.
413    #[inline(always)]
414    fn before_memory_read(
415        _tcx: TyCtxtAt<'tcx>,
416        _machine: &Self,
417        _alloc_extra: &Self::AllocExtra,
418        _ptr: Pointer<Option<Self::Provenance>>,
419        _prov: (AllocId, Self::ProvenanceExtra),
420        _range: AllocRange,
421    ) -> InterpResult<'tcx> {
422        interp_ok(())
423    }
424
425    /// Hook for performing extra checks on any memory read access,
426    /// that involves an allocation, even ZST reads.
427    ///
428    /// This will *not* be called during validation!
429    ///
430    /// Used to prevent statics from self-initializing by reading from their own memory
431    /// as it is being initialized.
432    fn before_alloc_read(_ecx: &InterpCx<'tcx, Self>, _alloc_id: AllocId) -> InterpResult<'tcx> {
433        interp_ok(())
434    }
435
436    /// Hook for performing extra checks on a memory write access.
437    /// This is not invoked for ZST accesses, as no write actually happens.
438    /// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
439    /// `range`.
440    #[inline(always)]
441    fn before_memory_write(
442        _tcx: TyCtxtAt<'tcx>,
443        _machine: &mut Self,
444        _alloc_extra: &mut Self::AllocExtra,
445        _ptr: Pointer<Option<Self::Provenance>>,
446        _prov: (AllocId, Self::ProvenanceExtra),
447        _range: AllocRange,
448    ) -> InterpResult<'tcx> {
449        interp_ok(())
450    }
451
452    /// Hook for performing extra operations on a memory deallocation.
453    /// `ptr` will always be a pointer with the provenance in `prov` pointing to the beginning of
454    /// the allocation.
455    #[inline(always)]
456    fn before_memory_deallocation(
457        _tcx: TyCtxtAt<'tcx>,
458        _machine: &mut Self,
459        _alloc_extra: &mut Self::AllocExtra,
460        _ptr: Pointer<Option<Self::Provenance>>,
461        _prov: (AllocId, Self::ProvenanceExtra),
462        _size: Size,
463        _align: Align,
464        _kind: MemoryKind<Self::MemoryKind>,
465    ) -> InterpResult<'tcx> {
466        interp_ok(())
467    }
468
469    /// Executes a retagging operation for a single pointer.
470    /// Returns the possibly adjusted pointer.
471    #[inline]
472    fn retag_ptr_value(
473        _ecx: &mut InterpCx<'tcx, Self>,
474        _kind: mir::RetagKind,
475        val: &ImmTy<'tcx, Self::Provenance>,
476    ) -> InterpResult<'tcx, ImmTy<'tcx, Self::Provenance>> {
477        interp_ok(val.clone())
478    }
479
480    /// Executes a retagging operation on a compound value.
481    /// Replaces all pointers stored in the given place.
482    #[inline]
483    fn retag_place_contents(
484        _ecx: &mut InterpCx<'tcx, Self>,
485        _kind: mir::RetagKind,
486        _place: &PlaceTy<'tcx, Self::Provenance>,
487    ) -> InterpResult<'tcx> {
488        interp_ok(())
489    }
490
491    /// Called on places used for in-place function argument and return value handling.
492    ///
493    /// These places need to be protected to make sure the program cannot tell whether the
494    /// argument/return value was actually copied or passed in-place..
495    fn protect_in_place_function_argument(
496        ecx: &mut InterpCx<'tcx, Self>,
497        mplace: &MPlaceTy<'tcx, Self::Provenance>,
498    ) -> InterpResult<'tcx> {
499        // Without an aliasing model, all we can do is put `Uninit` into the place.
500        // Conveniently this also ensures that the place actually points to suitable memory.
501        ecx.write_uninit(mplace)
502    }
503
504    /// Called immediately before a new stack frame gets pushed.
505    fn init_frame(
506        ecx: &mut InterpCx<'tcx, Self>,
507        frame: Frame<'tcx, Self::Provenance>,
508    ) -> InterpResult<'tcx, Frame<'tcx, Self::Provenance, Self::FrameExtra>>;
509
510    /// Borrow the current thread's stack.
511    fn stack<'a>(
512        ecx: &'a InterpCx<'tcx, Self>,
513    ) -> &'a [Frame<'tcx, Self::Provenance, Self::FrameExtra>];
514
515    /// Mutably borrow the current thread's stack.
516    fn stack_mut<'a>(
517        ecx: &'a mut InterpCx<'tcx, Self>,
518    ) -> &'a mut Vec<Frame<'tcx, Self::Provenance, Self::FrameExtra>>;
519
520    /// Called immediately after a stack frame got pushed and its locals got initialized.
521    fn after_stack_push(_ecx: &mut InterpCx<'tcx, Self>) -> InterpResult<'tcx> {
522        interp_ok(())
523    }
524
525    /// Called just before the return value is copied to the caller-provided return place.
526    fn before_stack_pop(
527        _ecx: &InterpCx<'tcx, Self>,
528        _frame: &Frame<'tcx, Self::Provenance, Self::FrameExtra>,
529    ) -> InterpResult<'tcx> {
530        interp_ok(())
531    }
532
533    /// Called immediately after a stack frame got popped, but before jumping back to the caller.
534    /// The `locals` have already been destroyed!
535    #[inline(always)]
536    fn after_stack_pop(
537        _ecx: &mut InterpCx<'tcx, Self>,
538        _frame: Frame<'tcx, Self::Provenance, Self::FrameExtra>,
539        unwinding: bool,
540    ) -> InterpResult<'tcx, ReturnAction> {
541        // By default, we do not support unwinding from panics
542        assert!(!unwinding);
543        interp_ok(ReturnAction::Normal)
544    }
545
546    /// Called immediately after an "immediate" local variable is read in a given frame
547    /// (i.e., this is called for reads that do not end up accessing addressable memory).
548    #[inline(always)]
549    fn after_local_read(
550        _ecx: &InterpCx<'tcx, Self>,
551        _frame: &Frame<'tcx, Self::Provenance, Self::FrameExtra>,
552        _local: mir::Local,
553    ) -> InterpResult<'tcx> {
554        interp_ok(())
555    }
556
557    /// Called immediately after an "immediate" local variable is assigned a new value
558    /// (i.e., this is called for writes that do not end up in memory).
559    /// `storage_live` indicates whether this is the initial write upon `StorageLive`.
560    #[inline(always)]
561    fn after_local_write(
562        _ecx: &mut InterpCx<'tcx, Self>,
563        _local: mir::Local,
564        _storage_live: bool,
565    ) -> InterpResult<'tcx> {
566        interp_ok(())
567    }
568
569    /// Called immediately after actual memory was allocated for a local
570    /// but before the local's stack frame is updated to point to that memory.
571    #[inline(always)]
572    fn after_local_moved_to_memory(
573        _ecx: &mut InterpCx<'tcx, Self>,
574        _local: mir::Local,
575        _mplace: &MPlaceTy<'tcx, Self::Provenance>,
576    ) -> InterpResult<'tcx> {
577        interp_ok(())
578    }
579
580    /// Evaluate the given constant. The `eval` function will do all the required evaluation,
581    /// but this hook has the chance to do some pre/postprocessing.
582    #[inline(always)]
583    fn eval_mir_constant<F>(
584        ecx: &InterpCx<'tcx, Self>,
585        val: mir::Const<'tcx>,
586        span: Span,
587        layout: Option<TyAndLayout<'tcx>>,
588        eval: F,
589    ) -> InterpResult<'tcx, OpTy<'tcx, Self::Provenance>>
590    where
591        F: Fn(
592            &InterpCx<'tcx, Self>,
593            mir::Const<'tcx>,
594            Span,
595            Option<TyAndLayout<'tcx>>,
596        ) -> InterpResult<'tcx, OpTy<'tcx, Self::Provenance>>,
597    {
598        eval(ecx, val, span, layout)
599    }
600
601    /// Returns the salt to be used for a deduplicated global alloation.
602    /// If the allocation is for a function, the instance is provided as well
603    /// (this lets Miri ensure unique addresses for some functions).
604    fn get_global_alloc_salt(
605        ecx: &InterpCx<'tcx, Self>,
606        instance: Option<ty::Instance<'tcx>>,
607    ) -> usize;
608
609    fn cached_union_data_range<'e>(
610        _ecx: &'e mut InterpCx<'tcx, Self>,
611        _ty: Ty<'tcx>,
612        compute_range: impl FnOnce() -> RangeSet,
613    ) -> Cow<'e, RangeSet> {
614        // Default to no caching.
615        Cow::Owned(compute_range())
616    }
617}
618
619/// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
620/// (CTFE and ConstProp) use the same instance. Here, we share that code.
621pub macro compile_time_machine(<$tcx: lifetime>) {
622    type Provenance = CtfeProvenance;
623    type ProvenanceExtra = bool; // the "immutable" flag
624
625    type ExtraFnVal = !;
626
627    type MemoryMap =
628        rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
629    const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
630
631    type AllocExtra = ();
632    type FrameExtra = ();
633    type Bytes = Box<[u8]>;
634
635    #[inline(always)]
636    fn ignore_optional_overflow_checks(_ecx: &InterpCx<$tcx, Self>) -> bool {
637        false
638    }
639
640    #[inline(always)]
641    fn unwind_terminate(
642        _ecx: &mut InterpCx<$tcx, Self>,
643        _reason: mir::UnwindTerminateReason,
644    ) -> InterpResult<$tcx> {
645        unreachable!("unwinding cannot happen during compile-time evaluation")
646    }
647
648    #[inline(always)]
649    fn check_fn_target_features(
650        _ecx: &InterpCx<$tcx, Self>,
651        _instance: ty::Instance<$tcx>,
652    ) -> InterpResult<$tcx> {
653        // For now we don't do any checking here. We can't use `tcx.sess` because that can differ
654        // between crates, and we need to ensure that const-eval always behaves the same.
655        interp_ok(())
656    }
657
658    #[inline(always)]
659    fn call_extra_fn(
660        _ecx: &mut InterpCx<$tcx, Self>,
661        fn_val: !,
662        _abi: &FnAbi<$tcx, Ty<$tcx>>,
663        _args: &[FnArg<$tcx>],
664        _destination: &MPlaceTy<$tcx, Self::Provenance>,
665        _target: Option<mir::BasicBlock>,
666        _unwind: mir::UnwindAction,
667    ) -> InterpResult<$tcx> {
668        match fn_val {}
669    }
670
671    #[inline(always)]
672    fn ub_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
673        // We can't look at `tcx.sess` here as that can differ across crates, which can lead to
674        // unsound differences in evaluating the same constant at different instantiation sites.
675        interp_ok(true)
676    }
677
678    #[inline(always)]
679    fn contract_checks(_ecx: &InterpCx<$tcx, Self>) -> InterpResult<$tcx, bool> {
680        // We can't look at `tcx.sess` here as that can differ across crates, which can lead to
681        // unsound differences in evaluating the same constant at different instantiation sites.
682        interp_ok(true)
683    }
684
685    #[inline(always)]
686    fn adjust_global_allocation<'b>(
687        _ecx: &InterpCx<$tcx, Self>,
688        _id: AllocId,
689        alloc: &'b Allocation,
690    ) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
691        // Overwrite default implementation: no need to adjust anything.
692        interp_ok(Cow::Borrowed(alloc))
693    }
694
695    fn init_local_allocation(
696        _ecx: &InterpCx<$tcx, Self>,
697        _id: AllocId,
698        _kind: MemoryKind<Self::MemoryKind>,
699        _size: Size,
700        _align: Align,
701    ) -> InterpResult<$tcx, Self::AllocExtra> {
702        interp_ok(())
703    }
704
705    fn extern_static_pointer(
706        ecx: &InterpCx<$tcx, Self>,
707        def_id: DefId,
708    ) -> InterpResult<$tcx, Pointer> {
709        // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
710        interp_ok(Pointer::new(ecx.tcx.reserve_and_set_static_alloc(def_id).into(), Size::ZERO))
711    }
712
713    #[inline(always)]
714    fn adjust_alloc_root_pointer(
715        _ecx: &InterpCx<$tcx, Self>,
716        ptr: Pointer<CtfeProvenance>,
717        _kind: Option<MemoryKind<Self::MemoryKind>>,
718    ) -> InterpResult<$tcx, Pointer<CtfeProvenance>> {
719        interp_ok(ptr)
720    }
721
722    #[inline(always)]
723    fn ptr_from_addr_cast(
724        _ecx: &InterpCx<$tcx, Self>,
725        addr: u64,
726    ) -> InterpResult<$tcx, Pointer<Option<CtfeProvenance>>> {
727        // Allow these casts, but make the pointer not dereferenceable.
728        // (I.e., they behave like transmutation.)
729        // This is correct because no pointers can ever be exposed in compile-time evaluation.
730        interp_ok(Pointer::from_addr_invalid(addr))
731    }
732
733    #[inline(always)]
734    fn ptr_get_alloc(
735        _ecx: &InterpCx<$tcx, Self>,
736        ptr: Pointer<CtfeProvenance>,
737        _size: i64,
738    ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
739        // We know `offset` is relative to the allocation, so we can use `into_parts`.
740        let (prov, offset) = ptr.into_parts();
741        Some((prov.alloc_id(), offset, prov.immutable()))
742    }
743
744    #[inline(always)]
745    fn get_global_alloc_salt(
746        _ecx: &InterpCx<$tcx, Self>,
747        _instance: Option<ty::Instance<$tcx>>,
748    ) -> usize {
749        CTFE_ALLOC_SALT
750    }
751}