Skip to content

Commit bf6b7aa

Browse files
committed
rewirte development part of README
1 parent 2481d60 commit bf6b7aa

File tree

1 file changed

+66
-53
lines changed

1 file changed

+66
-53
lines changed

README.md

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -113,90 +113,100 @@ find useful.
113113
### Using a nightly rustc
114114

115115
Miri heavily relies on internal rustc interfaces to execute MIR. Still, some
116-
things (like adding support for a new intrinsic) can be done by working just on
117-
the Miri side.
116+
things (like adding support for a new intrinsic or a shim for an external
117+
function being called) can be done by working just on the Miri side.
118118

119-
To prepare, make sure you are using a nightly Rust compiler. The most
120-
convenient way is to install Miri using cargo, then you can easily run it on
121-
other projects:
122-
123-
```sh
124-
rustup component remove miri # avoid having Miri installed twice
125-
cargo +nightly install --path "$DIR" --force
126-
cargo +nightly miri setup
127-
```
128-
129-
(We are giving `+nightly` explicitly here all the time because it is important
130-
that all of these commands get executed with the same toolchain.)
119+
To prepare, make sure you are using a nightly Rust compiler. Then you should be
120+
able to just `cargo build` Miri.
131121

132122
In case this fails, your nightly might be incompatible with Miri master. The
133123
`rust-version` file contains the commit hash of rustc that Miri is currently
134124
tested against; you can use that to find a nightly that works or you might have
135125
to wait for the next nightly to get released.
136126

137-
If you want to use a different libstd (not the one that comes with the
138-
nightly), you can do that by running
127+
### Testing the Miri driver
128+
[testing-miri]: #testing-the-miri-driver
139129

140-
```sh
141-
XARGO_RUST_SRC=~/src/rust/rustc/src/ cargo +nightly miri setup
130+
The Miri driver in the `miri` binary is the "heart" of Miri: it is basically a
131+
version of `rustc` that, instead of compiling your code, runs it. It accepts
132+
all the same flags as `rustc` (though the ones only affecting code generation
133+
and linking obviously will have no effect) [and more][miri-flags].
134+
135+
To run the Miri driver, you need to have the `MIRI_SYSROOT` environment variable
136+
set to an appropriate sysroot. You can generate such a sysroot with the
137+
following incantation:
138+
139+
```
140+
cargo run --bin cargo-miri -- miri setup
142141
```
143142

144-
Either way, you can now do `cargo +nightly miri run` to run Miri with your
145-
local changes on whatever project you are debugging.
143+
This basically runs the `cargo-miri` binary (which backs the `cargo miri`
144+
subcommand) with `cargo`, and asks it to `setup`. It should in the end print
145+
the directory where the libstd was built. In the following, we will assume it
146+
is `~/.cache/miri/HOST`; you may have to adjust that if you are not using Linux.
146147

147-
`cargo miri setup` should end in printing the directory where the libstd was
148-
built. For the next step to work, set that as your `MIRI_SYSROOT` environment
149-
variable:
148+
Now you can run the driver directly using
150149

151150
```sh
152-
export MIRI_SYSROOT=~/.cache/miri/HOST # or whatever the previous command said
151+
MIRI_SYSROOT=~/.cache/miri/HOST cargo run tests/run-pass/format.rs # or whatever test you like
153152
```
154153

155-
### Testing Miri
154+
and you can run the test suite using
156155

157-
Instead of running an entire project using `cargo miri`, you can also use the
158-
Miri "driver" directly to run just a single file. That can be easier during
159-
debugging.
160-
161-
```sh
162-
cargo run tests/run-pass/format.rs # or whatever test you like
163156
```
157+
cargo test
158+
```
159+
160+
We recommend adding the `--release` flag to make tests run faster.
164161

165-
You can also run the test suite with `cargo test --release`. `cargo test
166-
--release FILTER` only runs those tests that contain `FILTER` in their filename
167-
(including the base directory, e.g. `cargo test --release fail` will run all
168-
compile-fail tests). We recommend using `--release` to make test running take
169-
less time.
162+
`cargo test --release FILTER` only runs those tests that contain `FILTER` in
163+
their filename (including the base directory, e.g. `cargo test --release fail`
164+
will run all compile-fail tests).
170165

