blob: b282a83ac98fb88404c1335fa3a3c62eb30f4e47 [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);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000063Unit FileToVector(const std::string &Path, size_t MaxSize = 0,
64 bool ExitOnError = true);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000065void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000066 long *Epoch, size_t MaxSize, bool ExitOnError);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000067void WriteToFile(const Unit &U, const std::string &Path);
68void CopyFileToErr(const std::string &Path);
Kostya Serebryanyc5325ed2016-10-08 23:24:45 +000069void DeleteFile(const std::string &Path);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000070// 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);
Kostya Serebryany5ff481f2016-09-27 00:10:20 +000084std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000085std::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);
96size_t GetPeakRSSMb();
97
98// Private copy of SHA1 implementation.
99static const int kSHA1NumBytes = 20;
100// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
101void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
Kostya Serebryany29bb6642016-09-21 22:42:17 +0000102std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000103
104// Changes U to contain only ASCII (isprint+isspace) characters.
105// Returns true iff U has been changed.
106bool ToASCII(uint8_t *Data, size_t Size);
107bool IsASCII(const Unit &U);
108bool IsASCII(const uint8_t *Data, size_t Size);
109
110int NumberOfCpuCores();
111int GetPid();
112void SleepSeconds(int Seconds);
113
114} // namespace fuzzer
115#endif // LLVM_FUZZER_DEFS_H