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
24 typedef void (*UserCallback)(const uint8_t *Data, size_t Size);
25 /** Simple C-like interface with a single user-supplied callback.
30 #include "FuzzerInterface.h"
32 void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
33 DoStuffWithData(Data, Size);
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);
43 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
45 class 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; }
57 class FuzzerRandomLibc : public FuzzerRandomBase {
59 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
60 void ResetSeed(int seed) override;
61 ~FuzzerRandomLibc() override {}
62 size_t Rand() override;
65 class MutationDispatcher {
67 MutationDispatcher(FuzzerRandomBase &Rand) : Rand(Rand) {}
68 /// Mutates data by shuffling bytes.
69 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
70 /// Mutates data by erasing a byte.
71 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
72 /// Mutates data by inserting a byte.
73 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
74 /// Mutates data by chanding one byte.
75 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
76 /// Mutates data by chanding one bit.
77 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
79 /// Applies one of the above mutations.
80 /// Returns the new size of data which could be up to MaxSize.
81 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
83 /// Creates a cross-over of two pieces of Data, returns its size.
84 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
85 size_t Size2, uint8_t *Out, size_t MaxOutSize);
88 FuzzerRandomBase &Rand;
91 // For backward compatibility only, deprecated.
92 static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
93 FuzzerRandomBase &Rand) {
94 MutationDispatcher MD(Rand);
95 return MD.Mutate(Data, Size, MaxSize);
98 /** An abstract class that allows to use user-supplied mutators with libFuzzer.
103 #include "FuzzerInterface.h"
104 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
106 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
107 // Must define the target function.
108 void TargetFunction(...) { ... }
109 // Optionally define the mutator.
110 size_t Mutate(...) { ... }
111 // Optionally define the CrossOver method.
112 size_t CrossOver(...) { ... }
115 int main(int argc, char **argv) {
117 fuzzer::FuzzerDriver(argc, argv, F);
121 class UserSuppliedFuzzer {
123 UserSuppliedFuzzer(); // Deprecated, don't use.
124 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
125 /// Executes the target function on 'Size' bytes of 'Data'.
126 virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
127 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
128 /// returns the new size of the data, which should be positive.
129 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
130 return MD.Mutate(Data, Size, MaxSize);
132 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
133 /// returns the number of bytes written, which should be positive.
134 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
135 const uint8_t *Data2, size_t Size2,
136 uint8_t *Out, size_t MaxOutSize) {
137 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
139 virtual ~UserSuppliedFuzzer();
141 FuzzerRandomBase &GetRand() { return *Rand; }
144 bool OwnRand = false;
145 FuzzerRandomBase *Rand;
146 MutationDispatcher MD;
149 /// Runs the fuzzing with the UserSuppliedFuzzer.
150 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
152 } // namespace fuzzer
154 #endif // LLVM_FUZZER_INTERFACE_H