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