blob: 7ea54a92062bd0eb13969ba13129b3fd38244bbb [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
27#define LIBFUZZER_LINUX 1
28#define LIBFUZZER_WINDOWS 0
29#elif __APPLE__
30#define LIBFUZZER_APPLE 1
31#define LIBFUZZER_LINUX 0
32#define LIBFUZZER_WINDOWS 0
33#elif _WIN32
34#define LIBFUZZER_APPLE 0
35#define LIBFUZZER_LINUX 0
36#define LIBFUZZER_WINDOWS 1
37#else
38#error "Support for your platform has not been implemented"
39#endif
40
41#ifndef __has_attribute
42# define __has_attribute(x) 0
43#endif
44
45#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
46
47#ifdef __x86_64
48# if __has_attribute(target)
49# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
50# else
51# define ATTRIBUTE_TARGET_POPCNT
52# endif
53#else
54# define ATTRIBUTE_TARGET_POPCNT
55#endif
56
57
58#ifdef __clang__ // avoid gcc warning.
59# if __has_attribute(no_sanitize)
60# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
61# else
62# define ATTRIBUTE_NO_SANITIZE_MEMORY
63# endif
64# define ALWAYS_INLINE __attribute__((always_inline))
65#else
66# define ATTRIBUTE_NO_SANITIZE_MEMORY
67# define ALWAYS_INLINE
68#endif // __clang__
69
70#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
71
72#if defined(__has_feature)
73# if __has_feature(address_sanitizer)
74# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
75# elif __has_feature(memory_sanitizer)
76# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
77# else
78# define ATTRIBUTE_NO_SANITIZE_ALL
79# endif
80#else
81# define ATTRIBUTE_NO_SANITIZE_ALL
82#endif
83
84#if LIBFUZZER_WINDOWS
85#define ATTRIBUTE_INTERFACE __declspec(dllexport)
86#else
87#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
88#endif
89
90namespace fuzzer {
91
92template <class T> T Min(T a, T b) { return a < b ? a : b; }
93template <class T> T Max(T a, T b) { return a > b ? a : b; }
94
95class Random;
96class Dictionary;
97class DictionaryEntry;
98class MutationDispatcher;
99struct FuzzingOptions;
100class InputCorpus;
101struct InputInfo;
102struct ExternalFunctions;
103
104// Global interface to functions that may or may not be available.
105extern ExternalFunctions *EF;
106
george.karpenkovfbfa45c2017-08-27 23:20:09 +0000107// We are using a custom allocator to give a different symbol name to STL
108// containers in order to avoid ODR violations.
109template<typename T>
110 class fuzzer_allocator: public std::allocator<T> {
111 public:
112 template<class Other>
113 struct rebind { typedef fuzzer_allocator<Other> other; };
114 };
115
116template<typename T>
117using Vector = std::vector<T, fuzzer_allocator<T>>;
118
119template<typename T>
120using Set = std::set<T, std::less<T>, fuzzer_allocator<T>>;
121
122typedef Vector<uint8_t> Unit;
123typedef Vector<Unit> UnitVector;
george.karpenkov29efa6d2017-08-21 23:25:50 +0000124typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
125
126int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
127
128struct ScopedDoingMyOwnMemOrStr {
129 ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
130 ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
131 static int DoingMyOwnMemOrStr;
132};
133
134inline uint8_t Bswap(uint8_t x) { return x; }
135inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
136inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
137inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
138
139uint8_t *ExtraCountersBegin();
140uint8_t *ExtraCountersEnd();
141void ClearExtraCounters();
142
kcc1c0379f2017-08-22 01:28:32 +0000143uint64_t *ClangCountersBegin();
144uint64_t *ClangCountersEnd();
145void ClearClangCounters();
146
george.karpenkov29efa6d2017-08-21 23:25:50 +0000147} // namespace fuzzer
148
149#endif // LLVM_FUZZER_DEFS_H