1 //===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // Basic definitions. 9 //===----------------------------------------------------------------------===// 10 11 #ifndef LLVM_FUZZER_DEFS_H 12 #define LLVM_FUZZER_DEFS_H 13 14 #include <cassert> 15 #include <cstddef> 16 #include <cstdint> 17 #include <cstring> 18 #include <memory> 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 24 // Platform detection. 25 #ifdef __linux__ 26 #define LIBFUZZER_APPLE 0 27 #define LIBFUZZER_FUCHSIA 0 28 #define LIBFUZZER_LINUX 1 29 #define LIBFUZZER_NETBSD 0 30 #define LIBFUZZER_FREEBSD 0 31 #define LIBFUZZER_OPENBSD 0 32 #define LIBFUZZER_WINDOWS 0 33 #define LIBFUZZER_EMSCRIPTEN 0 34 #elif __APPLE__ 35 #define LIBFUZZER_APPLE 1 36 #define LIBFUZZER_FUCHSIA 0 37 #define LIBFUZZER_LINUX 0 38 #define LIBFUZZER_NETBSD 0 39 #define LIBFUZZER_FREEBSD 0 40 #define LIBFUZZER_OPENBSD 0 41 #define LIBFUZZER_WINDOWS 0 42 #define LIBFUZZER_EMSCRIPTEN 0 43 #elif __NetBSD__ 44 #define LIBFUZZER_APPLE 0 45 #define LIBFUZZER_FUCHSIA 0 46 #define LIBFUZZER_LINUX 0 47 #define LIBFUZZER_NETBSD 1 48 #define LIBFUZZER_FREEBSD 0 49 #define LIBFUZZER_OPENBSD 0 50 #define LIBFUZZER_WINDOWS 0 51 #define LIBFUZZER_EMSCRIPTEN 0 52 #elif __FreeBSD__ 53 #define LIBFUZZER_APPLE 0 54 #define LIBFUZZER_FUCHSIA 0 55 #define LIBFUZZER_LINUX 0 56 #define LIBFUZZER_NETBSD 0 57 #define LIBFUZZER_FREEBSD 1 58 #define LIBFUZZER_OPENBSD 0 59 #define LIBFUZZER_WINDOWS 0 60 #define LIBFUZZER_EMSCRIPTEN 0 61 #elif __OpenBSD__ 62 #define LIBFUZZER_APPLE 0 63 #define LIBFUZZER_FUCHSIA 0 64 #define LIBFUZZER_LINUX 0 65 #define LIBFUZZER_NETBSD 0 66 #define LIBFUZZER_FREEBSD 0 67 #define LIBFUZZER_OPENBSD 1 68 #define LIBFUZZER_WINDOWS 0 69 #define LIBFUZZER_EMSCRIPTEN 0 70 #elif _WIN32 71 #define LIBFUZZER_APPLE 0 72 #define LIBFUZZER_FUCHSIA 0 73 #define LIBFUZZER_LINUX 0 74 #define LIBFUZZER_NETBSD 0 75 #define LIBFUZZER_FREEBSD 0 76 #define LIBFUZZER_OPENBSD 0 77 #define LIBFUZZER_WINDOWS 1 78 #define LIBFUZZER_EMSCRIPTEN 0 79 #elif __Fuchsia__ 80 #define LIBFUZZER_APPLE 0 81 #define LIBFUZZER_FUCHSIA 1 82 #define LIBFUZZER_LINUX 0 83 #define LIBFUZZER_NETBSD 0 84 #define LIBFUZZER_FREEBSD 0 85 #define LIBFUZZER_OPENBSD 0 86 #define LIBFUZZER_WINDOWS 0 87 #define LIBFUZZER_EMSCRIPTEN 0 88 #elif __EMSCRIPTEN__ 89 #define LIBFUZZER_APPLE 0 90 #define LIBFUZZER_FUCHSIA 0 91 #define LIBFUZZER_LINUX 0 92 #define LIBFUZZER_NETBSD 0 93 #define LIBFUZZER_FREEBSD 0 94 #define LIBFUZZER_OPENBSD 0 95 #define LIBFUZZER_WINDOWS 0 96 #define LIBFUZZER_EMSCRIPTEN 1 97 #else 98 #error "Support for your platform has not been implemented" 99 #endif 100 101 #if defined(_MSC_VER) && !defined(__clang__) 102 // MSVC compiler is being used. 103 #define LIBFUZZER_MSVC 1 104 #else 105 #define LIBFUZZER_MSVC 0 106 #endif 107 108 #ifndef __has_attribute 109 # define __has_attribute(x) 0 110 #endif 111 112 #define LIBFUZZER_POSIX \ 113 (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \ 114 LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD || LIBFUZZER_EMSCRIPTEN) 115 116 #ifdef __x86_64 117 # if __has_attribute(target) 118 # define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt"))) 119 # else 120 # define ATTRIBUTE_TARGET_POPCNT 121 # endif 122 #else 123 # define ATTRIBUTE_TARGET_POPCNT 124 #endif 125 126 127 #ifdef __clang__ // avoid gcc warning. 128 # if __has_attribute(no_sanitize) 129 # define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory"))) 130 # else 131 # define ATTRIBUTE_NO_SANITIZE_MEMORY 132 # endif 133 # define ALWAYS_INLINE __attribute__((always_inline)) 134 #else 135 # define ATTRIBUTE_NO_SANITIZE_MEMORY 136 # define ALWAYS_INLINE 137 #endif // __clang__ 138 139 #if LIBFUZZER_WINDOWS 140 #define ATTRIBUTE_NO_SANITIZE_ADDRESS 141 #else 142 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) 143 #endif 144 145 #if LIBFUZZER_WINDOWS 146 #define ATTRIBUTE_ALIGNED(X) __declspec(align(X)) 147 #define ATTRIBUTE_INTERFACE __declspec(dllexport) 148 // This is used for __sancov_lowest_stack which is needed for 149 // -fsanitize-coverage=stack-depth. That feature is not yet available on 150 // Windows, so make the symbol static to avoid linking errors. 151 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static 152 #define ATTRIBUTE_NOINLINE __declspec(noinline) 153 #else 154 #define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X))) 155 #define ATTRIBUTE_INTERFACE __attribute__((visibility("default"))) 156 #define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \ 157 ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local 158 159 #define ATTRIBUTE_NOINLINE __attribute__((noinline)) 160 #endif 161 162 #if defined(__has_feature) 163 # if __has_feature(address_sanitizer) 164 # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS 165 # elif __has_feature(memory_sanitizer) 166 # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY 167 # else 168 # define ATTRIBUTE_NO_SANITIZE_ALL 169 # endif 170 #else 171 # define ATTRIBUTE_NO_SANITIZE_ALL 172 #endif 173 174 namespace fuzzer { 175 176 template <class T> T Min(T a, T b) { return a < b ? a : b; } 177 template <class T> T Max(T a, T b) { return a > b ? a : b; } 178 179 class Random; 180 class Dictionary; 181 class DictionaryEntry; 182 class MutationDispatcher; 183 struct FuzzingOptions; 184 class InputCorpus; 185 struct InputInfo; 186 struct ExternalFunctions; 187 188 // Global interface to functions that may or may not be available. 189 extern ExternalFunctions *EF; 190 191 // We are using a custom allocator to give a different symbol name to STL 192 // containers in order to avoid ODR violations. 193 template<typename T> 194 class fuzzer_allocator: public std::allocator<T> { 195 public: 196 fuzzer_allocator() = default; 197 198 template<class U> 199 fuzzer_allocator(const fuzzer_allocator<U>&) {} 200 201 template<class Other> 202 struct rebind { typedef fuzzer_allocator<Other> other; }; 203 }; 204 205 template<typename T> 206 using Vector = std::vector<T, fuzzer_allocator<T>>; 207 208 template<typename T> 209 using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>; 210 211 typedef Vector<uint8_t> Unit; 212 typedef Vector<Unit> UnitVector; 213 typedef int (*UserCallback)(const uint8_t *Data, size_t Size); 214 215 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); 216 217 uint8_t *ExtraCountersBegin(); 218 uint8_t *ExtraCountersEnd(); 219 void ClearExtraCounters(); 220 221 extern bool RunningUserCallback; 222 223 } // namespace fuzzer 224 225 #endif // LLVM_FUZZER_DEFS_H 226