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
26 typedef void (*DeprecatedUserCallback)(const uint8_t *Data, size_t Size);
27 /// Returns an int 0. Values other than zero are reserved for future.
28 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
29 /** Simple C-like interface with a single user-supplied callback.
34 #include "FuzzerInterface.h"
36 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
37 DoStuffWithData(Data, Size);
41 // Implement your own main() or use the one from FuzzerMain.cpp.
42 int main(int argc, char **argv) {
43 InitializeMeIfNeeded();
44 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
48 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
49 int FuzzerDriver(int argc, char **argv, DeprecatedUserCallback Callback);
51 class FuzzerRandomBase {
54 virtual ~FuzzerRandomBase(){};
55 virtual void ResetSeed(int seed) = 0;
56 // Return a random number.
57 virtual size_t Rand() = 0;
58 // Return a random number in range [0,n).
59 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
60 bool RandBool() { return Rand() % 2; }
63 class FuzzerRandomLibc : public FuzzerRandomBase {
65 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
66 void ResetSeed(int seed) override;
67 ~FuzzerRandomLibc() override {}
68 size_t Rand() override;
71 class MutationDispatcher {
73 MutationDispatcher(FuzzerRandomBase &Rand);
74 ~MutationDispatcher();
75 /// Mutates data by shuffling bytes.
76 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
77 /// Mutates data by erasing a byte.
78 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
79 /// Mutates data by inserting a byte.
80 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
81 /// Mutates data by chanding one byte.
82 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
83 /// Mutates data by chanding one bit.
84 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
86 /// Mutates data by adding a word from the dictionary.
87 size_t Mutate_AddWordFromDictionary(uint8_t *Data, size_t Size,
90 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
92 /// Applies one of the above mutations.
93 /// Returns the new size of data which could be up to MaxSize.
94 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
96 /// Creates a cross-over of two pieces of Data, returns its size.
97 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
98 size_t Size2, uint8_t *Out, size_t MaxOutSize);
100 void AddWordToDictionary(const uint8_t *Word, size_t Size);
103 FuzzerRandomBase &Rand;
108 // For backward compatibility only, deprecated.
109 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
110 FuzzerRandomBase &Rand) {
111 MutationDispatcher MD(Rand);
112 return MD.Mutate(Data, Size, MaxSize);
115 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
120 #include "FuzzerInterface.h"
121 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
123 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
124 // Must define the target function.
125 int TargetFunction(...) { ...; return 0; }
126 // Optionally define the mutator.
127 size_t Mutate(...) { ... }
128 // Optionally define the CrossOver method.
129 size_t CrossOver(...) { ... }
132 int main(int argc, char **argv) {
134 fuzzer::FuzzerDriver(argc, argv, F);
138 class UserSuppliedFuzzer {
140 UserSuppliedFuzzer(); // Deprecated, don't use.
141 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
142 /// Executes the target function on 'Size' bytes of 'Data'.
143 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
144 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
145 /// returns the new size of the data, which should be positive.
146 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
147 return MD.Mutate(Data, Size, MaxSize);
149 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
150 /// returns the number of bytes written, which should be positive.
151 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
152 const uint8_t *Data2, size_t Size2,
153 uint8_t *Out, size_t MaxOutSize) {
154 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
156 virtual ~UserSuppliedFuzzer();
158 FuzzerRandomBase &GetRand() { return *Rand; }
160 MutationDispatcher &GetMD() { return MD; }
163 bool OwnRand = false;
164 FuzzerRandomBase *Rand;
165 MutationDispatcher MD;
168 /// Runs the fuzzing with the UserSuppliedFuzzer.
169 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
171 /// More C++-ish interface.
172 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
173 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
175 } // namespace fuzzer
177 #endif // LLVM_FUZZER_INTERFACE_H