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