Skip to content
systemcoding.dev
Security

Split fuzz input into typed values with FuzzedDataProvider

Turn a raw fuzzer byte buffer into integers, ranges and strings with FuzzedDataProvider from LLVM's libFuzzer headers instead of parsing by hand.

1 min read
Summarize in AI

A fuzzer hands your target a flat array of bytes, but most code wants a mode flag, a small integer and a string. Slicing the array by hand makes every mutation break the layout. FuzzedDataProvider consumes the buffer front to back and returns typed values, shrinking gracefully when the input runs out, so each byte keeps its meaning as the corpus evolves.

target.cppcpp
#include <cstddef>
#include <cstdint>
#include <string>
#include <fuzzer/FuzzedDataProvider.h>

bool run(uint8_t mode, int n, const std::string &s);

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
    FuzzedDataProvider fdp(data, size);
    uint8_t mode = fdp.ConsumeIntegral<uint8_t>();
    int n = fdp.ConsumeIntegralInRange<int>(0, 64);
    std::string s = fdp.ConsumeRandomLengthString(32);
    run(mode, n, s);
    return 0;
}
Shell
clang++ -g -O1 -fsanitize=fuzzer,address target.cpp lib.cpp -o fuzz
./fuzz -runs=1 2>&1 | tail -1
Text
Done 1 runs in 0 second(s)

ConsumeIntegralInRange is bounds-safe by construction, so you never need to reject out-of-range input. The header ships with Clang's compiler-rt and also works with AFL++ through its libFuzzer compatibility layer. Consume the variable-length string last, because ConsumeRemainingBytesAsString takes whatever is left. See the FuzzedDataProvider header.

Also inDebugging
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.