onionwire/scripts/run-fuzz.sh

61 lines
2.1 KiB
Shell
Raw Permalink Normal View History

#!/usr/bin/env sh
# Run an OnionWire fuzz target. Needs a nightly toolchain and cargo-fuzz:
#
# rustup toolchain install nightly --profile minimal
# cargo install cargo-fuzz
#
# `cargo` must be rustup's shim, not a distro package: the `+nightly` directive
# only works through rustup. `export PATH="$HOME/.cargo/bin:$PATH"` if in doubt.
#
# Usage:
# scripts/run-fuzz.sh # all four targets, 60s each
# scripts/run-fuzz.sh qr_decode # one target
# FUZZ_SECS=300 scripts/run-fuzz.sh ratchet_frame
# FUZZ_JOBS=4 scripts/run-fuzz.sh # fork N workers per target
#
# Every target is a pure parser/decryptor over untrusted peer input, so a
# non-zero exit means libFuzzer found a crash: the reproducer lands in
# fuzz/artifacts/<target>/ and is named crash-<hash>.
#
# Reproduce a crash without fuzzing:
# cargo +nightly fuzz run <target> fuzz/artifacts/<target>/crash-<hash>
#
# Fold a fixed crash into the regression seeds (committed, so it runs every time
# instead of being rediscovered):
# cp fuzz/artifacts/<target>/crash-<hash> fuzz/seeds/<target>/
set -eu
cd "$(dirname "$0")/.."
TARGETS="ratchet_frame qr_decode dispatch_classify file_decode"
SECS="${FUZZ_SECS:-60}"
JOBS="${FUZZ_JOBS:-1}"
# A build tree per machine, outside the repo: the fuzz crate links the whole
# onionwire graph (Arti, SQLCipher, vendored OpenSSL) and must not share a
# target dir with a stable build.
: "${CARGO_TARGET_DIR:=$HOME/.cargo/fuzz-target}"
export CARGO_TARGET_DIR
: "${TMPDIR:=/var/tmp/onionwire-fuzz-tmp}"
export TMPDIR
mkdir -p "$TMPDIR"
if [ "$#" -gt 0 ]; then
TARGETS="$*"
fi
for t in $TARGETS; do
if [ ! -d "fuzz/corpus/$t" ]; then
mkdir -p "fuzz/corpus/$t"
fi
# Seed the working corpus from fuzz/seeds (hand-built regression inputs).
# fuzz/corpus itself is gitignored: libFuzzer grows it, and that growth is
# machine-local noise, not source.
cp -n fuzz/seeds/"$t"/* "fuzz/corpus/$t"/ 2>/dev/null || true
printf '\n=== fuzz %s (%ss, %s jobs) ===\n' "$t" "$SECS" "$JOBS"
cargo +nightly fuzz run "$t" -j "$JOBS" -- -max_total_time="$SECS"
done
printf '\nall targets survived %ss each\n' "$SECS"