2018-11-21 06:40:25 -06:00
|
|
|
#![feature(rustc_private)]
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
extern crate env_logger;
|
2016-05-09 16:30:47 -05:00
|
|
|
extern crate getopts;
|
2019-02-15 19:29:38 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate log_settings;
|
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 08:22:11 -05:00
|
|
|
extern crate rustc_metadata;
|
2015-11-12 15:50:58 -06:00
|
|
|
extern crate rustc_driver;
|
2017-01-24 06:28:36 -06:00
|
|
|
extern crate rustc_errors;
|
2018-05-19 05:14:13 -05:00
|
|
|
extern crate rustc_codegen_utils;
|
2019-02-01 16:26:42 -06:00
|
|
|
extern crate rustc_interface;
|
2016-06-10 06:01:51 -05:00
|
|
|
extern crate syntax;
|
2018-10-24 10:16:41 -05:00
|
|
|
|
2018-10-31 04:58:19 -05:00
|
|
|
use std::str::FromStr;
|
2018-10-31 04:09:55 -05:00
|
|
|
use std::env;
|
|
|
|
|
2019-07-13 07:28:33 -05:00
|
|
|
use hex::FromHexError;
|
|
|
|
|
2019-02-01 16:26:42 -06:00
|
|
|
use rustc_interface::interface;
|
2019-01-21 10:29:09 -06:00
|
|
|
use rustc::hir::def_id::LOCAL_CRATE;
|
2019-07-19 10:36:25 -05:00
|
|
|
use rustc_driver::Compilation;
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2017-08-28 10:32:21 -05:00
|
|
|
struct MiriCompilerCalls {
|
2019-02-01 16:26:42 -06:00
|
|
|
miri_config: miri::MiriConfig,
|
2017-08-28 10:32:21 -05:00
|
|
|
}
|
2015-11-12 15:50:58 -06:00
|
|
|
|
2019-02-01 16:26:42 -06:00
|
|
|
impl rustc_driver::Callbacks for MiriCompilerCalls {
|
2019-07-19 10:36:25 -05:00
|
|
|
fn after_parsing(&mut self, compiler: &interface::Compiler) -> Compilation {
|
2019-02-01 16:26:42 -06:00
|
|
|
let attr = (
|
2019-05-13 14:55:58 -05:00
|
|
|
syntax::symbol::Symbol::intern("miri"),
|
2019-02-01 16:26:42 -06:00
|
|
|
syntax::feature_gate::AttributeType::Whitelisted,
|
|
|
|
);
|
|
|
|
compiler.session().plugin_attributes.borrow_mut().push(attr);
|
2016-11-26 19:36:31 -06:00
|
|
|
|
2019-07-19 10:36:25 -05:00
|
|
|
Compilation::Continue
|
2019-02-01 16:26:42 -06:00
|
|
|
}
|
2016-06-30 22:33:24 -05:00
|
|
|
|
2019-07-19 10:36:25 -05:00
|
|
|
fn after_analysis(&mut self, compiler: &interface::Compiler) -> Compilation {
|
2019-02-01 16:26:42 -06:00
|
|
|
init_late_loggers();
|
|
|
|
compiler.session().abort_if_errors();
|
2016-12-16 19:10:16 -06:00
|
|
|
|
2019-02-01 16:26:42 -06:00
|
|
|
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
|
|
|
|
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect("no main function found!");
|
2019-03-10 06:03:51 -05:00
|
|
|
let mut config = self.miri_config.clone();
|
2019-01-21 10:29:09 -06:00
|
|
|
|
2019-03-10 06:03:51 -05:00
|
|
|
// Add filename to `miri` arguments.
|
|
|
|
config.args.insert(0, compiler.input().filestem().to_string());
|
|
|
|
|
|
|
|
miri::eval_main(tcx, entry_def_id, config);
|
2019-02-01 16:26:42 -06:00
|
|
|
});
|
2017-01-24 08:33:51 -06:00
|
|
|
|
2019-02-01 16:26:42 -06:00
|
|
|
compiler.session().abort_if_errors();
|
2018-11-27 08:06:23 -06:00
|
|
|
|
2019-07-19 10:36:25 -05:00
|
|
|
Compilation::Stop
|
2019-02-01 16:26:42 -06:00
|
|
|
}
|
2016-11-26 19:54:19 -06:00
|
|
|
}
|
|
|
|
|
2018-10-31 04:53:37 -05:00
|
|
|
fn init_early_loggers() {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Note that our `extern crate log` is *not* the same as rustc's; as a result, we have to
|
|
|
|
// initialize them both, and we always initialize `miri`'s first.
|
2018-10-31 04:09:55 -05:00
|
|
|
let env = env_logger::Env::new().filter("MIRI_LOG").write_style("MIRI_LOG_STYLE");
|
|
|
|
env_logger::init_from_env(env);
|
2019-02-15 19:29:38 -06:00
|
|
|
// We only initialize `rustc` if the env var is set (so the user asked for it).
|
2018-10-31 04:53:37 -05:00
|
|
|
// If it is not set, we avoid initializing now so that we can initialize
|
2019-02-15 19:29:38 -06:00
|
|
|
// later with our custom settings, and *not* log anything for what happens before
|
|
|
|
// `miri` gets started.
|
2019-05-13 07:10:52 -05:00
|
|
|
if env::var("RUSTC_LOG").is_ok() {
|
2018-10-31 04:53:37 -05:00
|
|
|
rustc_driver::init_rustc_env_logger();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_late_loggers() {
|
2019-05-13 07:10:52 -05:00
|
|
|
// We initialize loggers right before we start evaluation. We overwrite the `RUSTC_LOG`
|
2019-02-15 19:29:38 -06:00
|
|
|
// env var if it is not set, control it based on `MIRI_LOG`.
|
2018-10-31 04:09:55 -05:00
|
|
|
if let Ok(var) = env::var("MIRI_LOG") {
|
2019-05-13 07:10:52 -05:00
|
|
|
if env::var("RUSTC_LOG").is_err() {
|
2019-02-15 19:29:38 -06:00
|
|
|
// We try to be a bit clever here: if `MIRI_LOG` is just a single level
|
2018-10-31 04:09:55 -05:00
|
|
|
// used for everything, we only apply it to the parts of rustc that are
|
2019-05-13 07:10:52 -05:00
|
|
|
// CTFE-related. Otherwise, we use it verbatim for `RUSTC_LOG`.
|
2018-10-31 04:09:55 -05:00
|
|
|
// 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`.
|
2018-10-31 04:58:19 -05:00
|
|
|
if log::Level::from_str(&var).is_ok() {
|
2019-05-13 07:10:52 -05:00
|
|
|
env::set_var("RUSTC_LOG",
|
2018-10-31 04:09:55 -05:00
|
|
|
&format!("rustc::mir::interpret={0},rustc_mir::interpret={0}", var));
|
2018-10-31 04:58:19 -05:00
|
|
|
} else {
|
2019-05-13 07:10:52 -05:00
|
|
|
env::set_var("RUSTC_LOG", &var);
|
2018-10-31 04:09:55 -05:00
|
|
|
}
|
2018-10-31 04:53:37 -05:00
|
|
|
rustc_driver::init_rustc_env_logger();
|
2016-06-17 20:48:45 -05:00
|
|
|
}
|
2016-05-30 11:09:52 -05:00
|
|
|
}
|
2019-01-04 08:37:51 -06:00
|
|
|
|
2019-08-12 01:25:31 -05:00
|
|
|
// If `MIRI_BACKTRACE` is set and `RUSTC_CTFE_BACKTRACE` is not, set `RUSTC_CTFE_BACKTRACE`.
|
|
|
|
// Do this late, so we ideally only apply this to Miri's errors.
|
2019-01-04 08:37:51 -06:00
|
|
|
if let Ok(var) = env::var("MIRI_BACKTRACE") {
|
2019-08-12 01:25:31 -05:00
|
|
|
if env::var("RUSTC_CTFE_BACKTRACE") == Err(env::VarError::NotPresent) {
|
|
|
|
env::set_var("RUSTC_CTFE_BACKTRACE", &var);
|
2019-01-04 08:37:51 -06:00
|
|
|
}
|
|
|
|
}
|
2016-05-30 11:09:52 -05:00
|
|
|
}
|
2016-06-14 20:30:59 -05:00
|
|
|
|
2019-06-09 10:12:41 -05:00
|
|
|
/// Returns the "default sysroot" that Miri will use if no `--sysroot` flag is set.
|
|
|
|
/// Should be a compile-time constant.
|
2019-06-14 05:09:43 -05:00
|
|
|
fn compile_time_sysroot() -> Option<String> {
|
|
|
|
if option_env!("RUSTC_STAGE").is_some() {
|
|
|
|
// This is being built as part of rustc, and gets shipped with rustup.
|
|
|
|
// We can rely on the sysroot computation in librustc.
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// For builds outside rustc, we need to ensure that we got a sysroot
|
|
|
|
// that gets used as a default. The sysroot computation in librustc would
|
|
|
|
// end up somewhere in the build dir.
|
2019-02-15 19:29:38 -06:00
|
|
|
// Taken from PR <https://github.com/Manishearth/rust-clippy/pull/911>.
|
2016-06-14 20:30:59 -05:00
|
|
|
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
|
|
|
|
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
|
2019-06-14 05:09:43 -05:00
|
|
|
Some(match (home, toolchain) {
|
2016-06-14 20:30:59 -05:00
|
|
|
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
|
2017-08-10 10:48:38 -05:00
|
|
|
_ => {
|
|
|
|
option_env!("RUST_SYSROOT")
|
2019-06-14 05:09:43 -05:00
|
|
|
.expect("To build Miri without rustup, set the `RUST_SYSROOT` env var at build time")
|
2017-08-10 10:48:38 -05:00
|
|
|
.to_owned()
|
|
|
|
}
|
2019-06-14 05:09:43 -05:00
|
|
|
})
|
2016-06-14 20:30:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-10-31 04:53:37 -05:00
|
|
|
init_early_loggers();
|
2016-06-14 20:30:59 -05:00
|
|
|
|
2019-02-15 19:29:38 -06:00
|
|
|
// Parse our arguments and split them across `rustc` and `miri`.
|
2018-10-11 04:24:22 -05:00
|
|
|
let mut validate = true;
|
2019-08-06 10:28:50 -05:00
|
|
|
let mut communicate = false;
|
2019-04-07 17:17:43 -05:00
|
|
|
let mut seed: Option<u64> = None;
|
2019-02-08 12:21:44 -06:00
|
|
|
let mut rustc_args = vec![];
|
|
|
|
let mut miri_args = vec![];
|
|
|
|
let mut after_dashdash = false;
|
2019-08-28 17:20:50 -05:00
|
|
|
let mut excluded_env_vars = vec![];
|
2019-02-08 12:21:44 -06:00
|
|
|
for arg in std::env::args() {
|
|
|
|
if rustc_args.is_empty() {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Very first arg: for `rustc`.
|
2019-02-08 12:21:44 -06:00
|
|
|
rustc_args.push(arg);
|
2018-05-07 03:46:32 -05:00
|
|
|
}
|
2019-02-08 12:21:44 -06:00
|
|
|
else if after_dashdash {
|
2019-02-15 19:29:38 -06:00
|
|
|
// Everything that comes after are `miri` args.
|
2019-02-08 12:21:44 -06:00
|
|
|
miri_args.push(arg);
|
|
|
|
} else {
|
|
|
|
match arg.as_str() {
|
|
|
|
"-Zmiri-disable-validation" => {
|
|
|
|
validate = false;
|
|
|
|
},
|
2019-08-23 16:29:11 -05:00
|
|
|
"-Zmiri-disable-isolation" => {
|
2019-08-06 10:28:50 -05:00
|
|
|
communicate = true;
|
|
|
|
},
|
2019-02-08 12:21:44 -06:00
|
|
|
"--" => {
|
|
|
|
after_dashdash = true;
|
|
|
|
}
|
2019-04-08 20:40:52 -05:00
|
|
|
arg if arg.starts_with("-Zmiri-seed=") => {
|
|
|
|
if seed.is_some() {
|
|
|
|
panic!("Cannot specify -Zmiri-seed multiple times!");
|
|
|
|
}
|
2019-07-13 07:28:33 -05:00
|
|
|
let seed_raw = hex::decode(arg.trim_start_matches("-Zmiri-seed="))
|
|
|
|
.unwrap_or_else(|err| match err {
|
|
|
|
FromHexError::InvalidHexCharacter { .. } => panic!(
|
|
|
|
"-Zmiri-seed should only contain valid hex digits [0-9a-fA-F]"
|
|
|
|
),
|
|
|
|
FromHexError::OddLength => panic!("-Zmiri-seed should have an even number of digits"),
|
|
|
|
err => panic!("Unknown error decoding -Zmiri-seed as hex: {:?}", err),
|
|
|
|
});
|
2019-04-08 20:40:52 -05:00
|
|
|
if seed_raw.len() > 8 {
|
|
|
|
panic!(format!("-Zmiri-seed must be at most 8 bytes, was {}", seed_raw.len()));
|
2019-04-07 17:17:43 -05:00
|
|
|
}
|
2019-04-08 20:40:52 -05:00
|
|
|
|
|
|
|
let mut bytes = [0; 8];
|
|
|
|
bytes[..seed_raw.len()].copy_from_slice(&seed_raw);
|
|
|
|
seed = Some(u64::from_be_bytes(bytes));
|
|
|
|
|
|
|
|
},
|
2019-08-28 17:20:50 -05:00
|
|
|
arg if arg.starts_with("-Zmiri-env-exclude=") => {
|
|
|
|
excluded_env_vars.push(arg.trim_start_matches("-Zmiri-env-exclude=").to_owned());
|
|
|
|
},
|
2019-04-08 20:40:52 -05:00
|
|
|
_ => {
|
|
|
|
rustc_args.push(arg);
|
2019-02-08 12:21:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 03:46:32 -05:00
|
|
|
|
2019-06-14 05:09:43 -05:00
|
|
|
// Determine sysroot if needed. Make sure we always call `compile_time_sysroot`
|
|
|
|
// as that also does some sanity-checks of the environment we were built in.
|
|
|
|
// FIXME: Ideally we'd turn a bad build env into a compile-time error, but
|
|
|
|
// CTFE does not seem powerful enough for that yet.
|
|
|
|
if let Some(sysroot) = compile_time_sysroot() {
|
2019-07-13 08:18:15 -05:00
|
|
|
let sysroot_flag = "--sysroot";
|
|
|
|
if !rustc_args.iter().any(|e| e == sysroot_flag) {
|
2019-06-14 05:09:43 -05:00
|
|
|
// We need to overwrite the default that librustc would compute.
|
2019-07-13 08:18:15 -05:00
|
|
|
rustc_args.push(sysroot_flag.to_owned());
|
2019-06-14 05:09:43 -05:00
|
|
|
rustc_args.push(sysroot);
|
|
|
|
}
|
2018-10-24 10:16:41 -05:00
|
|
|
}
|
2019-06-09 07:31:05 -05:00
|
|
|
|
2018-11-03 05:03:53 -05:00
|
|
|
// Finally, add the default flags all the way in the beginning, but after the binary name.
|
2019-02-08 12:21:44 -06:00
|
|
|
rustc_args.splice(1..1, miri::miri_default_args().iter().map(ToString::to_string));
|
2018-07-25 10:28:16 -05:00
|
|
|
|
2019-02-08 12:21:44 -06:00
|
|
|
debug!("rustc arguments: {:?}", rustc_args);
|
|
|
|
debug!("miri arguments: {:?}", miri_args);
|
2019-08-28 17:20:50 -05:00
|
|
|
let miri_config = miri::MiriConfig {
|
|
|
|
validate,
|
|
|
|
communicate,
|
|
|
|
excluded_env_vars,
|
|
|
|
seed,
|
|
|
|
args: miri_args,
|
|
|
|
};
|
2019-09-16 02:31:56 -05:00
|
|
|
rustc_driver::install_ice_hook();
|
2019-09-15 16:27:58 -05:00
|
|
|
let result = rustc_driver::catch_fatal_errors(move || {
|
2019-02-01 16:26:42 -06:00
|
|
|
rustc_driver::run_compiler(&rustc_args, &mut MiriCompilerCalls { miri_config }, None, None)
|
|
|
|
}).and_then(|result| result);
|
|
|
|
std::process::exit(result.is_err() as i32);
|
2016-06-14 20:30:59 -05:00
|
|
|
}
|