Skip to content

Commit f42ede2

Browse files
committed
Auto merge of #3411 - RalfJung:sysroot, r=RalfJung
Handle Miri sysroot entirely outside the Miri driver (Extracted from #3409) This entirely moves the responsibility of setting miri-sysroot to whatever *invokes* the Miri driver. cargo-miri knows whether it is inside rustdoc or not and can adjust accordingly. I previously avoided doing that because there are a bunch of places that are invoking the driver (cargo-miri, the ui test suite, `./miri run`, `./x.py run miri`) and they all need to be adjusted now. But it is also somewhat less fragile as we usually have more information there -- and we can just decide that `./miri run file.rs --sysroot path` is not supported. The advantage of this is that the driver is reasonably clean and doesn't need magic environment variables like MIRI_SYSROOT, and we don't have to fight rustc_driver to use a different default sysroot. Everything is done in cargo-miri (and the other much simpler driver wrappers) where it can hopefully be debugged much better.
2 parents fcd2f0b + 8727602 commit f42ede2

File tree

5 files changed

+51
-54
lines changed

5 files changed

+51
-54
lines changed

README.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -451,36 +451,32 @@ Some native rustc `-Z` flags are also very relevant for Miri:
451451
* `-Zmir-emit-retag` controls whether `Retag` statements are emitted. Miri
452452
enables this per default because it is needed for [Stacked Borrows] and [Tree Borrows].
453453

454-
Moreover, Miri recognizes some environment variables:
454+
Moreover, Miri recognizes some environment variables (unless noted otherwise, these are supported
455+
by all intended entry points, i.e. `cargo miri` and `./miri {test,run}`):
455456

456457
* `MIRI_AUTO_OPS` indicates whether the automatic execution of rustfmt, clippy and toolchain setup
457458
should be skipped. If it is set to `no`, they are skipped. This is used to allow automated IDE
458459
actions to avoid the auto ops.
459460
* `MIRI_LOG`, `MIRI_BACKTRACE` control logging and backtrace printing during
460461
Miri executions, also [see "Testing the Miri driver" in `CONTRIBUTING.md`][testing-miri].
461-
* `MIRIFLAGS` (recognized by `cargo miri` and the test suite) defines extra
462-
flags to be passed to Miri.
463-
* `MIRI_LIB_SRC` defines the directory where Miri expects the sources of the
464-
standard library that it will build and use for interpretation. This directory
465-
must point to the `library` subdirectory of a `rust-lang/rust` repository
466-
checkout.
467-
* `MIRI_SYSROOT` (recognized by `cargo miri` and the Miri driver) indicates the sysroot to use. When
468-
using `cargo miri`, this skips the automatic setup -- only set this if you do not want to use the
469-
automatically created sysroot. For directly invoking the Miri driver, this variable (or a
470-
`--sysroot` flag) is mandatory. When invoking `cargo miri setup`, this indicates where the sysroot
471-
will be put.
472-
* `MIRI_TEST_TARGET` (recognized by the test suite and the `./miri` script) indicates which target
462+
* `MIRIFLAGS` defines extra flags to be passed to Miri.
463+
* `MIRI_LIB_SRC` defines the directory where Miri expects the sources of the standard library that
464+
it will build and use for interpretation. This directory must point to the `library` subdirectory
465+
of a `rust-lang/rust` repository checkout.
466+
* `MIRI_SYSROOT` indicates the sysroot to use. When using `cargo miri`, this skips the automatic
467+
setup -- only set this if you do not want to use the automatically created sysroot. When invoking
468+
`cargo miri setup`, this indicates where the sysroot will be put.
469+
* `MIRI_TEST_TARGET` (recognized by `./miri {test,run}`) indicates which target
473470
architecture to test against. `miri` and `cargo miri` accept the `--target` flag for the same
474471
purpose.
475-
* `MIRI_TEST_THREADS` (recognized by the test suite): set the number of threads to use for running tests.
476-
By default the number of cores is used.
477-
* `MIRI_NO_STD` (recognized by `cargo miri`) makes sure that the target's sysroot is built without
478-
libstd. This allows testing and running no_std programs.
479-
(Miri has a heuristic to detect no-std targets based on the target name; this environment variable
480-
is only needed when that heuristic fails.)
481-
* `RUSTC_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all
472+
* `MIRI_TEST_THREADS` (recognized by `./miri test`): set the number of threads to use for running tests.
473+
By default, the number of cores is used.
474+
* `MIRI_NO_STD` makes sure that the target's sysroot is built without libstd. This allows testing
475+
and running no_std programs. (Miri has a heuristic to detect no-std targets based on the target
476+
name; this environment variable is only needed when that heuristic fails.)
477+
* `RUSTC_BLESS` (recognized by `./miri test` and `cargo-miri-test/run-test.py`): overwrite all
482478
`stderr` and `stdout` files instead of checking whether the output matches.
483-
* `MIRI_SKIP_UI_CHECKS` (recognized by the test suite): don't check whether the
479+
* `MIRI_SKIP_UI_CHECKS` (recognized by `./miri test`): don't check whether the
484480
`stderr` or `stdout` files match the actual output.
485481

486482
The following environment variables are *internal* and must not be used by

cargo-miri/src/phases.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
172172
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
173173
cmd.args(args);
174174

175-
// Let it know where the Miri sysroot lives.
176-
cmd.env("MIRI_SYSROOT", miri_sysroot);
177175
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
178176
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish
179177
// the two codepaths. (That extra argument is why we prefer this over setting `RUSTC`.)
@@ -200,10 +198,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
200198
// always applied. However, buggy build scripts (https://github.com/eyre-rs/eyre/issues/84) and
201199
// also cargo (https://github.com/rust-lang/cargo/issues/10885) will invoke `rustc` even when
202200
// `RUSTC_WRAPPER` is set, bypassing the wrapper. To make sure everything is coherent, we want
203-
// that to be the Miri driver, but acting as rustc, on the target level. (Target, rather than
204-
// host, is needed for cross-interpretation situations.) This is not a perfect emulation of real
205-
// rustc (it might be unable to produce binaries since the sysroot is check-only), but it's as
206-
// close as we can get, and it's good enough for autocfg.
201+
// that to be the Miri driver, but acting as rustc, in host mode.
207202
//
208203
// In `main`, we need the value of `RUSTC` to distinguish RUSTC_WRAPPER invocations from rustdoc
209204
// or TARGET_RUNNER invocations, so we canonicalize it here to make it exceedingly unlikely that
@@ -212,14 +207,19 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
212207
// bootstrap `rustc` thing in our way! Instead, we have MIRI_HOST_SYSROOT to use for host
213208
// builds.
214209
cmd.env("RUSTC", fs::canonicalize(find_miri()).unwrap());
215-
cmd.env("MIRI_BE_RUSTC", "target"); // we better remember to *unset* this in the other phases!
210+
// In case we get invoked as RUSTC without the wrapper, let's be a host rustc. This makes no
211+
// sense for cross-interpretation situations, but without the wrapper, this will use the host
212+
// sysroot, so asking it to behave like a target build makes even less sense.
213+
cmd.env("MIRI_BE_RUSTC", "host"); // we better remember to *unset* this in the other phases!
216214

217215
// Set rustdoc to us as well, so we can run doctests.
218216
if let Some(orig_rustdoc) = env::var_os("RUSTDOC") {
219217
cmd.env("MIRI_ORIG_RUSTDOC", orig_rustdoc);
220218
}
221219
cmd.env("RUSTDOC", &cargo_miri_path);
222220

221+
// Forward some crucial information to our own re-invocations.
222+
cmd.env("MIRI_SYSROOT", miri_sysroot);
223223
cmd.env("MIRI_LOCAL_CRATES", local_crates(&metadata));
224224
if verbose > 0 {
225225
cmd.env("MIRI_VERBOSE", verbose.to_string()); // This makes the other phases verbose.
@@ -412,6 +412,9 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
412412
// Arguments are treated very differently depending on whether this crate is
413413
// for interpretation by Miri, or for use by a build script / proc macro.
414414
if target_crate {
415+
// Set the sysroot.
416+
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
417+
415418
// Forward arguments, but patched.
416419
let emit_flag = "--emit";
417420
// This hack helps bootstrap run standard library tests in Miri. The issue is as follows:
@@ -574,6 +577,12 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
574577
cmd.env(name, val);
575578
}
576579

580+
if phase != RunnerPhase::Rustdoc {
581+
// Set the sysroot. Not necessary in rustdoc, where we already set the sysroot when invoking
582+
// rustdoc itself, which will forward that flag when invoking rustc (i.e., us), so the flag
583+
// is present in `info.args`.
584+
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
585+
}
577586
// Forward rustc arguments.
578587
// We need to patch "--extern" filenames because we forced a check-only
579588
// build without cargo knowing about that: replace `.rlib` suffix by

miri-script/src/commands.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::env;
22
use std::ffi::OsString;
33
use std::io::Write;
44
use std::ops::Not;
5+
use std::path::PathBuf;
56
use std::process;
67
use std::thread;
78
use std::time;
@@ -20,10 +21,11 @@ const JOSH_FILTER: &str =
2021
const JOSH_PORT: &str = "42042";
2122

2223
impl MiriEnv {
23-
fn build_miri_sysroot(&mut self, quiet: bool) -> Result<()> {
24-
if self.sh.var("MIRI_SYSROOT").is_ok() {
24+
/// Returns the location of the sysroot.
25+
fn build_miri_sysroot(&mut self, quiet: bool) -> Result<PathBuf> {
26+
if let Some(miri_sysroot) = self.sh.var_os("MIRI_SYSROOT") {
2527
// Sysroot already set, use that.
26-
return Ok(());
28+
return Ok(miri_sysroot.into());
2729
}
2830
let manifest_path = path!(self.miri_dir / "cargo-miri" / "Cargo.toml");
2931
let Self { toolchain, cargo_extra_flags, .. } = &self;
@@ -57,8 +59,8 @@ impl MiriEnv {
5759
.with_context(|| "`cargo miri setup` failed")?;
5860
panic!("`cargo miri setup` didn't fail again the 2nd time?");
5961
};
60-
self.sh.set_var("MIRI_SYSROOT", output);
61-
Ok(())
62+
self.sh.set_var("MIRI_SYSROOT", &output);
63+
Ok(output.into())
6264
}
6365
}
6466

@@ -505,8 +507,10 @@ impl Command {
505507
flags.push("--edition=2021".into()); // keep in sync with `tests/ui.rs`.`
506508
}
507509

