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;
27 use rustc::hir::def_id::LOCAL_CRATE;
32 struct MiriCompilerCalls {
33 default: Box<RustcDefaultCalls>,
34 miri_config: MiriConfig,
37 impl<'a> CompilerCalls<'a> for MiriCompilerCalls {
40 matches: &getopts::Matches,
41 sopts: &config::Options,
42 cfg: &ast::CrateConfig,
43 descriptions: &rustc_errors::registry::Registry,
44 output: ErrorOutputType,
46 self.default.early_callback(
56 matches: &getopts::Matches,
57 sopts: &config::Options,
58 cfg: &ast::CrateConfig,
59 odir: &Option<PathBuf>,
60 ofile: &Option<PathBuf>,
61 descriptions: &rustc_errors::registry::Registry,
62 ) -> Option<(Input, Option<PathBuf>)> {
63 self.default.no_input(
74 codegen_backend: &CodegenBackend,
75 matches: &getopts::Matches,
79 odir: &Option<PathBuf>,
80 ofile: &Option<PathBuf>,
82 // Called *before* build_controller. Add filename to miri arguments.
83 self.miri_config.args.insert(0, input.filestem().to_string());
84 self.default.late_callback(codegen_backend, matches, sess, cstore, input, odir, ofile)
89 matches: &getopts::Matches,
90 ) -> CompileController<'a> {
92 let mut control = this.default.build_controller(sess, matches);
93 control.after_hir_lowering.callback = Box::new(after_hir_lowering);
94 let miri_config = this.miri_config;
95 control.after_analysis.callback =
96 Box::new(move |state| after_analysis(state, miri_config.clone()));
97 control.after_analysis.stop = Compilation::Stop;
102 fn after_hir_lowering(state: &mut CompileState) {
104 String::from("miri"),
105 syntax::feature_gate::AttributeType::Whitelisted,
107 state.session.plugin_attributes.borrow_mut().push(attr);
110 fn after_analysis<'a, 'tcx>(
111 state: &mut CompileState<'a, 'tcx>,
112 miri_config: MiriConfig,
115 state.session.abort_if_errors();
117 let tcx = state.tcx.unwrap();
120 let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!");
122 miri::eval_main(tcx, entry_def_id, miri_config);
124 state.session.abort_if_errors();
127 fn init_early_loggers() {
128 // Notice that our `extern crate log` is NOT the same as rustc's! So we have to initialize
129 // them both. We always initialize miri early.
130 let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
131 env_logger::init_from_env(env);
132 // We only initialize rustc if the env var is set (so the user asked for it).
133 // If it is not set, we avoid initializing now so that we can initialize
134 // later with our custom settings, and NOT log anything for what happens before
135 // miri gets started.
136 if env::var("RUST_LOG").is_ok() {
137 rustc_driver::init_rustc_env_logger();
141 fn init_late_loggers() {
142 // Initializing loggers right before we start evaluation. We overwrite the RUST_LOG
143 // env var if it is not set, control it based on MIRI_LOG.
144 if let Ok(var) = env::var("MIRI_LOG") {
145 if env::var("RUST_LOG").is_err() {
146 // We try to be a bit clever here: If MIRI_LOG is just a single level
147 // used for everything, we only apply it to the parts of rustc that are
148 // CTFE-related. Otherwise, we use it verbatim for RUST_LOG.
149 // This way, if you set `MIRI_LOG=trace`, you get only the right parts of
150 // rustc traced, but you can also do `MIRI_LOG=miri=trace,rustc_mir::interpret=debug`.
151 if log::Level::from_str(&var).is_ok() {
152 env::set_var("RUST_LOG",
153 &format!("rustc::mir::interpret={0},rustc_mir::interpret={0}", var));
155 env::set_var("RUST_LOG", &var);
157 rustc_driver::init_rustc_env_logger();
161 // If MIRI_BACKTRACE is set and RUST_CTFE_BACKTRACE is not, set RUST_CTFE_BACKTRACE.
162 // Do this late, so we really only apply this to miri's errors.
163 if let Ok(var) = env::var("MIRI_BACKTRACE") {
164 if env::var("RUST_CTFE_BACKTRACE") == Err(env::VarError::NotPresent) {
165 env::set_var("RUST_CTFE_BACKTRACE", &var);
170 fn find_sysroot() -> String {
171 if let Ok(sysroot) = std::env::var("MIRI_SYSROOT") {
175 // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
176 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
177 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
178 match (home, toolchain) {
179 (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
181 option_env!("RUST_SYSROOT")
183 "Could not find sysroot. Either set MIRI_SYSROOT at run-time, or at \
184 build-time specify RUST_SYSROOT env var or use rustup or multirust",
192 init_early_loggers();
194 // Parse our arguments and split them across rustc and miri
195 let mut validate = true;
196 let mut rustc_args = vec![];
197 let mut miri_args = vec![];
198 let mut after_dashdash = false;
199 for arg in std::env::args() {
200 if rustc_args.is_empty() {
201 // Very first arg: for rustc
202 rustc_args.push(arg);
204 else if after_dashdash {
205 // Everything that comes is Miri args
209 "-Zmiri-disable-validation" => {
213 after_dashdash = true;
216 rustc_args.push(arg);
222 // Determine sysroot and let rustc know about it
223 let sysroot_flag = String::from("--sysroot");
224 if !rustc_args.contains(&sysroot_flag) {
225 rustc_args.push(sysroot_flag);
226 rustc_args.push(find_sysroot());
228 // Finally, add the default flags all the way in the beginning, but after the binary name.
229 rustc_args.splice(1..1, miri::miri_default_args().iter().map(ToString::to_string));
231 debug!("rustc arguments: {:?}", rustc_args);
232 debug!("miri arguments: {:?}", miri_args);
233 let miri_config = MiriConfig { validate, args: miri_args };
234 let result = rustc_driver::run(move || {
235 rustc_driver::run_compiler(&rustc_args, Box::new(MiriCompilerCalls {
236 default: Box::new(RustcDefaultCalls),
240 std::process::exit(result as i32);