rust/src/bin/miri.rs

242 lines
7.5 KiB
Rust
Raw Normal View History

2018-03-23 06:18:33 -05:00
#![feature(rustc_private)]
2016-05-09 16:30:47 -05:00
extern crate getopts;
extern crate miri;
extern crate rustc;
2018-08-07 08:22:11 -05:00
extern crate rustc_metadata;
extern crate rustc_driver;
extern crate rustc_errors;
extern crate rustc_codegen_utils;
extern crate env_logger;
extern crate log_settings;
extern crate syntax;
2017-01-28 08:27:20 -06:00
extern crate log;
use rustc::session::Session;
2018-08-07 08:22:11 -05:00
use rustc_metadata::cstore::CStore;
use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
use rustc_driver::driver::{CompileState, CompileController};
use rustc::session::config::{self, Input, ErrorOutputType};
2017-01-24 08:33:51 -06:00
use rustc::hir::{self, itemlikevisit};
use rustc::ty::TyCtxt;
use rustc_codegen_utils::codegen_backend::CodegenBackend;
2018-01-14 11:59:13 -06:00
use syntax::ast;
use std::path::PathBuf;
struct MiriCompilerCalls {
default: Box<RustcDefaultCalls>,
/// Whether to begin interpretation at the start_fn lang item or not
2018-06-29 05:52:04 -05:00
///
/// If false, the interpretation begins at the `main` function
start_fn: bool,
}
impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
fn early_callback(
&mut self,
matches: &getopts::Matches,
sopts: &config::Options,
cfg: &ast::CrateConfig,
descriptions: &rustc_errors::registry::Registry,
output: ErrorOutputType,
) -> Compilation {
self.default.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.default.no_input(
matches,
sopts,
cfg,
odir,
ofile,
descriptions,
)
}
fn late_callback(
&mut self,
2018-05-19 07:09:29 -05:00
codegen_backend: &CodegenBackend,
matches: &getopts::Matches,
sess: &Session,
2018-08-07 08:22:11 -05:00
cstore: &CStore,
input: &Input,
odir: &Option<PathBuf>,
ofile: &Option<PathBuf>,
) -> Compilation {
2018-05-19 07:09:29 -05:00
self.default.late_callback(codegen_backend, matches, sess, cstore, input, odir, ofile)
}
fn build_controller(
self: Box<Self>,
sess: &Session,
matches: &getopts::Matches,
) -> CompileController<'a> {
let this = *self;
let mut control = this.default.build_controller(sess, matches);
control.after_hir_lowering.callback = Box::new(after_hir_lowering);
let start_fn = this.start_fn;
control.after_analysis.callback = Box::new(move |state| after_analysis(state, start_fn));
2018-07-11 10:53:44 -05:00
control.after_analysis.stop = Compilation::Stop;
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);
}
fn after_analysis<'a, 'tcx>(state: &mut CompileState<'a, 'tcx>, use_start_fn: bool) {
state.session.abort_if_errors();
let tcx = state.tcx.unwrap();
2017-01-24 08:33:51 -06:00
if std::env::args().any(|arg| arg == "--test") {
struct Visitor<'a, 'tcx: 'a>(
TyCtxt<'a, 'tcx, 'tcx>,
&'a CompileState<'a, 'tcx>
);
2017-01-24 08:33:51 -06: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 15:26:32 -05:00
if let hir::ItemKind::Fn(.., body_id) = i.node {
if i.attrs.iter().any(|attr| {
2018-05-07 03:18:45 -05:00
attr.name() == "test"
})
{
2018-01-14 11:59:13 -06:00
let did = self.0.hir.body_owner_def_id(body_id);
println!(
"running test: {}",
2018-01-14 11:59:13 -06:00
self.0.def_path_debug_str(did),
);
2018-01-14 11:59:13 -06:00
miri::eval_main(self.0, did, None);
self.1.session.abort_if_errors();
2017-01-24 08:33:51 -06:00
}
}
}
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(
2018-01-14 11:59:13 -06:00
&mut Visitor(tcx, state),
);
2018-04-17 07:26:17 -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);
// Use start_fn lang item if we have -Zmiri-start-fn set
let start_wrapper = if use_start_fn {
Some(tcx.lang_items().start_fn().unwrap())
} else {
None
};
2018-01-14 11:59:13 -06:00
miri::eval_main(tcx, entry_def_id, start_wrapper);
2017-01-24 08:33:51 -06:00
state.session.abort_if_errors();
} else {
println!("no main function found, assuming auxiliary build");
}
2016-11-26 19:54:19 -06:00
}
fn init_logger() {
2018-01-06 09:21:24 -06:00
let format = |formatter: &mut env_logger::fmt::Formatter, record: &log::Record| {
use std::io::Write;
if record.level() == log::Level::Trace {
// prepend frame number
2016-06-17 20:48:45 -05:00
let indentation = log_settings::settings().indentation;
2018-01-06 09:21:24 -06:00
writeln!(
formatter,
"{indentation}:{lvl}:{module}: {text}",
2016-06-17 20:48:45 -05:00
lvl = record.level(),
2018-01-06 09:21:24 -06:00
module = record.module_path().unwrap_or("<unknown module>"),
2017-02-07 02:45:22 -06:00
indentation = indentation,
text = record.args(),
)
2016-06-17 20:48:45 -05:00
} else {
2018-01-06 09:21:24 -06:00
writeln!(
formatter,
2017-02-07 02:45:22 -06:00
"{lvl}:{module}: {text}",
2016-06-17 20:48:45 -05:00
lvl = record.level(),
2018-01-06 09:21:24 -06:00
module = record.module_path().unwrap_or("<unknown_module>"),
2017-02-07 02:45:22 -06:00
text = record.args(),
)
2016-06-17 20:48:45 -05:00
}
};
2018-01-06 09:21:24 -06:00
let mut builder = env_logger::Builder::new();
builder.format(format).filter(
None,
2018-01-06 09:21:24 -06:00
log::LevelFilter::Info,
);
if std::env::var("MIRI_LOG").is_ok() {
builder.parse(&std::env::var("MIRI_LOG").unwrap());
}
2018-01-06 09:21:24 -06:00
builder.init();
}
fn find_sysroot() -> String {
if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
return sysroot;
}
// 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(
"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",
)
.to_owned()
}
}
}
fn main() {
rustc_driver::init_rustc_env_logger();
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
let mut start_fn = false;
args.retain(|arg| {
if arg == "-Zmiri-start-fn" {
start_fn = true;
false
} else {
true
}
});
let result = rustc_driver::run(move || {
rustc_driver::run_compiler(&args, Box::new(MiriCompilerCalls {
default: Box::new(RustcDefaultCalls),
start_fn,
}), None, None)
});
std::process::exit(result as i32);
}