blob: 27f5719236dd87509ccc58ba89e156590afb2d87 [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50: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//===----------------------------------------------------------------------===//
Marcos Pividori178fe582016-12-13 17:46:11 +000011
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000012#ifndef LLVM_FUZZER_DEFS_H
13#define LLVM_FUZZER_DEFS_H
14
Kostya Serebryany556894f2016-09-21 02:05:39 +000015#include <cassert>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000016#include <cstddef>
17#include <cstdint>
Kostya Serebryany86586182016-09-21 21:17:23 +000018#include <cstring>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000019#include <string>
20#include <vector>
21
22// Platform detection.
23#ifdef __linux__
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000024#define LIBFUZZER_APPLE 0
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000025#define LIBFUZZER_LINUX 1
26#define LIBFUZZER_WINDOWS 0
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000027#elif __APPLE__
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000028#define LIBFUZZER_APPLE 1
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000029#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
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000035#else
36#error "Support for your platform has not been implemented"
37#endif
38
Kuba Mracek9eb170f2017-04-21 16:57:37 +000039#ifndef __has_attribute
40# define __has_attribute(x) 0
41#endif
42
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000043#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
44
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045#ifdef __x86_64
Kuba Mracek9eb170f2017-04-21 16:57:37 +000046# if __has_attribute(target)
47# define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
48# else
49# define ATTRIBUTE_TARGET_POPCNT
50# endif
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000051#else
Kuba Mracek9eb170f2017-04-21 16:57:37 +000052# define ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000053#endif
54
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +000055
56#ifdef __clang__ // avoid gcc warning.
Kuba Mraceka0402622017-04-21 22:58:55 +000057# 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
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +000062# define ALWAYS_INLINE __attribute__((always_inline))
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +000063#else
64# define ATTRIBUTE_NO_SANITIZE_MEMORY
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +000065# define ALWAYS_INLINE
66#endif // __clang__
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +000067
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000068#define ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
69
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000070#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
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000081
Marcos Pividori6137f982017-01-22 01:27:38 +000082#if LIBFUZZER_WINDOWS
83#define ATTRIBUTE_INTERFACE __declspec(dllexport)
84#else
85#define ATTRIBUTE_INTERFACE __attribute__((visibility("default")))
86#endif
87
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000088namespace fuzzer {
89
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000090template <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
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000093class Random;
94class Dictionary;
95class DictionaryEntry;
96class MutationDispatcher;
Kostya Serebryany556894f2016-09-21 02:05:39 +000097struct FuzzingOptions;
98class InputCorpus;
Kostya Serebryany29bb6642016-09-21 22:42:17 +000099struct InputInfo;
Kostya Serebryany556894f2016-09-21 02:05:39 +0000100struct ExternalFunctions;
101
102// Global interface to functions that may or may not be available.
103extern ExternalFunctions *EF;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000104
105typedef std::vector<uint8_t> Unit;
106typedef std::vector<Unit> UnitVector;
107typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Zachary Turner24a148b2016-11-30 19:06:14 +0000108
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000109int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
110
Kostya Serebryanyaf2dfce2017-03-31 02:21:28 +0000111struct ScopedDoingMyOwnMemOrStr {
112 ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr++; }
113 ~ScopedDoingMyOwnMemOrStr() { DoingMyOwnMemOrStr--; }
114 static int DoingMyOwnMemOrStr;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000115};
116
Kostya Serebryany9a4b10a2016-10-15 04:00:07 +0000117inline 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
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000122uint8_t *ExtraCountersBegin();
123uint8_t *ExtraCountersEnd();
124void ClearExtraCounters();
125
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000126} // namespace fuzzer
Marcos Pividori178fe582016-12-13 17:46:11 +0000127
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000128#endif // LLVM_FUZZER_DEFS_H