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 std::vector<uint8_t> Unit;
28 /// Returns an int 0. Values other than zero are reserved for future.
29 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
30 /** Simple C-like interface with a single user-supplied callback.
35 #include "FuzzerInterface.h"
37 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
38 DoStuffWithData(Data, Size);
42 // Implement your own main() or use the one from FuzzerMain.cpp.
43 int main(int argc, char **argv) {
44 InitializeMeIfNeeded();
45 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
49 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
51 class FuzzerRandomBase {
54 virtual ~FuzzerRandomBase(){};
55 virtual void ResetSeed(unsigned 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(unsigned int seed) { ResetSeed(seed); }
66 void ResetSeed(unsigned int seed) override;
67 ~FuzzerRandomLibc() override {}
68 size_t Rand() override;
71 class MutationDispatcher {
73 MutationDispatcher(FuzzerRandomBase &Rand);
74 ~MutationDispatcher();
75 /// Indicate that we are about to start a new sequence of mutations.
76 void StartMutationSequence();
77 /// Print the current sequence of mutations.
78 void PrintMutationSequence();
79 /// Mutates data by shuffling bytes.
80 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
81 /// Mutates data by erasing a byte.
82 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
83 /// Mutates data by inserting a byte.
84 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
85 /// Mutates data by chanding one byte.
86 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
87 /// Mutates data by chanding one bit.
88 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
90 /// Mutates data by adding a word from the manual dictionary.
91 size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
94 /// Mutates data by adding a word from the automatic dictionary.
95 size_t Mutate_AddWordFromAutoDictionary(uint8_t *Data, size_t Size,
98 /// Tries to find an ASCII integer in Data, changes it to another ASCII int.
99 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
101 /// CrossOver Data with some other element of the corpus.
102 size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
104 /// Applies one of the above mutations.
105 /// Returns the new size of data which could be up to MaxSize.
106 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
108 /// Creates a cross-over of two pieces of Data, returns its size.
109 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
110 size_t Size2, uint8_t *Out, size_t MaxOutSize);
112 void AddWordToManualDictionary(const Unit &Word);
114 void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
115 void ClearAutoDictionary();
117 void SetCorpus(const std::vector<Unit> *Corpus);
120 FuzzerRandomBase &Rand;
125 // For backward compatibility only, deprecated.
126 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
127 FuzzerRandomBase &Rand) {
128 MutationDispatcher MD(Rand);
129 return MD.Mutate(Data, Size, MaxSize);
132 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
137 #include "FuzzerInterface.h"
138 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
140 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
141 // Must define the target function.
142 int TargetFunction(...) { ...; return 0; }
143 // Optionally define the mutator.
144 size_t Mutate(...) { ... }
145 // Optionally define the CrossOver method.
146 size_t CrossOver(...) { ... }
149 int main(int argc, char **argv) {
151 fuzzer::FuzzerDriver(argc, argv, F);
155 class UserSuppliedFuzzer {
157 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
158 /// Executes the target function on 'Size' bytes of 'Data'.
159 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
160 virtual void StartMutationSequence() { MD.StartMutationSequence(); }
161 virtual void PrintMutationSequence() { MD.PrintMutationSequence(); }
162 virtual void SetCorpus(const std::vector<Unit> *Corpus) {
163 MD.SetCorpus(Corpus);
165 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
166 /// returns the new size of the data, which should be positive.
167 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
168 return MD.Mutate(Data, Size, MaxSize);
170 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
171 /// returns the number of bytes written, which should be positive.
172 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
173 const uint8_t *Data2, size_t Size2,
174 uint8_t *Out, size_t MaxOutSize) {
175 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
177 virtual ~UserSuppliedFuzzer();
179 FuzzerRandomBase &GetRand() { return *Rand; }
181 MutationDispatcher &GetMD() { return MD; }
184 bool OwnRand = false;
185 FuzzerRandomBase *Rand;
186 MutationDispatcher MD;
189 /// Runs the fuzzing with the UserSuppliedFuzzer.
190 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
192 /// More C++-ish interface.
193 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
194 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
196 } // namespace fuzzer
198 #endif // LLVM_FUZZER_INTERFACE_H