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);
113 void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
114 void SetCorpus(const std::vector<Unit> *Corpus);
117 FuzzerRandomBase &Rand;
122 // For backward compatibility only, deprecated.
123 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
124 FuzzerRandomBase &Rand) {
125 MutationDispatcher MD(Rand);
126 return MD.Mutate(Data, Size, MaxSize);
129 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
134 #include "FuzzerInterface.h"
135 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
137 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
138 // Must define the target function.
139 int TargetFunction(...) { ...; return 0; }
140 // Optionally define the mutator.
141 size_t Mutate(...) { ... }
142 // Optionally define the CrossOver method.
143 size_t CrossOver(...) { ... }
146 int main(int argc, char **argv) {
148 fuzzer::FuzzerDriver(argc, argv, F);
152 class UserSuppliedFuzzer {
154 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
155 /// Executes the target function on 'Size' bytes of 'Data'.
156 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
157 virtual void StartMutationSequence() { MD.StartMutationSequence(); }
158 virtual void PrintMutationSequence() { MD.PrintMutationSequence(); }
159 virtual void SetCorpus(const std::vector<Unit> *Corpus) {
160 MD.SetCorpus(Corpus);
162 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
163 /// returns the new size of the data, which should be positive.
164 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
165 return MD.Mutate(Data, Size, MaxSize);
167 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
168 /// returns the number of bytes written, which should be positive.
169 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
170 const uint8_t *Data2, size_t Size2,
171 uint8_t *Out, size_t MaxOutSize) {
172 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
174 virtual ~UserSuppliedFuzzer();
176 FuzzerRandomBase &GetRand() { return *Rand; }
178 MutationDispatcher &GetMD() { return MD; }
181 bool OwnRand = false;
182 FuzzerRandomBase *Rand;
183 MutationDispatcher MD;
186 /// Runs the fuzzing with the UserSuppliedFuzzer.
187 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
189 /// More C++-ish interface.
190 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
191 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
193 } // namespace fuzzer
195 #endif // LLVM_FUZZER_INTERFACE_H