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