2022-02-08 06:21:23 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir;
|
2022-04-05 17:31:51 +00:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2020-04-27 17:01:30 +00:00
|
|
|
use std::borrow::Borrow;
|
2019-12-25 00:04:32 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-12-03 15:39:39 +00:00
|
|
|
use std::fmt;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::Mutability;
|
2020-03-30 20:54:15 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::mir::AssertMessage;
|
2020-05-26 18:48:08 +00:00
|
|
|
use rustc_session::Limit;
|
2020-09-12 08:10:13 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2020-12-03 06:51:47 +00:00
|
|
|
use rustc_target::abi::{Align, Size};
|
2022-07-07 16:01:36 +00:00
|
|
|
use rustc_target::spec::abi::Abi as CallAbi;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
|
|
|
use crate::interpret::{
|
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
|
|
|
self, compile_time_machine, AllocId, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
|
2022-05-13 17:30:25 +00:00
|
|
|
OpTy, PlaceTy, Pointer, Scalar, StackPopUnwind,
|
2019-12-25 00:04:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use super::error::*;
|
|
|
|
|
2020-03-16 22:12:42 +00:00
|
|
|
impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
|
2020-02-08 21:21:20 +00:00
|
|
|
/// "Intercept" a function call to a panic-related function
|
|
|
|
/// because we have something special to do for it.
|
2020-02-09 15:54:40 +00:00
|
|
|
/// If this returns successfully (`Ok`), the function should just be evaluated normally.
|
2021-10-12 05:06:37 +00:00
|
|
|
fn hook_special_const_fn(
|
2020-02-08 21:21:20 +00:00
|
|
|
&mut self,
|
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
args: &[OpTy<'tcx>],
|
2021-07-06 12:38:26 +00:00
|
|
|
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
|
2021-10-25 16:07:16 +00:00
|
|
|
// All `#[rustc_do_not_const_check]` functions should be hooked here.
|
2020-02-08 21:21:20 +00:00
|
|
|
let def_id = instance.def_id();
|
2021-10-12 05:06:37 +00:00
|
|
|
|
2021-10-25 16:07:16 +00:00
|
|
|
if Some(def_id) == self.tcx.lang_items().const_eval_select() {
|
|
|
|
// redirect to const_eval_select_ct
|
|
|
|
if let Some(const_eval_select) = self.tcx.lang_items().const_eval_select_ct() {
|
|
|
|
return Ok(Some(
|
|
|
|
ty::Instance::resolve(
|
|
|
|
*self.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
const_eval_select,
|
|
|
|
instance.substs,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
));
|
2021-10-12 05:06:37 +00:00
|
|
|
}
|
2021-10-25 16:07:16 +00:00
|
|
|
} else if Some(def_id) == self.tcx.lang_items().panic_display()
|
2020-02-08 21:21:20 +00:00
|
|
|
|| Some(def_id) == self.tcx.lang_items().begin_panic_fn()
|
|
|
|
{
|
2021-09-14 18:14:37 +00:00
|
|
|
// &str or &&str
|
2020-02-08 21:21:20 +00:00
|
|
|
assert!(args.len() == 1);
|
|
|
|
|
2021-09-14 18:14:37 +00:00
|
|
|
let mut msg_place = self.deref_operand(&args[0])?;
|
|
|
|
while msg_place.layout.ty.is_ref() {
|
|
|
|
msg_place = self.deref_operand(&msg_place.into())?;
|
|
|
|
}
|
|
|
|
|
2021-02-15 00:00:00 +00:00
|
|
|
let msg = Symbol::intern(self.read_str(&msg_place)?);
|
2020-03-30 20:54:15 +00:00
|
|
|
let span = self.find_closest_untracked_caller_location();
|
2020-02-08 21:21:20 +00:00
|
|
|
let (file, line, col) = self.location_triple_for_span(span);
|
2021-07-06 12:38:26 +00:00
|
|
|
return Err(ConstEvalErrKind::Panic { msg, file, line, col }.into());
|
2021-09-11 02:44:02 +00:00
|
|
|
} else if Some(def_id) == self.tcx.lang_items().panic_fmt() {
|
2021-07-06 12:38:26 +00:00
|
|
|
// For panic_fmt, call const_panic_fmt instead.
|
|
|
|
if let Some(const_panic_fmt) = self.tcx.lang_items().const_panic_fmt() {
|
|
|
|
return Ok(Some(
|
|
|
|
ty::Instance::resolve(
|
|
|
|
*self.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
const_panic_fmt,
|
|
|
|
self.tcx.intern_substs(&[]),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap(),
|
|
|
|
));
|
|
|
|
}
|
2020-02-08 21:21:20 +00:00
|
|
|
}
|
2021-07-06 12:38:26 +00:00
|
|
|
Ok(None)
|
2020-02-08 21:21:20 +00:00
|
|
|
}
|
2019-12-22 20:10:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 23:07:29 +00:00
|
|
|
/// Extra machine state for CTFE, and the Machine instance
|
2020-03-16 22:12:42 +00:00
|
|
|
pub struct CompileTimeInterpreter<'mir, 'tcx> {
|
2020-03-17 23:07:29 +00:00
|
|
|
/// For now, the number of terminators that can be evaluated before we throw a resource
|
2021-04-19 12:57:08 +00:00
|
|
|
/// exhaustion error.
|
2020-03-17 23:07:29 +00:00
|
|
|
///
|
|
|
|
/// Setting this to `0` disables the limit and allows the interpreter to run forever.
|
|
|
|
pub steps_remaining: usize,
|
2020-03-16 22:12:42 +00:00
|
|
|
|
|
|
|
/// The virtual call stack.
|
2021-07-12 16:22:15 +00:00
|
|
|
pub(crate) stack: Vec<Frame<'mir, 'tcx, AllocId, ()>>,
|
2019-12-25 00:04:32 +00:00
|
|
|
|
2020-04-28 21:48:22 +00:00
|
|
|
/// We need to make sure consts never point to anything mutable, even recursively. That is
|
|
|
|
/// relied on for pattern matching on consts with references.
|
|
|
|
/// To achieve this, two pieces have to work together:
|
|
|
|
/// * Interning makes everything outside of statics immutable.
|
|
|
|
/// * Pointers to allocations inside of statics can never leak outside, to a non-static global.
|
|
|
|
/// This boolean here controls the second part.
|
2019-12-25 00:28:30 +00:00
|
|
|
pub(super) can_access_statics: bool,
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 22:12:42 +00:00
|
|
|
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
|
2022-07-14 21:42:47 +00:00
|
|
|
pub(crate) fn new(const_eval_limit: Limit, can_access_statics: bool) -> Self {
|
2022-04-03 17:05:49 +00:00
|
|
|
CompileTimeInterpreter {
|
|
|
|
steps_remaining: const_eval_limit.0,
|
|
|
|
stack: Vec::new(),
|
|
|
|
can_access_statics,
|
|
|
|
}
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K: Hash + Eq, V> interpret::AllocMap<K, V> for FxHashMap<K, V> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn contains_key<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> bool
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
{
|
|
|
|
FxHashMap::contains_key(self, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn insert(&mut self, k: K, v: V) -> Option<V> {
|
|
|
|
FxHashMap::insert(self, k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn remove<Q: ?Sized + Hash + Eq>(&mut self, k: &Q) -> Option<V>
|
|
|
|
where
|
|
|
|
K: Borrow<Q>,
|
|
|
|
{
|
|
|
|
FxHashMap::remove(self, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn filter_map_collect<T>(&self, mut f: impl FnMut(&K, &V) -> Option<T>) -> Vec<T> {
|
|
|
|
self.iter().filter_map(move |(k, v)| f(k, &*v)).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn get_or<E>(&self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&V, E> {
|
|
|
|
match self.get(&k) {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => {
|
|
|
|
vacant()?;
|
|
|
|
bug!("The CTFE machine shouldn't ever need to extend the alloc_map when reading")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn get_mut_or<E>(&mut self, k: K, vacant: impl FnOnce() -> Result<V, E>) -> Result<&mut V, E> {
|
|
|
|
match self.entry(k) {
|
|
|
|
Entry::Occupied(e) => Ok(e.into_mut()),
|
|
|
|
Entry::Vacant(e) => {
|
|
|
|
let v = vacant()?;
|
|
|
|
Ok(e.insert(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) type CompileTimeEvalContext<'mir, 'tcx> =
|
2020-03-16 22:12:42 +00:00
|
|
|
InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>>;
|
2019-12-25 00:04:32 +00:00
|
|
|
|
2020-12-03 15:39:39 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
pub enum MemoryKind {
|
|
|
|
Heap,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for MemoryKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
MemoryKind::Heap => write!(f, "heap allocation"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl interpret::MayLeak for MemoryKind {
|
|
|
|
#[inline(always)]
|
|
|
|
fn may_leak(self) -> bool {
|
|
|
|
match self {
|
|
|
|
MemoryKind::Heap => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
impl interpret::MayLeak for ! {
|
|
|
|
#[inline(always)]
|
|
|
|
fn may_leak(self) -> bool {
|
|
|
|
// `self` is uninhabited
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 08:10:13 +00:00
|
|
|
impl<'mir, 'tcx: 'mir> CompileTimeEvalContext<'mir, 'tcx> {
|
2022-04-07 20:22:09 +00:00
|
|
|
fn guaranteed_eq(&mut self, a: Scalar, b: Scalar) -> InterpResult<'tcx, bool> {
|
|
|
|
Ok(match (a, b) {
|
2020-09-12 08:10:13 +00:00
|
|
|
// Comparisons between integers are always known.
|
2020-11-01 16:57:03 +00:00
|
|
|
(Scalar::Int { .. }, Scalar::Int { .. }) => a == b,
|
2020-09-12 08:10:13 +00:00
|
|
|
// Equality with integers can never be known for sure.
|
2021-07-12 18:29:05 +00:00
|
|
|
(Scalar::Int { .. }, Scalar::Ptr(..)) | (Scalar::Ptr(..), Scalar::Int { .. }) => false,
|
2020-09-12 08:10:13 +00:00
|
|
|
// FIXME: return `true` for when both sides are the same pointer, *except* that
|
|
|
|
// some things (like functions and vtables) do not have stable addresses
|
|
|
|
// so we need to be careful around them (see e.g. #73722).
|
2021-07-12 18:29:05 +00:00
|
|
|
(Scalar::Ptr(..), Scalar::Ptr(..)) => false,
|
2022-04-07 20:22:09 +00:00
|
|
|
})
|
2020-09-12 08:10:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 20:22:09 +00:00
|
|
|
fn guaranteed_ne(&mut self, a: Scalar, b: Scalar) -> InterpResult<'tcx, bool> {
|
|
|
|
Ok(match (a, b) {
|
2020-09-12 08:10:13 +00:00
|
|
|
// Comparisons between integers are always known.
|
2020-11-01 16:57:03 +00:00
|
|
|
(Scalar::Int(_), Scalar::Int(_)) => a != b,
|
2020-09-12 08:10:13 +00:00
|
|
|
// Comparisons of abstract pointers with null pointers are known if the pointer
|
|
|
|
// is in bounds, because if they are in bounds, the pointer can't be null.
|
|
|
|
// Inequality with integers other than null can never be known for sure.
|
2022-02-25 00:38:37 +00:00
|
|
|
(Scalar::Int(int), ptr @ Scalar::Ptr(..))
|
|
|
|
| (ptr @ Scalar::Ptr(..), Scalar::Int(int)) => {
|
2022-04-07 20:22:09 +00:00
|
|
|
int.is_null() && !self.scalar_may_be_null(ptr)?
|
2020-09-26 13:15:35 +00:00
|
|
|
}
|
2020-09-12 08:10:13 +00:00
|
|
|
// FIXME: return `true` for at least some comparisons where we can reliably
|
|
|
|
// determine the result of runtime inequality tests at compile-time.
|
|
|
|
// Examples include comparison of addresses in different static items.
|
2021-07-12 18:29:05 +00:00
|
|
|
(Scalar::Ptr(..), Scalar::Ptr(..)) => false,
|
2022-04-07 20:22:09 +00:00
|
|
|
})
|
2020-09-12 08:10:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 22:12:42 +00:00
|
|
|
impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, 'tcx> {
|
2020-04-27 17:01:30 +00:00
|
|
|
compile_time_machine!(<'mir, 'tcx>);
|
2019-12-25 00:04:32 +00:00
|
|
|
|
2020-12-03 15:39:39 +00:00
|
|
|
type MemoryKind = MemoryKind;
|
|
|
|
|
2021-07-02 20:06:12 +00:00
|
|
|
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
|
|
|
|
|
2020-12-07 14:27:46 +00:00
|
|
|
fn load_mir(
|
|
|
|
ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
instance: ty::InstanceDef<'tcx>,
|
|
|
|
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
|
|
|
|
match instance {
|
|
|
|
ty::InstanceDef::Item(def) => {
|
|
|
|
if ecx.tcx.is_ctfe_mir_available(def.did) {
|
|
|
|
Ok(ecx.tcx.mir_for_ctfe_opt_const_arg(def))
|
2022-02-08 06:21:23 +00:00
|
|
|
} else if ecx.tcx.def_kind(def.did) == DefKind::AssocConst {
|
2022-01-23 00:49:12 +00:00
|
|
|
let guar = ecx.tcx.sess.delay_span_bug(
|
2022-02-08 06:21:23 +00:00
|
|
|
rustc_span::DUMMY_SP,
|
|
|
|
"This is likely a const item that is missing from its impl",
|
|
|
|
);
|
2022-01-23 00:49:12 +00:00
|
|
|
throw_inval!(AlreadyReported(guar));
|
2020-12-07 14:27:46 +00:00
|
|
|
} else {
|
2021-07-24 12:08:04 +00:00
|
|
|
let path = ecx.tcx.def_path_str(def.did);
|
|
|
|
Err(ConstEvalErrKind::NeedsRfc(format!("calling extern function `{}`", path))
|
|
|
|
.into())
|
2020-12-07 14:27:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Ok(ecx.tcx.instance_mir(instance)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
fn find_mir_or_eval_fn(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
2021-07-10 16:32:27 +00:00
|
|
|
instance: ty::Instance<'tcx>,
|
2022-07-07 16:01:36 +00:00
|
|
|
_abi: CallAbi,
|
2019-12-25 00:04:32 +00:00
|
|
|
args: &[OpTy<'tcx>],
|
2022-04-16 13:27:54 +00:00
|
|
|
_dest: &PlaceTy<'tcx>,
|
|
|
|
_ret: Option<mir::BasicBlock>,
|
2021-05-22 20:37:17 +00:00
|
|
|
_unwind: StackPopUnwind, // unwinding is not supported in consts
|
2021-11-29 00:35:50 +00:00
|
|
|
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
|
2019-12-25 00:04:32 +00:00
|
|
|
debug!("find_mir_or_eval_fn: {:?}", instance);
|
|
|
|
|
|
|
|
// Only check non-glue functions
|
2020-07-03 17:13:39 +00:00
|
|
|
if let ty::InstanceDef::Item(def) = instance.def {
|
2019-12-25 00:04:32 +00:00
|
|
|
// Execution might have wandered off into other crates, so we cannot do a stability-
|
|
|
|
// sensitive check here. But we can at least rule out functions that are not const
|
|
|
|
// at all.
|
2020-12-08 22:17:02 +00:00
|
|
|
if !ecx.tcx.is_const_fn_raw(def.did) {
|
2022-03-16 09:49:54 +00:00
|
|
|
// allow calling functions inside a trait marked with #[const_trait].
|
2022-05-11 16:02:20 +00:00
|
|
|
if !ecx.tcx.is_const_default_method(def.did) {
|
2021-10-27 06:21:17 +00:00
|
|
|
// We certainly do *not* want to actually call the fn
|
|
|
|
// though, so be sure we return here.
|
|
|
|
throw_unsup_format!("calling non-const function `{}`", instance)
|
2021-07-10 03:17:14 +00:00
|
|
|
}
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
2021-10-12 05:06:37 +00:00
|
|
|
|
2021-10-25 16:07:16 +00:00
|
|
|
if let Some(new_instance) = ecx.hook_special_const_fn(instance, args)? {
|
|
|
|
// We call another const fn instead.
|
2021-11-30 03:02:17 +00:00
|
|
|
// However, we return the *original* instance to make backtraces work out
|
|
|
|
// (and we hope this does not confuse the FnAbi checks too much).
|
|
|
|
return Ok(Self::find_mir_or_eval_fn(
|
|
|
|
ecx,
|
|
|
|
new_instance,
|
|
|
|
_abi,
|
|
|
|
args,
|
2022-04-16 13:27:54 +00:00
|
|
|
_dest,
|
2021-11-30 03:02:17 +00:00
|
|
|
_ret,
|
|
|
|
_unwind,
|
|
|
|
)?
|
|
|
|
.map(|(body, _instance)| (body, instance)));
|
2021-10-25 16:07:16 +00:00
|
|
|
}
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
// This is a const fn. Call it.
|
2021-11-29 00:35:50 +00:00
|
|
|
Ok(Some((ecx.load_mir(instance.def, None)?, instance)))
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call_intrinsic(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
instance: ty::Instance<'tcx>,
|
|
|
|
args: &[OpTy<'tcx>],
|
2022-07-18 22:47:31 +00:00
|
|
|
dest: &PlaceTy<'tcx, Self::Provenance>,
|
2022-04-16 13:27:54 +00:00
|
|
|
target: Option<mir::BasicBlock>,
|
2021-05-22 20:37:17 +00:00
|
|
|
_unwind: StackPopUnwind,
|
2019-12-25 00:04:32 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
2020-09-12 08:10:13 +00:00
|
|
|
// Shared intrinsics.
|
2022-04-16 13:27:54 +00:00
|
|
|
if ecx.emulate_intrinsic(instance, args, dest, target)? {
|
2019-12-25 00:04:32 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
let intrinsic_name = ecx.tcx.item_name(instance.def_id());
|
2020-09-12 08:10:13 +00:00
|
|
|
|
|
|
|
// CTFE-specific intrinsics.
|
2022-04-16 13:27:54 +00:00
|
|
|
let Some(ret) = target else {
|
2022-02-18 23:47:43 +00:00
|
|
|
return Err(ConstEvalErrKind::NeedsRfc(format!(
|
|
|
|
"calling intrinsic `{}`",
|
|
|
|
intrinsic_name
|
|
|
|
))
|
|
|
|
.into());
|
2020-09-12 08:10:13 +00:00
|
|
|
};
|
|
|
|
match intrinsic_name {
|
|
|
|
sym::ptr_guaranteed_eq | sym::ptr_guaranteed_ne => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let a = ecx.read_immediate(&args[0])?.to_scalar()?;
|
|
|
|
let b = ecx.read_immediate(&args[1])?.to_scalar()?;
|
2020-09-12 08:10:13 +00:00
|
|
|
let cmp = if intrinsic_name == sym::ptr_guaranteed_eq {
|
2022-04-07 20:22:09 +00:00
|
|
|
ecx.guaranteed_eq(a, b)?
|
2020-09-12 08:10:13 +00:00
|
|
|
} else {
|
2022-04-07 20:22:09 +00:00
|
|
|
ecx.guaranteed_ne(a, b)?
|
2020-09-12 08:10:13 +00:00
|
|
|
};
|
|
|
|
ecx.write_scalar(Scalar::from_bool(cmp), dest)?;
|
|
|
|
}
|
2020-12-03 06:51:47 +00:00
|
|
|
sym::const_allocate => {
|
2021-02-15 00:00:00 +00:00
|
|
|
let size = ecx.read_scalar(&args[0])?.to_machine_usize(ecx)?;
|
|
|
|
let align = ecx.read_scalar(&args[1])?.to_machine_usize(ecx)?;
|
2020-12-03 06:51:47 +00:00
|
|
|
|
|
|
|
let align = match Align::from_bytes(align) {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
|
|
|
|
};
|
|
|
|
|
2022-04-03 17:05:49 +00:00
|
|
|
let ptr = ecx.allocate_ptr(
|
2020-12-03 06:51:47 +00:00
|
|
|
Size::from_bytes(size as u64),
|
|
|
|
align,
|
2020-12-03 15:39:39 +00:00
|
|
|
interpret::MemoryKind::Machine(MemoryKind::Heap),
|
2021-06-12 23:49:48 +00:00
|
|
|
)?;
|
2021-07-14 20:10:17 +00:00
|
|
|
ecx.write_pointer(ptr, dest)?;
|
2020-12-03 06:51:47 +00:00
|
|
|
}
|
2021-12-25 13:35:11 +00:00
|
|
|
sym::const_deallocate => {
|
|
|
|
let ptr = ecx.read_pointer(&args[0])?;
|
|
|
|
let size = ecx.read_scalar(&args[1])?.to_machine_usize(ecx)?;
|
|
|
|
let align = ecx.read_scalar(&args[2])?.to_machine_usize(ecx)?;
|
|
|
|
|
|
|
|
let size = Size::from_bytes(size);
|
|
|
|
let align = match Align::from_bytes(align) {
|
|
|
|
Ok(a) => a,
|
|
|
|
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
|
|
|
|
};
|
|
|
|
|
2022-01-26 04:06:09 +00:00
|
|
|
// If an allocation is created in an another const,
|
|
|
|
// we don't deallocate it.
|
2022-04-03 19:29:19 +00:00
|
|
|
let (alloc_id, _, _) = ecx.ptr_get_alloc_id(ptr)?;
|
2022-01-26 04:06:09 +00:00
|
|
|
let is_allocated_in_another_const = matches!(
|
|
|
|
ecx.tcx.get_global_alloc(alloc_id),
|
|
|
|
Some(interpret::GlobalAlloc::Memory(_))
|
|
|
|
);
|
|
|
|
|
|
|
|
if !is_allocated_in_another_const {
|
2022-04-03 17:05:49 +00:00
|
|
|
ecx.deallocate_ptr(
|
2022-01-26 04:06:09 +00:00
|
|
|
ptr,
|
|
|
|
Some((size, align)),
|
|
|
|
interpret::MemoryKind::Machine(MemoryKind::Heap),
|
|
|
|
)?;
|
|
|
|
}
|
2021-12-25 13:35:11 +00:00
|
|
|
}
|
2020-09-12 08:10:13 +00:00
|
|
|
_ => {
|
|
|
|
return Err(ConstEvalErrKind::NeedsRfc(format!(
|
|
|
|
"calling intrinsic `{}`",
|
|
|
|
intrinsic_name
|
|
|
|
))
|
|
|
|
.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ecx.go_to_block(ret);
|
|
|
|
Ok(())
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn assert_panic(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
msg: &AssertMessage<'tcx>,
|
|
|
|
_unwind: Option<mir::BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::AssertKind::*;
|
2020-06-19 16:57:15 +00:00
|
|
|
// Convert `AssertKind<Operand>` to `AssertKind<Scalar>`.
|
|
|
|
let eval_to_int =
|
2021-02-15 00:00:00 +00:00
|
|
|
|op| ecx.read_immediate(&ecx.eval_operand(op, None)?).map(|x| x.to_const_int());
|
2020-02-08 21:21:20 +00:00
|
|
|
let err = match msg {
|
2019-12-25 00:04:32 +00:00
|
|
|
BoundsCheck { ref len, ref index } => {
|
2020-06-19 16:57:15 +00:00
|
|
|
let len = eval_to_int(len)?;
|
|
|
|
let index = eval_to_int(index)?;
|
2020-02-08 21:21:20 +00:00
|
|
|
BoundsCheck { len, index }
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
2020-06-19 16:57:15 +00:00
|
|
|
Overflow(op, l, r) => Overflow(*op, eval_to_int(l)?, eval_to_int(r)?),
|
|
|
|
OverflowNeg(op) => OverflowNeg(eval_to_int(op)?),
|
|
|
|
DivisionByZero(op) => DivisionByZero(eval_to_int(op)?),
|
|
|
|
RemainderByZero(op) => RemainderByZero(eval_to_int(op)?),
|
2020-02-08 21:21:20 +00:00
|
|
|
ResumedAfterReturn(generator_kind) => ResumedAfterReturn(*generator_kind),
|
|
|
|
ResumedAfterPanic(generator_kind) => ResumedAfterPanic(*generator_kind),
|
|
|
|
};
|
2020-02-09 15:51:36 +00:00
|
|
|
Err(ConstEvalErrKind::AssertFailure(err).into())
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 19:25:13 +00:00
|
|
|
fn abort(_ecx: &mut InterpCx<'mir, 'tcx, Self>, msg: String) -> InterpResult<'tcx, !> {
|
|
|
|
Err(ConstEvalErrKind::Abort(msg).into())
|
|
|
|
}
|
|
|
|
|
2019-12-25 00:04:32 +00:00
|
|
|
fn binary_ptr_op(
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_bin_op: mir::BinOp,
|
2021-02-15 00:00:00 +00:00
|
|
|
_left: &ImmTy<'tcx>,
|
|
|
|
_right: &ImmTy<'tcx>,
|
2019-12-25 00:04:32 +00:00
|
|
|
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
|
2020-02-08 21:21:20 +00:00
|
|
|
Err(ConstEvalErrKind::NeedsRfc("pointer arithmetic or comparison".to_string()).into())
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn before_terminator(ecx: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
2020-03-17 23:07:29 +00:00
|
|
|
// The step limit has already been hit in a previous call to `before_terminator`.
|
|
|
|
if ecx.machine.steps_remaining == 0 {
|
2020-01-21 15:46:07 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:07:29 +00:00
|
|
|
ecx.machine.steps_remaining -= 1;
|
|
|
|
if ecx.machine.steps_remaining == 0 {
|
2020-03-22 19:49:58 +00:00
|
|
|
throw_exhaust!(StepLimitReached)
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 23:07:29 +00:00
|
|
|
Ok(())
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 17:30:25 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn expose_ptr(
|
|
|
|
_ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_ptr: Pointer<AllocId>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
Err(ConstEvalErrKind::NeedsRfc("exposing pointers".to_string()).into())
|
|
|
|
}
|
|
|
|
|
2020-08-12 08:18:21 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn init_frame_extra(
|
|
|
|
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
frame: Frame<'mir, 'tcx>,
|
|
|
|
) -> InterpResult<'tcx, Frame<'mir, 'tcx>> {
|
|
|
|
// Enforce stack size limit. Add 1 because this is run before the new frame is pushed.
|
2021-06-25 23:48:26 +00:00
|
|
|
if !ecx.recursion_limit.value_within_limit(ecx.stack().len() + 1) {
|
2020-08-09 16:01:37 +00:00
|
|
|
throw_exhaust!(StackFrameLimitReached)
|
|
|
|
} else {
|
2020-08-12 08:18:21 +00:00
|
|
|
Ok(frame)
|
2020-08-09 16:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:57:12 +00:00
|
|
|
#[inline(always)]
|
2021-12-14 03:34:51 +00:00
|
|
|
fn stack<'a>(
|
2020-04-16 16:57:12 +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-16 16:57:12 +00:00
|
|
|
&ecx.machine.stack
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2021-12-14 03:34:51 +00:00
|
|
|
fn stack_mut<'a>(
|
2020-04-16 16:57:12 +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-16 16:57:12 +00:00
|
|
|
&mut ecx.machine.stack
|
|
|
|
}
|
|
|
|
|
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
|
|
|
alloc: 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-25 00:04:32 +00:00
|
|
|
) -> InterpResult<'tcx> {
|
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
|
|
|
let alloc = alloc.inner();
|
2020-04-09 09:32:43 +00:00
|
|
|
if is_write {
|
|
|
|
// Write access. These are never allowed, but we give a targeted error message.
|
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
|
|
|
if alloc.mutability == Mutability::Not {
|
2020-04-09 09:32:43 +00:00
|
|
|
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
|
|
|
|
} else {
|
|
|
|
Err(ConstEvalErrKind::ModifiedGlobal.into())
|
|
|
|
}
|
2019-12-25 00:04:32 +00:00
|
|
|
} else {
|
2020-04-09 09:32:43 +00:00
|
|
|
// Read access. These are usually allowed, with some exceptions.
|
2022-04-03 17:05:49 +00:00
|
|
|
if machine.can_access_statics {
|
2020-04-10 09:28:51 +00:00
|
|
|
// Machine configuration allows us read from anything (e.g., `static` initializer).
|
2020-04-09 09:32:43 +00:00
|
|
|
Ok(())
|
2020-04-10 09:28:51 +00:00
|
|
|
} else if static_def_id.is_some() {
|
|
|
|
// Machine configuration does not allow us to read statics
|
|
|
|
// (e.g., `const` initializer).
|
2020-04-28 21:48:22 +00:00
|
|
|
// See const_eval::machine::MemoryExtra::can_access_statics for why
|
2020-04-28 21:54:47 +00:00
|
|
|
// this check is so important: if we could read statics, we could read pointers
|
|
|
|
// to mutable allocations *inside* statics. These allocations are not themselves
|
|
|
|
// statics, so pointers to them can get around the check in `validity.rs`.
|
2020-04-09 09:32:43 +00:00
|
|
|
Err(ConstEvalErrKind::ConstAccessesStatic.into())
|
|
|
|
} else {
|
|
|
|
// Immutable global, this read is fine.
|
2020-04-10 09:28:51 +00:00
|
|
|
// But make sure we never accept a read from something mutable, that would be
|
|
|
|
// unsound. The reason is that as the content of this allocation may be different
|
|
|
|
// now and at run-time, so if we permit reading now we might return the wrong value.
|
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
|
|
|
assert_eq!(alloc.mutability, Mutability::Not);
|
2020-04-09 09:32:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-25 00:04:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-25 00:08:39 +00:00
|
|
|
|
2019-12-22 20:10:43 +00:00
|
|
|
// Please do not add any code below the above `Machine` trait impl. I (oli-obk) plan more cleanups
|
|
|
|
// so we can end up having a file with just that impl, but for now, let's keep the impl discoverable
|
|
|
|
// at the bottom of this file.
|