]>
git.proxmox.com Git - rustc.git/blob - 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.
5 use std
::borrow
::{Borrow, Cow}
;
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
;
16 AllocId
, AllocRange
, Allocation
, ConstAllocation
, Frame
, ImmTy
, InterpCx
, InterpResult
,
17 LocalValue
, MemPlace
, Memory
, MemoryKind
, OpTy
, Operand
, PlaceTy
, Pointer
, Provenance
, Scalar
,
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
31 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
32 /// took care of everything.
36 /// Whether this kind of memory is allowed to leak
37 pub trait MayLeak
: Copy
{
38 fn may_leak(self) -> bool
;
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
50 /// Inserts a new entry into the map.
51 fn insert(&mut self, k
: K
, v
: V
) -> Option
<V
>;
53 /// Removes an entry from the map.
54 fn remove
<Q
: ?Sized
+ Hash
+ Eq
>(&mut self, k
: &Q
) -> Option
<V
>
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
>;
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
>;
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
>;
72 fn get(&self, k
: K
) -> Option
<&V
> {
73 self.get_or(k
, || Err(())).ok()
77 fn get_mut(&mut self, k
: K
) -> Option
<&mut V
> {
78 self.get_mut_or(k
, || Err(())).ok()
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;
88 /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
89 type PointerTag
: Provenance
+ Eq
+ Hash
+ '
static;
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
;
96 /// Extra data stored in every call frame.
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.
104 /// Extra data stored in every allocation.
105 type AllocExtra
: Debug
+ Clone
+ '
static;
107 /// Memory's allocation map
108 type MemoryMap
: AllocMap
<
110 (MemoryKind
<Self::MemoryKind
>, Allocation
<Self::PointerTag
, Self::AllocExtra
>),
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
>;
122 /// Should the machine panic on allocation failures?
123 const PANIC_ON_ALLOC_FAIL
: bool
;
125 /// Whether memory accesses should be alignment-checked.
126 fn enforce_alignment(memory_extra
: &Self::MemoryExtra
) -> bool
;
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
;
132 /// Whether to enforce the validity invariant
133 fn enforce_validity(ecx
: &InterpCx
<'mir
, 'tcx
, Self>) -> bool
;
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
;
139 /// Whether function calls should be [ABI](Abi)-checked.
140 fn enforce_abi(_ecx
: &InterpCx
<'mir
, 'tcx
, Self>) -> bool
{
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
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
))
154 /// Entry point to all function calls.
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
164 fn find_mir_or_eval_fn(
165 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
166 instance
: ty
::Instance
<'tcx
>,
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
>)>>;
173 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
174 /// pointer as appropriate.
176 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
177 fn_val
: Self::ExtraFnVal
,
179 args
: &[OpTy
<'tcx
, Self::PointerTag
>],
180 ret
: Option
<(&PlaceTy
<'tcx
, Self::PointerTag
>, mir
::BasicBlock
)>,
181 unwind
: StackPopUnwind
,
182 ) -> InterpResult
<'tcx
>;
184 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
185 /// responsibility to advance the instruction pointer as appropriate.
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
>;
194 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
196 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
197 msg
: &mir
::AssertMessage
<'tcx
>,
198 unwind
: Option
<mir
::BasicBlock
>,
199 ) -> InterpResult
<'tcx
>;
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")
206 /// Called for all binary operations where the LHS has pointer type.
208 /// Returns a (value, overflowed) pair if the operation succeeded
210 ecx
: &InterpCx
<'mir
, 'tcx
, Self>,
212 left
: &ImmTy
<'tcx
, Self::PointerTag
>,
213 right
: &ImmTy
<'tcx
, Self::PointerTag
>,
214 ) -> InterpResult
<'tcx
, (Scalar
<Self::PointerTag
>, bool
, Ty
<'tcx
>)>;
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
221 _ecx
: &InterpCx
<'mir
, 'tcx
, Self>,
222 frame
: &Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>,
224 ) -> InterpResult
<'tcx
, Operand
<Self::PointerTag
>> {
225 frame
.locals
[local
].access()
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
232 fn access_local_mut
<'a
>(
233 ecx
: &'a
mut InterpCx
<'mir
, 'tcx
, Self>,
236 ) -> InterpResult
<'tcx
, Result
<&'a
mut LocalValue
<Self::PointerTag
>, MemPlace
<Self::PointerTag
>>>
240 ecx
.stack_mut()[frame
].locals
[local
].access_mut()
243 /// Called before a basic block terminator is executed.
244 /// You can use this to detect endlessly running programs.
246 fn before_terminator(_ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>) -> InterpResult
<'tcx
> {
250 /// Called before a global allocation is accessed.
251 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
253 fn before_access_global(
254 _memory_extra
: &Self::MemoryExtra
,
256 _allocation
: ConstAllocation
<'tcx
>,
257 _static_def_id
: Option
<DefId
>,
259 ) -> InterpResult
<'tcx
> {
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>,
267 ) -> InterpResult
<'tcx
, Pointer
<Self::PointerTag
>> {
268 throw_unsup
!(ThreadLocalStatic(def_id
))
271 /// Return the root pointer for the given `extern static`.
272 fn extern_static_base_pointer(
273 mem
: &Memory
<'mir
, 'tcx
, Self>,
275 ) -> InterpResult
<'tcx
, Pointer
<Self::PointerTag
>>;
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.
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>,
284 ) -> Pointer
<Self::PointerTag
>;
286 /// "Int-to-pointer cast"
288 mem
: &Memory
<'mir
, 'tcx
, Self>,
290 ) -> Pointer
<Option
<Self::PointerTag
>>;
292 /// Convert a pointer with provenance into an allocation-offset pair.
294 mem
: &Memory
<'mir
, 'tcx
, Self>,
295 ptr
: Pointer
<Self::PointerTag
>,
296 ) -> (AllocId
, Size
);
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.
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`.
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>,
314 alloc
: Cow
<'b
, Allocation
>,
315 kind
: Option
<MemoryKind
<Self::MemoryKind
>>,
316 ) -> Cow
<'b
, Allocation
<Self::PointerTag
, Self::AllocExtra
>>;
318 /// Hook for performing extra checks on a memory read access.
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
325 _memory_extra
: &Self::MemoryExtra
,
326 _alloc_extra
: &Self::AllocExtra
,
327 _tag
: Self::PointerTag
,
329 ) -> InterpResult
<'tcx
> {
333 /// Hook for performing extra checks on a memory write access.
336 _memory_extra
: &mut Self::MemoryExtra
,
337 _alloc_extra
: &mut Self::AllocExtra
,
338 _tag
: Self::PointerTag
,
340 ) -> InterpResult
<'tcx
> {
344 /// Hook for performing extra operations on a memory deallocation.
346 fn memory_deallocated(
347 _memory_extra
: &mut Self::MemoryExtra
,
348 _alloc_extra
: &mut Self::AllocExtra
,
349 _tag
: Self::PointerTag
,
351 ) -> InterpResult
<'tcx
> {
355 /// Executes a retagging operation.
358 _ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
359 _kind
: mir
::RetagKind
,
360 _place
: &PlaceTy
<'tcx
, Self::PointerTag
>,
361 ) -> InterpResult
<'tcx
> {
365 /// Called immediately before a new stack frame gets pushed.
367 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
368 frame
: Frame
<'mir
, 'tcx
, Self::PointerTag
>,
369 ) -> InterpResult
<'tcx
, Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>>;
371 /// Borrow the current thread's stack.
373 ecx
: &'a InterpCx
<'mir
, 'tcx
, Self>,
374 ) -> &'a
[Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>];
376 /// Mutably borrow the current thread's stack.
378 ecx
: &'a
mut InterpCx
<'mir
, 'tcx
, Self>,
379 ) -> &'a
mut Vec
<Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>>;
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
> {
386 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
388 _ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
389 _frame
: Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>,
391 ) -> InterpResult
<'tcx
, StackPopJump
> {
392 // By default, we do not support unwinding from panics
393 Ok(StackPopJump
::Normal
)
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
;
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
407 type AllocExtra
= ();
408 type FrameExtra
= ();
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`.
418 fn force_int_for_alignment_check(_memory_extra
: &Self::MemoryExtra
) -> bool
{
419 // We do not support `force_int`.
424 fn enforce_validity(_ecx
: &InterpCx
<$mir
, $tcx
, Self>) -> bool
{
425 false // for now, we don't enforce validity
429 fn enforce_number_validity(_ecx
: &InterpCx
<$mir
, $tcx
, Self>) -> bool
{
435 _ecx
: &mut InterpCx
<$mir
, $tcx
, Self>,
438 _args
: &[OpTy
<$tcx
>],
439 _ret
: Option
<(&PlaceTy
<$tcx
>, mir
::BasicBlock
)>,
440 _unwind
: StackPopUnwind
,
441 ) -> InterpResult
<$tcx
> {
446 fn init_allocation_extra
<'b
>(
447 _mem
: &Memory
<$mir
, $tcx
, Self>,
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
456 fn extern_static_base_pointer(
457 mem
: &Memory
<$mir
, $tcx
, Self>,
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
))
465 fn tag_alloc_base_pointer(
466 _mem
: &Memory
<$mir
, $tcx
, Self>,
467 ptr
: Pointer
<AllocId
>,
468 ) -> Pointer
<AllocId
> {
473 fn ptr_from_addr(_mem
: &Memory
<$mir
, $tcx
, Self>, addr
: u64) -> Pointer
<Option
<AllocId
>> {
474 Pointer
::new(None
, Size
::from_bytes(addr
))
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();