The user wants a CI stage for fuzzing. This is a complex request involving several components:
- Fuzzing: Running the fuzzer (like libFuzzer, AFL++, or Go native fuzzer) in a CI environment.
- Coverage: Tracking how much code the fuzzer is exercising.
- Crash Triage: Handling bugs found (reproducing, minimizing, reporting).
- Suppressions: Ignoring known/expected issues to keep the pipeline green.
I need to provide a concrete, usable example. I'll choose a popular stack to make it practical:
- Language: C++ or Rust (common for fuzzing). Let's stick to C++ with libFuzzer as it's the industry standard for "build a fuzzing CI" tutorials, though Rust is arguably easier. Actually, a shell script/Makefile approach is more generic, but a specific example is better. Let's go with a C++/libFuzzer example orchestrated via a generic CI config (like GitHub Actions), as that's the most common use case.
Key components of the solution:
- The Setup (Docker/Environment): Fuzzing requires specific toolchains (clang, sanitizers). A Docker container is best for reproducibility.
- The Script (The "Stage"):
- Compilation: Build with sanitizers (Address, Undefined) and coverage instrumentation.
- Execution: Run the fuzzer for a set time/cycles.
- Corpus management: Syncing seeds (optional but good practice).
- Coverage Reporting: Generate a report (HTML/JSON) and verify thresholds.
- Crash Triage:
- Check for exit code != 0.
- Minimize the crash (using the fuzzer's built-in tools).
- Upload artifacts.
- Suppressions:
- How to ignore known crashes. (Blocklists/Suppressions files).
Drafting the Structure:
- Concept: Explain the flow (Build -> Fuzz -> Report).
- Prerequisites: Mention Sanitizers (ASan/UBSan) and Coverage.
- Step 1: The Build Script (Local/Docker): Compile code with
--coverage and -fsanitize=address.
- Step 2: The Fuzzing Script:
- Logic to run the fuzzer.
- Logic to handle "Suppressions" (this is tricky with libFuzzer directly, usually involves wrapper scripts or
-ignore_crashes logic, or specific sanitizer suppression files). I should explain Sanitizer Suppression files vs. "Ignore this specific crashing input".
- Step 3: Crash Triage:
- Artifact uploading.
- Minimization steps.
- Step 4: Coverage Collection:
llvm-cov.
- Quality Gates (e.g., fail if coverage < X%).
- Step 5: CI Configuration (GitHub Actions example): Tie it all together.
Refining "Suppressions":
There are two types of suppressions in fuzzing:
- Sanitizer Suppressions: "I know this function has an overflow, ignore it." (ASAN_OPTIONS=suppressions=...).
- Bug Suppressions (Crash Deduplication): "I know this specific crash happens, don't fail the build."
- Self-correction: In CI, we usually want the build to fail if a new crash is found. If we suppress known crashes, we might hide bugs. However, the user asked for it.
- Implementation: A common pattern is to have a
known_crashes directory. If a crash is found, check if it matches a hash in known_crashes. If so, ignore. If not, fail.
Refining "Crash Triage":
CI runners are ephemeral. If a crash is found, the CI usually just fails.
To "triage" in CI:
- Upload the crashing input and stack trace as an artifact.
- Ideally, run the minimizer automatically to save developer time.
Refining "Coverage":
- Run fuzzer.
- Merge coverage data.
- Generate report.
- Upload to codecov or similar.
Let's write the code.
I will create a comprehensive fuzz_ci.sh script and a GitHub Actions workflow. This is the most portable and understandable way to answer.
Plan:
- Introduction: Define the stack (libFuzzer + ASan + Clang + GH Actions).
- Suppression Strategy: Explain
ASAN_OPTIONS and the "known crashes" corpus pattern.
- Implementation:
- Dockerfile (Environment).
fuzz.sh (The logic).
triage.sh (Post-processing).
ci.yml (The pipeline).
- Coverage: Using
llvm-profdata and llvm-cov.
Self-Correction during drafting:
Writing a full C++ project is too much boilerplate. I'll assume a hypothetical project structure src/ and fuzz/ and provide the scripts that operate on them.
Crucial Detail on Suppressions:
LibFuzzer doesn't have a native "ignore this crashing input" flag for CI. If it crashes, it exits non-zero.
Solution: A wrapper script.
- Run fuzzer.
- If it crashes, parse the log/output.
- Compare crash hash/signature to a suppression list.
- If suppressed, exit 0 (with a warning). If not, exit 1 and upload artifact.
Crucial Detail on Coverage:
Running the fuzzer generates .profraw files. These need to be indexed and reported. I need to show the commands for that.
Final Polish:
Make sure to distinguish between "Code Coverage" (lines hit) and "Stability/Reliability" (crashes).
Let's generate the response. I will structure it as a guide to building the Fuzzing Factory.
- Phase 1: The Build (Coverage + Sanitizers)
- Phase 2: The Harness & Execution
- Phase 3: Suppressions (Sanitizer vs Logic)
- Phase 4: Crash Triage (Minimization)
- Phase 5: CI Integration (GitHub Actions example)
This looks solid. I will write the scripts to be copy-pasteable with minor adjustments.
Check: Did I miss anything? "Crash triage" usually implies deduplication and severity analysis. In an automated CI stage, we can't easily do severity analysis without human intervention. We can do deduplication (via stack hashes) and minimization. I will focus on those.
Check: Suppressions. Users often confuse sanitizer suppressions (ignoring memory errors in 3rd party code) with "ignoring known fuzzer findings". I must address both.
Let's go.
Building a robust fuzzing CI stage requires integrating three distinct mechanisms: instrumentation (for coverage), sanitizers (for crash detection), and state management (for corpus and suppressions).
Below is a comprehensive guide and implementation strategy using libFuzzer (C/C++/Rust) on GitHub Actions, though the patterns apply to Jenkins/GitLab CI as well.
1. Architecture Overview
A production fuzzing stage typically consists of three jobs:
- Build: Compiles the code with fuzzing instrumentation, sanitizers (ASan/UBSan), and coverage flags.
- Fuzz (Matrix): Runs the fuzzer(s) for a set duration (e.g., 10 minutes) using a shared corpus.
- Report: Collects crashes, generates coverage reports, and merges corpus updates.
2. The Implementation
A. Suppressions (Ignoring Known Issues)
Before running, you need a way to ignore known crashes (unfinished fixes) or false positives in 3rd party libraries.
Create a suppression file (e.g., .asan_suppressions):
# Suppress known leak in third-party lib
leak:third_party_lib_malloc
# Suppress a known issue tracked in JIRA-123
interceptor_via_lib:libbroken.so
B. The CI Workflow (GitHub Actions Example)
This workflow checks out code, builds the fuzzer, runs it, triages crashes, and uploads coverage.
File: .github/workflows/fuzzing.yml
name: Fuzzing CI
on:
schedule:
- cron: '0 4 * * *' # Run every night
pull_request:
branches: [ main ]
workflow_dispatch: # Allow manual triggers
jobs:
fuzz:
runs-on: ubuntu-latest
container:
image: gcr.io/oss-fuzz-base/base-builder # Use a standard fuzzing image
steps:
- uses: actions/checkout@v3
- name: Install Dependencies
run: |
sudo apt-get update && sudo apt-get install -y cmake llvm clang
# ---------------------------------------------------------
# 1. BUILD WITH COVERAGE & SANITIZERS
# ---------------------------------------------------------
- name: Build Fuzzer
run: |
mkdir build && cd build
cmake .. \
-DCMAKE_C_COMPILER=clang \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_BUILD_TYPE=Debug \
-DFSANITIZE_ADDRESS=ON \
-DFSANITIZE_UNDEFINED=ON \
# Enable Coverage tracking
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping"
make -j$(nproc) my_fuzz_target
working-directory: ${{ github.workspace }}
# ---------------------------------------------------------
# 2. PREPARE CORPUS & SUPPRESSIONS
# ---------------------------------------------------------
- name: Download Seed Corpus
uses: actions/download-artifact@v3
continue-on-error: true # Might not exist on first run
with:
name: fuzz-corpus
path: ./corpus
- name: Create Directories
run: |
mkdir -p artifacts/crashes
mkdir -p artifacts/coverage
mkdir -p corpus_seed
# ---------------------------------------------------------
# 3. RUN FUZZER (Timeboxed)
# ---------------------------------------------------------
# We use a specific run command to handle suppressions and output
- name: Run Fuzzer
id: fuzz_run
run: |
# Set ASAN Options to use our suppressions file
export ASAN_OPTIONS="suppressions=.asan_suppressions:halt_on_error=0:detect_leaks=1"
# Run for 60 seconds max for CI demo (increase for real usage)
# -max_total_time=60
# -artifact_prefix= Where to save crashes
# -print_final_stats=1
./build/my_fuzz_target \
-max_total_time=60 \
-artifact_prefix=artifacts/crashes/ \
corpus_seed/ \
|| true
# Note: We use '|| true' because the fuzzer exits with error code 76 on crash.
# We handle the failure in the next step.
# ---------------------------------------------------------
# 4. CRASH TRIAGE
# ---------------------------------------------------------
- name: Check for Crashes
id: check_crashes
run: |
if [ "$(ls -A artifacts/crashes 2>/dev/null)" ]; then
echo "::error::Crashes found!"
echo "crash_detected=true" >> $GITHUB_OUTPUT
# Optional: Auto-minimize crash
# ./build/my_fuzz_target -minimize_crash=1 -runs=100 artifacts/crashes/crash-xxx
exit 1 # Fail the build
else
echo "No crashes found."
fi
# ---------------------------------------------------------
# 5. COVERAGE REPORT
# ---------------------------------------------------------
- name: Generate Coverage
if: always() # Run even if previous steps failed (to see coverage on bugs)
run: |
cd build
# Merge all .profraw files generated by the fuzzer run
llvm-profdata merge -sparse default.profraw -o coverage.profdata
# Generate HTML report
llvm-cov show ./my_fuzz_target \
-instr-profile=coverage.profdata \
-format=html \
-output-dir=../artifacts/coverage \
-ignore-filename-regex="(test|fuzz)" # Exclude test harness code
# Generate Summary for PR comment (optional)
llvm-cov report ./my_fuzz_target -instr-profile=coverage.profdata > ../artifacts/coverage_summary.txt
- name: Upload Coverage Artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: fuzz-results
path: artifacts/
- name: Upload New Corpus
uses: actions/upload-artifact@v3
if: success()
with:
name: fuzz-corpus
path: corpus_seed/
3. Detailed Component Breakdown
1. Crash Triage Strategy
In the workflow above, the fuzzer is allowed to continue running (via -max_total_time) even if a crash occurs (though libFuzzer usually stops on the first ASan violation unless configured otherwise).
- Artifact Prefix:
-artifact_prefix=artifacts/crashes/ ensures crashing inputs are saved to a specific folder.
- CI Logic: We decouple the "Fuzzer Exit Code" from the "Build Status". The fuzzer will exit with an error code if a crash occurs. We capture this in
|| true, check if files exist in the crash directory, and then fail the build with a clear error message.
- Minimization: In a production setup, you should add a step that takes a raw crash file and runs the fuzzer again with
-minimize_crash=1. This reduces the crashing input to the smallest possible bytes, making the bug report much easier to read.
2. Suppressions
Suppressions are critical for keeping the pipeline "green" while you work on known technical debt.
3. Coverage
Coverage in fuzzing is distinct from unit test coverage.
- Instrumentation: The code must be built with
-fprofile-instr-generate -fcoverage-mapping.
- Data Collection: While the fuzzer runs, LLVM generates
default.profraw files.
- Reporting: After the run,
llvm-cov translates the raw data into HTML or LCOV format.
- Goal: The goal is not usually 100% coverage (fuzzers rarely hit error handling logic for network timeouts, etc.), but monitoring edge coverage. If the coverage number stops growing despite adding new seeds, the fuzzer is stuck.
4. Advanced Triage: ClusterFuzz / OSS-Fuzz
If this local CI is insufficient (e.g., you need 24/7 fuzzing), you should offload this to OSS-Fuzz (for open source) or ClusterFuzz (internal).
If using OSS-Fuzz:
- Google runs the fuzzer 24/7.
- When a crash is found, it is automatically minimized.
- A bug is filed in the project's Issue Tracker.
- Your CI job simply pulls the latest corpus from OSS-Fuzz and runs coverage checks.