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
|
|
|
|
|
|
|
use rustc::session::Session;
|
2016-12-15 03:16:06 -06:00
|
|
|
use rustc_driver::{CompilerCalls, Compilation};
|
2016-11-26 19:36:31 -06:00
|
|
|
use rustc_driver::driver::{CompileState, CompileController};
|
2016-09-06 09:04:51 -05:00
|
|
|
use syntax::ast::{MetaItemKind, NestedMetaItemKind};
|
2015-11-12 15:50:58 -06:00
|
|
|
|
|
|
|
struct MiriCompilerCalls;
|
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
|
2016-11-26 19:36:31 -06:00
|
|
|
fn build_controller(&mut self, _: &Session, _: &getopts::Matches) -> CompileController<'a> {
|
|
|
|
let mut control = CompileController::basic();
|
|
|
|
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
|
|
|
|
control.after_analysis.callback = Box::new(after_analysis);
|
2016-12-15 03:16:06 -06:00
|
|
|
control.after_analysis.stop = Compilation::Stop;
|
2016-11-26 19:36:31 -06:00
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn after_hir_lowering(state: &mut CompileState) {
|
|
|
|
let attr = (String::from("miri"), syntax::feature_gate::AttributeType::Whitelisted);
|
|
|
|
state.session.plugin_attributes.borrow_mut().push(attr);
|
|
|
|
}
|
2016-06-13 07:27:05 -05:00
|
|
|
|
2016-11-26 19:36:31 -06:00
|
|
|
fn after_analysis(state: &mut CompileState) {
|
|
|
|
state.session.abort_if_errors();
|
2016-06-30 22:33:24 -05:00
|
|
|
|
2016-11-26 19:36:31 -06:00
|
|
|
let tcx = state.tcx.unwrap();
|
2016-12-15 03:16:06 -06:00
|
|
|
let (entry_node_id, _) = state.session.entry_fn.borrow()
|
|
|
|
.expect("no main or start function found");
|
|
|
|
let entry_def_id = tcx.map.local_def_id(entry_node_id);
|
|
|
|
let limits = resource_limits_from_attributes(state);
|
|
|
|
miri::run_mir_passes(tcx);
|
|
|
|
miri::eval_main(tcx, entry_def_id, limits);
|
|
|
|
|
|
|
|
state.session.abort_if_errors();
|
2016-11-26 19:54:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resource_limits_from_attributes(state: &CompileState) -> miri::ResourceLimits {
|
|
|
|
let mut limits = miri::ResourceLimits::default();
|
2016-11-26 19:36:31 -06:00
|
|
|
let krate = state.hir_crate.as_ref().unwrap();
|
2016-11-26 19:54:19 -06:00
|
|
|
let err_msg = "miri attributes need to be in the form `miri(key = value)`";
|
2016-11-26 19:36:31 -06:00
|
|
|
let extract_int = |lit: &syntax::ast::Lit| -> u64 {
|
|
|
|
match lit.node {
|
|
|
|
syntax::ast::LitKind::Int(i, _) => i,
|
|
|
|
_ => state.session.span_fatal(lit.span, "expected an integer literal"),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for attr in krate.attrs.iter().filter(|a| a.name() == "miri") {
|
|
|
|
if let MetaItemKind::List(ref items) = attr.value.node {
|
|
|
|
for item in items {
|
|
|
|
if let NestedMetaItemKind::MetaItem(ref inner) = item.node {
|
|
|
|
if let MetaItemKind::NameValue(ref value) = inner.node {
|
|
|
|
match &inner.name().as_str()[..] {
|
2016-11-26 19:54:19 -06:00
|
|
|
"memory_size" => limits.memory_size = extract_int(value),
|
|
|
|
"step_limit" => limits.step_limit = extract_int(value),
|
|
|
|
"stack_limit" => limits.stack_limit = extract_int(value) as usize,
|
2016-11-26 19:36:31 -06:00
|
|
|
_ => state.session.span_err(item.span, "unknown miri attribute"),
|
2016-07-05 06:04:46 -05:00
|
|
|
}
|
2016-11-26 19:36:31 -06:00
|
|
|
} else {
|
|
|
|
state.session.span_err(inner.span, err_msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
state.session.span_err(item.span, err_msg);
|
2016-07-05 06:04:46 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-26 19:36:31 -06:00
|
|
|
} else {
|
|
|
|
state.session.span_err(attr.span, err_msg);
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 19:54:19 -06:00
|
|
|
limits
|
2015-11-12 15:50:58 -06:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-10-01 08:33:07 -05:00
|
|
|
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls, None, None);
|
2016-06-14 20:30:59 -05:00
|
|
|
}
|