Sub topic 14
Running Miri in continuous integration
This chapter assumes you can run cargo miri test locally, read a Miri error report, and set MIRIFLAGS. It also assumes you know what #[cfg(miri)] does in a test. Here you will move those local checks into a continuous integration (CI) pipeline, so that undefined behavior (UB) in unsafe code fails a pull request instead of reaching a release.
Why run Miri in CI
Miri only checks the code paths a run actually executes. A developer who runs it once, before a release, will miss UB that later changes introduce. CI runs it on every change, so each new unsafe block and each refactor is checked against your tests.
Three properties of Miri shape how you set it up:
- It is slow. Miri interprets the program, so a test suite can take tens of times longer than a native run.
- It needs a nightly toolchain. The
miricomponent is only distributed for nightly. - It exits non-zero when it finds UB. Any CI system that fails a job on a non-zero exit status therefore fails the build on a Miri error, with no extra scripting.
A minimal GitHub Actions job
The workflow below installs nightly with the miri component, builds Miri's custom standard library, and runs the test suite. GitHub Actions is used as the example. The same steps work in any CI system that can run shell commands.
name: miri
on:
push:
branches: [main]
pull_request:
env:
CARGO_TERM_COLOR: always
MIRIFLAGS: -Zmiri-strict-provenance
jobs:
miri:
name: cargo miri test
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install nightly with Miri
run: |
rustup toolchain install nightly --component miri --profile minimal
rustup override set nightly
- name: Build the Miri sysroot
run: cargo miri setup
- name: Run tests under Miri
run: cargo miri testEach part of the job has a reason:
--profile minimalskips documentation and other components you do not need, which shortens the install.rustup override set nightlymakescargouse nightly for this directory, so the later steps do not need+nightly.cargo miri setupbuilds the standard library in a form Miri can interpret.cargo miri testwould do this on first use anyway. A separate step makes the log easier to read and shows setup failures as their own step.timeout-minutesstops a runaway interpreted test from using the runner for the platform's default limit, which can be hours.MIRIFLAGSat the job level applies to every Miri command in the job.
Keep native tests and Miri tests separate
Run Miri as its own job, in parallel with the ordinary cargo test job. This has two benefits. Native failures show up in a minute or two without waiting for Miri. A Miri failure is also clearly labeled as a UB problem, not a logic failure.
Keep the native job on stable Rust. Your users build on stable, so the native job should check that stable accepts the code. Only the Miri job needs nightly.
Controlling run time
Miri's slowness is the main practical cost, so most CI tuning goes toward it. The techniques are the ones from the chapter on Miri-friendly tests, applied at the pipeline level.
Shrink the workload under Miri
Use cfg(miri) to reduce iteration counts and input sizes so the Miri job tests the same code paths with less data:
#[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);
}UB usually does not depend on how many times a loop runs. One pass over a code path is often enough to trigger it.
Skip tests that cannot run under Miri
Some tests do things Miri cannot interpret, such as calling foreign C functions or making real network connections. Mark them so they are skipped only under Miri:
#[test]
#[cfg_attr(miri, ignore)]
fn talks_to_the_network() {
// Uses a socket, which Miri cannot run.
}Prefer this to excluding whole crates. The rest of the suite keeps its coverage.
Filter by test name
When the full suite is too slow, run the tests that exercise unsafe code. Arguments after -- go to the test harness, so a name filter works as usual:
cargo miri test -- unsafe_This works best if you adopt a naming convention, such as a module called unsafe_tests. It also has a cost: a test outside the filter that reaches unsafe code indirectly is no longer checked. Check the filter against your code occasionally.
Cache what does not change
CI caches for the Cargo registry and the target directory help Miri as they help any Rust build. Miri also keeps its built sysroot in a cache directory. Caching it saves the cargo miri setup time. Key the cache on the nightly date. A new nightly needs a new sysroot, and a stale one is rebuilt or rejected anyway.
Choosing and pinning the nightly toolchain
Miri tracks the compiler closely, and nightlies sometimes ship without a working Miri component. Unpinned nightly has two failure modes:
rustup toolchain install nightly --component mirifails on a day when no nightly has Miri. This is rare but does happen.- A newer nightly makes Miri stricter or changes what it reports, so a job that was green yesterday is red today with no change in your code.
Pinning to a dated nightly avoids both. You choose when to upgrade.
env:
NIGHTLY: nightly-2026-11-20
steps:
- uses: actions/checkout@v4
- name: Install pinned nightly with Miri
run: |
rustup toolchain install "$NIGHTLY" --component miri --profile minimal
rustup override set "$NIGHTLY"
- run: cargo miri testReplace the date with one where the miri component exists. The rustup component history page for miri lists which dates have it. Update the pin on a schedule, for example monthly, in its own pull request. A Miri result that changes after an upgrade then points at the upgrade and not at your code.
A common compromise is two jobs. The required job uses the pinned toolchain. A second job, allowed to fail, uses the latest nightly and warns you early about upcoming changes.
Choosing flags for CI
Local runs and CI runs can use different MIRIFLAGS. These are useful in CI:
| Flag | What it does in CI |
|---|---|
| `-Zmiri-strict-provenance` | Rejects integer-to-pointer casts that lose provenance, so pointer code stays portable and checkable. |
| `-Zmiri-tree-borrows` | Switches aliasing checks from Stacked Borrows to Tree Borrows. |
| `-Zmiri-many-seeds=0..16` | Runs the program once per seed, so concurrency bugs that depend on thread scheduling get several chances to appear. |
| `-Zmiri-ignore-leaks` | Does not fail on memory that is never freed. Use it only for tests that leak on purpose. |
By default Miri reports allocations still live at program exit as errors. A leak failure in CI is often a real finding, so avoid turning on -Zmiri-ignore-leaks globally.
For a threaded test suite, run a many-seeds job in addition to the default one. Printing the failing seed in the log lets you reproduce the failure locally with -Zmiri-seed=<N>.
Testing other targets
Miri can interpret code for a different target than the machine it runs on. This catches bugs that depend on pointer width or byte order, even though the runner is a 64-bit little-endian machine:
strategy:
fail-fast: false
matrix:
target:
- x86_64-unknown-linux-gnu
- i686-unknown-linux-gnu
- s390x-unknown-linux-gnu
steps:
- uses: actions/checkout@v4
- run: |
rustup toolchain install nightly --component miri --profile minimal
rustup override set nightly
- run: cargo miri test --target ${{ matrix.target }}The i686 target is 32-bit and s390x is big-endian. Code that assumes 64-bit pointers or little-endian layout can pass on the default target and fail here. fail-fast: false lets every target finish, so one failure does not hide the others.
Reading failures in CI
When the job fails, the log contains the same error report you read locally: the kind of UB, the source location, and a backtrace. A few habits make CI failures easier to act on:
- Set
RUST_BACKTRACE=1so the report includes Rust frames. - Keep
CARGO_TERM_COLOR=always, which keeps the colored diagnostics readable in the web log. - Reproduce locally with the exact flags and toolchain from the workflow. A failure under
-Zmiri-many-seedsneeds the failing seed passed back as-Zmiri-seed. - Do not "fix" a Miri failure by adding
-Zmiri-disable-validationor similar relaxing flags. These hide the error without removing the UB.
Common mistakes
- Running Miri only on a schedule. A nightly cron job finds problems a day after they merge. Run it on pull requests as well, and use the schedule for the slow many-seeds job.
- Installing nightly without the component.
rustup toolchain install nightlyalone does not install Miri. Add--component miri. - Forgetting
cargo miri setupcaching. The first run in a fresh runner always rebuilds the sysroot, which can be a large part of a short job. - Mixing flags between jobs. If a failure only reproduces with the CI flags, you will waste time debugging locally. Keep the flags in one place, such as a workflow-level
envblock, and copy them from there. - Making the job optional forever. A job that is allowed to fail and is never looked at gives no protection. Make the pinned job required.
Exercise
Your Miri job passed on Monday. On Tuesday it fails on a pull request that only changes a README. The failure is a new aliasing error in code nobody touched. What is the most likely cause, and what change to the workflow prevents the surprise?
Show answer
The workflow installs the unpinned nightly toolchain, and Tuesday's nightly includes a newer Miri that reports something the earlier one did not. Pin the toolchain to a dated nightly that has the miri component. Upgrade the pin in its own pull request on a schedule, and optionally run an allowed-to-fail job on latest nightly as an early warning.
Exercise
A test spawns two threads and passes in all CI runs, but you suspect a data race that depends on scheduling. Which Miri flag gives the test several different thread schedules in one CI job, and how do you reproduce one failing run locally?
Show answer
Set MIRIFLAGS="-Zmiri-many-seeds=0..16" so Miri runs the program once per seed. When one seed fails, note it from the log and run locally with MIRIFLAGS="-Zmiri-seed=<N>" cargo miri test using the same toolchain.