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 | |
| 19 | #include <cstddef> |
| 20 | #include <cstdint> |
| 21 | |
Kostya Serebryany | 22cc5e2 | 2016-02-13 02:29:38 +0000 | [diff] [blame] | 22 | // Plain C interface. Should be sufficient for most uses. |
| 23 | extern "C" { |
| 24 | // The target function, mandatory. |
| 25 | // Must return 0. |
| 26 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
| 27 | // The initialization function, optional. |
| 28 | int LLVMFuzzerInitialize(int *argc, char ***argv); |
| 29 | // Custom mutator, optional. |
| 30 | // Mutates raw data in [Data, Data+Size] inplace. |
| 31 | // Returns the new size, which is not greater than MaxSize. |
| 32 | // Given the same Seed produces the same mutation. |
| 33 | size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize, |
| 34 | unsigned int Seed); |
| 35 | |
| 36 | } // extern "C" |
| 37 | |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 38 | namespace fuzzer { |
| 39 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 40 | /// Returns an int 0. Values other than zero are reserved for future. |
| 41 | typedef int (*UserCallback)(const uint8_t *Data, size_t Size); |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 42 | /** Simple C-like interface with a single user-supplied callback. |
| 43 | |
| 44 | Usage: |
| 45 | |
| 46 | #\code |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 47 | #include "FuzzerInterface.h" |
| 48 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 49 | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 50 | DoStuffWithData(Data, Size); |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 51 | return 0; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Kostya Serebryany | aca7696 | 2016-01-16 01:23:12 +0000 | [diff] [blame] | 54 | // Optional. |
| 55 | // Define this only if you need to read/modify argc/argv at startup |
| 56 | // and you are using libFuzzer's main(). |
| 57 | // Must return 0. |
| 58 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
| 59 | ReadAndMaybeModify(argc, argv); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 63 | // Implement your own main() or use the one from FuzzerMain.cpp. |
Kostya Serebryany | ecab57b | 2016-02-13 02:39:30 +0000 | [diff] [blame] | 64 | // *NOT* recommended for most cases. |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 65 | int main(int argc, char **argv) { |
| 66 | InitializeMeIfNeeded(); |
| 67 | return fuzzer::FuzzerDriver(argc, argv, LLVMFuzzerTestOneInput); |
| 68 | } |
Kostya Serebryany | e0d60ba | 2015-05-23 02:12:05 +0000 | [diff] [blame] | 69 | #\endcode |
| 70 | */ |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 71 | int FuzzerDriver(int argc, char **argv, UserCallback Callback); |
| 72 | |
Kostya Serebryany | 1deb049 | 2016-02-13 06:24:18 +0000 | [diff] [blame^] | 73 | // Mutates raw data in [Data, Data+Size] inplace. |
| 74 | // Returns the new size, which is not greater than MaxSize. |
Kostya Serebryany | ecab57b | 2016-02-13 02:39:30 +0000 | [diff] [blame] | 75 | // Can be used inside the user-supplied LLVMFuzzerTestOneInput. |
Kostya Serebryany | 1deb049 | 2016-02-13 06:24:18 +0000 | [diff] [blame^] | 76 | size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize); |
Kostya Serebryany | ecab57b | 2016-02-13 02:39:30 +0000 | [diff] [blame] | 77 | |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 78 | } // namespace fuzzer |
| 79 | |
| 80 | #endif // LLVM_FUZZER_INTERFACE_H |