2018-03-23 12:18:33 +01:00
|
|
|
#![feature(rustc_private)]
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2016-05-09 15:30:47 -06: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;
|
2018-08-07 15:22:11 +02:00
|
|
|
extern crate rustc_metadata;
|
2015-11-12 15:50:58 -06:00
|
|
|
extern crate rustc_driver;
|
2017-01-24 13:28:36 +01:00
|
|
|
extern crate rustc_errors;
|
2018-05-19 12:14:13 +02:00
|
|
|
extern crate rustc_codegen_utils;
|
2016-05-30 18:09:52 +02:00
|
|
|
extern crate env_logger;
|
|
|
|
extern crate log_settings;
|
2016-06-10 13:01:51 +02:00
|
|
|
extern crate syntax;
|
2017-01-28 15:27:20 +01:00
|
|
|
extern crate log;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2018-10-31 10:09:55 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::env;
|
|
|
|
|
2015-11-12 15:50:58 -06:00
|
|
|
use rustc::session::Session;
|
2018-08-07 15:22:11 +02:00
|
|
|
use rustc_metadata::cstore::CStore;
|
2017-01-24 13:28:36 +01:00
|
|
|
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
|
2016-11-26 17:36:31 -08:00
|
|
|
use rustc_driver::driver::{CompileState, CompileController};
|
2017-01-24 13:28:36 +01:00
|
|
|
use rustc::session::config::{self, Input, ErrorOutputType};
|
2017-01-24 15:33:51 +01:00
|
|
|
use rustc::hir::{self, itemlikevisit};
|
|
|
|
use rustc::ty::TyCtxt;
|
2018-05-19 12:14:13 +02:00
|
|
|
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
2018-01-14 18:59:13 +01:00
|
|
|
use syntax::ast;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2017-08-28 17:32:21 +02:00
|
|
|
struct MiriCompilerCalls {
|
2018-06-10 11:23:56 +02:00
|
|
|
default: Box<RustcDefaultCalls>,
|
2018-10-11 11:24:22 +02:00
|
|
|
|
|
|
|
/// Whether to enforce the validity invariant.
|
|
|
|
validate: bool,
|
2017-08-28 17:32:21 +02:00
|
|
|
}
|
2015-11-12 15:50:58 -06:00
|
|
|
|
|
|
|
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
|
2017-01-24 13:28:36 +01:00
|
|
|
fn early_callback(
|
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
descriptions: &rustc_errors::registry::Registry,
|
2017-08-10 08:48:38 -07:00
|
|
|
output: ErrorOutputType,
|
2017-01-24 13:28:36 +01:00
|
|
|
) -> Compilation {
|
2017-08-28 17:32:21 +02:00
|
|
|
self.default.early_callback(
|
2017-08-10 08:48:38 -07:00
|
|
|
matches,
|
|
|
|
sopts,
|
|
|
|
cfg,
|
|
|
|
descriptions,
|
|
|
|
output,
|
|
|
|
)
|
2017-01-24 13:28:36 +01:00
|
|
|
}
|
|
|
|
fn no_input(
|
|
|
|
&mut self,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
sopts: &config::Options,
|
|
|
|
cfg: &ast::CrateConfig,
|
|
|
|
odir: &Option<PathBuf>,
|
|
|
|
ofile: &Option<PathBuf>,
|
2017-08-10 08:48:38 -07:00
|
|
|
descriptions: &rustc_errors::registry::Registry,
|
2017-01-24 13:28:36 +01:00
|
|
|
) -> Option<(Input, Option<PathBuf>)> {
|
2017-08-28 17:32:21 +02:00
|
|
|
self.default.no_input(
|
2017-08-10 08:48:38 -07:00
|
|
|
matches,
|
|
|
|
sopts,
|
|
|
|
cfg,
|
|
|
|
odir,
|
|
|
|
ofile,
|
|
|
|
descriptions,
|
|
|
|
)
|
2017-01-24 13:28:36 +01:00
|
|
|
}
|
|
|
|
fn late_callback(
|
|
|
|
&mut self,
|
2018-05-19 14:09:29 +02:00
|
|
|
codegen_backend: &CodegenBackend,
|
2017-01-24 13:28:36 +01:00
|
|
|
matches: &getopts::Matches,
|
|
|
|
sess: &Session,
|
2018-08-07 15:22:11 +02:00
|
|
|
cstore: &CStore,
|
2017-01-24 13:28:36 +01:00
|
|
|
input: &Input,
|
|
|
|
odir: &Option<PathBuf>,
|
2017-08-10 08:48:38 -07:00
|
|
|
ofile: &Option<PathBuf>,
|
2017-01-24 13:28:36 +01:00
|
|
|
) -> Compilation {
|
2018-05-19 14:09:29 +02:00
|
|
|
self.default.late_callback(codegen_backend, matches, sess, cstore, input, odir, ofile)
|
2017-01-24 13:28:36 +01:00
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
fn build_controller(
|
2018-06-10 11:23:56 +02:00
|
|
|
self: Box<Self>,
|
2017-08-10 08:48:38 -07:00
|
|
|
sess: &Session,
|
|
|
|
matches: &getopts::Matches,
|
|
|
|
) -> CompileController<'a> {
|
2018-06-10 11:23:56 +02:00
|
|
|
let this = *self;
|
|
|
|
let mut control = this.default.build_controller(sess, matches);
|
2016-11-26 17:36:31 -08:00
|
|
|
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
|
2018-10-11 11:24:22 +02:00
|
|
|
let validate = this.validate;
|
|
|
|
control.after_analysis.callback =
|
2018-10-19 10:07:17 +02:00
|
|
|
Box::new(move |state| after_analysis(state, validate));
|
2018-07-11 17:53:44 +02:00
|
|
|
control.after_analysis.stop = Compilation::Stop;
|
2016-11-26 17:36:31 -08:00
|
|
|
control
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn after_hir_lowering(state: &mut CompileState) {
|
2017-08-10 08:48:38 -07:00
|
|
|
let attr = (
|
|
|
|
String::from("miri"),
|
|
|
|
syntax::feature_gate::AttributeType::Whitelisted,
|
|
|
|
);
|
2016-11-26 17:36:31 -08:00
|
|
|
state.session.plugin_attributes.borrow_mut().push(attr);
|
|
|
|
}
|
2016-06-13 14:27:05 +02:00
|
|
|
|
2018-10-11 11:24:22 +02:00
|
|
|
fn after_analysis<'a, 'tcx>(
|
|
|
|
state: &mut CompileState<'a, 'tcx>,
|
|
|
|
validate: bool,
|
|
|
|
) {
|
2018-10-31 10:53:37 +01:00
|
|
|
init_late_loggers();
|
2016-11-26 17:36:31 -08:00
|
|
|
state.session.abort_if_errors();
|
2016-06-30 21:33:24 -06:00
|
|
|
|
2016-11-26 17:36:31 -08:00
|
|
|
let tcx = state.tcx.unwrap();
|
2016-12-16 17:10:16 -08:00
|
|
|
|
2017-01-24 15:33:51 +01:00
|
|
|
if std::env::args().any(|arg| arg == "--test") {
|
2018-10-11 11:24:22 +02:00
|
|
|
struct Visitor<'a, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
state: &'a CompileState<'a, 'tcx>,
|
|
|
|
validate: bool,
|
|
|
|
};
|
2017-01-24 15:33:51 +01:00
|
|
|
impl<'a, 'tcx: 'a, 'hir> itemlikevisit::ItemLikeVisitor<'hir> for Visitor<'a, 'tcx> {
|
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item) {
|
2018-07-16 22:26:32 +02:00
|
|
|
if let hir::ItemKind::Fn(.., body_id) = i.node {
|
2017-08-10 08:48:38 -07:00
|
|
|
if i.attrs.iter().any(|attr| {
|
2018-05-07 10:18:45 +02:00
|
|
|
attr.name() == "test"
|
2017-08-10 08:48:38 -07:00
|
|
|
})
|
|
|
|
{
|
2018-10-11 11:24:22 +02:00
|
|
|
let did = self.tcx.hir.body_owner_def_id(body_id);
|
2017-08-10 08:48:38 -07:00
|
|
|
println!(
|
|
|
|
"running test: {}",
|
2018-10-11 11:24:22 +02:00
|
|
|
self.tcx.def_path_debug_str(did),
|
2017-08-10 08:48:38 -07:00
|
|
|
);
|
2018-10-19 10:07:17 +02:00
|
|
|
miri::eval_main(self.tcx, did, self.validate);
|
2018-10-11 11:24:22 +02:00
|
|
|
self.state.session.abort_if_errors();
|
2017-01-24 15:33:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'hir hir::TraitItem) {}
|
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'hir hir::ImplItem) {}
|
|
|
|
}
|
2017-08-10 08:48:38 -07:00
|
|
|
state.hir_crate.unwrap().visit_all_item_likes(
|
2018-10-11 11:24:22 +02:00
|
|
|
&mut Visitor { tcx, state, validate }
|
2017-08-10 08:48:38 -07:00
|
|
|
);
|
2018-04-17 14:26:17 +02:00
|
|
|
} else if let Some((entry_node_id, _, _)) = *state.session.entry_fn.borrow() {
|
2017-05-05 10:34:38 +02:00
|
|
|
let entry_def_id = tcx.hir.local_def_id(entry_node_id);
|
2018-10-19 10:07:17 +02:00
|
|
|
miri::eval_main(tcx, entry_def_id, validate);
|
2017-01-24 15:33:51 +01:00
|
|
|
|
2017-05-05 10:34:38 +02:00
|
|
|
state.session.abort_if_errors();
|
|
|
|
} else {
|
|
|
|
println!("no main function found, assuming auxiliary build");
|
2016-12-16 17:10:16 -08:00
|
|
|
}
|
2016-11-26 17:54:19 -08:00
|
|
|
}
|
|
|
|
|
2018-10-31 10:53:37 +01:00
|
|
|
fn init_early_loggers() {
|
2018-10-31 10:09:55 +01:00
|
|
|
// Notice that our `extern crate log` is NOT the same as rustc's! So we have to initialize
|
2018-10-31 10:53:37 +01:00
|
|
|
// them both. We always initialize miri early.
|
2018-10-31 10:09:55 +01:00
|
|
|
let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
|
|
|
|
env_logger::init_from_env(env);
|
2018-10-31 10:53:37 +01:00
|
|
|
// We only initialize rustc if the env var is set (so the user asked for it).
|
|
|
|
// If it is not set, we avoid initializing now so that we can initialize
|
|
|
|
// later with our custom settings, and NOT log anything for what happens before
|
|
|
|
// miri gets started.
|
|
|
|
if env::var("RUST_LOG").is_ok() {
|
|
|
|
rustc_driver::init_rustc_env_logger();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_late_loggers() {
|
|
|
|
// Initializing loggers right before we start evaluation. We overwrite the RUST_LOG
|
|
|
|
// env var if it is not set, control it based on MIRI_LOG.
|
2018-10-31 10:09:55 +01:00
|
|
|
if let Ok(var) = env::var("MIRI_LOG") {
|
2018-10-31 10:53:37 +01:00
|
|
|
if env::var("RUST_LOG").is_err() {
|
2018-10-31 10:09:55 +01:00
|
|
|
// We try to be a bit clever here: If MIRI_LOG is just a single level
|
|
|
|
// used for everything, we only apply it to the parts of rustc that are
|
|
|
|
// CTFE-related. Only if MIRI_LOG contains `module=level`, we just
|
|
|
|
// use the same value for RUST_LOG.
|
|
|
|
// This way, if you set `MIRI_LOG=trace`, you get only the right parts of
|
|
|
|
// rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_mir::interpret=debug`.
|
|
|
|
if var.contains('=') {
|
|
|
|
env::set_var("RUST_LOG", &var);
|
|
|
|
} else {
|
|
|
|
env::set_var("RUST_LOG",
|
|
|
|
&format!("rustc::mir::interpret={0},rustc_mir::interpret={0}", var));
|
|
|
|
}
|
2018-10-31 10:53:37 +01:00
|
|
|
rustc_driver::init_rustc_env_logger();
|
2016-06-17 19:48:45 -06:00
|
|
|
}
|
2016-05-30 18:09:52 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-14 19:30:59 -06:00
|
|
|
|
|
|
|
fn find_sysroot() -> String {
|
2017-05-30 14:09:40 -07:00
|
|
|
if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
|
|
|
|
return sysroot;
|
|
|
|
}
|
|
|
|
|
2016-06-14 19:30:59 -06: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),
|
2017-08-10 08:48:38 -07:00
|
|
|
_ => {
|
|
|
|
option_env!("RUST_SYSROOT")
|
|
|
|
.expect(
|
2018-07-30 14:49:01 +02:00
|
|
|
"Could not find sysroot. Either set MIRI_SYSROOT at run-time, or at \
|
|
|
|
build-time specify RUST_SYSROOT env var or use rustup or multirust",
|
2017-08-10 08:48:38 -07:00
|
|
|
)
|
|
|
|
.to_owned()
|
|
|
|
}
|
2016-06-14 19:30:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-10-31 10:53:37 +01:00
|
|
|
init_early_loggers();
|
2016-06-14 19:30:59 -06:00
|
|
|
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 16:05:14 +02:00
|
|
|
|
2018-10-11 11:24:22 +02:00
|
|
|
let mut validate = true;
|
2018-05-07 10:46:32 +02:00
|
|
|
args.retain(|arg| {
|
2018-10-11 11:24:22 +02:00
|
|
|
match arg.as_str() {
|
|
|
|
"-Zmiri-disable-validation" => {
|
|
|
|
validate = false;
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
2018-05-07 10:46:32 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-07-25 17:28:16 +02:00
|
|
|
|
|
|
|
let result = rustc_driver::run(move || {
|
|
|
|
rustc_driver::run_compiler(&args, Box::new(MiriCompilerCalls {
|
|
|
|
default: Box::new(RustcDefaultCalls),
|
2018-10-11 11:24:22 +02:00
|
|
|
validate,
|
2018-07-25 17:28:16 +02:00
|
|
|
}), None, None)
|
|
|
|
});
|
|
|
|
std::process::exit(result as i32);
|
2016-06-14 19:30:59 -06:00
|
|
|
}
|