|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +############################################ |
| 5 | +# Tools must be available directly in PATH: |
| 6 | +# llvm-profdata |
| 7 | +# llvm-cov |
| 8 | +############################################ |
| 9 | + |
| 10 | +PROJECT_ROOT="$(pwd)" |
| 11 | +HOST_TARGET=$(rustc +nightly --print host-tuple) |
| 12 | + |
| 13 | +echo "[+] Project root: $PROJECT_ROOT" |
| 14 | +echo "[+] Host target: $HOST_TARGET" |
| 15 | + |
| 16 | +############################################ |
| 17 | +# 1. Run coverage for all fuzz targets |
| 18 | +############################################ |
| 19 | + |
| 20 | +echo "[+] Running fuzz coverage" |
| 21 | +for target in $(cargo +nightly fuzz list); do |
| 22 | + echo " → $target" |
| 23 | + cargo +nightly fuzz coverage "$target" |
| 24 | +done |
| 25 | + |
| 26 | +############################################ |
| 27 | +# 2. Merge all .profdata |
| 28 | +############################################ |
| 29 | + |
| 30 | +echo "[+] Merging coverage profiles" |
| 31 | +llvm-profdata merge -sparse \ |
| 32 | + fuzz/coverage/*/coverage.profdata \ |
| 33 | + -o fuzz/coverage/merged.profdata |
| 34 | + |
| 35 | +############################################ |
| 36 | +# 3. Collect fuzz target coverage binaries |
| 37 | +############################################ |
| 38 | + |
| 39 | +echo "[+] Collecting fuzz binaries" |
| 40 | + |
| 41 | +OBJECTS="" |
| 42 | +FIRST_TARGET="" |
| 43 | + |
| 44 | +for target in $(cargo fuzz list); do |
| 45 | + BIN="target/$HOST_TARGET/coverage/$HOST_TARGET/release/$target" |
| 46 | + |
| 47 | + if [ ! -f "$BIN" ]; then |
| 48 | + echo "ERROR: expected binary not found: $BIN" |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + if [ -z "$FIRST_TARGET" ]; then |
| 53 | + FIRST_TARGET="$BIN" |
| 54 | + else |
| 55 | + OBJECTS="$OBJECTS -object $BIN" |
| 56 | + fi |
| 57 | +done |
| 58 | + |
| 59 | +############################################ |
| 60 | +# 4. Filtering rules: |
| 61 | +# - ignore stdlib (/rustc/…) |
| 62 | +# - ignore crates.io (~/.cargo/registry/…) |
| 63 | +# - ignore everything outside the project dir |
| 64 | +############################################ |
| 65 | + |
| 66 | +IGNORE_FLAGS=( |
| 67 | + "-ignore-filename-regex=/.*/rustc/.*" |
| 68 | + "-ignore-filename-regex=/.cargo/registry/.*" |
| 69 | + "-ignore-filename-regex=^/(?!${PROJECT_ROOT}).*$" |
| 70 | +) |
| 71 | + |
| 72 | +############################################ |
| 73 | +# 5. Generate text summary |
| 74 | +############################################ |
| 75 | + |
| 76 | +echo "[+] Generating text summary" |
| 77 | +llvm-cov report \ |
| 78 | + "${IGNORE_FLAGS[@]}" \ |
| 79 | + -instr-profile=fuzz/coverage/merged.profdata \ |
| 80 | + $FIRST_TARGET $OBJECTS \ |
| 81 | + > coverage_summary.txt |
| 82 | + |
| 83 | +############################################ |
| 84 | +# 6. Generate HTML report |
| 85 | +############################################ |
| 86 | + |
| 87 | +echo "[+] Generating HTML report" |
| 88 | +llvm-cov show \ |
| 89 | + "${IGNORE_FLAGS[@]}" \ |
| 90 | + -instr-profile=fuzz/coverage/merged.profdata \ |
| 91 | + $FIRST_TARGET $OBJECTS \ |
| 92 | + -format=html \ |
| 93 | + -output-dir=coverage_html |
| 94 | + |
| 95 | +echo "[✓] Done!" |
| 96 | +echo "HTML → coverage_html/index.html" |
| 97 | +echo "TXT → coverage_summary.txt" |
| 98 | + |
0 commit comments