blob: 0f5b8a7cf2119dba93e954dcb05dba3e4638b98e [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
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000039#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
40
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000041#ifdef __x86_64
42#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
43#else
44#define ATTRIBUTE_TARGET_POPCNT
45#endif
46
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +000047
48#ifdef __clang__ // avoid gcc warning.
49# define ATTRIBUTE_NO_SANITIZE_MEMORY __attribute__((no_sanitize("memory")))
50#else
51# define ATTRIBUTE_NO_SANITIZE_MEMORY
52#endif
53
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000054namespace fuzzer {
55
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000056template <class T> T Min(T a, T b) { return a < b ? a : b; }
57template <class T> T Max(T a, T b) { return a > b ? a : b; }
58
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000059class Random;
60class Dictionary;
61class DictionaryEntry;
62class MutationDispatcher;
Kostya Serebryany556894f2016-09-21 02:05:39 +000063struct FuzzingOptions;
64class InputCorpus;
Kostya Serebryany29bb6642016-09-21 22:42:17 +000065struct InputInfo;
Kostya Serebryany556894f2016-09-21 02:05:39 +000066struct ExternalFunctions;
67
68// Global interface to functions that may or may not be available.
69extern ExternalFunctions *EF;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000070
71typedef std::vector<uint8_t> Unit;
72typedef std::vector<Unit> UnitVector;
73typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
Zachary Turner24a148b2016-11-30 19:06:14 +000074
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000075int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
76
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000077struct ScopedDoingMyOwnMemmem {
78 ScopedDoingMyOwnMemmem();
79 ~ScopedDoingMyOwnMemmem();
80};
81
Kostya Serebryany9a4b10a2016-10-15 04:00:07 +000082inline uint8_t Bswap(uint8_t x) { return x; }
83inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
84inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
85inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
86
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000087} // namespace fuzzer
Marcos Pividori178fe582016-12-13 17:46:11 +000088
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000089#endif // LLVM_FUZZER_DEFS_H