Skip to content

About

Mutation testing tools for Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

844 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mutest-rs — Mutation testing tools for Rust

Docs DOI 10.1109/ICST57152.2023.00014

Generate and analyze runtime-swappable code mutants of Rust programs using a dynamic set of abstract mutation operators. For more information, see the mutest-rs book.

Output of mutest-rs

Note

mutest-rs is primarily a research tool, but is an extremely capable mutation testing tool for all use cases. It was originally developed for my ongoing PhD work.

Mutation Operators

Currently, the following list of mutation operators is implemented:

Mutation Operator Short Description
arg_default_shadow Ignore argument by shadowing it with Default::default().
bit_op_or_and_swap Swap bitwise OR for bitwise AND and vice versa.
bit_op_or_xor_swap Swap bitwise OR for bitwise XOR and vice versa.
bit_op_shift_dir_swap Swap the direction of bitwise shift operator.
bit_op_xor_and_swap Swap bitwise XOR for bitwise AND and vice versa.
bool_expr_negate Negate boolean expression.
call_delete Delete call and replace it with Default::default().
call_value_default_shadow Ignore return value of call by shadowing it with Default::default().
continue_break_swap Swap continue for break and vice versa.
eq_op_invert Invert equality check.
fn_return_default Return a fixed value without evaluating the function body.
logical_op_and_or_swap Swap logical and for logical or and vice versa.
match_arm_delete Delete match arm, so its values fall through to a later arm.
match_guard_value Replace match arm guard with true or false.
math_op_add_mul_swap Swap addition for multiplication and vice versa.
math_op_add_sub_swap Swap addition for subtraction and vice versa.
math_op_div_rem_swap Swap division for modulus and vice versa.
math_op_mul_div_swap Swap multiplication for division and vice versa.
range_limit_swap Swap limit (inclusivity) of range expression.
relational_op_eq_swap Include or remove the boundary (equality) of relational operator.
relational_op_invert Invert relational operator.
struct_field_delete Take field from the base of struct expression instead.
unary_op_delete Delete ! or - unary operator.

For more information, and examples, see docs/operators.md.

Build

mutest-rs relies on the nightly compiler toolchain. rustup is configured to automatically install and use the right nightly version.

Build the mutest-runtime crate in release mode.

cargo build --release -p mutest-runtime

Install mutest-driver and the Cargo subcommand cargo-mutest locally.

cargo install --force --path mutest-driver
cargo install --force --path cargo-mutest

mutest-driver embeds the release build of mutest-runtime, so build the runtime first. The installed driver unpacks it into target/mutest/mutest_deps of the package it tests.

Usage

Run the cargo mutest run subcommand against a standard Cargo package or workspace directory containing your crate.

cargo mutest run -p <PACKAGE>

Tip

It is recommended to specify specific Cargo test targets to start with, using the standard --lib, --bin <BIN>, --test <TEST>, and --example <EXAMPLE> targeting options, alongside the -p <PACKAGE> option.

See --help for more options and subcommands.

Exit codes

cargo mutest run exits with a code for scripts and CI to act on, following cargo-mutants:

Code Meaning
0 The analysis completed, and the tests caught every mutation.
1 The command could not run as given: an argument is wrong, or a part of mutest-rs it needs is not installed.
2 The analysis completed, and the tests missed some mutations.
3 The analysis completed, and some mutations timed out.
4 A test harness did not build, or its tests failed without mutations, so its mutations were not evaluated.
101 mutest-rs panicked, or a test harness ended abnormally, such as being killed by a signal.

A run of several test harnesses exits with the most severe of these codes, in the order 101, 1, 4, 3, 2, 0. With --simulate, it exits 0 if the tests catch the mutation and 2 if they miss it.

The time limit of a test is five times its time in the reference run, but at least one second more than that time. A loaded machine can make a test pass a limit under 20 seconds, so a mutation that times out only within such limits runs again after the analysis, alone and one test at a time. Each test with such a limit then gets ten seconds more. The result of this rerun is the result of the mutation, and the line timeouts confirmed: R re-run alone; D detected, U undetected, C crashed, T timed out again counts the reruns. A timeout within a limit of 20 seconds or more is final.

A test crashes when a mutation makes it end its process, such as by a panic that cannot unwind. The tests after it still run, and the mutation counts as detected once one of them fails. A mutation counts as crashed only if a test crashes and no test fails. The score leaves out the mutations that crashed or timed out.

Each mutated function keeps its original code beside the code that holds its mutations, and chooses one of the two each time it starts. With no mutation active, as in the reference run, it runs the code that holds its mutations, so the time limit of a test allows for the cost of that code. With a mutation of another function active, it runs its original code. A call that already runs original code when a mutation becomes active, such as the loop of a thread that a test left running, does not run that mutation. With --isolate=all, each mutation runs in a new process, where no such call exists.

What mutest-rs mutates

mutest-rs mutates a function only if a test can call it. The call graph starts from each test, and follows:

  • direct calls, and calls through generic functions after monomorphization;
  • calls through dyn Trait objects, which reach each method of the vtables built for the trait;
  • calls through function pointers and closures coerced to function pointers;
  • calls through the Fn, FnMut, and FnOnce traits, such as a closure given to Option::map;
  • calls inside a const fn that runs at runtime, and Drop implementations run by drop glue.

