]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_const_eval/src/interpret/machine.rs
New upstream version 1.57.0+dfsg1
[rustc.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};
11 use rustc_span::def_id::DefId;
12 use rustc_target::abi::Size;
13 use rustc_target::spec::abi::Abi;
14
15 use super::{
16 AllocId, AllocRange, Allocation, Frame, ImmTy, InterpCx, InterpResult, LocalValue, MemPlace,
17 Memory, 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 PointerTag: Provenance + Eq + Hash + 'static;
89
90 /// Machines can define extra (non-instance) things that represent values of function pointers.
91 /// For example, Miri uses this to return a function pointer from `dlsym`
92 /// that can later be called to execute the right thing.
93 type ExtraFnVal: Debug + Copy;
94
95 /// Extra data stored in every call frame.
96 type FrameExtra;
97
98 /// Extra data stored in memory. A reference to this is available when `AllocExtra`
99 /// gets initialized, so you can e.g., have an `Rc` here if there is global state you
100 /// need access to in the `AllocExtra` hooks.
101 type MemoryExtra;
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::PointerTag, 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 /// `tag_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(memory_extra: &Self::MemoryExtra) -> bool;
126
127 /// Whether, when checking alignment, we should `force_int` and thus support
128 /// custom alignment logic based on whatever the integer address happens to be.
129 fn force_int_for_alignment_check(memory_extra: &Self::MemoryExtra) -> bool;
130
131 /// Whether to enforce the validity invariant
132 fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
133
134 /// Whether function calls should be [ABI](Abi)-checked.
135 fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
136 true
137 }
138
139 /// Entry point for obtaining the MIR of anything that should get evaluated.
140 /// So not just functions and shims, but also const/static initializers, anonymous
141 /// constants, ...
142 fn load_mir(
143 ecx: &InterpCx<'mir, 'tcx, Self>,
144 instance: ty::InstanceDef<'tcx>,
145 ) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
146 Ok(ecx.tcx.instance_mir(instance))
147 }
148
149 /// Entry point to all function calls.
150 ///
151 /// Returns either the mir to use for the call, or `None` if execution should
152 /// just proceed (which usually means this hook did all the work that the
153 /// called function should usually have done). In the latter case, it is
154 /// this hook's responsibility to advance the instruction pointer!
155 /// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
156 /// nor just jump to `ret`, but instead push their own stack frame.)
157 /// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
158 /// was used.
159 fn find_mir_or_eval_fn(
160 ecx: &mut InterpCx<'mir, 'tcx, Self>,
161 instance: ty::Instance<'tcx>,
162 abi: Abi,
163 args: &[OpTy<'tcx, Self::PointerTag>],
164 ret: Option<(&PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
165 unwind: StackPopUnwind,
166 ) -> InterpResult<'tcx, Option<&'mir mir::Body<'tcx>>>;
167
168 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
169 /// pointer as appropriate.
170 fn call_extra_fn(
171 ecx: &mut InterpCx<'mir, 'tcx, Self>,
172 fn_val: Self::ExtraFnVal,
173 abi: Abi,
174 args: &[OpTy<'tcx, Self::PointerTag>],
175 ret: Option<(&PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
176 unwind: StackPopUnwind,
177 ) -> InterpResult<'tcx>;
178
179 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
180 /// responsibility to advance the instruction pointer as appropriate.
181 fn call_intrinsic(
182 ecx: &mut InterpCx<'mir, 'tcx, Self>,
183 instance: ty::Instance<'tcx>,
184 args: &[OpTy<'tcx, Self::PointerTag>],
185 ret: Option<(&PlaceTy<'tcx, Self::PointerTag>, mir::BasicBlock)>,
186 unwind: StackPopUnwind,
187 ) -> InterpResult<'tcx>;
188
189 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
190 fn assert_panic(
191 ecx: &mut InterpCx<'mir, 'tcx, Self>,
192 msg: &mir::AssertMessage<'tcx>,
193 unwind: Option<mir::BasicBlock>,
194 ) -> InterpResult<'tcx>;
195
196 /// Called to evaluate `Abort` MIR terminator.
197 fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
198 throw_unsup_format!("aborting execution is not supported")
199 }
200
201 /// Called for all binary operations where the LHS has pointer type.
202 ///
203 /// Returns a (value, overflowed) pair if the operation succeeded
204 fn binary_ptr_op(
205 ecx: &InterpCx<'mir, 'tcx, Self>,
206 bin_op: mir::BinOp,
207 left: &ImmTy<'tcx, Self::PointerTag>,
208 right: &ImmTy<'tcx, Self::PointerTag>,
209 ) -> InterpResult<'tcx, (Scalar<Self::PointerTag>, bool, Ty<'tcx>)>;
210
211 /// Heap allocations via the `box` keyword.
212 fn box_alloc(
213 ecx: &mut InterpCx<'mir, 'tcx, Self>,
214 dest: &PlaceTy<'tcx, Self::PointerTag>,
215 ) -> InterpResult<'tcx>;
216
217 /// Called to read the specified `local` from the `frame`.
218 /// Since reading a ZST is not actually accessing memory or locals, this is never invoked
219 /// for ZST reads.
220 #[inline]
221 fn access_local(
222 _ecx: &InterpCx<'mir, 'tcx, Self>,
223 frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
224 local: mir::Local,
225 ) -> InterpResult<'tcx, Operand<Self::PointerTag>> {
226 frame.locals[local].access()
227 }
228
229 /// Called to write the specified `local` from the `frame`.
230 /// Since writing a ZST is not actually accessing memory or locals, this is never invoked
231 /// for ZST reads.
232 #[inline]
233 fn access_local_mut<'a>(
234 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
235 frame: usize,
236 local: mir::Local,
237 ) -> InterpResult<'tcx, Result<&'a mut LocalValue<Self::PointerTag>, MemPlace<Self::PointerTag>>>
238 where
239 'tcx: 'mir,
240 {
241 ecx.stack_mut()[frame].locals[local].access_mut()
242 }
243
244 /// Called before a basic block terminator is executed.
245 /// You can use this to detect endlessly running programs.
246 #[inline]
247 fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
248 Ok(())
249 }
250
251 /// Called before a global allocation is accessed.
252 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
253 #[inline]
254 fn before_access_global(
255 _memory_extra: &Self::MemoryExtra,
256 _alloc_id: AllocId,
257 _allocation: &Allocation,
258 _static_def_id: Option<DefId>,
259 _is_write: bool,
260 ) -> InterpResult<'tcx> {
261 Ok(())
262 }
263
264 /// Return the `AllocId` for the given thread-local static in the current thread.
265 fn thread_local_static_base_pointer(
266 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
267 def_id: DefId,
268 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>> {
269 throw_unsup!(ThreadLocalStatic(def_id))
270 }
271
272 /// Return the root pointer for the given `extern static`.
273 fn extern_static_base_pointer(
274 mem: &Memory<'mir, 'tcx, Self>,
275 def_id: DefId,
276 ) -> InterpResult<'tcx, Pointer<Self::PointerTag>>;
277
278 /// Return a "base" pointer for the given allocation: the one that is used for direct
279 /// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
280 ///
281 /// Not called on `extern` or thread-local statics (those use the methods above).
282 fn tag_alloc_base_pointer(
283 mem: &Memory<'mir, 'tcx, Self>,
284 ptr: Pointer,
285 ) -> Pointer<Self::PointerTag>;
286
287 /// "Int-to-pointer cast"
288 fn ptr_from_addr(
289 mem: &Memory<'mir, 'tcx, Self>,
290 addr: u64,
291 ) -> Pointer<Option<Self::PointerTag>>;
292
293 /// Convert a pointer with provenance into an allocation-offset pair.
294 fn ptr_get_alloc(
295 mem: &Memory<'mir, 'tcx, Self>,
296 ptr: Pointer<Self::PointerTag>,
297 ) -> (AllocId, Size);
298
299 /// Called to initialize the "extra" state of an allocation and make the pointers
300 /// it contains (in relocations) tagged. The way we construct allocations is
301 /// to always first construct it without extra and then add the extra.
302 /// This keeps uniform code paths for handling both allocations created by CTFE
303 /// for globals, and allocations created by Miri during evaluation.
304 ///
305 /// `kind` is the kind of the allocation being tagged; it can be `None` when
306 /// it's a global and `GLOBAL_KIND` is `None`.
307 ///
308 /// This should avoid copying if no work has to be done! If this returns an owned
309 /// allocation (because a copy had to be done to add tags or metadata), machine memory will
310 /// cache the result. (This relies on `AllocMap::get_or` being able to add the
311 /// owned allocation to the map even when the map is shared.)
312 fn init_allocation_extra<'b>(
313 mem: &Memory<'mir, 'tcx, Self>,
314 id: AllocId,
315 alloc: Cow<'b, Allocation>,
316 kind: Option<MemoryKind<Self::MemoryKind>>,
317 ) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>;
318
319 /// Hook for performing extra checks on a memory read access.
320 ///
321 /// Takes read-only access to the allocation so we can keep all the memory read
322 /// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
323 /// need to mutate.
324 #[inline(always)]
325 fn memory_read(
326 _memory_extra: &Self::MemoryExtra,
327 _alloc_extra: &Self::AllocExtra,
328 _tag: Self::PointerTag,
329 _range: AllocRange,
330 ) -> InterpResult<'tcx> {
331 Ok(())
332 }
333
334 /// Hook for performing extra checks on a memory write access.
335 #[inline(always)]
336 fn memory_written(
337 _memory_extra: &mut Self::MemoryExtra,
338 _alloc_extra: &mut Self::AllocExtra,
339 _tag: Self::PointerTag,
340 _range: AllocRange,
341 ) -> InterpResult<'tcx> {
342 Ok(())
343 }
344
345 /// Hook for performing extra operations on a memory deallocation.
346 #[inline(always)]
347 fn memory_deallocated(
348 _memory_extra: &mut Self::MemoryExtra,
349 _alloc_extra: &mut Self::AllocExtra,
350 _tag: Self::PointerTag,
351 _range: AllocRange,
352 ) -> InterpResult<'tcx> {
353 Ok(())
354 }
355
356 /// Executes a retagging operation.
357 #[inline]
358 fn retag(
359 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
360 _kind: mir::RetagKind,
361 _place: &PlaceTy<'tcx, Self::PointerTag>,
362 ) -> InterpResult<'tcx> {
363 Ok(())
364 }
365
366 /// Called immediately before a new stack frame gets pushed.
367 fn init_frame_extra(
368 ecx: &mut InterpCx<'mir, 'tcx, Self>,
369 frame: Frame<'mir, 'tcx, Self::PointerTag>,
370 ) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>>;
371
372 /// Borrow the current thread's stack.
373 fn stack(
374 ecx: &'a InterpCx<'mir, 'tcx, Self>,
375 ) -> &'a [Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>];
376
377 /// Mutably borrow the current thread's stack.
378 fn stack_mut(
379 ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
380 ) -> &'a mut Vec<Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>>;
381
382 /// Called immediately after a stack frame got pushed and its locals got initialized.
383 fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
384 Ok(())
385 }
386
387 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
388 fn after_stack_pop(
389 _ecx: &mut InterpCx<'mir, 'tcx, Self>,
390 _frame: Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
391 _unwinding: bool,
392 ) -> InterpResult<'tcx, StackPopJump> {
393 // By default, we do not support unwinding from panics
394 Ok(StackPopJump::Normal)
395 }
396 }
397
398 // A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
399 // (CTFE and ConstProp) use the same instance. Here, we share that code.
400 pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
401 type PointerTag = AllocId;
402 type ExtraFnVal = !;
403
404 type MemoryMap =
405 rustc_data_structures::fx::FxHashMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
406 const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
407
408 type AllocExtra = ();
409 type FrameExtra = ();
410
411 #[inline(always)]
412 fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
413 // We do not check for alignment to avoid having to carry an `Align`
414 // in `ConstValue::ByRef`.
415 false
416 }
417
418 #[inline(always)]
419 fn force_int_for_alignment_check(_memory_extra: &Self::MemoryExtra) -> bool {
420 // We do not support `force_int`.
421 false
422 }
423
424 #[inline(always)]
425 fn enforce_validity(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
426 false // for now, we don't enforce validity
427 }
428
429 #[inline(always)]
430 fn call_extra_fn(
431 _ecx: &mut InterpCx<$mir, $tcx, Self>,
432 fn_val: !,
433 _abi: Abi,
434 _args: &[OpTy<$tcx>],
435 _ret: Option<(&PlaceTy<$tcx>, mir::BasicBlock)>,
436 _unwind: StackPopUnwind,
437 ) -> InterpResult<$tcx> {
438 match fn_val {}
439 }
440
441 #[inline(always)]
442 fn init_allocation_extra<'b>(
443 _mem: &Memory<$mir, $tcx, Self>,
444 _id: AllocId,
445 alloc: Cow<'b, Allocation>,
446 _kind: Option<MemoryKind<Self::MemoryKind>>,
447 ) -> Cow<'b, Allocation<Self::PointerTag>> {
448 // We do not use a tag so we can just cheaply forward the allocation
449 alloc
450 }
451
452 fn extern_static_base_pointer(
453 mem: &Memory<$mir, $tcx, Self>,
454 def_id: DefId,
455 ) -> InterpResult<$tcx, Pointer> {
456 // Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
457 Ok(Pointer::new(mem.tcx.create_static_alloc(def_id), Size::ZERO))
458 }
459
460 #[inline(always)]
461 fn tag_alloc_base_pointer(
462 _mem: &Memory<$mir, $tcx, Self>,
463 ptr: Pointer<AllocId>,
464 ) -> Pointer<AllocId> {
465 ptr
466 }
467
468 #[inline(always)]
469 fn ptr_from_addr(_mem: &Memory<$mir, $tcx, Self>, addr: u64) -> Pointer<Option<AllocId>> {
470 Pointer::new(None, Size::from_bytes(addr))
471 }
472
473 #[inline(always)]
474 fn ptr_get_alloc(_mem: &Memory<$mir, $tcx, Self>, ptr: Pointer<AllocId>) -> (AllocId, Size) {
475 // We know `offset` is relative to the allocation, so we can use `into_parts`.
476 let (alloc_id, offset) = ptr.into_parts();
477 (alloc_id, offset)
478 }
479 }