Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 1 | //===- 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 Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 12 | // 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 Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 16 | #ifndef LLVM_FUZZER_INTERFACE_H |
| 17 | #define LLVM_FUZZER_INTERFACE_H |
| 18 | |
Kostya Serebryany | 152ac7a | 2016-01-07 01:49:35 +0000 | [diff] [blame] | 19 | #include <limits> |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | #include <cstdint> |
Kostya Serebryany | a938bcb | 2015-09-10 16:57:57 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | #include <string> |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 24 | |
| 25 | namespace fuzzer { |
| 26 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 27 | /// Returns an int 0. Values other than zero are reserved for future. |
| 28 | typedef int (*UserCallback)(const uint8_t *Data, size_t Size); |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 29 | /** Simple C-like interface with a single user-supplied callback. |
| 30 | |
| 31 | Usage: |
| 32 | |
| 33 | #\code |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 34 | #include "FuzzerInterface.h" |
| 35 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 36 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 37 | DoStuffWithData(Data, Size); |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 38 | return 0; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kostya Serebryany | aca7696 | 2016-01-16 01:23:12 +0000 | [diff] [blame] | 41 | // 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. |
| 45 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
| 46 | ReadAndMaybeModify(argc, argv); |
| 47 | return 0; |
| 48 | } |
| 49 | |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 50 | // Implement your own main() or use the one from FuzzerMain.cpp. |
| 51 | int main(int argc, char **argv) { |
| 52 | InitializeMeIfNeeded(); |
| 53 | return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput); |
| 54 | } |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 55 | #\endcode |
| 56 | */ |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 57 | int FuzzerDriver(int argc, char **argv, UserCallback Callback); |
| 58 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 59 | class FuzzerRandomBase { |
| 60 | public: |
| 61 | FuzzerRandomBase(){} |
| 62 | virtual ~FuzzerRandomBase(){}; |
Mike Aizatsky | a1a5c69 | 2015-12-10 20:41:53 +0000 | [diff] [blame] | 63 | virtual void ResetSeed(unsigned int seed) = 0; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 64 | // Return a random number. |
| 65 | virtual size_t Rand() = 0; |
| 66 | // Return a random number in range [0,n). |
Kostya Serebryany | bf29ff2 | 2015-08-06 01:29:13 +0000 | [diff] [blame] | 67 | size_t operator()(size_t n) { return n ? Rand() % n : 0; } |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 68 | bool RandBool() { return Rand() % 2; } |
| 69 | }; |
| 70 | |
Kostya Serebryany | 311f27c | 2016-01-19 20:33:57 +0000 | [diff] [blame] | 71 | // Using libc's stand/rand. |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 72 | class FuzzerRandomLibc : public FuzzerRandomBase { |
| 73 | public: |
Mike Aizatsky | a1a5c69 | 2015-12-10 20:41:53 +0000 | [diff] [blame] | 74 | FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); } |
| 75 | void ResetSeed(unsigned int seed) override; |
Kostya Serebryany | 311f27c | 2016-01-19 20:33:57 +0000 | [diff] [blame] | 76 | ~FuzzerRandomLibc() override {}; |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 77 | size_t Rand() override; |
| 78 | }; |
| 79 | |
Kostya Serebryany | 311f27c | 2016-01-19 20:33:57 +0000 | [diff] [blame] | 80 | // Using std::mt19937 |
| 81 | class 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 Serebryany | ec2dcb1 | 2015-09-03 21:24:19 +0000 | [diff] [blame] | 92 | // For backward compatibility only, deprecated. |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 93 | size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize, |
| 94 | FuzzerRandomBase &Rand); |
| 95 | |
| 96 | class MutationDispatcher; |
Kostya Serebryany | 242ca93 | 2015-08-06 19:19:55 +0000 | [diff] [blame] | 97 | |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 98 | /** An abstract class that allows to use user-supplied mutators with libFuzzer. |
| 99 | |
| 100 | Usage: |
| 101 | |
| 102 | #\code |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 103 | #include "FuzzerInterface.h" |
| 104 | class MyFuzzer : public fuzzer::UserSuppliedFuzzer { |
| 105 | public: |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 106 | MyFuzzer(fuzzer::FuzzerRandomBase *Rand); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 107 | // Must define the target function. |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 108 | int TargetFunction(...) { ...; return 0; } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 109 | // Optionally define the mutator. |
| 110 | size_t Mutate(...) { ... } |
| 111 | // Optionally define the CrossOver method. |
| 112 | size_t CrossOver(...) { ... } |
| 113 | }; |
| 114 | |
| 115 | int main(int argc, char **argv) { |
| 116 | MyFuzzer F; |
| 117 | fuzzer::FuzzerDriver(argc, argv, F); |
| 118 | } |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 119 | #\endcode |
| 120 | */ |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 121 | class UserSuppliedFuzzer { |
| 122 | public: |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 123 | UserSuppliedFuzzer(FuzzerRandomBase *Rand); |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 124 | /// Executes the target function on 'Size' bytes of 'Data'. |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 125 | virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0; |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 126 | /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes, |
Kostya Serebryany | 2ea204e | 2015-05-30 17:33:13 +0000 | [diff] [blame] | 127 | /// returns the new size of the data, which should be positive. |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 128 | virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize); |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 129 | /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out, |
Kostya Serebryany | 2ea204e | 2015-05-30 17:33:13 +0000 | [diff] [blame] | 130 | /// returns the number of bytes written, which should be positive. |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 131 | virtual size_t CrossOver(const uint8_t *Data1, size_t Size1, |
| 132 | const uint8_t *Data2, size_t Size2, |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 133 | uint8_t *Out, size_t MaxOutSize); |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 134 | virtual ~UserSuppliedFuzzer(); |
| 135 | |
| 136 | FuzzerRandomBase &GetRand() { return *Rand; } |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 137 | |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 138 | MutationDispatcher &GetMD() { return *MD; } |
Kostya Serebryany | 7d21166 | 2015-09-04 00:12:11 +0000 | [diff] [blame] | 139 | |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 140 | private: |
| 141 | bool OwnRand = false; |
| 142 | FuzzerRandomBase *Rand; |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 143 | MutationDispatcher *MD; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 146 | /// Runs the fuzzing with the UserSuppliedFuzzer. |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 147 | int FuzzerDriver(int argc, char **argv, UserSuppliedFuzzer &USF); |
| 148 | |
Kostya Serebryany | a938bcb | 2015-09-10 16:57:57 +0000 | [diff] [blame] | 149 | /// More C++-ish interface. |
| 150 | int FuzzerDriver(const std::vector<std::string> &Args, UserSuppliedFuzzer &USF); |
| 151 | int FuzzerDriver(const std::vector<std::string> &Args, UserCallback Callback); |
| 152 | |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 153 | } // namespace fuzzer |
| 154 | |
| 155 | #endif // LLVM_FUZZER_INTERFACE_H |