From 800273c1d9005c6433098735f663bd8e362ce69b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Jul 2022 09:14:11 -0400 Subject: [PATCH 1/5] cargo-miri: set RUSTC to us --- cargo-miri/bin.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cargo-miri/bin.rs b/cargo-miri/bin.rs index 9f6603f80b..d2c772d7d9 100644 --- a/cargo-miri/bin.rs +++ b/cargo-miri/bin.rs @@ -661,14 +661,17 @@ fn phase_cargo_miri(mut args: env::Args) { ); } cmd.env("RUSTC_WRAPPER", &cargo_miri_path); - // Having both `RUSTC_WRAPPER` and `RUSTC` set does some odd things, so let's avoid that. - // See . + // We are going to invoke `MIRI` for everything, not `RUSTC`. if env::var_os("RUSTC").is_some() && env::var_os("MIRI").is_none() { println!( "WARNING: Ignoring `RUSTC` environment variable; set `MIRI` if you want to control the binary used as the driver." ); } - cmd.env_remove("RUSTC"); + // We'd prefer to just clear this env var, but cargo does not always honor `RUSTC_WRAPPER` + // (https://github.com/rust-lang/cargo/issues/10885). There is no good way to single out these invocations; + // some build scripts use the RUSTC env var as well. So we set it directly to the `miri` driver and + // hope that all they do is ask for the version number -- things could quickly go downhill from here. + cmd.env("RUSTC", &find_miri()); let runner_env_name = |triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_")); @@ -1173,8 +1176,14 @@ fn main() { match args.next().as_deref() { Some("miri") => phase_cargo_miri(args), - Some("rustc") => phase_rustc(args, RustcPhase::Build), Some(arg) => { + // If the first arg is equal to the RUSTC variable (which should be set at this point), + // then we need to behave as rustc. This is the somewhat counter-intuitive behavior of + // having both RUSTC and RUSTC_WRAPPER set (see + // https://github.com/rust-lang/cargo/issues/10886). + if arg == env::var_os("RUSTC").unwrap() { + return phase_rustc(args, RustcPhase::Build); + } // We have to distinguish the "runner" and "rustdoc" cases. // As runner, the first argument is the binary (a file that should exist, with an absolute path); // as rustdoc, the first argument is a flag (`--something`). From 7cd1d78a4769995546967e413dbbd672c11a153e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Jul 2022 09:30:09 -0400 Subject: [PATCH 2/5] only complain about runtime toolchain mismatch when there actually is a runtime toolchain --- src/bin/miri.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/bin/miri.rs b/src/bin/miri.rs index 8bd33b591d..516c730e3f 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -220,16 +220,17 @@ fn compile_time_sysroot() -> Option { let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); Some(match (home, toolchain) { (Some(home), Some(toolchain)) => { - // Check that at runtime, we are still in this toolchain. - let toolchain_runtime = - env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN")); - if !matches!(toolchain_runtime, Some(r) if r == toolchain) { - show_error(format!( - "This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\ + // Check that at runtime, we are still in this toolchain (if there is any toolchain). + if let Some(toolchain_runtime) = + env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN")) + { + if toolchain_runtime != toolchain { + show_error(format!( + "This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\ Make sure to run Miri in the toolchain it got built with, e.g. via `cargo +{toolchain} miri`." - )); + )); + } } - format!("{}/toolchains/{}", home, toolchain) } _ => option_env!("RUST_SYSROOT") From 929712c49c112e20332cbb87acc3f6941450979b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Jul 2022 09:36:59 -0400 Subject: [PATCH 3/5] reduce chance of RUSTC collisions --- cargo-miri/bin.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cargo-miri/bin.rs b/cargo-miri/bin.rs index d2c772d7d9..53d348c16d 100644 --- a/cargo-miri/bin.rs +++ b/cargo-miri/bin.rs @@ -671,7 +671,10 @@ fn phase_cargo_miri(mut args: env::Args) { // (https://github.com/rust-lang/cargo/issues/10885). There is no good way to single out these invocations; // some build scripts use the RUSTC env var as well. So we set it directly to the `miri` driver and // hope that all they do is ask for the version number -- things could quickly go downhill from here. - cmd.env("RUSTC", &find_miri()); + // In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc + // or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that + // there would be a collision. + cmd.env("RUSTC", &fs::canonicalize(find_miri()).unwrap()); let runner_env_name = |triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_")); From 0a9feb3c9f93eb66d75a731dfd41151af16252b5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Jul 2022 09:58:00 -0400 Subject: [PATCH 4/5] some more debug output --- cargo-miri/bin.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cargo-miri/bin.rs b/cargo-miri/bin.rs index 53d348c16d..9c964829dd 100644 --- a/cargo-miri/bin.rs +++ b/cargo-miri/bin.rs @@ -820,10 +820,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) { if verbose > 0 { eprintln!( - "[cargo-miri rustc] captured input:\n{}", + "[cargo-miri rustc inside rustdoc] captured input:\n{}", std::str::from_utf8(&env.stdin).unwrap() ); - eprintln!("[cargo-miri rustc] {:?}", cmd); + eprintln!("[cargo-miri rustc inside rustdoc] going to run:\n{:?}", cmd); } exec_with_pipe(cmd, &env.stdin); @@ -906,7 +906,10 @@ fn phase_rustc(mut args: env::Args, phase: RustcPhase) { // Run it. if verbose > 0 { - eprint!("[cargo-miri rustc] "); + eprintln!( + "[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate} print={print}" + ); + eprintln!("[cargo-miri rustc] going to run:"); if verbose > 1 { for (key, value) in env_vars_from_cmd(&cmd) { eprintln!("{key}={value:?} \\"); From bb52965b73bb5885591fdd5b596a8d8f36d75f4e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 21 Jul 2022 10:36:55 -0400 Subject: [PATCH 5/5] make the find_miri returned path actually exist --- cargo-miri/bin.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cargo-miri/bin.rs b/cargo-miri/bin.rs index 9c964829dd..48beb7c935 100644 --- a/cargo-miri/bin.rs +++ b/cargo-miri/bin.rs @@ -212,7 +212,11 @@ fn find_miri() -> PathBuf { return path.into(); } let mut path = std::env::current_exe().expect("current executable path invalid"); - path.set_file_name("miri"); + if cfg!(windows) { + path.set_file_name("miri.exe"); + } else { + path.set_file_name("miri"); + } path }