2017-07-21 10:25:30 -05:00
|
|
|
#![feature(
|
|
|
|
rustc_private,
|
2017-12-14 04:03:55 -06:00
|
|
|
catch_expr,
|
2018-05-01 11:13:22 -05:00
|
|
|
inclusive_range_fields,
|
|
|
|
inclusive_range_methods,
|
2017-07-21 10:25:30 -05:00
|
|
|
)]
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-01-06 09:21:24 -06:00
|
|
|
|
|
|
|
// From rustc.
|
2017-07-25 04:32:48 -05:00
|
|
|
#[macro_use]
|
2017-07-21 10:25:30 -05:00
|
|
|
extern crate rustc;
|
2017-12-14 04:03:55 -06:00
|
|
|
extern crate rustc_data_structures;
|
2018-05-03 17:29:13 -05:00
|
|
|
extern crate rustc_mir;
|
2018-05-01 11:13:22 -05:00
|
|
|
extern crate rustc_target;
|
2017-07-21 10:25:30 -05:00
|
|
|
extern crate syntax;
|
2017-12-14 04:03:55 -06:00
|
|
|
extern crate regex;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-07-24 08:19:32 -05:00
|
|
|
use rustc::ty::{self, TyCtxt};
|
2018-05-20 04:26:40 -05:00
|
|
|
use rustc::ty::layout::{TyLayout, LayoutOf, Size};
|
2018-03-23 06:18:33 -05:00
|
|
|
use rustc::ty::subst::Subst;
|
2017-07-24 08:19:32 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-07-21 10:25:30 -05:00
|
|
|
use rustc::mir;
|
2017-07-21 06:39:06 -05:00
|
|
|
|
2017-08-29 05:24:58 -05:00
|
|
|
use syntax::ast::Mutability;
|
2017-07-28 02:52:19 -05:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
|
2017-08-10 10:48:38 -05:00
|
|
|
use std::collections::{HashMap, BTreeMap};
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-10-05 05:31:47 -05:00
|
|
|
pub use rustc::mir::interpret::*;
|
2017-12-14 04:03:55 -06:00
|
|
|
pub use rustc_mir::interpret::*;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-07-28 02:52:19 -05:00
|
|
|
mod fn_call;
|
2017-07-25 04:32:48 -05:00
|
|
|
mod operator;
|
2017-07-28 06:08:27 -05:00
|
|
|
mod intrinsic;
|
|
|
|
mod helpers;
|
2017-07-28 09:48:43 -05:00
|
|
|
mod memory;
|
2017-07-31 06:30:44 -05:00
|
|
|
mod tls;
|
2017-12-14 04:03:55 -06:00
|
|
|
mod locks;
|
|
|
|
mod range_map;
|
|
|
|
mod validation;
|
2017-07-24 08:19:32 -05:00
|
|
|
|
2017-07-28 02:52:19 -05:00
|
|
|
use fn_call::EvalContextExt as MissingFnsEvalContextExt;
|
2017-07-25 04:32:48 -05:00
|
|
|
use operator::EvalContextExt as OperatorEvalContextExt;
|
2017-07-28 06:08:27 -05:00
|
|
|
use intrinsic::EvalContextExt as IntrinsicEvalContextExt;
|
2017-08-01 04:11:57 -05:00
|
|
|
use tls::EvalContextExt as TlsEvalContextExt;
|
2017-12-14 04:03:55 -06:00
|
|
|
use locks::LockInfo;
|
|
|
|
use locks::MemoryExt as LockMemoryExt;
|
|
|
|
use validation::EvalContextExt as ValidationEvalContextExt;
|
|
|
|
use range_map::RangeMap;
|
|
|
|
use validation::{ValidationQuery, AbsPlace};
|
2017-07-24 08:19:32 -05:00
|
|
|
|
2017-07-21 10:25:30 -05:00
|
|
|
pub fn eval_main<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
|
|
|
) {
|
2018-01-14 11:59:13 -06:00
|
|
|
fn run_main<'a, 'mir: 'a, 'tcx: 'mir>(
|
|
|
|
ecx: &mut rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>,
|
2017-07-21 10:25:30 -05:00
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-01-14 11:59:13 -06:00
|
|
|
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
|
2017-07-21 10:25:30 -05:00
|
|
|
let main_mir = ecx.load_mir(main_instance.def)?;
|
|
|
|
let mut cleanup_ptr = None; // Pointer to be deallocated when we are done
|
|
|
|
|
2017-11-21 06:32:40 -06:00
|
|
|
if !main_mir.return_ty().is_nil() || main_mir.arg_count != 0 {
|
2017-08-10 10:48:38 -05:00
|
|
|
return err!(Unimplemented(
|
|
|
|
"miri does not support main functions without `fn()` type signatures"
|
|
|
|
.to_owned(),
|
|
|
|
));
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(start_id) = start_wrapper {
|
2018-01-15 08:47:17 -06:00
|
|
|
let main_ret_ty = ecx.tcx.fn_sig(main_id).output();
|
|
|
|
let main_ret_ty = main_ret_ty.no_late_bound_regions().unwrap();
|
|
|
|
let start_instance = ty::Instance::resolve(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx.tcx.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
2018-01-15 08:47:17 -06:00
|
|
|
start_id,
|
|
|
|
ecx.tcx.mk_substs(
|
|
|
|
::std::iter::once(ty::subst::Kind::from(main_ret_ty)))).unwrap();
|
2017-07-21 10:25:30 -05:00
|
|
|
let start_mir = ecx.load_mir(start_instance.def)?;
|
|
|
|
|
|
|
|
if start_mir.arg_count != 3 {
|
2017-08-10 10:48:38 -05:00
|
|
|
return err!(AbiViolation(format!(
|
|
|
|
"'start' lang item should have three arguments, but has {}",
|
|
|
|
start_mir.arg_count
|
|
|
|
)));
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return value
|
2018-05-20 04:26:40 -05:00
|
|
|
let size = ecx.tcx.data_layout.pointer_size;
|
2018-01-02 16:43:03 -06:00
|
|
|
let align = ecx.tcx.data_layout.pointer_align;
|
2017-10-05 05:31:47 -05:00
|
|
|
let ret_ptr = ecx.memory_mut().allocate(size, align, Some(MemoryKind::Stack))?;
|
2017-07-21 10:25:30 -05:00
|
|
|
cleanup_ptr = Some(ret_ptr);
|
|
|
|
|
|
|
|
// Push our stack frame
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
start_instance,
|
|
|
|
start_mir.span,
|
|
|
|
start_mir,
|
2018-01-02 16:43:03 -06:00
|
|
|
Place::from_ptr(ret_ptr, align),
|
2017-07-21 10:25:30 -05:00
|
|
|
StackPopCleanup::None,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
|
|
|
|
// First argument: pointer to main()
|
|
|
|
let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance);
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2018-01-14 11:59:13 -06:00
|
|
|
let main_ty = main_instance.ty(ecx.tcx.tcx);
|
|
|
|
let main_ptr_ty = ecx.tcx.mk_fn_ptr(main_ty.fn_sig(ecx.tcx.tcx));
|
2017-08-10 10:48:38 -05:00
|
|
|
ecx.write_value(
|
2017-08-24 07:41:49 -05:00
|
|
|
ValTy {
|
|
|
|
value: Value::ByVal(PrimVal::Ptr(main_ptr)),
|
|
|
|
ty: main_ptr_ty,
|
|
|
|
},
|
2017-08-10 10:48:38 -05:00
|
|
|
dest,
|
|
|
|
)?;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-08-29 05:24:58 -05:00
|
|
|
// Second argument (argc): 1
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2017-07-21 10:25:30 -05:00
|
|
|
let ty = ecx.tcx.types.isize;
|
2017-08-29 05:24:58 -05:00
|
|
|
ecx.write_primval(dest, PrimVal::Bytes(1), ty)?;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-08-29 05:24:58 -05:00
|
|
|
// FIXME: extract main source file path
|
|
|
|
// Third argument (argv): &[b"foo"]
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2017-07-21 10:25:30 -05:00
|
|
|
let ty = ecx.tcx.mk_imm_ptr(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8));
|
2017-10-05 05:31:47 -05:00
|
|
|
let foo = ecx.memory.allocate_cached(b"foo\0");
|
2017-08-29 05:24:58 -05:00
|
|
|
let ptr_size = ecx.memory.pointer_size();
|
2018-01-02 16:43:03 -06:00
|
|
|
let ptr_align = ecx.tcx.data_layout.pointer_align;
|
|
|
|
let foo_ptr = ecx.memory.allocate(ptr_size, ptr_align, None)?;
|
2018-04-13 09:02:55 -05:00
|
|
|
ecx.memory.write_primval(foo_ptr.into(), ptr_align, PrimVal::Ptr(foo.into()), ptr_size, false)?;
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx.memory.mark_static_initialized(foo_ptr.alloc_id, Mutability::Immutable)?;
|
2017-08-29 05:24:58 -05:00
|
|
|
ecx.write_ptr(dest, foo_ptr.into(), ty)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
|
|
|
assert!(args.next().is_none(), "start lang item has more arguments than expected");
|
2017-07-21 10:25:30 -05:00
|
|
|
} else {
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
main_instance,
|
|
|
|
main_mir.span,
|
|
|
|
main_mir,
|
2018-05-07 11:03:28 -05:00
|
|
|
Place::from_primval_ptr(PrimVal::Bytes(1).into(), ty::layout::Align::from_bytes(1, 1).unwrap()),
|
2017-07-21 10:25:30 -05:00
|
|
|
StackPopCleanup::None,
|
|
|
|
)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
|
|
|
// No arguments
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
assert!(args.next().is_none(), "main function must not have arguments");
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while ecx.step()? {}
|
2017-08-01 04:11:57 -05:00
|
|
|
ecx.run_tls_dtors()?;
|
2017-07-21 10:25:30 -05:00
|
|
|
if let Some(cleanup_ptr) = cleanup_ptr {
|
2017-08-10 10:48:38 -05:00
|
|
|
ecx.memory_mut().deallocate(
|
|
|
|
cleanup_ptr,
|
|
|
|
None,
|
|
|
|
MemoryKind::Stack,
|
|
|
|
)?;
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-01-14 11:59:13 -06:00
|
|
|
let mut ecx = EvalContext::new(tcx.at(syntax::codemap::DUMMY_SP), ty::ParamEnv::reveal_all(), Default::default(), Default::default());
|
2017-07-21 10:25:30 -05:00
|
|
|
match run_main(&mut ecx, main_id, start_wrapper) {
|
|
|
|
Ok(()) => {
|
|
|
|
let leaks = ecx.memory().leak_report();
|
|
|
|
if leaks != 0 {
|
2018-03-23 06:18:33 -05:00
|
|
|
//tcx.sess.err("the evaluated program leaked memory");
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
2017-08-04 12:55:35 -05:00
|
|
|
Err(mut e) => {
|
2018-04-07 07:31:39 -05:00
|
|
|
ecx.tcx.sess.err(&e.to_string());
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx.report(&mut e, true, None);
|
2018-05-07 11:03:28 -05:00
|
|
|
for (i, frame) in ecx.stack().iter().enumerate() {
|
|
|
|
trace!("-------------------");
|
|
|
|
trace!("Frame {}", i);
|
|
|
|
trace!(" return: {:#?}", frame.return_place);
|
|
|
|
for (i, local) in frame.locals.iter().enumerate() {
|
|
|
|
if let Some(local) = local {
|
|
|
|
trace!(" local {}: {:?}", i, local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2017-12-14 04:03:55 -06:00
|
|
|
pub struct Evaluator<'tcx> {
|
2017-07-21 10:25:30 -05:00
|
|
|
/// Environment variables set by `setenv`
|
|
|
|
/// Miri does not expose env vars from the host to the emulated program
|
|
|
|
pub(crate) env_vars: HashMap<Vec<u8>, MemoryPointer>,
|
2017-12-14 04:03:55 -06:00
|
|
|
|
|
|
|
/// Places that were suspended by the validation subsystem, and will be recovered later
|
|
|
|
pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type TlsKey = usize;
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct TlsEntry<'tcx> {
|
|
|
|
data: Pointer, // Will eventually become a map from thread IDs to `Pointer`s, if we ever support more than one thread.
|
|
|
|
dtor: Option<ty::Instance<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2017-08-28 07:08:10 -05:00
|
|
|
pub struct MemoryData<'tcx> {
|
2017-07-21 10:25:30 -05:00
|
|
|
/// The Key to use for the next thread-local allocation.
|
|
|
|
next_thread_local: TlsKey,
|
|
|
|
|
|
|
|
/// pthreads-style thread-local storage.
|
|
|
|
thread_local: BTreeMap<TlsKey, TlsEntry<'tcx>>,
|
2017-12-14 04:03:55 -06:00
|
|
|
|
|
|
|
/// Memory regions that are locked by some function
|
|
|
|
///
|
|
|
|
/// Only mutable (static mut, heap, stack) allocations have an entry in this map.
|
|
|
|
/// The entry is created when allocating the memory and deleted after deallocation.
|
2018-01-14 21:31:59 -06:00
|
|
|
locks: HashMap<AllocId, RangeMap<LockInfo<'tcx>>>,
|
2018-03-23 06:06:32 -05:00
|
|
|
|
2018-04-07 04:43:46 -05:00
|
|
|
statics: HashMap<GlobalId<'tcx>, AllocId>,
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
2018-01-14 11:59:13 -06:00
|
|
|
impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
|
2017-07-21 10:25:30 -05:00
|
|
|
type MemoryData = MemoryData<'tcx>;
|
2017-08-09 07:53:22 -05:00
|
|
|
type MemoryKinds = memory::MemoryKind;
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2017-07-21 10:25:30 -05:00
|
|
|
/// Returns Ok() when the function was handled, fail otherwise
|
2017-07-28 02:52:19 -05:00
|
|
|
fn eval_fn_call<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-21 10:25:30 -05:00
|
|
|
instance: ty::Instance<'tcx>,
|
2017-12-06 01:39:31 -06:00
|
|
|
destination: Option<(Place, mir::BasicBlock)>,
|
2017-08-24 07:41:49 -05:00
|
|
|
args: &[ValTy<'tcx>],
|
2017-07-28 02:52:19 -05:00
|
|
|
span: Span,
|
2017-07-21 10:25:30 -05:00
|
|
|
sig: ty::FnSig<'tcx>,
|
2017-07-28 02:52:19 -05:00
|
|
|
) -> EvalResult<'tcx, bool> {
|
2017-08-24 07:41:49 -05:00
|
|
|
ecx.eval_fn_call(instance, destination, args, span, sig)
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2017-07-28 06:08:27 -05:00
|
|
|
fn call_intrinsic<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &mut rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-28 06:08:27 -05:00
|
|
|
instance: ty::Instance<'tcx>,
|
2017-08-24 07:41:49 -05:00
|
|
|
args: &[ValTy<'tcx>],
|
2017-12-06 01:39:31 -06:00
|
|
|
dest: Place,
|
2017-11-21 06:32:40 -06:00
|
|
|
dest_layout: TyLayout<'tcx>,
|
2017-07-28 06:08:27 -05:00
|
|
|
target: mir::BasicBlock,
|
|
|
|
) -> EvalResult<'tcx> {
|
2017-11-21 06:32:40 -06:00
|
|
|
ecx.call_intrinsic(instance, args, dest, dest_layout, target)
|
2017-07-28 06:08:27 -05:00
|
|
|
}
|
|
|
|
|
2017-08-01 04:11:57 -05:00
|
|
|
fn try_ptr_op<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-25 04:32:48 -05:00
|
|
|
bin_op: mir::BinOp,
|
|
|
|
left: PrimVal,
|
|
|
|
left_ty: ty::Ty<'tcx>,
|
|
|
|
right: PrimVal,
|
|
|
|
right_ty: ty::Ty<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, Option<(PrimVal, bool)>> {
|
|
|
|
ecx.ptr_op(bin_op, left, left_ty, right, right_ty)
|
|
|
|
}
|
2017-07-28 09:48:43 -05:00
|
|
|
|
2018-01-14 11:59:13 -06:00
|
|
|
fn mark_static_initialized<'a>(
|
2018-04-07 04:43:46 -05:00
|
|
|
mem: &mut Memory<'a, 'mir, 'tcx, Self>,
|
|
|
|
id: AllocId,
|
2018-01-14 11:59:13 -06:00
|
|
|
_mutability: Mutability,
|
|
|
|
) -> EvalResult<'tcx, bool> {
|
2018-04-07 04:43:46 -05:00
|
|
|
use memory::MemoryKind::*;
|
|
|
|
match mem.get_alloc_kind(id) {
|
2017-07-28 09:48:43 -05:00
|
|
|
// FIXME: This could be allowed, but not for env vars set during miri execution
|
2018-04-07 04:43:46 -05:00
|
|
|
Some(MemoryKind::Machine(Env)) => err!(Unimplemented("statics can't refer to env vars".to_owned())),
|
2018-01-14 11:59:13 -06:00
|
|
|
_ => Ok(false), // TODO: What does the bool mean?
|
2018-04-07 04:43:46 -05:00
|
|
|
}
|
2018-01-14 11:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init_static<'a>(
|
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
|
|
|
cid: GlobalId<'tcx>,
|
|
|
|
) -> EvalResult<'tcx, AllocId> {
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 1: If the static has already been evaluated return the cached version
|
2018-04-07 04:43:46 -05:00
|
|
|
if let Some(alloc_id) = ecx.memory.data.statics.get(&cid) {
|
2018-03-23 06:18:33 -05:00
|
|
|
return Ok(*alloc_id);
|
2018-03-23 06:06:32 -05:00
|
|
|
}
|
2018-03-23 06:18:33 -05:00
|
|
|
|
|
|
|
let tcx = ecx.tcx.tcx;
|
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 2: Load mir
|
2018-03-23 06:18:33 -05:00
|
|
|
let mut mir = ecx.load_mir(cid.instance.def)?;
|
|
|
|
if let Some(index) = cid.promoted {
|
|
|
|
mir = &mir.promoted[index];
|
|
|
|
}
|
|
|
|
assert!(mir.arg_count == 0);
|
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 3: Allocate storage
|
2018-03-23 06:06:32 -05:00
|
|
|
let layout = ecx.layout_of(mir.return_ty().subst(tcx, cid.instance.substs))?;
|
2018-03-23 06:18:33 -05:00
|
|
|
assert!(!layout.is_unsized());
|
|
|
|
let ptr = ecx.memory.allocate(
|
2018-05-20 04:26:40 -05:00
|
|
|
layout.size,
|
2018-03-23 06:06:32 -05:00
|
|
|
layout.align,
|
|
|
|
None,
|
|
|
|
)?;
|
2018-03-23 06:18:33 -05:00
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 4: Cache allocation id for recursive statics
|
2018-04-07 04:43:46 -05:00
|
|
|
assert!(ecx.memory.data.statics.insert(cid, ptr.alloc_id).is_none());
|
2018-03-23 06:18:33 -05:00
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 5: Push stackframe to evaluate static
|
2018-03-23 06:18:33 -05:00
|
|
|
let cleanup = StackPopCleanup::None;
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
cid.instance,
|
|
|
|
mir.span,
|
|
|
|
mir,
|
|
|
|
Place::from_ptr(ptr, layout.align),
|
|
|
|
cleanup,
|
|
|
|
)?;
|
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 6: Step until static has been initialized
|
|
|
|
let call_stackframe = ecx.stack().len();
|
|
|
|
while ecx.step()? && ecx.stack().len() >= call_stackframe {
|
|
|
|
if ecx.stack().len() == call_stackframe {
|
2018-04-07 03:44:19 -05:00
|
|
|
let frame = ecx.frame_mut();
|
2018-04-04 08:54:40 -05:00
|
|
|
let bb = &frame.mir.basic_blocks()[frame.block];
|
|
|
|
if bb.statements.len() == frame.stmt && !bb.is_cleanup {
|
|
|
|
match bb.terminator().kind {
|
|
|
|
::rustc::mir::TerminatorKind::Return => {
|
|
|
|
for (local, _local_decl) in mir.local_decls.iter_enumerated().skip(1) {
|
|
|
|
// Don't deallocate locals, because the return value might reference them
|
2018-04-07 03:44:19 -05:00
|
|
|
frame.storage_dead(local);
|
2018-04-04 08:54:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 06:18:33 -05:00
|
|
|
|
2018-04-07 04:43:46 -05:00
|
|
|
// TODO: Freeze immutable statics without copying them to the global static cache
|
|
|
|
|
2018-04-04 08:54:40 -05:00
|
|
|
// Step 7: Return the alloc
|
2018-03-23 06:18:33 -05:00
|
|
|
Ok(ptr.alloc_id)
|
2017-07-28 09:48:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn box_alloc<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-28 09:48:43 -05:00
|
|
|
ty: ty::Ty<'tcx>,
|
2017-12-06 01:39:31 -06:00
|
|
|
dest: Place,
|
2017-09-25 08:55:21 -05:00
|
|
|
) -> EvalResult<'tcx> {
|
2017-12-06 08:03:24 -06:00
|
|
|
let layout = ecx.layout_of(ty)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
|
|
|
// Call the `exchange_malloc` lang item
|
|
|
|
let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
|
2018-01-14 11:59:13 -06:00
|
|
|
let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
|
2017-09-25 08:55:21 -05:00
|
|
|
let malloc_mir = ecx.load_mir(malloc.def)?;
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
malloc,
|
|
|
|
malloc_mir.span,
|
|
|
|
malloc_mir,
|
|
|
|
dest,
|
|
|
|
// Don't do anything when we are done. The statement() function will increment
|
|
|
|
// the old stack frame's stmt counter to the next statement, which means that when
|
|
|
|
// exchange_malloc returns, we go on evaluating exactly where we want to be.
|
|
|
|
StackPopCleanup::None,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
let usize = ecx.tcx.types.usize;
|
|
|
|
|
|
|
|
// First argument: size
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2017-09-25 08:55:21 -05:00
|
|
|
ecx.write_value(
|
|
|
|
ValTy {
|
2018-03-23 06:18:33 -05:00
|
|
|
value: Value::ByVal(PrimVal::Bytes(match layout.size.bytes() {
|
2018-05-20 04:26:40 -05:00
|
|
|
0 => 1 as u128,
|
|
|
|
size => size as u128,
|
2018-03-23 06:18:33 -05:00
|
|
|
}.into())),
|
2017-09-25 08:55:21 -05:00
|
|
|
ty: usize,
|
|
|
|
},
|
|
|
|
dest,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Second argument: align
|
2017-12-06 01:39:31 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2017-09-25 08:55:21 -05:00
|
|
|
ecx.write_value(
|
|
|
|
ValTy {
|
2017-12-06 08:03:24 -06:00
|
|
|
value: Value::ByVal(PrimVal::Bytes(layout.align.abi().into())),
|
2017-09-25 08:55:21 -05:00
|
|
|
ty: usize,
|
|
|
|
},
|
|
|
|
dest,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// No more arguments
|
|
|
|
assert!(args.next().is_none(), "exchange_malloc lang item has more arguments than expected");
|
|
|
|
Ok(())
|
2017-07-28 09:48:43 -05:00
|
|
|
}
|
2017-09-15 06:02:33 -05:00
|
|
|
|
|
|
|
fn global_item_with_linkage<'a>(
|
2018-04-17 07:26:17 -05:00
|
|
|
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_mutability: Mutability,
|
2017-09-15 06:02:33 -05:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-04-17 07:26:17 -05:00
|
|
|
panic!("remove this function from rustc");
|
2017-09-15 06:02:33 -05:00
|
|
|
}
|
2017-12-14 04:03:55 -06:00
|
|
|
|
|
|
|
fn check_locks<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
mem: &Memory<'a, 'mir, 'tcx, Self>,
|
2017-12-14 04:03:55 -06:00
|
|
|
ptr: MemoryPointer,
|
2018-05-20 04:26:40 -05:00
|
|
|
size: Size,
|
2017-12-14 04:03:55 -06:00
|
|
|
access: AccessKind,
|
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-20 04:26:40 -05:00
|
|
|
mem.check_locks(ptr, size.bytes(), access)
|
2017-12-14 04:03:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_lock<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
mem: &mut Memory<'a, 'mir, 'tcx, Self>,
|
2018-01-14 21:31:59 -06:00
|
|
|
id: AllocId,
|
2017-12-14 04:03:55 -06:00
|
|
|
) {
|
|
|
|
mem.data.locks.insert(id, RangeMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn free_lock<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
mem: &mut Memory<'a, 'mir, 'tcx, Self>,
|
2018-01-14 21:31:59 -06:00
|
|
|
id: AllocId,
|
2017-12-14 04:03:55 -06:00
|
|
|
len: u64,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
mem.data.locks
|
|
|
|
.remove(&id)
|
|
|
|
.expect("allocation has no corresponding locks")
|
|
|
|
.check(
|
|
|
|
Some(mem.cur_frame),
|
|
|
|
0,
|
|
|
|
len,
|
|
|
|
AccessKind::Read,
|
|
|
|
)
|
|
|
|
.map_err(|lock| {
|
|
|
|
EvalErrorKind::DeallocatedLockedMemory {
|
|
|
|
//ptr, FIXME
|
|
|
|
ptr: MemoryPointer {
|
|
|
|
alloc_id: AllocId(0),
|
2018-05-20 04:26:40 -05:00
|
|
|
offset: Size::from_bytes(0),
|
2017-12-14 04:03:55 -06:00
|
|
|
},
|
|
|
|
lock: lock.active,
|
|
|
|
}.into()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_region<'a>(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-12-14 04:03:55 -06:00
|
|
|
reg: Option<::rustc::middle::region::Scope>,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
ecx.end_region(reg)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn validation_op<'a>(
|
2018-05-07 03:49:54 -05:00
|
|
|
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
|
|
|
_op: ::rustc::mir::ValidationOp,
|
|
|
|
_operand: &::rustc::mir::ValidationOperand<'tcx, ::rustc::mir::Place<'tcx>>,
|
2017-12-14 04:03:55 -06:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-07 03:38:13 -05:00
|
|
|
// FIXME: prevent this from ICEing
|
|
|
|
//ecx.validation_op(op, operand)
|
|
|
|
Ok(())
|
2017-12-14 04:03:55 -06:00
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|