blob: 5942efc47a4a1aeef23a415c8ec8faa3eed8d09f [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
george.karpenkov29efa6d2017-08-21 23:25:50 +000030#define LIBFUZZER_WINDOWS 0
31#elif __APPLE__
32#define LIBFUZZER_APPLE 1
morehouse400262a2017-12-08 22:54:44 +000033#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000034#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000035#define LIBFUZZER_NETBSD 0
36#define LIBFUZZER_WINDOWS 0
37#elif __NetBSD__
38#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000039#define LIBFUZZER_FUCHSIA 0
kamiledcfbba2017-08-30 22:44:11 +000040#define LIBFUZZER_LINUX 0
41#define LIBFUZZER_NETBSD 1
george.karpenkov29efa6d2017-08-21 23:25:50 +000042#define LIBFUZZER_WINDOWS 0
43#elif _WIN32
44#define LIBFUZZER_APPLE 0
morehouse400262a2017-12-08 22:54:44 +000045#define LIBFUZZER_FUCHSIA 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000046#define LIBFUZZER_LINUX 0
kamiledcfbba2017-08-30 22:44:11 +000047#define LIBFUZZER_NETBSD 0
george.karpenkov29efa6d2017-08-21 23:25:50 +000048#define LIBFUZZER_WINDOWS 1
morehouse400262a2017-12-08 22:54:44 +000049#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
george.karpenkov29efa6d2017-08-21 23:25:50 +000055#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
kamiledcfbba2017-08-30 22:44:11 +000063#define LIBFUZZER_POSIX (LIBFUZZER_APPLE || LIBFUZZER_LINUX || LIBFUZZER_NETBSD)
george.karpenkov29efa6d2017-08-21 23:25:50 +000064
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
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000125// 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;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000142typedef 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
kcc1c0379f2017-08-22 01:28:32 +0000161uint64_t *ClangCountersBegin();
162uint64_t *ClangCountersEnd();
163void ClearClangCounters();
164
george.karpenkov29efa6d2017-08-21 23:25:50 +0000165} // namespace fuzzer
166
167#endif // LLVM_FUZZER_DEFS_H