171-
Now you are set up! You can write a failing test case, and tweak miri until it
172-
fails no more.
173166
You can get a trace of which MIR statements are being executed by setting the
174167
`MIRI_LOG` environment variable. For example:
175168

176169
```sh
177170
MIRI_LOG=info cargo run tests/run-pass/vecs.rs
178171
```
179172

180-
Setting `MIRI_LOG` like this will configure logging for miri itself as well as
173+
Setting `MIRI_LOG` like this will configure logging for Miri itself as well as
181174
the `rustc::mir::interpret` and `rustc_mir::interpret` modules in rustc. You
182-
can also do more targeted configuration, e.g. to debug the stacked borrows
183-
implementation:
175+
can also do more targeted configuration, e.g. the following helps debug the
176+
stacked borrows implementation:
177+
184178
```sh
185179
MIRI_LOG=rustc_mir::interpret=info,miri::stacked_borrows cargo run tests/run-pass/vecs.rs
186180
```
187181

188182
In addition, you can set `MIRI_BACKTRACE=1` to get a backtrace of where an
189-
evaluation error was originally created.
183+
evaluation error was originally raised.
184+
185+
### Testing `cargo miri`
186+
187+
Working with the driver directly gives you full control, but you also lose all
188+
the convenience provided by cargo. Once your test case depends on a crate, it
189+
is probably easier to test it with the cargo wrapper. You can install your
190+
development version of Miri using
191+
192+
```
193+
cargo install --path . --force
194+
```
195+
196+
and then you can use it as if it was installed by `rustup`. Make sure you use
197+
the same toolchain when calling `cargo miri` that you used when installing Miri!
190198

199+
There's a test for the cargo wrapper in the `test-cargo-miri` directory; run
200+
`./run-test.py` in there to execute it.
191201

192202
### Using a locally built rustc
193203

194-
Since the heart of Miri (the main interpreter engine) lives in rustc, working on
195-
Miri will often require using a locally built rustc. The bug you want to fix
196-
may actually be on the rustc side, or you just need to get more detailed trace
197-
of the execution than what is possible with release builds -- in both cases, you
198-
should develop miri against a rustc you compiled yourself, with debug assertions
199-
(and hence tracing) enabled.
204+
A big part of the Miri driver lives in rustc, so working on Miri will sometimes
205+
require using a locally built rustc. The bug you want to fix may actually be on
206+
the rustc side, or you just need to get more detailed trace of the execution
207+
than what is possible with release builds -- in both cases, you should develop
208+
miri against a rustc you compiled yourself, with debug assertions (and hence
209+
tracing) enabled.
200210

201211
The setup for a local rustc works as follows:
202212
```sh
@@ -216,18 +226,21 @@ rustup override set custom
216226
```
217227

218228
With this, you should now have a working development setup! See
219-
["Testing Miri"](#testing-miri) above for how to proceed.
229+
[above][testing-miri] for how to proceed working with the Miri driver. Notice
230+
that rustc's sysroot is already built for Miri in this case, so you can set
231+
`MIRI_SYSROOT=$(rustc --print sysroot)`.
220232

221233
Running `cargo miri` in this setup is a bit more complicated, because the Miri
222-
binary you just created does not actually run without some environment variables.
223-
But you can contort cargo into calling `cargo miri` the right way for you:
234+
binary you just created needs help to find the libraries it links against. On
235+
Linux, you can set the rpath to make this "just work":
224236

225237
```sh
226-
# in some other project's directory, to run `cargo miri test`:
227-
MIRI_SYSROOT=$(rustc +custom --print sysroot) cargo +custom run --manifest-path /path/to/miri/Cargo.toml --bin cargo-miri --release -- miri test
238+
export RUSTFLAGS="-C link-args=-Wl,-rpath,$(rustc --print sysroot)/lib/rustlib/x86_64-unknown-linux-gnu/lib"
239+
cargo install --path . --force
228240
```
229241

230242
### Miri `-Z` flags and environment variables
243+
[miri-flags]: #miri--z-flags-and-environment-variables
231244

232245
Several `-Z` flags are relevant for Miri:
233246

0 commit comments

Comments
 (0)