Sub topic 13
Writing tests that run well under Miri
This chapter assumes you can run cargo miri test and read a Miri report, as covered earlier. It also assumes you know basic cfg attributes and Cargo test layout. The goal is a test suite that exercises your unsafe code under Miri in seconds or minutes, not hours.
Why tests need adjusting
Miri is an interpreter, so it runs code far more slowly than native execution. A loop of 100,000 iterations is trivial natively but can take a long time under Miri. Miri also cannot execute everything a normal test does, such as some foreign function calls or system interfaces.
You have two options. You can skip a test under Miri, or you can shrink it. Shrinking keeps the check on your unsafe code, so prefer it when you can.
Shrink workloads with `cfg(miri)`
Miri sets the miri configuration flag. Use it to pick a smaller workload only when running under Miri:
#[cfg(miri)]
const ITERATIONS: usize = 100;
#[cfg(not(miri))]
const ITERATIONS: usize = 100_000;
#[test]
fn stress() {
let mut sum = 0usize;
for i in 0..ITERATIONS {
sum += i;
}
assert!(sum > 0);
}Miri detects undefined behavior on the paths a program executes. A hundred iterations usually reach the same code paths as a hundred thousand, so the smaller run keeps most of the value.
Skip tests that cannot run
Some tests cannot work under Miri. Examples are tests that call unsupported system functions or that are only timing benchmarks. Mark them with cfg_attr:
#[test]
#[cfg_attr(miri, ignore)]
fn uses_unsupported_system_call() {
// ...
}Miri then reports the test as ignored. Use this sparingly. Each ignored test is code that Miri does not check.
Design tests around the unsafe code
Miri only helps where the unsafe code runs. Write tests that reach the unsafe paths, not only the safe wrappers around them. Cover these cases:
- Empty and single-element inputs, where off-by-one errors in pointer arithmetic show up.
- Growth, shrinking, and reallocation, where use-after-free bugs hide.
- Drop behavior, including dropping a value after a panic.
- Several different calls on the same value, which tests aliasing rules.
Keep each test small and focused. A short test gives a short backtrace, and Miri stops at the first error it finds.
Keep tests deterministic
Miri's default behavior is deterministic for single-threaded code, so a failure repeats. For threaded code, the schedule can vary with the seed. Run several seeds to explore more interleavings:
MIRIFLAGS="-Zmiri-many-seeds=0..16" cargo +nightly miri testIf a threaded test fails on one seed, record that seed and reproduce it with -Zmiri-seed=N. Avoid sleep calls to force an ordering. Use channels or join so the test does not depend on timing.
Run a subset
You can pass a name filter to select tests, as with cargo test:
cargo +nightly miri test stressWhile fixing a bug, run only the failing test. Run the full suite before you finish.
Exercise
A test loops 1,000,000 times over a function that uses unsafe pointer arithmetic, and it takes too long under Miri. What should you do, and why not just ignore it?
Show answer
Use #[cfg(miri)] and #[cfg(not(miri))] to set a much smaller iteration count under Miri, for example 100. This keeps the unsafe code running under the interpreter. Ignoring the test with #[cfg_attr(miri, ignore)] would stop Miri from checking that code at all.