george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerDefs.h - Internal 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 | // Basic definitions. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_FUZZER_DEFS_H |
| 13 | #define LLVM_FUZZER_DEFS_H |
| 14 | |
| 15 | #include <cassert> |
| 16 | #include <cstddef> |
| 17 | #include <cstdint> |
| 18 | #include <cstring> |
| 19 | #include <string> |
| 20 | #include <vector> |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <memory> |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 23 | |
| 24 | // Platform detection. |
| 25 | #ifdef __linux__ |
| 26 | #define LIBFUZZER_APPLE 0 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 27 | #define LIBFUZZER_FUCHSIA 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 28 | #define LIBFUZZER_LINUX 1 |
kamil | edcfbba | 2017-08-30 22:44:11 +0000 | [diff] [blame] | 29 | #define LIBFUZZER_NETBSD 0 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 30 | #define LIBFUZZER_FREEBSD 0 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 31 | #define LIBFUZZER_OPENBSD 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 32 | #define LIBFUZZER_WINDOWS 0 |
| 33 | #elif __APPLE__ |
| 34 | #define LIBFUZZER_APPLE 1 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 35 | #define LIBFUZZER_FUCHSIA 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 36 | #define LIBFUZZER_LINUX 0 |
kamil | edcfbba | 2017-08-30 22:44:11 +0000 | [diff] [blame] | 37 | #define LIBFUZZER_NETBSD 0 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 38 | #define LIBFUZZER_FREEBSD 0 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 39 | #define LIBFUZZER_OPENBSD 0 |
kamil | edcfbba | 2017-08-30 22:44:11 +0000 | [diff] [blame] | 40 | #define LIBFUZZER_WINDOWS 0 |
| 41 | #elif __NetBSD__ |
| 42 | #define LIBFUZZER_APPLE 0 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 43 | #define LIBFUZZER_FUCHSIA 0 |
kamil | edcfbba | 2017-08-30 22:44:11 +0000 | [diff] [blame] | 44 | #define LIBFUZZER_LINUX 0 |
| 45 | #define LIBFUZZER_NETBSD 1 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 46 | #define LIBFUZZER_FREEBSD 0 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 47 | #define LIBFUZZER_OPENBSD 0 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 48 | #define LIBFUZZER_WINDOWS 0 |
| 49 | #elif __FreeBSD__ |
| 50 | #define LIBFUZZER_APPLE 0 |
| 51 | #define LIBFUZZER_FUCHSIA 0 |
| 52 | #define LIBFUZZER_LINUX 0 |
| 53 | #define LIBFUZZER_NETBSD 0 |
| 54 | #define LIBFUZZER_FREEBSD 1 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 55 | #define LIBFUZZER_OPENBSD 0 |
| 56 | #define LIBFUZZER_WINDOWS 0 |
| 57 | #elif __OpenBSD__ |
| 58 | #define LIBFUZZER_APPLE 0 |
| 59 | #define LIBFUZZER_FUCHSIA 0 |
| 60 | #define LIBFUZZER_LINUX 0 |
| 61 | #define LIBFUZZER_NETBSD 0 |
| 62 | #define LIBFUZZER_FREEBSD 0 |
| 63 | #define LIBFUZZER_OPENBSD 1 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 64 | #define LIBFUZZER_WINDOWS 0 |
| 65 | #elif _WIN32 |
| 66 | #define LIBFUZZER_APPLE 0 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 67 | #define LIBFUZZER_FUCHSIA 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 68 | #define LIBFUZZER_LINUX 0 |
kamil | edcfbba | 2017-08-30 22:44:11 +0000 | [diff] [blame] | 69 | #define LIBFUZZER_NETBSD 0 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 70 | #define LIBFUZZER_FREEBSD 0 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 71 | #define LIBFUZZER_OPENBSD 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 72 | #define LIBFUZZER_WINDOWS 1 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 73 | #elif __Fuchsia__ |
| 74 | #define LIBFUZZER_APPLE 0 |
| 75 | #define LIBFUZZER_FUCHSIA 1 |
| 76 | #define LIBFUZZER_LINUX 0 |
| 77 | #define LIBFUZZER_NETBSD 0 |
kamil | 2142323 | 2018-01-12 17:15:05 +0000 | [diff] [blame] | 78 | #define LIBFUZZER_FREEBSD 0 |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 79 | #define LIBFUZZER_OPENBSD 0 |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 80 | #define LIBFUZZER_WINDOWS 0 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 81 | #else |
| 82 | #error "Support for your platform has not been implemented" |
| 83 | #endif |
| 84 | |
| 85 | #ifndef __has_attribute |
| 86 | # define __has_attribute(x) 0 |
| 87 | #endif |
| 88 | |
vitalybuka | 5f3206d | 2018-04-09 22:38:26 +0000 | [diff] [blame] | 89 | #define LIBFUZZER_POSIX \ |
| 90 | (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \ |
| 91 | LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD) |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 92 | |
| 93 | #ifdef __x86_64 |
| 94 | # if __has_attribute(target) |
| 95 | # define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt"))) |
| 96 | # else |
| 97 | # define ATTRIBUTE_TARGET_POPCNT |
| 98 | # endif |
| 99 | #else |
| 100 | # define ATTRIBUTE_TARGET_POPCNT |
| 101 | #endif |
| 102 | |
| 103 | |
| 104 | #ifdef __clang__ // avoid gcc warning. |
| 105 | # if __has_attribute(no_sanitize) |
| 106 | # define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory"))) |
| 107 | # else |
| 108 | # define ATTRIBUTE_NO_SANITIZE_MEMORY |
| 109 | # endif |
| 110 | # define ALWAYS_INLINE __attribute__((always_inline)) |
| 111 | #else |
| 112 | # define ATTRIBUTE_NO_SANITIZE_MEMORY |
| 113 | # define ALWAYS_INLINE |
| 114 | #endif // __clang__ |
| 115 | |
| 116 | #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) |
| 117 | |
| 118 | #if defined(__has_feature) |
| 119 | # if __has_feature(address_sanitizer) |
| 120 | # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 121 | # elif __has_feature(memory_sanitizer) |
| 122 | # define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY |
| 123 | # else |
| 124 | # define ATTRIBUTE_NO_SANITIZE_ALL |
| 125 | # endif |
| 126 | #else |
| 127 | # define ATTRIBUTE_NO_SANITIZE_ALL |
| 128 | #endif |
| 129 | |
| 130 | #if LIBFUZZER_WINDOWS |
| 131 | #define ATTRIBUTE_INTERFACE __declspec(dllexport) |
| 132 | #else |
| 133 | #define ATTRIBUTE_INTERFACE __attribute__((visibility("default"))) |
| 134 | #endif |
| 135 | |
| 136 | namespace fuzzer { |
| 137 | |
| 138 | template <class T> T Min(T a, T b) { return a < b ? a : b; } |
| 139 | template <class T> T Max(T a, T b) { return a > b ? a : b; } |
| 140 | |
| 141 | class Random; |
| 142 | class Dictionary; |
| 143 | class DictionaryEntry; |
| 144 | class MutationDispatcher; |
| 145 | struct FuzzingOptions; |
| 146 | class InputCorpus; |
| 147 | struct InputInfo; |
| 148 | struct ExternalFunctions; |
| 149 | |
| 150 | // Global interface to functions that may or may not be available. |
| 151 | extern ExternalFunctions *EF; |
| 152 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 153 | // We are using a custom allocator to give a different symbol name to STL |
| 154 | // containers in order to avoid ODR violations. |
| 155 | template<typename T> |
| 156 | class fuzzer_allocator: public std::allocator<T> { |
| 157 | public: |
ibiryukov | 2297022 | 2018-06-06 09:22:19 +0000 | [diff] [blame] | 158 | fuzzer_allocator() = default; |
| 159 | |
| 160 | template<class U> |
| 161 | fuzzer_allocator(const fuzzer_allocator<U>&) {} |
| 162 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 163 | template<class Other> |
| 164 | struct rebind { typedef fuzzer_allocator<Other> other; }; |
| 165 | }; |
| 166 | |
| 167 | template<typename T> |
| 168 | using Vector = std::vector<T, fuzzer_allocator<T>>; |
| 169 | |
| 170 | template<typename T> |
| 171 | using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>; |
| 172 | |
| 173 | typedef Vector<uint8_t> Unit; |
| 174 | typedef Vector<Unit> UnitVector; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 175 | typedef int (*UserCallback)(const uint8_t *Data, size_t Size); |
| 176 | |
| 177 | int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); |
| 178 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 179 | inline uint8_t Bswap(uint8_t x) { return x; } |
| 180 | inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); } |
| 181 | inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); } |
| 182 | inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); } |
| 183 | |
| 184 | uint8_t *ExtraCountersBegin(); |
| 185 | uint8_t *ExtraCountersEnd(); |
| 186 | void ClearExtraCounters(); |
| 187 | |
morehouse | c6ee875 | 2018-07-17 16:12:00 +0000 | [diff] [blame^] | 188 | extern bool RunningUserCallback; |
| 189 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 190 | } // namespace fuzzer |
| 191 | |
| 192 | #endif // LLVM_FUZZER_DEFS_H |