Files
ianitor/tests/fuzz/run.sh
T

67 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# SPDX-License-Identifier: CPAL-1.0
# Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
# License 1.0: see LICENSE (its Exhibit A is this file's notice).
#
# ROADMAP §5 layer 3: a bounded cargo-fuzz run of every target. Any crash fails the run, and the
# crashing input is copied to tests/fuzz/regressions/<target>/, where `cargo test` replays it
# (tests/fuzz/tests/regressions.rs) until it is fixed; commit it with the fix.
#
# tests/fuzz/run.sh [seconds per target, default 60] [target ...]
#
# Needs cargo-fuzz (`cargo install cargo-fuzz`, or nixpkgs#cargo-fuzz). With no nightly toolchain,
# RUSTC_BOOTSTRAP=1 lets a stable rustc take cargo-fuzz's flags. SANITIZER=address (default none)
# turns on ASan where the toolchain ships its runtime (rustup's x86_64 Linux does). The growing
# corpus lives outside the repository: $IANITOR_FUZZ_CORPUS (default ~/.cache/ianitor-fuzz).
# Linux only (libFuzzer); output and logs go to $CARGO_TARGET_DIR or tests/fuzz/fuzz/target.
set -euo pipefail
secs=${1:-60}
shift || true
here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
targets=("$@")
if [ ${#targets[@]} -eq 0 ]; then
targets=(routes header_rules share_token path_within pki_pem)
fi
corpus_root=${IANITOR_FUZZ_CORPUS:-$HOME/.cache/ianitor-fuzz}
sanitizer=${SANITIZER:-none}
logs=${CARGO_TARGET_DIR:-$here/fuzz/target}/fuzz-logs
mkdir -p "$logs"
toolchain=()
if cargo +nightly --version >/dev/null 2>&1; then
toolchain=(+nightly)
else
export RUSTC_BOOTSTRAP=1
fi
cargo "${toolchain[@]}" fuzz --version >/dev/null
cd "$here/fuzz"
status=0
printf '%-14s %10s %8s %s\n' target runs seconds result
for t in "${targets[@]}"; do
corpus="$corpus_root/$t"
artifacts="$logs/artifacts/$t/"
mkdir -p "$corpus" "$artifacts"
log="$logs/$t.log"
if cargo "${toolchain[@]}" fuzz run --sanitizer "$sanitizer" "$t" "$corpus" "$here/seeds/$t" -- \
-max_total_time="$secs" -timeout=10 -rss_limit_mb=2048 -print_final_stats=1 \
-artifact_prefix="$artifacts" >"$log" 2>&1; then
result=ok
else
result=CRASH
status=1
mkdir -p "$here/regressions/$t"
for a in "$artifacts"*; do
[ -f "$a" ] || continue
# Hex text: a crash input may hold NUL bytes, which the pre-commit hook refuses.
od -An -tx1 -v "$a" | tr -d ' \n' >"$here/regressions/$t/$(basename "$a").hex"
echo " saved $(basename "$a").hex to tests/fuzz/regressions/$t/" >&2
done
tail -40 "$log" >&2
fi
runs=$(grep -Eo 'stat::number_of_executed_units: *[0-9]+' "$log" | grep -Eo '[0-9]+$' | tail -1 || true)
printf '%-14s %10s %8s %s\n' "$t" "${runs:-?}" "$secs" "$result"
done
exit $status