]> git.lizzy.rs Git - rust.git/blob - README.md
install directly from git repo
[rust.git] / README.md
1 # Miri [![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)
2
3
4 An experimental interpreter for [Rust][rust]'s
5 [mid-level intermediate representation][mir] (MIR).  It can run binaries and
6 test suites of cargo projects and detect certain classes of undefined behavior,
7 for example:
8
9 * Out-of-bounds memory accesses and use-after-free
10 * Invalid use of uninitialized data
11 * Violation of intrinsic preconditions (an [`unreachable_unchecked`] being
12   reached, calling [`copy_nonoverlapping`] with overlapping ranges, ...)
13 * Not sufficiently aligned memory accesses and references
14 * Violation of basic type invariants (a `bool` that is not 0 or 1, for example,
15   or an invalid enum discriminant)
16 * WIP: Violations of the rules governing aliasing for reference types
17
18 [rust]: https://www.rust-lang.org/
19 [mir]: https://github.com/rust-lang/rfcs/blob/master/text/1211-mir.md
20 [`unreachable_unchecked`]: https://doc.rust-lang.org/stable/std/hint/fn.unreachable_unchecked.html
21 [`copy_nonoverlapping`]: https://doc.rust-lang.org/stable/std/ptr/fn.copy_nonoverlapping.html
22
23 ## Building Miri
24
25 We recommend that you install [rustup] to obtain Rust. Then all you have
26 to do is:
27
28 ```sh
29 cargo +nightly build
30 ```
31
32 This uses the very latest Rust version.  If you experience any problem, refer to
33 the `rust-version` file which contains a particular Rust nightly version that
34 has been tested against the version of miri you are using.  Make sure to use
35 that particular `nightly-YYYY-MM-DD` whenever the instructions just say
36 `nightly`.
37
38 To avoid repeating the nightly version all the time, you can use
39 `rustup override set nightly` (or `rustup override set nightly-YYYY-MM-DD`),
40 which means `nightly` Rust will automatically be used whenever you are working
41 in this directory.
42
43 [rustup]: https://www.rustup.rs
44
45 ## Running Miri on tiny examples
46
47 ```sh
48 cargo +nightly run -- -Zmiri-disable-validation tests/run-pass/vecs.rs # Or whatever test you like.
49 ```
50
51 We have to disable validation because that can lead to errors when libstd is not
52 compiled the right way.
53
54 ## Running Miri on your own project('s test suite)
55
56 Install Miri as a cargo subcommand:
57
58 ```sh
59 cargo +nightly install --git https://github.com/solson/miri/
60 ```
61
62 Be aware that if you used `rustup override set` to fix a particular Rust version
63 for the miri directory, that will *not* apply to your own project directory!
64 You have to use a consistent Rust version for building miri and your project for
65 this to work, so remember to either always specify the nightly version manually,
66 overriding it in your project directory as well, or use `rustup default nightly`
67 (or `rustup default nightly-YYYY-MM-DD`) to globally make `nightly` the default
68 toolchain.
69
70 1. Run `cargo clean` to eliminate any cached dependencies.  Miri needs your
71    dependencies to be compiled the right way, that would not happen if they have
72    previously already been compiled.
73 2. To run all tests in your project through Miri, use `cargo +nightly miri test`.
74    **NOTE**: This is currently broken, see the discussion in
75    [#479](https://github.com/solson/miri/issues/479).
76 3. If you have a binary project, you can run it through Miri using `cargo
77    +nightly miri run`.
78
79 ### Common Problems
80
81 When using the above instructions, you may encounter a number of confusing compiler
82 errors.
83
84 #### "found possibly newer version of crate `std` which `<dependency>` depends on"
85
86 Your build directory may contain artifacts from an earlier build that have/have
87 not been built for Miri. Run `cargo clean` before switching from non-Miri to
88 Miri builds and vice-versa.
89
90 #### "found crate `std` compiled by an incompatible version of rustc"
91
92 You may be running `cargo miri` with a different compiler version than the one
93 used to build the custom libstd that Miri uses, and Miri failed to detect that.
94 Try deleting `~/.miri`.
95
96 ## Miri `-Z` flags
97
98 Several `-Z` flags are relevant for miri:
99
100 * `-Zmir-opt-level` controls how many MIR optimizations are performed.  miri
101   overrides the default to be `0`; be advised that using any higher level can
102   make miri miss bugs in your program because they got optimized away.
103 * `-Zalways-encode-mir` makes rustc dump MIR even for completely monomorphic
104   functions.  This is needed so that miri can execute such functions, so miri
105   sets this flag per default.
106 * `-Zmiri-disable-validation` is a custom `-Z` flag added by miri.  It disables
107   enforcing the validity invariant, which is enforced by default.  This is
108   mostly useful for debugging; it means miri will miss bugs in your program.
109
110 ## Development and Debugging
111
112 Since the heart of Miri (the main interpreter engine) lives in rustc, working on
113 Miri will often require using a locally built rustc. This includes getting a
114 trace of the execution, as distributed rustc has `debug!` and `trace!` disabled.
115
116 The first-time setup for a local rustc looks as follows:
117 ```sh
118 git clone https://github.com/rust-lang/rust/ rustc
119 cd rustc
120 cp config.toml.example config.toml
121 # Now edit `config.toml` and set `debug-assertions = true` and `test-miri = true`.
122 # The latter is important to build libstd with the right flags for miri.
123 ./x.py build src/rustc
124 # You may have to change the architecture in the next command
125 rustup toolchain link custom build/x86_64-unknown-linux-gnu/stage2
126 # Now cd to your Miri directory
127 rustup override set custom
128 ```
129 The `build` step can take 30 minutes and more.
130
131 Now you can `cargo build` Miri, and you can `cargo test --release` it.  `cargo
132 test --release FILTER` only runs those tests that contain `FILTER` in their
133 filename (including the base directory, e.g. `cargo test --release fail` will
134 run all compile-fail tests).  We recommend using `--release` to make test
135 running take less time.
136
137 Notice that the "fullmir" tests only run if you have `MIRI_SYSROOT` set, the
138 test runner does not realized that your libstd comes with full MIR.  The
139 following will set it correctly:
140 ```sh
141 MIRI_SYSROOT=$(rustc --print sysroot) cargo test --release
142 ```
143
144 Moreover, you can now run Miri with a trace of all execution steps:
145 ```sh
146 MIRI_LOG=debug cargo run tests/run-pass/vecs.rs
147 ```
148
149 Setting `MIRI_LOG` like this will configure logging for miri itself as well as
150 the `rustc::mir::interpret` and `rustc_mir::interpret` modules in rustc.  You
151 can also do more targeted configuration, e.g. to debug the stacked borrows
152 implementation:
153 ```sh
154 MIRI_LOG=rustc_mir::interpret=debug,miri::stacked_borrows cargo run tests/run-pass/vecs.rs
155 ```
156
157 In addition, you can set `MIRI_BACKTRACE=1` to get a backtrace of where an
158 evaluation error was originally created.
159
160 If you changed something in rustc and want to re-build, run
161 ```
162 ./x.py --keep-stage 0 build src/rustc
163 ```
164 This avoids rebuilding the entire stage 0, which can save a lot of time.
165
166 ## Contributing and getting help
167
168 Check out the issues on this GitHub repository for some ideas. There's lots that
169 needs to be done that I haven't documented in the issues yet, however. For more
170 ideas or help with running or hacking on Miri, you can contact me (`scott`) on
171 Mozilla IRC in any of the Rust IRC channels (`#rust`, `#rust-offtopic`, etc).
172
173 ## History
174
175 This project began as part of an undergraduate research course in 2015 by
176 @solson at the [University of Saskatchewan][usask].  There are [slides] and a
177 [report] available from that project.  In 2016, @oli-obk joined to prepare miri
178 for eventually being used as const evaluator in the Rust compiler itself
179 (basically, for `const` and `static` stuff), replacing the old evaluator that
180 worked directly on the AST.  In 2017, @RalfJung did an internship with Mozilla
181 and began developing miri towards a tool for detecting undefined behavior, and
182 also using miri as a way to explore the consequences of various possible
183 definitions for undefined behavior in Rust.  @oli-obk's move of the miri engine
184 into the compiler finally came to completion in early 2018.  Meanwhile, later
185 that year, @RalfJung did a second internship, developing miri further with
186 support for checking basic type invariants and verifying that references are
187 used according to their aliasing restrictions.
188
189 [usask]: https://www.usask.ca/
190 [slides]: https://solson.me/miri-slides.pdf
191 [report]: https://solson.me/miri-report.pdf
192
193 ## License
194
195 Licensed under either of
196   * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
197     http://www.apache.org/licenses/LICENSE-2.0)
198   * MIT license ([LICENSE-MIT](LICENSE-MIT) or
199     http://opensource.org/licenses/MIT) at your option.
200
201 ### Contribution
202
203 Unless you explicitly state otherwise, any contribution intentionally submitted
204 for inclusion in the work by you shall be dual licensed as above, without any
205 additional terms or conditions.