1 #![feature(rustc_private)]
6 extern crate rustc_metadata;
7 extern crate rustc_driver;
8 extern crate rustc_errors;
9 extern crate rustc_codegen_utils;
10 extern crate env_logger;
11 extern crate log_settings;
17 use std::path::PathBuf;
18 use std::str::FromStr;
21 use rustc::session::Session;
22 use rustc_metadata::cstore::CStore;
23 use rustc_driver::{Compilation, CompilerCalls, RustcDefaultCalls};
24 use rustc_driver::driver::{CompileState, CompileController};
25 use rustc::session::config::{self, Input, ErrorOutputType};
26 use rustc_codegen_utils::codegen_backend::CodegenBackend;
29 struct MiriCompilerCalls {
30 default: Box<RustcDefaultCalls>,
32 /// Whether to enforce the validity invariant.
36 impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
39 matches: &getopts::Matches,
40 sopts: &config::Options,
41 cfg: &ast::CrateConfig,
42 descriptions: &rustc_errors::registry::Registry,
43 output: ErrorOutputType,
45 self.default.early_callback(
55 matches: &getopts::Matches,
56 sopts: &config::Options,
57 cfg: &ast::CrateConfig,
58 odir: &Option<PathBuf>,
59 ofile: &Option<PathBuf>,
60 descriptions: &rustc_errors::registry::Registry,
61 ) -> Option<(Input, Option<PathBuf>)> {
62 self.default.no_input(
73 codegen_backend: &CodegenBackend,
74 matches: &getopts::Matches,
78 odir: &Option<PathBuf>,
79 ofile: &Option<PathBuf>,
81 self.default.late_callback(codegen_backend, matches, sess, cstore, input, odir, ofile)
86 matches: &getopts::Matches,
87 ) -> CompileController<'a> {
89 let mut control = this.default.build_controller(sess, matches);
90 control.after_hir_lowering.callback = Box::new(after_hir_lowering);
91 let validate = this.validate;
92 control.after_analysis.callback =
93 Box::new(move |state| after_analysis(state, validate));
94 control.after_analysis.stop = Compilation::Stop;
99 fn after_hir_lowering(state: &mut CompileState) {
101 String::from("miri"),
102 syntax::feature_gate::AttributeType::Whitelisted,
104 state.session.plugin_attributes.borrow_mut().push(attr);
107 fn after_analysis<'a, 'tcx>(
108 state: &mut CompileState<'a, 'tcx>,
112 state.session.abort_if_errors();
114 let tcx = state.tcx.unwrap();
116 let (entry_node_id, _, _) = state.session.entry_fn.borrow().expect("no main function found!");
117 let entry_def_id = tcx.hir().local_def_id(entry_node_id);
119 miri::eval_main(tcx, entry_def_id, validate);
121 state.session.abort_if_errors();
124 fn init_early_loggers() {
125 // Notice that our `extern crate log` is NOT the same as rustc's! So we have to initialize
126 // them both. We always initialize miri early.
127 let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
128 env_logger::init_from_env(env);
129 // We only initialize rustc if the env var is set (so the user asked for it).
130 // If it is not set, we avoid initializing now so that we can initialize
131 // later with our custom settings, and NOT log anything for what happens before
132 // miri gets started.
133 if env::var("RUST_LOG").is_ok() {
134 rustc_driver::init_rustc_env_logger();
138 fn init_late_loggers() {
139 // Initializing loggers right before we start evaluation. We overwrite the RUST_LOG
140 // env var if it is not set, control it based on MIRI_LOG.
141 if let Ok(var) = env::var("MIRI_LOG") {
142 if env::var("RUST_LOG").is_err() {
143 // We try to be a bit clever here: If MIRI_LOG is just a single level
144 // used for everything, we only apply it to the parts of rustc that are
145 // CTFE-related. Otherwise, we use it verbatim for RUST_LOG.
146 // This way, if you set `MIRI_LOG=trace`, you get only the right parts of
147 // rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_mir::interpret=debug`.
148 if log::Level::from_str(&var).is_ok() {
149 env::set_var("RUST_LOG",
150 &format!("rustc::mir::interpret={0},rustc_mir::interpret={0}", var));
152 env::set_var("RUST_LOG", &var);
154 rustc_driver::init_rustc_env_logger();
158 // If MIRI_BACKTRACE is set and RUST_CTFE_BACKTRACE is not, set RUST_CTFE_BACKTRACE.
159 // Do this late, so we really only apply this to miri's errors.
160 if let Ok(var) = env::var("MIRI_BACKTRACE") {
161 if env::var("RUST_CTFE_BACKTRACE") == Err(env::VarError::NotPresent) {
162 env::set_var("RUST_CTFE_BACKTRACE", &var);
167 fn find_sysroot() -> String {
168 if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
172 // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
173 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
174 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
175 match (home, toolchain) {
176 (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
178 option_env!("RUST_SYSROOT")
180 "Could not find sysroot. Either set MIRI_SYSROOT at run-time, or at \
181 build-time specify RUST_SYSROOT env var or use rustup or multirust",
189 init_early_loggers();
190 let mut args: Vec<String> = std::env::args().collect();
192 // Parse our own -Z flags and remove them before rustc gets their hand on them.
193 let mut validate = true;
196 "-Zmiri-disable-validation" => {
204 // Determine sysroot and let rustc know about it
205 let sysroot_flag = String::from("--sysroot");
206 if !args.contains(&sysroot_flag) {
207 args.push(sysroot_flag);
208 args.push(find_sysroot());
210 // Finally, add the default flags all the way in the beginning, but after the binary name.
211 args.splice(1..1, miri::miri_default_args().iter().map(ToString::to_string));
213 trace!("rustc arguments: {:?}", args);
214 let result = rustc_driver::run(move || {
215 rustc_driver::run_compiler(&args, Box::new(MiriCompilerCalls {
216 default: Box::new(RustcDefaultCalls),
220 std::process::exit(result as i32);