1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // Define the main class fuzzer::Fuzzer and most functions. 9 //===----------------------------------------------------------------------===// 10 11 #ifndef LLVM_FUZZER_INTERNAL_H 12 #define LLVM_FUZZER_INTERNAL_H 13 14 #include "FuzzerDataFlowTrace.h" 15 #include "FuzzerDefs.h" 16 #include "FuzzerExtFunctions.h" 17 #include "FuzzerInterface.h" 18 #include "FuzzerOptions.h" 19 #include "FuzzerSHA1.h" 20 #include "FuzzerValueBitMap.h" 21 #include <algorithm> 22 #include <atomic> 23 #include <chrono> 24 #include <climits> 25 #include <cstdlib> 26 #include <string.h> 27 28 namespace fuzzer { 29 30 using namespace std::chrono; 31 32 class Fuzzer { 33 public: 34 35 Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD, 36 FuzzingOptions Options); 37 ~Fuzzer(); 38 void Loop(std::vector<SizedFile> &CorporaFiles); 39 void ReadAndExecuteSeedCorpora(std::vector<SizedFile> &CorporaFiles); 40 void MinimizeCrashLoop(const Unit &U); 41 void RereadOutputCorpus(size_t MaxSize); 42 43 size_t secondsSinceProcessStartUp() { 44 return duration_cast<seconds>(system_clock::now() - ProcessStartTime) 45 .count(); 46 } 47 48 bool TimedOut() { 49 return Options.MaxTotalTimeSec > 0 && 50 secondsSinceProcessStartUp() > 51 static_cast<size_t>(Options.MaxTotalTimeSec); 52 } 53 54 size_t execPerSec() { 55 size_t Seconds = secondsSinceProcessStartUp(); 56 return Seconds ? TotalNumberOfRuns / Seconds : 0; 57 } 58 59 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; } 60 61 static void StaticAlarmCallback(); 62 static void StaticCrashSignalCallback(); 63 static void StaticExitCallback(); 64 static void StaticInterruptCallback(); 65 static void StaticFileSizeExceedCallback(); 66 static void StaticGracefulExitCallback(); 67 68 // Executes the target callback on {Data, Size} once. 69 // Returns false if the input was rejected by the target (target returned -1), 70 // and true otherwise. 71 bool ExecuteCallback(const uint8_t *Data, size_t Size); 72 bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false, 73 InputInfo *II = nullptr, bool ForceAddToCorpus = false, 74 bool *FoundUniqFeatures = nullptr); 75 void TPCUpdateObservedPCs(); 76 77 // Merge Corpora[1:] into Corpora[0]. 78 void Merge(const std::vector<std::string> &Corpora); 79 void CrashResistantMergeInternalStep(const std::string &ControlFilePath, 80 bool IsSetCoverMerge); 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 static void MaybeExitGracefully(); 94 static int InterruptExitCode(); 95 std::string WriteToOutputCorpus(const Unit &U); 96 97 private: 98 void AlarmCallback(); 99 void CrashCallback(); 100 void ExitCallback(); 101 void CrashOnOverwrittenData(); 102 void InterruptCallback(); 103 void MutateAndTestOne(); 104 void PurgeAllocator(); 105 void ReportNewCoverage(InputInfo *II, const Unit &U); 106 void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size); 107 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix); 108 void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0, 109 size_t Features = 0); 110 void PrintStatusForNewUnit(const Unit &U, const char *Text); 111 void CheckExitOnSrcPosOrItem(); 112 113 static void StaticDeathCallback(); 114 void DumpCurrentUnit(const char *Prefix); 115 void DeathCallback(); 116 117 void AllocateCurrentUnitData(); 118 uint8_t *CurrentUnitData = nullptr; 119 std::atomic<size_t> CurrentUnitSize; 120 uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit. 121 122 bool GracefulExitRequested = false; 123 124 size_t TotalNumberOfRuns = 0; 125 size_t NumberOfNewUnitsAdded = 0; 126 127 size_t LastCorpusUpdateRun = 0; 128 129 bool HasMoreMallocsThanFrees = false; 130 size_t NumberOfLeakDetectionAttempts = 0; 131 132 system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now(); 133 134 UserCallback CB; 135 InputCorpus &Corpus; 136 MutationDispatcher &MD; 137 FuzzingOptions Options; 138 DataFlowTrace DFT; 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 149 std::vector<uint32_t> UniqFeatureSetTmp; 150 151 // Need to know our own thread. 152 static thread_local bool IsMyThread; 153 }; 154 155 struct ScopedEnableMsanInterceptorChecks { 156 ScopedEnableMsanInterceptorChecks() { 157 if (EF->__msan_scoped_enable_interceptor_checks) 158 EF->__msan_scoped_enable_interceptor_checks(); 159 } 160 ~ScopedEnableMsanInterceptorChecks() { 161 if (EF->__msan_scoped_disable_interceptor_checks) 162 EF->__msan_scoped_disable_interceptor_checks(); 163 } 164 }; 165 166 struct ScopedDisableMsanInterceptorChecks { 167 ScopedDisableMsanInterceptorChecks() { 168 if (EF->__msan_scoped_disable_interceptor_checks) 169 EF->__msan_scoped_disable_interceptor_checks(); 170 } 171 ~ScopedDisableMsanInterceptorChecks() { 172 if (EF->__msan_scoped_enable_interceptor_checks) 173 EF->__msan_scoped_enable_interceptor_checks(); 174 } 175 }; 176 177 } // namespace fuzzer 178 179 #endif // LLVM_FUZZER_INTERNAL_H 180