[libFuzzer] add a position hint to the dictionary-based mutator
[oota-llvm.git] / lib / Fuzzer / FuzzerInterface.h
1 //===- FuzzerInterface.h - Interface header for the Fuzzer ------*- C++ -* ===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Define the interface between the Fuzzer and the library being tested.
10 //===----------------------------------------------------------------------===//
11
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.
15
16 #ifndef LLVM_FUZZER_INTERFACE_H
17 #define LLVM_FUZZER_INTERFACE_H
18
19 #include <limits>
20 #include <cstddef>
21 #include <cstdint>
22 #include <vector>
23 #include <string>
24
25 namespace fuzzer {
26 typedef std::vector<uint8_t> Unit;
27
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.
31
32 Usage:
33
34 #\code
35 #include "FuzzerInterface.h"
36
37 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
38   DoStuffWithData(Data, Size);
39   return 0;
40 }
41
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);
46 }
47 #\endcode
48 */
49 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
50
51 class FuzzerRandomBase {
52  public:
53   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; }
61 };
62
63 class FuzzerRandomLibc : public FuzzerRandomBase {
64  public:
65   FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); }
66   void ResetSeed(unsigned int seed) override;
67   ~FuzzerRandomLibc() override {}
68   size_t Rand() override;
69 };
70
71 class MutationDispatcher {
72  public:
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);
89
90   /// Mutates data by adding a word from the manual dictionary.
91   size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
92                                             size_t MaxSize);
93
94   /// Mutates data by adding a word from the automatic dictionary.
95   size_t Mutate_AddWordFromAutoDictionary(uint8_t *Data, size_t Size,
96                                           size_t MaxSize);
97
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);
100
101   /// CrossOver Data with some other element of the corpus.
102   size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
103
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);
107
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);
111
112   void AddWordToManualDictionary(const Unit &Word);
113   void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
114   void SetCorpus(const std::vector<Unit> *Corpus);
115
116  private:
117   FuzzerRandomBase &Rand;
118   struct Impl;
119   Impl *MDImpl;
120 };
121
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);
127 }
128
129 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
130
131 Usage:
132
133 #\code
134 #include "FuzzerInterface.h"
135 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
136  public:
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(...) { ... }
144 };
145
146 int main(int argc, char **argv) {
147   MyFuzzer F;
148   fuzzer::FuzzerDriver(argc, argv, F);
149 }
150 #\endcode
151 */
152 class UserSuppliedFuzzer {
153  public:
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);
161   }
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);
166   }
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);
173   }
174   virtual ~UserSuppliedFuzzer();
175
176   FuzzerRandomBase &GetRand() { return *Rand; }
177
178   MutationDispatcher &GetMD() { return MD; }
179
180  private:
181   bool OwnRand = false;
182   FuzzerRandomBase *Rand;
183   MutationDispatcher MD;
184 };
185
186 /// Runs the fuzzing with the UserSuppliedFuzzer.
187 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
188
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);
192
193 }  // namespace fuzzer
194
195 #endif  // LLVM_FUZZER_INTERFACE_H