1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Define the main class fuzzer::Fuzzer and most functions.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_FUZZER_INTERNAL_H
13 #define LLVM_FUZZER_INTERNAL_H
15 #include "FuzzerDataFlowTrace.h"
16 #include "FuzzerDefs.h"
17 #include "FuzzerExtFunctions.h"
18 #include "FuzzerInterface.h"
19 #include "FuzzerOptions.h"
20 #include "FuzzerSHA1.h"
21 #include "FuzzerValueBitMap.h"
31 using namespace std::chrono;
36 Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
37 FuzzingOptions Options);
39 void Loop(const Vector<std::string> &CorpusDirs);
40 void ReadAndExecuteSeedCorpora(const Vector<std::string> &CorpusDirs);
41 void MinimizeCrashLoop(const Unit &U);
42 void RereadOutputCorpus(size_t MaxSize);
44 size_t secondsSinceProcessStartUp() {
45 return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
50 return Options.MaxTotalTimeSec > 0 &&
51 secondsSinceProcessStartUp() >
52 static_cast<size_t>(Options.MaxTotalTimeSec);
56 size_t Seconds = secondsSinceProcessStartUp();
57 return Seconds ? TotalNumberOfRuns / Seconds : 0;
60 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
62 static void StaticAlarmCallback();
63 static void StaticCrashSignalCallback();
64 static void StaticExitCallback();
65 static void StaticInterruptCallback();
66 static void StaticFileSizeExceedCallback();
67 static void StaticGracefulExitCallback();
69 void ExecuteCallback(const uint8_t *Data, size_t Size);
70 bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false,
71 InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr);
73 // Merge Corpora[1:] into Corpora[0].
74 void Merge(const Vector<std::string> &Corpora);
75 void CrashResistantMerge(const Vector<std::string> &Args,
76 const Vector<std::string> &Corpora,
77 const char *CoverageSummaryInputPathOrNull,
78 const char *CoverageSummaryOutputPathOrNull,
79 const char *MergeControlFilePathOrNull);
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();
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);
92 void HandleMalloc(size_t Size);
93 void AnnounceOutput(const uint8_t *Data, size_t Size);
99 void MaybeExitGracefully();
100 void CrashOnOverwrittenData();
101 void InterruptCallback();
102 void MutateAndTestOne();
103 void PurgeAllocator();
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);
110 void CheckExitOnSrcPosOrItem();
112 static void StaticDeathCallback();
113 void DumpCurrentUnit(const char *Prefix);
114 void DeathCallback();
116 void AllocateCurrentUnitData();
117 uint8_t *CurrentUnitData = nullptr;
118 std::atomic<size_t> CurrentUnitSize;
119 uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
121 bool GracefulExitRequested = false;
123 size_t TotalNumberOfRuns = 0;
124 size_t NumberOfNewUnitsAdded = 0;
126 size_t LastCorpusUpdateRun = 0;
128 bool HasMoreMallocsThanFrees = false;
129 size_t NumberOfLeakDetectionAttempts = 0;
131 system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now();
135 MutationDispatcher &MD;
136 FuzzingOptions Options;
139 system_clock::time_point ProcessStartTime = system_clock::now();
140 system_clock::time_point UnitStartTime, UnitStopTime;
141 long TimeOfLongestUnitInSeconds = 0;
142 long EpochOfLastReadOfOutputCorpus = 0;
144 size_t MaxInputLen = 0;
145 size_t MaxMutationLen = 0;
146 size_t TmpMaxMutationLen = 0;
148 Vector<uint32_t> UniqFeatureSetTmp;
150 // Need to know our own thread.
151 static thread_local bool IsMyThread;
154 struct ScopedEnableMsanInterceptorChecks {
155 ScopedEnableMsanInterceptorChecks() {
156 if (EF->__msan_scoped_enable_interceptor_checks)
157 EF->__msan_scoped_enable_interceptor_checks();
159 ~ScopedEnableMsanInterceptorChecks() {
160 if (EF->__msan_scoped_disable_interceptor_checks)
161 EF->__msan_scoped_disable_interceptor_checks();
165 struct ScopedDisableMsanInterceptorChecks {
166 ScopedDisableMsanInterceptorChecks() {
167 if (EF->__msan_scoped_disable_interceptor_checks)
168 EF->__msan_scoped_disable_interceptor_checks();
170 ~ScopedDisableMsanInterceptorChecks() {
171 if (EF->__msan_scoped_enable_interceptor_checks)
172 EF->__msan_scoped_enable_interceptor_checks();
176 } // namespace fuzzer
178 #endif // LLVM_FUZZER_INTERNAL_H