Sub topic 3
Miri: an interpreter that checks as it executes
This chapter assumes you know Rust ownership, borrowing and lifetimes, can use cargo run and cargo test, and have seen raw pointers and the unsafe keyword. The previous chapter showed that undefined behavior (UB) can pass tests unnoticed. Here you will see how Miri finds it.
What Miri is
Miri is an interpreter for Rust's MIR. MIR is the mid-level intermediate representation, a simplified form of your program that the compiler produces after type checking and borrow checking. The compiler normally lowers MIR to machine code. Miri instead executes the MIR directly, one step at a time.
An interpreter can do something native code cannot. It keeps extra bookkeeping about every allocation, pointer and value. Before each operation it checks that the operation is allowed by Rust's rules. If it is not, Miri stops and reports the error.
How it works internally
Miri does not use your computer's real memory. It simulates a machine with its own memory model. For each allocation it tracks the following:
- Which bytes are initialized.
- Whether the allocation is still alive.
- Which pointers may legitimately access it.
A pointer in Miri is more than an address. It also carries information about where it came from and what it may access. A later chapter covers this as provenance and aliasing.
Because the interpreter sees every access, it can catch errors that happen to behave "fine" natively. Reading one element past a Vec often returns a stale value on real hardware. Under Miri, the same read fails because the pointer has no right to that byte.
What Miri checks
Miri looks for many kinds of UB while the program runs. The list below names the kinds this tutorial covers.
| Kind of UB | Example |
|---|---|
| Out-of-bounds access | Reading past the end of an allocation |
| Use-after-free | Reading through a pointer after the owner was dropped |
| Invalid values | A `bool` holding 3 |
| Uninitialized memory | Reading bytes that were never written |
| Aliasing violations | Using a raw pointer after a new `&mut` invalidated it |
| Data races | Two threads writing the same location without synchronization |
Later chapters take these one at a time.
Only executed paths are checked
Miri checks what actually runs. If a branch never executes, Miri never examines it. The same holds for inputs. If your test does not trigger the bad case, Miri will not report it.
Miri is also deterministic by default for single-threaded code. It runs the same operations the same way each time. For threaded code, the schedule matters, and a later chapter shows how seeds vary it.
Interpretation is slow. Expect programs to run many times slower than native, so large workloads need to be shrunk. The testing chapter covers this.
Where Miri fits
Miri is a dynamic checker. It differs from the compiler, which checks safe code statically for all paths. It also differs from a sanitizer such as AddressSanitizer, which instruments native code. Miri checks Rust's own rules for validity and aliasing, which native tools do not know about.
Exercise
A test reads one element past the end of a Vec through a raw pointer and passes natively. Why can Miri still fail it, and what limits that guarantee?
Show answer
Miri tracks the bounds of each allocation and the right of each pointer to access it, so it sees the out-of-bounds read even when the stale memory happens to hold a plausible value. The limit is that Miri only checks code that actually executes. If the test never runs the bad access, or an input that triggers it, Miri cannot report it.