blob: 4c60572ab4c06dcca482a960bbf1b71f5d157802 [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__
23#define LIBFUZZER_LINUX 1
24#define LIBFUZZER_APPLE 0
25#elif __APPLE__
26#define LIBFUZZER_LINUX 0
27#define LIBFUZZER_APPLE 1
28#else
29#error "Support for your platform has not been implemented"
30#endif
31
32#ifdef __x86_64
33#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
34#else
35#define ATTRIBUTE_TARGET_POPCNT
36#endif
37
38namespace fuzzer {
39
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000040template <class T> T Min(T a, T b) { return a < b ? a : b; }
41template <class T> T Max(T a, T b) { return a > b ? a : b; }
42
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000043class Random;
44class Dictionary;
45class DictionaryEntry;
46class MutationDispatcher;
Kostya Serebryany556894f2016-09-21 02:05:39 +000047struct FuzzingOptions;
48class InputCorpus;
Kostya Serebryany29bb6642016-09-21 22:42:17 +000049struct InputInfo;
Kostya Serebryany556894f2016-09-21 02:05:39 +000050struct ExternalFunctions;
51
52// Global interface to functions that may or may not be available.
53extern ExternalFunctions *EF;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000054
55typedef std::vector<uint8_t> Unit;
56typedef std::vector<Unit> UnitVector;
57typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
58int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
59
60bool IsFile(const std::string &Path);
61long GetEpoch(const std::string &Path);
62std::string FileToString(const std::string &Path);
63Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
64void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
65 long *Epoch, size_t MaxSize);
66void WriteToFile(const Unit &U, const std::string &Path);
67void CopyFileToErr(const std::string &Path);
68// Returns "Dir/FileName" or equivalent for the current OS.
69std::string DirPlusFile(const std::string &DirPath,
70 const std::string &FileName);
71
72void DupAndCloseStderr();
73void CloseStdout();
74void Printf(const char *Fmt, ...);
75void PrintHexArray(const Unit &U, const char *PrintAfter = "");
76void PrintHexArray(const uint8_t *Data, size_t Size,
77 const char *PrintAfter = "");
78void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
79void PrintASCII(const Unit &U, const char *PrintAfter = "");
80
81void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
82std::string Hash(const Unit &U);
83void SetTimer(int Seconds);
84void SetSigSegvHandler();
85void SetSigBusHandler();
86void SetSigAbrtHandler();
87void SetSigIllHandler();
88void SetSigFpeHandler();
89void SetSigIntHandler();
90void SetSigTermHandler();
91std::string Base64(const Unit &U);
92int ExecuteCommand(const std::string &Command);
93size_t GetPeakRSSMb();
94
95// Private copy of SHA1 implementation.
96static const int kSHA1NumBytes = 20;
97// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
98void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
Kostya Serebryany29bb6642016-09-21 22:42:17 +000099std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000100
101// Changes U to contain only ASCII (isprint+isspace) characters.
102// Returns true iff U has been changed.
103bool ToASCII(uint8_t *Data, size_t Size);
104bool IsASCII(const Unit &U);
105bool IsASCII(const uint8_t *Data, size_t Size);
106
107int NumberOfCpuCores();
108int GetPid();
109void SleepSeconds(int Seconds);
110
111} // namespace fuzzer
112#endif // LLVM_FUZZER_DEFS_H