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.
Note: it is not currently possible 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!
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
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: 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 docs for more information.
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
.
Change to the same directory as the generated Cargo.toml
:
$ cd src/sys/lib/cm_rust
Run miri:
$ cargo +nightly miri test