1
1
# Miri
2
2
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:
8
6
9
7
* Out-of-bounds memory accesses and use-after-free
10
8
* Invalid use of uninitialized data
11
9
* Violation of intrinsic preconditions (an [ ` unreachable_unchecked ` ] being
12
10
reached, calling [ ` copy_nonoverlapping ` ] with overlapping ranges, ...)
13
11
* 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,
15
13
or an invalid enum discriminant)
16
14
* ** Experimental** : Violations of the [ Stacked Borrows] rules governing aliasing
17
15
for reference types
18
16
* ** Experimental** : Violations of the [ Tree Borrows] aliasing rules, as an optional
19
17
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.
21
20
22
21
On top of that, Miri will also tell you about memory leaks: when there is memory
23
22
still allocated at the end of the execution, and that memory is not reachable
24
23
from a global ` static ` , Miri will raise an error.
25
24
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
-
30
25
You can use Miri to emulate programs on other targets, e.g. to ensure that
31
26
byte-level data manipulation works correctly both on little-endian and
32
27
big-endian systems. See
33
28
[ cross-interpretation] ( #cross-interpretation-running-for-different-targets )
34
29
below.
35
30
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
37
32
found a bug with Miri, we'd appreciate if you tell us and we'll add it to the
38
33
list!
39
34
@@ -45,33 +40,36 @@ clocks, are replaced by deterministic "fake" implementations. Set
45
40
(In particular, the "fake" system RNG APIs make Miri ** not suited for
46
41
cryptographic use** ! Do not generate keys using Miri.)
47
42
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
44
+ specification** in your program, not least because there is no such specification.
45
+ Miri uses its own approximation of what is and is not Undefined Behavior in Rust.
46
+ To the best of our knowledge, all Undefined Behavior that has the potential to
47
+ affect a program's correctness * is* being detected by Miri, but you should consult
48
+ [ the Reference] [ reference-ub ] for the official definition of Undefined Behavior.
49
+ Miri will be updated with the Rust compiler to protect against UB as it is understood by the
50
+ current compiler, but it makes no promises about future versions of rustc.
50
51
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:
55
53
56
- In particular, Miri does not check that references point to valid data.
57
54
* If the program relies on unspecified details of how data is laid out, it will
58
55
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.)
60
58
* Program execution is non-deterministic when it depends, for example, on where
61
59
exactly in memory allocations end up, or on the exact interleaving of
62
60
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.
66
64
* Miri runs the program as a platform-independent interpreter, so the program
67
65
has no access to most platform-specific APIs or FFI. A few APIs have been
68
66
implemented (such as printing to stdout, accessing environment variables, and
69
67
basic file system access) but most have not: for example, Miri currently does
70
68
not support networking. System API support varies between targets; if you run
71
69
on Windows it is a good idea to use ` --target x86_64-unknown-linux-gnu ` to get
72
70
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
75
73
cannot produce all behaviors possibly observable on real hardware.
76
74
77
75
Moreover, Miri fundamentally cannot tell you whether your code is * sound* . [ Soundness] is the property
@@ -87,6 +85,7 @@ coverage.
87
85
[ Stacked Borrows ] : https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md
88
86
[ Tree Borrows ] : https://perso.crans.org/vanille/treebor/
89
87
[ 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
90
89
91
90
92
91
## Using Miri
0 commit comments