blob: 9f47ecec60978bd2ba8ff5dc27493c4644b8bd1b [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
14#include <cstddef>
15#include <cstdint>
16#include <string>
17#include <vector>
18
19// Platform detection.
20#ifdef __linux__
21#define LIBFUZZER_LINUX 1
22#define LIBFUZZER_APPLE 0
23#elif __APPLE__
24#define LIBFUZZER_LINUX 0
25#define LIBFUZZER_APPLE 1
26#else
27#error "Support for your platform has not been implemented"
28#endif
29
30#ifdef __x86_64
31#define ATTRIBUTE_TARGET_POPCNT __attribute__((target("popcnt")))
32#else
33#define ATTRIBUTE_TARGET_POPCNT
34#endif
35
36namespace fuzzer {
37
38class Random;
39class Dictionary;
40class DictionaryEntry;
41class MutationDispatcher;
42
43typedef std::vector<uint8_t> Unit;
44typedef std::vector<Unit> UnitVector;
45typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
46int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
47
48bool IsFile(const std::string &Path);
49long GetEpoch(const std::string &Path);
50std::string FileToString(const std::string &Path);
51Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
52void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
53 long *Epoch, size_t MaxSize);
54void WriteToFile(const Unit &U, const std::string &Path);
55void CopyFileToErr(const std::string &Path);
56// Returns "Dir/FileName" or equivalent for the current OS.
57std::string DirPlusFile(const std::string &DirPath,
58 const std::string &FileName);
59
60void DupAndCloseStderr();
61void CloseStdout();
62void Printf(const char *Fmt, ...);
63void PrintHexArray(const Unit &U, const char *PrintAfter = "");
64void PrintHexArray(const uint8_t *Data, size_t Size,
65 const char *PrintAfter = "");
66void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
67void PrintASCII(const Unit &U, const char *PrintAfter = "");
68
69void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC);
70std::string Hash(const Unit &U);
71void SetTimer(int Seconds);
72void SetSigSegvHandler();
73void SetSigBusHandler();
74void SetSigAbrtHandler();
75void SetSigIllHandler();
76void SetSigFpeHandler();
77void SetSigIntHandler();
78void SetSigTermHandler();
79std::string Base64(const Unit &U);
80int ExecuteCommand(const std::string &Command);
81size_t GetPeakRSSMb();
82
83// Private copy of SHA1 implementation.
84static const int kSHA1NumBytes = 20;
85// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
86void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
87std::string Sha1ToString(uint8_t Sha1[kSHA1NumBytes]);
88
89// Changes U to contain only ASCII (isprint+isspace) characters.
90// Returns true iff U has been changed.
91bool ToASCII(uint8_t *Data, size_t Size);
92bool IsASCII(const Unit &U);
93bool IsASCII(const uint8_t *Data, size_t Size);
94
95int NumberOfCpuCores();
96int GetPid();
97void SleepSeconds(int Seconds);
98
99} // namespace fuzzer
100#endif // LLVM_FUZZER_DEFS_H