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