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