test-fuzz

At a high-level, test-fuzz is a convenient front end for afl.rs. In more concrete terms, test-fuzz is a collection of Rust macros and a Cargo subcommand that automate certain fuzzing-related tasks, most notably:

test-fuzz accomplishes these (in part) using Rust's testing facilities. For example, to generate a fuzzing corpus, test-fuzz records a target's arguments each time it is called during an invocation of cargo test. Similarly, test-fuzz implements a fuzzing harness as an additional test in a cargo-test-generated binary. This tight integration with Rust's testing facilities is what motivates the name test-fuzz.

Contents

  1. Installation
  2. Usage
  3. Components
  4. Auto-generated Corpus Files
  5. Environment Variables
  6. Limitations
  7. Tips and Tricks

Installation

sh cargo install cargo-test-fuzz --version '>=0.1.0-alpha'

Usage

Fuzzing with test-fuzz is essentially three steps:*

  1. Identify a fuzz target:

  2. Generate a corpus by running cargo test: $ cargo test

  3. Fuzz your target by running cargo test-fuzz: $ cargo test-fuzz --target foo

* Some additional steps may be necessary following a reboot. AFL requires the following commands to be run as root:

Components

test_fuzz macro

Preceding a function with the test_fuzz macro indicates that the function is a fuzz target.

The primary effects of the test_fuzz macro are: * Add instrumentation to the target to serialize its arguments and write them to a corpus file each time the target is called. The instrumentation is guarded by #[cfg(test)] so that corpus files are generated only when running tests (however, see enable_in_production below). * Add a test to read and deserialize arguments from standard input and apply the target to them. The test checks an environment variable, set by cargo test-fuzz, so that the test does not block trying to read from standard input during a normal invocation of cargo test. The test is enclosed in a module to reduce the likelihood of a name collision. Currently, the name of the module is target_fuzz, where target is the name of the target (however, see rename below).

Options

test_fuzz_impl macro

Whenever the test_fuzz macro is used in an impl block, the impl must be preceded with the test_fuzz_impl macro. Example:

```rust

[testfuzzimpl]

impl Foo { #[test_fuzz] fn bar(&self, x: &str) { ... } } ```

The reason for this requirement is as follows. Expansion of the test_fuzz macro adds a module definition to the enclosing scope. However, a module definition cannot appear inside an impl block. Preceding the impl with the test_fuzz_impl macro causes the module to be added outside the impl block.

If you see an error like the following, it likely means a use of the test_fuzz_impl macro is missing: error: module is not supported in `trait`s or `impl`s

test_fuzz_impl currently has no options.

cargo test-fuzz command

The cargo test-fuzz command is used to interact with fuzz targets, and to manipulate their corpora, crashes, hangs, and work queues. Example invocations include:

  1. List fuzz targets cargo test-fuzz --list

  2. Display target foo's corpus cargo test-fuzz --target foo --display-corpus

  3. Fuzz target foo cargo test-fuzz --target foo

  4. Replay crashes found for target foo cargo test-fuzz --target foo --replay-crashes

Flags

Options

Auto-generated Corpus Files

cargo-test-fuzz can auto-generate values for types that implement certain traits. If all of a target's argument types implement such traits, cargo-test-fuzz can auto-generate corpus files for the target.

The traits that cargo-test-fuzz currently supports and the values generated for them are as follows:

| Trait(s) | Value(s) | |-|-| | Bounded | T::min_value(), T::max_value() | | Bounded + Add + One | T::min_value() + T::one() | | Bounded + Add + Div + Two | T::min_value() / T::two() + T::max_value() / T::two() | | Bounded + Add + Div + Two + One | T::min_value() / T::two() + T::max_value() / T::two() + T::one() | | Bounded + Sub + One | T::max_value() - T::one() | | Default | T::default() |

Key

Environment Variables

Limitations

Tips and tricks