blob: c4582d61f1acc2f0d96b2624bffd10c8b28bae16 [file] [log] [blame]
Kostya Serebryany016852c2015-02-19 18:45:37 +00001//===- 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
Kostya Serebryanyf3424592015-05-22 22:35:31 +000012// 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
Kostya Serebryany016852c2015-02-19 18:45:37 +000016#ifndef LLVM_FUZZER_INTERFACE_H
17#define LLVM_FUZZER_INTERFACE_H
18
19#include <cstddef>
20#include <cstdint>
21
22namespace fuzzer {
23
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000024typedef void (*UserCallback)(const uint8_t *Data, size_t Size);
25/** Simple C-like interface with a single user-supplied callback.
26
27Usage:
28
29#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +000030#include "FuzzerInterface.h"
31
32void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
33 DoStuffWithData(Data, Size);
34}
35
36// Implement your own main() or use the one from FuzzerMain.cpp.
37int main(int argc, char **argv) {
38 InitializeMeIfNeeded();
39 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
40}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000041#\endcode
42*/
Kostya Serebryany016852c2015-02-19 18:45:37 +000043int FuzzerDriver(int argc, char **argv, UserCallback Callback);
44
Kostya Serebryany404c69f2015-07-24 01:06:40 +000045class FuzzerRandomBase {
46 public:
47 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).
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000053 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany404c69f2015-07-24 01:06:40 +000054 bool RandBool() { return Rand() % 2; }
55};
56
57class FuzzerRandomLibc : public FuzzerRandomBase {
58 public:
59 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
60 void ResetSeed(int seed) override;
61 ~FuzzerRandomLibc() override {}
62 size_t Rand() override;
63};
64
Kostya Serebryany242ca932015-08-06 19:19:55 +000065
66/// Mutates data by shuffling bytes.
67size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize,
68 FuzzerRandomBase &Rand);
69/// Mutates data by erasing a byte.
70size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize,
71 FuzzerRandomBase &Rand);
72/// Mutates data by inserting a byte.
73size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize,
74 FuzzerRandomBase &Rand);
75/// Mutates data by chanding one byte.
76size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize,
77 FuzzerRandomBase &Rand);
78/// Mutates data by chanding one bit.
79size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize,
80 FuzzerRandomBase &Rand);
81
82/// Applies one of the above mutations.
83/// Returns the new size of data which could be up to MaxSize.
84size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
85 FuzzerRandomBase &Rand);
86
87/// Creates a cross-over of two pieces of Data, returns its size.
88size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
89 size_t Size2, uint8_t *Out, size_t MaxOutSize,
90 FuzzerRandomBase &Rand);
91
92
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000093/** An abstract class that allows to use user-supplied mutators with libFuzzer.
94
95Usage:
96
97#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +000098#include "FuzzerInterface.h"
99class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
100 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000101 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000102 // Must define the target function.
103 void TargetFunction(...) { ... }
104 // Optionally define the mutator.
105 size_t Mutate(...) { ... }
106 // Optionally define the CrossOver method.
107 size_t CrossOver(...) { ... }
108};
109
110int main(int argc, char **argv) {
111 MyFuzzer F;
112 fuzzer::FuzzerDriver(argc, argv, F);
113}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000114#\endcode
115*/
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000116class UserSuppliedFuzzer {
117 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000118 UserSuppliedFuzzer(); // Deprecated, don't use.
119 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000120 /// Executes the target function on 'Size' bytes of 'Data'.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000121 virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000122 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000123 /// returns the new size of the data, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000124 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryany242ca932015-08-06 19:19:55 +0000125 return ::fuzzer::Mutate(Data, Size, MaxSize, GetRand());
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000126 }
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000127 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000128 /// returns the number of bytes written, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000129 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
130 const uint8_t *Data2, size_t Size2,
131 uint8_t *Out, size_t MaxOutSize) {
Kostya Serebryany242ca932015-08-06 19:19:55 +0000132 return ::fuzzer::CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize,
133 GetRand());
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000134 }
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000135 virtual ~UserSuppliedFuzzer();
136
137 FuzzerRandomBase &GetRand() { return *Rand; }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000138
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000139 private:
140 bool OwnRand = false;
141 FuzzerRandomBase *Rand;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000142};
143
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000144/// Runs the fuzzing with the UserSuppliedFuzzer.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000145int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
146
Kostya Serebryany016852c2015-02-19 18:45:37 +0000147} // namespace fuzzer
148
149#endif // LLVM_FUZZER_INTERFACE_H