← Course Index

Coverage-Guided Fuzzing: AFL++ & libFuzzer
Edge Bitmaps, Sanitizers & Corpus Management

Instrumenting Native Binaries, Tracking Control-Flow Transitions via Shared Memory, and Triaging Memory Corruption with ASan/UBSan

Vulnerability Research & AppSec AFL++ & libFuzzer ASan / UBSan Triage

1. Evolutionary Grey-Box Fuzzing & Edge Coverage Bitmaps

Traditional black-box ("dumb") fuzzers blindly feed random byte streams into an application without observing internal control flow. When a parser checks a 4-byte magic header (e.g., 0x5A5A5546), blind random generation has only a 1-in-4.29-billion chance of passing the first branch. Coverage-guided (grey-box) fuzzers such as AFL++ and libFuzzer solve this by injecting lightweight compile-time instrumentation into every basic block, turning fuzzing into an evolutionary genetic search that retains inputs unlocking new code edges.

Edge Coverage vs. Basic Block Coverage: Recording only visited basic blocks misses control-flow transitions (e.g., A → B → D vs. A → C → D). AFL++ and libFuzzer track directed edges by hashing the previous and current basic block IDs into a shared-memory bitmap (trace_bits[65536]) via shared_mem[cur_loc ^ prev_loc]++; prev_location = cur_loc >> 1;, and quantizing loop hit counts into logarithmic power-of-two buckets.
1. CORPUS & MUTATOR Select Seed from Queue Bit/Byte Flips & Arithmetic Havoc & Splice Crossover Dictionary & CmpLog Tokens Mutated Candidate Input buf[0..N] → Target 2. INSTRUMENTED TARGET SanitizerCoverage Edge Hooks shm[cur ^ prev]++; prev = cur >> 1 ASan & UBSan Redzones Catch OOB read/write, UAF & UB 64KB trace_bits Evaluation New edge or hit bucket? → Save Signal / Sanitizer fault? → Crash 3. FEEDBACK & TRIAGE • New Edge → Enqueue Seed • No New Edge → Discard • Crash → Save Reproducer • Minimize (afl-cmin / tmin) • Continuous CI Regression Evolutionary Feedback Loop (Interesting Seeds Become Future Mutation Parents)
Figure 33.1: Coverage-Guided Grey-Box Fuzzing Loop Using Shared-Memory Edge Bitmaps and Sanitizer Instrumentation.

2. Architectural Comparison: AFL++ vs. libFuzzer

Architectural Aspect AFL++ (American Fuzzy Lop Plus Plus) libFuzzer (LLVM Compiler-RT)
Execution Model Out-of-process Fork-Server (fork() after target init) or high-speed shared-memory Persistent Mode (__AFL_LOOP). Strictly In-Process library linked directly into the binary; repeatedly invokes LLVMFuzzerTestOneInput() in a tight loop.
Speed & Fault Isolation Fork-server: ~1k–5k exec/s with full process isolation (crashes never terminate the fuzzer). Persistent mode: 20k–100k+ exec/s. Ultra-fast (10k–100k+ exec/s) with zero process-creation overhead, but a crash or OOM terminates the worker process.
Coverage & Power Schedules 64KB edge bitmap (trace_bits), CmpLog / RedQueen magic-byte solving, and schedules like fast, coe, and explore. LLVM SanitizerCoverage (inline-8bit-counters), value profiling, and the Entropic energy-allocation schedule.
Corpus Minimization Dedicated standalone utilities: afl-cmin (corpus distillation) and afl-tmin (single-input test case shrinking). Built-in flags: -merge=1 for corpus minimization and -minimize_crash=1 for crash reproducer shrinking.
Binary-Only & Target Fit Supports closed-source binaries via QEMU (-Q) and Frida (-O) modes; ideal for full CLI tools and stateful parsers. Requires compile-time source instrumentation; ideal for modular C/C++ libraries and OSS-Fuzz / ClusterFuzzLite CI pipelines.
Harness Determinism & State Hygiene: In persistent mode (__AFL_LOOP) or libFuzzer, heap memory and global variables persist across iterations. Always free allocated buffers and reset global state before returning from the harness—otherwise non-deterministic "phantom" edges and unreproducible crashes will degrade campaign stability.

3. Practical Fuzzing, Sanitizer & Corpus Workflows (10 Scenarios)

Example 1: Vulnerable C Target & Dual AFL++/libFuzzer Harness

