Sub topic 8
Configuring Miri with MIRIFLAGS
This chapter assumes you can run cargo miri test and read a basic Miri report. It also assumes you know what a raw pointer is and how rustup installs components. Here you will learn how to change Miri's behavior with the MIRIFLAGS environment variable.
What MIRIFLAGS does
Miri has defaults that suit most projects. When you need a different behavior, you pass options through MIRIFLAGS. Cargo does not read this variable. cargo miri hands its contents to the Miri interpreter. Every Miri-specific option starts with -Zmiri-.
The aliasing model is one option you may want to change. By default Miri checks aliasing with Stacked Borrows. The flag below switches to Tree Borrows.
MIRIFLAGS="-Zmiri-tree-borrows" cargo +nightly miri testOn Windows PowerShell, set the variable first with $env:MIRIFLAGS="-Zmiri-tree-borrows", then run the command.
A worked example
This program creates a raw pointer, then creates a new mutable reference to the same variable. It then writes through the raw pointer.
fn main() {
let mut x = 10;
let raw = &mut x as *mut i32;
let r = &mut x;
unsafe { *raw = 20; }
*r += 1;
}Run it with cargo +nightly miri run. Miri reports an aliasing violation. Creating r invalidated raw, so the write through raw is not allowed. Now run it with MIRIFLAGS="-Zmiri-tree-borrows". The two models do not always agree on which programs are valid. Check what each one reports for your own code. A program that passes under one model but fails under the other is a warning sign.
Common flags
| Flag | Effect |
|---|---|
| `-Zmiri-tree-borrows` | Use Tree Borrows instead of Stacked Borrows. |
| `-Zmiri-seed=N` | Fix the random seed, so a run is repeatable. |
| `-Zmiri-many-seeds` | Run the program with many seeds in turn. |
| `-Zmiri-disable-isolation` | Allow access to the host system, such as the clock and files. |
Miri is a moving target, so run cargo miri run -- --help or read the Miri README for the flags your nightly version supports.
Seeds and schedules
Miri makes some choices at random, such as how threads are scheduled. A bug that depends on thread timing may show up under one seed and not another. Two options help here.
-Zmiri-seed=Nrepeats a failing run exactly. Use it after a failure to debug the same schedule again.-Zmiri-many-seedsruns the program under a range of seeds, which gives it more chances to hit a bad schedule.
You can combine flags by separating them with spaces inside the quotes:
MIRIFLAGS="-Zmiri-tree-borrows -Zmiri-seed=7" cargo +nightly miri testIsolation
By default, Miri runs in isolation. Programs cannot read the real clock, environment variables or most of the file system. A test that needs them fails with an unsupported-operation error. Pass -Zmiri-disable-isolation to allow these calls. Use it only when you need it, because isolated runs are more repeatable.
Which model should you pick?
Start with the default. Switch to Tree Borrows when a report is hard to reason about, or when you want to check that your code is valid under both models. Keep in mind that the exact rules for aliasing are still being settled. Code that passes under both models is more likely to survive future changes.
Exercise
A multithreaded test fails under Miri on one run but passes on the next. Which flags help you reproduce the failure, and which help you find it more reliably?
Show answer
Use -Zmiri-seed=N with the seed of the failing run to reproduce it exactly. Use -Zmiri-many-seeds to run the test under many schedules, which finds timing-dependent failures more reliably.
Exercise
Your test reads the system clock and Miri stops with an unsupported-operation error. What flag lets it continue, and what is the cost?
Show answer
-Zmiri-disable-isolation lets the program reach the host system. The cost is that runs are less repeatable, since they now depend on the host.