508-
// Prepare a sysroot.
509-
e.build_miri_sysroot(/* quiet */ true)?;
510+
// Prepare a sysroot, and add it to the flags.
511+
let miri_sysroot = e.build_miri_sysroot(/* quiet */ true)?;
512+
flags.push("--sysroot".into());
513+
flags.push(miri_sysroot.into());
510514

511515
// Then run the actual command. Also add MIRIFLAGS.
512516
let miri_manifest = path!(e.miri_dir / "Cargo.toml");

src/bin/miri.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -271,25 +271,6 @@ fn run_compiler(
271271
callbacks: &mut (dyn rustc_driver::Callbacks + Send),
272272
using_internal_features: std::sync::Arc<std::sync::atomic::AtomicBool>,
273273
) -> ! {
274-
if target_crate {
275-
// Miri needs a custom sysroot for target crates.
276-
// If no `--sysroot` is given, the `MIRI_SYSROOT` env var is consulted to find where
277-
// that sysroot lives, and that is passed to rustc.
278-
let sysroot_flag = "--sysroot";
279-
if !args.iter().any(|e| e.starts_with(sysroot_flag)) {
280-
// Using the built-in default here would be plain wrong, so we *require*
281-
// the env var to make sure things make sense.
282-
let miri_sysroot = env::var("MIRI_SYSROOT").unwrap_or_else(|_| {
283-
show_error!(
284-
"Miri was invoked in 'target' mode without `MIRI_SYSROOT` or `--sysroot` being set"
285-
)
286-
});
287-
288-
args.push(sysroot_flag.to_owned());
289-
args.push(miri_sysroot);
290-
}
291-
}
292-
293274
// Don't insert `MIRI_DEFAULT_ARGS`, in particular, `--cfg=miri`, if we are building
294275
// a "host" crate. That may cause procedural macros (and probably build scripts) to
295276
// depend on Miri-only symbols, such as `miri_resolve_frame`:

tests/ui.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ fn run_tests(
112112
config.program.envs.push(("RUST_BACKTRACE".into(), Some("1".into())));
113113

114114
// Add some flags we always want.
115+
config.program.args.push(
116+
format!(
117+
"--sysroot={}",
118+
env::var("MIRI_SYSROOT").expect("MIRI_SYSROOT must be set to run the ui test suite")
119+
)
120+
.into(),
121+
);
115122
config.program.args.push("-Dwarnings".into());
116123
config.program.args.push("-Dunused".into());
117124
config.program.args.push("-Ainternal_features".into());

0 commit comments

Comments
 (0)