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