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_LINUX 1
28#define LIBFUZZER_NETBSD 0
29#define LIBFUZZER_WINDOWS 0
30#elif __APPLE__
31#define LIBFUZZER_APPLE 1
32#define LIBFUZZER_LINUX 0
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
39#define LIBFUZZER_WINDOWS 0
40#elif _WIN32
41#define LIBFUZZER_APPLE 0
42#define LIBFUZZER_LINUX 0
43#define LIBFUZZER_NETBSD 0
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
53#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD)
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
98namespace fuzzer {
99
100template <class T> T Min(T a, T b) { return a < b ? a : b; }
101template <class T> T Max(T a, T b) { return a > b ? a : b; }
102
103class Random;
104class Dictionary;
105class DictionaryEntry;
106class MutationDispatcher;
107struct FuzzingOptions;
108class InputCorpus;
109struct InputInfo;
110struct ExternalFunctions;
111
112// Global interface to functions that may or may not be available.
113extern ExternalFunctions *EF;
114
115// We are using a custom allocator to give a different symbol name to STL
116// containers in order to avoid ODR violations.
117template<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
124template<typename T>
125using Vector = std::vector<T, fuzzer_allocator<T>>;
126
127template<typename T>
128using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
129
130typedef Vector<uint8_t> Unit;
131typedef Vector<Unit> UnitVector;
132typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
133
134int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
135
136struct ScopedDoingMyOwnMemOrStr {
137  ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
138  ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
139  static int DoingMyOwnMemOrStr;
140};
141
142inline uint8_t  Bswap(uint8_t x)  { return x; }
143inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
144inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
145inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
146
147uint8_t *ExtraCountersBegin();
148uint8_t *ExtraCountersEnd();
149void ClearExtraCounters();
150
151uint64_t *ClangCountersBegin();
152uint64_t *ClangCountersEnd();
153void ClearClangCounters();
154
155}  // namespace fuzzer
156
157#endif  // LLVM_FUZZER_DEFS_H
158