<< All versions

Skill v1.0.0

currentAutomated scan100/100
tottenjordan/me-skittles/harness-writing
──Details
PublishedNovember 21, 2026 at 04:29 PM
Content Hashsha256:f87a8f596d1adf65...
Git SHA9b9d51394066
──Files
Files (1 file, 9.2 KB)
SKILL.md9.2 KBactive
SKILL.md · 270 lines · 9.2 KB

name: harness-writing type: technique description: > Techniques for writing effective fuzzing harnesses across languages. Use when creating new fuzz targets or improving existing harness code. metadata: version: "1.0.0"


Writing Fuzzing Harnesses

A fuzzing harness is the entrypoint function that receives random data from the fuzzer and routes it to your system under test (SUT). The quality of your harness directly determines which code paths get exercised and whether critical bugs are found. A poorly written harness can miss entire subsystems or produce non-reproducible crashes.

Overview

The harness is the bridge between the fuzzer's random byte generation and your application's API. It must parse raw bytes into meaningful inputs, call target functions, and handle edge cases gracefully. The most important part of any fuzzing setup is the harness—if written poorly, critical parts of your application may not be covered.

Key Concepts

ConceptDescription
HarnessFunction that receives fuzzer input and calls target code under test
SUTSystem Under Test—the code being fuzzed
Entry pointFunction signature required by the fuzzer (e.g., LLVMFuzzerTestOneInput)
FuzzedDataProviderHelper class for structured extraction of typed data from raw bytes
DeterminismProperty that ensures same input always produces same behavior
Interleaved fuzzingSingle harness that exercises multiple operations based on input

When to Apply

Apply this technique when:

  • Creating a new fuzz target for the first time
  • Fuzz campaign has low code coverage or isn't finding bugs
  • Crashes found during fuzzing are not reproducible
  • Target API requires complex or structured inputs
  • Multiple related functions should be tested together

Skip this technique when:

  • Using existing well-tested harnesses from your project
  • Tool provides automatic harness generation that meets your needs
  • Target already has comprehensive fuzzing infrastructure

Quick Reference

TaskPattern
Minimal C++ harnessextern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
Minimal Rust harness`fuzz_target!(data: &[u8]{ ... })`
Size validationif (size < MIN_SIZE) return 0;
Cast to integersuint32_t val = *(uint32_t*)(data);
Use FuzzedDataProviderFuzzedDataProvider fuzzed_data(data, size);
Extract typed data (C++)auto val = fuzzed_data.ConsumeIntegral<uint32_t>();
Extract string (C++)auto str = fuzzed_data.ConsumeBytesWithTerminator<char>(32, 0xFF);

Tool-Specific Guidance

libFuzzer

Harness signature:

cpp
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
// Your code here
return 0; // Non-zero return is reserved for future use
}

Compilation:

bash
clang++ -fsanitize=fuzzer,address -g harness.cc -o fuzz_target

Integration tips:

  • Use FuzzedDataProvider.h for structured input extraction
  • Compile with -fsanitize=fuzzer to link the fuzzing runtime
  • Add sanitizers (-fsanitize=address,undefined) to detect more bugs
  • Use -g for better stack traces when crashes occur
  • libFuzzer can start with empty corpus—no seed inputs required

Running:

bash
./fuzz_target corpus_dir/

Resources:

AFL++

AFL++ supports multiple harness styles. For best performance, use persistent mode:

Persistent mode harness:

cpp
#include <unistd.h>
int main(int argc, char **argv) {
#ifdef __AFL_HAVE_MANUAL_CONTROL
__AFL_INIT();
#endif
unsigned char buf[MAX_SIZE];
while (__AFL_LOOP(10000)) {
// Read input from stdin
ssize_t len = read(0, buf, sizeof(buf));
if (len <= 0) break;
// Call target function
target_function(buf, len);
}
return 0;
}

Compilation:

bash
afl-clang-fast++ -g harness.cc -o fuzz_target

Integration tips:

  • Use persistent mode (__AFL_LOOP) for 10-100x speedup
  • Consider deferred initialization (__AFL_INIT()) to skip setup overhead
  • AFL++ requires at least one seed input in the corpus directory
  • Use AFL_USE_ASAN=1 or AFL_USE_UBSAN=1 for sanitizer builds

