| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 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 | |
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 14 | #include <cassert> |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 15 | #include <cstddef> |
| 16 | #include <cstdint> |
| Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 17 | #include <cstring> |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 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 | |
| 38 | namespace fuzzer { |
| 39 | |
| Kostya Serebryany | d28099d | 2016-09-23 00:22:46 +0000 | [diff] [blame] | 40 | template <class T> T Min(T a, T b) { return a < b ? a : b; } |
| 41 | template <class T> T Max(T a, T b) { return a > b ? a : b; } |
| 42 | |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 43 | class Random; |
| 44 | class Dictionary; |
| 45 | class DictionaryEntry; |
| 46 | class MutationDispatcher; |
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 47 | struct FuzzingOptions; |
| 48 | class InputCorpus; |
| Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame] | 49 | struct InputInfo; |
| Kostya Serebryany | 556894f | 2016-09-21 02:05:39 +0000 | [diff] [blame] | 50 | struct ExternalFunctions; |
| 51 | |
| 52 | // Global interface to functions that may or may not be available. |
| 53 | extern ExternalFunctions *EF; |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 54 | |
| 55 | typedef std::vector<uint8_t> Unit; |
| 56 | typedef std::vector<Unit> UnitVector; |
| 57 | typedef int (*UserCallback)(const uint8_t *Data, size_t Size); |
| 58 | int FuzzerDriver(int *argc, char ***argv, UserCallback Callback); |
| 59 | |
| 60 | bool IsFile(const std::string &Path); |
| 61 | long GetEpoch(const std::string &Path); |
| 62 | std::string FileToString(const std::string &Path); |
| Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 63 | Unit FileToVector(const std::string &Path, size_t MaxSize = 0, |
| 64 | bool ExitOnError = true); |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 65 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, |
| Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 66 | long *Epoch, size_t MaxSize, bool ExitOnError); |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 67 | void WriteToFile(const Unit &U, const std::string &Path); |
| 68 | void CopyFileToErr(const std::string &Path); |
| Kostya Serebryany | c5325ed | 2016-10-08 23:24:45 +0000 | [diff] [blame^] | 69 | void DeleteFile(const std::string &Path); |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 70 | // Returns "Dir/FileName" or equivalent for the current OS. |
| 71 | std::string DirPlusFile(const std::string &DirPath, |
| 72 | const std::string &FileName); |
| 73 | |
| 74 | void DupAndCloseStderr(); |
| 75 | void CloseStdout(); |
| 76 | void Printf(const char *Fmt, ...); |
| 77 | void PrintHexArray(const Unit &U, const char *PrintAfter = ""); |
| 78 | void PrintHexArray(const uint8_t *Data, size_t Size, |
| 79 | const char *PrintAfter = ""); |
| 80 | void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = ""); |
| 81 | void PrintASCII(const Unit &U, const char *PrintAfter = ""); |
| 82 | |
| 83 | void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC); |
| Kostya Serebryany | 5ff481f | 2016-09-27 00:10:20 +0000 | [diff] [blame] | 84 | std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC); |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 85 | std::string Hash(const Unit &U); |
| 86 | void SetTimer(int Seconds); |
| 87 | void SetSigSegvHandler(); |
| 88 | void SetSigBusHandler(); |
| 89 | void SetSigAbrtHandler(); |
| 90 | void SetSigIllHandler(); |
| 91 | void SetSigFpeHandler(); |
| 92 | void SetSigIntHandler(); |
| 93 | void SetSigTermHandler(); |
| 94 | std::string Base64(const Unit &U); |
| 95 | int ExecuteCommand(const std::string &Command); |
| 96 | size_t GetPeakRSSMb(); |
| 97 | |
| 98 | // Private copy of SHA1 implementation. |
| 99 | static const int kSHA1NumBytes = 20; |
| 100 | // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'. |
| 101 | void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out); |
| Kostya Serebryany | 29bb664 | 2016-09-21 22:42:17 +0000 | [diff] [blame] | 102 | std::string Sha1ToString(const uint8_t Sha1[kSHA1NumBytes]); |
| Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 103 | |
| 104 | // Changes U to contain only ASCII (isprint+isspace) characters. |
| 105 | // Returns true iff U has been changed. |
| 106 | bool ToASCII(uint8_t *Data, size_t Size); |
| 107 | bool IsASCII(const Unit &U); |
| 108 | bool IsASCII(const uint8_t *Data, size_t Size); |
| 109 | |
| 110 | int NumberOfCpuCores(); |
| 111 | int GetPid(); |
| 112 | void SleepSeconds(int Seconds); |
| 113 | |
| 114 | } // namespace fuzzer |
| 115 | #endif // LLVM_FUZZER_DEFS_H |