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