]>
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
, Frame
, ImmTy
, InterpCx
, InterpResult
, LocalValue
, MemPlace
,
17 Memory
, MemoryKind
, OpTy
, Operand
, PlaceTy
, Pointer
, Provenance
, Scalar
, StackPopUnwind
,
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
30 /// Indicates that we should *not* jump to the return/unwind address, as the callback already
31 /// took care of everything.
35 /// Whether this kind of memory is allowed to leak
36 pub trait MayLeak
: Copy
{
37 fn may_leak(self) -> bool
;
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
49 /// Inserts a new entry into the map.
50 fn insert(&mut self, k
: K
, v
: V
) -> Option
<V
>;
52 /// Removes an entry from the map.
53 fn remove
<Q
: ?Sized
+ Hash
+ Eq
>(&mut self, k
: &Q
) -> Option
<V
>
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
>;
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
>;
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
>;
71 fn get(&self, k
: K
) -> Option
<&V
> {
72 self.get_or(k
, || Err(())).ok()
76 fn get_mut(&mut self, k
: K
) -> Option
<&mut V
> {
77 self.get_mut_or(k
, || Err(())).ok()
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;
87 /// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
88 type PointerTag
: Provenance
+ Eq
+ Hash
+ '
static;
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
;
95 /// Extra data stored in every call frame.
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.
103 /// Extra data stored in every allocation.
104 type AllocExtra
: Debug
+ Clone
+ '
static;
106 /// Memory's allocation map
107 type MemoryMap
: AllocMap
<
109 (MemoryKind
<Self::MemoryKind
>, Allocation
<Self::PointerTag
, Self::AllocExtra
>),
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
>;
121 /// Should the machine panic on allocation failures?
122 const PANIC_ON_ALLOC_FAIL
: bool
;
124 /// Whether memory accesses should be alignment-checked.
125 fn enforce_alignment(memory_extra
: &Self::MemoryExtra
) -> bool
;
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
;
131 /// Whether to enforce the validity invariant
132 fn enforce_validity(ecx
: &InterpCx
<'mir
, 'tcx
, Self>) -> bool
;
134 /// Whether function calls should be [ABI](Abi)-checked.
135 fn enforce_abi(_ecx
: &InterpCx
<'mir
, 'tcx
, Self>) -> bool
{
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
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
))
149 /// Entry point to all function calls.
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
159 fn find_mir_or_eval_fn(
160 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
161 instance
: ty
::Instance
<'tcx
>,
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
>>>;
168 /// Execute `fn_val`. It is the hook's responsibility to advance the instruction
169 /// pointer as appropriate.
171 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
172 fn_val
: Self::ExtraFnVal
,
174 args
: &[OpTy
<'tcx
, Self::PointerTag
>],
175 ret
: Option
<(&PlaceTy
<'tcx
, Self::PointerTag
>, mir
::BasicBlock
)>,
176 unwind
: StackPopUnwind
,
177 ) -> InterpResult
<'tcx
>;
179 /// Directly process an intrinsic without pushing a stack frame. It is the hook's
180 /// responsibility to advance the instruction pointer as appropriate.
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
>;
189 /// Called to evaluate `Assert` MIR terminators that trigger a panic.
191 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
192 msg
: &mir
::AssertMessage
<'tcx
>,
193 unwind
: Option
<mir
::BasicBlock
>,
194 ) -> InterpResult
<'tcx
>;
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")
201 /// Called for all binary operations where the LHS has pointer type.
203 /// Returns a (value, overflowed) pair if the operation succeeded
205 ecx
: &InterpCx
<'mir
, 'tcx
, Self>,
207 left
: &ImmTy
<'tcx
, Self::PointerTag
>,
208 right
: &ImmTy
<'tcx
, Self::PointerTag
>,
209 ) -> InterpResult
<'tcx
, (Scalar
<Self::PointerTag
>, bool
, Ty
<'tcx
>)>;
211 /// Heap allocations via the `box` keyword.
213 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
214 dest
: &PlaceTy
<'tcx
, Self::PointerTag
>,
215 ) -> InterpResult
<'tcx
>;
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
222 _ecx
: &InterpCx
<'mir
, 'tcx
, Self>,
223 frame
: &Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>,
225 ) -> InterpResult
<'tcx
, Operand
<Self::PointerTag
>> {
226 frame
.locals
[local
].access()
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
233 fn access_local_mut
<'a
>(
234 ecx
: &'a
mut InterpCx
<'mir
, 'tcx
, Self>,
237 ) -> InterpResult
<'tcx
, Result
<&'a
mut LocalValue
<Self::PointerTag
>, MemPlace
<Self::PointerTag
>>>
241 ecx
.stack_mut()[frame
].locals
[local
].access_mut()
244 /// Called before a basic block terminator is executed.
245 /// You can use this to detect endlessly running programs.
247 fn before_terminator(_ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>) -> InterpResult
<'tcx
> {
251 /// Called before a global allocation is accessed.
252 /// `def_id` is `Some` if this is the "lazy" allocation of a static.
254 fn before_access_global(
255 _memory_extra
: &Self::MemoryExtra
,
257 _allocation
: &Allocation
,
258 _static_def_id
: Option
<DefId
>,
260 ) -> InterpResult
<'tcx
> {
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>,
268 ) -> InterpResult
<'tcx
, Pointer
<Self::PointerTag
>> {
269 throw_unsup
!(ThreadLocalStatic(def_id
))
272 /// Return the root pointer for the given `extern static`.
273 fn extern_static_base_pointer(
274 mem
: &Memory
<'mir
, 'tcx
, Self>,
276 ) -> InterpResult
<'tcx
, Pointer
<Self::PointerTag
>>;
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.
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>,
285 ) -> Pointer
<Self::PointerTag
>;
287 /// "Int-to-pointer cast"
289 mem
: &Memory
<'mir
, 'tcx
, Self>,
291 ) -> Pointer
<Option
<Self::PointerTag
>>;
293 /// Convert a pointer with provenance into an allocation-offset pair.
295 mem
: &Memory
<'mir
, 'tcx
, Self>,
296 ptr
: Pointer
<Self::PointerTag
>,
297 ) -> (AllocId
, Size
);
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.
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`.
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>,
315 alloc
: Cow
<'b
, Allocation
>,
316 kind
: Option
<MemoryKind
<Self::MemoryKind
>>,
317 ) -> Cow
<'b
, Allocation
<Self::PointerTag
, Self::AllocExtra
>>;
319 /// Hook for performing extra checks on a memory read access.
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
326 _memory_extra
: &Self::MemoryExtra
,
327 _alloc_extra
: &Self::AllocExtra
,
328 _tag
: Self::PointerTag
,
330 ) -> InterpResult
<'tcx
> {
334 /// Hook for performing extra checks on a memory write access.
337 _memory_extra
: &mut Self::MemoryExtra
,
338 _alloc_extra
: &mut Self::AllocExtra
,
339 _tag
: Self::PointerTag
,
341 ) -> InterpResult
<'tcx
> {
345 /// Hook for performing extra operations on a memory deallocation.
347 fn memory_deallocated(
348 _memory_extra
: &mut Self::MemoryExtra
,
349 _alloc_extra
: &mut Self::AllocExtra
,
350 _tag
: Self::PointerTag
,
352 ) -> InterpResult
<'tcx
> {
356 /// Executes a retagging operation.
359 _ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
360 _kind
: mir
::RetagKind
,
361 _place
: &PlaceTy
<'tcx
, Self::PointerTag
>,
362 ) -> InterpResult
<'tcx
> {
366 /// Called immediately before a new stack frame gets pushed.
368 ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
369 frame
: Frame
<'mir
, 'tcx
, Self::PointerTag
>,
370 ) -> InterpResult
<'tcx
, Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>>;
372 /// Borrow the current thread's stack.
374 ecx
: &'a InterpCx
<'mir
, 'tcx
, Self>,
375 ) -> &'a
[Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>];
377 /// Mutably borrow the current thread's stack.
379 ecx
: &'a
mut InterpCx
<'mir
, 'tcx
, Self>,
380 ) -> &'a
mut Vec
<Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>>;
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
> {
387 /// Called immediately after a stack frame got popped, but before jumping back to the caller.
389 _ecx
: &mut InterpCx
<'mir
, 'tcx
, Self>,
390 _frame
: Frame
<'mir
, 'tcx
, Self::PointerTag
, Self::FrameExtra
>,
392 ) -> InterpResult
<'tcx
, StackPopJump
> {
393 // By default, we do not support unwinding from panics
394 Ok(StackPopJump
::Normal
)
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
;
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
408 type AllocExtra
= ();
409 type FrameExtra
= ();
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`.
419 fn force_int_for_alignment_check(_memory_extra
: &Self::MemoryExtra
) -> bool
{
420 // We do not support `force_int`.
425 fn enforce_validity(_ecx
: &InterpCx
<$mir
, $tcx
, Self>) -> bool
{
426 false // for now, we don't enforce validity
431 _ecx
: &mut InterpCx
<$mir
, $tcx
, Self>,
434 _args
: &[OpTy
<$tcx
>],
435 _ret
: Option
<(&PlaceTy
<$tcx
>, mir
::BasicBlock
)>,
436 _unwind
: StackPopUnwind
,
437 ) -> InterpResult
<$tcx
> {
442 fn init_allocation_extra
<'b
>(
443 _mem
: &Memory
<$mir
, $tcx
, Self>,
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
452 fn extern_static_base_pointer(
453 mem
: &Memory
<$mir
, $tcx
, Self>,
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
))
461 fn tag_alloc_base_pointer(
462 _mem
: &Memory
<$mir
, $tcx
, Self>,
463 ptr
: Pointer
<AllocId
>,
464 ) -> Pointer
<AllocId
> {
469 fn ptr_from_addr(_mem
: &Memory
<$mir
, $tcx
, Self>, addr
: u64) -> Pointer
<Option
<AllocId
>> {
470 Pointer
::new(None
, Size
::from_bytes(addr
))
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();