C (fuzz_harness.c) — TLV Packet Parser Supporting Both libFuzzer & AFL++ Persistent Mode
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

/* Vulnerable parser: expects [MAGIC:"FUZZ"][LEN:2][PAYLOAD:N] */
int parse_packet(const uint8_t *data, size_t size) {
    if (size < 6) return -1;
    if (memcmp(data, "FUZZ", 4) != 0) return -1;

    uint16_t claimed_len = ((uint16_t)data[4] << 8) | data[5];
    if (claimed_len > 0 && claimed_len <= 1024) {
        uint8_t *buf = (uint8_t *)malloc(claimed_len);
        if (!buf) return -1;
        /* BUG: Trusts claimed_len without validating (size - 6 >= claimed_len) */
        memcpy(buf, data + 6, claimed_len);
        free(buf);
    }
    return 0;
}

#ifdef __AFL_COMPILER
__AFL_FUZZ_INIT();
int main(void) {
#ifdef __AFL_HAVE_MANUAL_CONTROL
    __AFL_INIT();
#endif
    uint8_t *buf = __AFL_FUZZ_TESTCASE_BUF;
    while (__AFL_LOOP(10000)) {
        size_t len = __AFL_FUZZ_TESTCASE_LEN;
        parse_packet(buf, len);
    }
    return 0;
}
#else
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    parse_packet(data, size);
    return 0;
}
#endif
SAMPLE OUTPUT
[HARNESS READY] Supports __AFL_LOOP(10000) shared-memory mode and LLVMFuzzerTestOneInput()

Example 2: Compiling Target & CmpLog Binaries with AFL++ (afl-clang-fast)

Bash (afl-clang-fast) — Building ASan-Instrumented & CmpLog Binaries
AFL_USE_ASAN=1 AFL_USE_UBSAN=1 afl-clang-fast -g -O2 fuzz_harness.c -o target_afl_asan
AFL_LLVM_CMPLOG=1 afl-clang-fast -g -O2 fuzz_harness.c -o target_afl_cmplog
SAMPLE OUTPUT
afl-cc++4.21c by Michal Zalewski, Marc Heuse, Heiko Eissfeldt, Andrea Fioraldi
[+] Instrumented 14 locations with no collisions (on average) of 65536 collision-free locations (100.00%).
[+] Enabled AddressSanitizer and UndefinedBehaviorSanitizer.

Example 3: Running an AFL++ Fuzzing Campaign with Seed Corpus & CmpLog

Bash (afl-fuzz) — Launching Persistent Fuzzing with CmpLog Magic-Byte Solving
mkdir -p seeds_in findings_out
printf "INIT\x00\x02OK" > seeds_in/seed_01
afl-fuzz -i seeds_in -o findings_out -c ./target_afl_cmplog -- ./target_afl_asan
SAMPLE OUTPUT
[+] American Fuzzy Lop ++4.21c {default} (target_afl_asan) [explore]
[*] Shared memory fuzzing feature is enabled!
[+] All set and ready to roll!
[cycle 1] total execs: 14,820 | exec speed: 28,450/sec | corpus: 9 | saved crashes: 1

Example 4: Compiling a libFuzzer Target with ASan and UBSan

Bash (clang) — Building In-Process Fuzzer with SanitizerCoverage + ASan + UBSan
clang -g -O1 -fno-omit-frame-pointer \
  -fsanitize=fuzzer,address,undefined \
  fuzz_harness.c -o target_libfuzzer
SAMPLE OUTPUT
[BUILD OK] Linked libFuzzer runtime with inline-8bit-counters, AddressSanitizer, and UBSan

Example 5: Running libFuzzer & Reproducing a Crash Artifact

Bash (libFuzzer) — Executing Campaign & Replaying Discovered Crash Input
mkdir -p corpus_lf crashes_lf
printf "INIT\x00\x02OK" > corpus_lf/seed_01
./target_libfuzzer corpus_lf/ -artifact_prefix=crashes_lf/ -max_len=2048
SAMPLE OUTPUT
INFO: Seed: 2894012341
#2      INITED cov: 3 ft: 4 corp: 1/8b exec/s: 0 rss: 31Mb
#412    NEW    cov: 7 ft: 9 corp: 5/40b lim: 8 exec/s: 0 rss: 32Mb L: 8/8 MS: 2 ChangeByte-ChangeASCIIInt-
==19402==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000018 at pc 0x55a1b2c04819
READ of size 64 at 0x602000000018 thread T0
artifact_prefix='crashes_lf/'; Test unit written to crashes_lf/crash-7f9a2c4b8e11d039

