blob: 3fd807afcfeb13f4a7db174c66df2fc8ab038ada [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 Serebryanye0d60ba2015-05-23 02:12:05 +000045/** An abstract class that allows to use user-supplied mutators with libFuzzer.
46
47Usage:
48
49#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +000050#include "FuzzerInterface.h"
51class MyFuzzer : public fuzzer::UserSuppliedFuzzer {
52 public:
53 // Must define the target function.
54 void TargetFunction(...) { ... }
55 // Optionally define the mutator.
56 size_t Mutate(...) { ... }
57 // Optionally define the CrossOver method.
58 size_t CrossOver(...) { ... }
59};
60
61int main(int argc, char **argv) {
62 MyFuzzer F;
63 fuzzer::FuzzerDriver(argc, argv, F);
64}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000065#\endcode
66*/
Kostya Serebryanyf3424592015-05-22 22:35:31 +000067class UserSuppliedFuzzer {
68 public:
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000069 /// Executes the target function on 'Size' bytes of 'Data'.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000070 virtual void TargetFunction(const uint8_t *Data, size_t Size) = 0;
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000071 /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +000072 /// returns the new size of the data, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000073 virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize) {
74 return BasicMutate(Data, Size, MaxSize);
75 }
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000076 /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out,
Kostya Serebryany2ea204e2015-05-30 17:33:13 +000077 /// returns the number of bytes written, which should be positive.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000078 virtual size_t CrossOver(const uint8_t *Data1, size_t Size1,
79 const uint8_t *Data2, size_t Size2,
80 uint8_t *Out, size_t MaxOutSize) {
81 return BasicCrossOver(Data1, Size1, Data2, Size2, Out, MaxOutSize);
82 }
83 virtual ~UserSuppliedFuzzer() {}
84
85 protected:
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000086 /// These can be called internally by Mutate and CrossOver.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000087 size_t BasicMutate(uint8_t *Data, size_t Size, size_t MaxSize);
88 size_t BasicCrossOver(const uint8_t *Data1, size_t Size1,
89 const uint8_t *Data2, size_t Size2,
90 uint8_t *Out, size_t MaxOutSize);
91};
92
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000093/// Runs the fuzzing with the UserSuppliedFuzzer.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000094int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF);
95
Kostya Serebryany016852c2015-02-19 18:45:37 +000096} // namespace fuzzer
97
98#endif // LLVM_FUZZER_INTERFACE_H