1 //===- FuzzerInterface.h - Interface header for the Fuzzer ------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Define the interface between the Fuzzer and the library being tested.
10 //===----------------------------------------------------------------------===//
12 // WARNING: keep the interface free of STL or any other header-based C++ lib,
13 // to avoid bad interactions between the code used in the fuzzer and
14 // the code used in the target function.
16 #ifndef LLVM_FUZZER_INTERFACE_H
17 #define LLVM_FUZZER_INTERFACE_H
24 typedef void (*UserCallback)(const uint8_t *Data, size_t Size);
25 /** Simple C-like interface with a single user-supplied callback.
30 #include "FuzzerInterface.h"
32 void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
33 DoStuffWithData(Data, Size);
36 // Implement your own main() or use the one from FuzzerMain.cpp.
37 int main(int argc, char **argv) {
38 InitializeMeIfNeeded();
39 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
43 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
45 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
50 #include "FuzzerInterface.h"
51 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
53 // Must define the target function.
54 void TargetFunction(...) { ... }
55 // Optionally define the mutator.
56 size_t Mutate(...) { ... }
57 // Optionally define the CrossOver method.
58 size_t CrossOver(...) { ... }
61 int main(int argc, char **argv) {
63 fuzzer::FuzzerDriver(argc, argv, F);
67 class UserSuppliedFuzzer {
69 /// Executes the target function on 'Size' bytes of 'Data'.
70 virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
71 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
72 /// returns the new size of the data, which should be positive.
73 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
74 return BasicMutate(Data, Size, MaxSize);
76 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
77 /// returns the number of bytes written, which should be positive.
78 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
79 const uint8_t *Data2, size_t Size2,
80 uint8_t *Out, size_t MaxOutSize) {
81 return BasicCrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
83 virtual ~UserSuppliedFuzzer() {}
86 /// These can be called internally by Mutate and CrossOver.
87 size_t BasicMutate(uint8_t *Data, size_t Size, size_t MaxSize);
88 size_t BasicCrossOver(const uint8_t *Data1, size_t Size1,
89 const uint8_t *Data2, size_t Size2,
90 uint8_t *Out, size_t MaxOutSize);
93 /// Runs the fuzzing with the UserSuppliedFuzzer.
94 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
98 #endif // LLVM_FUZZER_INTERFACE_H