# Running tests in miri

Instructions for running Fuchsia host-side tests in [miri](https://github.com/rust-lang/miri). The examples use the
`//src/sys/lib/cm_rust:cm_rust_test($host_toolchain)` target.

miri is an interpreter for Rust code which you can use as a powerful tool to
find undefined behavior (UB) in Rust code, analogous to sanitizers for C and
C++ like UBSan.

> [!NOTE]
> **Note:** it is [not currently possible](https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=88691) to run tests under miri using Fuchsia's build system or infrastructure. This document describes a manual workflow which only supports host-side (Linux) targets and is not continuously tested. Fixes and updates are welcome!

## Prerequisites

Install [rustup](https://rustup.rs/) and run `rustup update`.

Add a nightly toolchain if you don't already have one:

    $ rustup install nightly

Add the miri component:

    $ rustup +nightly component add miri

## Generate `Cargo.toml`

Running `cargo miri` requires a `Cargo.toml` manifest for your crate.

Configure your build to include the test you want to run and to generate Cargo.toml's:

    $ fx set PRODUCT.BOARD --with //src/sys/lib/cm_rust:tests --cargo-toml-gen

Run a build:

    $ fx build

Link the generated manifest into your source repository:

    $ fx gen-cargo '//src/sys/lib/cm_rust:cm_rust_test_executable(//build/toolchain:host_x64)'

> [!NOTE]
> **Note:** The target given to `fx gen-cargo` must specify the actual executable target of the test (`cm_rust_test_executable`) and the toolchain of the library without using GN variables (`//build/toolchain:host_x64`).

See the [cargo on Fuchsia](https://fuchsia.dev/docs/development/languages/rust/cargo#cargo-toml-gen) docs for more information.

## Setup miri

`cargo miri` must compile a fresh Rust sysroot, which requires access to
external crates which are not vendored in the Fuchsia tree.

Open `src/.cargo/config` in your editor and comment out the configuration for
vendoring crates:

    --- a/src/.cargo/config
    +++ b/src/.cargo/config
    @@ -1,8 +1,8 @@
    -[source.crates-io]
    -replace-with = "vendored-sources"
    +#[source.crates-io]
    +#replace-with = "vendored-sources"

    -[source.vendored-sources]
    -directory = "../third_party/rust_crates/vendor"
    +#[source.vendored-sources]
    +#directory = "../third_party/rust_crates/vendor"

Run `cargo +nightly miri setup`.

## Run miri

Change to the same directory as the generated `Cargo.toml`:

    $ cd src/sys/lib/cm_rust

Run miri:

    $ cargo +nightly miri test

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/languages/rust/miri.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/languages/rust/miri.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/languages/rust/miri.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/languages/rust/miri.md "Edit the source code of this page")