Running tests in miri

Instructions for running Fuchsia host-side tests in 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.

Prerequisites

Install rustup 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)'

See the cargo on Fuchsia 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