george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerInterface.h - Interface header for the Fuzzer ------*- C++ -* ===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Define the interface between libFuzzer and the library being tested. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | // NOTE: the libFuzzer interface is thin and in the majority of cases |
| 12 | // you should not include this file into your target. In 95% of cases |
| 13 | // all you need is to define the following function in your file: |
| 14 | // extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
| 15 | |
| 16 | // WARNING: keep the interface in C. |
| 17 | |
| 18 | #ifndef LLVM_FUZZER_INTERFACE_H |
| 19 | #define LLVM_FUZZER_INTERFACE_H |
| 20 | |
| 21 | #include <stddef.h> |
| 22 | #include <stdint.h> |
| 23 | |
| 24 | #ifdef __cplusplus |
| 25 | extern "C" { |
| 26 | #endif // __cplusplus |
| 27 | |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 28 | // Define FUZZER_INTERFACE_VISIBILITY to set default visibility in a way that |
| 29 | // doesn't break MSVC. |
metzman | 6134add | 2019-01-28 17:51:13 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 31 | #define FUZZER_INTERFACE_VISIBILITY __declspec(dllexport) |
| 32 | #else |
| 33 | #define FUZZER_INTERFACE_VISIBILITY __attribute__((visibility("default"))) |
| 34 | #endif |
| 35 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 36 | // Mandatory user-provided target function. |
| 37 | // Executes the code under test with [Data, Data+Size) as the input. |
| 38 | // libFuzzer will invoke this function *many* times with different inputs. |
| 39 | // Must return 0. |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 40 | FUZZER_INTERFACE_VISIBILITY int |
phosek | 966475e | 2018-01-17 20:39:14 +0000 | [diff] [blame] | 41 | LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 42 | |
| 43 | // Optional user-provided initialization function. |
| 44 | // If provided, this function will be called by libFuzzer once at startup. |
| 45 | // It may read and modify argc/argv. |
| 46 | // Must return 0. |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 47 | FUZZER_INTERFACE_VISIBILITY int LLVMFuzzerInitialize(int *argc, char ***argv); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 48 | |
| 49 | // Optional user-provided custom mutator. |
| 50 | // Mutates raw data in [Data, Data+Size) inplace. |
| 51 | // Returns the new size, which is not greater than MaxSize. |
| 52 | // Given the same Seed produces the same mutation. |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 53 | FUZZER_INTERFACE_VISIBILITY size_t |
phosek | 966475e | 2018-01-17 20:39:14 +0000 | [diff] [blame] | 54 | LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize, |
| 55 | unsigned int Seed); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 56 | |
| 57 | // Optional user-provided custom cross-over function. |
| 58 | // Combines pieces of Data1 & Data2 together into Out. |
| 59 | // Returns the new size, which is not greater than MaxOutSize. |
| 60 | // Should produce the same mutation given the same Seed. |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 61 | FUZZER_INTERFACE_VISIBILITY size_t |
phosek | 966475e | 2018-01-17 20:39:14 +0000 | [diff] [blame] | 62 | LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1, |
| 63 | const uint8_t *Data2, size_t Size2, uint8_t *Out, |
| 64 | size_t MaxOutSize, unsigned int Seed); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 65 | |
| 66 | // Experimental, may go away in future. |
| 67 | // libFuzzer-provided function to be used inside LLVMFuzzerCustomMutator. |
| 68 | // Mutates raw data in [Data, Data+Size) inplace. |
| 69 | // Returns the new size, which is not greater than MaxSize. |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 70 | FUZZER_INTERFACE_VISIBILITY size_t |
phosek | 966475e | 2018-01-17 20:39:14 +0000 | [diff] [blame] | 71 | LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 72 | |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 73 | #undef FUZZER_INTERFACE_VISIBILITY |
| 74 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 75 | #ifdef __cplusplus |
| 76 | } // extern "C" |
| 77 | #endif // __cplusplus |
| 78 | |
| 79 | #endif // LLVM_FUZZER_INTERFACE_H |