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.
|
|
|
|
|
2023-07-24 11:44:58 +02:00
|
|
|
use std::assert_matches::assert_matches;
|
|
|
|
|
2022-11-18 10:18:32 +01:00
|
|
|
use either::{Either, Left, Right};
|
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_hir::def::Namespace;
|
2023-02-06 17:08:34 +01:00
|
|
|
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
|
2022-08-01 19:05:20 -04:00
|
|
|
use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter};
|
2022-11-15 12:06:20 +01:00
|
|
|
use rustc_middle::ty::{ConstInt, Ty, ValTree};
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::{mir, ty};
|
2022-11-15 12:06:20 +01:00
|
|
|
use rustc_span::Span;
|
2023-02-06 17:08:34 +01:00
|
|
|
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size};
|
2020-03-29 15:43:36 +02:00
|
|
|
|
|
|
|
use super::{
|
2022-07-02 16:24:42 -04:00
|
|
|
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
|
2023-07-23 21:35:54 +02:00
|
|
|
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, PlaceTy, Pointer,
|
2023-07-24 11:44:58 +02:00
|
|
|
Projectable, Provenance, Scalar,
|
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`.
|
2022-07-15 20:57:14 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub enum Immediate<Prov: Provenance = AllocId> {
|
2022-07-02 16:24:42 -04:00
|
|
|
/// A single scalar value (must have *initialized* `Scalar` ABI).
|
2022-08-01 19:05:20 -04:00
|
|
|
Scalar(Scalar<Prov>),
|
2022-07-02 16:24:42 -04:00
|
|
|
/// A pair of two scalar value (must have `ScalarPair` ABI where both fields are
|
|
|
|
/// `Scalar::Initialized`).
|
2022-08-01 19:05:20 -04:00
|
|
|
ScalarPair(Scalar<Prov>, Scalar<Prov>),
|
2023-07-20 18:40:15 +02:00
|
|
|
/// A value of fully uninitialized memory. Can have arbitrary size and layout.
|
2022-07-02 16:24:42 -04:00
|
|
|
Uninit,
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<Prov: Provenance> From<Scalar<Prov>> for Immediate<Prov> {
|
2019-07-24 19:01:12 +02:00
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from(val: Scalar<Prov>) -> Self {
|
2022-12-18 15:01:26 +01:00
|
|
|
Immediate::Scalar(val)
|
2019-07-24 19:01:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 19:05:20 -04:00
|
|
|
impl<Prov: Provenance> Immediate<Prov> {
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn from_pointer(p: Pointer<Prov>, cx: &impl HasDataLayout) -> Self {
|
2022-08-01 19:05:20 -04:00
|
|
|
Immediate::Scalar(Scalar::from_pointer(p, cx))
|
2021-07-12 20:29:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn from_maybe_pointer(p: Pointer<Option<Prov>>, cx: &impl HasDataLayout) -> Self {
|
2022-08-01 19:05:20 -04:00
|
|
|
Immediate::Scalar(Scalar::from_maybe_pointer(p, cx))
|
2019-11-29 11:09:26 +01:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn new_slice(val: Scalar<Prov>, len: u64, cx: &impl HasDataLayout) -> Self {
|
2023-02-14 14:31:26 +00:00
|
|
|
Immediate::ScalarPair(val, Scalar::from_target_usize(len, cx))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 20:09:22 +08:00
|
|
|
pub fn new_dyn_trait(
|
2022-07-18 18:47:31 -04:00
|
|
|
val: Scalar<Prov>,
|
|
|
|
vtable: Pointer<Option<Prov>>,
|
2021-08-01 20:09:22 +08:00
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
) -> Self {
|
2022-12-18 15:01:26 +01:00
|
|
|
Immediate::ScalarPair(val, Scalar::from_maybe_pointer(vtable, cx))
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2022-07-08 07:33:19 -04:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
|
2022-08-01 19:05:20 -04:00
|
|
|
pub fn to_scalar(self) -> Scalar<Prov> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
2018-10-26 12:33:26 +02:00
|
|
|
Immediate::Scalar(val) => val,
|
2021-08-01 20:09:22 +08:00
|
|
|
Immediate::ScalarPair(..) => bug!("Got a scalar pair where a scalar was expected"),
|
2022-08-01 19:05:20 -04:00
|
|
|
Immediate::Uninit => bug!("Got uninit where a scalar was expected"),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2022-07-08 07:33:19 -04:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)] // only in debug builds due to perf (see #98980)
|
2022-08-01 19:05:20 -04:00
|
|
|
pub fn to_scalar_pair(self) -> (Scalar<Prov>, Scalar<Prov>) {
|
2021-07-31 22:46:23 +08:00
|
|
|
match self {
|
2022-05-04 22:47:46 +02:00
|
|
|
Immediate::ScalarPair(val1, val2) => (val1, val2),
|
|
|
|
Immediate::Scalar(..) => bug!("Got a scalar where a scalar pair was expected"),
|
2022-08-01 19:05:20 -04:00
|
|
|
Immediate::Uninit => bug!("Got uninit where a scalar pair was expected"),
|
2021-07-31 22:46:23 +08:00
|
|
|
}
|
|
|
|
}
|
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.
|
2022-07-15 22:58:20 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub struct ImmTy<'tcx, Prov: Provenance = AllocId> {
|
|
|
|
imm: Immediate<Prov>,
|
2020-03-04 14:50:21 +00:00
|
|
|
pub layout: TyAndLayout<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<Prov: Provenance> std::fmt::Display for ImmTy<'_, Prov> {
|
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
|
2022-07-18 18:47:31 -04:00
|
|
|
fn p<'a, 'tcx, Prov: Provenance>(
|
2022-02-18 16:15:29 -05:00
|
|
|
cx: FmtPrinter<'a, 'tcx>,
|
2022-08-01 19:05:20 -04:00
|
|
|
s: Scalar<Prov>,
|
2019-12-23 17:41:06 +01:00
|
|
|
ty: Ty<'tcx>,
|
2022-02-18 16:15:29 -05:00
|
|
|
) -> Result<FmtPrinter<'a, 'tcx>, std::fmt::Error> {
|
2019-12-23 17:41:06 +01:00
|
|
|
match s {
|
2022-08-01 19:05:20 -04:00
|
|
|
Scalar::Int(int) => cx.pretty_print_const_scalar_int(int, ty, true),
|
|
|
|
Scalar::Ptr(ptr, _sz) => {
|
2021-07-16 09:39:35 +02:00
|
|
|
// Just print the ptr value. `pretty_print_const_scalar_ptr` would also try to
|
|
|
|
// print what is points to, which would fail since it has no access to the local
|
|
|
|
// memory.
|
2023-06-28 09:43:31 +00:00
|
|
|
cx.pretty_print_const_pointer(ptr, ty)
|
2019-12-23 17:41:06 +01:00
|
|
|
}
|
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) {
|
2022-02-18 16:15:29 -05:00
|
|
|
let cx = FmtPrinter::new(tcx, Namespace::ValueNS);
|
|
|
|
f.write_str(&p(cx, s, ty)?.into_buffer())?;
|
2019-12-23 17:41:06 +01:00
|
|
|
return Ok(());
|
2019-11-17 14:30:08 +01:00
|
|
|
}
|
2022-02-21 21:46:51 -05:00
|
|
|
write!(f, "{:x}: {}", s, 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
|
2022-07-02 16:24:42 -04:00
|
|
|
write!(f, "({:x}, {:x}): {}", a, b, self.layout.ty)
|
|
|
|
}
|
|
|
|
Immediate::Uninit => {
|
|
|
|
write!(f, "uninit: {}", self.layout.ty)
|
2019-11-17 14:30:08 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> std::ops::Deref for ImmTy<'tcx, Prov> {
|
|
|
|
type Target = Immediate<Prov>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn deref(&self) -> &Immediate<Prov> {
|
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.
|
2022-07-15 20:57:14 -04:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub enum Operand<Prov: Provenance = AllocId> {
|
|
|
|
Immediate(Immediate<Prov>),
|
|
|
|
Indirect(MemPlace<Prov>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-15 22:58:20 -04:00
|
|
|
#[derive(Clone, Debug)]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub struct OpTy<'tcx, Prov: Provenance = AllocId> {
|
|
|
|
op: Operand<Prov>, // Keep this private; it helps enforce invariants.
|
2020-03-04 14:50:21 +00:00
|
|
|
pub layout: TyAndLayout<'tcx>,
|
2022-07-03 10:21:47 -04:00
|
|
|
/// rustc does not have a proper way to represent the type of a field of a `repr(packed)` struct:
|
|
|
|
/// it needs to have a different alignment than the field type would usually have.
|
|
|
|
/// So we represent this here with a separate field that "overwrites" `layout.align`.
|
|
|
|
/// This means `layout.align` should never be used for an `OpTy`!
|
|
|
|
/// `None` means "alignment does not matter since this is a by-value operand"
|
2022-07-04 09:05:23 -04:00
|
|
|
/// (`Operand::Immediate`); this field is only relevant for `Operand::Indirect`.
|
|
|
|
/// Also CTFE ignores alignment anyway, so this is for Miri only.
|
2022-07-03 10:21:47 -04:00
|
|
|
pub align: Option<Align>,
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> std::ops::Deref for OpTy<'tcx, Prov> {
|
|
|
|
type Target = Operand<Prov>;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn deref(&self) -> &Operand<Prov> {
|
2018-08-13 16:14:22 +02:00
|
|
|
&self.op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> From<MPlaceTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from(mplace: MPlaceTy<'tcx, Prov>) -> Self {
|
2022-07-03 10:21:47 -04:00
|
|
|
OpTy { op: Operand::Indirect(*mplace), layout: mplace.layout, align: Some(mplace.align) }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> From<ImmTy<'tcx, Prov>> for OpTy<'tcx, Prov> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2022-07-18 18:47:31 -04:00
|
|
|
fn from(val: ImmTy<'tcx, Prov>) -> Self {
|
2022-07-03 10:21:47 -04:00
|
|
|
OpTy { op: Operand::Immediate(val.imm), layout: val.layout, align: None }
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
|
2019-02-08 14:00:52 +01:00
|
|
|
#[inline]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn from_scalar(val: Scalar<Prov>, 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]
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn from_immediate(imm: Immediate<Prov>, layout: TyAndLayout<'tcx>) -> Self {
|
2020-04-13 17:07:54 +02:00
|
|
|
ImmTy { imm, layout }
|
|
|
|
}
|
|
|
|
|
2022-07-04 08:48:05 -04:00
|
|
|
#[inline]
|
|
|
|
pub fn uninit(layout: TyAndLayout<'tcx>) -> Self {
|
|
|
|
ImmTy { imm: Immediate::Uninit, 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-16 19:50:59 +02:00
|
|
|
pub fn to_const_int(self) -> ConstInt {
|
2020-06-19 18:57:15 +02:00
|
|
|
assert!(self.layout.ty.is_integral());
|
2022-08-01 19:05:20 -04:00
|
|
|
let int = self.to_scalar().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
|
|
|
}
|
2023-07-23 21:35:54 +02:00
|
|
|
|
|
|
|
/// Compute the "sub-immediate" that is located within the `base` at the given offset with the
|
|
|
|
/// given layout.
|
2023-07-24 11:44:58 +02:00
|
|
|
// Not called `offset` to avoid confusion with the trait method.
|
|
|
|
fn offset_(&self, offset: Size, layout: TyAndLayout<'tcx>, cx: &impl HasDataLayout) -> Self {
|
2023-07-23 21:35:54 +02:00
|
|
|
// This makes several assumptions about what layouts we will encounter; we match what
|
|
|
|
// codegen does as good as we can (see `extract_field` in `rustc_codegen_ssa/src/mir/operand.rs`).
|
|
|
|
let inner_val: Immediate<_> = match (**self, self.layout.abi) {
|
|
|
|
// if the entire value is uninit, then so is the field (can happen in ConstProp)
|
|
|
|
(Immediate::Uninit, _) => Immediate::Uninit,
|
|
|
|
// the field contains no information, can be left uninit
|
2023-08-27 18:12:34 +02:00
|
|
|
// (Scalar/ScalarPair can contain even aligned ZST, not just 1-ZST)
|
2023-07-23 21:35:54 +02:00
|
|
|
_ if layout.is_zst() => Immediate::Uninit,
|
2023-07-24 11:44:58 +02:00
|
|
|
// some fieldless enum variants can have non-zero size but still `Aggregate` ABI... try
|
|
|
|
// to detect those here and also give them no data
|
|
|
|
_ if matches!(layout.abi, Abi::Aggregate { .. })
|
|
|
|
&& matches!(&layout.fields, abi::FieldsShape::Arbitrary { offsets, .. } if offsets.len() == 0) =>
|
|
|
|
{
|
|
|
|
Immediate::Uninit
|
|
|
|
}
|
2023-07-23 21:35:54 +02:00
|
|
|
// the field covers the entire type
|
|
|
|
_ if layout.size == self.layout.size => {
|
2023-07-24 11:44:58 +02:00
|
|
|
assert_eq!(offset.bytes(), 0);
|
|
|
|
assert!(
|
|
|
|
match (self.layout.abi, layout.abi) {
|
|
|
|
(Abi::Scalar(..), Abi::Scalar(..)) => true,
|
|
|
|
(Abi::ScalarPair(..), Abi::ScalarPair(..)) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
"cannot project into {} immediate with equally-sized field {}\nouter ABI: {:#?}\nfield ABI: {:#?}",
|
|
|
|
self.layout.ty,
|
|
|
|
layout.ty,
|
|
|
|
self.layout.abi,
|
|
|
|
layout.abi,
|
|
|
|
);
|
2023-07-23 21:35:54 +02:00
|
|
|
**self
|
|
|
|
}
|
|
|
|
// extract fields from types with `ScalarPair` ABI
|
|
|
|
(Immediate::ScalarPair(a_val, b_val), Abi::ScalarPair(a, b)) => {
|
|
|
|
assert!(matches!(layout.abi, Abi::Scalar(..)));
|
|
|
|
Immediate::from(if offset.bytes() == 0 {
|
|
|
|
debug_assert_eq!(layout.size, a.size(cx));
|
|
|
|
a_val
|
|
|
|
} else {
|
|
|
|
debug_assert_eq!(offset, a.size(cx).align_to(b.align(cx).abi));
|
|
|
|
debug_assert_eq!(layout.size, b.size(cx));
|
|
|
|
b_val
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// everything else is a bug
|
|
|
|
_ => bug!("invalid field access on immediate {}, layout {:#?}", self, self.layout),
|
|
|
|
};
|
|
|
|
|
|
|
|
ImmTy::from_immediate(inner_val, layout)
|
|
|
|
}
|
2019-02-08 14:00:52 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for ImmTy<'tcx, Prov> {
|
2023-07-24 11:44:58 +02:00
|
|
|
#[inline(always)]
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
|
|
|
self.layout
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
fn meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
|
2023-07-24 11:44:58 +02:00
|
|
|
&self,
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
) -> InterpResult<'tcx, MemPlaceMeta<M::Provenance>> {
|
|
|
|
assert!(self.layout.is_sized()); // unsized ImmTy can only exist temporarily and should never reach this here
|
|
|
|
Ok(MemPlaceMeta::None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn offset_with_meta(
|
|
|
|
&self,
|
|
|
|
offset: Size,
|
|
|
|
meta: MemPlaceMeta<Prov>,
|
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
|
|
|
assert_matches!(meta, MemPlaceMeta::None); // we can't store this anywhere anyway
|
|
|
|
Ok(self.offset_(offset, layout, cx))
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
fn to_op<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
|
2023-07-24 11:44:58 +02:00
|
|
|
&self,
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2023-07-25 22:19:18 +02:00
|
|
|
Ok(self.clone().into())
|
2023-07-24 11:44:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 18:47:31 -04:00
|
|
|
impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
|
2023-07-24 11:44:58 +02:00
|
|
|
// Provided as inherent method since it doesn't need the `ecx` of `Projectable::meta`.
|
|
|
|
pub fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>> {
|
2023-07-23 21:35:54 +02:00
|
|
|
Ok(if self.layout.is_unsized() {
|
|
|
|
if matches!(self.op, Operand::Immediate(_)) {
|
|
|
|
// Unsized immediate OpTy cannot occur. We create a MemPlace for all unsized locals during argument passing.
|
|
|
|
// However, ConstProp doesn't do that, so we can run into this nonsense situation.
|
|
|
|
throw_inval!(ConstPropNonsense);
|
2023-04-27 12:22:21 +02:00
|
|
|
}
|
2022-07-04 08:48:05 -04:00
|
|
|
// There are no unsized immediates.
|
2023-07-23 21:35:54 +02:00
|
|
|
self.assert_mem_place().meta
|
2022-07-04 08:48:05 -04:00
|
|
|
} else {
|
2023-07-23 21:35:54 +02:00
|
|
|
MemPlaceMeta::None
|
|
|
|
})
|
2022-07-04 08:48:05 -04:00
|
|
|
}
|
2023-07-24 11:44:58 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
impl<'tcx, Prov: Provenance + 'static> Projectable<'tcx, Prov> for OpTy<'tcx, Prov> {
|
2023-07-24 11:44:58 +02:00
|
|
|
#[inline(always)]
|
|
|
|
fn layout(&self) -> TyAndLayout<'tcx> {
|
|
|
|
self.layout
|
|
|
|
}
|
2022-07-04 08:48:05 -04:00
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
fn meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
|
2023-07-24 11:44:58 +02:00
|
|
|
&self,
|
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
) -> InterpResult<'tcx, MemPlaceMeta<M::Provenance>> {
|
|
|
|
self.meta()
|
2023-02-06 16:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-07-24 11:44:58 +02:00
|
|
|
fn offset_with_meta(
|
2022-07-04 08:48:05 -04:00
|
|
|
&self,
|
|
|
|
offset: Size,
|
2022-07-18 18:47:31 -04:00
|
|
|
meta: MemPlaceMeta<Prov>,
|
2022-07-04 08:48:05 -04:00
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
cx: &impl HasDataLayout,
|
|
|
|
) -> InterpResult<'tcx, Self> {
|
2022-11-18 10:18:32 +01:00
|
|
|
match self.as_mplace_or_imm() {
|
|
|
|
Left(mplace) => Ok(mplace.offset_with_meta(offset, meta, layout, cx)?.into()),
|
|
|
|
Right(imm) => {
|
2022-07-04 08:48:05 -04:00
|
|
|
assert!(!meta.has_meta()); // no place to store metadata here
|
|
|
|
// Every part of an uninit is uninit.
|
2023-07-24 11:44:58 +02:00
|
|
|
Ok(imm.offset(offset, layout, cx)?.into())
|
2022-07-04 08:48:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 20:32:45 -04:00
|
|
|
|
2023-07-25 22:04:02 +02:00
|
|
|
fn to_op<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
|
2022-07-14 20:32:45 -04:00
|
|
|
&self,
|
2023-07-24 11:44:58 +02:00
|
|
|
_ecx: &InterpCx<'mir, 'tcx, M>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
|
|
|
Ok(self.clone())
|
2022-07-14 20:32:45 -04:00
|
|
|
}
|
2022-07-04 08:48:05 -04:00
|
|
|
}
|
|
|
|
|
2023-07-25 22:19:18 +02:00
|
|
|
pub trait Readable<'tcx, Prov: Provenance>: Projectable<'tcx, Prov> {
|
|
|
|
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, Prov: Provenance + 'static> Readable<'tcx, Prov> for OpTy<'tcx, Prov> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
|
|
|
|
self.as_mplace_or_imm()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, Prov: Provenance + 'static> Readable<'tcx, Prov> for MPlaceTy<'tcx, Prov> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
|
|
|
|
Left(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, Prov: Provenance> Readable<'tcx, Prov> for ImmTy<'tcx, Prov> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
|
|
|
|
Right(self.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-05 09:55:38 +02:00
|
|
|
///
|
|
|
|
/// This is an internal function; call `read_immediate` instead.
|
|
|
|
fn read_immediate_from_mplace_raw(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
mplace: &MPlaceTy<'tcx, M::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::Provenance>>> {
|
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
|
|
|
|
2022-04-03 13:05:49 -04:00
|
|
|
let Some(alloc) = self.get_place_alloc(mplace)? else {
|
2022-07-04 08:48:05 -04:00
|
|
|
// zero-sized type can be left uninit
|
|
|
|
return Ok(Some(ImmTy::uninit(mplace.layout)));
|
2019-06-23 14:26:36 +02:00
|
|
|
};
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2022-03-03 12:02:12 +00:00
|
|
|
// It may seem like all types with `Scalar` or `ScalarPair` ABI are fair game at this point.
|
|
|
|
// However, `MaybeUninit<u64>` is considered a `Scalar` as far as its layout is concerned --
|
|
|
|
// and yet cannot be represented by an interpreter `Scalar`, since we have to handle the
|
2022-03-03 12:02:52 +00:00
|
|
|
// case where some of the bytes are initialized and others are not. So, we need an extra
|
|
|
|
// check that walks over the type of `mplace` to make sure it is truly correct to treat this
|
|
|
|
// like a `Scalar` (or `ScalarPair`).
|
2022-08-01 19:05:20 -04:00
|
|
|
Ok(match mplace.layout.abi {
|
|
|
|
Abi::Scalar(abi::Scalar::Initialized { value: s, .. }) => {
|
|
|
|
let size = s.size(self);
|
|
|
|
assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size");
|
|
|
|
let scalar = alloc.read_scalar(
|
|
|
|
alloc_range(Size::ZERO, size),
|
2023-01-22 23:03:58 -05:00
|
|
|
/*read_provenance*/ matches!(s, abi::Pointer(_)),
|
2022-08-01 19:05:20 -04:00
|
|
|
)?;
|
|
|
|
Some(ImmTy { imm: scalar.into(), layout: mplace.layout })
|
|
|
|
}
|
2022-03-03 12:02:52 +00:00
|
|
|
Abi::ScalarPair(
|
|
|
|
abi::Scalar::Initialized { value: a, .. },
|
|
|
|
abi::Scalar::Initialized { value: b, .. },
|
2022-08-01 19:05:20 -04: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.
|
|
|
|
let (a_size, b_size) = (a.size(self), b.size(self));
|
|
|
|
let b_offset = a_size.align_to(b.align(self).abi);
|
|
|
|
assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
|
|
|
|
let a_val = alloc.read_scalar(
|
|
|
|
alloc_range(Size::ZERO, a_size),
|
2023-01-22 23:03:58 -05:00
|
|
|
/*read_provenance*/ matches!(a, abi::Pointer(_)),
|
2022-08-01 19:05:20 -04:00
|
|
|
)?;
|
|
|
|
let b_val = alloc.read_scalar(
|
|
|
|
alloc_range(b_offset, b_size),
|
2023-01-22 23:03:58 -05:00
|
|
|
/*read_provenance*/ matches!(b, abi::Pointer(_)),
|
2022-08-01 19:05:20 -04:00
|
|
|
)?;
|
2022-12-18 15:01:26 +01:00
|
|
|
Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout })
|
2022-08-01 19:05:20 -04:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Neither a scalar nor scalar pair.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 09:55:38 +02:00
|
|
|
/// Try returning an immediate for the operand. If the layout does not permit loading this as an
|
|
|
|
/// immediate, return where in memory we can find the data.
|
2022-11-18 10:18:32 +01:00
|
|
|
/// Note that for a given layout, this operation will either always return Left or Right!
|
|
|
|
/// succeed! Whether it returns Left depends on whether the layout can be represented
|
2021-08-22 14:46:15 +02:00
|
|
|
/// in an `Immediate`, not on which data is stored there currently.
|
2022-05-04 22:47:46 +02:00
|
|
|
///
|
2022-05-05 09:55:38 +02:00
|
|
|
/// This is an internal function that should not usually be used; call `read_immediate` instead.
|
2022-07-04 08:48:05 -04:00
|
|
|
/// ConstProp needs it, though.
|
2022-05-05 09:55:38 +02:00
|
|
|
pub fn read_immediate_raw(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2023-07-25 22:19:18 +02:00
|
|
|
src: &impl Readable<'tcx, M::Provenance>,
|
2022-11-18 14:24:48 +01:00
|
|
|
) -> InterpResult<'tcx, Either<MPlaceTy<'tcx, M::Provenance>, ImmTy<'tcx, M::Provenance>>> {
|
2022-11-18 10:18:32 +01:00
|
|
|
Ok(match src.as_mplace_or_imm() {
|
|
|
|
Left(ref mplace) => {
|
2022-08-01 19:05:20 -04:00
|
|
|
if let Some(val) = self.read_immediate_from_mplace_raw(mplace)? {
|
2022-11-18 14:24:48 +01:00
|
|
|
Right(val)
|
2018-08-16 09:36:25 +02:00
|
|
|
} else {
|
2023-07-25 22:35:07 +02:00
|
|
|
Left(mplace.clone())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-18 14:24:48 +01:00
|
|
|
Right(val) => Right(val),
|
2018-08-16 09:36:25 +02:00
|
|
|
})
|
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.
|
2022-08-01 19:05:20 -04:00
|
|
|
///
|
2022-11-13 15:26:17 +08:00
|
|
|
/// If this succeeds, the `ImmTy` is never `Uninit`.
|
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,
|
2023-07-25 22:19:18 +02:00
|
|
|
op: &impl Readable<'tcx, M::Provenance>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
|
2022-08-01 19:05:20 -04:00
|
|
|
if !matches!(
|
2023-07-25 22:19:18 +02:00
|
|
|
op.layout().abi,
|
2022-08-01 19:05:20 -04:00
|
|
|
Abi::Scalar(abi::Scalar::Initialized { .. })
|
|
|
|
| Abi::ScalarPair(abi::Scalar::Initialized { .. }, abi::Scalar::Initialized { .. })
|
|
|
|
) {
|
2023-07-25 22:19:18 +02:00
|
|
|
span_bug!(
|
|
|
|
self.cur_span(),
|
|
|
|
"primitive read not possible for type: {:?}",
|
|
|
|
op.layout().ty
|
|
|
|
);
|
2022-08-01 19:05:20 -04:00
|
|
|
}
|
2022-11-18 14:24:48 +01:00
|
|
|
let imm = self.read_immediate_raw(op)?.right().unwrap();
|
2022-08-01 19:05:20 -04:00
|
|
|
if matches!(*imm, Immediate::Uninit) {
|
|
|
|
throw_ub!(InvalidUninitBytes(None));
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2022-08-01 19:05:20 -04:00
|
|
|
Ok(imm)
|
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,
|
2023-07-25 22:19:18 +02:00
|
|
|
op: &impl Readable<'tcx, M::Provenance>,
|
2022-08-01 19:05:20 -04:00
|
|
|
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
|
|
|
|
Ok(self.read_immediate(op)?.to_scalar())
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:10:19 +01:00
|
|
|
// Pointer-sized reads are fairly common and need target layout access, so we wrap them in
|
|
|
|
// convenience functions.
|
|
|
|
|
2021-07-12 18:22:15 +02:00
|
|
|
/// Read a pointer from a place.
|
|
|
|
pub fn read_pointer(
|
|
|
|
&self,
|
2023-07-25 22:19:18 +02:00
|
|
|
op: &impl Readable<'tcx, M::Provenance>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
|
2022-07-23 10:36:57 -04:00
|
|
|
self.read_scalar(op)?.to_pointer(self)
|
2021-07-12 18:22:15 +02:00
|
|
|
}
|
2022-12-12 11:10:19 +01:00
|
|
|
/// Read a pointer-sized unsigned integer from a place.
|
2023-07-25 22:19:18 +02:00
|
|
|
pub fn read_target_usize(
|
|
|
|
&self,
|
|
|
|
op: &impl Readable<'tcx, M::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, u64> {
|
2023-02-14 14:31:26 +00:00
|
|
|
self.read_scalar(op)?.to_target_usize(self)
|
2022-12-12 11:10:19 +01:00
|
|
|
}
|
|
|
|
/// Read a pointer-sized signed integer from a place.
|
2023-07-25 22:19:18 +02:00
|
|
|
pub fn read_target_isize(
|
|
|
|
&self,
|
|
|
|
op: &impl Readable<'tcx, M::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, i64> {
|
2023-02-14 14:31:26 +00:00
|
|
|
self.read_scalar(op)?.to_target_isize(self)
|
2022-12-12 11:10:19 +01:00
|
|
|
}
|
2021-07-12 18:22:15 +02:00
|
|
|
|
2022-07-02 16:24:42 -04:00
|
|
|
/// Turn the wide MPlace into a string (must already be dereferenced!)
|
2022-07-18 18:47:31 -04:00
|
|
|
pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx, &str> {
|
2018-08-25 14:36:24 +02:00
|
|
|
let len = mplace.len(self)?;
|
2022-08-27 14:54:02 -04:00
|
|
|
let bytes = self.read_bytes_ptr_strip_provenance(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
|
|
|
}
|
|
|
|
|
2021-11-17 22:29:21 -05:00
|
|
|
/// Converts a repr(simd) operand into an operand where `place_index` accesses the SIMD elements.
|
|
|
|
/// Also returns the number of elements.
|
2022-07-04 08:48:05 -04:00
|
|
|
///
|
|
|
|
/// Can (but does not always) trigger UB if `op` is uninitialized.
|
2021-11-17 22:29:21 -05:00
|
|
|
pub fn operand_to_simd(
|
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
op: &OpTy<'tcx, M::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::Provenance>, u64)> {
|
2021-11-17 22:29:21 -05:00
|
|
|
// Basically we just transmute this place into an array following simd_size_and_type.
|
|
|
|
// This only works in memory, but repr(simd) types should never be immediates anyway.
|
2022-07-04 08:48:05 -04:00
|
|
|
assert!(op.layout.ty.is_simd());
|
2022-11-18 10:18:32 +01:00
|
|
|
match op.as_mplace_or_imm() {
|
|
|
|
Left(mplace) => self.mplace_to_simd(&mplace),
|
|
|
|
Right(imm) => match *imm {
|
2022-07-04 08:48:05 -04:00
|
|
|
Immediate::Uninit => {
|
|
|
|
throw_ub!(InvalidUninitBytes(None))
|
|
|
|
}
|
|
|
|
Immediate::Scalar(..) | Immediate::ScalarPair(..) => {
|
|
|
|
bug!("arrays/slices can never have Scalar/ScalarPair layout")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-11-17 22:29:21 -05:00
|
|
|
}
|
|
|
|
|
2022-08-07 12:33:44 -04:00
|
|
|
/// Read from a local.
|
2020-06-26 11:02:43 +02:00
|
|
|
/// 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
|
2022-07-02 16:24:42 -04:00
|
|
|
/// OpTy from a local.
|
2022-07-03 10:21:47 -04:00
|
|
|
pub fn local_to_op(
|
2018-10-20 11:02:39 +02:00
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
frame: &Frame<'mir, 'tcx, M::Provenance, 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>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2019-01-30 14:55:31 +01:00
|
|
|
let layout = self.layout_of_local(frame, local, layout)?;
|
2022-08-07 12:33:44 -04:00
|
|
|
let op = *frame.locals[local].access()?;
|
2022-07-03 10:21:47 -04:00
|
|
|
Ok(OpTy { op, layout, align: Some(layout.align.abi) })
|
2018-10-20 11:02:39 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
pub fn place_to_op(
|
|
|
|
&self,
|
2022-07-18 18:47:31 -04:00
|
|
|
place: &PlaceTy<'tcx, M::Provenance>,
|
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2023-07-23 21:35:54 +02:00
|
|
|
match place.as_mplace_or_local() {
|
|
|
|
Left(mplace) => Ok(mplace.into()),
|
|
|
|
Right((frame, local, offset)) => {
|
|
|
|
let base = self.local_to_op(&self.stack()[frame], local, None)?;
|
|
|
|
let mut field = if let Some(offset) = offset {
|
|
|
|
// This got offset. We can be sure that the field is sized.
|
|
|
|
base.offset(offset, place.layout, self)?
|
|
|
|
} else {
|
|
|
|
assert_eq!(place.layout, base.layout);
|
|
|
|
// Unsized cases are possible here since an unsized local will be a
|
|
|
|
// `Place::Local` until the first projection calls `place_to_op` to extract the
|
|
|
|
// underlying mplace.
|
|
|
|
base
|
|
|
|
};
|
|
|
|
field.align = Some(place.align);
|
|
|
|
Ok(field)
|
2020-03-16 15:12:42 -07:00
|
|
|
}
|
2023-07-23 21:35:54 +02:00
|
|
|
}
|
2019-02-08 12:20:55 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 20:34:16 +00:00
|
|
|
/// Evaluate a place with the goal of reading from it. This lets us sometimes
|
2022-03-24 13:37:18 -05: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,
|
2022-07-11 13:42:08 -04:00
|
|
|
mir_place: mir::Place<'tcx>,
|
2020-03-04 14:50:21 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
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.
|
2022-07-11 13:42:08 -04:00
|
|
|
let layout = if mir_place.projection.is_empty() { layout } else { None };
|
2020-04-15 23:29:29 +02:00
|
|
|
|
2022-07-11 13:42:08 -04:00
|
|
|
let mut op = self.local_to_op(self.frame(), mir_place.local, layout)?;
|
|
|
|
// Using `try_fold` turned out to be bad for performance, hence the loop.
|
|
|
|
for elem in mir_place.projection.iter() {
|
2023-07-24 11:44:58 +02:00
|
|
|
op = self.project(&op, elem)?
|
2022-07-11 13:42:08 -04:00
|
|
|
}
|
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.
|
2022-07-04 08:48:05 -04:00
|
|
|
debug_assert!(
|
|
|
|
mir_assign_valid_types(
|
|
|
|
*self.tcx,
|
|
|
|
self.param_env,
|
|
|
|
self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
|
2022-07-11 13:42:08 -04:00
|
|
|
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty
|
2022-07-04 08:48:05 -04:00
|
|
|
)?)?,
|
|
|
|
op.layout,
|
|
|
|
),
|
|
|
|
"eval_place of a MIR place with type {:?} produced an interpreter operand with type {:?}",
|
2022-07-11 13:42:08 -04:00
|
|
|
mir_place.ty(&self.frame().body.local_decls, *self.tcx).ty,
|
2022-07-04 08:48:05 -04:00
|
|
|
op.layout.ty,
|
|
|
|
);
|
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>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::Operand::*;
|
2022-12-23 15:15:21 +00: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
|
2023-01-12 18:50:32 +00:00
|
|
|
&Copy(place) | &Move(place) => self.eval_place_to_op(place, layout)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2022-12-23 15:15:21 +00:00
|
|
|
Constant(constant) => {
|
2022-11-15 12:06:20 +01:00
|
|
|
let c =
|
2021-12-05 11:13:51 +01:00
|
|
|
self.subst_from_current_frame_and_normalize_erasing_regions(constant.literal)?;
|
2022-07-03 10:21:47 -04:00
|
|
|
|
2021-01-24 12:12:08 +01:00
|
|
|
// This can still fail:
|
2022-03-03 19:47:23 +08:00
|
|
|
// * During ConstProp, with `TooGeneric` or since the `required_consts` were not all
|
2021-01-24 12:12:08 +01:00
|
|
|
// checked yet.
|
|
|
|
// * During CTFE, since promoteds in `const`/`static` initializer bodies can fail.
|
2022-11-15 12:06:20 +01:00
|
|
|
self.eval_mir_constant(&c, Some(constant.span), 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
|
|
|
}
|
|
|
|
|
2022-11-15 12:06:20 +01:00
|
|
|
fn eval_ty_constant(
|
|
|
|
&self,
|
|
|
|
val: ty::Const<'tcx>,
|
|
|
|
span: Option<Span>,
|
|
|
|
) -> InterpResult<'tcx, ValTree<'tcx>> {
|
|
|
|
Ok(match val.kind() {
|
|
|
|
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
|
|
|
|
throw_inval!(TooGeneric)
|
|
|
|
}
|
2022-11-24 08:20:51 +00:00
|
|
|
// FIXME(generic_const_exprs): `ConstKind::Expr` should be able to be evaluated
|
2022-07-27 07:27:52 +00:00
|
|
|
ty::ConstKind::Expr(_) => throw_inval!(TooGeneric),
|
2022-11-15 12:06:20 +01:00
|
|
|
ty::ConstKind::Error(reported) => {
|
2023-05-15 00:00:00 +00:00
|
|
|
throw_inval!(AlreadyReported(reported.into()))
|
2022-11-15 12:06:20 +01:00
|
|
|
}
|
|
|
|
ty::ConstKind::Unevaluated(uv) => {
|
2023-07-11 22:35:29 +01:00
|
|
|
let instance = self.resolve(uv.def, uv.args)?;
|
2022-11-15 12:06:20 +01:00
|
|
|
let cid = GlobalId { instance, promoted: None };
|
2023-07-27 15:50:42 +00:00
|
|
|
self.ctfe_query(span, |tcx| tcx.eval_to_valtree(self.param_env.and(cid)))?
|
|
|
|
.unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"))
|
2022-11-15 12:06:20 +01:00
|
|
|
}
|
|
|
|
ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
|
|
|
|
span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {val:?}")
|
|
|
|
}
|
|
|
|
ty::ConstKind::Value(valtree) => valtree,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eval_mir_constant(
|
2021-03-08 16:18:03 +00:00
|
|
|
&self,
|
2021-03-15 11:23:44 +00:00
|
|
|
val: &mir::ConstantKind<'tcx>,
|
2022-11-15 12:06:20 +01:00
|
|
|
span: Option<Span>,
|
2021-03-08 16:18:03 +00:00
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2023-04-04 10:39:26 +00:00
|
|
|
match *val {
|
2022-09-19 16:17:33 +02:00
|
|
|
mir::ConstantKind::Ty(ct) => {
|
2022-11-15 12:06:20 +01:00
|
|
|
let ty = ct.ty();
|
|
|
|
let valtree = self.eval_ty_constant(ct, span)?;
|
|
|
|
let const_val = self.tcx.valtree_to_const_val((ty, valtree));
|
|
|
|
self.const_val_to_op(const_val, ty, layout)
|
2022-09-19 16:17:33 +02:00
|
|
|
}
|
2022-10-20 17:53:29 +00:00
|
|
|
mir::ConstantKind::Val(val, ty) => self.const_val_to_op(val, ty, layout),
|
2022-09-14 15:35:24 +02:00
|
|
|
mir::ConstantKind::Unevaluated(uv, _) => {
|
2023-07-11 22:35:29 +01:00
|
|
|
let instance = self.resolve(uv.def, uv.args)?;
|
2022-11-15 12:06:20 +01:00
|
|
|
Ok(self.eval_global(GlobalId { instance, promoted: uv.promoted }, span)?.into())
|
2022-09-14 15:35:24 +02:00
|
|
|
}
|
2021-03-08 16:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-28 09:23:34 +00:00
|
|
|
pub(crate) fn const_val_to_op(
|
2021-03-08 16:18:03 +00:00
|
|
|
&self,
|
|
|
|
val_val: ConstValue<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
layout: Option<TyAndLayout<'tcx>>,
|
2022-07-18 18:47:31 -04:00
|
|
|
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
|
2019-05-28 10:44:46 +02:00
|
|
|
// Other cases need layout.
|
2022-07-18 18:47:31 -04:00
|
|
|
let adjust_scalar = |scalar| -> InterpResult<'tcx, _> {
|
2021-03-08 16:18:03 +00:00
|
|
|
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))?;
|
2022-07-03 10:21:47 -04:00
|
|
|
Operand::Indirect(MemPlace::from_ptr(ptr.into()))
|
2019-05-28 10:44:46 +02:00
|
|
|
}
|
2022-07-18 18:47:31 -04:00
|
|
|
ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
|
2022-07-06 10:14:46 -04:00
|
|
|
ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
|
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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2022-07-03 10:21:47 -04:00
|
|
|
Ok(OpTy { op, layout, align: Some(layout.align.abi) })
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2019-02-08 12:20:55 +01:00
|
|
|
}
|
2022-07-31 06:57:53 +10:00
|
|
|
|
|
|
|
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
|
2022-09-20 15:41:42 +02:00
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2022-07-31 06:57:53 +10:00
|
|
|
mod size_asserts {
|
|
|
|
use super::*;
|
2022-08-10 09:47:59 +10:00
|
|
|
use rustc_data_structures::static_assert_size;
|
2022-10-05 21:46:21 +02:00
|
|
|
// tidy-alphabetical-start
|
2022-03-08 19:07:01 +00:00
|
|
|
static_assert_size!(Immediate, 48);
|
|
|
|
static_assert_size!(ImmTy<'_>, 64);
|
|
|
|
static_assert_size!(Operand, 56);
|
|
|
|
static_assert_size!(OpTy<'_>, 80);
|
2022-10-05 21:46:21 +02:00
|
|
|
// tidy-alphabetical-end
|
2022-07-31 06:57:53 +10:00
|
|
|
}
|