2018-08-24 12:49:57 -05:00
|
|
|
#![feature(rustc_private)]
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-07-10 10:32:38 -05:00
|
|
|
#![cfg_attr(feature = "cargo-clippy", allow(cast_lossless))]
|
|
|
|
|
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;
|
|
|
|
|
2018-10-05 11:08:50 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2018-08-23 14:22:57 -05:00
|
|
|
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
|
2018-05-20 04:26:40 -05:00
|
|
|
use rustc::ty::layout::{TyLayout, LayoutOf, Size};
|
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
|
|
|
|
2018-08-23 14:22:57 -05:00
|
|
|
use syntax::attr;
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2017-12-14 04:03:55 -06:00
|
|
|
pub use rustc_mir::interpret::*;
|
2018-10-05 11:08:50 -05:00
|
|
|
pub use rustc_mir::interpret::{self, AllocMap}; // resolve ambiguity
|
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-31 06:30:44 -05:00
|
|
|
mod tls;
|
2017-12-14 04:03:55 -06:00
|
|
|
mod range_map;
|
2018-10-05 11:08:50 -05:00
|
|
|
mod mono_hash_map;
|
2018-10-16 04:21:38 -05:00
|
|
|
mod stacked_borrows;
|
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;
|
2018-09-19 18:00:59 -05:00
|
|
|
use tls::{EvalContextExt as TlsEvalContextExt, TlsData};
|
2017-12-14 04:03:55 -06:00
|
|
|
use range_map::RangeMap;
|
2018-08-26 13:42:26 -05:00
|
|
|
use helpers::FalibleScalarExt;
|
2018-10-05 11:08:50 -05:00
|
|
|
use mono_hash_map::MonoHashMap;
|
2018-10-16 04:21:38 -05:00
|
|
|
use stacked_borrows::Borrow;
|
2018-05-26 10:07:34 -05:00
|
|
|
|
2018-06-12 00:30:29 -05:00
|
|
|
pub fn create_ecx<'a, 'mir: 'a, 'tcx: 'mir>(
|
2017-07-21 10:25:30 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
2018-10-11 04:24:22 -05:00
|
|
|
validate: bool,
|
2018-08-23 12:28:48 -05:00
|
|
|
) -> EvalResult<'tcx, EvalContext<'a, 'mir, 'tcx, Evaluator<'tcx>>> {
|
2018-07-16 04:42:46 -05:00
|
|
|
let mut ecx = EvalContext::new(
|
2018-08-20 09:27:23 -05:00
|
|
|
tcx.at(syntax::source_map::DUMMY_SP),
|
2018-07-16 04:42:46 -05:00
|
|
|
ty::ParamEnv::reveal_all(),
|
2018-10-11 04:24:22 -05:00
|
|
|
Evaluator::new(validate),
|
2018-07-16 04:42:46 -05:00
|
|
|
);
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-06-11 11:49:17 -05:00
|
|
|
let main_instance = ty::Instance::mono(ecx.tcx.tcx, main_id);
|
|
|
|
let main_mir = ecx.load_mir(main_instance.def)?;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-09-15 03:31:20 -05:00
|
|
|
if !main_mir.return_ty().is_unit() || main_mir.arg_count != 0 {
|
2018-06-11 11:49:17 -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
|
|
|
|
2018-06-11 11:49:17 -05: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 10:25:30 -05:00
|
|
|
|
2018-08-23 12:28:48 -05:00
|
|
|
// Return value (in static memory so that it does not count as leak)
|
2018-10-09 15:03:14 -05:00
|
|
|
let ret = ecx.layout_of(start_mir.return_ty())?;
|
|
|
|
let ret_ptr = ecx.allocate(ret, MiriMemoryKind::MutStatic.into())?;
|
2018-06-11 11:49:17 -05:00
|
|
|
|
|
|
|
// Push our stack frame
|
|
|
|
ecx.push_stack_frame(
|
|
|
|
start_instance,
|
|
|
|
start_mir.span,
|
|
|
|
start_mir,
|
2018-10-09 15:03:14 -05:00
|
|
|
Some(ret_ptr.into()),
|
2018-08-24 10:44:04 -05:00
|
|
|
StackPopCleanup::None { cleanup: true },
|
2018-06-11 11:49:17 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
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 14:01:40 -05:00
|
|
|
ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
|
2018-06-11 11:49:17 -05:00
|
|
|
|
|
|
|
// Second argument (argc): 1
|
|
|
|
let dest = ecx.eval_place(&mir::Place::Local(args.next().unwrap()))?;
|
2018-08-15 14:01:40 -05:00
|
|
|
ecx.write_scalar(Scalar::from_int(1, dest.layout.size), dest)?;
|
2018-06-11 11:49:17 -05: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 12:28:48 -05:00
|
|
|
let foo = ecx.memory.allocate_static_bytes(b"foo\0");
|
2018-08-15 14:01:40 -05:00
|
|
|
let foo_ty = ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8);
|
|
|
|
let foo_layout = ecx.layout_of(foo_ty)?;
|
2018-10-16 04:21:38 -05:00
|
|
|
let foo_place = ecx.allocate(foo_layout, MiriMemoryKind::Env.into())?;
|
2018-08-15 14:01:40 -05:00
|
|
|
ecx.write_scalar(Scalar::Ptr(foo), foo_place.into())?;
|
2018-10-16 04:21:38 -05:00
|
|
|
ecx.memory.mark_immutable(foo_place.to_ptr()?.alloc_id)?;
|
2018-08-15 14:01:40 -05:00
|
|
|
ecx.write_scalar(foo_place.ptr, dest)?;
|
2018-06-11 11:49:17 -05:00
|
|
|
|
|
|
|
assert!(args.next().is_none(), "start lang item has more arguments than expected");
|
|
|
|
} else {
|
2018-10-09 15:03:14 -05:00
|
|
|
let ret_place = MPlaceTy::dangling(ecx.layout_of(tcx.mk_unit())?, &ecx).into();
|
2018-06-11 11:49:17 -05:00
|
|
|
ecx.push_stack_frame(
|
|
|
|
main_instance,
|
|
|
|
main_mir.span,
|
|
|
|
main_mir,
|
2018-10-09 15:03:14 -05:00
|
|
|
Some(ret_place),
|
2018-08-24 10:44:04 -05:00
|
|
|
StackPopCleanup::None { cleanup: true },
|
2018-06-11 11:49:17 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
// No arguments
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
assert!(args.next().is_none(), "main function must not have arguments");
|
|
|
|
}
|
2017-09-25 08:55:21 -05:00
|
|
|
|
2018-08-23 12:28:48 -05:00
|
|
|
Ok(ecx)
|
2018-06-11 11:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eval_main<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
main_id: DefId,
|
|
|
|
start_wrapper: Option<DefId>,
|
2018-10-11 04:24:22 -05:00
|
|
|
validate: bool,
|
2018-06-11 11:49:17 -05:00
|
|
|
) {
|
2018-10-11 04:24:22 -05:00
|
|
|
let mut ecx = create_ecx(tcx, main_id, start_wrapper, validate).expect("Couldn't create ecx");
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-08-24 12:49:57 -05:00
|
|
|
let res: EvalResult = (|| {
|
2018-08-23 12:28:48 -05:00
|
|
|
ecx.run()?;
|
2018-08-24 12:49:57 -05:00
|
|
|
ecx.run_tls_dtors()
|
|
|
|
})();
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-06-11 11:49:17 -05:00
|
|
|
match res {
|
2017-07-21 10:25:30 -05:00
|
|
|
Ok(()) => {
|
|
|
|
let leaks = ecx.memory().leak_report();
|
2018-08-30 01:57:33 -05:00
|
|
|
// Disable the leak test on some platforms where we likely do not
|
|
|
|
// correctly implement TLS destructors.
|
2018-08-30 02:41:57 -05:00
|
|
|
let target_os = ecx.tcx.tcx.sess.target.target.target_os.to_lowercase();
|
|
|
|
let ignore_leaks = target_os == "windows" || target_os == "macos";
|
|
|
|
if !ignore_leaks && leaks != 0 {
|
2018-08-23 12:28:48 -05:00
|
|
|
tcx.sess.err("the evaluated program leaked memory");
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-10 04:23:56 -05: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 04:21:56 -05: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 04:23:56 -05:00
|
|
|
let (frames, span) = ecx.generate_stacktrace(None);
|
2018-07-15 04:21:56 -05:00
|
|
|
err.span_label(span, e);
|
2018-07-02 11:00:36 -05:00
|
|
|
for FrameInfo { span, location, .. } in frames {
|
2018-06-10 04:23:56 -05:00
|
|
|
err.span_note(span, &format!("inside call to `{}`", location));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
} else {
|
|
|
|
ecx.tcx.sess.err(&e.to_string());
|
|
|
|
}
|
|
|
|
|
2018-08-23 12:28:48 -05:00
|
|
|
/* Nice try, but with MIRI_BACKTRACE this shows 100s of backtraces.
|
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() {
|
2018-08-07 08:22:11 -05:00
|
|
|
if let Ok(local) = local.access() {
|
2018-05-07 11:03:28 -05:00
|
|
|
trace!(" local {}: {:?}", i, local);
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 12:28:48 -05:00
|
|
|
}*/
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum MiriMemoryKind {
|
|
|
|
/// `__rust_alloc` memory
|
|
|
|
Rust,
|
|
|
|
/// `malloc` memory
|
|
|
|
C,
|
|
|
|
/// Part of env var emulation
|
|
|
|
Env,
|
|
|
|
/// mutable statics
|
|
|
|
MutStatic,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
|
|
|
|
fn into(self) -> MemoryKind<MiriMemoryKind> {
|
|
|
|
MemoryKind::Machine(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-11 04:24:22 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
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
|
2018-10-16 04:21:38 -05:00
|
|
|
pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Borrow>>,
|
2017-12-14 04:03:55 -06:00
|
|
|
|
2018-09-19 18:00:59 -05:00
|
|
|
/// TLS state
|
|
|
|
pub(crate) tls: TlsData<'tcx>,
|
2018-10-11 04:24:22 -05:00
|
|
|
|
|
|
|
/// Whether to enforce the validity invariant
|
|
|
|
pub(crate) validate: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Evaluator<'tcx> {
|
|
|
|
fn new(validate: bool) -> Self {
|
|
|
|
Evaluator {
|
|
|
|
env_vars: HashMap::default(),
|
|
|
|
tls: TlsData::default(),
|
|
|
|
validate,
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
impl<'a, 'mir, 'tcx> Machine<'a, 'mir, 'tcx> for Evaluator<'tcx> {
|
2018-09-19 18:00:59 -05:00
|
|
|
type MemoryKinds = MiriMemoryKind;
|
2018-10-16 04:21:38 -05:00
|
|
|
type AllocExtra = ();
|
|
|
|
type PointerTag = Borrow;
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2018-10-16 04:21:38 -05:00
|
|
|
type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Borrow, Self::AllocExtra>)>;
|
2018-10-05 11:08:50 -05:00
|
|
|
|
|
|
|
const STATIC_KIND: Option<MiriMemoryKind> = Some(MiriMemoryKind::MutStatic);
|
2018-10-11 02:02:28 -05:00
|
|
|
|
2018-10-11 04:24:22 -05:00
|
|
|
fn enforce_validity(ecx: &EvalContext<'a, 'mir, 'tcx, Self>) -> bool {
|
2018-10-12 03:40:44 -05:00
|
|
|
if !ecx.machine.validate {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some functions are whitelisted until we figure out how to fix them.
|
|
|
|
// We walk up the stack a few frames to also cover their callees.
|
|
|
|
const WHITELIST: &[&str] = &[
|
|
|
|
// Uses mem::uninitialized
|
|
|
|
"std::ptr::read",
|
2018-10-14 04:06:36 -05:00
|
|
|
"std::sys::windows::mutex::Mutex::",
|
2018-10-12 03:40:44 -05:00
|
|
|
];
|
|
|
|
for frame in ecx.stack().iter()
|
|
|
|
.rev().take(3)
|
|
|
|
{
|
|
|
|
let name = frame.instance.to_string();
|
|
|
|
if WHITELIST.iter().any(|white| name.starts_with(white)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
2018-10-11 02:02:28 -05:00
|
|
|
}
|
2018-08-26 06:19:03 -05:00
|
|
|
|
2017-07-21 10:25:30 -05:00
|
|
|
/// Returns Ok() when the function was handled, fail otherwise
|
2018-09-20 05:24:55 -05:00
|
|
|
fn find_fn(
|
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>,
|
2018-10-16 04:21:38 -05:00
|
|
|
args: &[OpTy<'tcx, Borrow>],
|
|
|
|
dest: Option<PlaceTy<'tcx, Borrow>>,
|
2018-08-23 12:28:48 -05:00
|
|
|
ret: Option<mir::BasicBlock>,
|
|
|
|
) -> EvalResult<'tcx, Option<&'mir mir::Mir<'tcx>>> {
|
|
|
|
ecx.find_fn(instance, args, dest, ret)
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
fn call_intrinsic(
|
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>,
|
2018-10-16 04:21:38 -05:00
|
|
|
args: &[OpTy<'tcx, Borrow>],
|
|
|
|
dest: PlaceTy<'tcx, Borrow>,
|
2017-07-28 06:08:27 -05:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-23 12:28:48 -05:00
|
|
|
ecx.call_intrinsic(instance, args, dest)
|
2017-07-28 06:08:27 -05:00
|
|
|
}
|
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
fn ptr_op(
|
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,
|
2018-10-16 04:21:38 -05:00
|
|
|
left: Scalar<Borrow>,
|
2018-08-15 14:01:40 -05:00
|
|
|
left_layout: TyLayout<'tcx>,
|
2018-10-16 04:21:38 -05:00
|
|
|
right: Scalar<Borrow>,
|
2018-08-15 14:01:40 -05:00
|
|
|
right_layout: TyLayout<'tcx>,
|
2018-10-16 04:21:38 -05:00
|
|
|
) -> EvalResult<'tcx, (Scalar<Borrow>, bool)> {
|
2018-08-15 14:01:40 -05:00
|
|
|
ecx.ptr_op(bin_op, left, left_layout, right, right_layout)
|
2017-07-25 04:32:48 -05:00
|
|
|
}
|
2017-07-28 09:48:43 -05:00
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
fn box_alloc(
|
2018-01-14 11:59:13 -06:00
|
|
|
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
|
2018-10-16 04:21:38 -05:00
|
|
|
dest: PlaceTy<'tcx, Borrow>,
|
2017-09-25 08:55:21 -05:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-08-15 14:01:40 -05:00
|
|
|
trace!("box_alloc for {:?}", dest.layout.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,
|
2018-10-09 15:03:14 -05:00
|
|
|
Some(dest),
|
2017-09-25 08:55:21 -05: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.
|
2018-08-24 10:44:04 -05:00
|
|
|
StackPopCleanup::None { cleanup: true },
|
2017-09-25 08:55:21 -05:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
2018-08-15 14:01:40 -05:00
|
|
|
let layout = ecx.layout_of(dest.layout.ty.builtin_deref(false).unwrap().ty)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
|
|
|
// First argument: size
|
2018-08-15 14:01:40 -05: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 08:55:21 -05:00
|
|
|
|
|
|
|
// Second argument: align
|
2018-08-15 14:01:40 -05: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 08:55:21 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
fn find_foreign_static(
|
2018-08-23 14:22:57 -05:00
|
|
|
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
|
|
|
def_id: DefId,
|
2018-10-16 04:21:38 -05:00
|
|
|
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Borrow, Self::AllocExtra>>> {
|
2018-08-23 14:22:57 -05:00
|
|
|
let attrs = tcx.get_attrs(def_id);
|
|
|
|
let link_name = match attr::first_attr_value_str_by_name(&attrs, "link_name") {
|
|
|
|
Some(name) => name.as_str(),
|
|
|
|
None => tcx.item_name(def_id).as_str(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let alloc = match &link_name[..] {
|
|
|
|
"__cxa_thread_atexit_impl" => {
|
|
|
|
// This should be all-zero, pointer-sized
|
|
|
|
let data = vec![0; tcx.data_layout.pointer_size.bytes() as usize];
|
2018-10-05 11:08:50 -05:00
|
|
|
Allocation::from_bytes(&data[..], tcx.data_layout.pointer_align)
|
2018-08-23 14:22:57 -05:00
|
|
|
}
|
|
|
|
_ => return err!(Unimplemented(
|
|
|
|
format!("can't access foreign static: {}", link_name),
|
|
|
|
)),
|
|
|
|
};
|
2018-10-05 11:08:50 -05:00
|
|
|
Ok(Cow::Owned(alloc))
|
2017-09-15 06:02:33 -05:00
|
|
|
}
|
2017-12-14 04:03:55 -06:00
|
|
|
|
2018-09-20 05:24:55 -05:00
|
|
|
fn before_terminator(_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>) -> EvalResult<'tcx>
|
|
|
|
{
|
|
|
|
// We are not interested in detecting loops
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-05 11:08:50 -05:00
|
|
|
|
|
|
|
fn static_with_default_tag(
|
|
|
|
alloc: &'_ Allocation
|
2018-10-16 04:21:38 -05:00
|
|
|
) -> Cow<'_, Allocation<Borrow, Self::AllocExtra>> {
|
|
|
|
let alloc: Allocation<Borrow, Self::AllocExtra> = Allocation {
|
|
|
|
bytes: alloc.bytes.clone(),
|
|
|
|
relocations: Relocations::from_presorted(
|
|
|
|
alloc.relocations.iter()
|
|
|
|
.map(|&(offset, ((), alloc))| (offset, (Borrow::default(), alloc)))
|
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
undef_mask: alloc.undef_mask.clone(),
|
|
|
|
align: alloc.align,
|
|
|
|
mutability: alloc.mutability,
|
|
|
|
extra: Self::AllocExtra::default(),
|
|
|
|
};
|
2018-10-05 11:08:50 -05:00
|
|
|
Cow::Owned(alloc)
|
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|