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