2018-08-22 16:52:01 -03:00
|
|
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
use rustc::mir;
|
2018-08-23 19:04:33 +02:00
|
|
|
use rustc::ty::layout::{self, Size, Align, LayoutOf, TyLayout, HasDataLayout, IntegerExt};
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
|
|
|
|
|
|
|
use rustc::mir::interpret::{
|
|
|
|
GlobalId, ConstValue, Scalar, EvalResult, Pointer, ScalarMaybeUndef, EvalErrorKind
|
|
|
|
};
|
|
|
|
use super::{EvalContext, Machine, MemPlace, MPlaceTy, PlaceExtra, MemoryKind};
|
|
|
|
|
|
|
|
/// A `Value` represents a single immediate self-contained Rust value.
|
|
|
|
///
|
|
|
|
/// 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
|
|
|
|
/// operations and fat pointers. This idea was taken from rustc's codegen.
|
|
|
|
/// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
|
|
|
|
/// defined on `Value`, and do not have to work with a `Place`.
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Value {
|
|
|
|
Scalar(ScalarMaybeUndef),
|
|
|
|
ScalarPair(ScalarMaybeUndef, ScalarMaybeUndef),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Value {
|
|
|
|
pub fn new_slice(
|
|
|
|
val: Scalar,
|
|
|
|
len: u64,
|
|
|
|
cx: impl HasDataLayout
|
|
|
|
) -> Self {
|
|
|
|
Value::ScalarPair(val.into(), Scalar::Bits {
|
|
|
|
bits: len as u128,
|
|
|
|
size: cx.data_layout().pointer_size.bytes() as u8,
|
|
|
|
}.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_dyn_trait(val: Scalar, vtable: Pointer) -> Self {
|
|
|
|
Value::ScalarPair(val.into(), Scalar::Ptr(vtable).into())
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn to_scalar_or_undef(self) -> ScalarMaybeUndef {
|
|
|
|
match self {
|
|
|
|
Value::Scalar(val) => val,
|
|
|
|
Value::ScalarPair(..) => bug!("Got a fat pointer where a scalar was expected"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline]
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn to_scalar(self) -> EvalResult<'tcx, Scalar> {
|
|
|
|
self.to_scalar_or_undef().not_undef()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the value into a pointer (or a pointer-sized integer).
|
2018-08-17 17:47:37 +02:00
|
|
|
/// Throws away the second half of a ScalarPair!
|
|
|
|
#[inline]
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn to_scalar_ptr(self) -> EvalResult<'tcx, Scalar> {
|
|
|
|
match self {
|
|
|
|
Value::Scalar(ptr) |
|
|
|
|
Value::ScalarPair(ptr, _) => ptr.not_undef(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_scalar_dyn_trait(self) -> EvalResult<'tcx, (Scalar, Pointer)> {
|
|
|
|
match self {
|
|
|
|
Value::ScalarPair(ptr, vtable) =>
|
|
|
|
Ok((ptr.not_undef()?, vtable.to_ptr()?)),
|
|
|
|
_ => bug!("expected ptr and vtable, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_scalar_slice(self, cx: impl HasDataLayout) -> EvalResult<'tcx, (Scalar, u64)> {
|
|
|
|
match self {
|
|
|
|
Value::ScalarPair(ptr, val) => {
|
|
|
|
let len = val.to_bits(cx.data_layout().pointer_size)?;
|
|
|
|
Ok((ptr.not_undef()?, len as u64))
|
|
|
|
}
|
|
|
|
_ => bug!("expected ptr and length, got {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScalarPair needs a type to interpret, so we often have a value and a type together
|
|
|
|
// as input for binary and cast operations.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct ValTy<'tcx> {
|
|
|
|
pub value: Value,
|
|
|
|
pub layout: TyLayout<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ::std::ops::Deref for ValTy<'tcx> {
|
|
|
|
type Target = Value;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-08-13 16:14:22 +02:00
|
|
|
fn deref(&self) -> &Value {
|
|
|
|
&self.value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
|
|
|
|
/// or still in memory. The latter is an optimization, to delay reading that chunk of
|
|
|
|
/// memory and to avoid having to store arbitrary-sized data here.
|
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Operand {
|
|
|
|
Immediate(Value),
|
|
|
|
Indirect(MemPlace),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Operand {
|
|
|
|
#[inline]
|
|
|
|
pub fn from_ptr(ptr: Pointer, align: Align) -> Self {
|
|
|
|
Operand::Indirect(MemPlace::from_ptr(ptr, align))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn from_scalar_value(val: Scalar) -> Self {
|
|
|
|
Operand::Immediate(Value::Scalar(val.into()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn to_mem_place(self) -> MemPlace {
|
|
|
|
match self {
|
|
|
|
Operand::Indirect(mplace) => mplace,
|
|
|
|
_ => bug!("to_mem_place: expected Operand::Indirect, got {:?}", self),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn to_immediate(self) -> Value {
|
|
|
|
match self {
|
|
|
|
Operand::Immediate(val) => val,
|
|
|
|
_ => bug!("to_immediate: expected Operand::Immediate, got {:?}", self),
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct OpTy<'tcx> {
|
|
|
|
pub op: Operand,
|
|
|
|
pub layout: TyLayout<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ::std::ops::Deref for OpTy<'tcx> {
|
|
|
|
type Target = Operand;
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-08-13 16:14:22 +02:00
|
|
|
fn deref(&self) -> &Operand {
|
|
|
|
&self.op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<MPlaceTy<'tcx>> for OpTy<'tcx> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-08-13 16:14:22 +02:00
|
|
|
fn from(mplace: MPlaceTy<'tcx>) -> Self {
|
|
|
|
OpTy {
|
|
|
|
op: Operand::Indirect(*mplace),
|
|
|
|
layout: mplace.layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> From<ValTy<'tcx>> for OpTy<'tcx> {
|
2018-08-17 17:47:37 +02:00
|
|
|
#[inline(always)]
|
2018-08-13 16:14:22 +02:00
|
|
|
fn from(val: ValTy<'tcx>) -> Self {
|
|
|
|
OpTy {
|
|
|
|
op: Operand::Immediate(val.value),
|
|
|
|
layout: val.layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> OpTy<'tcx> {
|
|
|
|
#[inline]
|
|
|
|
pub fn from_ptr(ptr: Pointer, align: Align, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
OpTy { op: Operand::from_ptr(ptr, align), layout }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn from_aligned_ptr(ptr: Pointer, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
OpTy { op: Operand::from_ptr(ptr, layout.align), layout }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn from_scalar_value(val: Scalar, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
OpTy { op: Operand::Immediate(Value::Scalar(val.into())), layout }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 15:21:04 +02:00
|
|
|
// Use the existing layout if given (but sanity check in debug mode),
|
|
|
|
// or compute the layout.
|
|
|
|
#[inline(always)]
|
|
|
|
fn from_known_layout<'tcx>(
|
|
|
|
layout: Option<TyLayout<'tcx>>,
|
|
|
|
compute: impl FnOnce() -> EvalResult<'tcx, TyLayout<'tcx>>
|
|
|
|
) -> EvalResult<'tcx, TyLayout<'tcx>> {
|
|
|
|
match layout {
|
|
|
|
None => compute(),
|
|
|
|
Some(layout) => {
|
2018-08-20 20:08:37 +02:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
let layout2 = compute()?;
|
|
|
|
assert_eq!(layout.details, layout2.details,
|
|
|
|
"Mismatch in layout of supposedly equal-layout types {:?} and {:?}",
|
|
|
|
layout.ty, layout2.ty);
|
|
|
|
}
|
2018-08-20 15:21:04 +02:00
|
|
|
Ok(layout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
|
|
|
/// Try reading a value in memory; this is interesting particularily for ScalarPair.
|
|
|
|
/// Return None if the layout does not permit loading this as a value.
|
2018-08-13 13:48:47 +02:00
|
|
|
pub(super) fn try_read_value_from_mplace(
|
2018-08-13 16:14:22 +02:00
|
|
|
&self,
|
2018-08-16 09:36:25 +02:00
|
|
|
mplace: MPlaceTy<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
) -> EvalResult<'tcx, Option<Value>> {
|
2018-08-16 09:36:25 +02:00
|
|
|
if mplace.extra != PlaceExtra::None {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
let (ptr, ptr_align) = mplace.to_scalar_ptr_align();
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2018-08-16 09:36:25 +02:00
|
|
|
if mplace.layout.size.bytes() == 0 {
|
2018-08-17 17:47:37 +02:00
|
|
|
// Not all ZSTs have a layout we would handle below, so just short-circuit them
|
|
|
|
// all here.
|
|
|
|
self.memory.check_align(ptr, ptr_align)?;
|
2018-08-17 12:31:50 +02:00
|
|
|
return Ok(Some(Value::Scalar(Scalar::zst().into())));
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let ptr = ptr.to_ptr()?;
|
2018-08-16 09:36:25 +02:00
|
|
|
match mplace.layout.abi {
|
2018-08-13 16:14:22 +02:00
|
|
|
layout::Abi::Scalar(..) => {
|
2018-08-16 09:36:25 +02:00
|
|
|
let scalar = self.memory.read_scalar(ptr, ptr_align, mplace.layout.size)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(Some(Value::Scalar(scalar)))
|
|
|
|
}
|
|
|
|
layout::Abi::ScalarPair(ref a, ref b) => {
|
|
|
|
let (a, b) = (&a.value, &b.value);
|
|
|
|
let (a_size, b_size) = (a.size(self), b.size(self));
|
|
|
|
let a_ptr = ptr;
|
|
|
|
let b_offset = a_size.abi_align(b.align(self));
|
|
|
|
assert!(b_offset.bytes() > 0); // we later use the offset to test which field to use
|
|
|
|
let b_ptr = ptr.offset(b_offset, self)?.into();
|
|
|
|
let a_val = self.memory.read_scalar(a_ptr, ptr_align, a_size)?;
|
|
|
|
let b_val = self.memory.read_scalar(b_ptr, ptr_align, b_size)?;
|
|
|
|
Ok(Some(Value::ScalarPair(a_val, b_val)))
|
|
|
|
}
|
|
|
|
_ => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try returning an immediate value for the operand.
|
|
|
|
/// If the layout does not permit loading this as a value, return where in memory
|
|
|
|
/// 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
|
|
|
|
/// in a `Value`, not on which data is stored there currently.
|
|
|
|
pub(super) fn try_read_value(
|
|
|
|
&self,
|
2018-08-17 12:31:50 +02:00
|
|
|
src: OpTy<'tcx>,
|
2018-08-13 16:14:22 +02:00
|
|
|
) -> EvalResult<'tcx, Result<Value, MemPlace>> {
|
2018-08-16 09:36:25 +02:00
|
|
|
Ok(match src.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
|
|
|
if let Some(val) = self.try_read_value_from_mplace(mplace)? {
|
|
|
|
Ok(val)
|
|
|
|
} else {
|
|
|
|
Err(*mplace)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
},
|
2018-08-16 09:36:25 +02:00
|
|
|
Err(val) => Ok(val),
|
|
|
|
})
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a value from a place, asserting that that is possible with the given layout.
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn read_value(&self, op: OpTy<'tcx>) -> EvalResult<'tcx, ValTy<'tcx>> {
|
|
|
|
if let Ok(value) = self.try_read_value(op)? {
|
|
|
|
Ok(ValTy { value, layout: op.layout })
|
|
|
|
} else {
|
|
|
|
bug!("primitive read failed for type: {:?}", op.layout.ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read a scalar from a place
|
2018-08-17 12:31:50 +02:00
|
|
|
pub fn read_scalar(&self, op: OpTy<'tcx>) -> EvalResult<'tcx, ScalarMaybeUndef> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match *self.read_value(op)? {
|
|
|
|
Value::ScalarPair(..) => bug!("got ScalarPair for type: {:?}", op.layout.ty),
|
|
|
|
Value::Scalar(val) => Ok(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
// operand must be a &str or compatible layout
|
|
|
|
pub fn read_str(
|
|
|
|
&self,
|
|
|
|
op: OpTy<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, &str> {
|
|
|
|
let val = self.read_value(op)?;
|
|
|
|
if let Value::ScalarPair(ptr, len) = *val {
|
|
|
|
let len = len.not_undef()?.to_bits(self.memory.pointer_size())?;
|
|
|
|
let bytes = self.memory.read_bytes(ptr.not_undef()?, Size::from_bytes(len as u64))?;
|
|
|
|
let str = ::std::str::from_utf8(bytes).map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
|
|
|
|
Ok(str)
|
|
|
|
} else {
|
|
|
|
bug!("read_str: not a str")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn uninit_operand(&mut self, layout: TyLayout<'tcx>) -> EvalResult<'tcx, Operand> {
|
|
|
|
// This decides which types we will use the Immediate optimization for, and hence should
|
|
|
|
// match what `try_read_value` and `eval_place_to_op` support.
|
2018-08-16 09:36:53 +02:00
|
|
|
if layout.is_zst() {
|
2018-08-17 17:47:37 +02:00
|
|
|
return Ok(Operand::Immediate(Value::Scalar(Scalar::zst().into())));
|
2018-08-16 09:36:53 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(match layout.abi {
|
|
|
|
layout::Abi::Scalar(..) =>
|
|
|
|
Operand::Immediate(Value::Scalar(ScalarMaybeUndef::Undef)),
|
|
|
|
layout::Abi::ScalarPair(..) =>
|
|
|
|
Operand::Immediate(Value::ScalarPair(
|
|
|
|
ScalarMaybeUndef::Undef,
|
|
|
|
ScalarMaybeUndef::Undef,
|
|
|
|
)),
|
|
|
|
_ => {
|
|
|
|
trace!("Forcing allocation for local of type {:?}", layout.ty);
|
|
|
|
Operand::Indirect(
|
|
|
|
*self.allocate(layout, MemoryKind::Stack)?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Projection functions
|
|
|
|
pub fn operand_field(
|
|
|
|
&self,
|
|
|
|
op: OpTy<'tcx>,
|
|
|
|
field: u64,
|
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx>> {
|
|
|
|
let base = match op.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
|
|
|
// The easy case
|
|
|
|
let field = self.mplace_field(mplace, field)?;
|
|
|
|
return Ok(field.into());
|
|
|
|
},
|
|
|
|
Err(value) => value
|
|
|
|
};
|
|
|
|
|
|
|
|
let field = field.try_into().unwrap();
|
|
|
|
let field_layout = op.layout.field(self, field)?;
|
|
|
|
if field_layout.size.bytes() == 0 {
|
|
|
|
let val = Value::Scalar(Scalar::zst().into());
|
|
|
|
return Ok(OpTy { op: Operand::Immediate(val), layout: field_layout });
|
|
|
|
}
|
|
|
|
let offset = op.layout.fields.offset(field);
|
|
|
|
let value = match base {
|
|
|
|
// the field covers the entire type
|
|
|
|
_ if offset.bytes() == 0 && field_layout.size == op.layout.size => base,
|
|
|
|
// extract fields from types with `ScalarPair` ABI
|
|
|
|
Value::ScalarPair(a, b) => {
|
|
|
|
let val = if offset.bytes() == 0 { a } else { b };
|
|
|
|
Value::Scalar(val)
|
|
|
|
},
|
|
|
|
Value::Scalar(val) =>
|
|
|
|
bug!("field access on non aggregate {:#?}, {:#?}", val, op.layout),
|
|
|
|
};
|
|
|
|
Ok(OpTy { op: Operand::Immediate(value), layout: field_layout })
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn operand_downcast(
|
|
|
|
&self,
|
|
|
|
op: OpTy<'tcx>,
|
|
|
|
variant: usize,
|
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx>> {
|
|
|
|
// Downcasts only change the layout
|
|
|
|
Ok(match op.try_as_mplace() {
|
|
|
|
Ok(mplace) => {
|
|
|
|
self.mplace_downcast(mplace, variant)?.into()
|
|
|
|
},
|
|
|
|
Err(..) => {
|
|
|
|
let layout = op.layout.for_variant(self, variant);
|
|
|
|
OpTy { layout, ..op }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take an operand, representing a pointer, and dereference it -- that
|
|
|
|
// will always be a MemPlace.
|
|
|
|
pub(super) fn deref_operand(
|
|
|
|
&self,
|
|
|
|
src: OpTy<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, MPlaceTy<'tcx>> {
|
|
|
|
let val = self.read_value(src)?;
|
|
|
|
trace!("deref to {} on {:?}", val.layout.ty, val);
|
|
|
|
Ok(self.ref_to_mplace(val)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn operand_projection(
|
|
|
|
&self,
|
|
|
|
base: OpTy<'tcx>,
|
|
|
|
proj_elem: &mir::PlaceElem<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx>> {
|
|
|
|
use rustc::mir::ProjectionElem::*;
|
|
|
|
Ok(match *proj_elem {
|
|
|
|
Field(field, _) => self.operand_field(base, field.index() as u64)?,
|
|
|
|
Downcast(_, variant) => self.operand_downcast(base, variant)?,
|
|
|
|
Deref => self.deref_operand(base)?.into(),
|
|
|
|
// The rest should only occur as mplace, we do not use Immediates for types
|
|
|
|
// allowing such operations. This matches place_projection forcing an allocation.
|
|
|
|
Subslice { .. } | ConstantIndex { .. } | Index(_) => {
|
|
|
|
let mplace = base.to_mem_place();
|
|
|
|
self.mplace_projection(mplace, proj_elem)?.into()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate a place with the goal of reading from it. This lets us sometimes
|
2018-08-20 15:21:04 +02:00
|
|
|
// avoid allocations. If you already know the layout, you can pass it in
|
|
|
|
// to avoid looking it up again.
|
2018-08-13 16:14:22 +02:00
|
|
|
fn eval_place_to_op(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
mir_place: &mir::Place<'tcx>,
|
2018-08-20 15:21:04 +02:00
|
|
|
layout: Option<TyLayout<'tcx>>,
|
2018-08-13 16:14:22 +02:00
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx>> {
|
|
|
|
use rustc::mir::Place::*;
|
2018-08-23 19:27:14 +02:00
|
|
|
let op = match *mir_place {
|
2018-08-13 16:14:22 +02:00
|
|
|
Local(mir::RETURN_PLACE) => return err!(ReadFromReturnPointer),
|
|
|
|
Local(local) => {
|
|
|
|
let op = *self.frame().locals[local].access()?;
|
2018-08-20 15:21:04 +02:00
|
|
|
let layout = from_known_layout(layout,
|
|
|
|
|| self.layout_of_local(self.cur_frame(), local))?;
|
|
|
|
OpTy { op, layout }
|
2018-08-13 16:14:22 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
Projection(ref proj) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
let op = self.eval_place_to_op(&proj.base, None)?;
|
2018-08-13 16:14:22 +02:00
|
|
|
self.operand_projection(op, &proj.elem)?
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:27:14 +02:00
|
|
|
_ => self.eval_place_to_mplace(mir_place)?.into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!("eval_place_to_op: got {:?}", *op);
|
|
|
|
Ok(op)
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Evaluate the operand, returning a place where you can then find the data.
|
2018-08-20 15:21:04 +02:00
|
|
|
/// if you already know the layout, you can save two some table lookups
|
|
|
|
/// by passing it in here.
|
|
|
|
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>,
|
|
|
|
layout: Option<TyLayout<'tcx>>,
|
|
|
|
) -> EvalResult<'tcx, OpTy<'tcx>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::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
|
|
|
|
Copy(ref place) |
|
|
|
|
Move(ref place) =>
|
2018-08-20 15:21:04 +02:00
|
|
|
self.eval_place_to_op(place, layout)?,
|
2018-08-13 16:14:22 +02:00
|
|
|
|
|
|
|
Constant(ref constant) => {
|
2018-08-20 15:21:04 +02:00
|
|
|
let layout = from_known_layout(layout, || {
|
|
|
|
let ty = self.monomorphize(mir_op.ty(self.mir(), *self.tcx), self.substs());
|
|
|
|
self.layout_of(ty)
|
|
|
|
})?;
|
2018-08-13 16:14:22 +02:00
|
|
|
let op = self.const_value_to_op(constant.literal.val)?;
|
2018-08-15 20:18:40 +02:00
|
|
|
OpTy { op, layout }
|
2018-08-13 16:14:22 +02: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
|
|
|
|
pub(crate) fn eval_operands(
|
2018-08-23 19:27:14 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
ops: &[mir::Operand<'tcx>],
|
|
|
|
) -> EvalResult<'tcx, Vec<OpTy<'tcx>>> {
|
|
|
|
ops.into_iter()
|
2018-08-20 15:21:04 +02:00
|
|
|
.map(|op| self.eval_operand(op, None))
|
2018-08-13 16:14:22 +02:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also used e.g. when miri runs into a constant.
|
|
|
|
pub fn const_value_to_op(
|
2018-08-23 19:04:33 +02:00
|
|
|
&self,
|
2018-08-13 16:14:22 +02:00
|
|
|
val: ConstValue<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, Operand> {
|
2018-08-23 19:04:33 +02:00
|
|
|
trace!("const_value_to_op: {:?}", val);
|
2018-08-13 16:14:22 +02:00
|
|
|
match val {
|
|
|
|
ConstValue::Unevaluated(def_id, substs) => {
|
|
|
|
let instance = self.resolve(def_id, substs)?;
|
|
|
|
self.global_to_op(GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
})
|
|
|
|
}
|
2018-08-23 19:04:33 +02:00
|
|
|
ConstValue::ByRef(id, alloc, offset) => {
|
|
|
|
// We rely on mutability being set correctly in that allocation to prevent writes
|
|
|
|
// where none should happen -- and for `static mut`, we copy on demand anyway.
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(Operand::from_ptr(Pointer::new(id, offset), alloc.align))
|
|
|
|
},
|
|
|
|
ConstValue::ScalarPair(a, b) =>
|
|
|
|
Ok(Operand::Immediate(Value::ScalarPair(a.into(), b))),
|
|
|
|
ConstValue::Scalar(x) =>
|
|
|
|
Ok(Operand::Immediate(Value::Scalar(x.into()))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
pub(super) fn global_to_op(&self, gid: GlobalId<'tcx>) -> EvalResult<'tcx, Operand> {
|
2018-08-13 16:14:22 +02:00
|
|
|
let cv = self.const_eval(gid)?;
|
|
|
|
self.const_value_to_op(cv.val)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// reads a tag and produces the corresponding variant index
|
|
|
|
pub fn read_discriminant_as_variant_index(
|
|
|
|
&self,
|
|
|
|
rval: OpTy<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, usize> {
|
|
|
|
match rval.layout.variants {
|
|
|
|
layout::Variants::Single { index } => Ok(index),
|
|
|
|
layout::Variants::Tagged { .. } => {
|
|
|
|
let discr_val = self.read_discriminant_value(rval)?;
|
|
|
|
rval.layout.ty
|
|
|
|
.ty_adt_def()
|
|
|
|
.expect("tagged layout for non adt")
|
|
|
|
.discriminants(self.tcx.tcx)
|
|
|
|
.position(|var| var.val == discr_val)
|
|
|
|
.ok_or_else(|| EvalErrorKind::InvalidDiscriminant.into())
|
|
|
|
}
|
|
|
|
layout::Variants::NicheFilling { .. } => {
|
|
|
|
let discr_val = self.read_discriminant_value(rval)?;
|
|
|
|
assert_eq!(discr_val as usize as u128, discr_val);
|
|
|
|
Ok(discr_val as usize)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_discriminant_value(
|
|
|
|
&self,
|
|
|
|
rval: OpTy<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, u128> {
|
|
|
|
trace!("read_discriminant_value {:#?}", rval.layout);
|
|
|
|
if rval.layout.abi == layout::Abi::Uninhabited {
|
|
|
|
return err!(Unreachable);
|
|
|
|
}
|
|
|
|
|
|
|
|
match rval.layout.variants {
|
|
|
|
layout::Variants::Single { index } => {
|
|
|
|
let discr_val = rval.layout.ty.ty_adt_def().map_or(
|
|
|
|
index as u128,
|
|
|
|
|def| def.discriminant_for_variant(*self.tcx, index).val);
|
|
|
|
return Ok(discr_val);
|
|
|
|
}
|
|
|
|
layout::Variants::Tagged { .. } |
|
|
|
|
layout::Variants::NicheFilling { .. } => {},
|
|
|
|
}
|
|
|
|
let discr_op = self.operand_field(rval, 0)?;
|
|
|
|
let discr_val = self.read_value(discr_op)?;
|
|
|
|
trace!("discr value: {:?}", discr_val);
|
|
|
|
let raw_discr = discr_val.to_scalar()?;
|
|
|
|
Ok(match rval.layout.variants {
|
|
|
|
layout::Variants::Single { .. } => bug!(),
|
|
|
|
// FIXME: We should catch invalid discriminants here!
|
|
|
|
layout::Variants::Tagged { .. } => {
|
|
|
|
if discr_val.layout.ty.is_signed() {
|
|
|
|
let i = raw_discr.to_bits(discr_val.layout.size)? as i128;
|
|
|
|
// going from layout tag type to typeck discriminant type
|
|
|
|
// requires first sign extending with the layout discriminant
|
|
|
|
let shift = 128 - discr_val.layout.size.bits();
|
|
|
|
let sexted = (i << shift) >> shift;
|
|
|
|
// and then zeroing with the typeck discriminant type
|
|
|
|
let discr_ty = rval.layout.ty
|
|
|
|
.ty_adt_def().expect("tagged layout corresponds to adt")
|
|
|
|
.repr
|
|
|
|
.discr_type();
|
|
|
|
let discr_ty = layout::Integer::from_attr(self.tcx.tcx, discr_ty);
|
|
|
|
let shift = 128 - discr_ty.size().bits();
|
|
|
|
let truncatee = sexted as u128;
|
|
|
|
(truncatee << shift) >> shift
|
|
|
|
} else {
|
|
|
|
raw_discr.to_bits(discr_val.layout.size)?
|
|
|
|
}
|
|
|
|
},
|
|
|
|
layout::Variants::NicheFilling {
|
|
|
|
dataful_variant,
|
|
|
|
ref niche_variants,
|
|
|
|
niche_start,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let variants_start = *niche_variants.start() as u128;
|
|
|
|
let variants_end = *niche_variants.end() as u128;
|
|
|
|
match raw_discr {
|
|
|
|
Scalar::Ptr(_) => {
|
|
|
|
assert!(niche_start == 0);
|
|
|
|
assert!(variants_start == variants_end);
|
|
|
|
dataful_variant as u128
|
|
|
|
},
|
|
|
|
Scalar::Bits { bits: raw_discr, size } => {
|
|
|
|
assert_eq!(size as u64, discr_val.layout.size.bytes());
|
|
|
|
let discr = raw_discr.wrapping_sub(niche_start)
|
|
|
|
.wrapping_add(variants_start);
|
|
|
|
if variants_start <= discr && discr <= variants_end {
|
|
|
|
discr
|
|
|
|
} else {
|
|
|
|
dataful_variant as u128
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|