]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/fuzzer/FuzzerInternal.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / fuzzer / FuzzerInternal.h
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(Vector<SizedFile> &CorporaFiles);
39   void ReadAndExecuteSeedCorpora(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   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 *FoundUniqFeatures = nullptr);
71
72   // Merge Corpora[1:] into Corpora[0].
73   void Merge(const Vector<std::string> &Corpora);
74   void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
75   MutationDispatcher &GetMD() { return MD; }
76   void PrintFinalStats();
77   void SetMaxInputLen(size_t MaxInputLen);
78   void SetMaxMutationLen(size_t MaxMutationLen);
79   void RssLimitCallback();
80
81   bool InFuzzingThread() const { return IsMyThread; }
82   size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
83   void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
84                                bool DuringInitialCorpusExecution);
85
86   void HandleMalloc(size_t Size);
87   static void MaybeExitGracefully();
88   std::string WriteToOutputCorpus(const Unit &U);
89
90 private:
91   void AlarmCallback();
92   void CrashCallback();
93   void ExitCallback();
94   void CrashOnOverwrittenData();
95   void InterruptCallback();
96   void MutateAndTestOne();
97   void PurgeAllocator();
98   void ReportNewCoverage(InputInfo *II, const Unit &U);
99   void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size);
100   void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
101   void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0);
102   void PrintStatusForNewUnit(const Unit &U, const char *Text);
103   void CheckExitOnSrcPosOrItem();
104
105   static void StaticDeathCallback();
106   void DumpCurrentUnit(const char *Prefix);
107   void DeathCallback();
108
109   void AllocateCurrentUnitData();
110   uint8_t *CurrentUnitData = nullptr;
111   std::atomic<size_t> CurrentUnitSize;
112   uint8_t BaseSha1[kSHA1NumBytes];  // Checksum of the base unit.
113
114   bool GracefulExitRequested = false;
115
116   size_t TotalNumberOfRuns = 0;
117   size_t NumberOfNewUnitsAdded = 0;
118
119   size_t LastCorpusUpdateRun = 0;
120
121   bool HasMoreMallocsThanFrees = false;
122   size_t NumberOfLeakDetectionAttempts = 0;
123
124   system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now();
125
126   UserCallback CB;
127   InputCorpus &Corpus;
128   MutationDispatcher &MD;
129   FuzzingOptions Options;
130   DataFlowTrace DFT;
131
132   system_clock::time_point ProcessStartTime = system_clock::now();
133   system_clock::time_point UnitStartTime, UnitStopTime;
134   long TimeOfLongestUnitInSeconds = 0;
135   long EpochOfLastReadOfOutputCorpus = 0;
136
137   size_t MaxInputLen = 0;
138   size_t MaxMutationLen = 0;
139   size_t TmpMaxMutationLen = 0;
140
141   Vector<uint32_t> UniqFeatureSetTmp;
142
143   // Need to know our own thread.
144   static thread_local bool IsMyThread;
145 };
146
147 struct ScopedEnableMsanInterceptorChecks {
148   ScopedEnableMsanInterceptorChecks() {
149     if (EF->__msan_scoped_enable_interceptor_checks)
150       EF->__msan_scoped_enable_interceptor_checks();
151   }
152   ~ScopedEnableMsanInterceptorChecks() {
153     if (EF->__msan_scoped_disable_interceptor_checks)
154       EF->__msan_scoped_disable_interceptor_checks();
155   }
156 };
157
158 struct ScopedDisableMsanInterceptorChecks {
159   ScopedDisableMsanInterceptorChecks() {
160     if (EF->__msan_scoped_disable_interceptor_checks)
161       EF->__msan_scoped_disable_interceptor_checks();
162   }
163   ~ScopedDisableMsanInterceptorChecks() {
164     if (EF->__msan_scoped_enable_interceptor_checks)
165       EF->__msan_scoped_enable_interceptor_checks();
166   }
167 };
168
169 } // namespace fuzzer
170
171 #endif // LLVM_FUZZER_INTERNAL_H