Example 6: Creating a Fuzzing Dictionary for Structured Protocols

Dictionary (tlv_protocol.dict) — Supplying Magic Tokens & Boundary Values
# Protocol magic headers and edge-case integer lengths
magic_hdr="FUZZ"
alt_hdr="TEST"
len_zero="\x00\x00"
len_max_valid="\x04\x00"
len_overflow="\xff\xff"
SAMPLE OUTPUT
# Usage:
# afl-fuzz -i seeds_in -o findings_out -x tlv_protocol.dict -- ./target_afl_asan
# ./target_libfuzzer corpus_lf/ -dict=tlv_protocol.dict

Example 7: Minimizing a Redundant Corpus (afl-cmin & libFuzzer -merge=1)

Bash — Distilling Overlapping Seeds into a Minimal High-Coverage Set
# AFL++ corpus minimization
afl-cmin -i findings_out/default/queue -o corpus_minimized -- ./target_afl_asan

# libFuzzer equivalent corpus merge/distillation
mkdir -p corpus_lf_min
./target_libfuzzer -merge=1 corpus_lf_min/ corpus_lf/
SAMPLE OUTPUT
[*] Found 428 input files in 'findings_out/default/queue'.
[+] Narrowed down to 11 files, saved in 'corpus_minimized'.
MERGE-OUTER: 428 files, 11 in the initial corpus, 11 merged

Example 8: Shrinking a Crashing Test Case with afl-tmin & libFuzzer

Bash — Stripping Unnecessary Bytes While Preserving the Crash Signature
# Minimize an AFL++ crashing input
afl-tmin -i findings_out/default/crashes/id:000000,sig:06 -o min_crash.bin -- ./target_afl_asan

# Minimize a libFuzzer crashing input
./target_libfuzzer -minimize_crash=1 -max_total_time=30 crashes_lf/crash-7f9a2c4b8e11d039
SAMPLE OUTPUT
[*] Target binary called by afl-tmin: ./target_afl_asan
[*] Performing dry run (mem limit = 0 MB, timeout = 1000 ms)...
[+] Program terminates with signal 6 in dry run - good!
[+] File size reduced from 512 bytes to 7 bytes -> saved to min_crash.bin

Example 9: Triaging Sanitizer Crash Reports & Symbolizing Stack Traces

Bash (ASan / llvm-symbolizer) — Deterministic Crash Replay & Root-Cause Inspection
ASAN_OPTIONS=symbolize=1:abort_on_error=1:detect_leaks=0 \
  ./target_libfuzzer min_crash.bin
SAMPLE OUTPUT
==19844==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000000017
READ of size 128 at 0x602000000017 thread T0
    #0 0x55b418a12410 in __asan_memcpy
    #1 0x55b418a491c2 in parse_packet /src/fuzz_harness.c:16:9
    #2 0x55b418a49288 in LLVMFuzzerTestOneInput /src/fuzz_harness.c:35:5
SUMMARY: AddressSanitizer: heap-buffer-overflow /src/fuzz_harness.c:16:9 in parse_packet

Example 10: Continuous Fuzzing in CI with ClusterFuzzLite (GitHub Actions YAML)

YAML (.github/workflows/cflite.yml) — Automated Pull-Request & Batch Fuzzing
name: ClusterFuzzLite Continuous Fuzzing
on: [pull_request]
jobs:
  fuzzing:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build Fuzzers
        uses: google/clusterfuzzlite/actions/build_fuzzers@v1
        with:
          sanitizer: address
      - name: Run Short PR Fuzzing Campaign
        uses: google/clusterfuzzlite/actions/run_fuzzers@v1
        with:
          fuzz-seconds: 300
          mode: code-change
          sanitizer: address
SAMPLE OUTPUT
[ClusterFuzzLite] Built target_libfuzzer (sanitizer=address)
[ClusterFuzzLite] Executed 3,420,110 inputs in 300s | New coverage: +4 edges | Crashes: 0

4. Interactive Fuzzing Loop & Coverage Simulator

Select a fuzzing campaign stage below to inspect the instrumentation bitmap, mutation mechanics, or sanitizer triage workflow:

← Previous: Course Modules Web Application Fuzzing & Automated Validation →