2017-01-12 01:28:42 -06:00
|
|
|
#![feature(rustc_private, i128_type)]
|
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;
|
2017-01-24 06:28:36 -06:00
|
|
|
extern crate rustc_errors;
|
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;
|
2017-01-28 08:27:20 -06:00
|
|
|
extern crate log;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
|
|
|
use rustc::session::Session;
|
2017-01-24 06:28:36 -06:00
|
|
|
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
|
2016-11-26 19:36:31 -06:00
|
|
|
use rustc_driver::driver::{CompileState, CompileController};
|
2017-01-24 06:28:36 -06:00
|
|
|
use rustc::session::config::{self, Input, ErrorOutputType};
|
2017-01-24 08:33:51 -06:00
|
|
|
use rustc::hir::{self, itemlikevisit};
|
|
|
|
use rustc::ty::TyCtxt;
|
2017-01-24 06:28:36 -06:00
|
|
|
use syntax::ast::{MetaItemKind, NestedMetaItemKind, self};
|
|
|
|
use std::path::PathBuf;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2017-01-24 06:28:36 -06:00
|
|
|
struct MiriCompilerCalls(RustcDefaultCalls);
|
2015-11-12 15:50:58 -06:00
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
|
2017-01-24 06:28:36 -06:00
|
|
|
fn early_callback(
|
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
descriptions: &rustc_errors::registry::Registry,
|
|
|
|
output: ErrorOutputType
|
|
|
|
) -> Compilation {
|
|
|
|
self.0.early_callback(matches, sopts, cfg, descriptions, output)
|
|
|
|
}
|
|
|
|
fn no_input(
|
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
odir: &Option<PathBuf>,
|
|
|
|
ofile: &Option<PathBuf>,
|
|
|
|
descriptions: &rustc_errors::registry::Registry
|
|
|
|
) -> Option<(Input, Option<PathBuf>)> {
|
|
|
|
self.0.no_input(matches, sopts, cfg, odir, ofile, descriptions)
|
|
|
|
}
|
|
|
|
fn late_callback(
|
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sess: &Session,
|
|
|
|
input: &Input,
|
|
|
|
odir: &Option<PathBuf>,
|
|
|
|
ofile: &Option<PathBuf>
|
|
|
|
) -> Compilation {
|
|
|
|
self.0.late_callback(matches, sess, input, odir, ofile)
|
|
|
|
}
|
|
|
|
fn build_controller(&mut self, sess: &Session, matches: &getopts::Matches) -> CompileController<'a> {
|
|
|
|
let mut control = self.0.build_controller(sess, matches);
|
2016-11-26 19:36:31 -06:00
|
|
|
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
|
|
|
|
control.after_analysis.callback = Box::new(after_analysis);
|
2016-12-19 08:46:03 -06:00
|
|
|
if std::env::var("MIRI_HOST_TARGET") != Ok("yes".to_owned()) {
|
|
|
|
// only fully compile targets on the host
|
|
|
|
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
|
|
|
|
2017-01-24 08:33:51 -06:00
|
|
|
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>) {
|
2016-11-26 19:36:31 -06:00
|
|
|
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();
|
2017-01-24 08:33:51 -06:00
|
|
|
let limits = resource_limits_from_attributes(state);
|
2016-12-16 19:10:16 -06:00
|
|
|
|
2017-01-24 08:33:51 -06:00
|
|
|
if std::env::args().any(|arg| arg == "--test") {
|
|
|
|
struct Visitor<'a, 'tcx: 'a>(miri::ResourceLimits, TyCtxt<'a, 'tcx, 'tcx>, &'a CompileState<'a, 'tcx>);
|
|
|
|
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item) {
|
|
|
|
if let hir::Item_::ItemFn(_, _, _, _, _, body_id) = i.node {
|
2017-03-21 10:04:49 -05:00
|
|
|
if i.attrs.iter().any(|attr| attr.name().map_or(false, |n| n == "test")) {
|
2017-01-29 01:21:24 -06:00
|
|
|
let did = self.1.hir.body_owner_def_id(body_id);
|
|
|
|
println!("running test: {}", self.1.hir.def_path(did).to_string(self.1));
|
2017-05-26 19:36:16 -05:00
|
|
|
miri::eval_main(self.1, did, None, self.0);
|
2017-01-24 08:33:51 -06:00
|
|
|
self.2.session.abort_if_errors();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
|
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
|
|
|
|
}
|
|
|
|
state.hir_crate.unwrap().visit_all_item_likes(&mut Visitor(limits, tcx, state));
|
2017-05-05 03:34:38 -05:00
|
|
|
} else if let Some((entry_node_id, _)) = *state.session.entry_fn.borrow() {
|
|
|
|
let entry_def_id = tcx.hir.local_def_id(entry_node_id);
|
2017-05-30 12:24:37 -05:00
|
|
|
let start_wrapper = tcx.lang_items.start_fn().and_then(|start_fn|
|
|
|
|
if tcx.is_mir_available(start_fn) { Some(start_fn) } else { None });
|
2017-05-26 19:36:16 -05:00
|
|
|
miri::eval_main(tcx, entry_def_id, start_wrapper, limits);
|
2017-01-24 08:33:51 -06:00
|
|
|
|
2017-05-05 03:34:38 -05:00
|
|
|
state.session.abort_if_errors();
|
|
|
|
} else {
|
|
|
|
println!("no main function found, assuming auxiliary build");
|
2016-12-16 19:10:16 -06:00
|
|
|
}
|
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)`";
|
2017-01-12 01:28:42 -06:00
|
|
|
let extract_int = |lit: &syntax::ast::Lit| -> u128 {
|
2016-11-26 19:36:31 -06:00
|
|
|
match lit.node {
|
|
|
|
syntax::ast::LitKind::Int(i, _) => i,
|
|
|
|
_ => state.session.span_fatal(lit.span, "expected an integer literal"),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-21 10:04:49 -05:00
|
|
|
for attr in krate.attrs.iter().filter(|a| a.name().map_or(false, |n| n == "miri")) {
|
|
|
|
if let Some(items) = attr.meta_item_list() {
|
2016-11-26 19:36:31 -06:00
|
|
|
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()[..] {
|
2017-01-12 01:28:42 -06:00
|
|
|
"memory_size" => limits.memory_size = extract_int(value) as u64,
|
|
|
|
"step_limit" => limits.step_limit = extract_int(value) as u64,
|
2016-11-26 19:54:19 -06:00
|
|
|
"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() {
|
|
|
|
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;
|
2017-02-07 02:45:22 -06:00
|
|
|
format!(
|
|
|
|
"{lvl}:{module}:{indent:<indentation$} {text}",
|
2016-06-17 20:48:45 -05:00
|
|
|
lvl = record.level(),
|
|
|
|
module = record.location().module_path(),
|
2017-02-07 02:45:22 -06:00
|
|
|
indentation = indentation,
|
2016-06-17 20:48:45 -05:00
|
|
|
indent = "",
|
2017-02-07 02:45:22 -06:00
|
|
|
text = record.args(),
|
|
|
|
)
|
2016-06-17 20:48:45 -05:00
|
|
|
} else {
|
2017-02-07 02:45:22 -06:00
|
|
|
format!(
|
|
|
|
"{lvl}:{module}: {text}",
|
2016-06-17 20:48:45 -05:00
|
|
|
lvl = record.level(),
|
|
|
|
module = record.location().module_path(),
|
2017-02-07 02:45:22 -06:00
|
|
|
text = record.args(),
|
|
|
|
)
|
2016-06-17 20:48:45 -05:00
|
|
|
}
|
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 {
|
2017-05-30 16:09:40 -05:00
|
|
|
if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
|
|
|
|
return sysroot;
|
|
|
|
}
|
|
|
|
|
2016-06-14 20:30:59 -05:00
|
|
|
// 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());
|
|
|
|
}
|
2017-07-20 09:05:14 -05:00
|
|
|
|
2016-12-19 08:46:03 -06:00
|
|
|
// for auxilary builds in unit tests
|
2016-12-16 19:10:16 -06:00
|
|
|
args.push("-Zalways-encode-mir".to_owned());
|
2017-07-26 16:41:08 -05:00
|
|
|
args.push("-Zmir-emit-validate".to_owned());
|
2016-06-14 20:30:59 -05:00
|
|
|
|
2017-01-24 06:28:36 -06:00
|
|
|
rustc_driver::run_compiler(&args, &mut MiriCompilerCalls(RustcDefaultCalls), None, None);
|
2016-06-14 20:30:59 -05:00
|
|
|
}
|