blob: 04e9b8350b92885062130e284ec526ebca4f9c08 [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
Kostya Serebryany22cc5e22016-02-13 02:29:38 +000025// Plain C interface. Should be sufficient for most uses.
26extern "C" {
27// The target function, mandatory.
28// Must return 0.
29int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
30// The initialization function, optional.
31int LLVMFuzzerInitialize(int *argc, char ***argv);
32// Custom mutator, optional.
33// Mutates raw data in [Data, Data+Size] inplace.
34// Returns the new size, which is not greater than MaxSize.
35// Given the same Seed produces the same mutation.
36size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize,
37 unsigned int Seed);
38
39} // extern "C"
40
Kostya Serebryany016852c2015-02-19 18:45:37 +000041namespace fuzzer {
42
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000043/// Returns an int 0. Values other than zero are reserved for future.
44typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000045/** Simple C-like interface with a single user-supplied callback.
46
47Usage:
48
49#\code
Kostya Serebryanyf3424592015-05-22 22:35:31 +000050#include "FuzzerInterface.h"
51
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000052int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +000053 DoStuffWithData(Data, Size);
Kostya Serebryany20bb5e72015-10-02 23:34:06 +000054 return 0;
Kostya Serebryanyf3424592015-05-22 22:35:31 +000055}
56
Kostya Serebryanyaca76962016-01-16 01:23:12 +000057// Optional.
58// Define this only if you need to read/modify argc/argv at startup
59// and you are using libFuzzer's main().
60// Must return 0.
61int LLVMFuzzerInitialize(int *argc, char ***argv) {
62 ReadAndMaybeModify(argc, argv);
63 return 0;
64}
65
Kostya Serebryanyf3424592015-05-22 22:35:31 +000066// Implement your own main() or use the one from FuzzerMain.cpp.
Kostya Serebryanyecab57b2016-02-13 02:39:30 +000067// *NOT* recommended for most cases.
Kostya Serebryanyf3424592015-05-22 22:35:31 +000068int main(int argc, char **argv) {
69 InitializeMeIfNeeded();
70 return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput);
71}
Kostya Serebryanye0d60ba2015-05-23 02:12:05 +000072#\endcode
73*/
Kostya Serebryany016852c2015-02-19 18:45:37 +000074int FuzzerDriver(int argc, char **argv, UserCallback Callback);
75
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000076/// More C++-ish interface.
Kostya Serebryanya938bcb2015-09-10 16:57:57 +000077int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback);
78
Kostya Serebryanyecab57b2016-02-13 02:39:30 +000079// Same interface as LLVMFuzzerTestOneInput.
80// Can be used inside the user-supplied LLVMFuzzerTestOneInput.
81size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed);
82
Kostya Serebryany016852c2015-02-19 18:45:37 +000083} // namespace fuzzer
84
85#endif // LLVM_FUZZER_INTERFACE_H