blob: 13aa212c96c30a5638b7534c5675406017b994f2 [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//===----------------------------------------------------------------------===//
11#ifndef LLVM_FUZZER_DEFS_H
12#define LLVM_FUZZER_DEFS_H
13
Kostya Serebryany556894f2016-09-21 02:05:39 +000014#include <cassert>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000015#include <cstddef>
16#include <cstdint>
Kostya Serebryany86586182016-09-21 21:17:23 +000017#include <cstring>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000018#include <string>
19#include <vector>
20
21// Platform detection.
22#ifdef __linux__
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000023#define LIBFUZZER_APPLE 0
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000024#define LIBFUZZER_LINUX 1
25#define LIBFUZZER_WINDOWS 0
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000026#elif __APPLE__
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000027#define LIBFUZZER_APPLE 1
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000028#define LIBFUZZER_LINUX 0
29#define LIBFUZZER_WINDOWS 0
30#elif _WIN32
31#define LIBFUZZER_APPLE 0
32#define LIBFUZZER_LINUX 0
33#define LIBFUZZER_WINDOWS 1
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000034#else
35#error "Support for your platform has not been implemented"
36#endif
37
Zachary Turnerc6d8b4c2016-11-30 16:32:54 +000038#define LIBFUZZER_POSIX LIBFUZZER_APPLE || LIBFUZZER_LINUX
39
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000040#ifdef __x86_64
41#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
42#else
43#define ATTRIBUTE_TARGET_POPCNT
44#endif
45
46namespace fuzzer {
47
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000048template <class T> T Min(T a, T b) { return a < b ? a : b; }
49template <class T> T Max(T a, T b) { return a > b ? a : b; }
50
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000051class Random;
52class Dictionary;
53class DictionaryEntry;
54class MutationDispatcher;
Kostya Serebryany556894f2016-09-21 02:05:39 +000055struct FuzzingOptions;
56class InputCorpus;
Kostya Serebryany29bb6642016-09-21 22:42:17 +000057struct InputInfo;
Kostya Serebryany556894f2016-09-21 02:05:39 +000058struct ExternalFunctions;
59
60// Global interface to functions that may or may not be available.
61extern ExternalFunctions *EF;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000062
63typedef std::vector<uint8_t> Unit;
64typedef std::vector<Unit> UnitVector;
65typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
66int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
67
68bool IsFile(const std::string &Path);
69long GetEpoch(const std::string &Path);
70std::string FileToString(const std::string &Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000071Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
72 bool ExitOnError = true);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000073void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000074 long *Epoch, size_t MaxSize, bool ExitOnError);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000075void WriteToFile(const Unit &U, const std::string &Path);
76void CopyFileToErr(const std::string &Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000077void DeleteFile(const std::string &Path);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000078// Returns "Dir/FileName" or equivalent for the current OS.
79std::string DirPlusFile(const std::string &DirPath,
80 const std::string &FileName);
81
82void DupAndCloseStderr();
83void CloseStdout();
84void Printf(const char *Fmt, ...);
85void PrintHexArray(const Unit &U, const char *PrintAfter = "");
86void PrintHexArray(const uint8_t *Data, size_t Size,
87 const char *PrintAfter = "");
88void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
89void PrintASCII(const Unit &U, const char *PrintAfter = "");
90
91void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
Kostya Serebryany5ff481f2016-09-27 00:10:20 +000092std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000093std::string Hash(const Unit &U);
94void SetTimer(int Seconds);
95void SetSigSegvHandler();
96void SetSigBusHandler();
97void SetSigAbrtHandler();
98void SetSigIllHandler();
99void SetSigFpeHandler();
100void SetSigIntHandler();
101void SetSigTermHandler();
102std::string Base64(const Unit &U);
103int ExecuteCommand(const std::string &Command);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000104bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);
105
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000106size_t GetPeakRSSMb();
107
108// Private copy of SHA1 implementation.
109static const int kSHA1NumBytes = 20;
110// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
111void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000112std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000113
114// Changes U to contain only ASCII (isprint+isspace) characters.
115// Returns true iff U has been changed.
116bool ToASCII(uint8_t *Data, size_t Size);
117bool IsASCII(const Unit &U);
118bool IsASCII(const uint8_t *Data, size_t Size);
119
120int NumberOfCpuCores();
121int GetPid();
122void SleepSeconds(int Seconds);
123
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000124
125struct ScopedDoingMyOwnMemmem {
126 ScopedDoingMyOwnMemmem();
127 ~ScopedDoingMyOwnMemmem();
128};
129
Kostya Serebryany9a4b10a2016-10-15 04:00:07 +0000130inline uint8_t Bswap(uint8_t x) { return x; }
131inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
132inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
133inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
134
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000135} // namespace fuzzer
136#endif // LLVM_FUZZER_DEFS_H