[libFuzzer] print successfull mutations sequences
[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 <cstddef>
20 #include <cstdint>
21 #include <vector>
22 #include <string>
23
24 namespace fuzzer {
25
26 /// Returns an int 0. Values other than zero are reserved for future.
27 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
28 /** Simple C-like interface with a single user-supplied callback.
29
30 Usage:
31
32 #\code
33 #include "FuzzerInterface.h"
34
35 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
36   DoStuffWithData(Data, Size);
37   return 0;
38 }
39
40 // Implement your own main() or use the one from FuzzerMain.cpp.
41 int main(int argc, char **argv) {
42   InitializeMeIfNeeded();
43   return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
44 }
45 #\endcode
46 */
47 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
48
49 class FuzzerRandomBase {
50  public:
51   FuzzerRandomBase(){}
52   virtual ~FuzzerRandomBase(){};
53   virtual void ResetSeed(unsigned int seed) = 0;
54   // Return a random number.
55   virtual size_t Rand() = 0;
56   // Return a random number in range [0,n).
57   size_t operator()(size_t n) { return n ? Rand() % n : 0; }
58   bool RandBool() { return Rand() % 2; }
59 };
60
61 class FuzzerRandomLibc : public FuzzerRandomBase {
62  public:
63   FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); }
64   void ResetSeed(unsigned int seed) override;
65   ~FuzzerRandomLibc() override {}
66   size_t Rand() override;
67 };
68
69 class MutationDispatcher {
70  public:
71   MutationDispatcher(FuzzerRandomBase &Rand);
72   ~MutationDispatcher();
73   /// Indicate that we are about to start a new sequence of mutations.
74   void StartMutationSequence();
75   /// Print the current sequence of mutations.
76   void PrintMutationSequence();
77   /// Mutates data by shuffling bytes.
78   size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
79   /// Mutates data by erasing a byte.
80   size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
81   /// Mutates data by inserting a byte.
82   size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
83   /// Mutates data by chanding one byte.
84   size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
85   /// Mutates data by chanding one bit.
86   size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
87
88   /// Mutates data by adding a word from the dictionary.
89   size_t Mutate_AddWordFromDictionary(uint8_t *Data, size_t Size,
90                                       size_t MaxSize);
91
92   size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
93
94   /// Applies one of the above mutations.
95   /// Returns the new size of data which could be up to MaxSize.
96   size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
97
98   /// Creates a cross-over of two pieces of Data, returns its size.
99   size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
100                    size_t Size2, uint8_t *Out, size_t MaxOutSize);
101
102   void AddWordToDictionary(const uint8_t *Word, size_t Size);
103
104  private:
105   FuzzerRandomBase &Rand;
106   struct Impl;
107   Impl *MDImpl;
108 };
109
110 // For backward compatibility only, deprecated.
111 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
112                             FuzzerRandomBase &Rand) {
113   MutationDispatcher MD(Rand);
114   return MD.Mutate(Data, Size, MaxSize);
115 }
116
117 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
118
119 Usage:
120
121 #\code
122 #include "FuzzerInterface.h"
123 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
124  public:
125   MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
126   // Must define the target function.
127   int TargetFunction(...) { ...; return 0; }
128   // Optionally define the mutator.
129   size_t Mutate(...) { ... }
130   // Optionally define the CrossOver method.
131   size_t CrossOver(...) { ... }
132 };
133
134 int main(int argc, char **argv) {
135   MyFuzzer F;
136   fuzzer::FuzzerDriver(argc, argv, F);
137 }
138 #\endcode
139 */
140 class UserSuppliedFuzzer {
141  public:
142   UserSuppliedFuzzer(FuzzerRandomBase *Rand);
143   /// Executes the target function on 'Size' bytes of 'Data'.
144   virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
145   virtual void StartMutationSequence() { MD.StartMutationSequence(); }
146   virtual void PrintMutationSequence() { MD.PrintMutationSequence(); }
147   /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
148   /// returns the new size of the data, which should be positive.
149   virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
150     return MD.Mutate(Data, Size, MaxSize);
151   }
152   /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
153   /// returns the number of bytes written, which should be positive.
154   virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
155                            const uint8_t *Data2, size_t Size2,
156                            uint8_t *Out, size_t MaxOutSize) {
157     return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
158   }
159   virtual ~UserSuppliedFuzzer();
160
161   FuzzerRandomBase &GetRand() { return *Rand; }
162
163   MutationDispatcher &GetMD() { return MD; }
164
165  private:
166   bool OwnRand = false;
167   FuzzerRandomBase *Rand;
168   MutationDispatcher MD;
169 };
170
171 /// Runs the fuzzing with the UserSuppliedFuzzer.
172 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
173
174 /// More C++-ish interface.
175 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
176 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
177
178 }  // namespace fuzzer
179
180 #endif  // LLVM_FUZZER_INTERFACE_H