george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +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 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #ifndef LLVM_FUZZER_INTERNAL_H |
| 13 | #define LLVM_FUZZER_INTERNAL_H |
| 14 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 15 | #include "FuzzerDataFlowTrace.h" |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 16 | #include "FuzzerDefs.h" |
| 17 | #include "FuzzerExtFunctions.h" |
| 18 | #include "FuzzerInterface.h" |
| 19 | #include "FuzzerOptions.h" |
| 20 | #include "FuzzerSHA1.h" |
| 21 | #include "FuzzerValueBitMap.h" |
| 22 | #include <algorithm> |
| 23 | #include <atomic> |
| 24 | #include <chrono> |
| 25 | #include <climits> |
| 26 | #include <cstdlib> |
| 27 | #include <string.h> |
| 28 | |
| 29 | namespace fuzzer { |
| 30 | |
| 31 | using namespace std::chrono; |
| 32 | |
| 33 | class Fuzzer { |
| 34 | public: |
| 35 | |
| 36 | Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD, |
| 37 | FuzzingOptions Options); |
| 38 | ~Fuzzer(); |
kcc | 2e93b3f | 2017-08-29 02:05:01 +0000 | [diff] [blame] | 39 | void Loop(const Vector<std::string> &CorpusDirs); |
| 40 | void ReadAndExecuteSeedCorpora(const Vector<std::string> &CorpusDirs); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 41 | void MinimizeCrashLoop(const Unit &U); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 42 | void RereadOutputCorpus(size_t MaxSize); |
| 43 | |
| 44 | size_t secondsSinceProcessStartUp() { |
| 45 | return duration_cast<seconds>(system_clock::now() - ProcessStartTime) |
| 46 | .count(); |
| 47 | } |
| 48 | |
| 49 | bool TimedOut() { |
| 50 | return Options.MaxTotalTimeSec > 0 && |
| 51 | secondsSinceProcessStartUp() > |
| 52 | static_cast<size_t>(Options.MaxTotalTimeSec); |
| 53 | } |
| 54 | |
| 55 | size_t execPerSec() { |
| 56 | size_t Seconds = secondsSinceProcessStartUp(); |
| 57 | return Seconds ? TotalNumberOfRuns / Seconds : 0; |
| 58 | } |
| 59 | |
| 60 | size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; } |
| 61 | |
| 62 | static void StaticAlarmCallback(); |
| 63 | static void StaticCrashSignalCallback(); |
| 64 | static void StaticExitCallback(); |
| 65 | static void StaticInterruptCallback(); |
| 66 | static void StaticFileSizeExceedCallback(); |
kcc | 1239a99 | 2017-11-09 20:30:19 +0000 | [diff] [blame] | 67 | static void StaticGracefulExitCallback(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 68 | |
| 69 | void ExecuteCallback(const uint8_t *Data, size_t Size); |
| 70 | bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false, |
kcc | b6836be | 2017-12-01 19:18:38 +0000 | [diff] [blame] | 71 | InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 72 | |
| 73 | // Merge Corpora[1:] into Corpora[0]. |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 74 | void Merge(const Vector<std::string> &Corpora); |
| 75 | void CrashResistantMerge(const Vector<std::string> &Args, |
| 76 | const Vector<std::string> &Corpora, |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 77 | const char *CoverageSummaryInputPathOrNull, |
kcc | c51afd7 | 2017-11-09 01:05:29 +0000 | [diff] [blame] | 78 | const char *CoverageSummaryOutputPathOrNull, |
| 79 | const char *MergeControlFilePathOrNull); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 80 | void CrashResistantMergeInternalStep(const std::string &ControlFilePath); |
| 81 | MutationDispatcher &GetMD() { return MD; } |
| 82 | void PrintFinalStats(); |
| 83 | void SetMaxInputLen(size_t MaxInputLen); |
| 84 | void SetMaxMutationLen(size_t MaxMutationLen); |
| 85 | void RssLimitCallback(); |
| 86 | |
| 87 | bool InFuzzingThread() const { return IsMyThread; } |
| 88 | size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const; |
| 89 | void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size, |
| 90 | bool DuringInitialCorpusExecution); |
| 91 | |
| 92 | void HandleMalloc(size_t Size); |
| 93 | void AnnounceOutput(const uint8_t *Data, size_t Size); |
| 94 | |
| 95 | private: |
| 96 | void AlarmCallback(); |
| 97 | void CrashCallback(); |
| 98 | void ExitCallback(); |
kcc | 1239a99 | 2017-11-09 20:30:19 +0000 | [diff] [blame] | 99 | void MaybeExitGracefully(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 100 | void CrashOnOverwrittenData(); |
| 101 | void InterruptCallback(); |
| 102 | void MutateAndTestOne(); |
alekseyshl | d995b55 | 2017-10-23 22:04:30 +0000 | [diff] [blame] | 103 | void PurgeAllocator(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 104 | void ReportNewCoverage(InputInfo *II, const Unit &U); |
| 105 | void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size); |
| 106 | void WriteToOutputCorpus(const Unit &U); |
| 107 | void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix); |
| 108 | void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0); |
| 109 | void PrintStatusForNewUnit(const Unit &U, const char *Text); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 110 | void CheckExitOnSrcPosOrItem(); |
| 111 | |
| 112 | static void StaticDeathCallback(); |
| 113 | void DumpCurrentUnit(const char *Prefix); |
| 114 | void DeathCallback(); |
| 115 | |
| 116 | void AllocateCurrentUnitData(); |
| 117 | uint8_t *CurrentUnitData = nullptr; |
| 118 | std::atomic<size_t> CurrentUnitSize; |
| 119 | uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit. |
| 120 | bool RunningCB = false; |
| 121 | |
kcc | 1239a99 | 2017-11-09 20:30:19 +0000 | [diff] [blame] | 122 | bool GracefulExitRequested = false; |
| 123 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 124 | size_t TotalNumberOfRuns = 0; |
| 125 | size_t NumberOfNewUnitsAdded = 0; |
| 126 | |
| 127 | size_t LastCorpusUpdateRun = 0; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 128 | |
| 129 | bool HasMoreMallocsThanFrees = false; |
| 130 | size_t NumberOfLeakDetectionAttempts = 0; |
| 131 | |
alekseyshl | d995b55 | 2017-10-23 22:04:30 +0000 | [diff] [blame] | 132 | system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now(); |
| 133 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 134 | UserCallback CB; |
| 135 | InputCorpus &Corpus; |
| 136 | MutationDispatcher &MD; |
| 137 | FuzzingOptions Options; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 138 | DataFlowTrace DFT; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 139 | |
| 140 | system_clock::time_point ProcessStartTime = system_clock::now(); |
| 141 | system_clock::time_point UnitStartTime, UnitStopTime; |
| 142 | long TimeOfLongestUnitInSeconds = 0; |
| 143 | long EpochOfLastReadOfOutputCorpus = 0; |
| 144 | |
| 145 | size_t MaxInputLen = 0; |
| 146 | size_t MaxMutationLen = 0; |
| 147 | size_t TmpMaxMutationLen = 0; |
| 148 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 149 | Vector<uint32_t> UniqFeatureSetTmp; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 150 | |
| 151 | // Need to know our own thread. |
| 152 | static thread_local bool IsMyThread; |
| 153 | }; |
| 154 | |
| 155 | } // namespace fuzzer |
| 156 | |
| 157 | #endif // LLVM_FUZZER_INTERNAL_H |