]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/machine.rs
Rollup merge of #100350 - jhpratt:stringify-vis, r=cjgillot
[rust.git] / compiler / rustc_const_eval / src / 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
5 use std::borrow::{Borrow, Cow};
6 use std::fmt::Debug;
7 use std::hash::Hash;
8
9 use rustc_middle::mir;
10 use rustc_middle::ty::{self, Ty, TyCtxt};
11 use rustc_span::def_id::DefId;
12 use rustc_target::abi::Size;
13 use rustc_target::spec::abi::Abi as CallAbi;
14
15 use super::{
16     AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
17     MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
18 };
19
20 /// Data returned by Machine::stack_pop,
21 /// to provide further control over the popping of the stack frame
22 #[derive(Eq, PartialEq, Debug, Copy, Clone)]
23 pub enum StackPopJump {
24     /// Indicates that no special handling should be
25     /// done - we'll either return normally or unwind
26     /// based on the terminator for the function
27     /// we're leaving.
28     Normal,
29
30     /// Indicates that we should *not* jump to the return/unwind address, as the callback already
31     /// took care of everything.
32     NoJump,
33 }
34
35 /// Whether this kind of memory is allowed to leak
36 pub trait MayLeak: Copy {
37     fn may_leak(self) -> bool;
38 }
39
40 /// The functionality needed by memory to manage its allocations
41 pub trait AllocMap<K: Hash + Eq, V> {
42     /// Tests if the map contains the given key.
43     /// Deliberately takes `&mut` because that is sufficient, and some implementations
44     /// can be more efficient then (using `RefCell::get_mut`).
45     fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
46     where
47         K: Borrow<Q>;
48
49     /// Inserts a new entry into the map.
50     fn insert(&mut self, k: K, v: V) -> Option<V>;
51
52     /// Removes an entry from the map.
53     fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
54     where
55         K: Borrow<Q>;
56
57     /// Returns data based on the keys and values in the map.
58     fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
59
60     /// Returns a reference to entry `k`. If no such entry exists, call
61     /// `vacant` and either forward its error, or add its result to the map
62     /// and return a reference to *that*.
63     fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
64
65     /// Returns a mutable reference to entry `k`. If no such entry exists, call
66     /// `vacant` and either forward its error, or add its result to the map
67     /// and return a reference to *that*.
68     fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
69
70     /// Read-only lookup.
71     fn get(&self, k: K) -> Option<&V> {
72         self.get_or(k, || Err(())).ok()
73     }
74
75     /// Mutable lookup.
76     fn get_mut(&mut self, k: K) -> Option<&mut V> {
77         self.get_mut_or(k, || Err(())).ok()
78     }
79 }
80
81 /// Methods of this trait signifies a point where CTFE evaluation would fail
82 /// and some use case dependent behaviour can instead be applied.
83 pub trait Machine<'mir, 'tcx>: Sized {
84     /// Additional memory kinds a machine wishes to distinguish from the builtin ones
85     type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
86
87     /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
88     type Provenance: Provenance + Eq + Hash + 'static;
89
90     /// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
91     /// that is passed to memory access hooks so they can do things with it.
92     type ProvenanceExtra: Copy + 'static;
93
94     /// Machines can define extra (non-instance) things that represent values of function pointers.
95     /// For example, Miri uses this to return a function pointer from `dlsym`
96     /// that can later be called to execute the right thing.
97     type ExtraFnVal: Debug + Copy;
98
99     /// Extra data stored in every call frame.
100     type FrameExtra;
101
102     /// Extra data stored in every allocation.
103     type AllocExtra: Debug + Clone + 'static;
104
105     /// Memory's allocation map
106     type MemoryMap: AllocMap<
107             AllocId,
108             (MemoryKind<Self::MemoryKind>, Allocation<Self::Provenance, Self::AllocExtra>),
109         > + Default
110         + Clone;
111
112     /// The memory kind to use for copied global memory (held in `tcx`) --
113     /// or None if such memory should not be mutated and thus any such attempt will cause
114     /// a `ModifiedStatic` error to be raised.
115     /// Statics are copied under two circumstances: When they are mutated, and when
116     /// `adjust_allocation` (see below) returns an owned allocation
117     /// that is added to the memory so that the work is not done twice.
118     const GLOBAL_KIND: Option<Self::MemoryKind>;
119
120     /// Should the machine panic on allocation failures?
121     const PANIC_ON_ALLOC_FAIL: bool;
122
123     /// Whether memory accesses should be alignment-checked.
124     fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
125
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     ///
129     /// Requires Provenance::OFFSET_IS_ADDR to be true.
130     fn force_int_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
131
132     /// Whether to enforce the validity invariant
133     fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
134
135     /// Whether to enforce integers and floats being initialized.
136     fn enforce_number_init(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
137
138     /// Whether function calls should be [ABI](CallAbi)-checked.
139     fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
140         true
141     }
142
143     /// Whether CheckedBinOp MIR statements should actually check for overflow.
144     fn checked_binop_checks_overflow(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
145
146     /// Entry point for obtaining the MIR of anything that should get evaluated.
147     /// So not just functions and shims, but also const/static initializers, anonymous
148     /// constants, ...
149     fn load_mir(
150         ecx: &InterpCx<'mir, 'tcx, Self>,
151         instance: ty::InstanceDef<'tcx>,
152     ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
153         Ok(ecx.tcx.instance_mir(instance))
154     }
155
156     /// Entry point to all function calls.
157     ///
158     /// Returns either the mir to use for the call, or `None` if execution should
159     /// just proceed (which usually means this hook did all the work that the
160     /// called function should usually have done). In the latter case, it is
161     /// this hook's responsibility to advance the instruction pointer!
162     /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
163     /// nor just jump to `ret`, but instead push their own stack frame.)
164     /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
165     /// was used.
166     fn find_mir_or_eval_fn(
167         ecx: &mut InterpCx<'mir, 'tcx, Self>,
168         instance: ty::Instance<'tcx>,
169         abi: CallAbi,
170         args: &[OpTy<'tcx, Self::Provenance>],
171         destination: &PlaceTy<'tcx, Self::Provenance>,
172         target: Option<mir::BasicBlock>,
173         unwind: StackPopUnwind,
174     ) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
175
176     /// Execute `fn_val`.  It is the hook's responsibility to advance the instruction
177     /// pointer as appropriate.
178     fn call_extra_fn(
179         ecx: &mut InterpCx<'mir, 'tcx, Self>,
180         fn_val: Self::ExtraFnVal,
181         abi: CallAbi,
182         args: &[OpTy<'tcx, Self::Provenance>],
183         destination: &PlaceTy<'tcx, Self::Provenance>,
184         target: Option<mir::BasicBlock>,
185         unwind: StackPopUnwind,
186     ) -> InterpResult<'tcx>;
187
188     /// Directly process an intrinsic without pushing a stack frame. It is the hook's
189     /// responsibility to advance the instruction pointer as appropriate.
190     fn call_intrinsic(
191         ecx: &mut InterpCx<'mir, 'tcx, Self>,
192         instance: ty::Instance<'tcx>,
193         args: &[OpTy<'tcx, Self::Provenance>],
194         destination: &PlaceTy<'tcx, Self::Provenance>,
195         target: Option<mir::BasicBlock>,
196         unwind: StackPopUnwind,
197     ) -> InterpResult<'tcx>;
198
199     /// Called to evaluate `Assert` MIR terminators that trigger a panic.
200     fn assert_panic(
201         ecx: &mut InterpCx<'mir, 'tcx, Self>,
202         msg: &mir::AssertMessage<'tcx>,
203         unwind: Option<mir::BasicBlock>,
204     ) -> InterpResult<'tcx>;
205
206     /// Called to evaluate `Abort` MIR terminator.
207     fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
208         throw_unsup_format!("aborting execution is not supported")
209     }
210
211     /// Called for all binary operations where the LHS has pointer type.
212     ///
213     /// Returns a (value, overflowed) pair if the operation succeeded
214     fn binary_ptr_op(
215         ecx: &InterpCx<'mir, 'tcx, Self>,
216         bin_op: mir::BinOp,
217         left: &ImmTy<'tcx, Self::Provenance>,
218         right: &ImmTy<'tcx, Self::Provenance>,
219     ) -> InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)>;
220
221     /// Called to read the specified `local` from the `frame`.
222     /// Since reading a ZST is not actually accessing memory or locals, this is never invoked
223     /// for ZST reads.
224     #[inline]
225     fn access_local<'a>(
226         frame: &'a Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
227         local: mir::Local,
228     ) -> InterpResult<'tcx, &'a Operand<Self::Provenance>>
229     where
230         'tcx: 'mir,
231     {
232         frame.locals[local].access()
233     }
234
235     /// Called to write the specified `local` from the `frame`.
236     /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
237     /// for ZST reads.
238     #[inline]
239     fn access_local_mut<'a>(
240         ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
241         frame: usize,
242         local: mir::Local,
243     ) -> InterpResult<'tcx, &'a mut Operand<Self::Provenance>>
244     where
245         'tcx: 'mir,
246     {
247         ecx.stack_mut()[frame].locals[local].access_mut()
248     }
249
250     /// Called before a basic block terminator is executed.
251     /// You can use this to detect endlessly running programs.
252     #[inline]
253     fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
254         Ok(())
255     }
256
257     /// Called before a global allocation is accessed.
258     /// `def_id` is `Some` if this is the "lazy" allocation of a static.
259     #[inline]
260     fn before_access_global(
261         _tcx: TyCtxt<'tcx>,
262         _machine: &Self,
263         _alloc_id: AllocId,
264         _allocation: ConstAllocation<'tcx>,
265         _static_def_id: Option<DefId>,
266         _is_write: bool,
267     ) -> InterpResult<'tcx> {
268         Ok(())
269     }
270
271     /// Return the `AllocId` for the given thread-local static in the current thread.
272     fn thread_local_static_base_pointer(
273         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
274         def_id: DefId,
275     ) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
276         throw_unsup!(ThreadLocalStatic(def_id))
277     }
278
279     /// Return the root pointer for the given `extern static`.
280     fn extern_static_base_pointer(
281         ecx: &InterpCx<'mir, 'tcx, Self>,
282         def_id: DefId,
283     ) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
284
285     /// Return a "base" pointer for the given allocation: the one that is used for direct
286     /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
287     ///
288     /// Not called on `extern` or thread-local statics (those use the methods above).
289     fn adjust_alloc_base_pointer(
290         ecx: &InterpCx<'mir, 'tcx, Self>,
291         ptr: Pointer,
292     ) -> Pointer<Self::Provenance>;
293
294     /// "Int-to-pointer cast"
295     fn ptr_from_addr_cast(
296         ecx: &InterpCx<'mir, 'tcx, Self>,
297         addr: u64,
298     ) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
299
300     /// Marks a pointer as exposed, allowing it's provenance
301     /// to be recovered. "Pointer-to-int cast"
302     fn expose_ptr(
303         ecx: &mut InterpCx<'mir, 'tcx, Self>,
304         ptr: Pointer<Self::Provenance>,
305     ) -> InterpResult<'tcx>;
306
307     /// Convert a pointer with provenance into an allocation-offset pair
308     /// and extra provenance info.
309     ///
310     /// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
311     ///
312     /// When this fails, that means the pointer does not point to a live allocation.
313     fn ptr_get_alloc(
314         ecx: &InterpCx<'mir, 'tcx, Self>,
315         ptr: Pointer<Self::Provenance>,
316     ) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
317
318     /// Called to adjust allocations to the Provenance and AllocExtra of this machine.
319     ///
320     /// The way we construct allocations is to always first construct it without extra and then add
321     /// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
322     /// globals, and allocations created by Miri during evaluation.
323     ///
324     /// `kind` is the kind of the allocation being adjusted; it can be `None` when
325     /// it's a global and `GLOBAL_KIND` is `None`.
326     ///
327     /// This should avoid copying if no work has to be done! If this returns an owned
328     /// allocation (because a copy had to be done to adjust things), machine memory will
329     /// cache the result. (This relies on `AllocMap::get_or` being able to add the
330     /// owned allocation to the map even when the map is shared.)
331     ///
332     /// This must only fail if `alloc` contains relocations.
333     fn adjust_allocation<'b>(
334         ecx: &InterpCx<'mir, 'tcx, Self>,
335         id: AllocId,
336         alloc: Cow<'b, Allocation>,
337         kind: Option<MemoryKind<Self::MemoryKind>>,
338     ) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra>>>;
339
340     /// Hook for performing extra checks on a memory read access.
341     ///
342     /// Takes read-only access to the allocation so we can keep all the memory read
343     /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
344     /// need to mutate.
345     #[inline(always)]
346     fn memory_read(
347         _tcx: TyCtxt<'tcx>,
348         _machine: &Self,
349         _alloc_extra: &Self::AllocExtra,
350         _prov: (AllocId, Self::ProvenanceExtra),
351         _range: AllocRange,
352     ) -> InterpResult<'tcx> {
353         Ok(())
354     }
355
356     /// Hook for performing extra checks on a memory write access.
357     #[inline(always)]
358     fn memory_written(
359         _tcx: TyCtxt<'tcx>,
360         _machine: &mut Self,
361         _alloc_extra: &mut Self::AllocExtra,
362         _prov: (AllocId, Self::ProvenanceExtra),
363         _range: AllocRange,
364     ) -> InterpResult<'tcx> {
365         Ok(())
366     }
367
368     /// Hook for performing extra operations on a memory deallocation.
369     #[inline(always)]
370     fn memory_deallocated(
371         _tcx: TyCtxt<'tcx>,
372         _machine: &mut Self,
373         _alloc_extra: &mut Self::AllocExtra,
374         _prov: (AllocId, Self::ProvenanceExtra),
375         _range: AllocRange,
376     ) -> InterpResult<'tcx> {
377         Ok(())
378     }
379
380     /// Executes a retagging operation.
381     #[inline]
382     fn retag(
383         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
384         _kind: mir::RetagKind,
385         _place: &PlaceTy<'tcx, Self::Provenance>,
386     ) -> InterpResult<'tcx> {
387         Ok(())
388     }
389
390     /// Called immediately before a new stack frame gets pushed.
391     fn init_frame_extra(
392         ecx: &mut InterpCx<'mir, 'tcx, Self>,
393         frame: Frame<'mir, 'tcx, Self::Provenance>,
394     ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
395
396     /// Borrow the current thread's stack.
397     fn stack<'a>(
398         ecx: &'a InterpCx<'mir, 'tcx, Self>,
399     ) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>];
400
401     /// Mutably borrow the current thread's stack.
402     fn stack_mut<'a>(
403         ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
404     ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
405
406     /// Called immediately after a stack frame got pushed and its locals got initialized.
407     fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
408         Ok(())
409     }
410
411     /// Called immediately after a stack frame got popped, but before jumping back to the caller.
412     /// The `locals` have already been destroyed!
413     fn after_stack_pop(
414         _ecx: &mut InterpCx<'mir, 'tcx, Self>,
415         _frame: Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
416         unwinding: bool,
417     ) -> InterpResult<'tcx, StackPopJump> {
418         // By default, we do not support unwinding from panics
419         assert!(!unwinding);
420         Ok(StackPopJump::Normal)
421     }
422 }
423
424 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
425 // (CTFE and ConstProp) use the same instance.  Here, we share that code.
426 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
427     type Provenance = AllocId;
428     type ProvenanceExtra = ();
429
430     type ExtraFnVal = !;
431
432     type MemoryMap =
433         rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
434     const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
435
436     type AllocExtra = ();
437     type FrameExtra = ();
438
439     #[inline(always)]
440     fn enforce_alignment(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
441         // We do not check for alignment to avoid having to carry an `Align`
442         // in `ConstValue::ByRef`.
443         false
444     }
445
446     #[inline(always)]
447     fn force_int_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
448         // We do not support `force_int`.
449         false
450     }
451
452     #[inline(always)]
453     fn enforce_validity(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
454         false // for now, we don't enforce validity
455     }
456
457     #[inline(always)]
458     fn enforce_number_init(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
459         true
460     }
461
462     #[inline(always)]
463     fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
464         true
465     }
466
467     #[inline(always)]
468     fn call_extra_fn(
469         _ecx: &mut InterpCx<$mir, $tcx, Self>,
470         fn_val: !,
471         _abi: CallAbi,
472         _args: &[OpTy<$tcx>],
473         _destination: &PlaceTy<$tcx, Self::Provenance>,
474         _target: Option<mir::BasicBlock>,
475         _unwind: StackPopUnwind,
476     ) -> InterpResult<$tcx> {
477         match fn_val {}
478     }
479
480     #[inline(always)]
481     fn adjust_allocation<'b>(
482         _ecx: &InterpCx<$mir, $tcx, Self>,
483         _id: AllocId,
484         alloc: Cow<'b, Allocation>,
485         _kind: Option<MemoryKind<Self::MemoryKind>>,
486     ) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
487         Ok(alloc)
488     }
489
490     fn extern_static_base_pointer(
491         ecx: &InterpCx<$mir, $tcx, Self>,
492         def_id: DefId,
493     ) -> InterpResult<$tcx, Pointer> {
494         // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
495         Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
496     }
497
498     #[inline(always)]
499     fn adjust_alloc_base_pointer(
500         _ecx: &InterpCx<$mir, $tcx, Self>,
501         ptr: Pointer<AllocId>,
502     ) -> Pointer<AllocId> {
503         ptr
504     }
505
506     #[inline(always)]
507     fn ptr_from_addr_cast(
508         _ecx: &InterpCx<$mir, $tcx, Self>,
509         addr: u64,
510     ) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
511         // Allow these casts, but make the pointer not dereferenceable.
512         // (I.e., they behave like transmutation.)
513         Ok(Pointer::from_addr(addr))
514     }
515
516     #[inline(always)]
517     fn ptr_get_alloc(
518         _ecx: &InterpCx<$mir, $tcx, Self>,
519         ptr: Pointer<AllocId>,
520     ) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
521         // We know `offset` is relative to the allocation, so we can use `into_parts`.
522         let (alloc_id, offset) = ptr.into_parts();
523         Some((alloc_id, offset, ()))
524     }
525 }