blob: de5084222ef4539e317d5743f47bec329e15e573 [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>
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000021#include <vector>
22#include <string>
Kostya Serebryany016852c2015-02-19 18:45:37 +000023
24namespace fuzzer {
25
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000026typedef void (*DeprecatedUserCallback)(const uint8_t *Data, size_t Size);
27/// Returns an int 0. Values other than zero are reserved for future.
28typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000029/** Simple C-like interface with a single user-supplied callback.
30
31Usage:
32
33#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +000034#include "FuzzerInterface.h"
35
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000036int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +000037 DoStuffWithData(Data, Size);
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000038 return 0;
Kostya Serebryanyf3424592015-05-22 22:35:31 +000039}
40
41// Implement your own main() or use the one from FuzzerMain.cpp.
42int main(int argc, char **argv) {
43 InitializeMeIfNeeded();
44 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
45}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000046#\endcode
47*/
Kostya Serebryany016852c2015-02-19 18:45:37 +000048int FuzzerDriver(int argc, char **argv, UserCallback Callback);
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000049int FuzzerDriver(int argc, char **argv, DeprecatedUserCallback Callback);
Kostya Serebryany016852c2015-02-19 18:45:37 +000050
Kostya Serebryany404c69f2015-07-24 01:06:40 +000051class FuzzerRandomBase {
52 public:
53 FuzzerRandomBase(){}
54 virtual ~FuzzerRandomBase(){};
55 virtual void ResetSeed(int seed) = 0;
56 // Return a random number.
57 virtual size_t Rand() = 0;
58 // Return a random number in range [0,n).
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000059 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany404c69f2015-07-24 01:06:40 +000060 bool RandBool() { return Rand() % 2; }
61};
62
63class FuzzerRandomLibc : public FuzzerRandomBase {
64 public:
65 FuzzerRandomLibc(int seed) { ResetSeed(seed); }
66 void ResetSeed(int seed) override;
67 ~FuzzerRandomLibc() override {}
68 size_t Rand() override;
69};
70
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000071class MutationDispatcher {
72 public:
Kostya Serebryany7d211662015-09-04 00:12:11 +000073 MutationDispatcher(FuzzerRandomBase &Rand);
74 ~MutationDispatcher();
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000075 /// Mutates data by shuffling bytes.
76 size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
77 /// Mutates data by erasing a byte.
78 size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize);
79 /// Mutates data by inserting a byte.
80 size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
81 /// Mutates data by chanding one byte.
82 size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
83 /// Mutates data by chanding one bit.
84 size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany242ca932015-08-06 19:19:55 +000085
Kostya Serebryany7d211662015-09-04 00:12:11 +000086 /// Mutates data by adding a word from the dictionary.
87 size_t Mutate_AddWordFromDictionary(uint8_t *Data, size_t Size,
88 size_t MaxSize);
89
Kostya Serebryany25425ad2015-09-08 17:19:31 +000090 size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
91
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000092 /// Applies one of the above mutations.
93 /// Returns the new size of data which could be up to MaxSize.
94 size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryany242ca932015-08-06 19:19:55 +000095
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000096 /// Creates a cross-over of two pieces of Data, returns its size.
97 size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
98 size_t Size2, uint8_t *Out, size_t MaxOutSize);
Kostya Serebryany242ca932015-08-06 19:19:55 +000099
Kostya Serebryany7d211662015-09-04 00:12:11 +0000100 void AddWordToDictionary(const uint8_t *Word, size_t Size);
101
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000102 private:
103 FuzzerRandomBase &Rand;
Kostya Serebryany7d211662015-09-04 00:12:11 +0000104 struct Impl;
105 Impl *MDImpl;
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000106};
Kostya Serebryany242ca932015-08-06 19:19:55 +0000107
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000108// For backward compatibility only, deprecated.
109static inline size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
110 FuzzerRandomBase &Rand) {
111 MutationDispatcher MD(Rand);
112 return MD.Mutate(Data, Size, MaxSize);
113}
Kostya Serebryany242ca932015-08-06 19:19:55 +0000114
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000115/** An abstract class that allows to use user-supplied mutators with libFuzzer.
116
117Usage:
118
119#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000120#include "FuzzerInterface.h"
121class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
122 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000123 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000124 // Must define the target function.
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000125 int TargetFunction(...) { ...; return 0; }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000126 // Optionally define the mutator.
127 size_t Mutate(...) { ... }
128 // Optionally define the CrossOver method.
129 size_t CrossOver(...) { ... }
130};
131
132int main(int argc, char **argv) {
133 MyFuzzer F;
134 fuzzer::FuzzerDriver(argc, argv, F);
135}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000136#\endcode
137*/
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000138class UserSuppliedFuzzer {
139 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000140 UserSuppliedFuzzer(); // Deprecated, don't use.
141 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000142 /// Executes the target function on 'Size' bytes of 'Data'.
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000143 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000144 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000145 /// returns the new size of the data, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000146 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000147 return MD.Mutate(Data, Size, MaxSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000148 }
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000149 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000150 /// returns the number of bytes written, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000151 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
152 const uint8_t *Data2, size_t Size2,
153 uint8_t *Out, size_t MaxOutSize) {
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000154 return MD.CrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000155 }
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000156 virtual ~UserSuppliedFuzzer();
157
158 FuzzerRandomBase &GetRand() { return *Rand; }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000159
Kostya Serebryany7d211662015-09-04 00:12:11 +0000160 MutationDispatcher &GetMD() { return MD; }
161
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000162 private:
163 bool OwnRand = false;
164 FuzzerRandomBase *Rand;
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000165 MutationDispatcher MD;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000166};
167
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000168/// Runs the fuzzing with the UserSuppliedFuzzer.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000169int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
170
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000171/// More C++-ish interface.
172int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
173int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
174
Kostya Serebryany016852c2015-02-19 18:45:37 +0000175} // namespace fuzzer
176
177#endif // LLVM_FUZZER_INTERFACE_H