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 /// 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.
33 #include "FuzzerInterface.h"
35 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
36 DoStuffWithData(Data, Size);
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);
47 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
49 class FuzzerRandomBase {
52 virtual ~FuzzerRandomBase(){};
53 virtual void ResetSeed(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; }
61 class FuzzerRandomLibc : public FuzzerRandomBase {
63 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
64 void ResetSeed(int seed) override;
65 ~FuzzerRandomLibc() override {}
66 size_t Rand() override;
69 class MutationDispatcher {
71 MutationDispatcher(FuzzerRandomBase &Rand);
72 ~MutationDispatcher();
73 /// Mutates data by shuffling bytes.
74 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
75 /// Mutates data by erasing a byte.
76 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
77 /// Mutates data by inserting a byte.
78 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
79 /// Mutates data by chanding one byte.
80 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
81 /// Mutates data by chanding one bit.
82 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
84 /// Mutates data by adding a word from the dictionary.
85 size_t Mutate_AddWordFromDictionary(uint8_t *Data, size_t Size,
88 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
90 /// Applies one of the above mutations.
91 /// Returns the new size of data which could be up to MaxSize.
92 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
94 /// Creates a cross-over of two pieces of Data, returns its size.
95 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
96 size_t Size2, uint8_t *Out, size_t MaxOutSize);
98 void AddWordToDictionary(const uint8_t *Word, size_t Size);
101 FuzzerRandomBase &Rand;
106 // For backward compatibility only, deprecated.
107 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
108 FuzzerRandomBase &Rand) {
109 MutationDispatcher MD(Rand);
110 return MD.Mutate(Data, Size, MaxSize);
113 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
118 #include "FuzzerInterface.h"
119 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
121 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
122 // Must define the target function.
123 int TargetFunction(...) { ...; return 0; }
124 // Optionally define the mutator.
125 size_t Mutate(...) { ... }
126 // Optionally define the CrossOver method.
127 size_t CrossOver(...) { ... }
130 int main(int argc, char **argv) {
132 fuzzer::FuzzerDriver(argc, argv, F);
136 class UserSuppliedFuzzer {
138 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
139 /// Executes the target function on 'Size' bytes of 'Data'.
140 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
141 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
142 /// returns the new size of the data, which should be positive.
143 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
144 return MD.Mutate(Data, Size, MaxSize);
146 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
147 /// returns the number of bytes written, which should be positive.
148 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
149 const uint8_t *Data2, size_t Size2,
150 uint8_t *Out, size_t MaxOutSize) {
151 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
153 virtual ~UserSuppliedFuzzer();
155 FuzzerRandomBase &GetRand() { return *Rand; }
157 MutationDispatcher &GetMD() { return MD; }
160 bool OwnRand = false;
161 FuzzerRandomBase *Rand;
162 MutationDispatcher MD;
165 /// Runs the fuzzing with the UserSuppliedFuzzer.
166 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
168 /// More C++-ish interface.
169 int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
170 int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
172 } // namespace fuzzer
174 #endif // LLVM_FUZZER_INTERFACE_H