blob: 27f5719236dd87509ccc58ba89e156590afb2d87 [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>
21
22// Platform detection.
23#ifdef __linux__
24#define LIBFUZZER_APPLE 0
25#define LIBFUZZER_LINUX 1
26#define LIBFUZZER_WINDOWS 0
27#elif __APPLE__
28#define LIBFUZZER_APPLE 1
29#define LIBFUZZER_LINUX 0
30#define LIBFUZZER_WINDOWS 0
31#elif _WIN32
32#define LIBFUZZER_APPLE 0
33#define LIBFUZZER_LINUX 0
34#define LIBFUZZER_WINDOWS 1
35#else
36#error "Support for your platform has not been implemented"
37#endif
38
39#ifndef __has_attribute
40# define __has_attribute(x) 0
41#endif
42
43#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
44
45#ifdef __x86_64
46# if __has_attribute(target)
47# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
48# else
49# define ATTRIBUTE_TARGET_POPCNT
50# endif
51#else
52# define ATTRIBUTE_TARGET_POPCNT
53#endif
54
55
56#ifdef __clang__ // avoid gcc warning.
57# if __has_attribute(no_sanitize)
58# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
59# else
60# define ATTRIBUTE_NO_SANITIZE_MEMORY
61# endif
62# define ALWAYS_INLINE __attribute__((always_inline))
63#else
64# define ATTRIBUTE_NO_SANITIZE_MEMORY
65# define ALWAYS_INLINE
66#endif // __clang__
67
68#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
69
70#if defined(__has_feature)
71# if __has_feature(address_sanitizer)
72# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_ADDRESS
73# elif __has_feature(memory_sanitizer)
74# define ATTRIBUTE_NO_SANITIZE_ALL ATTRIBUTE_NO_SANITIZE_MEMORY
75# else
76# define ATTRIBUTE_NO_SANITIZE_ALL
77# endif
78#else
79# define ATTRIBUTE_NO_SANITIZE_ALL
80#endif
81
82#if LIBFUZZER_WINDOWS
83#define ATTRIBUTE_INTERFACE __declspec(dllexport)
84#else
85#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
86#endif
87
88namespace fuzzer {
89
90template <class T> T Min(T a, T b) { return a < b ? a : b; }
91template <class T> T Max(T a, T b) { return a > b ? a : b; }
92
93class Random;
94class Dictionary;
95class DictionaryEntry;
96class MutationDispatcher;
97struct FuzzingOptions;
98class InputCorpus;
99struct InputInfo;
100struct ExternalFunctions;
101
102// Global interface to functions that may or may not be available.
103extern ExternalFunctions *EF;
104
105typedef std::vector<uint8_t> Unit;
106typedef std::vector<Unit> UnitVector;
107typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
108
109int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
110
111struct ScopedDoingMyOwnMemOrStr {
112 ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
113 ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
114 static int DoingMyOwnMemOrStr;
115};
116
117inline uint8_t Bswap(uint8_t x) { return x; }
118inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
119inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
120inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
121
122uint8_t *ExtraCountersBegin();
123uint8_t *ExtraCountersEnd();
124void ClearExtraCounters();
125
126} // namespace fuzzer
127
128#endif // LLVM_FUZZER_DEFS_H