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.
|
|
|
|
|
2017-02-07 00:39:40 -08:00
|
|
|
use std::fmt::Write;
|
2018-06-22 13:31:25 -07:00
|
|
|
use std::mem;
|
2016-12-07 20:30:37 -08:00
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
use syntax::source_map::{self, Span, DUMMY_SP};
|
2017-12-12 17:14:49 +01:00
|
|
|
use rustc::hir::def_id::DefId;
|
2018-03-22 16:59:02 +01:00
|
|
|
use rustc::hir::def::Def;
|
2017-12-12 17:14:49 +01:00
|
|
|
use rustc::hir::map::definitions::DefPathData;
|
|
|
|
use rustc::mir;
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc::ty::layout::{
|
2018-08-17 12:18:02 +02:00
|
|
|
self, Size, Align, HasDataLayout, LayoutOf, TyLayout
|
2018-08-13 16:14:22 +02:00
|
|
|
};
|
2018-02-23 01:13:54 +00:00
|
|
|
use rustc::ty::subst::{Subst, Substs};
|
2018-08-16 00:18:09 +02:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2018-06-13 16:44:43 +03:00
|
|
|
use rustc::ty::query::TyCtxtAt;
|
2018-08-13 16:14:22 +02:00
|
|
|
use rustc_data_structures::indexed_vec::IndexVec;
|
2017-12-12 17:14:49 +01:00
|
|
|
use rustc::mir::interpret::{
|
2018-08-11 19:08:14 +02:00
|
|
|
GlobalId, Scalar, FrameInfo, AllocId,
|
2018-08-13 16:14:22 +02:00
|
|
|
EvalResult, EvalErrorKind,
|
2018-08-16 11:38:16 +02:00
|
|
|
truncate, sign_extend,
|
2017-12-12 17:14:49 +01:00
|
|
|
};
|
2018-10-12 16:10:16 +02:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2018-06-08 03:47:26 +01:00
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
use super::{
|
2018-10-09 21:05:53 +02:00
|
|
|
Value, Operand, MemPlace, MPlaceTy, Place, PlaceTy, ScalarMaybeUndef,
|
2018-08-13 16:14:22 +02:00
|
|
|
Memory, Machine
|
|
|
|
};
|
2016-06-01 17:05:20 +02:00
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
|
2017-12-06 14:23:32 +02:00
|
|
|
/// Stores the `Machine` instance.
|
|
|
|
pub machine: M,
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2016-03-14 21:18:39 -06:00
|
|
|
/// The results of the type checker, from rustc.
|
2018-09-20 12:06:39 +02:00
|
|
|
pub tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
2016-03-14 21:18:39 -06:00
|
|
|
|
2017-12-06 14:12:05 +02:00
|
|
|
/// Bounds in scope for polymorphic evaluations.
|
2018-09-20 10:22:11 +02:00
|
|
|
pub(crate) param_env: ty::ParamEnv<'tcx>,
|
2017-12-06 14:12:05 +02:00
|
|
|
|
2018-06-22 12:36:54 -07:00
|
|
|
/// The virtual memory system.
|
|
|
|
pub memory: Memory<'a, 'mir, 'tcx, M>,
|
|
|
|
|
|
|
|
/// The virtual call stack.
|
2018-09-21 23:32:59 +02:00
|
|
|
pub(crate) stack: Vec<Frame<'mir, 'tcx, M::PointerTag>>,
|
2018-10-12 16:10:16 +02:00
|
|
|
|
|
|
|
/// A cache for deduplicating vtables
|
2018-10-12 16:45:17 +02:00
|
|
|
pub(super) vtables: FxHashMap<(Ty<'tcx>, ty::PolyExistentialTraitRef<'tcx>), AllocId>,
|
2016-05-09 18:21:21 -06:00
|
|
|
}
|
|
|
|
|
2016-03-06 04:23:24 -06:00
|
|
|
/// A stack frame.
|
2018-06-21 21:40:14 -07:00
|
|
|
#[derive(Clone)]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub struct Frame<'mir, 'tcx: 'mir, Tag=()> {
|
2016-06-14 20:13:59 -06:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Function and callsite information
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// The MIR for the function called on this frame.
|
2018-01-16 09:31:48 +01:00
|
|
|
pub mir: &'mir mir::Mir<'tcx>,
|
2016-06-14 20:13:59 -06:00
|
|
|
|
2017-03-21 13:53:55 +01:00
|
|
|
/// The def_id and substs of the current function
|
|
|
|
pub instance: ty::Instance<'tcx>,
|
2016-06-02 18:21:32 +02:00
|
|
|
|
2016-06-14 20:13:59 -06:00
|
|
|
/// The span of the call site.
|
2018-08-18 12:14:03 +02:00
|
|
|
pub span: source_map::Span,
|
2016-03-07 07:10:52 -06:00
|
|
|
|
2016-06-14 20:13:59 -06:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2017-12-06 09:25:29 +01:00
|
|
|
// Return place and locals
|
2016-06-14 20:13:59 -06:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2018-08-23 19:04:33 +02:00
|
|
|
/// Work to perform when returning from this function
|
2016-09-09 17:44:04 +02:00
|
|
|
pub return_to_block: StackPopCleanup,
|
2016-07-06 17:55:05 +02:00
|
|
|
|
2018-10-09 21:05:53 +02:00
|
|
|
/// The location where the result of the current stack frame should be written to,
|
|
|
|
/// and its layout in the caller.
|
|
|
|
pub return_place: Option<PlaceTy<'tcx, Tag>>,
|
2016-10-15 19:48:30 -06:00
|
|
|
|
|
|
|
/// The list of locals for this stack frame, stored in order as
|
2018-08-22 16:58:39 -03:00
|
|
|
/// `[return_ptr, arguments..., variables..., temporaries...]`.
|
|
|
|
/// The locals are stored as `Option<Value>`s.
|
2017-05-31 17:41:33 -07:00
|
|
|
/// `None` represents a local that is currently dead, while a live local
|
2018-05-20 23:43:16 +02:00
|
|
|
/// can either directly contain `Scalar` or refer to some part of an `Allocation`.
|
2018-09-21 23:32:59 +02:00
|
|
|
pub locals: IndexVec<mir::Local, LocalValue<Tag>>,
|
2016-02-27 19:20:25 -06:00
|
|
|
|
2016-06-14 20:13:59 -06:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Current position within the function
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/// The block that is currently executed (or will be executed after the above call stacks
|
|
|
|
/// return).
|
|
|
|
pub block: mir::BasicBlock,
|
|
|
|
|
2018-02-16 15:56:50 +01:00
|
|
|
/// The index of the currently evaluated statement.
|
2016-06-10 13:01:51 +02:00
|
|
|
pub stmt: usize,
|
2016-03-06 04:23:24 -06:00
|
|
|
}
|
2016-02-27 19:20:25 -06:00
|
|
|
|
2018-08-23 19:04:33 +02:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
|
|
|
pub enum StackPopCleanup {
|
|
|
|
/// Jump to the next block in the caller, or cause UB if None (that's a function
|
2018-10-09 21:05:53 +02:00
|
|
|
/// that may never return). Also store layout of return place so
|
|
|
|
/// we can validate it at that layout.
|
2018-08-23 19:04:33 +02:00
|
|
|
Goto(Option<mir::BasicBlock>),
|
2018-08-24 17:36:18 +02:00
|
|
|
/// Just do nohing: Used by Main and for the box_alloc hook in miri.
|
|
|
|
/// `cleanup` says whether locals are deallocated. Static computation
|
|
|
|
/// wants them leaked to intern what they need (and just throw away
|
|
|
|
/// the entire `ecx` when it is done).
|
|
|
|
None { cleanup: bool },
|
2018-08-23 19:04:33 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
// State of a local variable
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub enum LocalValue<Tag=(), Id=AllocId> {
|
2018-08-13 16:14:22 +02:00
|
|
|
Dead,
|
|
|
|
// Mostly for convenience, we re-use the `Operand` type here.
|
|
|
|
// This is an optimization over just always having a pointer here;
|
|
|
|
// we can thus avoid doing an allocation when the local just stores
|
|
|
|
// immediate values *and* never has its address taken.
|
2018-09-21 23:32:59 +02:00
|
|
|
Live(Operand<Tag, Id>),
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
impl<'tcx, Tag> LocalValue<Tag> {
|
|
|
|
pub fn access(&self) -> EvalResult<'tcx, &Operand<Tag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
|
|
|
LocalValue::Dead => err!(DeadLocal),
|
|
|
|
LocalValue::Live(ref val) => Ok(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn access_mut(&mut self) -> EvalResult<'tcx, &mut Operand<Tag>> {
|
2018-08-13 16:14:22 +02:00
|
|
|
match self {
|
|
|
|
LocalValue::Dead => err!(DeadLocal),
|
|
|
|
LocalValue::Live(ref mut val) => Ok(val),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
|
|
|
|
for &'b EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &layout::TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> HasDataLayout
|
2018-08-26 20:42:52 +02:00
|
|
|
for &'c &'b mut EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
|
|
|
fn data_layout(&self) -> &layout::TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'b, 'a, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for &'b EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
where M: Machine<'a, 'mir, 'tcx>
|
2018-08-22 16:59:14 -03:00
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
2018-09-20 10:12:21 +02:00
|
|
|
fn tcx<'d>(&'d self) -> TyCtxt<'d, 'tcx, 'tcx> {
|
2018-02-06 18:33:59 +01:00
|
|
|
*self.tcx
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> layout::HasTyCtxt<'tcx>
|
|
|
|
for &'c &'b mut EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
#[inline]
|
|
|
|
fn tcx<'d>(&'d self) -> TyCtxt<'d, 'tcx, 'tcx> {
|
2018-02-06 18:33:59 +01:00
|
|
|
*self.tcx
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> LayoutOf
|
|
|
|
for &'b EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
{
|
2018-02-05 21:07:20 +02:00
|
|
|
type Ty = Ty<'tcx>;
|
2017-12-06 09:25:29 +01:00
|
|
|
type TyLayout = EvalResult<'tcx, TyLayout<'tcx>>;
|
|
|
|
|
2018-08-19 17:01:31 +02:00
|
|
|
#[inline]
|
2017-12-06 09:25:29 +01:00
|
|
|
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
|
2018-02-01 00:00:38 +02:00
|
|
|
self.tcx.layout_of(self.param_env.and(ty))
|
2017-12-06 09:25:29 +01:00
|
|
|
.map_err(|layout| EvalErrorKind::Layout(layout).into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> LayoutOf
|
|
|
|
for &'c &'b mut EvalContext<'a, 'mir, 'tcx, M>
|
|
|
|
{
|
2018-02-05 21:07:20 +02:00
|
|
|
type Ty = Ty<'tcx>;
|
2017-12-06 09:25:29 +01:00
|
|
|
type TyLayout = EvalResult<'tcx, TyLayout<'tcx>>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn layout_of(self, ty: Ty<'tcx>) -> Self::TyLayout {
|
|
|
|
(&**self).layout_of(ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
|
2017-07-21 17:25:30 +02:00
|
|
|
pub fn new(
|
2018-02-06 18:33:59 +01:00
|
|
|
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
2017-12-06 14:12:05 +02:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-12-06 14:23:32 +02:00
|
|
|
machine: M,
|
2018-07-04 13:05:43 -07:00
|
|
|
) -> Self {
|
2016-06-10 16:20:17 +02:00
|
|
|
EvalContext {
|
2017-12-06 14:23:32 +02:00
|
|
|
machine,
|
2017-01-16 18:45:30 -08:00
|
|
|
tcx,
|
2017-12-06 14:12:05 +02:00
|
|
|
param_env,
|
2018-10-16 09:15:13 +02:00
|
|
|
memory: Memory::new(tcx),
|
2018-06-22 12:36:54 -07:00
|
|
|
stack: Vec::new(),
|
2018-10-16 10:39:48 +02:00
|
|
|
vtables: FxHashMap::default(),
|
2016-03-06 04:23:24 -06:00
|
|
|
}
|
|
|
|
}
|
2016-06-01 14:33:37 +02:00
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2018-01-16 09:31:48 +01:00
|
|
|
pub fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M> {
|
2018-06-22 12:36:54 -07:00
|
|
|
&self.memory
|
2016-06-10 13:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2018-01-16 09:31:48 +01:00
|
|
|
pub fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> {
|
2018-06-22 12:36:54 -07:00
|
|
|
&mut self.memory
|
2016-06-15 12:55:04 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn stack(&self) -> &[Frame<'mir, 'tcx, M::PointerTag>] {
|
2018-06-22 12:36:54 -07:00
|
|
|
&self.stack
|
2016-06-10 13:01:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
2017-06-16 17:58:18 -07:00
|
|
|
pub fn cur_frame(&self) -> usize {
|
2018-06-22 12:36:54 -07:00
|
|
|
assert!(self.stack.len() > 0);
|
|
|
|
self.stack.len() - 1
|
2017-06-16 17:58:18 -07:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn frame(&self) -> &Frame<'mir, 'tcx, M::PointerTag> {
|
|
|
|
self.stack.last().expect("no call frames exist")
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn frame_mut(&mut self) -> &mut Frame<'mir, 'tcx, M::PointerTag> {
|
|
|
|
self.stack.last_mut().expect("no call frames exist")
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
#[inline(always)]
|
|
|
|
pub(super) fn mir(&self) -> &'mir mir::Mir<'tcx> {
|
|
|
|
self.frame().mir
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
pub fn substs(&self) -> &'tcx Substs<'tcx> {
|
|
|
|
if let Some(frame) = self.stack.last() {
|
|
|
|
frame.instance.substs
|
|
|
|
} else {
|
|
|
|
Substs::empty()
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 08:45:59 -07:00
|
|
|
pub(super) fn resolve(
|
|
|
|
&self,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: &'tcx Substs<'tcx>
|
2018-08-23 09:34:21 -07:00
|
|
|
) -> EvalResult<'tcx, ty::Instance<'tcx>> {
|
2018-02-20 11:49:50 +01:00
|
|
|
trace!("resolve: {:?}, {:#?}", def_id, substs);
|
|
|
|
trace!("substs: {:#?}", self.substs());
|
|
|
|
trace!("param_env: {:#?}", self.param_env);
|
2018-03-09 12:53:17 -05:00
|
|
|
let substs = self.tcx.subst_and_normalize_erasing_regions(
|
|
|
|
self.substs(),
|
|
|
|
self.param_env,
|
|
|
|
&substs,
|
|
|
|
);
|
2017-12-06 09:25:29 +01:00
|
|
|
ty::Instance::resolve(
|
2018-02-06 18:33:59 +01:00
|
|
|
*self.tcx,
|
2017-12-06 14:12:05 +02:00
|
|
|
self.param_env,
|
2017-12-06 09:25:29 +01:00
|
|
|
def_id,
|
|
|
|
substs,
|
2018-06-25 15:08:05 +02:00
|
|
|
).ok_or_else(|| EvalErrorKind::TooGeneric.into())
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
|
2018-02-06 18:33:59 +01:00
|
|
|
ty.is_sized(self.tcx, self.param_env)
|
2016-06-08 09:38:59 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
|
|
|
|
ty.is_freeze(*self.tcx, self.param_env, DUMMY_SP)
|
|
|
|
}
|
|
|
|
|
2017-08-10 08:48:38 -07:00
|
|
|
pub fn load_mir(
|
|
|
|
&self,
|
|
|
|
instance: ty::InstanceDef<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, &'tcx mir::Mir<'tcx>> {
|
2017-12-06 09:25:29 +01:00
|
|
|
// do not continue if typeck errors occurred (can only occur in local crate)
|
|
|
|
let did = instance.def_id();
|
2018-08-22 16:59:14 -03:00
|
|
|
if did.is_local()
|
|
|
|
&& self.tcx.has_typeck_tables(did)
|
2018-08-23 08:45:59 -07:00
|
|
|
&& self.tcx.typeck_tables_of(did).tainted_by_errors
|
|
|
|
{
|
2017-12-06 09:25:29 +01:00
|
|
|
return err!(TypeckError);
|
|
|
|
}
|
2017-03-21 13:53:55 +01:00
|
|
|
trace!("load mir {:?}", instance);
|
|
|
|
match instance {
|
2017-08-10 08:48:38 -07:00
|
|
|
ty::InstanceDef::Item(def_id) => {
|
2018-04-19 08:56:27 +10:00
|
|
|
self.tcx.maybe_optimized_mir(def_id).ok_or_else(||
|
2017-08-10 08:48:38 -07:00
|
|
|
EvalErrorKind::NoMirFor(self.tcx.item_path_str(def_id)).into()
|
2018-04-19 08:56:27 +10:00
|
|
|
)
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-03-21 13:53:55 +01:00
|
|
|
_ => Ok(self.tcx.instance_mir(instance)),
|
2016-06-06 15:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-08 11:11:08 +02:00
|
|
|
|
2018-08-16 00:18:09 +02:00
|
|
|
pub fn monomorphize<T: TypeFoldable<'tcx> + Subst<'tcx>>(
|
|
|
|
&self,
|
|
|
|
t: T,
|
|
|
|
substs: &'tcx Substs<'tcx>
|
|
|
|
) -> T {
|
2017-01-28 15:28:24 +01:00
|
|
|
// miri doesn't care about lifetimes, and will choke on some crazy ones
|
|
|
|
// let's simply get rid of them
|
2018-08-16 00:18:09 +02:00
|
|
|
let substituted = t.subst(*self.tcx, substs);
|
2018-03-03 08:23:28 -05:00
|
|
|
self.tcx.normalize_erasing_regions(ty::ParamEnv::reveal_all(), substituted)
|
2017-03-14 11:12:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 16:14:22 +02:00
|
|
|
pub fn layout_of_local(
|
|
|
|
&self,
|
2018-10-20 11:02:39 +02:00
|
|
|
frame: &Frame<'mir, 'tcx, M::PointerTag>,
|
2018-08-13 16:14:22 +02:00
|
|
|
local: mir::Local
|
|
|
|
) -> EvalResult<'tcx, TyLayout<'tcx>> {
|
2018-10-20 11:02:39 +02:00
|
|
|
let local_ty = frame.mir.local_decls[local].ty;
|
|
|
|
let local_ty = self.monomorphize(local_ty, frame.instance.substs);
|
2018-08-13 16:14:22 +02:00
|
|
|
self.layout_of(local_ty)
|
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
pub fn str_to_value(&mut self, s: &str) -> EvalResult<'tcx, Value<M::PointerTag>> {
|
|
|
|
let ptr = self.memory.allocate_static_bytes(s.as_bytes());
|
|
|
|
Ok(Value::new_slice(Scalar::Ptr(ptr), s.len() as u64, self.tcx.tcx))
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:05:01 +02:00
|
|
|
/// Return the actual dynamic size and alignment of the place at the given type.
|
2018-10-09 22:41:41 +02:00
|
|
|
/// Only the "meta" (metadata) part of the place matters.
|
|
|
|
/// This can fail to provide an answer for extern types.
|
2018-08-25 14:36:24 +02:00
|
|
|
pub(super) fn size_and_align_of(
|
2018-08-03 11:21:44 +02:00
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
metadata: Option<Scalar<M::PointerTag>>,
|
2018-08-25 14:36:24 +02:00
|
|
|
layout: TyLayout<'tcx>,
|
2018-10-09 22:41:41 +02:00
|
|
|
) -> EvalResult<'tcx, Option<(Size, Align)>> {
|
|
|
|
if !layout.is_unsized() {
|
|
|
|
return Ok(Some(layout.size_and_align()));
|
|
|
|
}
|
2018-08-25 14:36:24 +02:00
|
|
|
match layout.ty.sty {
|
|
|
|
ty::Adt(..) | ty::Tuple(..) => {
|
|
|
|
// First get the size of all statically known fields.
|
|
|
|
// Don't use type_of::sizing_type_of because that expects t to be sized,
|
|
|
|
// and it also rounds up to alignment, which we want to avoid,
|
|
|
|
// as the unsized field's alignment could be smaller.
|
|
|
|
assert!(!layout.ty.is_simd());
|
2018-10-17 12:36:18 +02:00
|
|
|
trace!("DST layout: {:?}", layout);
|
2018-08-25 14:36:24 +02:00
|
|
|
|
|
|
|
let sized_size = layout.fields.offset(layout.fields.count() - 1);
|
|
|
|
let sized_align = layout.align;
|
2018-10-17 12:36:18 +02:00
|
|
|
trace!(
|
2018-08-25 14:36:24 +02:00
|
|
|
"DST {} statically sized prefix size: {:?} align: {:?}",
|
|
|
|
layout.ty,
|
|
|
|
sized_size,
|
|
|
|
sized_align
|
|
|
|
);
|
|
|
|
|
|
|
|
// Recurse to get the size of the dynamically sized field (must be
|
2018-10-09 22:41:41 +02:00
|
|
|
// the last field). Can't have foreign types here, how would we
|
|
|
|
// adjust alignment and size for them?
|
2018-08-25 14:36:24 +02:00
|
|
|
let field = layout.field(self, layout.fields.count() - 1)?;
|
2018-10-09 22:41:41 +02:00
|
|
|
let (unsized_size, unsized_align) = self.size_and_align_of(metadata, field)?
|
|
|
|
.expect("Fields cannot be extern types");
|
2018-08-25 14:36:24 +02:00
|
|
|
|
|
|
|
// FIXME (#26403, #27023): We should be adding padding
|
|
|
|
// to `sized_size` (to accommodate the `unsized_align`
|
|
|
|
// required of the unsized field that follows) before
|
|
|
|
// summing it with `sized_size`. (Note that since #26403
|
|
|
|
// is unfixed, we do not yet add the necessary padding
|
|
|
|
// here. But this is where the add would go.)
|
|
|
|
|
|
|
|
// Return the sum of sizes and max of aligns.
|
|
|
|
let size = sized_size + unsized_size;
|
|
|
|
|
|
|
|
// Choose max of two known alignments (combined value must
|
|
|
|
// be aligned according to more restrictive of the two).
|
|
|
|
let align = sized_align.max(unsized_align);
|
|
|
|
|
|
|
|
// Issue #27023: must add any necessary padding to `size`
|
|
|
|
// (to make it a multiple of `align`) before returning it.
|
|
|
|
//
|
|
|
|
// Namely, the returned size should be, in C notation:
|
|
|
|
//
|
|
|
|
// `size + ((size & (align-1)) ? align : 0)`
|
|
|
|
//
|
|
|
|
// emulated via the semi-standard fast bit trick:
|
|
|
|
//
|
|
|
|
// `(size + (align-1)) & -align`
|
|
|
|
|
2018-10-09 22:41:41 +02:00
|
|
|
Ok(Some((size.abi_align(align), align)))
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
|
|
|
ty::Dynamic(..) => {
|
2018-10-09 22:41:41 +02:00
|
|
|
let vtable = metadata.expect("dyn trait fat ptr must have vtable").to_ptr()?;
|
2018-08-25 14:36:24 +02:00
|
|
|
// the second entry in the vtable is the dynamic size of the object.
|
2018-10-09 22:41:41 +02:00
|
|
|
Ok(Some(self.read_size_and_align_from_vtable(vtable)?))
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::Slice(_) | ty::Str => {
|
2018-10-09 22:41:41 +02:00
|
|
|
let len = metadata.expect("slice fat ptr must have vtable").to_usize(self)?;
|
2018-08-25 14:36:24 +02:00
|
|
|
let (elem_size, align) = layout.field(self, 0)?.size_and_align();
|
2018-10-09 22:41:41 +02:00
|
|
|
Ok(Some((elem_size * len, align)))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Foreign(_) => {
|
|
|
|
Ok(None)
|
2017-07-28 10:16:36 +02:00
|
|
|
}
|
2018-08-25 14:36:24 +02:00
|
|
|
|
|
|
|
_ => bug!("size_and_align_of::<{:?}> not supported", layout.ty),
|
2017-07-28 10:16:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-25 14:36:24 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn size_and_align_of_mplace(
|
|
|
|
&self,
|
2018-09-21 23:32:59 +02:00
|
|
|
mplace: MPlaceTy<'tcx, M::PointerTag>
|
2018-10-09 22:41:41 +02:00
|
|
|
) -> EvalResult<'tcx, Option<(Size, Align)>> {
|
2018-09-29 21:35:20 +02:00
|
|
|
self.size_and_align_of(mplace.meta, mplace.layout)
|
2018-08-25 14:36:24 +02:00
|
|
|
}
|
2017-07-28 10:16:36 +02:00
|
|
|
|
2016-07-05 10:47:10 +02:00
|
|
|
pub fn push_stack_frame(
|
|
|
|
&mut self,
|
2017-03-21 13:53:55 +01:00
|
|
|
instance: ty::Instance<'tcx>,
|
2018-08-18 12:14:03 +02:00
|
|
|
span: source_map::Span,
|
2018-01-16 09:31:48 +01:00
|
|
|
mir: &'mir mir::Mir<'tcx>,
|
2018-10-09 21:05:53 +02:00
|
|
|
return_place: Option<PlaceTy<'tcx, M::PointerTag>>,
|
2016-09-09 17:44:04 +02:00
|
|
|
return_to_block: StackPopCleanup,
|
2017-02-04 13:09:10 -08:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-10-17 12:46:20 +02:00
|
|
|
if self.stack.len() > 1 { // FIXME should be "> 0", printing topmost frame crashes rustc...
|
|
|
|
debug!("PAUSING({}) {}", self.cur_frame(), self.frame().instance);
|
|
|
|
}
|
2016-05-30 18:09:52 +02:00
|
|
|
::log_settings::settings().indentation += 1;
|
|
|
|
|
2018-07-24 18:28:53 +02:00
|
|
|
// first push a stack frame so we have access to the local substs
|
|
|
|
self.stack.push(Frame {
|
|
|
|
mir,
|
|
|
|
block: mir::START_BLOCK,
|
|
|
|
return_to_block,
|
|
|
|
return_place,
|
|
|
|
// empty local array, we fill it in below, after we are inside the stack frame and
|
|
|
|
// all methods actually know about the frame
|
|
|
|
locals: IndexVec::new(),
|
|
|
|
span,
|
|
|
|
instance,
|
|
|
|
stmt: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
// don't allocate at all for trivial constants
|
|
|
|
if mir.local_decls.len() > 1 {
|
2018-08-13 16:14:22 +02:00
|
|
|
// We put some marker value into the locals that we later want to initialize.
|
|
|
|
// This can be anything except for LocalValue::Dead -- because *that* is the
|
|
|
|
// value we use for things that we know are initially dead.
|
|
|
|
let dummy =
|
|
|
|
LocalValue::Live(Operand::Immediate(Value::Scalar(ScalarMaybeUndef::Undef)));
|
2018-08-19 17:01:31 +02:00
|
|
|
let mut locals = IndexVec::from_elem(dummy, &mir.local_decls);
|
2018-10-02 21:16:35 +02:00
|
|
|
// Return place is handled specially by the `eval_place` functions, and the
|
|
|
|
// entry in `locals` should never be used. Make it dead, to be sure.
|
|
|
|
locals[mir::RETURN_PLACE] = LocalValue::Dead;
|
2018-08-13 16:14:22 +02:00
|
|
|
// Now mark those locals as dead that we do not want to initialize
|
2018-03-23 10:47:55 +01:00
|
|
|
match self.tcx.describe_def(instance.def_id()) {
|
|
|
|
// statics and constants don't have `Storage*` statements, no need to look for them
|
|
|
|
Some(Def::Static(..)) | Some(Def::Const(..)) | Some(Def::AssociatedConst(..)) => {},
|
|
|
|
_ => {
|
|
|
|
trace!("push_stack_frame: {:?}: num_bbs: {}", span, mir.basic_blocks().len());
|
|
|
|
for block in mir.basic_blocks() {
|
|
|
|
for stmt in block.statements.iter() {
|
|
|
|
use rustc::mir::StatementKind::{StorageDead, StorageLive};
|
|
|
|
match stmt.kind {
|
|
|
|
StorageLive(local) |
|
2018-08-13 16:14:22 +02:00
|
|
|
StorageDead(local) => {
|
2018-08-19 17:01:31 +02:00
|
|
|
locals[local] = LocalValue::Dead;
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
2018-03-23 10:47:55 +01:00
|
|
|
_ => {}
|
|
|
|
}
|
2018-03-22 16:59:02 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-23 10:47:55 +01:00
|
|
|
},
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
// Finally, properly initialize all those that still have the dummy value
|
2018-08-19 17:01:31 +02:00
|
|
|
for (local, decl) in locals.iter_mut().zip(mir.local_decls.iter()) {
|
|
|
|
match *local {
|
|
|
|
LocalValue::Live(_) => {
|
|
|
|
// This needs to be peoperly initialized.
|
|
|
|
let layout = self.layout_of(self.monomorphize(decl.ty, instance.substs))?;
|
|
|
|
*local = LocalValue::Live(self.uninit_operand(layout)?);
|
|
|
|
}
|
|
|
|
LocalValue::Dead => {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 17:01:31 +02:00
|
|
|
// done
|
|
|
|
self.frame_mut().locals = locals;
|
2018-07-24 18:28:53 +02:00
|
|
|
}
|
2016-10-15 19:48:30 -06:00
|
|
|
|
2018-10-17 16:06:11 +02:00
|
|
|
if self.stack.len() > 1 { // FIXME no check should be needed, but some instances ICE
|
2018-10-17 12:46:20 +02:00
|
|
|
debug!("ENTERING({}) {}", self.cur_frame(), self.frame().instance);
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:12:21 +02:00
|
|
|
if self.stack.len() > self.tcx.sess.const_eval_stack_frame_limit {
|
2017-08-02 16:59:01 +02:00
|
|
|
err!(StackFrameLimitReached)
|
2016-07-05 13:23:58 +02:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-11-12 15:50:58 -06:00
|
|
|
}
|
|
|
|
|
2017-02-04 13:09:10 -08:00
|
|
|
pub(super) fn pop_stack_frame(&mut self) -> EvalResult<'tcx> {
|
2018-10-17 16:06:11 +02:00
|
|
|
if self.stack.len() > 1 { // FIXME no check should be needed, but some instances ICE
|
2018-10-17 12:46:20 +02:00
|
|
|
debug!("LEAVING({}) {}", self.cur_frame(), self.frame().instance);
|
|
|
|
}
|
2016-05-30 18:09:52 +02:00
|
|
|
::log_settings::settings().indentation -= 1;
|
2018-06-22 12:36:54 -07:00
|
|
|
let frame = self.stack.pop().expect(
|
2017-08-10 08:48:38 -07:00
|
|
|
"tried to pop a stack frame, but there were none",
|
|
|
|
);
|
2016-09-09 17:44:04 +02:00
|
|
|
match frame.return_to_block {
|
2018-08-23 19:04:33 +02:00
|
|
|
StackPopCleanup::Goto(block) => {
|
|
|
|
self.goto_block(block)?;
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2018-08-24 17:36:18 +02:00
|
|
|
StackPopCleanup::None { cleanup } => {
|
|
|
|
if !cleanup {
|
2018-10-09 21:05:53 +02:00
|
|
|
// Leak the locals. Also skip validation, this is only used by
|
|
|
|
// static/const computation which does its own (stronger) final
|
|
|
|
// validation.
|
2018-08-24 17:36:18 +02:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 17:55:05 +02:00
|
|
|
}
|
2018-10-09 21:05:53 +02:00
|
|
|
// Deallocate all locals that are backed by an allocation.
|
2017-01-22 00:19:35 -08:00
|
|
|
for local in frame.locals {
|
2017-05-31 17:41:33 -07:00
|
|
|
self.deallocate_local(local)?;
|
2016-11-17 14:48:34 +01:00
|
|
|
}
|
2018-10-09 21:05:53 +02:00
|
|
|
// Validate the return value.
|
|
|
|
if let Some(return_place) = frame.return_place {
|
2018-10-11 08:48:15 +02:00
|
|
|
if M::enforce_validity(self) {
|
2018-10-09 21:05:53 +02:00
|
|
|
// Data got changed, better make sure it matches the type!
|
|
|
|
// It is still possible that the return place held invalid data while
|
|
|
|
// the function is running, but that's okay because nobody could have
|
|
|
|
// accessed that same data from the "outside" to observe any broken
|
|
|
|
// invariant -- that is, unless a function somehow has a ptr to
|
|
|
|
// its return place... but the way MIR is currently generated, the
|
|
|
|
// return place is always a local and then this cannot happen.
|
|
|
|
self.validate_operand(
|
|
|
|
self.place_to_op(return_place)?,
|
|
|
|
&mut vec![],
|
|
|
|
None,
|
|
|
|
/*const_mode*/false,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Uh, that shouln't happen... the function did not intend to return
|
|
|
|
return err!(Unreachable);
|
|
|
|
}
|
2017-03-21 13:53:55 +01:00
|
|
|
|
2018-10-17 12:46:20 +02:00
|
|
|
if self.stack.len() > 1 { // FIXME should be "> 0", printing topmost frame crashes rustc...
|
|
|
|
debug!("CONTINUING({}) {}", self.cur_frame(), self.frame().instance);
|
|
|
|
}
|
|
|
|
|
2016-09-09 17:44:04 +02:00
|
|
|
Ok(())
|
2016-03-06 04:23:24 -06:00
|
|
|
}
|
|
|
|
|
2018-10-16 15:31:04 +02:00
|
|
|
/// Mark a storage as live, killing the previous content and returning it.
|
|
|
|
/// Remember to deallocate that!
|
|
|
|
pub fn storage_live(
|
|
|
|
&mut self,
|
|
|
|
local: mir::Local
|
|
|
|
) -> EvalResult<'tcx, LocalValue<M::PointerTag>> {
|
|
|
|
assert!(local != mir::RETURN_PLACE, "Cannot make return place live");
|
|
|
|
trace!("{:?} is now live", local);
|
|
|
|
|
2018-10-20 11:02:39 +02:00
|
|
|
let layout = self.layout_of_local(self.frame(), local)?;
|
2018-10-16 15:31:04 +02:00
|
|
|
let init = LocalValue::Live(self.uninit_operand(layout)?);
|
|
|
|
// StorageLive *always* kills the value that's currently stored
|
|
|
|
Ok(mem::replace(&mut self.frame_mut().locals[local], init))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the old value of the local.
|
|
|
|
/// Remember to deallocate that!
|
|
|
|
pub fn storage_dead(&mut self, local: mir::Local) -> LocalValue<M::PointerTag> {
|
|
|
|
assert!(local != mir::RETURN_PLACE, "Cannot make return place dead");
|
|
|
|
trace!("{:?} is now dead", local);
|
|
|
|
|
|
|
|
mem::replace(&mut self.frame_mut().locals[local], LocalValue::Dead)
|
|
|
|
}
|
|
|
|
|
2018-09-22 13:36:46 +02:00
|
|
|
pub(super) fn deallocate_local(
|
|
|
|
&mut self,
|
|
|
|
local: LocalValue<M::PointerTag>,
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-13 16:14:22 +02:00
|
|
|
// FIXME: should we tell the user that there was a local which was never written to?
|
|
|
|
if let LocalValue::Live(Operand::Indirect(MemPlace { ptr, .. })) = local {
|
|
|
|
trace!("deallocating local");
|
|
|
|
let ptr = ptr.to_ptr()?;
|
|
|
|
self.memory.dump_alloc(ptr.alloc_id);
|
|
|
|
self.memory.deallocate_local(ptr)?;
|
|
|
|
};
|
|
|
|
Ok(())
|
2017-08-08 15:53:07 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 15:06:45 +01:00
|
|
|
pub fn const_eval(&self, gid: GlobalId<'tcx>) -> EvalResult<'tcx, &'tcx ty::Const<'tcx>> {
|
|
|
|
let param_env = if self.tcx.is_static(gid.instance.def_id()).is_some() {
|
2018-02-10 13:18:02 -05:00
|
|
|
ty::ParamEnv::reveal_all()
|
2018-01-31 15:06:45 +01:00
|
|
|
} else {
|
|
|
|
self.param_env
|
|
|
|
};
|
2018-08-26 15:19:34 +02:00
|
|
|
self.tcx.const_eval(param_env.and(gid)).map_err(|_| {
|
|
|
|
EvalErrorKind::ReferencedConstant.into()
|
|
|
|
})
|
2018-01-31 15:06:45 +01:00
|
|
|
}
|
|
|
|
|
2018-09-21 23:32:59 +02:00
|
|
|
pub fn dump_place(&self, place: Place<M::PointerTag>) {
|
2017-05-30 10:24:37 -07:00
|
|
|
// Debug output
|
2018-04-26 15:35:24 +10:00
|
|
|
if !log_enabled!(::log::Level::Trace) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-06 09:25:29 +01:00
|
|
|
match place {
|
|
|
|
Place::Local { frame, local } => {
|
2017-08-08 16:29:47 +02:00
|
|
|
let mut allocs = Vec::new();
|
|
|
|
let mut msg = format!("{:?}", local);
|
|
|
|
if frame != self.cur_frame() {
|
|
|
|
write!(msg, " ({} frames up)", self.cur_frame() - frame).unwrap();
|
2017-05-31 17:41:33 -07:00
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
write!(msg, ":").unwrap();
|
|
|
|
|
2018-07-24 18:28:53 +02:00
|
|
|
match self.stack[frame].locals[local].access() {
|
2017-08-08 16:29:47 +02:00
|
|
|
Err(err) => {
|
2018-01-31 15:06:45 +01:00
|
|
|
if let EvalErrorKind::DeadLocal = err.kind {
|
2018-01-25 14:15:56 +01:00
|
|
|
write!(msg, " is dead").unwrap();
|
|
|
|
} else {
|
|
|
|
panic!("Failed to access local: {:?}", err);
|
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(Operand::Indirect(mplace)) => {
|
|
|
|
let (ptr, align) = mplace.to_scalar_ptr_align();
|
2018-05-21 00:30:00 +02:00
|
|
|
match ptr {
|
2018-05-20 23:43:16 +02:00
|
|
|
Scalar::Ptr(ptr) => {
|
2017-12-16 14:07:04 +02:00
|
|
|
write!(msg, " by align({}) ref:", align.abi()).unwrap();
|
2017-08-10 08:48:38 -07:00
|
|
|
allocs.push(ptr.alloc_id);
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
ptr => write!(msg, " by integral ref: {:?}", ptr).unwrap(),
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(Operand::Immediate(Value::Scalar(val))) => {
|
2017-08-08 16:29:47 +02:00
|
|
|
write!(msg, " {:?}", val).unwrap();
|
2018-07-24 18:28:53 +02:00
|
|
|
if let ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) = val {
|
2017-08-10 08:48:38 -07:00
|
|
|
allocs.push(ptr.alloc_id);
|
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
Ok(Operand::Immediate(Value::ScalarPair(val1, val2))) => {
|
2017-08-08 16:29:47 +02:00
|
|
|
write!(msg, " ({:?}, {:?})", val1, val2).unwrap();
|
2018-07-24 18:28:53 +02:00
|
|
|
if let ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) = val1 {
|
2017-08-10 08:48:38 -07:00
|
|
|
allocs.push(ptr.alloc_id);
|
|
|
|
}
|
2018-07-24 18:28:53 +02:00
|
|
|
if let ScalarMaybeUndef::Scalar(Scalar::Ptr(ptr)) = val2 {
|
2017-08-10 08:48:38 -07:00
|
|
|
allocs.push(ptr.alloc_id);
|
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
}
|
2017-05-31 17:41:33 -07:00
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
|
|
|
|
trace!("{}", msg);
|
2018-06-22 12:36:54 -07:00
|
|
|
self.memory.dump_allocs(allocs);
|
2017-08-08 16:29:47 +02:00
|
|
|
}
|
2018-08-13 16:14:22 +02:00
|
|
|
Place::Ptr(mplace) => {
|
2018-08-15 20:22:28 +02:00
|
|
|
match mplace.ptr {
|
2018-08-13 16:14:22 +02:00
|
|
|
Scalar::Ptr(ptr) => {
|
2018-08-15 20:22:28 +02:00
|
|
|
trace!("by align({}) ref:", mplace.align.abi());
|
2018-06-22 12:36:54 -07:00
|
|
|
self.memory.dump_alloc(ptr.alloc_id);
|
2017-08-10 08:48:38 -07:00
|
|
|
}
|
2017-08-08 16:29:47 +02:00
|
|
|
ptr => trace!(" integral by ref: {:?}", ptr),
|
2016-10-18 21:45:48 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-07 00:39:40 -08:00
|
|
|
}
|
2016-10-18 21:45:48 -06:00
|
|
|
}
|
2016-11-03 12:52:13 +01:00
|
|
|
|
2018-08-26 15:19:34 +02:00
|
|
|
pub fn generate_stacktrace(&self, explicit_span: Option<Span>) -> Vec<FrameInfo> {
|
2018-01-31 15:06:45 +01:00
|
|
|
let mut last_span = None;
|
|
|
|
let mut frames = Vec::new();
|
|
|
|
// skip 1 because the last frame is just the environment of the constant
|
2018-06-02 23:38:57 +02:00
|
|
|
for &Frame { instance, span, mir, block, stmt, .. } in self.stack().iter().skip(1).rev() {
|
2018-01-31 15:06:45 +01:00
|
|
|
// make sure we don't emit frames that are duplicates of the previous
|
|
|
|
if explicit_span == Some(span) {
|
|
|
|
last_span = Some(span);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(last) = last_span {
|
|
|
|
if last == span {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
last_span = Some(span);
|
|
|
|
}
|
2018-08-22 16:59:14 -03:00
|
|
|
let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data
|
2018-08-23 08:45:59 -07:00
|
|
|
== DefPathData::ClosureExpr
|
|
|
|
{
|
2018-01-31 15:06:45 +01:00
|
|
|
"closure".to_owned()
|
|
|
|
} else {
|
|
|
|
instance.to_string()
|
|
|
|
};
|
2018-06-02 23:38:57 +02:00
|
|
|
let block = &mir.basic_blocks()[block];
|
|
|
|
let source_info = if stmt < block.statements.len() {
|
|
|
|
block.statements[stmt].source_info
|
2018-01-16 09:31:48 +01:00
|
|
|
} else {
|
2018-06-02 23:38:57 +02:00
|
|
|
block.terminator().source_info
|
2018-01-16 09:31:48 +01:00
|
|
|
};
|
2018-06-02 23:38:57 +02:00
|
|
|
let lint_root = match mir.source_scope_local_data {
|
|
|
|
mir::ClearCrossCrate::Set(ref ivs) => Some(ivs[source_info.scope].lint_root),
|
|
|
|
mir::ClearCrossCrate::Clear => None,
|
|
|
|
};
|
|
|
|
frames.push(FrameInfo { span, location, lint_root });
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
2018-06-02 23:38:57 +02:00
|
|
|
trace!("generate stacktrace: {:#?}, {:?}", frames, explicit_span);
|
2018-08-26 15:19:34 +02:00
|
|
|
frames
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
2018-02-21 22:02:52 +01:00
|
|
|
|
2018-08-16 11:38:16 +02:00
|
|
|
#[inline(always)]
|
2018-07-24 18:28:53 +02:00
|
|
|
pub fn sign_extend(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
2018-08-14 20:38:30 +02:00
|
|
|
assert!(ty.abi.is_signed());
|
2018-08-16 11:38:16 +02:00
|
|
|
sign_extend(value, ty.size)
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:38:16 +02:00
|
|
|
#[inline(always)]
|
2018-07-24 18:28:53 +02:00
|
|
|
pub fn truncate(&self, value: u128, ty: TyLayout<'_>) -> u128 {
|
2018-08-16 11:38:16 +02:00
|
|
|
truncate(value, ty.size)
|
2018-02-21 22:02:52 +01:00
|
|
|
}
|
2016-10-15 19:48:30 -06:00
|
|
|
}
|