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-04-22 03:34:14 -05:00
|
|
|
use rustc_driver::{driver, CompilerCalls};
|
2016-06-10 06:01:51 -05:00
|
|
|
use rustc::ty::{TyCtxt, subst};
|
|
|
|
use rustc::mir::mir_map::MirMap;
|
2016-06-13 07:27:05 -05:00
|
|
|
use rustc::mir::repr::Mir;
|
2016-06-10 06:01:51 -05:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-06-13 07:27:05 -05:00
|
|
|
use rustc::hir::{map, ItemFn, Item};
|
|
|
|
use syntax::codemap::Span;
|
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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
let (span, mir, def_id) = get_main(tcx, mir_map);
|
|
|
|
println!("found `main` function at: {:?}", span);
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 15:50:58 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 07:27:05 -05:00
|
|
|
fn get_main<'a, 'b, 'tcx: 'b>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mir_map: &'b MirMap<'tcx>) -> (Span, &'b Mir<'tcx>, DefId) {
|
2016-06-10 06:01:51 -05:00
|
|
|
for (&id, mir) in &mir_map.map {
|
2016-06-13 07:27:05 -05:00
|
|
|
if let map::Node::NodeItem(&Item { name, span, ref node, .. }) = tcx.map.get(id) {
|
|
|
|
if let ItemFn(..) = *node {
|
|
|
|
if name.as_str() == "main" {
|
|
|
|
return (span, mir, tcx.map.local_def_id(id));
|
2016-06-10 09:32:39 -05:00
|
|
|
}
|
2016-06-10 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 07:27:05 -05:00
|
|
|
panic!("no main function found");
|
2016-06-10 06:01:51 -05:00
|
|
|
}
|
|
|
|
|
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-11 13:38:28 -05:00
|
|
|
let block = &frame.mir.basic_blocks()[frame.next_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();
|
|
|
|
}
|
|
|
|
|
2015-11-12 15:50:58 -06:00
|
|
|
fn main() {
|
2016-05-30 11:09:52 -05:00
|
|
|
init_logger();
|
2015-11-12 15:50:58 -06:00
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls);
|
|
|
|
}
|
2016-05-30 11:09:52 -05:00
|
|
|
|
|
|
|
fn init_logger() {
|
2016-06-01 11:33:29 -05:00
|
|
|
const NSPACES: usize = 40;
|
2016-05-30 11:09:52 -05:00
|
|
|
let format = |record: &log::LogRecord| {
|
|
|
|
// prepend spaces to indent the final string
|
|
|
|
let indentation = log_settings::settings().indentation;
|
2016-06-01 11:42:57 -05:00
|
|
|
format!("{lvl}:{module}{depth:2}{indent:<indentation$} {text}",
|
2016-06-01 11:33:29 -05:00
|
|
|
lvl = record.level(),
|
|
|
|
module = record.location().module_path(),
|
2016-06-01 11:42:57 -05:00
|
|
|
depth = indentation / NSPACES,
|
|
|
|
indentation = indentation % NSPACES,
|
2016-06-01 11:33:29 -05:00
|
|
|
indent = "",
|
|
|
|
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();
|
|
|
|
}
|