Running:

bash
afl-fuzz -i seeds/ -o findings/ -- ./fuzz_target

cargo-fuzz (Rust)

Harness signature:

rust
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: &[u8]| {
// Your code here
});

With structured input (arbitrary crate):

rust
#![no_main]
use libfuzzer_sys::fuzz_target;
fuzz_target!(|data: YourStruct| {
data.check();
});

Creating harness:

bash
cargo fuzz init
cargo fuzz add my_target

Integration tips:

  • Use arbitrary crate for automatic struct deserialization
  • cargo-fuzz wraps libFuzzer, so all libFuzzer features work
  • Compile with sanitizers automatically via cargo-fuzz
  • Harnesses go in fuzz/fuzz_targets/ directory

Running:

bash
cargo +nightly fuzz run my_target

Resources:

go-fuzz

Harness signature:

go
// +build gofuzz
package mypackage
func Fuzz(data []byte) int {
// Call target function
target(data)
// Return codes:
// -1 if input is invalid
// 0 if input is valid but not interesting
// 1 if input is interesting (e.g., added new coverage)
return 0
}

Building:

bash
go-fuzz-build

Integration tips:

  • Return 1 for inputs that add coverage (optional—fuzzer can detect automatically)
  • Return -1 for invalid inputs to deprioritize similar mutations
  • go-fuzz handles persistence automatically

Running:

bash
go-fuzz -bin=./mypackage-fuzz.zip -workdir=fuzz

Troubleshooting

IssueCauseSolution
Low executions/secHarness is too slow (logging, I/O, complexity)Profile harness, remove bottlenecks, mock I/O
No crashes foundCoverage not reaching buggy codeCheck coverage, improve harness to reach more paths
Non-reproducible crashesNon-determinism or global stateRemove randomness, reset globals between iterations
Fuzzer exits immediatelyHarness calls exit()Replace exit() with abort() or return error
Out of memory errorsMemory leaks in harness or SUTFree allocations, use leak sanitizer to find leaks
Crashes on empty inputHarness doesn't validate sizeAdd if (size < MIN_SIZE) return 0;
Corpus not growingInputs too constrained or format too strictUse FuzzedDataProvider or structure-aware fuzzing

Reference material

[references/patterns.md](references/patterns.md) — the step-by-step process and common harness patterns

[references/advanced.md](references/advanced.md) — advanced usage, practical rules, and anti-patterns

Related Skills

Tools That Use This Technique

SkillHow It Applies
libfuzzerUses LLVMFuzzerTestOneInput harness signature with FuzzedDataProvider
aflppSupports persistent mode harnesses with __AFL_LOOP for performance
cargo-fuzzUses Rust-specific fuzz_target! macro with arbitrary crate integration
atherisPython harness takes bytes, calls Python functions
ossfuzzRequires harnesses in specific directory structure for cloud fuzzing

Related Techniques

SkillRelationship
coverage-analysisMeasure harness effectiveness—are you reaching target code?
address-sanitizerDetects bugs found by harness (buffer overflows, use-after-free)
fuzzing-dictionaryProvide tokens to help fuzzer pass format checks in harness
fuzzing-obstaclesPatch SUT when it violates harness rules (exit, non-determinism)

Resources

Key External Resources

[Split Inputs in libFuzzer - Google Fuzzing Docs](https://github.com/google/fuzzing/blob/master/docs/split-inputs.md) Explains techniques for handling multiple input parameters in a single fuzzing harness, including use of magic separators and FuzzedDataProvider.

[Structure-Aware Fuzzing with Protocol Buffers](https://github.com/google/fuzzing/blob/master/docs/structure-aware-fuzzing.md) Advanced technique using protobuf as intermediate format with custom mutators to ensure fuzzer mutates message contents rather than format encoding.

[libFuzzer Documentation](https://llvm.org/docs/LibFuzzer.html) Official LLVM documentation covering harness requirements, best practices, and advanced features.

[cargo-fuzz Book](https://rust-fuzz.github.io/book/cargo-fuzz.html) Comprehensive guide to writing Rust fuzzing harnesses with cargo-fuzz and the arbitrary crate.

Video Resources

All versions