blob: c3dccbcd86f5c8edf6f5448af91b09b276051b15 [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
vitalybuka5f3206d2018-04-09 22:38:26 +000031#define LIBFUZZER_OPENBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000032#define LIBFUZZER_WINDOWS 0
33#elif __APPLE__
34#define LIBFUZZER_APPLE 1
morehouse400262a2017-12-08 22:54:44 +000035#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000036#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000037#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000038#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000039#define LIBFUZZER_OPENBSD 0
kamiledcfbba2017-08-30 22:44:11 +000040#define LIBFUZZER_WINDOWS 0
41#elif __NetBSD__
42#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000043#define LIBFUZZER_FUCHSIA 0
kamiledcfbba2017-08-30 22:44:11 +000044#define LIBFUZZER_LINUX 0
45#define LIBFUZZER_NETBSD 1
kamil21423232018-01-12 17:15:05 +000046#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000047#define LIBFUZZER_OPENBSD 0
kamil21423232018-01-12 17:15:05 +000048#define LIBFUZZER_WINDOWS 0
49#elif __FreeBSD__
50#define LIBFUZZER_APPLE 0
51#define LIBFUZZER_FUCHSIA 0
52#define LIBFUZZER_LINUX 0
53#define LIBFUZZER_NETBSD 0
54#define LIBFUZZER_FREEBSD 1
vitalybuka5f3206d2018-04-09 22:38:26 +000055#define LIBFUZZER_OPENBSD 0
56#define LIBFUZZER_WINDOWS 0
57#elif __OpenBSD__
58#define LIBFUZZER_APPLE 0
59#define LIBFUZZER_FUCHSIA 0
60#define LIBFUZZER_LINUX 0
61#define LIBFUZZER_NETBSD 0
62#define LIBFUZZER_FREEBSD 0
63#define LIBFUZZER_OPENBSD 1
george.karpenkov29efa6d2017-08-21 23:25:50 +000064#define LIBFUZZER_WINDOWS 0
65#elif _WIN32
66#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000067#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000068#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000069#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000070#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000071#define LIBFUZZER_OPENBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000072#define LIBFUZZER_WINDOWS 1
morehouse400262a2017-12-08 22:54:44 +000073#elif __Fuchsia__
74#define LIBFUZZER_APPLE 0
75#define LIBFUZZER_FUCHSIA 1
76#define LIBFUZZER_LINUX 0
77#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000078#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000079#define LIBFUZZER_OPENBSD 0
morehouse400262a2017-12-08 22:54:44 +000080#define LIBFUZZER_WINDOWS 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000081#else
82#error "Support for your platform has not been implemented"
83#endif
84
metzman40132972019-01-09 21:46:09 +000085#if defined(_MSC_VER) && !defined(__clang__)
86// MSVC compiler is being used.
87#define LIBFUZZER_MSVC 1
88#else
89#define LIBFUZZER_MSVC 0
90#endif
91
george.karpenkov29efa6d2017-08-21 23:25:50 +000092#ifndef __has_attribute
93# define __has_attribute(x) 0
94#endif
95
vitalybuka5f3206d2018-04-09 22:38:26 +000096#define LIBFUZZER_POSIX \
97 (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \
98 LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD)
george.karpenkov29efa6d2017-08-21 23:25:50 +000099
100#ifdef __x86_64
101# if __has_attribute(target)
102# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
103# else
104# define ATTRIBUTE_TARGET_POPCNT
105# endif
106#else
107# define ATTRIBUTE_TARGET_POPCNT
108#endif
109
110
111#ifdef __clang__ // avoid gcc warning.
112# if __has_attribute(no_sanitize)
113# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
114# else
115# define ATTRIBUTE_NO_SANITIZE_MEMORY
116# endif
117# define ALWAYS_INLINE __attribute__((always_inline))
118#else
119# define ATTRIBUTE_NO_SANITIZE_MEMORY
120# define ALWAYS_INLINE
121#endif // __clang__
122
123#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
124
125#if defined(__has_feature)
126# if __has_feature(address_sanitizer)
127# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
128# elif __has_feature(memory_sanitizer)
129# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
130# else
131# define ATTRIBUTE_NO_SANITIZE_ALL
132# endif
133#else
134# define ATTRIBUTE_NO_SANITIZE_ALL
135#endif
136
137#if LIBFUZZER_WINDOWS
138#define ATTRIBUTE_INTERFACE __declspec(dllexport)
morehouse68f46432018-08-30 15:54:44 +0000139// This is used for __sancov_lowest_stack which is needed for
140// -fsanitize-coverage=stack-depth. That feature is not yet available on
141// Windows, so make the symbol static to avoid linking errors.
142#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
143 __attribute__((tls_model("initial-exec"))) thread_local static
george.karpenkov29efa6d2017-08-21 23:25:50 +0000144#else
145#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
morehouse68f46432018-08-30 15:54:44 +0000146#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
147 ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
george.karpenkov29efa6d2017-08-21 23:25:50 +0000148#endif
149
150namespace fuzzer {
151
152template <class T> T Min(T a, T b) { return a < b ? a : b; }
153template <class T> T Max(T a, T b) { return a > b ? a : b; }
154
155class Random;
156class Dictionary;
157class DictionaryEntry;
158class MutationDispatcher;
159struct FuzzingOptions;
160class InputCorpus;
161struct InputInfo;
162struct ExternalFunctions;
163
164// Global interface to functions that may or may not be available.
165extern ExternalFunctions *EF;
166
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000167// We are using a custom allocator to give a different symbol name to STL
168// containers in order to avoid ODR violations.
169template<typename T>
170 class fuzzer_allocator: public std::allocator<T> {
171 public:
ibiryukov22970222018-06-06 09:22:19 +0000172 fuzzer_allocator() = default;
173
174 template<class U>
175 fuzzer_allocator(const fuzzer_allocator<U>&) {}
176
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000177 template<class Other>
178 struct rebind { typedef fuzzer_allocator<Other> other; };
179 };
180
181template<typename T>
182using Vector = std::vector<T, fuzzer_allocator<T>>;
183
184template<typename T>
185using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
186
187typedef Vector<uint8_t> Unit;
188typedef Vector<Unit> UnitVector;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000189typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
190
191int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
192
george.karpenkov29efa6d2017-08-21 23:25:50 +0000193uint8_t *ExtraCountersBegin();
194uint8_t *ExtraCountersEnd();
195void ClearExtraCounters();
196
morehousec6ee8752018-07-17 16:12:00 +0000197extern bool RunningUserCallback;
198
george.karpenkov29efa6d2017-08-21 23:25:50 +0000199} // namespace fuzzer
200
201#endif // LLVM_FUZZER_DEFS_H