George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [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 libFuzzer and the library being tested. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | // NOTE: the libFuzzer interface is thin and in the majority of cases |
| 13 | // you should not include this file into your target. In 95% of cases |
| 14 | // all you need is to define the following function in your file: |
| 15 | // extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
| 16 | |
| 17 | // WARNING: keep the interface in C. |
| 18 | |
| 19 | #ifndef LLVM_FUZZER_INTERFACE_H |
| 20 | #define LLVM_FUZZER_INTERFACE_H |
| 21 | |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | |
| 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif // __cplusplus |
| 28 | |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 29 | // Define FUZZER_INTERFACE_VISIBILITY to set default visibility in a way that |
| 30 | // doesn't break MSVC. |
| 31 | #if defined(_MSC_VER) && !defined(__clang__) |
| 32 | #define FUZZER_INTERFACE_VISIBILITY __declspec(dllexport) |
| 33 | #else |
| 34 | #define FUZZER_INTERFACE_VISIBILITY __attribute__((visibility("default"))) |
| 35 | #endif |
| 36 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 37 | // Mandatory user-provided target function. |
| 38 | // Executes the code under test with [Data, Data+Size) as the input. |
| 39 | // libFuzzer will invoke this function *many* times with different inputs. |
| 40 | // Must return 0. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 41 | FUZZER_INTERFACE_VISIBILITY int |
Petr Hosek | eac2b47 | 2018-01-17 20:39:14 | [diff] [blame] | 42 | LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 43 | |
| 44 | // Optional user-provided initialization function. |
| 45 | // If provided, this function will be called by libFuzzer once at startup. |
| 46 | // It may read and modify argc/argv. |
| 47 | // Must return 0. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 48 | FUZZER_INTERFACE_VISIBILITY int LLVMFuzzerInitialize(int *argc, char ***argv); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 49 | |
| 50 | // Optional user-provided custom mutator. |
| 51 | // Mutates raw data in [Data, Data+Size) inplace. |
| 52 | // Returns the new size, which is not greater than MaxSize. |
| 53 | // Given the same Seed produces the same mutation. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 54 | FUZZER_INTERFACE_VISIBILITY size_t |
Petr Hosek | eac2b47 | 2018-01-17 20:39:14 | [diff] [blame] | 55 | LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize, |
| 56 | unsigned int Seed); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 57 | |
| 58 | // Optional user-provided custom cross-over function. |
| 59 | // Combines pieces of Data1 & Data2 together into Out. |
| 60 | // Returns the new size, which is not greater than MaxOutSize. |
| 61 | // Should produce the same mutation given the same Seed. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 62 | FUZZER_INTERFACE_VISIBILITY size_t |
Petr Hosek | eac2b47 | 2018-01-17 20:39:14 | [diff] [blame] | 63 | LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1, |
| 64 | const uint8_t *Data2, size_t Size2, uint8_t *Out, |
| 65 | size_t MaxOutSize, unsigned int Seed); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 66 | |
| 67 | // Experimental, may go away in future. |
| 68 | // libFuzzer-provided function to be used inside LLVMFuzzerCustomMutator. |
| 69 | // Mutates raw data in [Data, Data+Size) inplace. |
| 70 | // Returns the new size, which is not greater than MaxSize. |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 71 | FUZZER_INTERFACE_VISIBILITY size_t |
Petr Hosek | eac2b47 | 2018-01-17 20:39:14 | [diff] [blame] | 72 | LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize); |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 73 | |
Jonathan Metzman | b795c31 | 2019-01-17 16:36:05 | [diff] [blame^] | 74 | #undef FUZZER_INTERFACE_VISIBILITY |
| 75 | |
George Karpenkov | 10ab2ac | 2017-08-21 23:25:50 | [diff] [blame] | 76 | #ifdef __cplusplus |
| 77 | } // extern "C" |
| 78 | #endif // __cplusplus |
| 79 | |
| 80 | #endif // LLVM_FUZZER_INTERFACE_H |