blob: 320b37d5f8e3ac9c68959218e5cd47bd9a65054c [file] [log] [blame]
george.karpenkov29efa6d2017-08-21 23:25:50 +00001//===- FuzzerDefs.h - Internal header for the Fuzzer ------------*- C++ -* ===//
2//
chandlerc40284492019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
george.karpenkov29efa6d2017-08-21 23:25:50 +00006//
7//===----------------------------------------------------------------------===//
8// Basic definitions.
9//===----------------------------------------------------------------------===//
10
11#ifndef LLVM_FUZZER_DEFS_H
12#define LLVM_FUZZER_DEFS_H
13
14#include <cassert>
15#include <cstddef>
16#include <cstdint>
17#include <cstring>
18#include <string>
19#include <vector>
george.karpenkovfbfa45c2017-08-27 23:20:09 +000020#include <set>
21#include <memory>
george.karpenkov29efa6d2017-08-21 23:25:50 +000022
23// Platform detection.
24#ifdef __linux__
25#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000026#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000027#define LIBFUZZER_LINUX 1
kamiledcfbba2017-08-30 22:44:11 +000028#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000029#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000030#define LIBFUZZER_OPENBSD 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
vitalybuka5f3206d2018-04-09 22:38:26 +000038#define LIBFUZZER_OPENBSD 0
kamiledcfbba2017-08-30 22:44:11 +000039#define LIBFUZZER_WINDOWS 0
40#elif __NetBSD__
41#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000042#define LIBFUZZER_FUCHSIA 0
kamiledcfbba2017-08-30 22:44:11 +000043#define LIBFUZZER_LINUX 0
44#define LIBFUZZER_NETBSD 1
kamil21423232018-01-12 17:15:05 +000045#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000046#define LIBFUZZER_OPENBSD 0
kamil21423232018-01-12 17:15:05 +000047#define LIBFUZZER_WINDOWS 0
48#elif __FreeBSD__
49#define LIBFUZZER_APPLE 0
50#define LIBFUZZER_FUCHSIA 0
51#define LIBFUZZER_LINUX 0
52#define LIBFUZZER_NETBSD 0
53#define LIBFUZZER_FREEBSD 1
vitalybuka5f3206d2018-04-09 22:38:26 +000054#define LIBFUZZER_OPENBSD 0
55#define LIBFUZZER_WINDOWS 0
56#elif __OpenBSD__
57#define LIBFUZZER_APPLE 0
58#define LIBFUZZER_FUCHSIA 0
59#define LIBFUZZER_LINUX 0
60#define LIBFUZZER_NETBSD 0
61#define LIBFUZZER_FREEBSD 0
62#define LIBFUZZER_OPENBSD 1
george.karpenkov29efa6d2017-08-21 23:25:50 +000063#define LIBFUZZER_WINDOWS 0
64#elif _WIN32
65#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000066#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000067#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000068#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000069#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000070#define LIBFUZZER_OPENBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000071#define LIBFUZZER_WINDOWS 1
morehouse400262a2017-12-08 22:54:44 +000072#elif __Fuchsia__
73#define LIBFUZZER_APPLE 0
74#define LIBFUZZER_FUCHSIA 1
75#define LIBFUZZER_LINUX 0
76#define LIBFUZZER_NETBSD 0
kamil21423232018-01-12 17:15:05 +000077#define LIBFUZZER_FREEBSD 0
vitalybuka5f3206d2018-04-09 22:38:26 +000078#define LIBFUZZER_OPENBSD 0
morehouse400262a2017-12-08 22:54:44 +000079#define LIBFUZZER_WINDOWS 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000080#else
81#error "Support for your platform has not been implemented"
82#endif
83
metzman40132972019-01-09 21:46:09 +000084#if defined(_MSC_VER) && !defined(__clang__)
85// MSVC compiler is being used.
86#define LIBFUZZER_MSVC 1
87#else
88#define LIBFUZZER_MSVC 0
89#endif
90
george.karpenkov29efa6d2017-08-21 23:25:50 +000091#ifndef __has_attribute
92# define __has_attribute(x) 0
93#endif
94
vitalybuka5f3206d2018-04-09 22:38:26 +000095#define LIBFUZZER_POSIX \
96 (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD || \
97 LIBFUZZER_FREEBSD || LIBFUZZER_OPENBSD)
george.karpenkov29efa6d2017-08-21 23:25:50 +000098
99#ifdef __x86_64
100# if __has_attribute(target)
101# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
102# else
103# define ATTRIBUTE_TARGET_POPCNT
104# endif
105#else
106# define ATTRIBUTE_TARGET_POPCNT
107#endif
108
109
110#ifdef __clang__ // avoid gcc warning.
111# if __has_attribute(no_sanitize)
112# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
113# else
114# define ATTRIBUTE_NO_SANITIZE_MEMORY
115# endif
116# define ALWAYS_INLINE __attribute__((always_inline))
117#else
118# define ATTRIBUTE_NO_SANITIZE_MEMORY
119# define ALWAYS_INLINE
120#endif // __clang__
121
metzman2fe66e62019-01-17 16:36:05 +0000122#if LIBFUZZER_WINDOWS
123#define ATTRIBUTE_NO_SANITIZE_ADDRESS
124#else
george.karpenkov29efa6d2017-08-21 23:25:50 +0000125#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
metzman2fe66e62019-01-17 16:36:05 +0000126#endif
127
128#if LIBFUZZER_WINDOWS
129#define ATTRIBUTE_ALIGNED(X) __declspec(align(X))
130#define ATTRIBUTE_INTERFACE __declspec(dllexport)
131// This is used for __sancov_lowest_stack which is needed for
132// -fsanitize-coverage=stack-depth. That feature is not yet available on
133// Windows, so make the symbol static to avoid linking errors.
134#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC static
135#define ATTRIBUTE_NOINLINE __declspec(noinline)
136#else
137#define ATTRIBUTE_ALIGNED(X) __attribute__((aligned(X)))
138#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
139#define ATTRIBUTES_INTERFACE_TLS_INITIAL_EXEC \
140 ATTRIBUTE_INTERFACE __attribute__((tls_model("initial-exec"))) thread_local
141
142#define ATTRIBUTE_NOINLINE __attribute__((noinline))
143#endif
george.karpenkov29efa6d2017-08-21 23:25:50 +0000144
145#if defined(__has_feature)
146# if __has_feature(address_sanitizer)
147# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
148# elif __has_feature(memory_sanitizer)
149# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
150# else
151# define ATTRIBUTE_NO_SANITIZE_ALL
152# endif
153#else
154# define ATTRIBUTE_NO_SANITIZE_ALL
155#endif
156
george.karpenkov29efa6d2017-08-21 23:25:50 +0000157namespace fuzzer {
158
159template <class T> T Min(T a, T b) { return a < b ? a : b; }
160template <class T> T Max(T a, T b) { return a > b ? a : b; }
161
162class Random;
163class Dictionary;
164class DictionaryEntry;
165class MutationDispatcher;
166struct FuzzingOptions;
167class InputCorpus;
168struct InputInfo;
169struct ExternalFunctions;
170
171// Global interface to functions that may or may not be available.
172extern ExternalFunctions *EF;
173
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000174// We are using a custom allocator to give a different symbol name to STL
175// containers in order to avoid ODR violations.
176template<typename T>
177 class fuzzer_allocator: public std::allocator<T> {
178 public:
ibiryukov22970222018-06-06 09:22:19 +0000179 fuzzer_allocator() = default;
180
181 template<class U>
182 fuzzer_allocator(const fuzzer_allocator<U>&) {}
183
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000184 template<class Other>
185 struct rebind { typedef fuzzer_allocator<Other> other; };
186 };
187
188template<typename T>
189using Vector = std::vector<T, fuzzer_allocator<T>>;
190
191template<typename T>
192using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
193
194typedef Vector<uint8_t> Unit;
195typedef Vector<Unit> UnitVector;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000196typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
197
198int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
199
george.karpenkov29efa6d2017-08-21 23:25:50 +0000200uint8_t *ExtraCountersBegin();
201uint8_t *ExtraCountersEnd();
202void ClearExtraCounters();
203
morehousec6ee8752018-07-17 16:12:00 +0000204extern bool RunningUserCallback;
205
george.karpenkov29efa6d2017-08-21 23:25:50 +0000206} // namespace fuzzer
207
208#endif // LLVM_FUZZER_DEFS_H