1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
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
7 //===----------------------------------------------------------------------===//
8 // Define the main class fuzzer::Fuzzer and most functions.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_INTERNAL_H
12 #define LLVM_FUZZER_INTERNAL_H
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"
30 using namespace std::chrono
;
35 Fuzzer(UserCallback CB
, InputCorpus
&Corpus
, MutationDispatcher
&MD
,
36 FuzzingOptions Options
);
38 int Loop(Vector
<SizedFile
> &CorporaFiles
);
39 int ReadAndExecuteSeedCorpora(Vector
<SizedFile
> &CorporaFiles
);
40 void MinimizeCrashLoop(const Unit
&U
);
41 void RereadOutputCorpus(size_t MaxSize
);
43 size_t secondsSinceProcessStartUp() {
44 return duration_cast
<seconds
>(system_clock::now() - ProcessStartTime
)
49 return Options
.MaxTotalTimeSec
> 0 &&
50 secondsSinceProcessStartUp() >
51 static_cast<size_t>(Options
.MaxTotalTimeSec
);
55 size_t Seconds
= secondsSinceProcessStartUp();
56 return Seconds
? TotalNumberOfRuns
/ Seconds
: 0;
59 size_t getTotalNumberOfRuns() { return TotalNumberOfRuns
; }
61 static void StaticAlarmCallback();
62 static void StaticCrashSignalCallback();
63 static void StaticExitCallback();
64 static void StaticInterruptCallback();
65 static void StaticFileSizeExceedCallback();
66 static void StaticGracefulExitCallback();
68 static void GracefullyExit();
69 static bool isGracefulExitRequested();
71 int 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 *FoundUniqFeatures
= nullptr);
75 // Merge Corpora[1:] into Corpora[0].
76 void Merge(const Vector
<std::string
> &Corpora
);
77 int CrashResistantMergeInternalStep(const std::string
&ControlFilePath
);
78 MutationDispatcher
&GetMD() { return MD
; }
79 void PrintFinalStats();
80 void SetMaxInputLen(size_t MaxInputLen
);
81 void SetMaxMutationLen(size_t MaxMutationLen
);
82 void RssLimitCallback();
84 bool InFuzzingThread() const { return IsMyThread
; }
85 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data
) const;
86 void TryDetectingAMemoryLeak(const uint8_t *Data
, size_t Size
,
87 bool DuringInitialCorpusExecution
);
89 void HandleMalloc(size_t Size
);
90 static bool MaybeExitGracefully();
91 std::string
WriteToOutputCorpus(const Unit
&U
);
97 void CrashOnOverwrittenData();
98 void InterruptCallback();
99 bool MutateAndTestOne();
100 void PurgeAllocator();
101 void ReportNewCoverage(InputInfo
*II
, const Unit
&U
);
102 void PrintPulseAndReportSlowInput(const uint8_t *Data
, size_t Size
);
103 void WriteUnitToFileWithPrefix(const Unit
&U
, const char *Prefix
);
104 void PrintStats(const char *Where
, const char *End
= "\n", size_t Units
= 0,
105 size_t Features
= 0);
106 void PrintStatusForNewUnit(const Unit
&U
, const char *Text
);
107 void CheckExitOnSrcPosOrItem();
109 static void StaticDeathCallback();
110 void DumpCurrentUnit(const char *Prefix
);
111 void DeathCallback();
113 void AllocateCurrentUnitData();
114 uint8_t *CurrentUnitData
= nullptr;
115 std::atomic
<size_t> CurrentUnitSize
;
116 uint8_t BaseSha1
[kSHA1NumBytes
]; // Checksum of the base unit.
118 bool GracefulExitRequested
= false;
120 size_t TotalNumberOfRuns
= 0;
121 size_t NumberOfNewUnitsAdded
= 0;
123 size_t LastCorpusUpdateRun
= 0;
125 bool HasMoreMallocsThanFrees
= false;
126 size_t NumberOfLeakDetectionAttempts
= 0;
128 system_clock::time_point LastAllocatorPurgeAttemptTime
= system_clock::now();
132 MutationDispatcher
&MD
;
133 FuzzingOptions Options
;
136 system_clock::time_point ProcessStartTime
= system_clock::now();
137 system_clock::time_point UnitStartTime
, UnitStopTime
;
138 long TimeOfLongestUnitInSeconds
= 0;
139 long EpochOfLastReadOfOutputCorpus
= 0;
141 size_t MaxInputLen
= 0;
142 size_t MaxMutationLen
= 0;
143 size_t TmpMaxMutationLen
= 0;
145 Vector
<uint32_t> UniqFeatureSetTmp
;
147 // Need to know our own thread.
148 static thread_local
bool IsMyThread
;
151 struct ScopedEnableMsanInterceptorChecks
{
152 ScopedEnableMsanInterceptorChecks() {
153 if (EF
->__msan_scoped_enable_interceptor_checks
)
154 EF
->__msan_scoped_enable_interceptor_checks();
156 ~ScopedEnableMsanInterceptorChecks() {
157 if (EF
->__msan_scoped_disable_interceptor_checks
)
158 EF
->__msan_scoped_disable_interceptor_checks();
162 struct ScopedDisableMsanInterceptorChecks
{
163 ScopedDisableMsanInterceptorChecks() {
164 if (EF
->__msan_scoped_disable_interceptor_checks
)
165 EF
->__msan_scoped_disable_interceptor_checks();
167 ~ScopedDisableMsanInterceptorChecks() {
168 if (EF
->__msan_scoped_enable_interceptor_checks
)
169 EF
->__msan_scoped_enable_interceptor_checks();
173 } // namespace fuzzer
175 #endif // LLVM_FUZZER_INTERNAL_H