Instrumenting Native Binaries, Tracking Control-Flow Transitions via Shared Memory, and Triaging Memory Corruption with ASan/UBSan
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.
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.
| 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. |
__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.
#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
[HARNESS READY] Supports __AFL_LOOP(10000) shared-memory mode and LLVMFuzzerTestOneInput()
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
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.
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
[+] 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
clang -g -O1 -fno-omit-frame-pointer \
-fsanitize=fuzzer,address,undefined \
fuzz_harness.c -o target_libfuzzer
[BUILD OK] Linked libFuzzer runtime with inline-8bit-counters, AddressSanitizer, and UBSan
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
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
# 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"
# Usage:
# afl-fuzz -i seeds_in -o findings_out -x tlv_protocol.dict -- ./target_afl_asan
# ./target_libfuzzer corpus_lf/ -dict=tlv_protocol.dict
# 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/
[*] 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
# 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
[*] 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
ASAN_OPTIONS=symbolize=1:abort_on_error=1:detect_leaks=0 \
./target_libfuzzer min_crash.bin
==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
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
[ClusterFuzzLite] Built target_libfuzzer (sanitizer=address)
[ClusterFuzzLite] Executed 3,420,110 inputs in 300s | New coverage: +4 edges | Crashes: 0
Select a fuzzing campaign stage below to inspect the instrumentation bitmap, mutation mechanics, or sanitizer triage workflow: