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 void (*UserCallback)(const uint8_t *Data, size_t Size);
27 /** Simple C-like interface with a single user-supplied callback.
32 #include "FuzzerInterface.h"
34 void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
35 DoStuffWithData(Data, Size);
38 // Implement your own main() or use the one from FuzzerMain.cpp.
39 int main(int argc, char **argv) {
40 InitializeMeIfNeeded();
41 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
45 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
47 class FuzzerRandomBase {
50 virtual ~FuzzerRandomBase(){};
51 virtual void ResetSeed(int seed) = 0;
52 // Return a random number.
53 virtual size_t Rand() = 0;
54 // Return a random number in range [0,n).
55 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
56 bool RandBool() { return Rand() % 2; }
59 class FuzzerRandomLibc : public FuzzerRandomBase {
61 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
62 void ResetSeed(int seed) override;
63 ~FuzzerRandomLibc() override {}
64 size_t Rand() override;
67 class MutationDispatcher {
69 MutationDispatcher(FuzzerRandomBase &Rand);
70 ~MutationDispatcher();
71 /// Mutates data by shuffling bytes.
72 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
73 /// Mutates data by erasing a byte.
74 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
75 /// Mutates data by inserting a byte.
76 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
77 /// Mutates data by chanding one byte.
78 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
79 /// Mutates data by chanding one bit.
80 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
82 /// Mutates data by adding a word from the dictionary.
83 size_t Mutate_AddWordFromDictionary(uint8_t *Data, size_t Size,
86 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
88 /// Applies one of the above mutations.
89 /// Returns the new size of data which could be up to MaxSize.
90 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
92 /// Creates a cross-over of two pieces of Data, returns its size.
93 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
94 size_t Size2, uint8_t *Out, size_t MaxOutSize);
96 void AddWordToDictionary(const uint8_t *Word, size_t Size);
99 FuzzerRandomBase &Rand;
104 // For backward compatibility only, deprecated.
105 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
106 FuzzerRandomBase &Rand) {
107 MutationDispatcher MD(Rand);
108 return MD.Mutate(Data, Size, MaxSize);
111 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
116 #include "FuzzerInterface.h"
117 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
119 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
120 // Must define the target function.
121 void TargetFunction(...) { ... }
122 // Optionally define the mutator.
123 size_t Mutate(...) { ... }
124 // Optionally define the CrossOver method.
125 size_t CrossOver(...) { ... }
128 int main(int argc, char **argv) {
130 fuzzer::FuzzerDriver(argc, argv, F);
134 class UserSuppliedFuzzer {
136 UserSuppliedFuzzer(); // Deprecated, don't use.
137 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
138 /// Executes the target function on 'Size' bytes of 'Data'.
139 virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
140 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
141 /// returns the new size of the data, which should be positive.
142 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
143 return MD.Mutate(Data, Size, MaxSize);
145 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
146 /// returns the number of bytes written, which should be positive.
147 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
148 const uint8_t *Data2, size_t Size2,
149 uint8_t *Out, size_t MaxOutSize) {
150 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
152 virtual ~UserSuppliedFuzzer();
154 FuzzerRandomBase &GetRand() { return *Rand; }
156 MutationDispatcher &GetMD() { return MD; }
159 bool OwnRand = false;
160 FuzzerRandomBase *Rand;
161 MutationDispatcher MD;
164 /// Runs the fuzzing with the UserSuppliedFuzzer.
165 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
167 /// More C++-ish interface.
168 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
169 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
171 } // namespace fuzzer
173 #endif // LLVM_FUZZER_INTERFACE_H