2017-09-18 11:16:40 +02:00
|
|
|
# Miri [[slides](https://solson.me/miri-slides.pdf)] [[report](https://solson.me/miri-report.pdf)] [![Build Status](https://travis-ci.org/solson/miri.svg?branch=master)](https://travis-ci.org/solson/miri) [![Windows build status](https://ci.appveyor.com/api/projects/status/github/solson/miri?svg=true)](https://ci.appveyor.com/project/solson63299/miri)
|
2016-02-02 04:47:28 -06:00
|
|
|
|
2016-04-13 18:41:37 -06:00
|
|
|
|
2016-04-13 07:32:32 -06:00
|
|
|
An experimental interpreter for [Rust][rust]'s [mid-level intermediate
|
2016-04-13 18:47:03 -06:00
|
|
|
representation][mir] (MIR). This project began as part of my work for the
|
|
|
|
undergraduate research course at the [University of Saskatchewan][usask].
|
2016-02-02 04:47:28 -06:00
|
|
|
|
2016-06-19 00:04:11 -06:00
|
|
|
## Building Miri
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
I recommend that you install [rustup][rustup] to obtain Rust. Miri comes with a
|
2018-10-23 15:21:19 +00:00
|
|
|
`rust-version` file describing the latest supported nightly version of the Rust
|
|
|
|
compiler toolchain. Then all you have to do is:
|
2018-08-14 00:54:04 +02:00
|
|
|
|
2016-06-19 00:04:11 -06:00
|
|
|
```sh
|
2018-10-23 15:21:19 +00:00
|
|
|
cargo +nightly build
|
2016-04-13 07:32:32 -06:00
|
|
|
```
|
|
|
|
|
2018-10-23 15:21:19 +00:00
|
|
|
with `+nightly` replaced with the appropriate nightly version of Rust.
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
## Running Miri
|
2016-04-13 07:32:32 -06:00
|
|
|
|
|
|
|
```sh
|
2018-10-23 15:21:19 +00:00
|
|
|
cargo +nightly run tests/run-pass/vecs.rs # Or whatever test you like.
|
2016-04-13 07:32:32 -06:00
|
|
|
```
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
## Running Miri with full libstd
|
2017-05-24 15:36:48 -07:00
|
|
|
|
2018-07-15 23:17:06 +02:00
|
|
|
Per default libstd does not contain the MIR of non-polymorphic functions. When
|
2018-08-14 09:35:00 +02:00
|
|
|
Miri hits a call to such a function, execution terminates. To fix this, it is
|
2017-05-24 15:36:48 -07:00
|
|
|
possible to compile libstd with full MIR:
|
|
|
|
|
|
|
|
```sh
|
2018-10-23 15:21:19 +00:00
|
|
|
rustup component add --toolchain nightly rust-src
|
|
|
|
cargo +nightly install xargo
|
|
|
|
rustup run nightly xargo/build.sh
|
2017-05-24 15:36:48 -07:00
|
|
|
```
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
Now you can run Miri against the libstd compiled by xargo:
|
2017-05-24 15:36:48 -07:00
|
|
|
|
|
|
|
```sh
|
2018-10-23 15:21:19 +00:00
|
|
|
MIRI_SYSROOT=~/.xargo/HOST cargo +nightly run tests/run-pass-fullmir/hashmap.rs
|
2017-05-24 15:36:48 -07:00
|
|
|
```
|
|
|
|
|
|
|
|
Notice that you will have to re-run the last step of the preparations above when
|
2017-05-30 13:19:15 -07:00
|
|
|
your toolchain changes (e.g., when you update the nightly).
|
2017-05-24 15:36:48 -07:00
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
You can also set `-Zmiri-start-fn` to make Miri start evaluation with the
|
2018-07-11 19:39:09 +02:00
|
|
|
`start_fn` lang item, instead of starting at the `main` function.
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
## Running Miri on your own project('s test suite)
|
|
|
|
|
2018-10-23 15:21:19 +00:00
|
|
|
Install Miri as a cargo subcommand with `cargo install +nightly --all-features --path .`.
|
2018-08-14 09:35:00 +02:00
|
|
|
|
2018-10-15 18:45:55 +00:00
|
|
|
Compile your project and its dependencies against a MIR-enabled libstd as described
|
|
|
|
above:
|
|
|
|
|
|
|
|
1. Run `cargo clean` to eliminate any cached dependencies that were built against
|
|
|
|
the non-MIR `libstd`.
|
2018-10-20 16:31:15 +00:00
|
|
|
2. To run all tests in your project through, Miri, use
|
|
|
|
`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri test`.
|
|
|
|
3. If you have a binary project, you can run it through Miri using
|
|
|
|
`MIRI_SYSROOT=~/.xargo/HOST cargo +nightly miri`.
|
2018-10-15 18:45:55 +00:00
|
|
|
|
2018-10-20 16:31:15 +00:00
|
|
|
### Common Problems
|
|
|
|
|
2018-10-23 15:21:19 +00:00
|
|
|
When using the above instructions, you may encounter a number of confusing compiler
|
2018-10-20 16:31:15 +00:00
|
|
|
errors.
|
|
|
|
|
|
|
|
#### "constant evaluation error: no mir for `<function>`"
|
|
|
|
|
2018-10-23 15:21:19 +00:00
|
|
|
You may have forgotten to set `MIRI_SYSROOT` when calling `cargo miri`, and
|
2018-10-20 16:31:15 +00:00
|
|
|
your program called into `std` or `core`. Be sure to set `MIRI_SYSROOT=~/.xargo/HOST`.
|
|
|
|
|
|
|
|
#### "found possibly newer version of crate `std` which `<dependency>` depends on"
|
|
|
|
|
|
|
|
Your build directory may contain artifacts from an earlier build that did/did not
|
|
|
|
have `MIRI_SYSROOT` set. Run `cargo clean` before switching from non-Miri to Miri
|
|
|
|
builds and vice-versa.
|
|
|
|
|
|
|
|
#### "found crate `std` compiled by an incompatible version of rustc"
|
|
|
|
|
2018-10-23 15:21:19 +00:00
|
|
|
You may be running `cargo miri` with a different compiler version than the one
|
2018-10-20 16:31:15 +00:00
|
|
|
used to build the MIR-enabled `std`. Be sure to consistently use the same toolchain,
|
2018-10-23 15:21:19 +00:00
|
|
|
which should be the toolchain specified in the `rust-version` file.
|
2018-08-14 09:35:00 +02:00
|
|
|
|
2018-10-15 09:21:29 +02:00
|
|
|
## Miri `-Z` flags
|
|
|
|
|
|
|
|
Miri adds some extra `-Z` flags to control its behavior:
|
|
|
|
|
|
|
|
* `-Zmiri-start-fn`: This makes interpretation start with `lang_start` (defined
|
|
|
|
in libstd) instead of starting with `main`. Requires full MIR!
|
|
|
|
* `-Zmiri-disable-validation` disables enforcing the validity invariant.
|
|
|
|
|
2018-07-15 23:17:06 +02:00
|
|
|
## Development and Debugging
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
Since the heart of Miri (the main interpreter engine) lives in rustc, working on
|
|
|
|
Miri will often require using a locally built rustc. This includes getting a
|
2018-07-15 23:17:06 +02:00
|
|
|
trace of the execution, as distributed rustc has `trace!` disabled.
|
|
|
|
|
|
|
|
The first-time setup for a local rustc looks as follows:
|
|
|
|
```
|
|
|
|
git clone https://github.com/rust-lang/rust/ rustc
|
|
|
|
cd rustc
|
|
|
|
cp config.toml.example config.toml
|
|
|
|
# Now edit `config.toml` and set `debug-assertions = true`
|
|
|
|
./x.py build src/rustc
|
|
|
|
# You may have to change the architecture in the next command
|
|
|
|
rustup toolchain link custom build/x86_64-unknown-linux-gnu/stage2
|
2018-08-14 09:35:00 +02:00
|
|
|
# Now cd to your Miri directory
|
2018-07-15 23:17:06 +02:00
|
|
|
rustup override set custom
|
|
|
|
```
|
|
|
|
The `build` step can take 30 minutes and more.
|
|
|
|
|
2018-08-14 09:35:00 +02:00
|
|
|
Now you can `cargo build` Miri, and you can `cargo test --tests`. (`--tests`
|
2018-07-15 23:17:06 +02:00
|
|
|
is needed to skip doctests because we have not built rustdoc for your custom
|
|
|
|
toolchain.) You can also set `RUST_LOG=rustc_mir::interpret=trace` as
|
|
|
|
environment variable to get a step-by-step trace.
|
|
|
|
|
|
|
|
If you changed something in rustc and want to re-build, run
|
|
|
|
```
|
|
|
|
./x.py build src/rustc --keep-stage 0
|
|
|
|
```
|
|
|
|
This avoids rebuilding the entire stage 0, which can save a lot of time.
|
|
|
|
|
2016-06-19 00:04:11 -06:00
|
|
|
## Contributing and getting help
|
2016-04-14 10:21:32 +02:00
|
|
|
|
2016-06-19 00:04:11 -06:00
|
|
|
Check out the issues on this GitHub repository for some ideas. There's lots that
|
|
|
|
needs to be done that I haven't documented in the issues yet, however. For more
|
|
|
|
ideas or help with running or hacking on Miri, you can contact me (`scott`) on
|
2016-06-19 00:15:03 -06:00
|
|
|
Mozilla IRC in any of the Rust IRC channels (`#rust`, `#rust-offtopic`, etc).
|
2016-02-02 04:47:28 -06:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed under either of
|
|
|
|
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
* MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
http://opensource.org/licenses/MIT) at your option.
|
|
|
|
|
|
|
|
### Contribution
|
|
|
|
|
|
|
|
Unless you explicitly state otherwise, any contribution intentionally submitted
|
|
|
|
for inclusion in the work by you shall be dual licensed as above, without any
|
|
|
|
additional terms or conditions.
|
2016-04-13 07:32:32 -06:00
|
|
|
|
|
|
|
[rust]: https://www.rust-lang.org/
|
|
|
|
[mir]: https://github.com/rust-lang/rfcs/blob/master/text/1211-mir.md
|
|
|
|
[usask]: https://www.usask.ca/
|
2016-04-15 16:50:47 +02:00
|
|
|
[rustup]: https://www.rustup.rs
|