|
| 1 | +#!/bin/bash |
| 2 | +# fuzz.sh - Build and run the wolfMQTT broker fuzzer |
| 3 | +# |
| 4 | +# Usage: ./scripts/fuzz.sh [seconds] |
| 5 | +# seconds: fuzz duration (default: 60) |
| 6 | +# |
| 7 | +# Requires: clang with libFuzzer support |
| 8 | + |
| 9 | +set -e |
| 10 | + |
| 11 | +FUZZ_TIME=${1:-60} |
| 12 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 13 | +ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 14 | + |
| 15 | +cd "$ROOT_DIR" |
| 16 | + |
| 17 | +# Check for clang |
| 18 | +if ! command -v clang >/dev/null 2>&1; then |
| 19 | + echo "Error: clang is required for fuzzing (libFuzzer)" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Generate configure if needed |
| 24 | +if [ ! -f ./configure ]; then |
| 25 | + ./autogen.sh |
| 26 | +fi |
| 27 | + |
| 28 | +# Configure with fuzzer and address sanitizer |
| 29 | +CC=clang ./configure --enable-broker --enable-v5 --enable-fuzz \ |
| 30 | + --disable-tls --disable-examples \ |
| 31 | + CFLAGS="-fsanitize=fuzzer-no-link,address -fno-omit-frame-pointer -g -O1" \ |
| 32 | + LDFLAGS="-fsanitize=address" |
| 33 | + |
| 34 | +make -j$(nproc) |
| 35 | + |
| 36 | +# Generate seed corpus |
| 37 | +python3 tests/fuzz/gen_corpus.py |
| 38 | + |
| 39 | +# Run fuzzer |
| 40 | +echo "Fuzzing for ${FUZZ_TIME} seconds..." |
| 41 | +export ASAN_OPTIONS="detect_leaks=1:abort_on_error=1:symbolize=1" |
| 42 | + |
| 43 | +timeout "$FUZZ_TIME" \ |
| 44 | + ./tests/fuzz/broker_fuzz \ |
| 45 | + tests/fuzz/corpus/ \ |
| 46 | + -dict=tests/fuzz/mqtt.dict \ |
| 47 | + -max_len=4096 \ |
| 48 | + -timeout=10 \ |
| 49 | + -rss_limit_mb=2048 \ |
| 50 | + -print_final_stats=1 \ |
| 51 | + || FUZZ_RC=$? |
| 52 | + |
| 53 | +# timeout returns 124 on normal expiry, fuzzer returns 0 on no crash |
| 54 | +if [ "${FUZZ_RC:-0}" -eq 124 ] || [ "${FUZZ_RC:-0}" -eq 0 ]; then |
| 55 | + echo "Fuzzer completed without crashes" |
| 56 | +else |
| 57 | + echo "Fuzzer found crashes (exit code $FUZZ_RC)" |
| 58 | + ls -la crash-* 2>/dev/null || true |
| 59 | + exit 1 |
| 60 | +fi |
0 commit comments