2016-04-22 07:38:46 -05:00
|
|
|
#![feature(rustc_private, custom_attribute)]
|
|
|
|
#![allow(unused_attributes)]
|
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;
|
|
|
|
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();
|
|
|
|
|
|
|
|
control.after_analysis.callback = Box::new(|state| {
|
2016-03-21 03:54:20 -05:00
|
|
|
state.session.abort_if_errors();
|
2016-06-10 06:01:51 -05:00
|
|
|
interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
|
2015-11-12 15:50:58 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 06:01:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
fn interpret_start_points<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir_map: &MirMap<'tcx>,
|
|
|
|
) {
|
|
|
|
let initial_indentation = ::log_settings::settings().indentation;
|
|
|
|
for (&id, mir) in &mir_map.map {
|
|
|
|
for attr in tcx.map.attrs(id) {
|
|
|
|
use syntax::attr::AttrMetaMethods;
|
|
|
|
if attr.check_name("miri_run") {
|
|
|
|
let item = tcx.map.expect_item(id);
|
|
|
|
|
|
|
|
::log_settings::settings().indentation = initial_indentation;
|
|
|
|
|
|
|
|
debug!("Interpreting: {}", item.name);
|
|
|
|
|
2016-06-10 09:20:17 -05:00
|
|
|
let mut gecx = EvalContext::new(tcx, mir_map);
|
2016-06-10 06:01:51 -05:00
|
|
|
let substs = tcx.mk_substs(subst::Substs::empty());
|
|
|
|
let return_ptr = gecx.alloc_ret_ptr(mir.return_ty, substs);
|
|
|
|
|
|
|
|
gecx.push_stack_frame(tcx.map.local_def_id(id), mir.span, CachedMir::Ref(mir), substs, return_ptr);
|
|
|
|
|
2016-06-10 09:32:39 -05:00
|
|
|
loop {
|
|
|
|
match (step(&mut gecx), return_ptr) {
|
|
|
|
(Ok(true), _) => {},
|
|
|
|
(Ok(false), Some(ptr)) => if log_enabled!(::log::LogLevel::Debug) {
|
|
|
|
gecx.memory().dump(ptr.alloc_id);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
(Ok(false), None) => {
|
|
|
|
warn!("diverging function returned");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
// FIXME: diverging functions can end up here in some future miri
|
|
|
|
(Err(e), _) => {
|
|
|
|
report(tcx, &gecx, e);
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2016-06-10 06:01:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 09:20:17 -05:00
|
|
|
fn report(tcx: TyCtxt, gecx: &EvalContext, e: EvalError) {
|
2016-06-10 06:01:51 -05:00
|
|
|
let frame = gecx.stack().last().expect("stackframe was empty");
|
|
|
|
let block = frame.mir.basic_block_data(frame.next_block);
|
|
|
|
let span = if frame.stmt < block.statements.len() {
|
|
|
|
block.statements[frame.stmt].span
|
|
|
|
} else {
|
|
|
|
block.terminator().span
|
|
|
|
};
|
|
|
|
let mut err = tcx.sess.struct_span_err(span, &e.to_string());
|
|
|
|
for &Frame { def_id, substs, span, .. } in gecx.stack().iter().rev() {
|
|
|
|
// 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, &[],
|
|
|
|
|tcx| tcx.lookup_item_type(self.0).generics)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.span_note(span, &format!("inside call to {}", Instance(def_id, substs)));
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
2016-04-22 07:38:46 -05:00
|
|
|
#[miri_run]
|
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
|
|
|
|
|
|
|
#[miri_run]
|
|
|
|
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();
|
|
|
|
}
|