Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 1 | //===- FuzzerInternal.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 | // Define the main class fuzzer::Fuzzer and most functions. |
| 10 | //===----------------------------------------------------------------------===// |
Yaron Keren | 347663b | 2015-08-10 16:37:40 +0000 | [diff] [blame] | 11 | |
| 12 | #ifndef LLVM_FUZZER_INTERNAL_H |
| 13 | #define LLVM_FUZZER_INTERNAL_H |
| 14 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 15 | #include <cassert> |
| 16 | #include <chrono> |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 17 | #include <climits> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 18 | #include <cstddef> |
| 19 | #include <cstdlib> |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 20 | #include <random> |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 21 | #include <string.h> |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 22 | #include <string> |
Kostya Serebryany | 2c1b33b | 2015-01-29 23:01:07 +0000 | [diff] [blame] | 23 | #include <unordered_set> |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 24 | #include <vector> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 25 | |
Kostya Serebryany | 016852c | 2015-02-19 18:45:37 +0000 | [diff] [blame] | 26 | #include "FuzzerInterface.h" |
| 27 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 28 | namespace fuzzer { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 29 | using namespace std::chrono; |
Kostya Serebryany | aca7696 | 2016-01-16 01:23:12 +0000 | [diff] [blame] | 30 | typedef std::vector<uint8_t> Unit; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 31 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 32 | // A simple POD sized array of bytes. |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 33 | template <size_t kMaxSize> class FixedWord { |
| 34 | public: |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 35 | FixedWord() {} |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 36 | FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); } |
| 37 | |
| 38 | void Set(const uint8_t *B, uint8_t S) { |
| 39 | assert(S <= kMaxSize); |
| 40 | memcpy(Data, B, S); |
| 41 | Size = S; |
| 42 | } |
| 43 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 44 | bool operator==(const FixedWord<kMaxSize> &w) const { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 45 | return Size == w.Size && 0 == memcmp(Data, w.Data, Size); |
| 46 | } |
| 47 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 48 | bool operator<(const FixedWord<kMaxSize> &w) const { |
| 49 | if (Size != w.Size) |
| 50 | return Size < w.Size; |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 51 | return memcmp(Data, w.Data, Size) < 0; |
| 52 | } |
| 53 | |
| 54 | static size_t GetMaxSize() { return kMaxSize; } |
| 55 | const uint8_t *data() const { return Data; } |
| 56 | uint8_t size() const { return Size; } |
| 57 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 58 | private: |
Kostya Serebryany | 160dcba | 2016-01-22 23:55:14 +0000 | [diff] [blame] | 59 | uint8_t Size = 0; |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 60 | uint8_t Data[kMaxSize]; |
| 61 | }; |
| 62 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 63 | typedef FixedWord<27> Word; // 28 bytes. |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 64 | |
Kostya Serebryany | bfbe7fc | 2016-02-02 03:03:47 +0000 | [diff] [blame] | 65 | bool IsFile(const std::string &Path); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 66 | std::string FileToString(const std::string &Path); |
| 67 | Unit FileToVector(const std::string &Path); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 68 | void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V, |
| 69 | long *Epoch); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 70 | void WriteToFile(const Unit &U, const std::string &Path); |
Kostya Serebryany | 5b266a8 | 2015-02-04 19:10:20 +0000 | [diff] [blame] | 71 | void CopyFileToErr(const std::string &Path); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 72 | // Returns "Dir/FileName" or equivalent for the current OS. |
| 73 | std::string DirPlusFile(const std::string &DirPath, |
| 74 | const std::string &FileName); |
| 75 | |
Kostya Serebryany | 20e9bcb | 2015-05-23 01:07:46 +0000 | [diff] [blame] | 76 | void Printf(const char *Fmt, ...); |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 77 | void PrintHexArray(const Unit &U, const char *PrintAfter = ""); |
| 78 | void PrintHexArray(const uint8_t *Data, size_t Size, |
| 79 | const char *PrintAfter = ""); |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 80 | void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = ""); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 81 | void PrintASCII(const Unit &U, const char *PrintAfter = ""); |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 82 | void PrintASCII(const Word &W, const char *PrintAfter = ""); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 83 | std::string Hash(const Unit &U); |
| 84 | void SetTimer(int Seconds); |
Kostya Serebryany | 9e48cda | 2015-12-04 22:29:39 +0000 | [diff] [blame] | 85 | std::string Base64(const Unit &U); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 86 | int ExecuteCommand(const std::string &Command); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 87 | |
Kostya Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 88 | // Private copy of SHA1 implementation. |
| 89 | static const int kSHA1NumBytes = 20; |
| 90 | // Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'. |
| 91 | void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out); |
| 92 | |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 93 | // Changes U to contain only ASCII (isprint+isspace) characters. |
| 94 | // Returns true iff U has been changed. |
| 95 | bool ToASCII(Unit &U); |
Kostya Serebryany | a9346c2 | 2015-09-02 19:08:08 +0000 | [diff] [blame] | 96 | bool IsASCII(const Unit &U); |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 97 | |
Kostya Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 98 | int NumberOfCpuCores(); |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 99 | int GetPid(); |
Kostya Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 100 | |
Kostya Serebryany | ecab57b | 2016-02-13 02:39:30 +0000 | [diff] [blame^] | 101 | class FuzzerRandomBase { |
| 102 | public: |
| 103 | FuzzerRandomBase(){} |
| 104 | virtual ~FuzzerRandomBase(){}; |
| 105 | virtual void ResetSeed(unsigned int seed) = 0; |
| 106 | // Return a random number. |
| 107 | virtual size_t Rand() = 0; |
| 108 | // Return a random number in range [0,n). |
| 109 | size_t operator()(size_t n) { return n ? Rand() % n : 0; } |
| 110 | bool RandBool() { return Rand() % 2; } |
| 111 | }; |
| 112 | |
| 113 | // Using libc's stand/rand. |
| 114 | class FuzzerRandomLibc : public FuzzerRandomBase { |
| 115 | public: |
| 116 | FuzzerRandomLibc(unsigned int seed) { ResetSeed(seed); } |
| 117 | void ResetSeed(unsigned int seed) override; |
| 118 | ~FuzzerRandomLibc() override {}; |
| 119 | size_t Rand() override; |
| 120 | }; |
| 121 | |
| 122 | // Using std::mt19937 |
| 123 | class FuzzerRandom_mt19937 : public FuzzerRandomBase { |
| 124 | public: |
| 125 | FuzzerRandom_mt19937(unsigned int seed) { ResetSeed(seed); } |
| 126 | void ResetSeed(unsigned int seed) override; |
| 127 | ~FuzzerRandom_mt19937() override; |
| 128 | size_t Rand() override; |
| 129 | private: |
| 130 | struct Impl; |
| 131 | Impl *R = nullptr; |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame] | 136 | // Dictionary. |
| 137 | |
| 138 | // Parses one dictionary entry. |
| 139 | // If successfull, write the enty to Unit and returns true, |
| 140 | // otherwise returns false. |
| 141 | bool ParseOneDictionaryEntry(const std::string &Str, Unit *U); |
| 142 | // Parses the dictionary file, fills Units, returns true iff all lines |
| 143 | // were parsed succesfully. |
| 144 | bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units); |
| 145 | |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 146 | class MutationDispatcher { |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 147 | public: |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 148 | MutationDispatcher(FuzzerRandomBase &Rand); |
| 149 | ~MutationDispatcher(); |
| 150 | /// Indicate that we are about to start a new sequence of mutations. |
| 151 | void StartMutationSequence(); |
| 152 | /// Print the current sequence of mutations. |
| 153 | void PrintMutationSequence(); |
| 154 | /// Indicate that the current sequence of mutations was successfull. |
| 155 | void RecordSuccessfulMutationSequence(); |
| 156 | /// Mutates data by shuffling bytes. |
| 157 | size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize); |
| 158 | /// Mutates data by erasing a byte. |
| 159 | size_t Mutate_EraseByte(uint8_t *Data, size_t Size, size_t MaxSize); |
| 160 | /// Mutates data by inserting a byte. |
| 161 | size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize); |
| 162 | /// Mutates data by chanding one byte. |
| 163 | size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize); |
| 164 | /// Mutates data by chanding one bit. |
| 165 | size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize); |
| 166 | |
| 167 | /// Mutates data by adding a word from the manual dictionary. |
| 168 | size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size, |
| 169 | size_t MaxSize); |
| 170 | |
| 171 | /// Mutates data by adding a word from the temporary automatic dictionary. |
| 172 | size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size, |
| 173 | size_t MaxSize); |
| 174 | |
| 175 | /// Mutates data by adding a word from the persistent automatic dictionary. |
| 176 | size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size, |
| 177 | size_t MaxSize); |
| 178 | |
| 179 | /// Tries to find an ASCII integer in Data, changes it to another ASCII int. |
| 180 | size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize); |
| 181 | |
| 182 | /// CrossOver Data with some other element of the corpus. |
| 183 | size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize); |
| 184 | |
| 185 | /// Applies one of the above mutations. |
| 186 | /// Returns the new size of data which could be up to MaxSize. |
| 187 | size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize); |
| 188 | |
| 189 | /// Creates a cross-over of two pieces of Data, returns its size. |
| 190 | size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2, |
| 191 | size_t Size2, uint8_t *Out, size_t MaxOutSize); |
| 192 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 193 | void AddWordToManualDictionary(const Word &W); |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 194 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 195 | void AddWordToAutoDictionary(const Word &W, size_t PositionHint); |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 196 | void ClearAutoDictionary(); |
| 197 | void PrintRecommendedDictionary(); |
| 198 | |
| 199 | void SetCorpus(const std::vector<Unit> *Corpus); |
| 200 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 201 | private: |
Kostya Serebryany | 628bc3e | 2016-01-16 00:04:36 +0000 | [diff] [blame] | 202 | FuzzerRandomBase &Rand; |
| 203 | struct Impl; |
| 204 | Impl *MDImpl; |
| 205 | }; |
| 206 | |
Kostya Serebryany | ecab57b | 2016-02-13 02:39:30 +0000 | [diff] [blame^] | 207 | class UserSuppliedFuzzer { |
| 208 | public: |
| 209 | UserSuppliedFuzzer(FuzzerRandomBase *Rand); |
| 210 | /// Executes the target function on 'Size' bytes of 'Data'. |
| 211 | virtual int TargetFunction(const uint8_t *Data, size_t Size) = 0; |
| 212 | /// Mutates 'Size' bytes of data in 'Data' inplace into up to 'MaxSize' bytes, |
| 213 | /// returns the new size of the data, which should be positive. |
| 214 | virtual size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize); |
| 215 | /// Crosses 'Data1' and 'Data2', writes up to 'MaxOutSize' bytes into Out, |
| 216 | /// returns the number of bytes written, which should be positive. |
| 217 | virtual size_t CrossOver(const uint8_t *Data1, size_t Size1, |
| 218 | const uint8_t *Data2, size_t Size2, |
| 219 | uint8_t *Out, size_t MaxOutSize); |
| 220 | virtual ~UserSuppliedFuzzer(); |
| 221 | |
| 222 | FuzzerRandomBase &GetRand() { return *Rand; } |
| 223 | |
| 224 | MutationDispatcher &GetMD() { return *MD; } |
| 225 | |
| 226 | private: |
| 227 | bool OwnRand = false; |
| 228 | FuzzerRandomBase *Rand; |
| 229 | MutationDispatcher *MD; |
| 230 | }; |
| 231 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 232 | class Fuzzer { |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 233 | public: |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 234 | struct FuzzingOptions { |
| 235 | int Verbosity = 1; |
| 236 | int MaxLen = 0; |
Kostya Serebryany | 490bbd6 | 2015-05-19 22:12:57 +0000 | [diff] [blame] | 237 | int UnitTimeoutSec = 300; |
Kostya Serebryany | 9768e7f | 2016-01-23 19:34:19 +0000 | [diff] [blame] | 238 | bool AbortOnTimeout = false; |
Kostya Serebryany | 54a6363 | 2016-01-29 23:30:07 +0000 | [diff] [blame] | 239 | int TimeoutExitCode = 77; |
Kostya Serebryany | b85db17 | 2015-10-02 20:47:55 +0000 | [diff] [blame] | 240 | int MaxTotalTimeSec = 0; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 241 | bool DoCrossOver = true; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 242 | int MutateDepth = 5; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 243 | bool ExitOnFirst = false; |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 244 | bool UseCounters = false; |
Kostya Serebryany | 2e9fca9 | 2015-10-22 23:55:39 +0000 | [diff] [blame] | 245 | bool UseIndirCalls = true; |
Kostya Serebryany | 5a99ecb | 2015-05-11 20:51:19 +0000 | [diff] [blame] | 246 | bool UseTraces = false; |
Kostya Serebryany | ae5b956 | 2016-01-15 06:24:05 +0000 | [diff] [blame] | 247 | bool UseMemcmp = true; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 248 | bool UseFullCoverageSet = false; |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 249 | bool Reload = true; |
Kostya Serebryany | fed509e | 2015-10-17 04:38:26 +0000 | [diff] [blame] | 250 | bool ShuffleAtStartUp = true; |
Kostya Serebryany | 92e0476 | 2015-02-04 23:42:42 +0000 | [diff] [blame] | 251 | int PreferSmallDuringInitialShuffle = -1; |
Kostya Serebryany | 33f8669 | 2015-02-04 22:20:09 +0000 | [diff] [blame] | 252 | size_t MaxNumberOfRuns = ULONG_MAX; |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 253 | int SyncTimeout = 600; |
Kostya Serebryany | 70926ae | 2015-08-05 21:43:48 +0000 | [diff] [blame] | 254 | int ReportSlowUnits = 10; |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 255 | bool OnlyASCII = false; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 256 | std::string OutputCorpus; |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 257 | std::string SyncCommand; |
Kostya Serebryany | bd5d1cd | 2015-10-09 03:57:59 +0000 | [diff] [blame] | 258 | std::string ArtifactPrefix = "./"; |
Kostya Serebryany | 2d0ef14 | 2015-11-25 21:40:46 +0000 | [diff] [blame] | 259 | std::string ExactArtifactPath; |
Kostya Serebryany | b91c62b | 2015-10-16 22:41:47 +0000 | [diff] [blame] | 260 | bool SaveArtifacts = true; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 261 | bool PrintNEW = true; // Print a status line when new units are found; |
Mike Aizatsky | a9c2387 | 2015-11-12 04:38:40 +0000 | [diff] [blame] | 262 | bool OutputCSV = false; |
Mike Aizatsky | 8b11f87 | 2016-01-06 00:21:22 +0000 | [diff] [blame] | 263 | bool PrintNewCovPcs = false; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 264 | }; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 265 | Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 266 | void AddToCorpus(const Unit &U) { |
| 267 | Corpus.push_back(U); |
| 268 | UpdateCorpusDistribution(); |
| 269 | } |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 270 | size_t ChooseUnitIdxToMutate(); |
| 271 | const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; }; |
Kostya Serebryany | 468ed78 | 2015-09-08 17:30:35 +0000 | [diff] [blame] | 272 | void Loop(); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 273 | void Drill(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 274 | void ShuffleAndMinimize(); |
Kostya Serebryany | 2252625 | 2015-05-11 21:16:27 +0000 | [diff] [blame] | 275 | void InitializeTraceState(); |
Kostya Serebryany | d50a3ee | 2016-01-13 23:02:30 +0000 | [diff] [blame] | 276 | void AssignTaintLabels(uint8_t *Data, size_t Size); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 277 | size_t CorpusSize() const { return Corpus.size(); } |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 278 | void ReadDir(const std::string &Path, long *Epoch) { |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 279 | Printf("Loading corpus: %s\n", Path.c_str()); |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 280 | ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 281 | } |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 282 | void RereadOutputCorpus(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 283 | // Save the current corpus to OutputCorpus. |
| 284 | void SaveCorpus(); |
| 285 | |
Kostya Serebryany | 92e0476 | 2015-02-04 23:42:42 +0000 | [diff] [blame] | 286 | size_t secondsSinceProcessStartUp() { |
| 287 | return duration_cast<seconds>(system_clock::now() - ProcessStartTime) |
| 288 | .count(); |
| 289 | } |
| 290 | |
| 291 | size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; } |
| 292 | |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 293 | static void StaticAlarmCallback(); |
| 294 | |
Ivan Krasin | 95e82d5 | 2015-10-01 23:23:06 +0000 | [diff] [blame] | 295 | void ExecuteCallback(const Unit &U); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 296 | |
Kostya Serebryany | 9cc3b0d | 2015-10-24 01:16:40 +0000 | [diff] [blame] | 297 | // Merge Corpora[1:] into Corpora[0]. |
| 298 | void Merge(const std::vector<std::string> &Corpora); |
| 299 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 300 | private: |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 301 | void AlarmCallback(); |
Kostya Serebryany | 27ab2d7 | 2015-12-19 02:49:09 +0000 | [diff] [blame] | 302 | void MutateAndTestOne(); |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 303 | void ReportNewCoverage(const Unit &U); |
| 304 | bool RunOne(const Unit &U); |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 305 | void RunOneAndUpdateCorpus(Unit &U); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 306 | void WriteToOutputCorpus(const Unit &U); |
Kostya Serebryany | 2b7d2e9 | 2015-07-23 18:37:22 +0000 | [diff] [blame] | 307 | void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix); |
Kostya Serebryany | 09d2a5f | 2015-10-22 22:56:45 +0000 | [diff] [blame] | 308 | void PrintStats(const char *Where, const char *End = "\n"); |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 309 | void PrintStatusForNewUnit(const Unit &U); |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 310 | // Updates the probability distribution for the units in the corpus. |
| 311 | // Must be called whenever the corpus or unit weights are changed. |
| 312 | void UpdateCorpusDistribution(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 313 | |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 314 | void SyncCorpus(); |
| 315 | |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 316 | size_t RecordBlockCoverage(); |
Kostya Serebryany | 2e9fca9 | 2015-10-22 23:55:39 +0000 | [diff] [blame] | 317 | size_t RecordCallerCalleeCoverage(); |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 318 | void PrepareCoverageBeforeRun(); |
| 319 | bool CheckCoverageAfterRun(); |
| 320 | |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 321 | // Trace-based fuzzing: we run a unit with some kind of tracing |
| 322 | // enabled and record potentially useful mutations. Then |
| 323 | // We apply these mutations one by one to the unit and run it again. |
| 324 | |
| 325 | // Start tracing; forget all previously proposed mutations. |
| 326 | void StartTraceRecording(); |
Kostya Serebryany | b65805a | 2016-01-09 03:08:58 +0000 | [diff] [blame] | 327 | // Stop tracing. |
| 328 | void StopTraceRecording(); |
Kostya Serebryany | beb24c3 | 2015-05-07 21:02:11 +0000 | [diff] [blame] | 329 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 330 | void SetDeathCallback(); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 331 | static void StaticDeathCallback(); |
| 332 | void DeathCallback(); |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 333 | |
| 334 | uint8_t *CurrentUnitData; |
| 335 | size_t CurrentUnitSize; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 336 | |
| 337 | size_t TotalNumberOfRuns = 0; |
Kostya Serebryany | 12c7837 | 2015-08-12 01:55:37 +0000 | [diff] [blame] | 338 | size_t TotalNumberOfExecutedTraceBasedMutations = 0; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 339 | |
| 340 | std::vector<Unit> Corpus; |
Kostya Serebryany | cbb2334 | 2015-05-19 01:06:07 +0000 | [diff] [blame] | 341 | std::unordered_set<std::string> UnitHashesAddedToCorpus; |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 342 | |
| 343 | // For UseCounters |
| 344 | std::vector<uint8_t> CounterBitmap; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 345 | size_t TotalBits() { // Slow. Call it only for printing stats. |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 346 | size_t Res = 0; |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 347 | for (auto x : CounterBitmap) |
| 348 | Res += __builtin_popcount(x); |
Kostya Serebryany | be5e0ed | 2015-03-03 23:27:02 +0000 | [diff] [blame] | 349 | return Res; |
| 350 | } |
| 351 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 352 | // TODO(krasin): remove GetRand from UserSuppliedFuzzer, |
| 353 | // and fully rely on the generator and the seed. |
| 354 | // The user supplied fuzzer will have a way to access the |
| 355 | // generator for its own purposes (like seeding the custom |
| 356 | // PRNG). |
| 357 | std::mt19937 Generator; |
| 358 | std::piecewise_constant_distribution<double> CorpusDistribution; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 359 | UserSuppliedFuzzer &USF; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 360 | FuzzingOptions Options; |
| 361 | system_clock::time_point ProcessStartTime = system_clock::now(); |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 362 | system_clock::time_point LastExternalSync = system_clock::now(); |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 363 | system_clock::time_point UnitStartTime; |
Kostya Serebryany | 16901a9 | 2015-03-30 23:04:35 +0000 | [diff] [blame] | 364 | long TimeOfLongestUnitInSeconds = 0; |
Kostya Serebryany | 1ac8055 | 2015-05-08 21:30:55 +0000 | [diff] [blame] | 365 | long EpochOfLastReadOfOutputCorpus = 0; |
Kostya Serebryany | 007c9b2 | 2015-10-22 22:50:47 +0000 | [diff] [blame] | 366 | size_t LastRecordedBlockCoverage = 0; |
Kostya Serebryany | 2e9fca9 | 2015-10-22 23:55:39 +0000 | [diff] [blame] | 367 | size_t LastRecordedCallerCalleeCoverage = 0; |
Mike Aizatsky | 8b11f87 | 2016-01-06 00:21:22 +0000 | [diff] [blame] | 368 | size_t LastCoveragePcBufferLen = 0; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 371 | class SimpleUserSuppliedFuzzer : public UserSuppliedFuzzer { |
| 372 | public: |
Kostya Serebryany | 404c69f | 2015-07-24 01:06:40 +0000 | [diff] [blame] | 373 | SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback) |
| 374 | : UserSuppliedFuzzer(Rand), Callback(Callback) {} |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 375 | |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 376 | virtual int TargetFunction(const uint8_t *Data, size_t Size) override { |
Kostya Serebryany | 94660b3 | 2015-10-23 18:37:58 +0000 | [diff] [blame] | 377 | return Callback(Data, Size); |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 380 | private: |
Kostya Serebryany | 20bb5e7 | 2015-10-02 23:34:06 +0000 | [diff] [blame] | 381 | UserCallback Callback = nullptr; |
Kostya Serebryany | f342459 | 2015-05-22 22:35:31 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
Ivan Krasin | df91910 | 2016-01-22 22:28:27 +0000 | [diff] [blame] | 384 | }; // namespace fuzzer |
Yaron Keren | 347663b | 2015-08-10 16:37:40 +0000 | [diff] [blame] | 385 | |
| 386 | #endif // LLVM_FUZZER_INTERNAL_H |