Taints the non-acquire RMW's store address with the load part
[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
114   void AddWordToAutoDictionary(const Unit &Word, size_t PositionHint);
115   void ClearAutoDictionary();
116
117   void SetCorpus(const std::vector<Unit> *Corpus);
118
119  private:
120   FuzzerRandomBase &Rand;
121   struct Impl;
122   Impl *MDImpl;
123 };
124
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);
130 }
131
132 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
133
134 Usage:
135
136 #\code
137 #include "FuzzerInterface.h"
138 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
139  public:
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(...) { ... }
147 };
148
149 int main(int argc, char **argv) {
150   MyFuzzer F;
151   fuzzer::FuzzerDriver(argc, argv, F);
152 }
153 #\endcode
154 */
155 class UserSuppliedFuzzer {
156  public:
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);
164   }
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);
169   }
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);
176   }
177   virtual ~UserSuppliedFuzzer();
178
179   FuzzerRandomBase &GetRand() { return *Rand; }
180
181   MutationDispatcher &GetMD() { return MD; }
182
183  private:
184   bool OwnRand = false;
185   FuzzerRandomBase *Rand;
186   MutationDispatcher MD;
187 };
188
189 /// Runs the fuzzing with the UserSuppliedFuzzer.
190 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
191
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);
195
196 }  // namespace fuzzer
197
198 #endif  // LLVM_FUZZER_INTERFACE_H