Sub topic 15
What a Clean Miri Run Does and Does Not Tell You
This chapter assumes you can run cargo miri test, read a Miri report, and know the basics of raw pointers and unsafe. It covers what a passing Miri run proves and what it leaves open.
Miri checks executions, not programs
Miri interprets your code and checks each operation that actually runs. If a test never reaches a faulty line, Miri never sees it. A clean run means that these executions, with this input, on this schedule, showed no undefined behavior that Miri knows how to detect.
That is weaker than "this code is sound". A function is sound if no safe caller can trigger undefined behavior. Miri samples calls, so it cannot establish that property.
pub fn get(v: &[u32], i: usize) -> u32 {
if i == 1000 {
unsafe { *v.as_ptr().add(i) }
} else {
v[i]
}
}
#[test]
fn small_index() {
assert_eq!(get(&[1, 2, 3], 2), 3);
}This test passes under Miri because it never takes the unsafe branch. Calling get(&[1, 2, 3], 1000) would be reported immediately.
Common gaps
| Gap | Why Miri misses it |
|---|---|
| Untested paths | Only executed code is checked. |
| Untested inputs | A bug may appear only for some lengths, values, or alignments. |
| Thread schedules | A data race may need an interleaving that one seed does not produce. |
| Foreign code | Miri cannot run most C libraries or system calls. |
| Hardware details | Miri models the abstract machine, not your CPU or OS. |
| Weak memory behavior | Miri explores only some of the orderings that real hardware allows. |
Rules that are still being decided
Some rules are not settled. Stacked Borrows and Tree Borrows are both experimental models of what Rust will allow. Code can pass one and fail the other. Miri may also be stricter than the final rules, or lag behind them. A report can therefore flag code that might eventually be declared valid, and a pass is not a promise about future compilers.
Miri also makes choices that vary between runs. Pointer addresses and thread scheduling are examples. Running with several seeds explores more possibilities.
for seed in $(seq 0 15); do
MIRIFLAGS="-Zmiri-seed=$seed" cargo +nightly miri test || break
doneGetting more from a clean run
- Test edge cases: empty slices, length one, maximum sizes, zero-sized types, and unaligned data.
- Run many seeds for concurrent code, and consider
-Zmiri-many-seeds. - Run both aliasing models and understand any difference.
- Use property tests or fuzzing as input generators, with reduced iteration counts under
cfg(miri). - Keep a safe wrapper around each unsafe block and write down its safety conditions.
flowchart TDA sensible stance
Miri is among the best tools for finding undefined behavior in Rust, and a failure is almost always worth fixing. A pass lowers risk in proportion to how well your tests cover the unsafe code. For code where a bug would be costly, combine Miri with careful review of each safety invariant, and use formal verification where it is warranted.
Exercise
Your test suite passes under cargo miri test with the default settings. Name two reasons the unsafe code could still contain undefined behavior.
Show answer
Any two of: the tests never execute the faulty path or input; a data race needs a thread schedule that the chosen seed did not produce; the code violates Tree Borrows but was only run under Stacked Borrows (or the reverse); the bug depends on foreign code or hardware behavior that Miri does not model.
Exercise
Why is "Miri found nothing" different from "this function is sound"?
Show answer
Miri checks only the executions your tests perform. Soundness means no safe caller can trigger undefined behavior with any input, so a clean run on a few inputs cannot establish it.