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