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