2017-07-21 17:25:30 +02:00
|
|
|
#![feature(
|
|
|
|
rustc_private,
|
2017-12-14 11:03:55 +01:00
|
|
|
catch_expr,
|
2017-07-21 17:25:30 +02:00
|
|
|
)]
|
|
|
|
|
2018-07-10 17:32:38 +02:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
|
|
|
|
|
2017-07-21 17:25:30 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-01-06 10:21:24 -05:00
|
|
|
|
|
|
|
// From rustc.
|
2017-07-25 11:32:48 +02:00
|
|
|
#[macro_use]
|
2017-07-21 17:25:30 +02:00
|
|
|
extern crate rustc;
|
2017-12-14 11:03:55 +01:00
|
|
|
extern crate rustc_data_structures;
|
2018-05-03 23:29:13 +01:00
|
|
|
extern crate rustc_mir;
|
2018-05-01 17:13:22 +01:00
|
|
|
extern crate rustc_target;
|
2017-07-21 17:25:30 +02:00
|
|
|
extern crate syntax;
|
|
|
|
|
2017-07-24 15:19:32 +02:00
|
|
|
use rustc::ty::{self, TyCtxt};
|
2018-05-20 11:26:40 +02:00
|
|
|
use rustc::ty::layout::{TyLayout, LayoutOf, Size};
|
2017-07-24 15:19:32 +02:00
|
|
|
use rustc::hir::def_id::DefId;
|
2017-07-21 17:25:30 +02:00
|
|
|
use rustc::mir;
|
2017-07-21 13:39:06 +02:00
|
|
|
|
2018-06-29 12:48:55 -07:00
|
|
|
use rustc_data_structures::fx::FxHasher;
|
|
|
|
|
2017-08-29 12:24:58 +02:00
|
|
|
use syntax::ast::Mutability;
|
2017-07-28 09:52:19 +02:00
|
|
|
|
2018-08-15 21:01:40 +02:00
|
|
|
use std::marker::PhantomData;
|
2017-08-10 08:48:38 -07:00
|
|
|
use std::collections::{HashMap, BTreeMap};
|
2018-06-29 12:48:55 -07:00
|
|
|
use std::hash::{Hash, Hasher};
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2017-10-05 12:31:47 +02:00
|
|
|
pub use rustc::mir::interpret::*;
|
2017-12-14 11:03:55 +01:00
|
|
|
pub use rustc_mir::interpret::*;
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2017-07-28 09:52:19 +02:00
|
|
|
mod fn_call;
|
2017-07-25 11:32:48 +02:00
|
|
|
mod operator;
|
2017-07-28 13:08:27 +02:00
|
|
|
mod intrinsic;
|
|
|
|
mod helpers;
|
2017-07-28 16:48:43 +02:00
|
|
|
mod memory;
|
2017-07-31 13:30:44 +02:00
|
|
|
mod tls;
|
2017-12-14 11:03:55 +01:00
|
|
|
mod locks;
|
|
|
|
mod range_map;
|
2017-07-24 15:19:32 +02:00
|
|
|
|
2017-07-28 09:52:19 +02:00
|
|
|
use fn_call::EvalContextExt as MissingFnsEvalContextExt;
|
2017-07-25 11:32:48 +02:00
|
|
|
use operator::EvalContextExt as OperatorEvalContextExt;
|
2017-07-28 13:08:27 +02:00
|
|
|
use intrinsic::EvalContextExt as IntrinsicEvalContextExt;
|
2017-08-01 11:11:57 +02:00
|
|
|
use tls::EvalContextExt as TlsEvalContextExt;
|
2018-08-23 19:28:48 +02:00
|
|
|
use memory::MemoryKind as MiriMemoryKind;
|
2017-12-14 11:03:55 +01:00
|
|
|
use locks::LockInfo;
|
|
|
|
use range_map::RangeMap;
|
2018-08-15 21:01:40 +02:00
|
|
|
use helpers::{ScalarExt, FalibleScalarExt};
|
2018-05-26 17:07:34 +02:00
|
|
|
|
2018-06-12 07:30:29 +02:00
|
|
|
pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
|
2017-07-21 17:25:30 +02:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
2018-08-23 19:28:48 +02:00
|
|
|
) -> EvalResult<'tcx, EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>> {
|
2018-07-16 11:42:46 +02:00
|
|
|
let mut ecx = EvalContext::new(
|
2018-08-20 16:27:23 +02:00
|
|
|
tcx.at(syntax::source_map::DUMMY_SP),
|
2018-07-16 11:42:46 +02:00
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
Default::default(),
|
|
|
|
MemoryData::new()
|
|
|
|
);
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-11 18:49:17 +02:00
|
|
|
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
|
|
|
|
let main_mir = ecx.load_mir(main_instance.def)?;
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-11 18:49:17 +02:00
|
|
|
if !main_mir.return_ty().is_nil() || main_mir.arg_count != 0 {
|
|
|
|
return err!(Unimplemented(
|
|
|
|
"miri does not support main functions without `fn()` type signatures"
|
|
|
|
.to_owned(),
|
|
|
|
));
|
|
|
|
}
|
2018-08-07 15:22:11 +02:00
|
|
|
let ptr_size = ecx.memory.pointer_size();
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-11 18:49:17 +02:00
|
|
|
if let Some(start_id) = start_wrapper {
|
|
|
|
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(
|
|
|
|
ecx.tcx.tcx,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
start_id,
|
|
|
|
ecx.tcx.mk_substs(
|
|
|
|
::std::iter::once(ty::subst::Kind::from(main_ret_ty)))
|
|
|
|
).unwrap();
|
|
|
|
let start_mir = ecx.load_mir(start_instance.def)?;
|
|
|
|
|
|
|
|
if start_mir.arg_count != 3 {
|
|
|
|
return err!(AbiViolation(format!(
|
|
|
|
"'start' lang item should have three arguments, but has {}",
|
|
|
|
start_mir.arg_count
|
|
|
|
)));
|
|
|
|
}
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-08-23 19:28:48 +02:00
|
|
|
// Return value (in static memory so that it does not count as leak)
|
2018-06-11 18:49:17 +02:00
|
|
|
let size = ecx.tcx.data_layout.pointer_size;
|
|
|
|
let align = ecx.tcx.data_layout.pointer_align;
|
2018-08-23 19:28:48 +02:00
|
|
|
let ret_ptr = ecx.memory_mut().allocate(size, align, MiriMemoryKind::MutStatic.into())?;
|
2018-06-11 18:49:17 +02:00
|
|
|
|
|
|
|
// Push our stack frame
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
start_instance,
|
|
|
|
start_mir.span,
|
|
|
|
start_mir,
|
|
|
|
Place::from_ptr(ret_ptr, align),
|
|
|
|
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);
|
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2018-08-15 21:01:40 +02:00
|
|
|
ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
|
2018-06-11 18:49:17 +02:00
|
|
|
|
|
|
|
// Second argument (argc): 1
|
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2018-08-15 21:01:40 +02:00
|
|
|
ecx.write_scalar(Scalar::from_int(1, dest.layout.size), dest)?;
|
2018-06-11 18:49:17 +02:00
|
|
|
|
|
|
|
// FIXME: extract main source file path
|
|
|
|
// Third argument (argv): &[b"foo"]
|
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2018-08-23 19:28:48 +02:00
|
|
|
let foo = ecx.memory.allocate_static_bytes(b"foo\0");
|
2018-08-15 21:01:40 +02:00
|
|
|
let foo_ty = ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8);
|
|
|
|
let foo_layout = ecx.layout_of(foo_ty)?;
|
2018-08-23 19:28:48 +02:00
|
|
|
let foo_place = ecx.allocate(foo_layout, MemoryKind::Stack)?; // will be marked as static in just a second
|
2018-08-15 21:01:40 +02:00
|
|
|
ecx.write_scalar(Scalar::Ptr(foo), foo_place.into())?;
|
2018-08-23 19:28:48 +02:00
|
|
|
ecx.memory.mark_static_initialized(foo_place.to_ptr()?.alloc_id, Mutability::Immutable)?; // marked as static
|
2018-08-15 21:01:40 +02:00
|
|
|
ecx.write_scalar(foo_place.ptr, dest)?;
|
2018-06-11 18:49:17 +02:00
|
|
|
|
|
|
|
assert!(args.next().is_none(), "start lang item has more arguments than expected");
|
|
|
|
} else {
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
main_instance,
|
|
|
|
main_mir.span,
|
|
|
|
main_mir,
|
2018-08-07 15:22:11 +02:00
|
|
|
Place::from_scalar_ptr(Scalar::from_int(1, ptr_size).into(), ty::layout::Align::from_bytes(1, 1).unwrap()),
|
2018-06-11 18:49:17 +02:00
|
|
|
StackPopCleanup::None,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// No arguments
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
assert!(args.next().is_none(), "main function must not have arguments");
|
|
|
|
}
|
2017-09-25 15:55:21 +02:00
|
|
|
|
2018-08-23 19:28:48 +02:00
|
|
|
Ok(ecx)
|
2018-06-11 18:49:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eval_main<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
|
|
|
) {
|
2018-08-23 19:28:48 +02:00
|
|
|
let mut ecx = create_ecx(tcx, main_id, start_wrapper).expect("Couldn't create ecx");
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-11 18:49:17 +02:00
|
|
|
let res: EvalResult = do catch {
|
2018-08-23 19:28:48 +02:00
|
|
|
ecx.run()?;
|
2017-08-01 11:11:57 +02:00
|
|
|
ecx.run_tls_dtors()?;
|
2018-06-11 18:49:17 +02:00
|
|
|
};
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-11 18:49:17 +02:00
|
|
|
match res {
|
2017-07-21 17:25:30 +02:00
|
|
|
Ok(()) => {
|
|
|
|
let leaks = ecx.memory().leak_report();
|
|
|
|
if leaks != 0 {
|
2018-08-23 19:28:48 +02:00
|
|
|
tcx.sess.err("the evaluated program leaked memory");
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-10 11:23:56 +02:00
|
|
|
Err(e) => {
|
|
|
|
if let Some(frame) = ecx.stack().last() {
|
|
|
|
let block = &frame.mir.basic_blocks()[frame.block];
|
|
|
|
let span = if frame.stmt < block.statements.len() {
|
|
|
|
block.statements[frame.stmt].source_info.span
|
|
|
|
} else {
|
|
|
|
block.terminator().source_info.span
|
|
|
|
};
|
|
|
|
|
2018-07-15 11:21:56 +02:00
|
|
|
let e = e.to_string();
|
|
|
|
let msg = format!("constant evaluation error: {}", e);
|
|
|
|
let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
|
2018-06-10 11:23:56 +02:00
|
|
|
let (frames, span) = ecx.generate_stacktrace(None);
|
2018-07-15 11:21:56 +02:00
|
|
|
err.span_label(span, e);
|
2018-07-02 17:00:36 +01:00
|
|
|
for FrameInfo { span, location, .. } in frames {
|
2018-06-10 11:23:56 +02:00
|
|
|
err.span_note(span, &format!("inside call to `{}`", location));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
ecx.tcx.sess.err(&e.to_string());
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:28:48 +02:00
|
|
|
/* Nice try, but with MIRI_BACKTRACE this shows 100s of backtraces.
|
2018-05-07 18:03:28 +02: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() {
|
2018-08-07 15:22:11 +02:00
|
|
|
if let Ok(local) = local.access() {
|
2018-05-07 18:03:28 +02:00
|
|
|
trace!(" local {}: {:?}", i, local);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 19:28:48 +02:00
|
|
|
}*/
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:48:55 -07:00
|
|
|
#[derive(Clone, Default, PartialEq, Eq)]
|
2017-12-14 11:03:55 +01:00
|
|
|
pub struct Evaluator<'tcx> {
|
2017-07-21 17:25:30 +02:00
|
|
|
/// Environment variables set by `setenv`
|
|
|
|
/// Miri does not expose env vars from the host to the emulated program
|
2018-05-26 17:07:34 +02:00
|
|
|
pub(crate) env_vars: HashMap<Vec<u8>, Pointer>,
|
2017-12-14 11:03:55 +01:00
|
|
|
|
2018-08-15 21:01:40 +02:00
|
|
|
/// Use the lifetime
|
|
|
|
_dummy : PhantomData<&'tcx ()>,
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:48:55 -07:00
|
|
|
impl<'tcx> Hash for Evaluator<'tcx> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
let Evaluator {
|
|
|
|
env_vars,
|
2018-08-15 21:01:40 +02:00
|
|
|
_dummy: _,
|
2018-06-29 12:48:55 -07:00
|
|
|
} = self;
|
|
|
|
|
|
|
|
env_vars.iter()
|
|
|
|
.map(|(env, ptr)| {
|
|
|
|
let mut h = FxHasher::default();
|
|
|
|
env.hash(&mut h);
|
|
|
|
ptr.hash(&mut h);
|
|
|
|
h.finish()
|
|
|
|
})
|
|
|
|
.fold(0u64, |acc, hash| acc.wrapping_add(hash))
|
|
|
|
.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 15:59:04 +02:00
|
|
|
pub type TlsKey = u128;
|
2017-07-21 17:25:30 +02:00
|
|
|
|
2018-06-29 12:48:55 -07:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
|
2017-07-21 17:25:30 +02:00
|
|
|
pub struct TlsEntry<'tcx> {
|
2018-05-26 17:07:34 +02:00
|
|
|
data: Scalar, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
|
2017-07-21 17:25:30 +02:00
|
|
|
dtor: Option<ty::Instance<'tcx>>,
|
|
|
|
}
|
|
|
|
|
2018-07-16 11:42:46 +02:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2017-08-28 14:08:10 +02:00
|
|
|
pub struct MemoryData<'tcx> {
|
2017-07-21 17:25:30 +02: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 11:03:55 +01: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 22:31:59 -05:00
|
|
|
locks: HashMap<AllocId, RangeMap<LockInfo<'tcx>>>,
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
|
|
|
|
2018-07-16 11:42:46 +02:00
|
|
|
impl<'tcx> MemoryData<'tcx> {
|
|
|
|
fn new() -> Self {
|
|
|
|
MemoryData {
|
|
|
|
next_thread_local: 1, // start with 1 as we must not use 0 on Windows
|
|
|
|
thread_local: BTreeMap::new(),
|
|
|
|
locks: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:48:55 -07:00
|
|
|
impl<'tcx> Hash for MemoryData<'tcx> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
let MemoryData {
|
|
|
|
next_thread_local: _,
|
|
|
|
thread_local,
|
|
|
|
locks: _,
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
thread_local.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 18:59:13 +01:00
|
|
|
impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
|
2017-07-21 17:25:30 +02:00
|
|
|
type MemoryData = MemoryData<'tcx>;
|
2017-08-09 14:53:22 +02:00
|
|
|
type MemoryKinds = memory::MemoryKind;
|
2017-07-28 09:52:19 +02:00
|
|
|
|
2017-07-21 17:25:30 +02:00
|
|
|
/// Returns Ok() when the function was handled, fail otherwise
|
2018-08-23 19:28:48 +02:00
|
|
|
fn find_fn<'a>(
|
2018-01-14 18:59:13 +01:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-21 17:25:30 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
2018-08-15 21:01:40 +02:00
|
|
|
args: &[OpTy<'tcx>],
|
2018-08-23 19:28:48 +02:00
|
|
|
dest: Option<PlaceTy<'tcx>>,
|
|
|
|
ret: Option<mir::BasicBlock>,
|
|
|
|
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
|
|
|
|
ecx.find_fn(instance, args, dest, ret)
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|
2017-07-28 09:52:19 +02:00
|
|
|
|
2017-07-28 13:08:27 +02:00
|
|
|
fn call_intrinsic<'a>(
|
2018-01-14 18:59:13 +01:00
|
|
|
ecx: &mut rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-28 13:08:27 +02:00
|
|
|
instance: ty::Instance<'tcx>,
|
2018-08-15 21:01:40 +02:00
|
|
|
args: &[OpTy<'tcx>],
|
|
|
|
dest: PlaceTy<'tcx>,
|
2017-07-28 13:08:27 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-23 19:28:48 +02:00
|
|
|
ecx.call_intrinsic(instance, args, dest)
|
2017-07-28 13:08:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-01 11:11:57 +02:00
|
|
|
fn try_ptr_op<'a>(
|
2018-01-14 18:59:13 +01:00
|
|
|
ecx: &rustc_mir::interpret::EvalContext<'a, 'mir, 'tcx, Self>,
|
2017-07-25 11:32:48 +02:00
|
|
|
bin_op: mir::BinOp,
|
2018-05-26 17:07:34 +02:00
|
|
|
left: Scalar,
|
2018-08-15 21:01:40 +02:00
|
|
|
left_layout: TyLayout<'tcx>,
|
2018-05-26 17:07:34 +02:00
|
|
|
right: Scalar,
|
2018-08-15 21:01:40 +02:00
|
|
|
right_layout: TyLayout<'tcx>,
|
2018-05-26 17:07:34 +02:00
|
|
|
) -> EvalResult<'tcx, Option<(Scalar, bool)>> {
|
2018-08-15 21:01:40 +02:00
|
|
|
ecx.ptr_op(bin_op, left, left_layout, right, right_layout)
|
2017-07-25 11:32:48 +02:00
|
|
|
}
|
2017-07-28 16:48:43 +02:00
|
|
|
|
2018-08-23 19:28:48 +02:00
|
|
|
fn access_static_mut<'a, 'm>(
|
|
|
|
mem: &'m mut Memory<'a, 'mir, 'tcx, Self>,
|
2018-04-07 11:43:46 +02:00
|
|
|
id: AllocId,
|
2018-08-23 19:28:48 +02:00
|
|
|
) -> EvalResult<'tcx, &'m mut Allocation> {
|
|
|
|
// Make a copy, use that.
|
|
|
|
mem.deep_copy_static(id, MiriMemoryKind::MutStatic.into())?;
|
|
|
|
mem.get_mut(id) // this is recursive, but now we know that `id` is in `alloc_map`
|
2017-07-28 16:48:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn box_alloc<'a>(
|
2018-01-14 18:59:13 +01:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2018-08-15 21:01:40 +02:00
|
|
|
dest: PlaceTy<'tcx>,
|
2017-09-25 15:55:21 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-15 21:01:40 +02:00
|
|
|
trace!("box_alloc for {:?}", dest.layout.ty);
|
2017-09-25 15:55:21 +02:00
|
|
|
// Call the `exchange_malloc` lang item
|
|
|
|
let malloc = ecx.tcx.lang_items().exchange_malloc_fn().unwrap();
|
2018-01-14 18:59:13 +01:00
|
|
|
let malloc = ty::Instance::mono(ecx.tcx.tcx, malloc);
|
2017-09-25 15:55:21 +02:00
|
|
|
let malloc_mir = ecx.load_mir(malloc.def)?;
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
malloc,
|
|
|
|
malloc_mir.span,
|
|
|
|
malloc_mir,
|
2018-08-15 21:01:40 +02:00
|
|
|
*dest,
|
2017-09-25 15:55:21 +02:00
|
|
|
// 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();
|
2018-08-15 21:01:40 +02:00
|
|
|
let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
|
2017-09-25 15:55:21 +02:00
|
|
|
|
|
|
|
// First argument: size
|
2018-08-15 21:01:40 +02:00
|
|
|
// (0 is allowed here, this is expected to be handled by the lang item)
|
|
|
|
let arg = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
|
|
|
let size = layout.size.bytes();
|
|
|
|
ecx.write_scalar(Scalar::from_uint(size, arg.layout.size), arg)?;
|
2017-09-25 15:55:21 +02:00
|
|
|
|
|
|
|
// Second argument: align
|
2018-08-15 21:01:40 +02:00
|
|
|
let arg = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
|
|
|
let align = layout.align.abi();
|
|
|
|
ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
|
2017-09-25 15:55:21 +02:00
|
|
|
|
|
|
|
// No more arguments
|
|
|
|
assert!(args.next().is_none(), "exchange_malloc lang item has more arguments than expected");
|
|
|
|
Ok(())
|
2017-07-28 16:48:43 +02:00
|
|
|
}
|
2017-09-15 13:02:33 +02:00
|
|
|
|
|
|
|
fn global_item_with_linkage<'a>(
|
2018-04-17 14:26:17 +02:00
|
|
|
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
|
|
|
_instance: ty::Instance<'tcx>,
|
|
|
|
_mutability: Mutability,
|
2017-09-15 13:02:33 +02:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-04-17 14:26:17 +02:00
|
|
|
panic!("remove this function from rustc");
|
2017-09-15 13:02:33 +02:00
|
|
|
}
|
2017-12-14 11:03:55 +01:00
|
|
|
|
|
|
|
fn validation_op<'a>(
|
2018-05-07 10:49:54 +02:00
|
|
|
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
|
|
|
_op: ::rustc::mir::ValidationOp,
|
|
|
|
_operand: &::rustc::mir::ValidationOperand<'tcx, ::rustc::mir::Place<'tcx>>,
|
2017-12-14 11:03:55 +01:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-05-07 10:38:13 +02:00
|
|
|
// FIXME: prevent this from ICEing
|
|
|
|
//ecx.validation_op(op, operand)
|
|
|
|
Ok(())
|
2017-12-14 11:03:55 +01:00
|
|
|
}
|
2017-07-21 17:25:30 +02:00
|
|
|
}
|