[lib/Fuzzer] extend the fuzzer interface to allow user-supplied mutators
[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 // Simple C-like interface with a single user-supplied callback.
25 /* Usage: ---------------------------------------------------------------------
26 #include "FuzzerInterface.h"
27
28 void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
29   DoStuffWithData(Data, Size);
30 }
31
32 // Implement your own main() or use the one from FuzzerMain.cpp.
33 int main(int argc, char **argv) {
34   InitializeMeIfNeeded();
35   return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
36 }
37 ----------------------------------------------------------------------------- */
38 typedef void (*UserCallback)(const uint8_t *Data, size_t Size);
39 int FuzzerDriver(int argc, char **argv, UserCallback Callback);
40
41 // An abstract class that allows to use user-supplied mutators with libFuzzer.
42 /* Usage: ---------------------------------------------------------------------
43 #include "FuzzerInterface.h"
44 class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
45  public:
46   // Must define the target function.
47   void TargetFunction(...) { ... }
48   // Optionally define the mutator.
49   size_t Mutate(...) { ... }
50   // Optionally define the CrossOver method.
51   size_t CrossOver(...) { ... }
52 };
53
54 int main(int argc, char **argv) {
55   MyFuzzer F;
56   fuzzer::FuzzerDriver(argc, argv, F);
57 }
58 ----------------------------------------------------------------------------- */
59 class UserSuppliedFuzzer {
60  public:
61   // Executes the target function on 'Size' bytes of 'Data'.
62   virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
63   // Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
64   // returns the new size of the data.
65   virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
66     return BasicMutate(Data, Size, MaxSize);
67   }
68   // Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
69   // returns the number of bytes written.
70   virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
71                            const uint8_t *Data2, size_t Size2,
72                            uint8_t *Out, size_t MaxOutSize) {
73     return BasicCrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
74   }
75   virtual ~UserSuppliedFuzzer() {}
76
77  protected:
78   // These can be called internally by Mutate and CrossOver.
79   size_t BasicMutate(uint8_t *Data, size_t Size, size_t MaxSize);
80   size_t BasicCrossOver(const uint8_t *Data1, size_t Size1,
81                         const uint8_t *Data2, size_t Size2,
82                         uint8_t *Out, size_t MaxOutSize);
83 };
84
85 int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
86
87 }  // namespace fuzzer
88
89 #endif  // LLVM_FUZZER_INTERFACE_H