1 //===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===//
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
7 //===----------------------------------------------------------------------===//
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_DEFS_H
12 #define LLVM_FUZZER_DEFS_H
23 // Platform detection.
25 #define LIBFUZZER_APPLE 0
26 #define LIBFUZZER_FUCHSIA 0
27 #define LIBFUZZER_LINUX 1
28 #define LIBFUZZER_NETBSD 0
29 #define LIBFUZZER_FREEBSD 0
30 #define LIBFUZZER_OPENBSD 0
31 #define LIBFUZZER_WINDOWS 0
33 #define LIBFUZZER_APPLE 1
34 #define LIBFUZZER_FUCHSIA 0
35 #define LIBFUZZER_LINUX 0
36 #define LIBFUZZER_NETBSD 0
37 #define LIBFUZZER_FREEBSD 0
38 #define LIBFUZZER_OPENBSD 0
39 #define LIBFUZZER_WINDOWS 0
41 #define LIBFUZZER_APPLE 0
42 #define LIBFUZZER_FUCHSIA 0
43 #define LIBFUZZER_LINUX 0
44 #define LIBFUZZER_NETBSD 1
45 #define LIBFUZZER_FREEBSD 0
46 #define LIBFUZZER_OPENBSD 0
47 #define LIBFUZZER_WINDOWS 0
49 #define LIBFUZZER_APPLE 0
50 #define LIBFUZZER_FUCHSIA 0
51 #define LIBFUZZER_LINUX 0
52 #define LIBFUZZER_NETBSD 0
53 #define LIBFUZZER_FREEBSD 1
54 #define LIBFUZZER_OPENBSD 0
55 #define LIBFUZZER_WINDOWS 0
57 #define LIBFUZZER_APPLE 0
58 #define LIBFUZZER_FUCHSIA 0
59 #define LIBFUZZER_LINUX 0
60 #define LIBFUZZER_NETBSD 0
61 #define LIBFUZZER_FREEBSD 0
62 #define LIBFUZZER_OPENBSD 1
63 #define LIBFUZZER_WINDOWS 0
65 #define LIBFUZZER_APPLE 0
66 #define LIBFUZZER_FUCHSIA 0
67 #define LIBFUZZER_LINUX 0
68 #define LIBFUZZER_NETBSD 0
69 #define LIBFUZZER_FREEBSD 0
70 #define LIBFUZZER_OPENBSD 0
71 #define LIBFUZZER_WINDOWS 1
73 #define LIBFUZZER_APPLE 0
74 #define LIBFUZZER_FUCHSIA 1
75 #define LIBFUZZER_LINUX 0
76 #define LIBFUZZER_NETBSD 0
77 #define LIBFUZZER_FREEBSD 0
78 #define LIBFUZZER_OPENBSD 0
79 #define LIBFUZZER_WINDOWS 0
81 #error "Support for your platform has not been implemented"
84 #if defined(_MSC_VER) && !defined(__clang__)
85 // MSVC compiler is being used.
86 #define LIBFUZZER_MSVC 1
88 #define LIBFUZZER_MSVC 0
91 #ifndef __has_attribute
92 # define __has_attribute(x) 0
95 #define LIBFUZZER_POSIX \
96 (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \
97 LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD)
100 # if __has_attribute(target)
101 # define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
103 # define ATTRIBUTE_TARGET_POPCNT
106 # define ATTRIBUTE_TARGET_POPCNT
110 #ifdef __clang__ // avoid gcc warning.
111 # if __has_attribute(no_sanitize)
112 # define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
114 # define ATTRIBUTE_NO_SANITIZE_MEMORY
116 # define ALWAYS_INLINE __attribute__((always_inline))
118 # define ATTRIBUTE_NO_SANITIZE_MEMORY
119 # define ALWAYS_INLINE
122 #if LIBFUZZER_WINDOWS
123 #define ATTRIBUTE_NO_SANITIZE_ADDRESS
125 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
128 #if LIBFUZZER_WINDOWS
129 #define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
130 #define ATTRIBUTE_INTERFACE __declspec(dllexport)
131 // This is used for __sancov_lowest_stack which is needed for
132 // -fsanitize-coverage=stack-depth. That feature is not yet available on
133 // Windows, so make the symbol static to avoid linking errors.
134 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
135 #define ATTRIBUTE_NOINLINE __declspec(noinline)
137 #define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
138 #define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
139 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
140 ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
142 #define ATTRIBUTE_NOINLINE __attribute__((noinline))
145 #if defined(__has_feature)
146 # if __has_feature(address_sanitizer)
147 # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
148 # elif __has_feature(memory_sanitizer)
149 # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
151 # define ATTRIBUTE_NO_SANITIZE_ALL
154 # define ATTRIBUTE_NO_SANITIZE_ALL
159 template <class T> T Min(T a, T b) { return a < b ? a : b; }
160 template <class T> T Max(T a, T b) { return a > b ? a : b; }
164 class DictionaryEntry;
165 class MutationDispatcher;
166 struct FuzzingOptions;
169 struct ExternalFunctions;
171 // Global interface to functions that may or may not be available.
172 extern ExternalFunctions *EF;
174 // We are using a custom allocator to give a different symbol name to STL
175 // containers in order to avoid ODR violations.
177 class fuzzer_allocator: public std::allocator<T> {
179 fuzzer_allocator() = default;
182 fuzzer_allocator(const fuzzer_allocator<U>&) {}
184 template<class Other>
185 struct rebind { typedef fuzzer_allocator<Other> other; };
189 using Vector = std::vector<T, fuzzer_allocator<T>>;
192 using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
194 typedef Vector<uint8_t> Unit;
195 typedef Vector<Unit> UnitVector;
196 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
198 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
200 uint8_t *ExtraCountersBegin();
201 uint8_t *ExtraCountersEnd();
202 void ClearExtraCounters();
204 extern bool RunningUserCallback;
206 } // namespace fuzzer
208 #endif // LLVM_FUZZER_DEFS_H