OSDN Git Service

[libFuzzer] Check for target(popcnt) capability before usage
[android-x86/external-llvm.git] / lib / Fuzzer / FuzzerDefs.h
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>
21
22 // Platform detection.
23 #ifdef __linux__
24 #define LIBFUZZER_APPLE 0
25 #define LIBFUZZER_LINUX 1
26 #define LIBFUZZER_WINDOWS 0
27 #elif __APPLE__
28 #define LIBFUZZER_APPLE 1
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
35 #else
36 #error "Support for your platform has not been implemented"
37 #endif
38
39 #ifndef __has_attribute
40 #  define __has_attribute(x) 0
41 #endif
42
43 #define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
44
45 #ifdef __x86_64
46 #  if __has_attribute(target)
47 #    define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
48 #  else
49 #    define ATTRIBUTE_TARGET_POPCNT
50 #  endif
51 #else
52 #  define ATTRIBUTE_TARGET_POPCNT
53 #endif
54
55
56 #ifdef __clang__  // avoid gcc warning.
57 #  define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
58 #  define ALWAYS_INLINE __attribute__((always_inline))
59 #else
60 #  define ATTRIBUTE_NO_SANITIZE_MEMORY
61 #  define ALWAYS_INLINE
62 #endif // __clang__
63
64 #define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
65
66 #if defined(__has_feature)
67 #  if __has_feature(address_sanitizer)
68 #    define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
69 #  elif __has_feature(memory_sanitizer)
70 #    define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
71 #  else
72 #    define ATTRIBUTE_NO_SANITIZE_ALL
73 #  endif
74 #else
75 #  define ATTRIBUTE_NO_SANITIZE_ALL
76 #endif
77
78 #if LIBFUZZER_WINDOWS
79 #define ATTRIBUTE_INTERFACE __declspec(dllexport)
80 #else
81 #define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
82 #endif
83
84 namespace fuzzer {
85
86 template <class T> T Min(T a, T b) { return a < b ? a : b; }
87 template <class T> T Max(T a, T b) { return a > b ? a : b; }
88
89 class Random;
90 class Dictionary;
91 class DictionaryEntry;
92 class MutationDispatcher;
93 struct FuzzingOptions;
94 class InputCorpus;
95 struct InputInfo;
96 struct ExternalFunctions;
97
98 // Global interface to functions that may or may not be available.
99 extern ExternalFunctions *EF;
100
101 typedef std::vector<uint8_t> Unit;
102 typedef std::vector<Unit> UnitVector;
103 typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
104
105 int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
106
107 struct ScopedDoingMyOwnMemOrStr {
108   ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
109   ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
110   static int DoingMyOwnMemOrStr;
111 };
112
113 inline uint8_t  Bswap(uint8_t x)  { return x; }
114 inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
115 inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
116 inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
117
118 uint8_t *ExtraCountersBegin();
119 uint8_t *ExtraCountersEnd();
120 void ClearExtraCounters();
121
122 }  // namespace fuzzer
123
124 #endif  // LLVM_FUZZER_DEFS_H