blob: 64b8f868f6507050adb450e4bcfb8edf3ad64293 [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
Kostya Serebryany152ac7a2016-01-07 01:49:35 +000019#include <limits>
Kostya Serebryany016852c2015-02-19 18:45:37 +000020#include <cstddef>
21#include <cstdint>
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000022#include <vector>
23#include <string>
Kostya Serebryany016852c2015-02-19 18:45:37 +000024
25namespace fuzzer {
26
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000027/// 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
Kostya Serebryanyaca76962016-01-16 01:23:12 +000041// Optional.
42// Define this only if you need to read/modify argc/argv at startup
43// and you are using libFuzzer's main().
44// Must return 0.
45int LLVMFuzzerInitialize(int *argc, char ***argv) {
46 ReadAndMaybeModify(argc, argv);
47 return 0;
48}
49
Kostya Serebryanyf3424592015-05-22 22:35:31 +000050// Implement your own main() or use the one from FuzzerMain.cpp.
51int main(int argc, char **argv) {
52 InitializeMeIfNeeded();
53 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
54}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000055#\endcode
56*/
Kostya Serebryany016852c2015-02-19 18:45:37 +000057int FuzzerDriver(int argc, char **argv, UserCallback Callback);
58
Kostya Serebryany404c69f2015-07-24 01:06:40 +000059class FuzzerRandomBase {
60 public:
61 FuzzerRandomBase(){}
62 virtual ~FuzzerRandomBase(){};
Mike Aizatskya1a5c692015-12-10 20:41:53 +000063 virtual void ResetSeed(unsigned int seed) = 0;
Kostya Serebryany404c69f2015-07-24 01:06:40 +000064 // Return a random number.
65 virtual size_t Rand() = 0;
66 // Return a random number in range [0,n).
Kostya Serebryanybf29ff22015-08-06 01:29:13 +000067 size_t operator()(size_t n) { return n ? Rand() % n : 0; }
Kostya Serebryany404c69f2015-07-24 01:06:40 +000068 bool RandBool() { return Rand() % 2; }
69};
70
Kostya Serebryany311f27c2016-01-19 20:33:57 +000071// Using libc's stand/rand.
Kostya Serebryany404c69f2015-07-24 01:06:40 +000072class FuzzerRandomLibc : public FuzzerRandomBase {
73 public:
Mike Aizatskya1a5c692015-12-10 20:41:53 +000074 FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); }
75 void ResetSeed(unsigned int seed) override;
Kostya Serebryany311f27c2016-01-19 20:33:57 +000076 ~FuzzerRandomLibc() override {};
Kostya Serebryany404c69f2015-07-24 01:06:40 +000077 size_t Rand() override;
78};
79
Kostya Serebryany311f27c2016-01-19 20:33:57 +000080// Using std::mt19937
81class FuzzerRandom_mt19937 : public FuzzerRandomBase {
82 public:
83 FuzzerRandom_mt19937(unsigned int seed) { ResetSeed(seed); }
84 void ResetSeed(unsigned int seed) override;
85 ~FuzzerRandom_mt19937() override;
86 size_t Rand() override;
87 private:
88 struct Impl;
89 Impl *R = nullptr;
90};
91
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000092// For backward compatibility only, deprecated.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +000093size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize,
94 FuzzerRandomBase &Rand);
95
96class MutationDispatcher;
Kostya Serebryany242ca932015-08-06 19:19:55 +000097
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000098/** An abstract class that allows to use user-supplied mutators with libFuzzer.
99
100Usage:
101
102#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000103#include "FuzzerInterface.h"
104class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
105 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000106 MyFuzzer(fuzzer::FuzzerRandomBase *Rand);
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000107 // Must define the target function.
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000108 int TargetFunction(...) { ...; return 0; }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000109 // Optionally define the mutator.
110 size_t Mutate(...) { ... }
111 // Optionally define the CrossOver method.
112 size_t CrossOver(...) { ... }
113};
114
115int main(int argc, char **argv) {
116 MyFuzzer F;
117 fuzzer::FuzzerDriver(argc, argv, F);
118}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000119#\endcode
120*/
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000121class UserSuppliedFuzzer {
122 public:
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000123 UserSuppliedFuzzer(FuzzerRandomBase *Rand);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000124 /// Executes the target function on 'Size' bytes of 'Data'.
Kostya Serebryany20bb5e72015-10-02 23:34:06 +0000125 virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0;
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000126 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000127 /// returns the new size of the data, which should be positive.
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000128 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000129 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +0000130 /// returns the number of bytes written, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000131 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
132 const uint8_t *Data2, size_t Size2,
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000133 uint8_t *Out, size_t MaxOutSize);
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000134 virtual ~UserSuppliedFuzzer();
135
136 FuzzerRandomBase &GetRand() { return *Rand; }
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000137
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000138 MutationDispatcher &GetMD() { return *MD; }
Kostya Serebryany7d211662015-09-04 00:12:11 +0000139
Kostya Serebryany404c69f2015-07-24 01:06:40 +0000140 private:
141 bool OwnRand = false;
142 FuzzerRandomBase *Rand;
Kostya Serebryany628bc3e2016-01-16 00:04:36 +0000143 MutationDispatcher *MD;
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000144};
145
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +0000146/// Runs the fuzzing with the UserSuppliedFuzzer.
Kostya Serebryanyf3424592015-05-22 22:35:31 +0000147int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
148
Kostya Serebryanya938bcb2015-09-10 16:57:57 +0000149/// More C++-ish interface.
150int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF);
151int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
152
Kostya Serebryany016852c2015-02-19 18:45:37 +0000153} // namespace fuzzer
154
155#endif // LLVM_FUZZER_INTERFACE_H