Skip to content

Commit 720ff0d

Browse files
committed
Auto merge of rust-lang#3583 - RalfJung:readme, r=saethlin
README: update introduction For some time now, we have (to my knowledge) been able to detect all the UB that rustc actually exploits. I think it's time to advertise that. We also haven't had the problem of "not every nightly has Miri" for a while. `@rust-lang/miri` what do you think?
2 parents 4d6d9a9 + 34f64cd commit 720ff0d

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

src/tools/miri/README.md

+31-52
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
# Miri
22

3-
An experimental interpreter for [Rust][rust]'s
4-
[mid-level intermediate representation][mir] (MIR). It can run binaries and
5-
test suites of cargo projects and detect certain classes of
6-
[undefined behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html),
7-
for example:
3+
Miri is an [Undefined Behavior][reference-ub] detection tool for Rust. It can run binaries and test
4+
suites of cargo projects and detect unsafe code that fails to uphold its safety requirements. For
5+
instance:
86

97
* Out-of-bounds memory accesses and use-after-free
108
* Invalid use of uninitialized data
119
* Violation of intrinsic preconditions (an [`unreachable_unchecked`] being
1210
reached, calling [`copy_nonoverlapping`] with overlapping ranges, ...)
1311
* Not sufficiently aligned memory accesses and references
14-
* Violation of *some* basic type invariants (a `bool` that is not 0 or 1, for example,
12+
* Violation of basic type invariants (a `bool` that is not 0 or 1, for example,
1513
or an invalid enum discriminant)
1614
* **Experimental**: Violations of the [Stacked Borrows] rules governing aliasing
1715
for reference types
1816
* **Experimental**: Violations of the [Tree Borrows] aliasing rules, as an optional
1917
alternative to [Stacked Borrows]
20-
* **Experimental**: Data races
18+
* **Experimental**: Data races and emulation of weak memory effects, i.e.,
19+
atomic reads can return outdated values.
2120

2221
On top of that, Miri will also tell you about memory leaks: when there is memory
2322
still allocated at the end of the execution, and that memory is not reachable
2423
from a global `static`, Miri will raise an error.
2524

26-
Miri supports almost all Rust language features; in particular, unwinding and
27-
concurrency are properly supported (including some experimental emulation of
28-
weak memory effects, i.e., reads can return outdated values).
29-
3025
You can use Miri to emulate programs on other targets, e.g. to ensure that
3126
byte-level data manipulation works correctly both on little-endian and
3227
big-endian systems. See
3328
[cross-interpretation](#cross-interpretation-running-for-different-targets)
3429
below.
3530

36-
Miri has already discovered some [real-world bugs](#bugs-found-by-miri). If you
31+
Miri has already discovered many [real-world bugs](#bugs-found-by-miri). If you
3732
found a bug with Miri, we'd appreciate if you tell us and we'll add it to the
3833
list!
3934

@@ -45,33 +40,36 @@ clocks, are replaced by deterministic "fake" implementations. Set
4540
(In particular, the "fake" system RNG APIs make Miri **not suited for
4641
cryptographic use**! Do not generate keys using Miri.)
4742

48-
All that said, be aware that Miri will **not catch all cases of undefined
49-
behavior** in your program, and cannot run all programs:
43+
All that said, be aware that Miri does **not catch every violation of the Rust specification** in
44+
your program, not least because there is no such specification. Miri uses its own approximation of
45+
what is and is not Undefined Behavior in Rust. To the best of our knowledge, all Undefined Behavior
46+
that has the potential to affect a program's correctness *is* being detected by Miri (modulo
47+
[bugs][I-misses-ub]), but you should consult [the Reference][reference-ub] for the official
48+
definition of Undefined Behavior. Miri will be updated with the Rust compiler to protect against UB
49+
as it is understood by the current compiler, but it makes no promises about future versions of
50+
rustc.
5051

51-
* There are still plenty of open questions around the basic invariants for some
52-
types and when these invariants even have to hold. Miri tries to avoid false
53-
positives here, so if your program runs fine in Miri right now that is by no
54-
means a guarantee that it is UB-free when these questions get answered.
52+
Further caveats that Miri users should be aware of:
5553

56-
In particular, Miri does not check that references point to valid data.
5754
* If the program relies on unspecified details of how data is laid out, it will
5855
still run fine in Miri -- but might break (including causing UB) on different
59-
compiler versions or different platforms.
56+
compiler versions or different platforms. (You can use `-Zrandomize-layout`
57+
to detect some of these cases.)
6058
* Program execution is non-deterministic when it depends, for example, on where
6159
exactly in memory allocations end up, or on the exact interleaving of
6260
concurrent threads. Miri tests one of many possible executions of your
63-
program. You can alleviate this to some extent by running Miri with different
64-
values for `-Zmiri-seed`, but that will still by far not explore all possible
65-
executions.
61+
program, but it will miss bugs that only occur in a different possible execution.
62+
You can alleviate this to some extent by running Miri with different
63+
values for `-Zmiri-seed`, but that will still by far not explore all possible executions.
6664
* Miri runs the program as a platform-independent interpreter, so the program
6765
has no access to most platform-specific APIs or FFI. A few APIs have been
6866
implemented (such as printing to stdout, accessing environment variables, and
6967
basic file system access) but most have not: for example, Miri currently does
7068
not support networking. System API support varies between targets; if you run
7169
on Windows it is a good idea to use `--target x86_64-unknown-linux-gnu` to get
7270
better support.
73-
* Weak memory emulation may [produce weak behaviours](https://github.com/rust-lang/miri/issues/2301)
74-
unobservable by compiled programs running on real hardware when `SeqCst` fences are used, and it
71+
* Weak memory emulation may [produce weak behaviors](https://github.com/rust-lang/miri/issues/2301)
72+
when `SeqCst` fences are used that are not actually permitted by the Rust memory model, and it
7573
cannot produce all behaviors possibly observable on real hardware.
7674

7775
Moreover, Miri fundamentally cannot tell you whether your code is *sound*. [Soundness] is the property
@@ -87,6 +85,8 @@ coverage.
8785
[Stacked Borrows]: https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md
8886
[Tree Borrows]: https://perso.crans.org/vanille/treebor/
8987
[Soundness]: https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library
88+
[reference-ub]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
89+
[I-misses-ub]: https://github.com/rust-lang/miri/labels/I-misses-UB
9090

9191

9292
## Using Miri
@@ -97,14 +97,8 @@ Install Miri on Rust nightly via `rustup`:
9797
rustup +nightly component add miri
9898
```
9999

100-
If `rustup` says the `miri` component is unavailable, that's because not all
101-
nightly releases come with all tools. Check out
102-
[this website](https://rust-lang.github.io/rustup-components-history) to
103-
determine a nightly version that comes with Miri and install that using `rustup
104-
toolchain install nightly-YYYY-MM-DD`. Either way, all of the following commands
105-
assume the right toolchain is pinned via `rustup override set nightly` or
106-
`rustup override set nightly-YYYY-MM-DD`. (Alternatively, use `cargo
107-
+nightly`/`cargo +nightly-YYYY-MM-DD` for each of the following commands.)
100+
All the following commands assume the nightly toolchain is pinned via `rustup override set nightly`.
101+
Alternatively, use `cargo +nightly` for each of the following commands.
108102

109103
Now you can run your project in Miri:
110104

@@ -118,12 +112,12 @@ dependencies. It will ask you for confirmation before installing anything.
118112
example, `cargo miri test filter` only runs the tests containing `filter` in
119113
their name.
120114

121-
You can pass arguments to Miri via `MIRIFLAGS`. For example,
115+
You can pass [flags][miri-flags] to Miri via `MIRIFLAGS`. For example,
122116
`MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run` runs the program
123117
without checking the aliasing of references.
124118

125119
When compiling code via `cargo miri`, the `cfg(miri)` config flag is set for code
126-
that will be interpret under Miri. You can use this to ignore test cases that fail
120+
that will be interpreted under Miri. You can use this to ignore test cases that fail
127121
under Miri because they do things Miri does not support:
128122

129123
```rust
@@ -159,10 +153,8 @@ endian-sensitive code.
159153

160154
### Running Miri on CI
161155

162-
To run Miri on CI, make sure that you handle the case where the latest nightly
163-
does not ship the Miri component because it currently does not build. `rustup
164-
toolchain install --component` knows how to handle this situation, so the
165-
following snippet should always work:
156+
When running Miri on CI, use the following snippet to install a nightly toolchain with the Miri
157+
component:
166158

167159
```sh
168160
rustup toolchain install nightly --component miri
@@ -273,25 +265,12 @@ To get a backtrace, you need to disable isolation
273265
RUST_BACKTRACE=1 MIRIFLAGS="-Zmiri-disable-isolation" cargo miri test
274266
```
275267

276-
#### "found possibly newer version of crate `std` which `<dependency>` depends on"
277-
278-
Your build directory may contain artifacts from an earlier build that have/have
279-
not been built for Miri. Run `cargo clean` before switching from non-Miri to
280-
Miri builds and vice-versa.
281-
282268
#### "found crate `std` compiled by an incompatible version of rustc"
283269

284270
You may be running `cargo miri` with a different compiler version than the one
285271
used to build the custom libstd that Miri uses, and Miri failed to detect that.
286272
Try running `cargo miri clean`.
287273

288-
#### "no mir for `std::rt::lang_start_internal`"
289-
290-
This means the sysroot you are using was not compiled with Miri in mind. This
291-
should never happen when you use `cargo miri` because that takes care of setting
292-
up the sysroot. If you are using `miri` (the Miri driver) directly, see the
293-
[contributors' guide](CONTRIBUTING.md) for how to use `./miri` to best do that.
294-
295274

296275
## Miri `-Z` flags and environment variables
297276
[miri-flags]: #miri--z-flags-and-environment-variables

0 commit comments

Comments
 (0)