From 31a57557f6fc9319ef8cb8f18ad1f22690d92d39 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 4 Oct 2023 22:11:15 +0200 Subject: [PATCH] auto-detect no_std where possible --- src/tools/miri/CONTRIBUTING.md | 17 +++++------------ src/tools/miri/README.md | 6 ++++-- src/tools/miri/cargo-miri/src/setup.rs | 12 +++++++++++- src/tools/miri/ci.sh | 2 +- src/tools/miri/tests/compiletest.rs | 4 +--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/tools/miri/CONTRIBUTING.md b/src/tools/miri/CONTRIBUTING.md index 4e50b8fae81ce..40a4332cdb9c0 100644 --- a/src/tools/miri/CONTRIBUTING.md +++ b/src/tools/miri/CONTRIBUTING.md @@ -71,19 +71,12 @@ and you can (cross-)run the entire test suite using: MIRI_TEST_TARGET=i686-unknown-linux-gnu ./miri test ``` -If your target doesn't support libstd, you can run miri with +If your target doesn't support libstd that should usually just work. However, if you are using a +custom target file, you might have to set `MIRI_NO_STD=1`. -``` -MIRI_NO_STD=1 MIRI_TEST_TARGET=thumbv7em-none-eabihf ./miri test tests/fail/alloc/no_global_allocator.rs -MIRI_NO_STD=1 ./miri run tests/pass/no_std.rs --target thumbv7em-none-eabihf -``` - -to avoid attempting (and failing) to build libstd. Note that almost no tests will pass -this way, but you can run individual tests. - -`./miri test FILTER` only runs those tests that contain `FILTER` in their -filename (including the base directory, e.g. `./miri test fail` will run all -compile-fail tests). +`./miri test FILTER` only runs those tests that contain `FILTER` in their filename (including the +base directory, e.g. `./miri test fail` will run all compile-fail tests). These filters are passed +to `cargo test`, so for multiple filers you need to use `./miri test -- FILTER1 FILTER2`. You can get a trace of which MIR statements are being executed by setting the `MIRI_LOG` environment variable. For example: diff --git a/src/tools/miri/README.md b/src/tools/miri/README.md index 6127eada2221e..ca2bb80bd26d3 100644 --- a/src/tools/miri/README.md +++ b/src/tools/miri/README.md @@ -480,8 +480,10 @@ Moreover, Miri recognizes some environment variables: purpose. * `MIRI_TEST_THREADS` (recognized by the test suite): set the number of threads to use for running tests. By default the number of cores is used. -* `MIRI_NO_STD` (recognized by `cargo miri` and the test suite) makes sure that the target's - sysroot is built without libstd. This allows testing and running no_std programs. +* `MIRI_NO_STD` (recognized by `cargo miri`) makes sure that the target's sysroot is built without + libstd. This allows testing and running no_std programs. + (Miri has a heuristic to detect no-std targets based on the target name; this environment variable + is only needed when that heuristic fails.) * `RUSTC_BLESS` (recognized by the test suite and `cargo-miri-test/run-test.py`): overwrite all `stderr` and `stdout` files instead of checking whether the output matches. * `MIRI_SKIP_UI_CHECKS` (recognized by the test suite): don't check whether the diff --git a/src/tools/miri/cargo-miri/src/setup.rs b/src/tools/miri/cargo-miri/src/setup.rs index 77cecddcb8b14..d921741d5dec2 100644 --- a/src/tools/miri/cargo-miri/src/setup.rs +++ b/src/tools/miri/cargo-miri/src/setup.rs @@ -74,7 +74,17 @@ pub fn setup( } }; // Sysroot configuration and build details. - let sysroot_config = if std::env::var_os("MIRI_NO_STD").is_some() { + let no_std = match std::env::var_os("MIRI_NO_STD") { + None => + // No-std heuristic taken from rust/src/bootstrap/config.rs + // (https://github.com/rust-lang/rust/blob/25b5af1b3a0b9e2c0c57b223b2d0e3e203869b2c/src/bootstrap/config.rs#L549-L555). + target.contains("-none") + || target.contains("nvptx") + || target.contains("switch") + || target.contains("-uefi"), + Some(val) => val != "0", + }; + let sysroot_config = if no_std { SysrootConfig::NoStd } else { SysrootConfig::WithStd { diff --git a/src/tools/miri/ci.sh b/src/tools/miri/ci.sh index 9e7779e3513d9..1b3ed796c665b 100755 --- a/src/tools/miri/ci.sh +++ b/src/tools/miri/ci.sh @@ -112,7 +112,7 @@ case $HOST_TARGET in MIRI_TEST_TARGET=aarch64-linux-android run_tests_minimal hello integer vec panic/panic MIRI_TEST_TARGET=wasm32-wasi run_tests_minimal no_std integer strings MIRI_TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std integer strings - MIRI_TEST_TARGET=thumbv7em-none-eabihf MIRI_NO_STD=1 run_tests_minimal no_std # no_std embedded architecture + MIRI_TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std # no_std embedded architecture MIRI_TEST_TARGET=tests/avr.json MIRI_NO_STD=1 run_tests_minimal no_std # JSON target file ;; x86_64-apple-darwin) diff --git a/src/tools/miri/tests/compiletest.rs b/src/tools/miri/tests/compiletest.rs index 035846f0ef952..c2dccf81377e4 100644 --- a/src/tools/miri/tests/compiletest.rs +++ b/src/tools/miri/tests/compiletest.rs @@ -93,9 +93,7 @@ fn test_config(target: &str, path: &str, mode: Mode, with_dependencies: bool) -> ..Config::rustc(path) }; - let use_std = env::var_os("MIRI_NO_STD").is_none(); - - if with_dependencies && use_std { + if with_dependencies { config.dependencies_crate_manifest_path = Some(Path::new("test_dependencies").join("Cargo.toml")); let mut builder_args = vec!["run".into()];