2017-07-21 15:25:30 +00:00
|
|
|
//! This module contains everything needed to instantiate an interpreter.
|
|
|
|
//! This separation exists to ensure that no fancy miri features like
|
|
|
|
//! interpreting common C functions leak into CTFE.
|
|
|
|
|
2018-10-05 13:13:59 +00:00
|
|
|
use std::borrow::{Borrow, Cow};
|
2020-10-13 08:17:05 +00:00
|
|
|
use std::fmt::Debug;
|
2018-09-21 21:32:59 +00:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
2022-09-03 01:22:43 +00:00
|
|
|
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir;
|
2023-03-21 08:52:31 +00:00
|
|
|
use rustc_middle::ty::layout::TyAndLayout;
|
2022-04-05 17:31:51 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2020-03-30 20:54:15 +00:00
|
|
|
use rustc_span::def_id::DefId;
|
2022-11-22 11:16:33 +00:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2022-07-07 16:01:36 +00:00
|
|
|
use rustc_target::spec::abi::Abi as CallAbi;
|
2018-08-23 19:22:27 +00:00
|
|
|
|
2022-11-21 16:51:16 +00:00
|
|
|
use crate::const_eval::CheckAlignment;
|
|
|
|
|
2018-10-16 12:50:07 +00:00
|
|
|
use super::{
|
2023-07-10 20:07:07 +00:00
|
|
|
AllocBytes, AllocId, AllocRange, Allocation, ConstAllocation, FnArg, Frame, ImmTy, InterpCx,
|
2022-10-10 18:50:49 +00:00
|
|
|
InterpResult, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar,
|
2018-10-16 12:50:07 +00:00
|
|
|
};
|
|
|
|
|
2019-04-17 01:04:54 +00:00
|
|
|
/// Data returned by Machine::stack_pop,
|
|
|
|
/// to provide further control over the popping of the stack frame
|
2019-10-20 16:51:25 +00:00
|
|
|
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
|
2020-03-14 10:51:27 +00:00
|
|
|
pub enum StackPopJump {
|
2019-11-04 18:49:50 +00:00
|
|
|
/// Indicates that no special handling should be
|
|
|
|
/// done - we'll either return normally or unwind
|
|
|
|
/// based on the terminator for the function
|
|
|
|
/// we're leaving.
|
2019-04-17 01:04:54 +00:00
|
|
|
Normal,
|
|
|
|
|
2020-03-14 10:51:27 +00:00
|
|
|
/// Indicates that we should *not* jump to the return/unwind address, as the callback already
|
|
|
|
/// took care of everything.
|
|
|
|
NoJump,
|
2019-04-17 01:04:54 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:45:44 +00:00
|
|
|
/// Whether this kind of memory is allowed to leak
|
|
|
|
pub trait MayLeak: Copy {
|
|
|
|
fn may_leak(self) -> bool;
|
|
|
|
}
|
|
|
|
|
2018-10-05 13:13:59 +00:00
|
|
|
/// The functionality needed by memory to manage its allocations
|
|
|
|
pub trait AllocMap<K: Hash + Eq, V> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Tests if the map contains the given key.
|
2018-10-05 13:13:59 +00:00
|
|
|
/// Deliberately takes `&mut` because that is sufficient, and some implementations
|
|
|
|
/// can be more efficient then (using `RefCell::get_mut`).
|
|
|
|
fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
|
|
|
|
where
|
|
|
|
K: Borrow<Q>;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Inserts a new entry into the map.
|
2018-10-05 13:13:59 +00:00
|
|
|
fn insert(&mut self, k: K, v: V) -> Option<V>;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Removes an entry from the map.
|
2018-10-05 13:13:59 +00:00
|
|
|
fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>;
|
|
|
|
|
2020-04-04 10:19:10 +00:00
|
|
|
/// Returns data based on the keys and values in the map.
|
2018-10-05 13:13:59 +00:00
|
|
|
fn filter_map_collect<T>(&self, f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T>;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns a reference to entry `k`. If no such entry exists, call
|
2018-10-05 13:13:59 +00:00
|
|
|
/// `vacant` and either forward its error, or add its result to the map
|
|
|
|
/// and return a reference to *that*.
|
|
|
|
fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E>;
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns a mutable reference to entry `k`. If no such entry exists, call
|
2018-10-05 13:13:59 +00:00
|
|
|
/// `vacant` and either forward its error, or add its result to the map
|
|
|
|
/// and return a reference to *that*.
|
|
|
|
fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E>;
|
2019-07-28 10:58:39 +00:00
|
|
|
|
|
|
|
/// Read-only lookup.
|
|
|
|
fn get(&self, k: K) -> Option<&V> {
|
2019-07-28 20:30:19 +00:00
|
|
|
self.get_or(k, || Err(())).ok()
|
2019-07-28 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable lookup.
|
|
|
|
fn get_mut(&mut self, k: K) -> Option<&mut V> {
|
2019-07-28 20:30:19 +00:00
|
|
|
self.get_mut_or(k, || Err(())).ok()
|
2019-07-28 10:58:39 +00:00
|
|
|
}
|
2018-10-05 13:13:59 +00:00
|
|
|
}
|
2018-08-23 17:04:33 +00:00
|
|
|
|
2017-07-21 15:25:30 +00:00
|
|
|
/// Methods of this trait signifies a point where CTFE evaluation would fail
|
2018-09-20 08:12:21 +00:00
|
|
|
/// and some use case dependent behaviour can instead be applied.
|
2023-07-10 20:07:07 +00:00
|
|
|
pub trait Machine<'mir, 'tcx: 'mir>: Sized {
|
2017-07-28 14:48:43 +00:00
|
|
|
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
|
2020-10-13 08:17:05 +00:00
|
|
|
type MemoryKind: Debug + std::fmt::Display + MayLeak + Eq + 'static;
|
2018-10-16 07:15:13 +00:00
|
|
|
|
2021-07-12 16:22:15 +00:00
|
|
|
/// Pointers are "tagged" with provenance information; typically the `AllocId` they belong to.
|
2022-07-18 22:47:31 +00:00
|
|
|
type Provenance: Provenance + Eq + Hash + 'static;
|
2018-10-16 07:15:13 +00:00
|
|
|
|
2022-07-18 22:47:31 +00:00
|
|
|
/// When getting the AllocId of a pointer, some extra data is also obtained from the provenance
|
2022-04-18 14:00:42 +00:00
|
|
|
/// that is passed to memory access hooks so they can do things with it.
|
2022-07-18 22:47:31 +00:00
|
|
|
type ProvenanceExtra: Copy + 'static;
|
2022-04-18 14:00:42 +00:00
|
|
|
|
2019-06-30 11:51:18 +00:00
|
|
|
/// Machines can define extra (non-instance) things that represent values of function pointers.
|
2019-11-27 03:19:54 +00:00
|
|
|
/// For example, Miri uses this to return a function pointer from `dlsym`
|
2019-06-30 11:51:18 +00:00
|
|
|
/// that can later be called to execute the right thing.
|
2020-10-13 08:17:05 +00:00
|
|
|
type ExtraFnVal: Debug + Copy;
|
2019-06-30 11:51:18 +00:00
|
|
|
|
2018-11-15 16:14:53 +00:00
|
|
|
/// Extra data stored in every call frame.
|
|
|
|
type FrameExtra;
|
|
|
|
|
2018-10-16 07:15:13 +00:00
|
|
|
/// Extra data stored in every allocation.
|
2023-03-12 22:30:33 +00:00
|
|
|
type AllocExtra: Debug + Clone + 'tcx;
|
2018-08-26 10:59:59 +00:00
|
|
|
|
2023-02-14 03:30:53 +00:00
|
|
|
/// Type for the bytes of the allocation.
|
|
|
|
type Bytes: AllocBytes + 'static;
|
|
|
|
|
2018-10-05 13:13:59 +00:00
|
|
|
/// Memory's allocation map
|
|
|
|
type MemoryMap: AllocMap<
|
2018-10-16 07:15:13 +00:00
|
|
|
AllocId,
|
2023-02-14 20:26:47 +00:00
|
|
|
(
|
|
|
|
MemoryKind<Self::MemoryKind>,
|
|
|
|
Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>,
|
|
|
|
),
|
2018-10-05 13:13:59 +00:00
|
|
|
> + Default
|
|
|
|
+ Clone;
|
|
|
|
|
2020-03-21 18:19:10 +00:00
|
|
|
/// The memory kind to use for copied global memory (held in `tcx`) --
|
|
|
|
/// or None if such memory should not be mutated and thus any such attempt will cause
|
|
|
|
/// a `ModifiedStatic` error to be raised.
|
2018-09-21 21:32:59 +00:00
|
|
|
/// Statics are copied under two circumstances: When they are mutated, and when
|
2022-07-18 22:47:31 +00:00
|
|
|
/// `adjust_allocation` (see below) returns an owned allocation
|
2018-09-21 21:32:59 +00:00
|
|
|
/// that is added to the memory so that the work is not done twice.
|
2020-03-21 18:19:10 +00:00
|
|
|
const GLOBAL_KIND: Option<Self::MemoryKind>;
|
2017-07-28 14:48:43 +00:00
|
|
|
|
2021-07-02 20:06:12 +00:00
|
|
|
/// Should the machine panic on allocation failures?
|
|
|
|
const PANIC_ON_ALLOC_FAIL: bool;
|
|
|
|
|
2019-07-28 11:44:11 +00:00
|
|
|
/// Whether memory accesses should be alignment-checked.
|
2022-11-21 16:51:16 +00:00
|
|
|
fn enforce_alignment(ecx: &InterpCx<'mir, 'tcx, Self>) -> CheckAlignment;
|
2019-07-28 11:44:11 +00:00
|
|
|
|
2022-07-27 01:43:17 +00:00
|
|
|
/// Whether, when checking alignment, we should look at the actual address and thus support
|
2020-08-16 15:36:46 +00:00
|
|
|
/// custom alignment logic based on whatever the integer address happens to be.
|
2022-04-18 15:43:13 +00:00
|
|
|
///
|
2022-07-27 01:43:17 +00:00
|
|
|
/// If this returns true, Provenance::OFFSET_IS_ADDR must be true.
|
|
|
|
fn use_addr_for_alignment_check(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
|
2020-08-16 15:36:46 +00:00
|
|
|
|
2022-11-22 11:16:33 +00:00
|
|
|
fn alignment_check_failed(
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
has: Align,
|
|
|
|
required: Align,
|
|
|
|
check: CheckAlignment,
|
|
|
|
) -> InterpResult<'tcx, ()>;
|
|
|
|
|
2023-03-21 08:52:31 +00:00
|
|
|
/// Whether to enforce the validity invariant for a specific layout.
|
|
|
|
fn enforce_validity(ecx: &InterpCx<'mir, 'tcx, Self>, layout: TyAndLayout<'tcx>) -> bool;
|
2018-10-02 18:20:14 +00:00
|
|
|
|
2022-07-07 16:01:36 +00:00
|
|
|
/// Whether function calls should be [ABI](CallAbi)-checked.
|
2021-05-22 11:04:28 +00:00
|
|
|
fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2021-05-21 14:19:37 +00:00
|
|
|
|
2023-02-12 15:31:34 +00:00
|
|
|
/// Whether Assert(OverflowNeg) and Assert(Overflow) MIR terminators should actually
|
|
|
|
/// check for overflow.
|
2023-03-16 00:00:00 +00:00
|
|
|
fn ignore_optional_overflow_checks(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
|
2022-07-04 23:24:41 +00:00
|
|
|
|
2020-12-07 14:27:46 +00:00
|
|
|
/// Entry point for obtaining the MIR of anything that should get evaluated.
|
|
|
|
/// So not just functions and shims, but also const/static initializers, anonymous
|
|
|
|
/// constants, ...
|
|
|
|
fn load_mir(
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
instance: ty::InstanceDef<'tcx>,
|
|
|
|
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
|
|
|
|
Ok(ecx.tcx.instance_mir(instance))
|
|
|
|
}
|
|
|
|
|
2017-07-28 07:52:19 +00:00
|
|
|
/// Entry point to all function calls.
|
|
|
|
///
|
2018-08-23 17:04:33 +00:00
|
|
|
/// Returns either the mir to use for the call, or `None` if execution should
|
|
|
|
/// just proceed (which usually means this hook did all the work that the
|
2019-02-08 13:53:55 +00:00
|
|
|
/// called function should usually have done). In the latter case, it is
|
2019-11-25 15:23:44 +00:00
|
|
|
/// this hook's responsibility to advance the instruction pointer!
|
2018-08-23 17:04:33 +00:00
|
|
|
/// (This is to support functions like `__rust_maybe_catch_panic` that neither find a MIR
|
|
|
|
/// nor just jump to `ret`, but instead push their own stack frame.)
|
|
|
|
/// Passing `dest`and `ret` in the same `Option` proved very annoying when only one of them
|
|
|
|
/// was used.
|
2019-11-30 16:53:02 +00:00
|
|
|
fn find_mir_or_eval_fn(
|
2019-06-27 09:36:01 +00:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2017-07-21 15:25:30 +00:00
|
|
|
instance: ty::Instance<'tcx>,
|
2022-07-07 16:01:36 +00:00
|
|
|
abi: CallAbi,
|
2023-07-10 20:07:07 +00:00
|
|
|
args: &[FnArg<'tcx, Self::Provenance>],
|
2022-07-18 22:47:31 +00:00
|
|
|
destination: &PlaceTy<'tcx, Self::Provenance>,
|
2022-04-16 13:27:54 +00:00
|
|
|
target: Option<mir::BasicBlock>,
|
2022-10-10 18:50:49 +00:00
|
|
|
unwind: mir::UnwindAction,
|
2021-11-29 00:35:50 +00:00
|
|
|
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>>;
|
2017-07-25 09:32:48 +00:00
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Execute `fn_val`. It is the hook's responsibility to advance the instruction
|
2019-06-30 13:06:13 +00:00
|
|
|
/// pointer as appropriate.
|
|
|
|
fn call_extra_fn(
|
2019-07-01 09:26:28 +00:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2019-06-30 13:06:13 +00:00
|
|
|
fn_val: Self::ExtraFnVal,
|
2022-07-07 16:01:36 +00:00
|
|
|
abi: CallAbi,
|
2023-07-10 20:07:07 +00:00
|
|
|
args: &[FnArg<'tcx, Self::Provenance>],
|
2022-07-18 22:47:31 +00:00
|
|
|
destination: &PlaceTy<'tcx, Self::Provenance>,
|
2022-04-16 13:27:54 +00:00
|
|
|
target: Option<mir::BasicBlock>,
|
2022-10-10 18:50:49 +00:00
|
|
|
unwind: mir::UnwindAction,
|
2019-06-30 13:06:13 +00:00
|
|
|
) -> InterpResult<'tcx>;
|
|
|
|
|
2019-11-25 15:23:44 +00:00
|
|
|
/// Directly process an intrinsic without pushing a stack frame. It is the hook's
|
|
|
|
/// responsibility to advance the instruction pointer as appropriate.
|
2018-09-20 08:12:21 +00:00
|
|
|
fn call_intrinsic(
|
2019-06-27 09:36:01 +00:00
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2017-07-28 11:08:27 +00:00
|
|
|
instance: ty::Instance<'tcx>,
|
2022-07-18 22:47:31 +00:00
|
|
|
args: &[OpTy<'tcx, Self::Provenance>],
|
|
|
|
destination: &PlaceTy<'tcx, Self::Provenance>,
|
2022-04-16 13:27:54 +00:00
|
|
|
target: Option<mir::BasicBlock>,
|
2022-10-10 18:50:49 +00:00
|
|
|
unwind: mir::UnwindAction,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx>;
|
2017-07-28 11:08:27 +00:00
|
|
|
|
2019-11-29 08:59:52 +00:00
|
|
|
/// Called to evaluate `Assert` MIR terminators that trigger a panic.
|
|
|
|
fn assert_panic(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2020-02-10 19:53:01 +00:00
|
|
|
msg: &mir::AssertMessage<'tcx>,
|
2022-10-08 22:47:59 +00:00
|
|
|
unwind: mir::UnwindAction,
|
2019-11-29 08:59:52 +00:00
|
|
|
) -> InterpResult<'tcx>;
|
|
|
|
|
2022-10-31 01:01:24 +00:00
|
|
|
/// Called to abort evaluation.
|
2020-12-06 19:25:13 +00:00
|
|
|
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, _msg: String) -> InterpResult<'tcx, !> {
|
2020-03-19 08:07:43 +00:00
|
|
|
throw_unsup_format!("aborting execution is not supported")
|
2020-03-09 09:45:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 14:08:50 +00:00
|
|
|
/// Called for all binary operations where the LHS has pointer type.
|
2017-07-25 09:32:48 +00:00
|
|
|
///
|
2017-08-01 09:11:57 +00:00
|
|
|
/// Returns a (value, overflowed) pair if the operation succeeded
|
2019-07-24 14:08:50 +00:00
|
|
|
fn binary_ptr_op(
|
2019-06-27 09:36:01 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2017-07-25 09:32:48 +00:00
|
|
|
bin_op: mir::BinOp,
|
2022-07-18 22:47:31 +00:00
|
|
|
left: &ImmTy<'tcx, Self::Provenance>,
|
|
|
|
right: &ImmTy<'tcx, Self::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)>;
|
2017-07-28 14:48:43 +00:00
|
|
|
|
2020-06-26 09:02:43 +00:00
|
|
|
/// Called to write the specified `local` from the `frame`.
|
|
|
|
/// Since writing a ZST is not actually accessing memory or locals, this is never invoked
|
|
|
|
/// for ZST reads.
|
2022-08-07 16:33:44 +00:00
|
|
|
///
|
|
|
|
/// Due to borrow checker trouble, we indicate the `frame` as an index rather than an `&mut
|
|
|
|
/// Frame`.
|
2020-06-26 09:02:43 +00:00
|
|
|
#[inline]
|
|
|
|
fn access_local_mut<'a>(
|
|
|
|
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
frame: usize,
|
|
|
|
local: mir::Local,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<'tcx, &'a mut Operand<Self::Provenance>>
|
2020-06-26 09:02:43 +00:00
|
|
|
where
|
|
|
|
'tcx: 'mir,
|
|
|
|
{
|
|
|
|
ecx.stack_mut()[frame].locals[local].access_mut()
|
|
|
|
}
|
|
|
|
|
2022-12-30 00:34:17 +00:00
|
|
|
/// Called before a basic block terminator is executed.
|
|
|
|
#[inline]
|
|
|
|
fn before_terminator(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-29 23:14:29 +00:00
|
|
|
/// Called when the interpreter encounters a `StatementKind::ConstEvalCounter` instruction.
|
|
|
|
/// You can use this to detect long or endlessly running programs.
|
2020-02-23 16:04:34 +00:00
|
|
|
#[inline]
|
2022-12-29 23:14:29 +00:00
|
|
|
fn increment_const_eval_counter(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
2020-02-23 16:04:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-21 18:19:10 +00:00
|
|
|
/// Called before a global allocation is accessed.
|
2020-03-22 08:23:19 +00:00
|
|
|
/// `def_id` is `Some` if this is the "lazy" allocation of a static.
|
2020-02-23 16:04:34 +00:00
|
|
|
#[inline]
|
2020-03-21 18:19:10 +00:00
|
|
|
fn before_access_global(
|
2022-04-05 17:31:51 +00:00
|
|
|
_tcx: TyCtxt<'tcx>,
|
2022-04-03 17:05:49 +00:00
|
|
|
_machine: &Self,
|
2020-03-21 19:44:39 +00:00
|
|
|
_alloc_id: AllocId,
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-01 20:15:04 +00:00
|
|
|
_allocation: ConstAllocation<'tcx>,
|
2020-03-25 07:47:59 +00:00
|
|
|
_static_def_id: Option<DefId>,
|
2020-03-21 18:19:10 +00:00
|
|
|
_is_write: bool,
|
2019-12-16 14:23:42 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2019-09-25 01:12:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-26 09:11:17 +00:00
|
|
|
/// Return the `AllocId` for the given thread-local static in the current thread.
|
2021-07-14 20:10:17 +00:00
|
|
|
fn thread_local_static_base_pointer(
|
2020-07-26 09:11:17 +00:00
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
def_id: DefId,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Self::Provenance>> {
|
2020-07-26 09:11:17 +00:00
|
|
|
throw_unsup!(ThreadLocalStatic(def_id))
|
2020-02-23 16:04:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 20:10:17 +00:00
|
|
|
/// Return the root pointer for the given `extern static`.
|
|
|
|
fn extern_static_base_pointer(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2020-07-26 09:11:17 +00:00
|
|
|
def_id: DefId,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
|
2020-04-14 21:40:08 +00:00
|
|
|
|
2021-07-14 20:10:17 +00:00
|
|
|
/// Return a "base" pointer for the given allocation: the one that is used for direct
|
|
|
|
/// accesses to this static/const/fn allocation, or the one returned from the heap allocator.
|
2021-07-12 18:29:05 +00:00
|
|
|
///
|
2021-07-14 20:10:17 +00:00
|
|
|
/// Not called on `extern` or thread-local statics (those use the methods above).
|
2022-07-18 22:47:31 +00:00
|
|
|
fn adjust_alloc_base_pointer(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2021-07-12 16:22:15 +00:00
|
|
|
ptr: Pointer,
|
2023-02-07 10:39:07 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Self::Provenance>>;
|
2020-07-26 09:11:17 +00:00
|
|
|
|
2021-07-14 20:10:17 +00:00
|
|
|
/// "Int-to-pointer cast"
|
2022-05-13 17:30:25 +00:00
|
|
|
fn ptr_from_addr_cast(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2021-07-14 20:10:17 +00:00
|
|
|
addr: u64,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
|
2021-07-14 20:10:17 +00:00
|
|
|
|
2022-05-13 17:30:25 +00:00
|
|
|
/// Marks a pointer as exposed, allowing it's provenance
|
|
|
|
/// to be recovered. "Pointer-to-int cast"
|
|
|
|
fn expose_ptr(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
ptr: Pointer<Self::Provenance>,
|
2022-05-13 17:30:25 +00:00
|
|
|
) -> InterpResult<'tcx>;
|
|
|
|
|
2022-04-18 14:00:42 +00:00
|
|
|
/// Convert a pointer with provenance into an allocation-offset pair
|
|
|
|
/// and extra provenance info.
|
|
|
|
///
|
|
|
|
/// The returned `AllocId` must be the same as `ptr.provenance.get_alloc_id()`.
|
2022-05-13 17:30:25 +00:00
|
|
|
///
|
|
|
|
/// When this fails, that means the pointer does not point to a live allocation.
|
2021-07-14 20:10:17 +00:00
|
|
|
fn ptr_get_alloc(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
ptr: Pointer<Self::Provenance>,
|
|
|
|
) -> Option<(AllocId, Size, Self::ProvenanceExtra)>;
|
|
|
|
|
|
|
|
/// Called to adjust allocations to the Provenance and AllocExtra of this machine.
|
|
|
|
///
|
|
|
|
/// The way we construct allocations is to always first construct it without extra and then add
|
|
|
|
/// the extra. This keeps uniform code paths for handling both allocations created by CTFE for
|
|
|
|
/// globals, and allocations created by Miri during evaluation.
|
2019-05-28 08:44:46 +00:00
|
|
|
///
|
2022-07-18 22:47:31 +00:00
|
|
|
/// `kind` is the kind of the allocation being adjusted; it can be `None` when
|
2020-03-21 18:19:10 +00:00
|
|
|
/// it's a global and `GLOBAL_KIND` is `None`.
|
2019-04-15 08:05:13 +00:00
|
|
|
///
|
|
|
|
/// This should avoid copying if no work has to be done! If this returns an owned
|
2022-07-18 22:47:31 +00:00
|
|
|
/// allocation (because a copy had to be done to adjust things), machine memory will
|
2019-04-15 08:05:13 +00:00
|
|
|
/// cache the result. (This relies on `AllocMap::get_or` being able to add the
|
|
|
|
/// owned allocation to the map even when the map is shared.)
|
2022-06-14 16:40:15 +00:00
|
|
|
///
|
2022-08-27 18:11:19 +00:00
|
|
|
/// This must only fail if `alloc` contains provenance.
|
2022-07-18 22:47:31 +00:00
|
|
|
fn adjust_allocation<'b>(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
2019-05-28 08:44:46 +00:00
|
|
|
id: AllocId,
|
|
|
|
alloc: Cow<'b, Allocation>,
|
2020-03-21 18:19:10 +00:00
|
|
|
kind: Option<MemoryKind<Self::MemoryKind>>,
|
2023-02-14 03:30:53 +00:00
|
|
|
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::Provenance, Self::AllocExtra, Self::Bytes>>>;
|
2019-05-28 08:44:46 +00:00
|
|
|
|
2022-09-03 01:22:43 +00:00
|
|
|
fn eval_inline_asm(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_template: &'tcx [InlineAsmTemplatePiece],
|
|
|
|
_operands: &[mir::InlineAsmOperand<'tcx>],
|
|
|
|
_options: InlineAsmOptions,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
throw_unsup_format!("inline assembly is not supported")
|
|
|
|
}
|
|
|
|
|
2021-05-16 16:53:20 +00:00
|
|
|
/// Hook for performing extra checks on a memory read access.
|
|
|
|
///
|
|
|
|
/// Takes read-only access to the allocation so we can keep all the memory read
|
|
|
|
/// operations take `&self`. Use a `RefCell` in `AllocExtra` if you
|
|
|
|
/// need to mutate.
|
|
|
|
#[inline(always)]
|
2022-08-15 22:06:06 +00:00
|
|
|
fn before_memory_read(
|
2022-04-05 17:31:51 +00:00
|
|
|
_tcx: TyCtxt<'tcx>,
|
2022-04-03 17:05:49 +00:00
|
|
|
_machine: &Self,
|
2021-05-19 14:37:17 +00:00
|
|
|
_alloc_extra: &Self::AllocExtra,
|
2022-07-18 22:47:31 +00:00
|
|
|
_prov: (AllocId, Self::ProvenanceExtra),
|
2021-07-14 20:10:17 +00:00
|
|
|
_range: AllocRange,
|
2021-05-16 16:53:20 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hook for performing extra checks on a memory write access.
|
|
|
|
#[inline(always)]
|
2022-08-15 22:06:06 +00:00
|
|
|
fn before_memory_write(
|
2022-04-05 17:31:51 +00:00
|
|
|
_tcx: TyCtxt<'tcx>,
|
2022-04-03 17:05:49 +00:00
|
|
|
_machine: &mut Self,
|
2021-05-19 14:37:17 +00:00
|
|
|
_alloc_extra: &mut Self::AllocExtra,
|
2022-07-18 22:47:31 +00:00
|
|
|
_prov: (AllocId, Self::ProvenanceExtra),
|
2021-07-14 20:10:17 +00:00
|
|
|
_range: AllocRange,
|
2021-05-16 16:53:20 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hook for performing extra operations on a memory deallocation.
|
|
|
|
#[inline(always)]
|
2022-08-15 22:06:06 +00:00
|
|
|
fn before_memory_deallocation(
|
2022-04-05 17:31:51 +00:00
|
|
|
_tcx: TyCtxt<'tcx>,
|
2022-04-03 17:05:49 +00:00
|
|
|
_machine: &mut Self,
|
2021-05-19 14:37:17 +00:00
|
|
|
_alloc_extra: &mut Self::AllocExtra,
|
2022-07-18 22:47:31 +00:00
|
|
|
_prov: (AllocId, Self::ProvenanceExtra),
|
2021-07-14 20:10:17 +00:00
|
|
|
_range: AllocRange,
|
2020-12-11 18:42:36 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-05 14:17:11 +00:00
|
|
|
/// Executes a retagging operation for a single pointer.
|
|
|
|
/// Returns the possibly adjusted pointer.
|
2018-10-16 12:50:07 +00:00
|
|
|
#[inline]
|
2022-12-05 14:17:11 +00:00
|
|
|
fn retag_ptr_value(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_kind: mir::RetagKind,
|
|
|
|
val: &ImmTy<'tcx, Self::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, Self::Provenance>> {
|
|
|
|
Ok(val.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes a retagging operation on a compound value.
|
|
|
|
/// Replaces all pointers stored in the given place.
|
|
|
|
#[inline]
|
|
|
|
fn retag_place_contents(
|
2019-06-27 09:36:01 +00:00
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2018-12-11 18:54:38 +00:00
|
|
|
_kind: mir::RetagKind,
|
2022-07-18 22:47:31 +00:00
|
|
|
_place: &PlaceTy<'tcx, Self::Provenance>,
|
2019-06-07 16:56:27 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2017-12-14 10:36:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-11-06 10:04:10 +00:00
|
|
|
|
2023-07-10 20:07:07 +00:00
|
|
|
/// Called on places used for in-place function argument and return value handling.
|
|
|
|
///
|
|
|
|
/// These places need to be protected to make sure the program cannot tell whether the
|
|
|
|
/// argument/return value was actually copied or passed in-place..
|
|
|
|
fn protect_in_place_function_argument(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
place: &PlaceTy<'tcx, Self::Provenance>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
// Without an aliasing model, all we can do is put `Uninit` into the place.
|
|
|
|
ecx.write_uninit(place)
|
|
|
|
}
|
|
|
|
|
2020-04-13 14:06:51 +00:00
|
|
|
/// Called immediately before a new stack frame gets pushed.
|
|
|
|
fn init_frame_extra(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
frame: Frame<'mir, 'tcx, Self::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
|
2018-11-15 16:14:53 +00:00
|
|
|
|
2020-04-14 21:40:08 +00:00
|
|
|
/// Borrow the current thread's stack.
|
2021-12-14 03:34:51 +00:00
|
|
|
fn stack<'a>(
|
2020-04-14 21:40:08 +00:00
|
|
|
ecx: &'a InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> &'a [Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>];
|
2020-04-14 21:40:08 +00:00
|
|
|
|
|
|
|
/// Mutably borrow the current thread's stack.
|
2021-12-14 03:34:51 +00:00
|
|
|
fn stack_mut<'a>(
|
2020-04-14 21:40:08 +00:00
|
|
|
ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> &'a mut Vec<Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>>;
|
2020-04-14 21:40:08 +00:00
|
|
|
|
2020-04-13 15:07:54 +00:00
|
|
|
/// Called immediately after a stack frame got pushed and its locals got initialized.
|
|
|
|
fn after_stack_push(_ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-11-15 16:14:53 +00:00
|
|
|
|
2023-07-10 20:07:07 +00:00
|
|
|
/// Called just before the return value is copied to the caller-provided return place.
|
|
|
|
fn before_stack_pop(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_frame: &Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-13 14:06:51 +00:00
|
|
|
/// Called immediately after a stack frame got popped, but before jumping back to the caller.
|
2022-07-02 20:24:42 +00:00
|
|
|
/// The `locals` have already been destroyed!
|
2020-04-13 14:06:51 +00:00
|
|
|
fn after_stack_pop(
|
2019-10-28 23:09:54 +00:00
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2022-07-18 22:47:31 +00:00
|
|
|
_frame: Frame<'mir, 'tcx, Self::Provenance, Self::FrameExtra>,
|
2022-07-02 20:24:42 +00:00
|
|
|
unwinding: bool,
|
2020-03-14 10:51:27 +00:00
|
|
|
) -> InterpResult<'tcx, StackPopJump> {
|
2019-10-28 23:09:54 +00:00
|
|
|
// By default, we do not support unwinding from panics
|
2022-07-02 20:24:42 +00:00
|
|
|
assert!(!unwinding);
|
2020-03-14 10:51:27 +00:00
|
|
|
Ok(StackPopJump::Normal)
|
2019-10-28 23:09:54 +00:00
|
|
|
}
|
2017-07-21 15:25:30 +00:00
|
|
|
}
|
2020-04-27 17:01:30 +00:00
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// A lot of the flexibility above is just needed for `Miri`, but all "compile-time" machines
|
2022-11-16 20:34:16 +00:00
|
|
|
/// (CTFE and ConstProp) use the same instance. Here, we share that code.
|
2020-04-27 17:01:30 +00:00
|
|
|
pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
|
2022-07-18 22:47:31 +00:00
|
|
|
type Provenance = AllocId;
|
|
|
|
type ProvenanceExtra = ();
|
2022-04-18 14:00:42 +00:00
|
|
|
|
2020-04-27 17:01:30 +00:00
|
|
|
type ExtraFnVal = !;
|
|
|
|
|
2020-12-03 15:39:39 +00:00
|
|
|
type MemoryMap =
|
2022-10-04 19:18:15 +00:00
|
|
|
rustc_data_structures::fx::FxIndexMap<AllocId, (MemoryKind<Self::MemoryKind>, Allocation)>;
|
2020-12-03 15:39:39 +00:00
|
|
|
const GLOBAL_KIND: Option<Self::MemoryKind> = None; // no copying of globals from `tcx` to machine memory
|
2020-04-27 17:01:30 +00:00
|
|
|
|
|
|
|
type AllocExtra = ();
|
|
|
|
type FrameExtra = ();
|
2023-02-14 03:30:53 +00:00
|
|
|
type Bytes = Box<[u8]>;
|
2020-04-27 17:01:30 +00:00
|
|
|
|
2020-08-16 15:36:46 +00:00
|
|
|
#[inline(always)]
|
2022-07-27 01:43:17 +00:00
|
|
|
fn use_addr_for_alignment_check(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
|
|
|
|
// We do not support `use_addr`.
|
2020-08-16 15:36:46 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-07-04 23:24:41 +00:00
|
|
|
#[inline(always)]
|
2023-03-16 00:00:00 +00:00
|
|
|
fn ignore_optional_overflow_checks(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
|
2023-02-12 15:31:34 +00:00
|
|
|
false
|
2022-07-04 23:24:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:01:30 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn call_extra_fn(
|
|
|
|
_ecx: &mut InterpCx<$mir, $tcx, Self>,
|
|
|
|
fn_val: !,
|
2022-07-07 16:01:36 +00:00
|
|
|
_abi: CallAbi,
|
2023-07-10 20:07:07 +00:00
|
|
|
_args: &[FnArg<$tcx>],
|
2022-07-18 22:47:31 +00:00
|
|
|
_destination: &PlaceTy<$tcx, Self::Provenance>,
|
2022-04-16 13:27:54 +00:00
|
|
|
_target: Option<mir::BasicBlock>,
|
2022-10-10 18:50:49 +00:00
|
|
|
_unwind: mir::UnwindAction,
|
2020-04-27 17:01:30 +00:00
|
|
|
) -> InterpResult<$tcx> {
|
|
|
|
match fn_val {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 22:47:31 +00:00
|
|
|
fn adjust_allocation<'b>(
|
2022-04-03 17:05:49 +00:00
|
|
|
_ecx: &InterpCx<$mir, $tcx, Self>,
|
2021-07-14 20:10:17 +00:00
|
|
|
_id: AllocId,
|
2020-04-27 17:01:30 +00:00
|
|
|
alloc: Cow<'b, Allocation>,
|
2020-12-03 15:39:39 +00:00
|
|
|
_kind: Option<MemoryKind<Self::MemoryKind>>,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> InterpResult<$tcx, Cow<'b, Allocation<Self::Provenance>>> {
|
2022-06-14 16:40:15 +00:00
|
|
|
Ok(alloc)
|
2021-07-14 20:10:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extern_static_base_pointer(
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx: &InterpCx<$mir, $tcx, Self>,
|
2021-07-14 20:10:17 +00:00
|
|
|
def_id: DefId,
|
|
|
|
) -> InterpResult<$tcx, Pointer> {
|
|
|
|
// Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
|
2022-04-03 17:05:49 +00:00
|
|
|
Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
|
2020-04-27 17:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-07-18 22:47:31 +00:00
|
|
|
fn adjust_alloc_base_pointer(
|
2022-04-03 17:05:49 +00:00
|
|
|
_ecx: &InterpCx<$mir, $tcx, Self>,
|
2021-07-12 16:22:15 +00:00
|
|
|
ptr: Pointer<AllocId>,
|
2023-02-07 10:39:07 +00:00
|
|
|
) -> InterpResult<$tcx, Pointer<AllocId>> {
|
|
|
|
Ok(ptr)
|
2021-07-12 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:30:25 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn ptr_from_addr_cast(
|
|
|
|
_ecx: &InterpCx<$mir, $tcx, Self>,
|
|
|
|
addr: u64,
|
2022-06-05 14:53:35 +00:00
|
|
|
) -> InterpResult<$tcx, Pointer<Option<AllocId>>> {
|
|
|
|
// Allow these casts, but make the pointer not dereferenceable.
|
|
|
|
// (I.e., they behave like transmutation.)
|
2022-08-28 17:13:13 +00:00
|
|
|
// This is correct because no pointers can ever be exposed in compile-time evaluation.
|
2023-02-14 13:55:50 +00:00
|
|
|
Ok(Pointer::from_addr_invalid(addr))
|
2021-07-12 16:22:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2022-04-18 14:00:42 +00:00
|
|
|
fn ptr_get_alloc(
|
|
|
|
_ecx: &InterpCx<$mir, $tcx, Self>,
|
|
|
|
ptr: Pointer<AllocId>,
|
2022-07-18 22:47:31 +00:00
|
|
|
) -> Option<(AllocId, Size, Self::ProvenanceExtra)> {
|
2021-07-12 18:29:05 +00:00
|
|
|
// We know `offset` is relative to the allocation, so we can use `into_parts`.
|
2021-07-12 16:22:15 +00:00
|
|
|
let (alloc_id, offset) = ptr.into_parts();
|
2022-05-13 17:30:25 +00:00
|
|
|
Some((alloc_id, offset, ()))
|
2020-04-27 17:01:30 +00:00
|
|
|
}
|
|
|
|
}
|