blob: 6c53e6347b48c451bb2886985c17a903909a08af [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- 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.karpenkovfbfa45c2017-08-27 23:20:09 +000021#include <set>
22#include <memory>
george.karpenkov29efa6d2017-08-21 23:25:50 +000023
24// Platform detection.
25#ifdef __linux__
26#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000027#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000028#define LIBFUZZER_LINUX 1
kamiledcfbba2017-08-30 22:44:11 +000029#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000030#define LIBFUZZER_FREEBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000031#define LIBFUZZER_WINDOWS 0
32#elif __APPLE__
33#define LIBFUZZER_APPLE 1
morehouse400262a2017-12-08 22:54:44 +000034#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000035#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000036#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000037#define LIBFUZZER_FREEBSD 0
kamiledcfbba2017-08-30 22:44:11 +000038#define LIBFUZZER_WINDOWS 0
39#elif __NetBSD__
40#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000041#define LIBFUZZER_FUCHSIA 0
kamiledcfbba2017-08-30 22:44:11 +000042#define LIBFUZZER_LINUX 0
43#define LIBFUZZER_NETBSD 1
kamil21423232018-01-12 17:15:05 +000044#define LIBFUZZER_FREEBSD 0
45#define LIBFUZZER_WINDOWS 0
46#elif __FreeBSD__
47#define LIBFUZZER_APPLE 0
48#define LIBFUZZER_FUCHSIA 0
49#define LIBFUZZER_LINUX 0
50#define LIBFUZZER_NETBSD 0
51#define LIBFUZZER_FREEBSD 1
george.karpenkov29efa6d2017-08-21 23:25:50 +000052#define LIBFUZZER_WINDOWS 0
53#elif _WIN32
54#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000055#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000056#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000057#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000058#define LIBFUZZER_FREEBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000059#define LIBFUZZER_WINDOWS 1
morehouse400262a2017-12-08 22:54:44 +000060#elif __Fuchsia__
61#define LIBFUZZER_APPLE 0
62#define LIBFUZZER_FUCHSIA 1
63#define LIBFUZZER_LINUX 0
64#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000065#define LIBFUZZER_FREEBSD 0
morehouse400262a2017-12-08 22:54:44 +000066#define LIBFUZZER_WINDOWS 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000067#else
68#error "Support for your platform has not been implemented"
69#endif
70
71#ifndef __has_attribute
72# define __has_attribute(x) 0
73#endif
74
kamil21423232018-01-12 17:15:05 +000075#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD)
george.karpenkov29efa6d2017-08-21 23:25:50 +000076
77#ifdef __x86_64
78# if __has_attribute(target)
79# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
80# else
81# define ATTRIBUTE_TARGET_POPCNT
82# endif
83#else
84# define ATTRIBUTE_TARGET_POPCNT
85#endif
86
87
88#ifdef __clang__ // avoid gcc warning.
89# if __has_attribute(no_sanitize)
90# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
91# else
92# define ATTRIBUTE_NO_SANITIZE_MEMORY
93# endif
94# define ALWAYS_INLINE __attribute__((always_inline))
95#else
96# define ATTRIBUTE_NO_SANITIZE_MEMORY
97# define ALWAYS_INLINE
98#endif // __clang__
99
100#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
101
102#if defined(__has_feature)
103# if __has_feature(address_sanitizer)
104# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
105# elif __has_feature(memory_sanitizer)
106# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
107# else
108# define ATTRIBUTE_NO_SANITIZE_ALL
109# endif
110#else
111# define ATTRIBUTE_NO_SANITIZE_ALL
112#endif
113
114#if LIBFUZZER_WINDOWS
115#define ATTRIBUTE_INTERFACE __declspec(dllexport)
116#else
117#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
118#endif
119
120namespace fuzzer {
121
122template <class T> T Min(T a, T b) { return a < b ? a : b; }
123template <class T> T Max(T a, T b) { return a > b ? a : b; }
124
125class Random;
126class Dictionary;
127class DictionaryEntry;
128class MutationDispatcher;
129struct FuzzingOptions;
130class InputCorpus;
131struct InputInfo;
132struct ExternalFunctions;
133
134// Global interface to functions that may or may not be available.
135extern ExternalFunctions *EF;
136
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000137// We are using a custom allocator to give a different symbol name to STL
138// containers in order to avoid ODR violations.
139template<typename T>
140 class fuzzer_allocator: public std::allocator<T> {
141 public:
142 template<class Other>
143 struct rebind { typedef fuzzer_allocator<Other> other; };
144 };
145
146template<typename T>
147using Vector = std::vector<T, fuzzer_allocator<T>>;
148
149template<typename T>
150using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
151
152typedef Vector<uint8_t> Unit;
153typedef Vector<Unit> UnitVector;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000154typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
155
156int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
157
158struct ScopedDoingMyOwnMemOrStr {
159 ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
160 ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
161 static int DoingMyOwnMemOrStr;
162};
163
164inline uint8_t Bswap(uint8_t x) { return x; }
165inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
166inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
167inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
168
169uint8_t *ExtraCountersBegin();
170uint8_t *ExtraCountersEnd();
171void ClearExtraCounters();
172
kcc1c0379f2017-08-22 01:28:32 +0000173uint64_t *ClangCountersBegin();
174uint64_t *ClangCountersEnd();
175void ClearClangCounters();
176
george.karpenkov29efa6d2017-08-21 23:25:50 +0000177} // namespace fuzzer
178
179#endif // LLVM_FUZZER_DEFS_H