Sub topic 4
Installing Miri and running your code and tests
This chapter assumes you know Rust ownership and borrowing, basic Cargo usage (cargo run, cargo test), and how to install toolchains and components with rustup. You should also have a basic grasp of raw pointers and the unsafe keyword.
What you need
Miri is a rustup component that ships only for the nightly toolchain. You don't have to make nightly your default. The +nightly prefix selects it for a single command, so your normal builds keep using stable.
rustup +nightly component add miriIf nightly isn't installed yet, this command installs it first. Rustup picks a nightly build that includes Miri.
Running a program or test suite
Cargo gets a miri subcommand once the component is installed. It has two common forms:
cargo +nightly miri run
cargo +nightly miri testcargo miri run builds your binary and executes it inside the interpreter. cargo miri test does the same for every test in your package. Both forms accept the arguments you already use with cargo run and cargo test, such as a test name filter.
The first run also builds a custom copy of the standard library for Miri. This takes a while and happens once per toolchain. Later runs reuse it.
A first run
Save this as src/main.rs. It reads one element past the end of a three-element Vec.
fn main() {
let v = vec![1, 2, 3];
let p = v.as_ptr();
let x = unsafe { *p.add(3) };
println!("{x}");
}Run it natively with cargo run. It will often print a number and exit normally, because the read lands in memory that happens to be mapped. Nothing tells you the program is wrong.
Now run it under Miri:
cargo +nightly miri runMiri stops at the *p.add(3) line and reports undefined behavior. The report names an out-of-bounds pointer access and points at the source location. Chapter 5 covers how to read these reports in detail. For now, notice the difference: the native run succeeded and Miri did not.
What to expect from Miri runs
- Speed. Miri is much slower than native code. A test that takes a second natively can take minutes.
- Interpreted only. Miri checks the code paths that actually execute. Code that never runs is never checked.
- Exit status. When Miri finds undefined behavior, the command exits with a failure code. That is what lets a CI job fail on it.
- Test isolation. Tests run in the interpreter, so operations that need the real system, such as reading the clock or the host's files, may be restricted. Chapter 8 covers the flags that relax this.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no such command: miri` | The component isn't installed for that toolchain | Run `rustup +nightly component add miri` |
| `cargo miri` runs but uses stable | The `+nightly` prefix is missing | Add `+nightly` after `cargo` |
| Very long first run | Miri is building its standard library | Wait. Later runs are faster |
| Test hangs for minutes | The workload is too large for an interpreter | Reduce iteration counts, as chapter 13 shows |
Exercise
You run cargo miri test and get no such command: miri. Name two things you should check, and give the command that installs Miri.
Show answer
Check that the Miri component is installed for the nightly toolchain, and that you used the +nightly prefix (cargo +nightly miri test). Install it with rustup +nightly component add miri.