2019-03-18 07:30:57 -05:00
|
|
|
#![feature(rustc_private)]
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-11-25 08:44:28 -06:00
|
|
|
#![allow(clippy::cast_lossless)]
|
2018-07-10 10:32:38 -05:00
|
|
|
|
2017-07-21 10:25:30 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2018-01-06 09:21:24 -06:00
|
|
|
// From rustc.
|
2018-10-31 04:19:20 -05:00
|
|
|
extern crate syntax;
|
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
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
mod fn_call;
|
|
|
|
mod operator;
|
|
|
|
mod intrinsic;
|
|
|
|
mod helpers;
|
|
|
|
mod tls;
|
|
|
|
mod range_map;
|
|
|
|
mod mono_hash_map;
|
|
|
|
mod stacked_borrows;
|
|
|
|
|
2018-10-05 11:08:50 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::borrow::Cow;
|
2019-04-15 08:36:09 -05:00
|
|
|
use std::rc::Rc;
|
2018-10-05 11:08:50 -05:00
|
|
|
|
2019-04-07 17:17:43 -05:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2018-10-05 11:08:50 -05:00
|
|
|
|
2018-11-05 09:05:17 -06:00
|
|
|
use rustc::ty::{self, TyCtxt, query::TyCtxtAt};
|
2019-02-08 09:27:00 -06:00
|
|
|
use rustc::ty::layout::{LayoutOf, Size, Align};
|
2018-10-18 09:59:08 -05:00
|
|
|
use rustc::hir::{self, def_id::DefId};
|
2017-07-21 10:25:30 -05:00
|
|
|
use rustc::mir;
|
2019-02-15 19:29:38 -06:00
|
|
|
pub use rustc_mir::interpret::*;
|
|
|
|
// Resolve ambiguity.
|
|
|
|
pub use rustc_mir::interpret::{self, AllocMap, PlaceTy};
|
2018-08-23 14:22:57 -05:00
|
|
|
use syntax::attr;
|
2018-11-15 02:09:48 -06:00
|
|
|
use syntax::source_map::DUMMY_SP;
|
2019-05-13 14:55:58 -05:00
|
|
|
use syntax::symbol::sym;
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2018-11-27 07:41:53 -06:00
|
|
|
pub use crate::fn_call::EvalContextExt as MissingFnsEvalContextExt;
|
|
|
|
pub use crate::operator::EvalContextExt as OperatorEvalContextExt;
|
|
|
|
pub use crate::intrinsic::EvalContextExt as IntrinsicEvalContextExt;
|
|
|
|
pub use crate::tls::{EvalContextExt as TlsEvalContextExt, TlsData};
|
2018-11-01 02:56:41 -05:00
|
|
|
use crate::range_map::RangeMap;
|
2019-02-26 12:37:05 -06:00
|
|
|
#[allow(unused_imports)] // FIXME: rustc bug, issue <https://github.com/rust-lang/rust/issues/53682>.
|
2018-12-11 07:32:59 -06:00
|
|
|
pub use crate::helpers::{EvalContextExt as HelpersEvalContextExt};
|
2018-11-01 02:56:41 -05:00
|
|
|
use crate::mono_hash_map::MonoHashMap;
|
2018-11-27 07:41:53 -06:00
|
|
|
pub use crate::stacked_borrows::{EvalContextExt as StackedBorEvalContextExt};
|
2018-05-26 10:07:34 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Used by priroda.
|
2019-04-15 08:36:09 -05:00
|
|
|
pub use crate::stacked_borrows::{Tag, Permission, Stack, Stacks, Item};
|
2018-10-24 04:39:31 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
|
2018-10-24 10:16:41 -05:00
|
|
|
/// set per default, for maximal validation power.
|
2018-11-03 05:03:53 -05:00
|
|
|
pub fn miri_default_args() -> &'static [&'static str] {
|
2018-10-24 10:16:41 -05:00
|
|
|
// The flags here should be kept in sync with what bootstrap adds when `test-miri` is
|
2018-11-25 15:23:54 -06:00
|
|
|
// set, which happens in `bootstrap/bin/rustc.rs` in the rustc sources.
|
2019-02-07 06:00:27 -06:00
|
|
|
&["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri"]
|
2018-10-24 10:16:41 -05:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Configuration needed to spawn a Miri instance.
|
2019-02-08 12:21:44 -06:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MiriConfig {
|
|
|
|
pub validate: bool,
|
|
|
|
pub args: Vec<String>,
|
2019-04-07 17:17:43 -05:00
|
|
|
|
|
|
|
// The seed to use when non-determinism is required (e.g. getrandom())
|
|
|
|
pub seed: Option<u64>
|
2019-02-08 12:21:44 -06:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Used by priroda.
|
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,
|
2019-02-08 12:21:44 -06:00
|
|
|
config: MiriConfig,
|
2019-03-27 03:58:11 -05:00
|
|
|
) -> EvalResult<'tcx, InterpretCx<'a, 'mir, 'tcx, Evaluator<'tcx>>> {
|
|
|
|
let mut ecx = InterpretCx::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(),
|
2019-04-07 17:17:43 -05:00
|
|
|
Evaluator::new(config.validate, config.seed),
|
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-12-10 05:26:20 -06:00
|
|
|
let start_id = tcx.lang_items().start_fn().unwrap();
|
|
|
|
let main_ret_ty = tcx.fn_sig(main_id).output();
|
|
|
|
let main_ret_ty = main_ret_ty.no_bound_vars().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
|
|
|
|
)));
|
2018-06-11 11:49:17 -05:00
|
|
|
}
|
2017-09-25 08:55:21 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Return value (in static memory so that it does not count as leak).
|
2018-12-10 05:26:20 -06:00
|
|
|
let ret = ecx.layout_of(start_mir.return_ty())?;
|
2018-12-23 07:13:16 -06:00
|
|
|
let ret_ptr = ecx.allocate(ret, MiriMemoryKind::MutStatic.into());
|
2018-12-10 05:26:20 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Push our stack frame.
|
2018-12-10 05:26:20 -06:00
|
|
|
ecx.push_stack_frame(
|
|
|
|
start_instance,
|
2019-02-15 19:29:38 -06:00
|
|
|
// There is no call site.
|
|
|
|
DUMMY_SP,
|
2018-12-10 05:26:20 -06:00
|
|
|
start_mir,
|
|
|
|
Some(ret_ptr.into()),
|
|
|
|
StackPopCleanup::None { cleanup: true },
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut args = ecx.frame().mir.args_iter();
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// First argument: pointer to `main()`.
|
2018-12-10 05:26:20 -06:00
|
|
|
let main_ptr = ecx.memory_mut().create_fn_alloc(main_instance).with_default_tag();
|
2019-02-26 11:58:13 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
|
2018-12-10 05:26:20 -06:00
|
|
|
ecx.write_scalar(Scalar::Ptr(main_ptr), dest)?;
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Second argument (argc): `1`.
|
2019-02-26 11:58:13 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
|
2019-02-08 12:21:44 -06:00
|
|
|
let argc = Scalar::from_uint(config.args.len() as u128, dest.layout.size);
|
2018-12-18 12:26:57 -06:00
|
|
|
ecx.write_scalar(argc, dest)?;
|
2019-02-26 12:37:05 -06:00
|
|
|
// Store argc for macOS's `_NSGetArgc`.
|
2018-12-19 08:25:25 -06:00
|
|
|
{
|
2018-12-23 07:13:16 -06:00
|
|
|
let argc_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
|
2018-12-19 08:25:25 -06:00
|
|
|
ecx.write_scalar(argc, argc_place.into())?;
|
|
|
|
ecx.machine.argc = Some(argc_place.ptr.to_ptr()?);
|
|
|
|
}
|
2018-12-10 05:26:20 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// FIXME: extract main source file path.
|
|
|
|
// Third argument (`argv`): created from `config.args`.
|
2019-02-26 11:58:13 -06:00
|
|
|
let dest = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
|
2019-02-15 19:29:38 -06:00
|
|
|
// For Windows, construct a command string with all the aguments.
|
2019-02-08 13:37:07 -06:00
|
|
|
let mut cmd = String::new();
|
|
|
|
for arg in config.args.iter() {
|
|
|
|
if !cmd.is_empty() {
|
|
|
|
cmd.push(' ');
|
|
|
|
}
|
|
|
|
cmd.push_str(&*shell_escape::windows::escape(arg.as_str().into()));
|
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// Don't forget `0` terminator.
|
|
|
|
cmd.push(std::char::from_u32(0).unwrap());
|
2019-02-08 12:21:44 -06:00
|
|
|
// Collect the pointers to the individual strings.
|
2019-04-15 08:36:09 -05:00
|
|
|
let mut argvs = Vec::<Pointer<Tag>>::new();
|
2019-02-08 12:21:44 -06:00
|
|
|
for arg in config.args {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Add `0` terminator.
|
2019-02-08 12:21:44 -06:00
|
|
|
let mut arg = arg.into_bytes();
|
|
|
|
arg.push(0);
|
|
|
|
argvs.push(ecx.memory_mut().allocate_static_bytes(arg.as_slice()).with_default_tag());
|
|
|
|
}
|
|
|
|
// Make an array with all these pointers, in the Miri memory.
|
|
|
|
let argvs_layout = ecx.layout_of(ecx.tcx.mk_array(ecx.tcx.mk_imm_ptr(ecx.tcx.types.u8), argvs.len() as u64))?;
|
|
|
|
let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Env.into());
|
|
|
|
for (idx, arg) in argvs.into_iter().enumerate() {
|
|
|
|
let place = ecx.mplace_field(argvs_place, idx as u64)?;
|
|
|
|
ecx.write_scalar(Scalar::Ptr(arg), place.into())?;
|
|
|
|
}
|
|
|
|
ecx.memory_mut().mark_immutable(argvs_place.to_ptr()?.alloc_id)?;
|
2019-02-15 19:29:38 -06:00
|
|
|
// Write a pointer to that place as the argument.
|
2019-02-08 12:21:44 -06:00
|
|
|
let argv = argvs_place.ptr;
|
|
|
|
ecx.write_scalar(argv, dest)?;
|
2019-02-15 19:29:38 -06:00
|
|
|
// Store `argv` for macOS `_NSGetArgv`.
|
2018-12-19 08:25:25 -06:00
|
|
|
{
|
2018-12-23 07:13:16 -06:00
|
|
|
let argv_place = ecx.allocate(dest.layout, MiriMemoryKind::Env.into());
|
2018-12-19 08:25:25 -06:00
|
|
|
ecx.write_scalar(argv, argv_place.into())?;
|
|
|
|
ecx.machine.argv = Some(argv_place.ptr.to_ptr()?);
|
|
|
|
}
|
2019-02-15 19:29:38 -06:00
|
|
|
// Store command line as UTF-16 for Windows `GetCommandLineW`.
|
2018-12-19 08:25:25 -06:00
|
|
|
{
|
|
|
|
let tcx = &{ecx.tcx.tcx};
|
2019-02-08 13:37:07 -06:00
|
|
|
let cmd_utf16: Vec<u16> = cmd.encode_utf16().collect();
|
2018-12-19 08:25:25 -06:00
|
|
|
let cmd_ptr = ecx.memory_mut().allocate(
|
|
|
|
Size::from_bytes(cmd_utf16.len() as u64 * 2),
|
|
|
|
Align::from_bytes(2).unwrap(),
|
|
|
|
MiriMemoryKind::Env.into(),
|
2019-04-15 08:36:09 -05:00
|
|
|
);
|
2018-12-19 08:25:25 -06:00
|
|
|
ecx.machine.cmd_line = Some(cmd_ptr);
|
2019-02-15 19:29:38 -06:00
|
|
|
// Store the UTF-16 string.
|
2018-12-19 08:25:25 -06:00
|
|
|
let char_size = Size::from_bytes(2);
|
|
|
|
let cmd_alloc = ecx.memory_mut().get_mut(cmd_ptr.alloc_id)?;
|
|
|
|
let mut cur_ptr = cmd_ptr;
|
|
|
|
for &c in cmd_utf16.iter() {
|
|
|
|
cmd_alloc.write_scalar(
|
|
|
|
tcx,
|
|
|
|
cur_ptr,
|
|
|
|
Scalar::from_uint(c, char_size).into(),
|
|
|
|
char_size,
|
|
|
|
)?;
|
|
|
|
cur_ptr = cur_ptr.offset(char_size, tcx)?;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 05:26:20 -06:00
|
|
|
|
|
|
|
assert!(args.next().is_none(), "start lang item has more arguments than expected");
|
|
|
|
|
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,
|
2019-02-08 12:21:44 -06:00
|
|
|
config: MiriConfig,
|
2018-06-11 11:49:17 -05:00
|
|
|
) {
|
2019-04-15 08:36:09 -05:00
|
|
|
let mut ecx = match create_ecx(tcx, main_id, config) {
|
|
|
|
Ok(ecx) => ecx,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.print_backtrace();
|
|
|
|
panic!("Miri initialziation error: {}", err.kind)
|
|
|
|
}
|
|
|
|
};
|
2017-07-21 10:25:30 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Perform the main execution.
|
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-10-31 04:09:55 -05:00
|
|
|
// Process the result.
|
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-11-15 02:09:48 -06:00
|
|
|
// Disable the leak test on some platforms where we do not
|
2018-08-30 01:57:33 -05:00
|
|
|
// 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-10-31 04:09:55 -05:00
|
|
|
Err(mut e) => {
|
2019-04-19 10:25:27 -05:00
|
|
|
// Special treatment for some error kinds
|
|
|
|
let msg = match e.kind {
|
|
|
|
InterpError::Exit(code) => std::process::exit(code),
|
|
|
|
InterpError::NoMirFor(..) =>
|
|
|
|
format!("{}. Did you set `MIRI_SYSROOT` to a Miri-enabled sysroot? You can prepare one with `cargo miri setup`.", e),
|
|
|
|
_ => e.to_string()
|
|
|
|
};
|
2018-10-31 04:09:55 -05:00
|
|
|
e.print_backtrace();
|
2018-06-10 04:23:56 -05:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2019-04-19 10:25:27 -05:00
|
|
|
let msg = format!("Miri evaluation error: {}", msg);
|
2018-07-15 04:21:56 -05:00
|
|
|
let mut err = struct_error(ecx.tcx.tcx.at(span), msg.as_str());
|
2018-10-29 03:09:03 -05:00
|
|
|
let frames = ecx.generate_stacktrace(None);
|
2019-04-19 10:25:27 -05:00
|
|
|
err.span_label(span, msg);
|
2019-02-15 19:29:38 -06:00
|
|
|
// We iterate with indices because we need to look at the next frame (the caller).
|
2018-11-15 02:09:48 -06:00
|
|
|
for idx in 0..frames.len() {
|
|
|
|
let frame_info = &frames[idx];
|
|
|
|
let call_site_is_local = frames.get(idx+1).map_or(false,
|
|
|
|
|caller_info| caller_info.instance.def_id().is_local());
|
|
|
|
if call_site_is_local {
|
|
|
|
err.span_note(frame_info.call_site, &frame_info.to_string());
|
|
|
|
} else {
|
|
|
|
err.note(&frame_info.to_string());
|
|
|
|
}
|
2018-06-10 04:23:56 -05:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
} else {
|
2019-04-19 10:25:27 -05:00
|
|
|
ecx.tcx.sess.err(&msg);
|
2018-06-10 04:23:56 -05:00
|
|
|
}
|
|
|
|
|
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() {
|
2019-04-08 06:40:11 -05:00
|
|
|
trace!(" local {}: {:?}", i, local.value);
|
2018-05-07 11:03:28 -05:00
|
|
|
}
|
2018-10-31 04:16:42 -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 {
|
2019-02-15 19:29:38 -06:00
|
|
|
/// `__rust_alloc` memory.
|
2018-09-19 18:00:59 -05:00
|
|
|
Rust,
|
2019-02-15 19:29:38 -06:00
|
|
|
/// `malloc` memory.
|
2018-09-19 18:00:59 -05:00
|
|
|
C,
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Part of env var emulation.
|
2018-09-19 18:00:59 -05:00
|
|
|
Env,
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Mutable statics.
|
2018-09-19 18:00:59 -05:00
|
|
|
MutStatic,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<MemoryKind<MiriMemoryKind>> for MiriMemoryKind {
|
2018-10-16 05:44:04 -05:00
|
|
|
#[inline(always)]
|
2018-09-19 18:00:59 -05:00
|
|
|
fn into(self) -> MemoryKind<MiriMemoryKind> {
|
|
|
|
MemoryKind::Machine(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 05:44:04 -05:00
|
|
|
impl MayLeak for MiriMemoryKind {
|
|
|
|
#[inline(always)]
|
|
|
|
fn may_leak(self) -> bool {
|
2018-11-05 08:45:24 -06:00
|
|
|
use self::MiriMemoryKind::*;
|
2018-10-16 05:44:04 -05:00
|
|
|
match self {
|
|
|
|
Rust | C => false,
|
|
|
|
Env | MutStatic => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 18:00:59 -05:00
|
|
|
|
2017-12-14 04:03:55 -06:00
|
|
|
pub struct Evaluator<'tcx> {
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Environment variables set by `setenv`.
|
|
|
|
/// Miri does not expose env vars from the host to the emulated program.
|
2019-04-15 08:36:09 -05:00
|
|
|
pub(crate) env_vars: HashMap<Vec<u8>, Pointer<Tag>>,
|
2017-12-14 04:03:55 -06:00
|
|
|
|
2018-12-18 12:26:57 -06:00
|
|
|
/// Program arguments (`Option` because we can only initialize them after creating the ecx).
|
|
|
|
/// These are *pointers* to argc/argv because macOS.
|
2019-02-15 19:29:38 -06:00
|
|
|
/// We also need the full command line as one string because of Windows.
|
2019-04-15 08:36:09 -05:00
|
|
|
pub(crate) argc: Option<Pointer<Tag>>,
|
|
|
|
pub(crate) argv: Option<Pointer<Tag>>,
|
|
|
|
pub(crate) cmd_line: Option<Pointer<Tag>>,
|
2018-12-19 08:25:25 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Last OS error.
|
2018-12-19 08:25:25 -06:00
|
|
|
pub(crate) last_error: u32,
|
2018-12-18 12:26:57 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// TLS state.
|
2018-09-19 18:00:59 -05:00
|
|
|
pub(crate) tls: TlsData<'tcx>,
|
2018-10-11 04:24:22 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Whether to enforce the validity invariant.
|
2018-10-11 04:24:22 -05:00
|
|
|
pub(crate) validate: bool,
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2019-04-07 17:17:43 -05:00
|
|
|
/// The random number generator to use if Miri
|
|
|
|
/// is running in non-deterministic mode
|
2019-04-09 14:49:34 -05:00
|
|
|
pub(crate) rng: Option<StdRng>
|
2018-10-11 04:24:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Evaluator<'tcx> {
|
2019-04-07 17:17:43 -05:00
|
|
|
fn new(validate: bool, seed: Option<u64>) -> Self {
|
2018-10-11 04:24:22 -05:00
|
|
|
Evaluator {
|
|
|
|
env_vars: HashMap::default(),
|
2018-12-18 12:26:57 -06:00
|
|
|
argc: None,
|
|
|
|
argv: None,
|
2018-12-19 08:25:25 -06:00
|
|
|
cmd_line: None,
|
|
|
|
last_error: 0,
|
2018-10-11 04:24:22 -05:00
|
|
|
tls: TlsData::default(),
|
|
|
|
validate,
|
2019-04-09 14:49:34 -05:00
|
|
|
rng: seed.map(|s| StdRng::seed_from_u64(s))
|
2018-10-11 04:24:22 -05:00
|
|
|
}
|
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// FIXME: rustc issue <https://github.com/rust-lang/rust/issues/47131>.
|
|
|
|
#[allow(dead_code)]
|
2019-03-27 03:58:11 -05:00
|
|
|
type MiriEvalContext<'a, 'mir, 'tcx> = InterpretCx<'a, 'mir, 'tcx, Evaluator<'tcx>>;
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// A little trait that's useful to be inherited by extension traits.
|
2018-12-11 07:16:58 -06:00
|
|
|
pub trait MiriEvalContextExt<'a, 'mir, 'tcx> {
|
|
|
|
fn eval_context_ref(&self) -> &MiriEvalContext<'a, 'mir, 'tcx>;
|
|
|
|
fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'a, 'mir, 'tcx>;
|
|
|
|
}
|
|
|
|
impl<'a, 'mir, 'tcx> MiriEvalContextExt<'a, 'mir, 'tcx> for MiriEvalContext<'a, 'mir, 'tcx> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn eval_context_ref(&self) -> &MiriEvalContext<'a, 'mir, 'tcx> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'a, 'mir, 'tcx> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:01:50 -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-17 10:36:07 -05:00
|
|
|
|
2018-11-15 11:15:05 -06:00
|
|
|
type FrameExtra = stacked_borrows::CallId;
|
|
|
|
type MemoryExtra = stacked_borrows::MemoryState;
|
2018-10-16 11:01:50 -05:00
|
|
|
type AllocExtra = stacked_borrows::Stacks;
|
2019-04-15 08:36:09 -05:00
|
|
|
type PointerTag = Tag;
|
2017-07-28 02:52:19 -05:00
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
type MemoryMap = MonoHashMap<AllocId, (MemoryKind<MiriMemoryKind>, Allocation<Tag, 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-12-02 10:18:25 -06:00
|
|
|
#[inline(always)]
|
2019-03-27 03:58:11 -05:00
|
|
|
fn enforce_validity(ecx: &InterpretCx<'a, 'mir, 'tcx, Self>) -> bool {
|
2018-12-02 10:18:25 -06:00
|
|
|
ecx.machine.validate
|
2018-10-11 02:02:28 -05:00
|
|
|
}
|
2018-08-26 06:19:03 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
/// Returns `Ok()` when the function was handled; fail otherwise.
|
2018-10-16 11:01:50 -05:00
|
|
|
#[inline(always)]
|
2018-09-20 05:24:55 -05:00
|
|
|
fn find_fn(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
|
2017-07-21 10:25:30 -05:00
|
|
|
instance: ty::Instance<'tcx>,
|
2019-04-15 08:36:09 -05:00
|
|
|
args: &[OpTy<'tcx, Tag>],
|
|
|
|
dest: Option<PlaceTy<'tcx, Tag>>,
|
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-10-16 11:01:50 -05:00
|
|
|
#[inline(always)]
|
2018-09-20 05:24:55 -05:00
|
|
|
fn call_intrinsic(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut rustc_mir::interpret::InterpretCx<'a, 'mir, 'tcx, Self>,
|
2017-07-28 06:08:27 -05:00
|
|
|
instance: ty::Instance<'tcx>,
|
2019-04-15 08:36:09 -05:00
|
|
|
args: &[OpTy<'tcx, Tag>],
|
|
|
|
dest: PlaceTy<'tcx, Tag>,
|
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-10-16 11:01:50 -05:00
|
|
|
#[inline(always)]
|
2018-09-20 05:24:55 -05:00
|
|
|
fn ptr_op(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &rustc_mir::interpret::InterpretCx<'a, 'mir, 'tcx, Self>,
|
2017-07-25 04:32:48 -05:00
|
|
|
bin_op: mir::BinOp,
|
2019-04-15 08:36:09 -05:00
|
|
|
left: ImmTy<'tcx, Tag>,
|
|
|
|
right: ImmTy<'tcx, Tag>,
|
|
|
|
) -> EvalResult<'tcx, (Scalar<Tag>, bool)> {
|
2019-02-08 07:01:40 -06:00
|
|
|
ecx.ptr_op(bin_op, left, right)
|
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(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
|
2019-04-15 08:36:09 -05:00
|
|
|
dest: PlaceTy<'tcx, Tag>,
|
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);
|
2019-02-15 19:29:38 -06:00
|
|
|
// Call the `exchange_malloc` lang item.
|
2017-09-25 08:55:21 -05:00
|
|
|
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),
|
2019-02-15 19:29:38 -06:00
|
|
|
// Don't do anything when we are done. The `statement()` function will increment
|
2017-09-25 08:55:21 -05:00
|
|
|
// the old stack frame's stmt counter to the next statement, which means that when
|
2019-02-15 19:29:38 -06:00
|
|
|
// `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
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// First argument: `size`.
|
|
|
|
// (`0` is allowed here -- this is expected to be handled by the lang item).
|
2019-02-26 11:58:13 -06:00
|
|
|
let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
|
2018-08-15 14:01:40 -05:00
|
|
|
let size = layout.size.bytes();
|
|
|
|
ecx.write_scalar(Scalar::from_uint(size, arg.layout.size), arg)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Second argument: `align`.
|
2019-02-26 11:58:13 -06:00
|
|
|
let arg = ecx.eval_place(&mir::Place::Base(mir::PlaceBase::Local(args.next().unwrap())))?;
|
2018-11-23 02:46:51 -06:00
|
|
|
let align = layout.align.abi.bytes();
|
2018-08-15 14:01:40 -05:00
|
|
|
ecx.write_scalar(Scalar::from_uint(align, arg.layout.size), arg)?;
|
2017-09-25 08:55:21 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// No more arguments.
|
|
|
|
assert!(
|
|
|
|
args.next().is_none(),
|
|
|
|
"`exchange_malloc` lang item has more arguments than expected"
|
|
|
|
);
|
2017-09-25 08:55:21 -05:00
|
|
|
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
|
|
|
def_id: DefId,
|
2018-11-15 06:29:55 -06:00
|
|
|
tcx: TyCtxtAt<'a, 'tcx, 'tcx>,
|
|
|
|
memory_extra: &Self::MemoryExtra,
|
2019-04-15 08:36:09 -05:00
|
|
|
) -> EvalResult<'tcx, Cow<'tcx, Allocation<Tag, Self::AllocExtra>>> {
|
2018-08-23 14:22:57 -05:00
|
|
|
let attrs = tcx.get_attrs(def_id);
|
2019-05-13 14:55:58 -05:00
|
|
|
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
|
2019-04-12 05:32:14 -05:00
|
|
|
Some(name) => name.as_str(),
|
|
|
|
None => tcx.item_name(def_id).as_str(),
|
2018-08-23 14:22:57 -05:00
|
|
|
};
|
|
|
|
|
2019-04-12 05:32:14 -05:00
|
|
|
let alloc = match link_name.get() {
|
2018-08-23 14:22:57 -05:00
|
|
|
"__cxa_thread_atexit_impl" => {
|
2019-02-15 19:29:38 -06:00
|
|
|
// This should be all-zero, pointer-sized.
|
2018-11-15 06:29:55 -06:00
|
|
|
let size = tcx.data_layout.pointer_size;
|
|
|
|
let data = vec![0; size.bytes() as usize];
|
2019-04-15 08:36:09 -05:00
|
|
|
let extra = Stacks::new(size, Tag::default(), Rc::clone(memory_extra));
|
2019-02-27 04:38:43 -06:00
|
|
|
Allocation::from_bytes(&data, tcx.data_layout.pointer_align.abi, extra)
|
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-10-16 11:01:50 -05:00
|
|
|
#[inline(always)]
|
2019-03-27 03:58:11 -05:00
|
|
|
fn before_terminator(_ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>) -> EvalResult<'tcx>
|
2018-09-20 05:24:55 -05:00
|
|
|
{
|
2019-02-15 19:29:38 -06:00
|
|
|
// We are not interested in detecting loops.
|
2018-09-20 05:24:55 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-10-05 11:08:50 -05:00
|
|
|
|
2018-11-15 06:29:55 -06:00
|
|
|
fn adjust_static_allocation<'b>(
|
|
|
|
alloc: &'b Allocation,
|
|
|
|
memory_extra: &Self::MemoryExtra,
|
2019-04-15 08:36:09 -05:00
|
|
|
) -> Cow<'b, Allocation<Tag, Self::AllocExtra>> {
|
|
|
|
let extra = Stacks::new(
|
2018-11-15 06:29:55 -06:00
|
|
|
Size::from_bytes(alloc.bytes.len() as u64),
|
2019-04-15 08:36:09 -05:00
|
|
|
Tag::default(),
|
|
|
|
Rc::clone(memory_extra),
|
2018-11-15 06:29:55 -06:00
|
|
|
);
|
2019-04-15 08:36:09 -05:00
|
|
|
let alloc: Allocation<Tag, Self::AllocExtra> = Allocation {
|
2018-10-16 04:21:38 -05:00
|
|
|
bytes: alloc.bytes.clone(),
|
|
|
|
relocations: Relocations::from_presorted(
|
|
|
|
alloc.relocations.iter()
|
2019-04-15 08:36:09 -05:00
|
|
|
.map(|&(offset, ((), alloc))| (offset, (Tag::default(), alloc)))
|
2018-10-16 04:21:38 -05:00
|
|
|
.collect()
|
|
|
|
),
|
|
|
|
undef_mask: alloc.undef_mask.clone(),
|
|
|
|
align: alloc.align,
|
|
|
|
mutability: alloc.mutability,
|
2018-11-15 06:29:55 -06:00
|
|
|
extra,
|
2018-10-16 04:21:38 -05:00
|
|
|
};
|
2018-10-05 11:08:50 -05:00
|
|
|
Cow::Owned(alloc)
|
|
|
|
}
|
2018-10-16 11:01:50 -05:00
|
|
|
|
2019-04-15 08:36:09 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn new_allocation(
|
|
|
|
size: Size,
|
|
|
|
extra: &Self::MemoryExtra,
|
|
|
|
kind: MemoryKind<MiriMemoryKind>,
|
|
|
|
) -> (Self::AllocExtra, Self::PointerTag) {
|
|
|
|
Stacks::new_allocation(size, extra, kind)
|
2018-10-16 11:01:50 -05:00
|
|
|
}
|
2018-10-22 11:01:32 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
2019-04-15 08:36:09 -05:00
|
|
|
fn tag_dereference(
|
|
|
|
_ecx: &InterpretCx<'a, 'mir, 'tcx, Self>,
|
|
|
|
place: MPlaceTy<'tcx, Tag>,
|
|
|
|
_mutability: Option<hir::Mutability>,
|
|
|
|
) -> EvalResult<'tcx, Scalar<Tag>> {
|
|
|
|
// Nothing happens.
|
|
|
|
Ok(place.ptr)
|
2018-10-22 11:01:32 -05:00
|
|
|
}
|
2018-10-24 10:17:44 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn retag(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
|
2018-12-12 04:11:20 -06:00
|
|
|
kind: mir::RetagKind,
|
2019-04-15 08:36:09 -05:00
|
|
|
place: PlaceTy<'tcx, Tag>,
|
2018-10-24 10:17:44 -05:00
|
|
|
) -> EvalResult<'tcx> {
|
2018-10-26 04:31:20 -05:00
|
|
|
if !ecx.tcx.sess.opts.debugging_opts.mir_emit_retag || !Self::enforce_validity(ecx) {
|
2018-11-07 07:56:25 -06:00
|
|
|
// No tracking, or no retagging. The latter is possible because a dependency of ours
|
|
|
|
// might be called with different flags than we are, so there are `Retag`
|
|
|
|
// statements but we do not want to execute them.
|
2018-10-26 04:31:20 -05:00
|
|
|
// Also, honor the whitelist in `enforce_validity` because otherwise we might retag
|
|
|
|
// uninitialized data.
|
2018-11-07 07:56:25 -06:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2018-12-12 04:11:20 -06:00
|
|
|
ecx.retag(kind, place)
|
2018-10-24 10:17:44 -05:00
|
|
|
}
|
|
|
|
}
|
2018-11-15 11:15:05 -06:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn stack_push(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
|
2018-11-15 11:15:05 -06:00
|
|
|
) -> EvalResult<'tcx, stacked_borrows::CallId> {
|
|
|
|
Ok(ecx.memory().extra.borrow_mut().new_call())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn stack_pop(
|
2019-03-27 03:58:11 -05:00
|
|
|
ecx: &mut InterpretCx<'a, 'mir, 'tcx, Self>,
|
2018-11-15 11:15:05 -06:00
|
|
|
extra: stacked_borrows::CallId,
|
|
|
|
) -> EvalResult<'tcx> {
|
|
|
|
Ok(ecx.memory().extra.borrow_mut().end_call(extra))
|
|
|
|
}
|
2017-07-21 10:25:30 -05:00
|
|
|
}
|