1//===- 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
14#include <cassert>
15#include <cstddef>
16#include <cstdint>
17#include <cstring>
18#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
40template <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
43class Random;
44class Dictionary;
45class DictionaryEntry;
46class MutationDispatcher;
47struct FuzzingOptions;
48class InputCorpus;
49struct InputInfo;
50struct ExternalFunctions;
51
52// Global interface to functions that may or may not be available.
53extern ExternalFunctions *EF;
54
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,
64                  bool ExitOnError = true);
65void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
66                            long *Epoch, size_t MaxSize, bool ExitOnError);
67void WriteToFile(const Unit &U, const std::string &Path);
68void CopyFileToErr(const std::string &Path);
69void DeleteFile(const std::string &Path);
70// Returns "Dir/FileName" or equivalent for the current OS.
71std::string DirPlusFile(const std::string &DirPath,
72                        const std::string &FileName);
73
74void DupAndCloseStderr();
75void CloseStdout();
76void Printf(const char *Fmt, ...);
77void PrintHexArray(const Unit &U, const char *PrintAfter = "");
78void PrintHexArray(const uint8_t *Data, size_t Size,
79                   const char *PrintAfter = "");
80void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
81void PrintASCII(const Unit &U, const char *PrintAfter = "");
82
83void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
84std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
85std::string Hash(const Unit &U);
86void SetTimer(int Seconds);
87void SetSigSegvHandler();
88void SetSigBusHandler();
89void SetSigAbrtHandler();
90void SetSigIllHandler();
91void SetSigFpeHandler();
92void SetSigIntHandler();
93void SetSigTermHandler();
94std::string Base64(const Unit &U);
95int ExecuteCommand(const std::string &Command);
96bool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out);
97
98size_t GetPeakRSSMb();
99
100// Private copy of SHA1 implementation.
101static const int kSHA1NumBytes = 20;
102// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
103void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
104std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
105
106// Changes U to contain only ASCII (isprint+isspace) characters.
107// Returns true iff U has been changed.
108bool ToASCII(uint8_t *Data, size_t Size);
109bool IsASCII(const Unit &U);
110bool IsASCII(const uint8_t *Data, size_t Size);
111
112int NumberOfCpuCores();
113int GetPid();
114void SleepSeconds(int Seconds);
115
116
117struct ScopedDoingMyOwnMemmem {
118  ScopedDoingMyOwnMemmem();
119  ~ScopedDoingMyOwnMemmem();
120};
121
122inline uint8_t  Bswap(uint8_t x)  { return x; }
123inline uint16_t Bswap(uint16_t x) { return __builtin_bswap16(x); }
124inline uint32_t Bswap(uint32_t x) { return __builtin_bswap32(x); }
125inline uint64_t Bswap(uint64_t x) { return __builtin_bswap64(x); }
126
127}  // namespace fuzzer
128#endif  // LLVM_FUZZER_DEFS_H
129