| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50: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 | //===----------------------------------------------------------------------===// | 
| Marcos Pividori | 178fe58 | 2016-12-13 17:46:11 +0000 | [diff] [blame] | 11 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 12 | #ifndef LLVM_FUZZER_DEFS_H | 
|  | 13 | #define LLVM_FUZZER_DEFS_H | 
|  | 14 |  | 
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 15 | #include <cassert> | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 16 | #include <cstddef> | 
|  | 17 | #include <cstdint> | 
| Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 18 | #include <cstring> | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 19 | #include <string> | 
|  | 20 | #include <vector> | 
|  | 21 |  | 
|  | 22 | // Platform detection. | 
|  | 23 | #ifdef __linux__ | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 24 | #define LIBFUZZER_APPLE 0 | 
| Zachary Turner | c6d8b4c | 2016-11-30 16:32:54 +0000 | [diff] [blame] | 25 | #define LIBFUZZER_LINUX 1 | 
|  | 26 | #define LIBFUZZER_WINDOWS 0 | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 27 | #elif __APPLE__ | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 28 | #define LIBFUZZER_APPLE 1 | 
| Zachary Turner | c6d8b4c | 2016-11-30 16:32:54 +0000 | [diff] [blame] | 29 | #define LIBFUZZER_LINUX 0 | 
|  | 30 | #define LIBFUZZER_WINDOWS 0 | 
|  | 31 | #elif _WIN32 | 
|  | 32 | #define LIBFUZZER_APPLE 0 | 
|  | 33 | #define LIBFUZZER_LINUX 0 | 
|  | 34 | #define LIBFUZZER_WINDOWS 1 | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 35 | #else | 
|  | 36 | #error "Support for your platform has not been implemented" | 
|  | 37 | #endif | 
|  | 38 |  | 
| Zachary Turner | c6d8b4c | 2016-11-30 16:32:54 +0000 | [diff] [blame] | 39 | #define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX | 
|  | 40 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 41 | #ifdef __x86_64 | 
|  | 42 | #define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt"))) | 
|  | 43 | #else | 
|  | 44 | #define ATTRIBUTE_TARGET_POPCNT | 
|  | 45 | #endif | 
|  | 46 |  | 
| Kostya Serebryany | 3a4e2dd | 2016-12-16 22:45:25 +0000 | [diff] [blame] | 47 |  | 
|  | 48 | #ifdef __clang__  // avoid gcc warning. | 
|  | 49 | #  define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory"))) | 
| Mike Aizatsky | 0e37f8e | 2017-01-17 23:11:32 +0000 | [diff] [blame] | 50 | #  define ALWAYS_INLINE __attribute__((always_inline)) | 
| Kostya Serebryany | 3a4e2dd | 2016-12-16 22:45:25 +0000 | [diff] [blame] | 51 | #else | 
|  | 52 | #  define ATTRIBUTE_NO_SANITIZE_MEMORY | 
| Mike Aizatsky | 0e37f8e | 2017-01-17 23:11:32 +0000 | [diff] [blame] | 53 | #  define ALWAYS_INLINE | 
|  | 54 | #endif // __clang__ | 
| Kostya Serebryany | 3a4e2dd | 2016-12-16 22:45:25 +0000 | [diff] [blame] | 55 |  | 
| Kostya Serebryany | d0ecb4c | 2017-01-26 01:04:54 +0000 | [diff] [blame] | 56 | #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) | 
|  | 57 |  | 
| Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 58 | #if defined(__has_feature) | 
|  | 59 | #  if __has_feature(address_sanitizer) | 
|  | 60 | #    define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS | 
|  | 61 | #  elif __has_feature(memory_sanitizer) | 
|  | 62 | #    define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY | 
|  | 63 | #  else | 
|  | 64 | #    define ATTRIBUTE_NO_SANITIZE_ALL | 
|  | 65 | #  endif | 
|  | 66 | #else | 
|  | 67 | #  define ATTRIBUTE_NO_SANITIZE_ALL | 
|  | 68 | #endif | 
| Kostya Serebryany | d0ecb4c | 2017-01-26 01:04:54 +0000 | [diff] [blame] | 69 |  | 
| Marcos Pividori | 6137f98 | 2017-01-22 01:27:38 +0000 | [diff] [blame] | 70 | #if LIBFUZZER_WINDOWS | 
|  | 71 | #define ATTRIBUTE_INTERFACE __declspec(dllexport) | 
|  | 72 | #else | 
|  | 73 | #define ATTRIBUTE_INTERFACE __attribute__((visibility("default"))) | 
|  | 74 | #endif | 
|  | 75 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 76 | namespace fuzzer { | 
|  | 77 |  | 
| Kostya Serebryany | d28099d | 2016-09-23 00:22:46 +0000 | [diff] [blame] | 78 | template <class T> T Min(T a, T b) { return a < b ? a : b; } | 
|  | 79 | template <class T> T Max(T a, T b) { return a > b ? a : b; } | 
|  | 80 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 81 | class Random; | 
|  | 82 | class Dictionary; | 
|  | 83 | class DictionaryEntry; | 
|  | 84 | class MutationDispatcher; | 
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 85 | struct FuzzingOptions; | 
|  | 86 | class InputCorpus; | 
| Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame] | 87 | struct InputInfo; | 
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 88 | struct ExternalFunctions; | 
|  | 89 |  | 
|  | 90 | // Global interface to functions that may or may not be available. | 
|  | 91 | extern ExternalFunctions *EF; | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 92 |  | 
|  | 93 | typedef std::vector<uint8_t> Unit; | 
|  | 94 | typedef std::vector<Unit> UnitVector; | 
|  | 95 | typedef int (*UserCallback)(const uint8_t *Data, size_t Size); | 
| Zachary Turner | 24a148b | 2016-11-30 19:06:14 +0000 | [diff] [blame] | 96 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 97 | int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); | 
|  | 98 |  | 
| Kostya Serebryany | af2dfce | 2017-03-31 02:21:28 +0000 | [diff] [blame] | 99 | struct ScopedDoingMyOwnMemOrStr { | 
|  | 100 | ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; } | 
|  | 101 | ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; } | 
|  | 102 | static int DoingMyOwnMemOrStr; | 
| Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 103 | }; | 
|  | 104 |  | 
| Kostya Serebryany | 9a4b10a | 2016-10-15 04:00:07 +0000 | [diff] [blame] | 105 | inline uint8_t  Bswap(uint8_t x)  { return x; } | 
|  | 106 | inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); } | 
|  | 107 | inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); } | 
|  | 108 | inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); } | 
|  | 109 |  | 
| Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 110 | uint8_t *ExtraCountersBegin(); | 
|  | 111 | uint8_t *ExtraCountersEnd(); | 
|  | 112 | void ClearExtraCounters(); | 
|  | 113 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 114 | }  // namespace fuzzer | 
| Marcos Pividori | 178fe58 | 2016-12-13 17:46:11 +0000 | [diff] [blame] | 115 |  | 
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 116 | #endif  // LLVM_FUZZER_DEFS_H |