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