Skip to content

Commit ce29fbf

Browse files
committed
Auto merge of #1540 - RalfJung:cargo-miri-redone, r=oli-obk
Redo cargo-miri logic This rewrite the cargo-miri logic for running the requested crate(s) following what we outlined in #739: `cargo miri run/test $FLAGS` (almost) literally invokes `cargo run/test $FLAGS` but with some environment variables set so that we can control what happens: * `RUSTC_WRAPPER` is set so that we get invoked instead of `rustc`. We use that power to mess with the flags being used for the build (things to be interpreted by Miri use a different sysroot), and when we are detecting a binary crate meant to be run by Miri, we grab the info we care about and put it into a JSON file for later use. * `CARGO_TARGET_$TARGET_RUNNER` is set so what we get invoked when cargo wants to run a binary. At that point we take that JSON info from before and use it to invoke Miri. Overall this works great! We get all sorts of cargo magic for free, and do not even need `cargo-metadata` any more. There's one annoying point though, which I have not managed to entirely work around yet: this means we are doing a full build, not just a check-build. Given that our sysroot is MIR-only, I was surprised that this even worked, but still -- this means we are doing more work than we should. So I also added some patching of arguments to make sure `--emit` does not contain `link`, and then more patching was required of the `--extern` flags for the binary because those referenced the `.rlib` files but now only `.rmeta` exists, and that is still not fully working because cargo seems to expect those `.rmeta` files and now triggers a rebuild each time as those files are still missing. My current plan is to make our wrapper create some empty dummy files with the right names, but the amount of hacks we are stacking on top of each other here is getting worrysome.^^ `@ehuss` your input would be welcome on this issue. Due to re-using cargo more literally, this also changes flag parsing to match `cargo`. So `-Zmiri` flags now have to be passed via an environment variable (Cc #1416). This PR is not ready yet, but the parts that are there I think can be reviewed already. TODO: * [x] [Fix Windows](#1540 (comment)). * [x] Figure out how we can do check-only builds without the rebuild problem above. ~~I am also worried about cases where `build.rs` script might detect check-only builds and then do less work; I think some crates in rustc are doing that and if this is a thing in the wider ecosystem we need to find a way to support this as well.~~ (postponed that until we have a concrete example) * [x] Currently cargo runs doctests as well, and they are not run in Miri. We should at least figure out a way to not run them at all (resolving #584 is left for a future PR). * [x] For some time, detect the old way of passing `-Zmiri` flags and warn that this will need updating. For some simple situations we can probably make it still support the old way, but I plan to remove those hacks after a bit. This is just to give people and me time to go around and send PRs to all projects that use Miri on CI, and update their use of the flags. * [x] Add a test for stdin handling (#1505). This should work now but we should be sure. * [x] Add a test for cargo env vars (#1515). * [x] Check if #1516 is resolved. * [x] Check if #1001 and #1514 are resolved. * [x] Check if #1312 is resolved (not sure if it is wort adding a test). * [x] Check if #1512 is resolved (not sure if it is wort adding a test). Fixes #700. Fixes #739. Fixes #1001. Fixes #1312 (without a test, as we run without cargo's stdin/stdout wrapping now, as the test for stdin confirms). Fixes #1416. Fixes #1505. Fixes #1512 (without a test, as cargo now does all that handling anyway, which various other tests confirm). Fixes #1514. Fixes #1516. Cc `@alecmocatta` who reported many of the bugs above; would be great if you could help with the tests e.g. by providing some small examples I could try. r? `@oli-obk`
2 parents 79f023e + ae859c3 commit ce29fbf

31 files changed

+598
-416
lines changed

README.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@ Now you can run your project in Miri:
8383
The first time you run Miri, it will perform some extra setup and install some
8484
dependencies. It will ask you for confirmation before installing anything.
8585

86-
You can pass arguments to Miri after the first `--`, and pass arguments to the
87-
interpreted program or test suite after the second `--`. For example, `cargo
88-
miri run -- -Zmiri-disable-stacked-borrows` runs the program without checking
89-
the aliasing of references. To filter the tests being run, use `cargo miri test
90-
-- -- filter`.
86+
`cargo miri run/test` supports the exact same flags as `cargo run/test`. You
87+
can pass arguments to Miri via `MIRIFLAGS`. For example,
88+
`MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run` runs the program
89+
without checking the aliasing of references.
9190

9291
Miri supports cross-execution: if you want to run the program as if it was a
9392
Linux program, you can do `cargo miri run --target x86_64-unknown-linux-gnu`.
@@ -163,7 +162,8 @@ up the sysroot. If you are using `miri` (the Miri driver) directly, see the
163162
## Miri `-Z` flags and environment variables
164163
[miri-flags]: #miri--z-flags-and-environment-variables
165164

166-
Miri adds its own set of `-Z` flags:
165+
Miri adds its own set of `-Z` flags, which are usually set via the `MIRIFLAGS`
166+
environment variable:
167167

168168
* `-Zmiri-disable-alignment-check` disables checking pointer alignment, so you
169169
can focus on other failures, but it means Miri can miss bugs in your program.
@@ -229,21 +229,25 @@ Moreover, Miri recognizes some environment variables:
229229

230230
* `MIRI_LOG`, `MIRI_BACKTRACE` control logging and backtrace printing during
231231
Miri executions, also [see above][testing-miri].
232+
* `MIRIFLAGS` (recognized by `cargo miri` and the test suite) defines extra
233+
flags to be passed to Miri.
232234
* `MIRI_SYSROOT` (recognized by `cargo miri` and the test suite)
233235
indicates the sysroot to use. To do the same thing with `miri`
234236
directly, use the `--sysroot` flag.
235237
* `MIRI_TEST_TARGET` (recognized by the test suite) indicates which target
236238
architecture to test against. `miri` and `cargo miri` accept the `--target`
237239
flag for the same purpose.
238-
* `MIRI_TEST_FLAGS` (recognized by the test suite) defines extra flags to be
239-
passed to Miri.
240240

241241
The following environment variables are internal, but used to communicate between
242242
different Miri binaries, and as such worth documenting:
243243

244244
* `MIRI_BE_RUSTC` when set to any value tells the Miri driver to actually not
245245
interpret the code but compile it like rustc would. This is useful to be sure
246246
that the compiled `rlib`s are compatible with Miri.
247+
* `MIRI_CWD` when set to any value tells the Miri driver to change to the given
248+
directory after loading all the source files, but before commencing
249+
interpretation. This is useful if the interpreted program wants a different
250+
working directory at run-time than at build-time.
247251

248252
## Miri `extern` functions
249253

cargo-miri/Cargo.lock

+1-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-miri/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ test = false # we have no unit tests
1414
doctest = false # and no doc tests
1515

1616
[dependencies]
17-
cargo_metadata = "0.11"
1817
directories = "2.0"
1918
rustc_version = "0.2.3"
2019
serde_json = "1.0.40"

0 commit comments

Comments
 (0)