2016-06-11 14:10:25 -05:00
|
|
|
#![feature(rustc_private)]
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2016-05-09 16:30:47 -05:00
|
|
|
extern crate getopts;
|
2015-11-21 21:20:06 -06:00
|
|
|
extern crate miri;
|
2015-11-12 15:50:58 -06:00
|
|
|
extern crate rustc;
|
|
|
|
extern crate rustc_driver;
|
2016-05-30 11:09:52 -05:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate log_settings;
|
2016-06-10 06:01:51 -05:00
|
|
|
extern crate syntax;
|
|
|
|
#[macro_use] extern crate log;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2016-06-10 06:01:51 -05:00
|
|
|
use miri::{
|
2016-06-10 09:20:17 -05:00
|
|
|
EvalContext,
|
2016-06-10 06:01:51 -05:00
|
|
|
CachedMir,
|
|
|
|
step,
|
|
|
|
EvalError,
|
|
|
|
Frame,
|
|
|
|
};
|
2015-11-12 15:50:58 -06:00
|
|
|
use rustc::session::Session;
|
2016-06-15 06:18:35 -05:00
|
|
|
use rustc_driver::{driver, CompilerCalls, Compilation};
|
2016-06-10 06:01:51 -05:00
|
|
|
use rustc::ty::{TyCtxt, subst};
|
|
|
|
use rustc::hir::def_id::DefId;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
|
|
|
struct MiriCompilerCalls;
|
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
|
2016-05-09 16:30:47 -05:00
|
|
|
fn build_controller(
|
|
|
|
&mut self,
|
|
|
|
_: &Session,
|
|
|
|
_: &getopts::Matches
|
|
|
|
) -> driver::CompileController<'a> {
|
2015-11-12 15:50:58 -06:00
|
|
|
let mut control = driver::CompileController::basic();
|
|
|
|
|
2016-06-15 06:18:35 -05:00
|
|
|
control.after_analysis.stop = Compilation::Stop;
|
2015-11-12 15:50:58 -06:00
|
|
|
control.after_analysis.callback = Box::new(|state| {
|
2016-03-21 03:54:20 -05:00
|
|
|
state.session.abort_if_errors();
|
2016-06-13 07:27:05 -05:00
|
|
|
|
|
|
|
let tcx = state.tcx.unwrap();
|
|
|
|
let mir_map = state.mir_map.unwrap();
|
2016-06-14 04:52:45 -05:00
|
|
|
|
|
|
|
let (node_id, span) = state.session.entry_fn.borrow().expect("no main or start function found");
|
2016-06-15 06:00:51 -05:00
|
|
|
debug!("found `main` function at: {:?}", span);
|
2016-06-13 07:27:05 -05:00
|
|
|
|
2016-06-14 04:52:45 -05:00
|
|
|
let mir = mir_map.map.get(&node_id).expect("no mir for main function");
|
|
|
|
let def_id = tcx.map.local_def_id(node_id);
|
2016-06-13 07:27:05 -05:00
|
|
|
let mut ecx = EvalContext::new(tcx, mir_map);
|
|
|
|
let substs = tcx.mk_substs(subst::Substs::empty());
|
|
|
|
let return_ptr = ecx.alloc_ret_ptr(mir.return_ty, substs).expect("main function should not be diverging");
|
|
|
|
|
|
|
|
ecx.push_stack_frame(def_id, mir.span, CachedMir::Ref(mir), substs, Some(return_ptr));
|
|
|
|
|
2016-06-15 05:55:04 -05:00
|
|
|
if mir.arg_decls.len() == 2 {
|
|
|
|
// start function
|
|
|
|
let ptr_size = ecx.memory().pointer_size;
|
|
|
|
let nargs = ecx.memory_mut().allocate(ptr_size);
|
|
|
|
ecx.memory_mut().write_usize(nargs, 0).unwrap();
|
|
|
|
let args = ecx.memory_mut().allocate(ptr_size);
|
|
|
|
ecx.memory_mut().write_usize(args, 0).unwrap();
|
|
|
|
ecx.frame_mut().locals[0] = nargs;
|
|
|
|
ecx.frame_mut().locals[1] = args;
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:27:05 -05:00
|
|
|
loop {
|
|
|
|
match step(&mut ecx) {
|
|
|
|
Ok(true) => {}
|
|
|
|
Ok(false) => break,
|
|
|
|
// FIXME: diverging functions can end up here in some future miri
|
|
|
|
Err(e) => {
|
|
|
|
report(tcx, &ecx, e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 06:18:35 -05:00
|
|
|
state.session.abort_if_errors();
|
2015-11-12 15:50:58 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 09:56:04 -05:00
|
|
|
fn report(tcx: TyCtxt, ecx: &EvalContext, e: EvalError) {
|
|
|
|
let frame = ecx.stack().last().expect("stackframe was empty");
|
2016-06-14 21:13:59 -05:00
|
|
|
let block = &frame.mir.basic_blocks()[frame.block];
|
2016-06-10 06:01:51 -05:00
|
|
|
let span = if frame.stmt < block.statements.len() {
|
2016-06-11 13:38:28 -05:00
|
|
|
block.statements[frame.stmt].source_info.span
|
2016-06-10 06:01:51 -05:00
|
|
|
} else {
|
2016-06-11 13:38:28 -05:00
|
|
|
block.terminator().source_info.span
|
2016-06-10 06:01:51 -05:00
|
|
|
};
|
|
|
|
let mut err = tcx.sess.struct_span_err(span, &e.to_string());
|
2016-06-10 09:56:04 -05:00
|
|
|
for &Frame { def_id, substs, span, .. } in ecx.stack().iter().rev() {
|
2016-06-10 06:01:51 -05:00
|
|
|
// FIXME(solson): Find a way to do this without this Display impl hack.
|
|
|
|
use rustc::util::ppaux;
|
|
|
|
use std::fmt;
|
|
|
|
struct Instance<'tcx>(DefId, &'tcx subst::Substs<'tcx>);
|
|
|
|
impl<'tcx> fmt::Display for Instance<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
ppaux::parameterized(f, self.1, self.0, ppaux::Ns::Value, &[],
|
2016-06-11 13:38:28 -05:00
|
|
|
|tcx| Some(tcx.lookup_item_type(self.0).generics))
|
2016-06-10 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
2016-05-30 11:09:52 -05:00
|
|
|
fn init_logger() {
|
2016-06-17 20:55:24 -05:00
|
|
|
const MAX_INDENT: usize = 40;
|
|
|
|
|
2016-05-30 11:09:52 -05:00
|
|
|
let format = |record: &log::LogRecord| {
|
2016-06-17 20:48:45 -05:00
|
|
|
if record.level() == log::LogLevel::Trace {
|
|
|
|
// prepend spaces to indent the final string
|
|
|
|
let indentation = log_settings::settings().indentation;
|
|
|
|
format!("{lvl}:{module}{depth:2}{indent:<indentation$} {text}",
|
|
|
|
lvl = record.level(),
|
|
|
|
module = record.location().module_path(),
|
2016-06-17 20:55:24 -05:00
|
|
|
depth = indentation / MAX_INDENT,
|
|
|
|
indentation = indentation % MAX_INDENT,
|
2016-06-17 20:48:45 -05:00
|
|
|
indent = "",
|
|
|
|
text = record.args())
|
|
|
|
} else {
|
|
|
|
format!("{lvl}:{module}: {text}",
|
|
|
|
lvl = record.level(),
|
|
|
|
module = record.location().module_path(),
|
|
|
|
text = record.args())
|
|
|
|
}
|
2016-05-30 11:09:52 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut builder = env_logger::LogBuilder::new();
|
|
|
|
builder.format(format).filter(None, log::LogLevelFilter::Info);
|
|
|
|
|
2016-06-01 10:32:57 -05:00
|
|
|
if std::env::var("MIRI_LOG").is_ok() {
|
|
|
|
builder.parse(&std::env::var("MIRI_LOG").unwrap());
|
2016-05-30 11:09:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
builder.init().unwrap();
|
|
|
|
}
|
2016-06-14 20:30:59 -05:00
|
|
|
|
|
|
|
fn find_sysroot() -> String {
|
|
|
|
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
|
|
|
|
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
|
|
|
|
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
|
|
|
|
match (home, toolchain) {
|
|
|
|
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
|
|
|
|
_ => option_env!("RUST_SYSROOT")
|
|
|
|
.expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
|
|
|
|
.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
init_logger();
|
|
|
|
let mut args: Vec<String> = std::env::args().collect();
|
|
|
|
|
|
|
|
let sysroot_flag = String::from("--sysroot");
|
|
|
|
if !args.contains(&sysroot_flag) {
|
|
|
|
args.push(sysroot_flag);
|
|
|
|
args.push(find_sysroot());
|
|
|
|
}
|
|
|
|
|
|
|
|
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
|
|
|
|
}
|