2018-08-13 16:14:22 +02:00
|
|
|
//! Functions concerning immediate values and operands, and reading from operands.
|
|
|
|
//! All high-level functions to read from memory work on operands as sources.
|
|
|
|
|
2020-03-22 17:48:11 +01:00
|
|
|
use std::convert::TryFrom;
|
2020-03-29 15:43:36 +02:00
|
|
|
use std::fmt::Write;
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2020-04-12 04:24:25 +03:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_hir::def::Namespace;
|
|
|
|
use rustc_macros::HashStable;
|
2020-05-23 15:42:35 +02:00
|
|
|
use rustc_middle::ty::layout::{PrimitiveExt, TyAndLayout};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Printer};
|
2020-06-19 18:57:15 +02:00
|
|
|
use rustc_middle::ty::{ConstInt, Ty};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::{mir, ty};
|
2020-05-30 14:21:56 +02:00
|
|
|
use rustc_target::abi::{Abi, HasDataLayout, LayoutOf, Size, TagEncoding};
|
2020-03-31 18:16:47 +02:00
|
|
|
use rustc_target::abi::{VariantIdx, Variants};
|
2020-03-29 15:43:36 +02:00
|
|
|
|
|
|
|
use super::{
|
2021-07-12 18:22:15 +02:00
|
|
|
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, GlobalId,
|
|
|
|
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, Place, PlaceTy, Pointer, Provenance,
|
|
|
|
Scalar, ScalarMaybeUninit,
|
2020-03-29 15:43:36 +02:00
|
|
|
};
|
2018-09-30 13:09:26 +02:00
|
|
|
|
2019-10-30 10:03:41 +00:00
|
|
|
/// An `Immediate` represents a single immediate self-contained Rust value.
|
2018-08-13 16:14:22 +02:00
|
|
|
///
|
|
|
|
/// For optimization of a few very common cases, there is also a representation for a pair of
|
|
|
|
/// primitive values (`ScalarPair`). It allows Miri to avoid making allocations for checked binary
|
2019-11-30 10:28:01 +01:00
|
|
|
/// operations and wide pointers. This idea was taken from rustc's codegen.
|
2018-08-13 16:14:22 +02:00
|
|
|
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
|
2018-11-02 12:51:26 +01:00
|
|
|
/// defined on `Immediate`, and do not have to work with a `Place`.
|
2021-07-12 18:22:15 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, HashStable, Hash)]
|
|
|
|
pub enum Immediate<Tag = AllocId> {
|
2020-04-22 03:20:40 -04:00
|
|
|
Scalar(ScalarMaybeUninit<Tag>),
|
|
|
|
ScalarPair(ScalarMaybeUninit<Tag>, ScalarMaybeUninit<Tag>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-07-12 18:22:15 +02:00
|
|
|
//FIXME rustc_data_structures::static_assert_size!(Immediate, 56);
|
|
|
|
|
|
|
|
impl<Tag: Provenance> std::fmt::Debug for Immediate<Tag> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Immediate::*;
|
|
|
|
match self {
|
|
|
|
Scalar(s) => f.debug_tuple("Scalar").field(s).finish(),
|
|
|
|
ScalarPair(s1, s2) => f.debug_tuple("ScalarPair").field(s1).field(s2).finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 00:00:00 +00:00
|
|
|
|
2020-04-22 03:20:40 -04:00
|
|
|
impl<Tag> From<ScalarMaybeUninit<Tag>> for Immediate<Tag> {
|
2019-07-24 19:01:12 +02:00
|
|
|
#[inline(always)]
|
2020-04-22 03:20:40 -04:00
|
|
|
fn from(val: ScalarMaybeUninit<Tag>) -> Self {
|
2019-07-24 19:01:12 +02:00
|
|
|
Immediate::Scalar(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Tag> From<Scalar<Tag>> for Immediate<Tag> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(val: Scalar<Tag>) -> Self {
|
|
|
|
Immediate::Scalar(val.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-12 20:29:05 +02:00
|
|
|
impl<'tcx, Tag> Immediate<Tag> {
|
|
|
|
pub fn from_pointer(p: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
|
|
|
|
Immediate::Scalar(ScalarMaybeUninit::from_pointer(p, cx))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_maybe_pointer(p: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
|
|
|
|
Immediate::Scalar(ScalarMaybeUninit::from_maybe_pointer(p, cx))
|
2019-11-29 11:09:26 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self {
|
2020-03-28 19:51:54 +01:00
|
|
|
Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 20:29:05 +02:00
|
|
|
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
|
|
|
|
Immediate::ScalarPair(val.into(), ScalarMaybeUninit::from_pointer(vtable, cx))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2020-07-22 09:08:59 -06:00
|
|
|
pub fn to_scalar_or_uninit(self) -> ScalarMaybeUninit<Tag> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(val) => val,
|
2019-11-30 10:28:01 +01:00
|
|
|
Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2019-06-07 18:56:27 +02:00
|
|
|
pub fn to_scalar(self) -> InterpResult<'tcx, Scalar<Tag>> {
|
2020-07-22 09:08:59 -06:00
|
|
|
self.to_scalar_or_uninit().check_init()
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
// ScalarPair needs a type to interpret, so we often have an immediate and a type together
|
2018-08-13 16:14:22 +02:00
|
|
|
// as input for binary and cast operations.
|
2021-07-12 18:22:15 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct ImmTy<'tcx, Tag = AllocId> {
|
2020-04-13 17:07:54 +02:00
|
|
|
imm: Immediate<Tag>,
|
2020-03-04 14:50:21 +00:00
|
|
|
pub layout: TyAndLayout<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-07-12 18:22:15 +02:00
|
|
|
//FIXME rustc_data_structures::static_assert_size!(ImmTy<'_>, 72);
|
|
|
|
|
|
|
|
impl<'tcx, Tag: Provenance> std::fmt::Debug for ImmTy<'tcx, Tag> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let ImmTy { imm, layout } = self;
|
|
|
|
f.debug_struct("ImmTy").field("imm", imm).field("layout", layout).finish()
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 00:00:00 +00:00
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
impl<Tag: Provenance> std::fmt::Display for ImmTy<'tcx, Tag> {
|
2019-12-23 17:41:06 +01:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
/// Helper function for printing a scalar to a FmtPrinter
|
2021-07-12 18:22:15 +02:00
|
|
|
fn p<'a, 'tcx, F: std::fmt::Write, Tag: Provenance>(
|
2020-02-26 19:36:10 +01:00
|
|
|
cx: FmtPrinter<'a, 'tcx, F>,
|
2020-04-22 03:20:40 -04:00
|
|
|
s: ScalarMaybeUninit<Tag>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<FmtPrinter<'a, 'tcx, F>, std::fmt::Error> {
|
|
|
|
match s {
|
2020-04-22 03:20:40 -04:00
|
|
|
ScalarMaybeUninit::Scalar(s) => {
|
2021-07-12 18:22:15 +02:00
|
|
|
cx.pretty_print_const_scalar(s.erase_for_fmt(), ty, true)
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
2020-04-22 03:20:40 -04:00
|
|
|
ScalarMaybeUninit::Uninit => cx.typed_value(
|
2020-02-26 19:36:10 +01:00
|
|
|
|mut this| {
|
2021-02-16 09:59:38 +01:00
|
|
|
this.write_str("uninit ")?;
|
2020-02-26 19:36:10 +01:00
|
|
|
Ok(this)
|
|
|
|
},
|
|
|
|
|this| this.print_type(ty),
|
|
|
|
" ",
|
|
|
|
),
|
2020-02-28 11:04:12 +01:00
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
ty::tls::with(|tcx| {
|
|
|
|
match self.imm {
|
|
|
|
Immediate::Scalar(s) => {
|
2020-10-16 21:59:49 +02:00
|
|
|
if let Some(ty) = tcx.lift(self.layout.ty) {
|
2019-12-23 17:41:06 +01:00
|
|
|
let cx = FmtPrinter::new(tcx, f, Namespace::ValueNS);
|
|
|
|
p(cx, s, ty)?;
|
|
|
|
return Ok(());
|
2019-11-17 14:30:08 +01:00
|
|
|
}
|
2021-07-12 18:22:15 +02:00
|
|
|
write!(f, "{}: {}", s.erase_for_fmt(), self.layout.ty)
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
|
|
|
Immediate::ScalarPair(a, b) => {
|
|
|
|
// FIXME(oli-obk): at least print tuples and slices nicely
|
2021-07-12 18:22:15 +02:00
|
|
|
write!(f, "({}, {}): {}", a.erase_for_fmt(), b.erase_for_fmt(), self.layout.ty,)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-02-28 11:04:12 +01:00
|
|
|
}
|
2019-12-23 17:41:06 +01:00
|
|
|
})
|
2019-11-17 14:30:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:17:05 +02:00
|
|
|
impl<'tcx, Tag> std::ops::Deref for ImmTy<'tcx, Tag> {
|
2018-10-26 12:33:26 +02:00
|
|
|
type Target = Immediate<Tag>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
fn deref(&self) -> &Immediate<Tag> {
|
2019-02-08 12:20:55 +01:00
|
|
|
&self.imm
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
|
2019-02-08 14:53:55 +01:00
|
|
|
/// or still in memory. The latter is an optimization, to delay reading that chunk of
|
2018-08-13 16:14:22 +02:00
|
|
|
/// memory and to avoid having to store arbitrary-sized data here.
|
2021-07-12 18:22:15 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, HashStable, Hash)]
|
|
|
|
pub enum Operand<Tag = AllocId> {
|
2020-05-01 11:33:21 +02:00
|
|
|
Immediate(Immediate<Tag>),
|
|
|
|
Indirect(MemPlace<Tag>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
impl<Tag: Provenance> std::fmt::Debug for Operand<Tag> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
use Operand::*;
|
|
|
|
match self {
|
|
|
|
Immediate(i) => f.debug_tuple("Immediate").field(i).finish(),
|
|
|
|
Indirect(p) => f.debug_tuple("Indirect").field(p).finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct OpTy<'tcx, Tag = AllocId> {
|
2019-05-17 02:20:14 +01:00
|
|
|
op: Operand<Tag>, // Keep this private; it helps enforce invariants.
|
2020-03-04 14:50:21 +00:00
|
|
|
pub layout: TyAndLayout<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-03-06 16:02:48 +00:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2021-02-14 00:00:00 +00:00
|
|
|
rustc_data_structures::static_assert_size!(OpTy<'_, ()>, 80);
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
impl<'tcx, Tag: Provenance> std::fmt::Debug for OpTy<'tcx, Tag> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let OpTy { op, layout } = self;
|
|
|
|
f.debug_struct("OpTy").field("op", op).field("layout", layout).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 10:17:05 +02:00
|
|
|
impl<'tcx, Tag> std::ops::Deref for OpTy<'tcx, Tag> {
|
2018-09-21 23:32:59 +02:00
|
|
|
type Target = Operand<Tag>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-09-21 23:32:59 +02:00
|
|
|
fn deref(&self) -> &Operand<Tag> {
|
2018-08-13 16:14:22 +02:00
|
|
|
&self.op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
impl<'tcx, Tag: Copy> From<MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-09-21 23:32:59 +02:00
|
|
|
fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
OpTy { op: Operand::Indirect(*mplace), layout: mplace.layout }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 00:00:00 +00:00
|
|
|
impl<'tcx, Tag: Copy> From<&'_ MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn from(mplace: &MPlaceTy<'tcx, Tag>) -> Self {
|
|
|
|
OpTy { op: Operand::Indirect(**mplace), layout: mplace.layout }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
fn from(val: ImmTy<'tcx, Tag>) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
OpTy { op: Operand::Immediate(val.imm), layout: val.layout }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 12:06:05 +02:00
|
|
|
impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
|
2019-02-08 14:00:52 +01:00
|
|
|
#[inline]
|
2020-03-04 14:50:21 +00:00
|
|
|
pub fn from_scalar(val: Scalar<Tag>, layout: TyAndLayout<'tcx>) -> Self {
|
2019-07-24 20:20:55 +02:00
|
|
|
ImmTy { imm: val.into(), layout }
|
2019-02-08 14:00:52 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 17:07:54 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn from_immediate(imm: Immediate<Tag>, layout: TyAndLayout<'tcx>) -> Self {
|
|
|
|
ImmTy { imm, layout }
|
|
|
|
}
|
|
|
|
|
2019-12-14 12:13:26 -05:00
|
|
|
#[inline]
|
2020-03-04 14:50:21 +00:00
|
|
|
pub fn try_from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
|
2019-12-21 10:27:58 -05:00
|
|
|
Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
|
2019-12-14 12:13:26 -05:00
|
|
|
}
|
2019-08-17 12:06:05 +02:00
|
|
|
#[inline]
|
2020-03-04 14:50:21 +00:00
|
|
|
pub fn from_uint(i: impl Into<u128>, layout: TyAndLayout<'tcx>) -> Self {
|
2019-08-17 12:06:05 +02:00
|
|
|
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
|
|
|
|
}
|
|
|
|
|
2019-12-14 12:13:26 -05:00
|
|
|
#[inline]
|
2020-03-04 14:50:21 +00:00
|
|
|
pub fn try_from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Option<Self> {
|
2019-12-21 10:27:58 -05:00
|
|
|
Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
|
2019-08-17 12:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-04 14:50:21 +00:00
|
|
|
pub fn from_int(i: impl Into<i128>, layout: TyAndLayout<'tcx>) -> Self {
|
2019-08-17 12:06:05 +02:00
|
|
|
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
|
|
|
|
}
|
2020-06-19 18:57:15 +02:00
|
|
|
|
|
|
|
#[inline]
|
2021-07-12 20:29:05 +02:00
|
|
|
pub fn to_const_int(self) -> ConstInt
|
|
|
|
where
|
|
|
|
Tag: Provenance,
|
|
|
|
{
|
2020-06-19 18:57:15 +02:00
|
|
|
assert!(self.layout.ty.is_integral());
|
2020-11-01 17:21:33 +00:00
|
|
|
let int = self.to_scalar().expect("to_const_int doesn't work on scalar pairs").assert_int();
|
2020-09-26 15:15:35 +02:00
|
|
|
ConstInt::new(int, self.layout.ty.is_signed(), self.layout.ty.is_ptr_sized_integral())
|
2020-06-19 18:57:15 +02:00
|
|
|
}
|
2019-02-08 14:00:52 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:12:42 -07:00
|
|
|
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
2019-05-31 05:01:35 -04:00
|
|
|
/// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `None` if the layout does not permit loading this as a value.
|
2019-02-16 13:27:54 +01:00
|
|
|
fn try_read_immediate_from_mplace(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
mplace: &MPlaceTy<'tcx, M::PointerTag>,
|
2019-02-10 14:59:13 +01:00
|
|
|
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
|
2018-08-26 14:35:15 +02:00
|
|
|
if mplace.layout.is_unsized() {
|
2018-10-22 18:21:55 +02:00
|
|
|
// Don't touch unsized
|
2018-08-16 09:36:25 +02:00
|
|
|
return Ok(None);
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2021-05-16 18:53:20 +02:00
|
|
|
let alloc = match self.get_alloc(mplace)? {
|
2019-06-23 14:26:36 +02:00
|
|
|
Some(ptr) => ptr,
|
2019-12-22 17:42:04 -05:00
|
|
|
None => {
|
|
|
|
return Ok(Some(ImmTy {
|
|
|
|
// zero-sized type
|
2020-11-01 17:04:13 +00:00
|
|
|
imm: Scalar::ZST.into(),
|
2019-12-22 17:42:04 -05:00
|
|
|
layout: mplace.layout,
|
|
|
|
}));
|
|
|
|
}
|
2019-06-23 14:26:36 +02:00
|
|
|
};
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2018-08-16 09:36:25 +02:00
|
|
|
match mplace.layout.abi {
|
2020-03-31 18:16:47 +02:00
|
|
|
Abi::Scalar(..) => {
|
2021-05-16 18:53:20 +02:00
|
|
|
let scalar = alloc.read_scalar(alloc_range(Size::ZERO, mplace.layout.size))?;
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2020-03-31 18:16:47 +02:00
|
|
|
Abi::ScalarPair(ref a, ref b) => {
|
2019-06-23 14:26:36 +02:00
|
|
|
// We checked `ptr_align` above, so all fields will have the alignment they need.
|
|
|
|
// We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
|
|
|
|
// which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
|
2018-08-13 16:14:22 +02:00
|
|
|
let (a, b) = (&a.value, &b.value);
|
|
|
|
let (a_size, b_size) = (a.size(self), b.size(self));
|
2018-09-09 01:16:45 +03:00
|
|
|
let b_offset = a_size.align_to(b.align(self).abi);
|
2019-06-23 14:26:36 +02:00
|
|
|
assert!(b_offset.bytes() > 0); // we later use the offset to tell apart the fields
|
2021-05-16 18:53:20 +02:00
|
|
|
let a_val = alloc.read_scalar(alloc_range(Size::ZERO, a_size))?;
|
|
|
|
let b_val = alloc.read_scalar(alloc_range(b_offset, b_size))?;
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout }))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
_ => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Try returning an immediate for the operand.
|
|
|
|
/// If the layout does not permit loading this as an immediate, return where in memory
|
2018-08-13 16:14:22 +02:00
|
|
|
/// we can find the data.
|
|
|
|
/// Note that for a given layout, this operation will either always fail or always
|
|
|
|
/// succeed! Whether it succeeds depends on whether the layout can be represented
|
2018-10-26 12:33:26 +02:00
|
|
|
/// in a `Immediate`, not on which data is stored there currently.
|
2019-06-01 13:08:04 -05:00
|
|
|
pub(crate) fn try_read_immediate(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
src: &OpTy<'tcx, M::PointerTag>,
|
2019-02-10 14:59:13 +01:00
|
|
|
) -> InterpResult<'tcx, Result<ImmTy<'tcx, M::PointerTag>, MPlaceTy<'tcx, M::PointerTag>>> {
|
2021-07-12 18:22:15 +02:00
|
|
|
Ok(match src.try_as_mplace() {
|
2021-02-15 00:00:00 +00:00
|
|
|
Ok(ref mplace) => {
|
2018-10-26 12:33:26 +02:00
|
|
|
if let Some(val) = self.try_read_immediate_from_mplace(mplace)? {
|
2018-08-16 09:36:25 +02:00
|
|
|
Ok(val)
|
|
|
|
} else {
|
2021-02-15 00:00:00 +00:00
|
|
|
Err(*mplace)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2018-08-16 09:36:25 +02:00
|
|
|
Err(val) => Ok(val),
|
|
|
|
})
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 12:33:26 +02:00
|
|
|
/// Read an immediate from a place, asserting that that is possible with the given layout.
|
2018-08-13 16:14:22 +02:00
|
|
|
#[inline(always)]
|
2018-10-26 12:33:26 +02:00
|
|
|
pub fn read_immediate(
|
2018-09-21 23:32:59 +02:00
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
|
2019-02-08 12:20:55 +01:00
|
|
|
if let Ok(imm) = self.try_read_immediate(op)? {
|
2019-02-10 14:59:13 +01:00
|
|
|
Ok(imm)
|
2018-08-13 16:14:22 +02:00
|
|
|
} else {
|
2020-06-21 16:13:31 +02:00
|
|
|
span_bug!(self.cur_span(), "primitive read failed for type: {:?}", op.layout.ty);
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a scalar from a place
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn read_scalar(
|
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2020-04-22 03:20:40 -04:00
|
|
|
) -> InterpResult<'tcx, ScalarMaybeUninit<M::PointerTag>> {
|
2020-07-22 09:08:59 -06:00
|
|
|
Ok(self.read_immediate(op)?.to_scalar_or_uninit())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
/// Read a pointer from a place.
|
|
|
|
pub fn read_pointer(
|
|
|
|
&self,
|
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::PointerTag>>> {
|
|
|
|
Ok(self.scalar_to_ptr(self.read_scalar(op)?.check_init()?))
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:28:01 +01:00
|
|
|
// Turn the wide MPlace into a string (must already be dereferenced!)
|
2021-02-15 00:00:00 +00:00
|
|
|
pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, &str> {
|
2018-08-25 14:36:24 +02:00
|
|
|
let len = mplace.len(self)?;
|
2020-03-21 17:28:46 +01:00
|
|
|
let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len))?;
|
2020-10-13 10:17:05 +02:00
|
|
|
let str = std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
|
2018-08-25 14:36:24 +02:00
|
|
|
Ok(str)
|
2018-08-23 19:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
/// Projection functions
|
|
|
|
pub fn operand_field(
|
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2020-03-21 17:17:01 +01:00
|
|
|
field: usize,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2021-07-12 18:22:15 +02:00
|
|
|
let base = match op.try_as_mplace() {
|
2021-02-15 00:00:00 +00:00
|
|
|
Ok(ref mplace) => {
|
2020-03-11 13:57:54 +01:00
|
|
|
// We can reuse the mplace field computation logic for indirect operands.
|
2018-08-13 16:14:22 +02:00
|
|
|
let field = self.mplace_field(mplace, field)?;
|
|
|
|
return Ok(field.into());
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
|
|
|
Err(value) => value,
|
2018-08-13 16:14:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let field_layout = op.layout.field(self, field)?;
|
2018-09-10 17:46:30 +02:00
|
|
|
if field_layout.is_zst() {
|
2020-11-01 17:04:13 +00:00
|
|
|
let immediate = Scalar::ZST.into();
|
2018-10-26 12:33:26 +02:00
|
|
|
return Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout });
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
let offset = op.layout.fields.offset(field);
|
2019-02-10 14:59:13 +01:00
|
|
|
let immediate = match *base {
|
2018-08-13 16:14:22 +02:00
|
|
|
// the field covers the entire type
|
2019-02-10 14:59:13 +01:00
|
|
|
_ if offset.bytes() == 0 && field_layout.size == op.layout.size => *base,
|
2018-08-13 16:14:22 +02:00
|
|
|
// extract fields from types with `ScalarPair` ABI
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::ScalarPair(a, b) => {
|
2018-08-13 16:14:22 +02:00
|
|
|
let val = if offset.bytes() == 0 { a } else { b };
|
2019-07-24 20:20:55 +02:00
|
|
|
Immediate::from(val)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
Immediate::Scalar(val) => span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"field access on non aggregate {:#?}, {:#?}",
|
|
|
|
val,
|
|
|
|
op.layout
|
|
|
|
),
|
2018-08-13 16:14:22 +02:00
|
|
|
};
|
2018-10-26 12:33:26 +02:00
|
|
|
Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout })
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 17:17:01 +01:00
|
|
|
pub fn operand_index(
|
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2020-03-21 17:17:01 +01:00
|
|
|
index: u64,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
|
|
|
if let Ok(index) = usize::try_from(index) {
|
|
|
|
// We can just treat this as a field.
|
|
|
|
self.operand_field(op, index)
|
|
|
|
} else {
|
|
|
|
// Indexing into a big array. This must be an mplace.
|
2021-07-12 18:22:15 +02:00
|
|
|
let mplace = op.assert_mem_place();
|
2021-02-15 00:00:00 +00:00
|
|
|
Ok(self.mplace_index(&mplace, index)?.into())
|
2020-03-21 17:17:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 16:39:25 +02:00
|
|
|
pub fn operand_downcast(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2018-11-01 19:03:38 +01:00
|
|
|
variant: VariantIdx,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
// Downcasts only change the layout
|
2021-07-12 18:22:15 +02:00
|
|
|
Ok(match op.try_as_mplace() {
|
2021-02-15 00:00:00 +00:00
|
|
|
Ok(ref mplace) => self.mplace_downcast(mplace, variant)?.into(),
|
2018-08-13 16:14:22 +02:00
|
|
|
Err(..) => {
|
|
|
|
let layout = op.layout.for_variant(self, variant);
|
2021-02-15 00:00:00 +00:00
|
|
|
OpTy { layout, ..*op }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operand_projection(
|
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
base: &OpTy<'tcx, M::PointerTag>,
|
2020-05-23 12:02:54 +02:00
|
|
|
proj_elem: mir::PlaceElem<'tcx>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::ProjectionElem::*;
|
2020-05-23 12:02:54 +02:00
|
|
|
Ok(match proj_elem {
|
2020-03-21 17:17:01 +01:00
|
|
|
Field(field, _) => self.operand_field(base, field.index())?,
|
2018-08-13 16:14:22 +02:00
|
|
|
Downcast(_, variant) => self.operand_downcast(base, variant)?,
|
|
|
|
Deref => self.deref_operand(base)?.into(),
|
2019-12-22 17:42:04 -05:00
|
|
|
Subslice { .. } | ConstantIndex { .. } | Index(_) => {
|
2018-09-10 17:46:30 +02:00
|
|
|
// The rest should only occur as mplace, we do not use Immediates for types
|
|
|
|
// allowing such operations. This matches place_projection forcing an allocation.
|
2021-07-12 18:22:15 +02:00
|
|
|
let mplace = base.assert_mem_place();
|
2021-02-15 00:00:00 +00:00
|
|
|
self.mplace_projection(&mplace, proj_elem)?.into()
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-26 11:02:43 +02:00
|
|
|
/// Read from a local. Will not actually access the local if reading from a ZST.
|
|
|
|
/// Will not access memory, instead an indirect `Operand` is returned.
|
|
|
|
///
|
|
|
|
/// This is public because it is used by [priroda](https://github.com/oli-obk/priroda) to get an
|
|
|
|
/// OpTy from a local
|
2019-01-30 15:01:42 +01:00
|
|
|
pub fn access_local(
|
2018-10-20 11:02:39 +02:00
|
|
|
&self,
|
2018-11-15 17:14:53 +01:00
|
|
|
frame: &super::Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>,
|
2018-10-20 11:02:39 +02:00
|
|
|
local: mir::Local,
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2019-01-30 14:55:31 +01:00
|
|
|
let layout = self.layout_of_local(frame, local, layout)?;
|
2019-04-08 13:28:51 +02:00
|
|
|
let op = if layout.is_zst() {
|
|
|
|
// Do not read from ZST, they might not be initialized
|
2020-11-01 17:04:13 +00:00
|
|
|
Operand::Immediate(Scalar::ZST.into())
|
2019-04-08 13:28:51 +02:00
|
|
|
} else {
|
2019-09-24 21:12:59 -04:00
|
|
|
M::access_local(&self, frame, local)?
|
2019-04-08 13:28:51 +02:00
|
|
|
};
|
2018-10-20 11:02:39 +02:00
|
|
|
Ok(OpTy { op, layout })
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:07:54 +02:00
|
|
|
/// Every place can be read from, so we can turn them into an operand.
|
|
|
|
/// This will definitely return `Indirect` if the place is a `Ptr`, i.e., this
|
|
|
|
/// will never actually read from memory.
|
2019-02-08 12:20:55 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn place_to_op(
|
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
place: &PlaceTy<'tcx, M::PointerTag>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2021-02-15 00:00:00 +00:00
|
|
|
let op = match **place {
|
2019-12-22 17:42:04 -05:00
|
|
|
Place::Ptr(mplace) => Operand::Indirect(mplace),
|
2020-03-16 15:12:42 -07:00
|
|
|
Place::Local { frame, local } => {
|
|
|
|
*self.access_local(&self.stack()[frame], local, None)?
|
|
|
|
}
|
2019-02-08 12:20:55 +01:00
|
|
|
};
|
|
|
|
Ok(OpTy { op, layout: place.layout })
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
// Evaluate a place with the goal of reading from it. This lets us sometimes
|
2019-01-16 20:45:53 +01:00
|
|
|
// avoid allocations.
|
2019-09-03 21:56:15 -04:00
|
|
|
pub fn eval_place_to_op(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2020-03-31 12:19:29 -03:00
|
|
|
place: mir::Place<'tcx>,
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2020-04-16 20:04:46 +02:00
|
|
|
// Do not use the layout passed in as argument if the base we are looking at
|
|
|
|
// here is not the entire place.
|
|
|
|
let layout = if place.projection.is_empty() { layout } else { None };
|
2020-04-15 23:29:29 +02:00
|
|
|
|
2020-04-16 20:04:46 +02:00
|
|
|
let base_op = self.access_local(self.frame(), place.local, layout)?;
|
2018-08-23 19:27:14 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let op = place
|
|
|
|
.projection
|
|
|
|
.iter()
|
2021-02-15 00:00:00 +00:00
|
|
|
.try_fold(base_op, |op, elem| self.operand_projection(&op, elem))?;
|
2019-07-30 00:07:28 +02:00
|
|
|
|
|
|
|
trace!("eval_place_to_op: got {:?}", *op);
|
2020-05-30 00:02:30 +02:00
|
|
|
// Sanity-check the type we ended up with.
|
|
|
|
debug_assert!(mir_assign_valid_types(
|
2020-06-14 15:02:51 +02:00
|
|
|
*self.tcx,
|
2020-06-22 09:17:33 +02:00
|
|
|
self.param_env,
|
2020-05-30 00:02:30 +02:00
|
|
|
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
|
2020-06-14 15:02:51 +02:00
|
|
|
place.ty(&self.frame().body.local_decls, *self.tcx).ty
|
2020-05-30 00:02:30 +02:00
|
|
|
))?,
|
|
|
|
op.layout,
|
|
|
|
));
|
2019-07-30 00:07:28 +02:00
|
|
|
Ok(op)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the operand, returning a place where you can then find the data.
|
2019-05-31 05:01:35 -04:00
|
|
|
/// If you already know the layout, you can save two table lookups
|
2018-08-20 15:21:04 +02:00
|
|
|
/// by passing it in here.
|
2020-10-03 11:18:24 +02:00
|
|
|
#[inline]
|
2018-08-20 15:21:04 +02:00
|
|
|
pub fn eval_operand(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-20 15:21:04 +02:00
|
|
|
mir_op: &mir::Operand<'tcx>,
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::Operand::*;
|
2018-08-15 20:18:40 +02:00
|
|
|
let op = match *mir_op {
|
2018-08-13 16:14:22 +02:00
|
|
|
// FIXME: do some more logic on `move` to invalidate the old location
|
2020-03-31 12:19:29 -03:00
|
|
|
Copy(place) | Move(place) => self.eval_place_to_op(place, layout)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2019-08-12 16:33:38 +03:00
|
|
|
Constant(ref constant) => {
|
2020-03-11 19:36:07 +00:00
|
|
|
let val =
|
|
|
|
self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal);
|
2021-01-24 12:12:08 +01:00
|
|
|
// This can still fail:
|
|
|
|
// * During ConstProp, with `TooGeneric` or since the `requried_consts` were not all
|
|
|
|
// checked yet.
|
|
|
|
// * During CTFE, since promoteds in `const`/`static` initializer bodies can fail.
|
2021-03-08 16:18:03 +00:00
|
|
|
|
|
|
|
self.mir_const_to_op(&val, layout)?
|
2019-08-12 16:33:38 +03:00
|
|
|
}
|
2018-08-15 20:18:40 +02:00
|
|
|
};
|
|
|
|
trace!("{:?}: {:?}", mir_op, *op);
|
|
|
|
Ok(op)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate a bunch of operands at once
|
2018-08-24 16:39:25 +02:00
|
|
|
pub(super) fn eval_operands(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
ops: &[mir::Operand<'tcx>],
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, Vec<OpTy<'tcx, M::PointerTag>>> {
|
2020-02-29 03:05:14 +01:00
|
|
|
ops.iter().map(|op| self.eval_operand(op, None)).collect()
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:16:02 +01:00
|
|
|
// Used when the miri-engine runs into a constant and for extracting information from constants
|
|
|
|
// in patterns via the `const_eval` module
|
2019-08-11 10:12:26 +02:00
|
|
|
/// The `val` and `layout` are assumed to already be in our interpreter
|
|
|
|
/// "universe" (param_env).
|
2020-07-27 13:01:01 +02:00
|
|
|
crate fn const_to_op(
|
2019-02-16 12:32:22 +01:00
|
|
|
&self,
|
2020-02-15 11:56:23 +13:00
|
|
|
val: &ty::Const<'tcx>,
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2019-06-07 18:56:27 +02:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2021-03-08 16:18:03 +00:00
|
|
|
match val.val {
|
2020-09-10 19:43:53 +02:00
|
|
|
ty::ConstKind::Param(_) | ty::ConstKind::Bound(..) => throw_inval!(TooGeneric),
|
2020-11-04 22:23:43 +05:30
|
|
|
ty::ConstKind::Error(_) => throw_inval!(AlreadyReported(ErrorReported)),
|
2021-03-12 00:01:34 +01:00
|
|
|
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) => {
|
2020-09-09 09:43:18 +02:00
|
|
|
let instance = self.resolve(def, substs)?;
|
2021-03-08 16:18:03 +00:00
|
|
|
Ok(self.eval_to_allocation(GlobalId { instance, promoted })?.into())
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-09-10 19:43:53 +02:00
|
|
|
ty::ConstKind::Infer(..) | ty::ConstKind::Placeholder(..) => {
|
2020-07-27 13:01:01 +02:00
|
|
|
span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", val)
|
2019-12-23 17:13:50 +01:00
|
|
|
}
|
2021-03-08 16:18:03 +00:00
|
|
|
ty::ConstKind::Value(val_val) => self.const_val_to_op(val_val, val.ty, layout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn mir_const_to_op(
|
|
|
|
&self,
|
2021-03-15 11:23:44 +00:00
|
|
|
val: &mir::ConstantKind<'tcx>,
|
2021-03-08 16:18:03 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
|
|
|
match val {
|
2021-03-15 11:23:44 +00:00
|
|
|
mir::ConstantKind::Ty(ct) => self.const_to_op(ct, layout),
|
2021-03-30 16:08:53 +00:00
|
|
|
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, ty, layout),
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
crate fn const_val_to_op(
|
|
|
|
&self,
|
|
|
|
val_val: ConstValue<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
|
2019-05-28 10:44:46 +02:00
|
|
|
// Other cases need layout.
|
2021-03-08 16:18:03 +00:00
|
|
|
let tag_scalar = |scalar| -> InterpResult<'tcx, _> {
|
|
|
|
Ok(match scalar {
|
2021-07-12 20:29:05 +02:00
|
|
|
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
|
2021-03-08 16:18:03 +00:00
|
|
|
Scalar::Int(int) => Scalar::Int(int),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
|
2019-11-08 23:11:51 +01:00
|
|
|
let op = match val_val {
|
2019-07-28 13:44:11 +02:00
|
|
|
ConstValue::ByRef { alloc, offset } => {
|
2020-04-24 12:53:18 +02:00
|
|
|
let id = self.tcx.create_memory_alloc(alloc);
|
2019-05-28 10:44:46 +02:00
|
|
|
// We rely on mutability being set correctly in that allocation to prevent writes
|
2019-05-29 15:16:18 +02:00
|
|
|
// where none should happen.
|
2020-07-26 11:11:17 +02:00
|
|
|
let ptr = self.global_base_pointer(Pointer::new(id, offset))?;
|
2021-07-12 18:22:15 +02:00
|
|
|
Operand::Indirect(MemPlace::from_ptr(ptr.into(), layout.align.abi))
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2021-07-12 18:22:15 +02:00
|
|
|
ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(x.into())?.into()),
|
2019-05-28 10:44:46 +02:00
|
|
|
ConstValue::Slice { data, start, end } => {
|
2019-05-29 15:16:18 +02:00
|
|
|
// We rely on mutability being set correctly in `data` to prevent writes
|
|
|
|
// where none should happen.
|
2019-05-28 10:44:46 +02:00
|
|
|
let ptr = Pointer::new(
|
2020-04-24 12:53:18 +02:00
|
|
|
self.tcx.create_memory_alloc(data),
|
2020-03-22 17:48:11 +01:00
|
|
|
Size::from_bytes(start), // offset: `start`
|
2019-05-28 10:44:46 +02:00
|
|
|
);
|
|
|
|
Operand::Immediate(Immediate::new_slice(
|
2021-07-12 20:29:05 +02:00
|
|
|
Scalar::from_pointer(self.global_base_pointer(ptr)?, &*self.tcx),
|
2020-03-21 13:49:02 +01:00
|
|
|
u64::try_from(end.checked_sub(start).unwrap()).unwrap(), // len: `end - start`
|
2019-05-28 10:44:46 +02:00
|
|
|
self,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Ok(OpTy { op, layout })
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-25 11:07:03 +02:00
|
|
|
/// Read discriminant, return the runtime value as well as the variant index.
|
|
|
|
pub fn read_discriminant(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2021-02-15 00:00:00 +00:00
|
|
|
op: &OpTy<'tcx, M::PointerTag>,
|
2020-04-14 12:49:15 +02:00
|
|
|
) -> InterpResult<'tcx, (Scalar<M::PointerTag>, VariantIdx)> {
|
2020-05-23 15:42:35 +02:00
|
|
|
trace!("read_discriminant_value {:#?}", op.layout);
|
|
|
|
// Get type and layout of the discriminant.
|
2020-06-14 15:02:51 +02:00
|
|
|
let discr_layout = self.layout_of(op.layout.ty.discriminant_ty(*self.tcx))?;
|
2020-05-23 15:42:35 +02:00
|
|
|
trace!("discriminant type: {:?}", discr_layout.ty);
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2020-05-23 17:24:33 +02:00
|
|
|
// We use "discriminant" to refer to the value associated with a particular enum variant.
|
2020-05-23 12:10:13 +02:00
|
|
|
// This is not to be confused with its "variant index", which is just determining its position in the
|
|
|
|
// declared list of variants -- they can differ with explicitly assigned discriminants.
|
|
|
|
// We use "tag" to refer to how the discriminant is encoded in memory, which can be either
|
2020-05-23 13:22:45 +02:00
|
|
|
// straight-forward (`TagEncoding::Direct`) or with a niche (`TagEncoding::Niche`).
|
|
|
|
let (tag_scalar_layout, tag_encoding, tag_field) = match op.layout.variants {
|
2020-03-31 18:16:47 +02:00
|
|
|
Variants::Single { index } => {
|
2020-06-14 15:02:51 +02:00
|
|
|
let discr = match op.layout.ty.discriminant_for_variant(*self.tcx, index) {
|
2020-04-14 12:49:15 +02:00
|
|
|
Some(discr) => {
|
|
|
|
// This type actually has discriminants.
|
2020-05-23 15:42:35 +02:00
|
|
|
assert_eq!(discr.ty, discr_layout.ty);
|
2020-04-14 12:49:15 +02:00
|
|
|
Scalar::from_uint(discr.val, discr_layout.size)
|
|
|
|
}
|
|
|
|
None => {
|
2020-05-23 15:42:35 +02:00
|
|
|
// On a type without actual discriminants, variant is 0.
|
2020-05-23 13:22:13 +02:00
|
|
|
assert_eq!(index.as_u32(), 0);
|
2020-04-14 12:49:15 +02:00
|
|
|
Scalar::from_uint(index.as_u32(), discr_layout.size)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return Ok((discr, index));
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2020-05-23 13:22:45 +02:00
|
|
|
Variants::Multiple { ref tag, ref tag_encoding, tag_field, .. } => {
|
|
|
|
(tag, tag_encoding, tag_field)
|
2020-03-31 18:16:47 +02:00
|
|
|
}
|
2019-03-29 07:44:54 +02:00
|
|
|
};
|
|
|
|
|
2020-05-23 15:42:35 +02:00
|
|
|
// There are *three* layouts that come into play here:
|
|
|
|
// - The discriminant has a type for typechecking. This is `discr_layout`, and is used for
|
2020-05-23 12:10:13 +02:00
|
|
|
// the `Scalar` we return.
|
2020-05-23 15:42:35 +02:00
|
|
|
// - The tag (encoded discriminant) has layout `tag_layout`. This is always an integer type,
|
|
|
|
// and used to interpret the value we read from the tag field.
|
|
|
|
// For the return value, a cast to `discr_layout` is performed.
|
|
|
|
// - The field storing the tag has a layout, which is very similar to `tag_layout` but
|
|
|
|
// may be a pointer. This is `tag_val.layout`; we just use it for sanity checks.
|
2020-04-14 12:49:15 +02:00
|
|
|
|
|
|
|
// Get layout for tag.
|
2020-06-14 15:02:51 +02:00
|
|
|
let tag_layout = self.layout_of(tag_scalar_layout.value.to_int_ty(*self.tcx))?;
|
2020-04-14 12:49:15 +02:00
|
|
|
|
2020-05-23 12:10:13 +02:00
|
|
|
// Read tag and sanity-check `tag_layout`.
|
2021-02-15 00:00:00 +00:00
|
|
|
let tag_val = self.read_immediate(&self.operand_field(op, tag_field)?)?;
|
2020-05-23 12:10:13 +02:00
|
|
|
assert_eq!(tag_layout.size, tag_val.layout.size);
|
|
|
|
assert_eq!(tag_layout.abi.is_signed(), tag_val.layout.abi.is_signed());
|
|
|
|
let tag_val = tag_val.to_scalar()?;
|
|
|
|
trace!("tag value: {:?}", tag_val);
|
2020-04-14 12:49:15 +02:00
|
|
|
|
|
|
|
// Figure out which discriminant and variant this corresponds to.
|
2020-05-23 13:22:45 +02:00
|
|
|
Ok(match *tag_encoding {
|
|
|
|
TagEncoding::Direct => {
|
2021-07-12 18:22:15 +02:00
|
|
|
let tag_bits = tag_val
|
|
|
|
.to_bits(tag_layout.size)
|
|
|
|
.map_err(|_| err_ub!(InvalidTag(tag_val.erase_for_fmt())))?;
|
2020-05-23 12:10:13 +02:00
|
|
|
// Cast bits from tag layout to discriminant layout.
|
2020-05-23 13:22:45 +02:00
|
|
|
let discr_val = self.cast_from_scalar(tag_bits, tag_layout, discr_layout.ty);
|
|
|
|
let discr_bits = discr_val.assert_bits(discr_layout.size);
|
2020-05-23 12:10:13 +02:00
|
|
|
// Convert discriminant to variant index, and catch invalid discriminants.
|
2020-08-03 00:49:11 +02:00
|
|
|
let index = match *op.layout.ty.kind() {
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::Adt(adt, _) => {
|
2020-06-14 15:02:51 +02:00
|
|
|
adt.discriminants(*self.tcx).find(|(_, var)| var.val == discr_bits)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-10-03 21:51:30 +08:00
|
|
|
ty::Generator(def_id, substs, _) => {
|
|
|
|
let substs = substs.as_generator();
|
|
|
|
substs
|
2020-06-14 15:02:51 +02:00
|
|
|
.discriminants(def_id, *self.tcx)
|
2020-04-14 12:49:15 +02:00
|
|
|
.find(|(_, var)| var.val == discr_bits)
|
2019-10-03 21:51:30 +08:00
|
|
|
}
|
2020-06-21 16:13:31 +02:00
|
|
|
_ => span_bug!(self.cur_span(), "tagged layout for non-adt non-generator"),
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2021-07-12 18:22:15 +02:00
|
|
|
.ok_or_else(|| err_ub!(InvalidTag(tag_val.erase_for_fmt())))?;
|
2020-04-14 12:49:15 +02:00
|
|
|
// Return the cast value, and the index.
|
2020-05-23 13:22:45 +02:00
|
|
|
(discr_val, index.0)
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-05-23 13:22:45 +02:00
|
|
|
TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start } => {
|
2020-05-23 12:10:13 +02:00
|
|
|
// Compute the variant this niche value/"tag" corresponds to. With niche layout,
|
|
|
|
// discriminant (encoded in niche/tag) and variant index are the same.
|
2019-09-19 09:02:45 +02:00
|
|
|
let variants_start = niche_variants.start().as_u32();
|
|
|
|
let variants_end = niche_variants.end().as_u32();
|
2021-07-12 20:29:05 +02:00
|
|
|
let variant = match tag_val.to_bits_or_ptr(tag_layout.size) {
|
2019-05-26 14:13:12 +02:00
|
|
|
Err(ptr) => {
|
2018-11-12 11:22:18 +01:00
|
|
|
// The niche must be just 0 (which an inbounds pointer value never is)
|
2019-12-22 17:42:04 -05:00
|
|
|
let ptr_valid = niche_start == 0
|
|
|
|
&& variants_start == variants_end
|
2021-07-12 18:22:15 +02:00
|
|
|
&& !self.memory.ptr_may_be_null(ptr.into());
|
2018-11-12 11:22:18 +01:00
|
|
|
if !ptr_valid {
|
2021-07-12 18:22:15 +02:00
|
|
|
throw_ub!(InvalidTag(tag_val.erase_for_fmt()))
|
2018-11-12 11:22:18 +01:00
|
|
|
}
|
2020-04-14 12:49:15 +02:00
|
|
|
dataful_variant
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-05-23 12:10:13 +02:00
|
|
|
Ok(tag_bits) => {
|
2019-09-19 09:02:45 +02:00
|
|
|
// We need to use machine arithmetic to get the relative variant idx:
|
2020-05-23 12:10:13 +02:00
|
|
|
// variant_index_relative = tag_val - niche_start_val
|
|
|
|
let tag_val = ImmTy::from_uint(tag_bits, tag_layout);
|
|
|
|
let niche_start_val = ImmTy::from_uint(niche_start, tag_layout);
|
2019-12-22 17:42:04 -05:00
|
|
|
let variant_index_relative_val =
|
2021-02-15 00:00:00 +00:00
|
|
|
self.binary_op(mir::BinOp::Sub, &tag_val, &niche_start_val)?;
|
2019-09-16 15:04:33 +02:00
|
|
|
let variant_index_relative = variant_index_relative_val
|
2019-08-17 12:57:44 +02:00
|
|
|
.to_scalar()?
|
2020-05-23 12:10:13 +02:00
|
|
|
.assert_bits(tag_val.layout.size);
|
2019-08-17 12:57:44 +02:00
|
|
|
// Check if this is in the range that indicates an actual discriminant.
|
2019-09-19 09:02:45 +02:00
|
|
|
if variant_index_relative <= u128::from(variants_end - variants_start) {
|
|
|
|
let variant_index_relative = u32::try_from(variant_index_relative)
|
|
|
|
.expect("we checked that this fits into a u32");
|
|
|
|
// Then computing the absolute variant idx should not overflow any more.
|
|
|
|
let variant_index = variants_start
|
|
|
|
.checked_add(variant_index_relative)
|
2020-03-06 12:13:55 +01:00
|
|
|
.expect("overflow computing absolute variant idx");
|
2020-05-23 15:42:35 +02:00
|
|
|
let variants_len = op
|
2020-01-03 13:31:56 +01:00
|
|
|
.layout
|
|
|
|
.ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.expect("tagged layout for non adt")
|
|
|
|
.variants
|
|
|
|
.len();
|
2020-03-21 13:49:02 +01:00
|
|
|
assert!(usize::try_from(variant_index).unwrap() < variants_len);
|
2020-04-14 12:49:15 +02:00
|
|
|
VariantIdx::from_u32(variant_index)
|
2018-08-13 16:14:22 +02:00
|
|
|
} else {
|
2020-04-14 12:49:15 +02:00
|
|
|
dataful_variant
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2020-04-14 12:49:15 +02:00
|
|
|
};
|
|
|
|
// Compute the size of the scalar we need to return.
|
2020-05-23 12:10:13 +02:00
|
|
|
// No need to cast, because the variant index directly serves as discriminant and is
|
|
|
|
// encoded in the tag.
|
2020-05-23 15:42:35 +02:00
|
|
|
(Scalar::from_uint(variant.as_u32(), discr_layout.size), variant)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-02-08 12:20:55 +01:00
|
|
|
}
|