The distance from a test to a function counts only the frames of the crate under test. Frames of the standard library and of dependencies add nothing, so --depth and --call-graph-depth-limit limit the frames of your own code.

Use --print unreached to list the functions that mutest-rs does not mutate: functions that no test calls, and functions beyond the mutation depth.

cargo mutest run -p <PACKAGE> --lib --print unreached

Platforms

mutest-rs builds with the nightly toolchain pinned in rust-toolchain.toml, and runs on Linux, macOS, and Windows. A mutation that crashes or hangs the test process ends only that process; mutest-rs records the crash or timeout and continues. Each platform makes sure that processes the tests start do not outlive them:

Platform Processes started by tests When mutest-rs is killed Crash reports
Linux A subreaper adopts them, and kills them when the test ends. PR_SET_PDEATHSIG kills the test process. Core dumps are limited to 1 byte.
macOS They are in the process group of the test, which is killed when the test ends. A thread in the test process checks its parent every 50 ms, and kills the process group of the test when the parent exits. Core dumps are disabled.
Windows They are in a job object, which is closed when the test ends. Closing the job object kills them. The job object sets JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION, so a crash shows no error dialog.

On macOS, a process that leaves the process group on purpose, with setsid or setpgid, is not killed. The job object on Windows does not allow CREATE_BREAKAWAY_FROM_JOB.

Using cfg(mutest)

When running cargo mutest, the mutest cfg is set. This can be used to detect if code is running under mutest-rs, and enable conditional compilation based on it.

Starting with Rust 1.80, cfgs are checked against a known set of config names and values. If your Cargo package is checked with a regular Cargo command, it will warn you about the "unexpected" mutest cfg. To let rustc know that this custom cfg is expected, ensure that cfg(mutest) is present in the lints.rust.unexpected_cfgs.check-cfg array in the package's Cargo.toml, like so:

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(mutest)"] }

Annotating code with tool attributes

mutest-rs provides tool attributes that can be used to optionally annotate your code for use with the tool. Note that these attributes are only available when running cargo mutest, so they need to be wrapped in #[cfg_attr(mutest, <MUTEST_ATTRIBUTE>)] for regular Cargo commands to run.

#[mutest::skip] (use #[cfg_attr(mutest, mutest::skip)])

Tells mutest-rs to skip the function when applying mutation operators. Useful for marking helper functions for tests (test cases themselves are automatically skipped).

This attribute can only be applied to function declarations:

#[cfg_attr(mutest, mutest::skip)]
fn perform_tests() {

#[mutest::ignore] (use #[cfg_attr(mutest, mutest::ignore)])

Tells mutest-rs to ignore the statement or expression, including any subexpressions, or function parameter, when applying mutation operators. Useful if mutest-rs is trying to apply mutations to a critical piece of code that might be causing problems.

This attribute can be applied to

Development

Set up a checkout with one command. It installs each tool at the version that tool-versions.env pins, and it is safe to run again.

just setup

Without just, run the script itself. It installs just too.

bin/setup dev

bin/setup gates installs only what chock run needs, and CI runs it on Linux, macOS and Windows. On Windows, run the scripts in Git Bash, and install rustup first.

tool what it is for
the nightly of rust-toolchain.toml, with rustc-dev and llvm-tools mutest-driver links the compiler, so it builds on this nightly only
clippy the lints of the lints CI job
the aarch64-unknown-none target bin/test builds the runtime stub for a bare-metal target
cargo-binstall installs each tool below from its prebuilt release
chock runs every quality gate (chock run), and the git hooks that chock init --local adds
cargo-llvm-cov the coverage gate, through bin/coverage
cargo-crap the crap gate: the risk of each function, from its complexity and its coverage
cargo-deny the deps gate: advisories, licences and sources of the dependencies
cargo-machete the unused gate: dependencies that no code uses
cargo-sort the sort gate: sorted Cargo.toml files
typos-cli the typos gate: spelling
cargo-bsize chock run bsize: where the bytes of each binary go
just runs the recipes of the justfile
TLC and Apalache bin/tla checks the TLA+ specs of proofs/tla with them
Java 17 or newer (install it yourself) runs TLC and Apalache
Claude Code the coding agent; .claude/settings.json sends each edit to chock edited --hook
tova (optional, private to Outpost) checks each edit of the agent; tova.toml holds its settings
outpost (optional, private to Outpost) the code graph and the project reports that the agent reads
gh (optional) follows the CI runs, and starts the records job of .github/workflows/tests.yaml

Run the unit tests and the UI tests with bin/test, and every gate with chock run.

Check the TLA+ specs with bin/tla. bin/setup tla installs only TLC and Apalache, and checks the sha256 of each download.

License

The mutest-rs project is dual-licensed under Apache 2.0 and MIT terms.

See LICENSE-APACHE, LICENSE-MIT, and COPYRIGHT for details.

About

Mutation testing tools for Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages