[DominatorTree] Add support for mixed pre/post CFG views.
[llvm-project.git] / compiler-rt / lib / fuzzer / FuzzerInternal.h
blob37c8a01dc3c64709c703e9a72457445383ce263e
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 //===----------------------------------------------------------------------===//
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"
21 #include <algorithm>
22 #include <atomic>
23 #include <chrono>
24 #include <climits>
25 #include <cstdlib>
26 #include <string.h>
28 namespace fuzzer {
30 using namespace std::chrono;
32 class Fuzzer {
33 public:
35 Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
36 FuzzingOptions Options);
37 ~Fuzzer();
38 void Loop(Vector<SizedFile> &CorporaFiles);
39 void 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)
45 .count();
48 bool TimedOut() {
49 return Options.MaxTotalTimeSec > 0 &&
50 secondsSinceProcessStartUp() >
51 static_cast<size_t>(Options.MaxTotalTimeSec);
54 size_t execPerSec() {
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 void ExecuteCallback(const uint8_t *Data, size_t Size);
69 bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false,
70 InputInfo *II = nullptr, bool ForceAddToCorpus = false,
71 bool *FoundUniqFeatures = nullptr);
72 void TPCUpdateObservedPCs();
74 // Merge Corpora[1:] into Corpora[0].
75 void Merge(const Vector<std::string> &Corpora);
76 void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
77 MutationDispatcher &GetMD() { return MD; }
78 void PrintFinalStats();
79 void SetMaxInputLen(size_t MaxInputLen);
80 void SetMaxMutationLen(size_t MaxMutationLen);
81 void RssLimitCallback();
83 bool InFuzzingThread() const { return IsMyThread; }
84 size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
85 void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
86 bool DuringInitialCorpusExecution);
88 void HandleMalloc(size_t Size);
89 static void MaybeExitGracefully();
90 std::string WriteToOutputCorpus(const Unit &U);
92 private:
93 void AlarmCallback();
94 void CrashCallback();
95 void ExitCallback();
96 void CrashOnOverwrittenData();
97 void InterruptCallback();
98 void MutateAndTestOne();
99 void PurgeAllocator();
100 void ReportNewCoverage(InputInfo *II, const Unit &U);
101 void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size);
102 void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
103 void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0,
104 size_t Features = 0);
105 void PrintStatusForNewUnit(const Unit &U, const char *Text);
106 void CheckExitOnSrcPosOrItem();
108 static void StaticDeathCallback();
109 void DumpCurrentUnit(const char *Prefix);
110 void DeathCallback();
112 void AllocateCurrentUnitData();
113 uint8_t *CurrentUnitData = nullptr;
114 std::atomic<size_t> CurrentUnitSize;
115 uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
117 bool GracefulExitRequested = false;
119 size_t TotalNumberOfRuns = 0;
120 size_t NumberOfNewUnitsAdded = 0;
122 size_t LastCorpusUpdateRun = 0;
124 bool HasMoreMallocsThanFrees = false;
125 size_t NumberOfLeakDetectionAttempts = 0;
127 system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now();
129 UserCallback CB;
130 InputCorpus &Corpus;
131 MutationDispatcher &MD;
132 FuzzingOptions Options;
133 DataFlowTrace DFT;
135 system_clock::time_point ProcessStartTime = system_clock::now();
136 system_clock::time_point UnitStartTime, UnitStopTime;
137 long TimeOfLongestUnitInSeconds = 0;
138 long EpochOfLastReadOfOutputCorpus = 0;
140 size_t MaxInputLen = 0;
141 size_t MaxMutationLen = 0;
142 size_t TmpMaxMutationLen = 0;
144 Vector<uint32_t> UniqFeatureSetTmp;
146 // Need to know our own thread.
147 static thread_local bool IsMyThread;
150 struct ScopedEnableMsanInterceptorChecks {
151 ScopedEnableMsanInterceptorChecks() {
152 if (EF->__msan_scoped_enable_interceptor_checks)
153 EF->__msan_scoped_enable_interceptor_checks();
155 ~ScopedEnableMsanInterceptorChecks() {
156 if (EF->__msan_scoped_disable_interceptor_checks)
157 EF->__msan_scoped_disable_interceptor_checks();
161 struct ScopedDisableMsanInterceptorChecks {
162 ScopedDisableMsanInterceptorChecks() {
163 if (EF->__msan_scoped_disable_interceptor_checks)
164 EF->__msan_scoped_disable_interceptor_checks();
166 ~ScopedDisableMsanInterceptorChecks() {
167 if (EF->__msan_scoped_enable_interceptor_checks)
168 EF->__msan_scoped_enable_interceptor_checks();
172 } // namespace fuzzer
174 #endif // LLVM_FUZZER_INTERNAL_H