Sub topic 14
Running Miri in continuous integration
This chapter assumes you can run cargo +nightly miri test locally, know what MIRIFLAGS does, and have written tests that use cfg(miri) to shrink their workload. Here you will move that setup into continuous integration (CI), so every change is checked automatically.
Why run Miri in CI
Miri only checks the code paths that execute. A developer who forgets to run it locally can merge a change that introduces undefined behavior (UB). A CI job removes that dependency on memory. It also pins the toolchain and flags, so the whole team gets the same result.
Miri is slow, often 10 to 100 times slower than native execution. Treat it as a separate job from your normal test job, so a slow Miri run does not delay ordinary feedback.
A GitHub Actions job
This workflow installs nightly with the Miri component and runs the test suite under it.
name: miri
on:
push:
branches: [main]
pull_request:
jobs:
miri:
runs-on: ubuntu-latest
env:
MIRIFLAGS: "-Zmiri-tree-borrows"
steps:
- uses: actions/checkout@v4
- name: Install nightly with Miri
run: |
rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup
- name: Run tests under Miri
run: cargo miri testThe steps do the following:
rustup toolchain install nightly --component miriinstalls Miri together with the toolchain.rustup override set nightlymakescargo miriuse that toolchain without a+nightlyprefix.cargo miri setupbuilds the Miri sysroot (a custom copy of the standard library) ahead of time. Doing it in its own step keeps setup output out of the test log.cargo miri testruns the tests with the flags fromMIRIFLAGS.
Controlling run time
A Miri job can take many minutes. These measures keep it manageable:
- Use
cfg(miri)to reduce iteration counts and input sizes, as in the previous chapter. - Run only the crates or tests that contain
unsafecode, for examplecargo miri test -p my-unsafe-crate. - Filter by test name:
cargo miri test ring_buffer. - Cache the Cargo registry and the Miri sysroot between runs.
- Set a job timeout so a hung test fails the job instead of consuming runner time.
Covering more than one configuration
A single run exercises one thread schedule and one target. You can widen coverage with a matrix. The -Zmiri-many-seeds flag runs each test under a range of seeds, which vary the thread schedule and other nondeterministic choices.
strategy:
matrix:
flags:
- "-Zmiri-tree-borrows"
- "-Zmiri-many-seeds=0..16"
env:
MIRIFLAGS: ${{ matrix.flags }}Putting -Zmiri-many-seeds in a separate matrix entry means a seed sweep can fail without hiding the result of the default run.
Handling failures
When Miri fails in CI, the log contains the same report you read locally: the error kind, the source location, and the backtrace. Reproduce it on your machine with the same toolchain and the same MIRIFLAGS. If the failure depends on a seed, the report shows it, and you can pass it back with -Zmiri-seed=N.
Do not mark the job as allowed to fail. A Miri job that never blocks a merge is quickly ignored. If a failure comes from a known Miri limitation and not from your code, skip that single test with #[cfg_attr(miri, ignore)] and add a comment that says why.
Exercise
Your Miri job passed yesterday and fails today on an unchanged branch, with a new error in code you did not touch. What is the most likely cause, and what change to the workflow prevents surprises like it?
Show answer
The unpinned nightly toolchain updated, and the new Miri version checks something it did not check before (or has a regression). Pin the toolchain to a dated nightly, such as nightly-2026-11-01, and update that date deliberately, so you can review new findings when you choose to.