george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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 | |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 14 | #include "FuzzerDataFlowTrace.h" |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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(); |
kcc | 11883b2 | 2019-05-10 01:34:26 +0000 | [diff] [blame^] | 38 | void Loop(Vector<SizedFile> &CorporaFiles); |
| 39 | void ReadAndExecuteSeedCorpora(Vector<SizedFile> &CorporaFiles); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 40 | void MinimizeCrashLoop(const Unit &U); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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(); |
kcc | 1239a99 | 2017-11-09 20:30:19 +0000 | [diff] [blame] | 66 | static void StaticGracefulExitCallback(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 67 | |
| 68 | void ExecuteCallback(const uint8_t *Data, size_t Size); |
| 69 | bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false, |
kcc | b6836be | 2017-12-01 19:18:38 +0000 | [diff] [blame] | 70 | InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 71 | |
| 72 | // Merge Corpora[1:] into Corpora[0]. |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 73 | void Merge(const Vector<std::string> &Corpora); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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); |
kcc | a381586 | 2019-02-08 21:27:23 +0000 | [diff] [blame] | 87 | static void MaybeExitGracefully(); |
kcc | 243006d | 2019-02-12 00:12:33 +0000 | [diff] [blame] | 88 | std::string WriteToOutputCorpus(const Unit &U); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 89 | |
| 90 | private: |
| 91 | void AlarmCallback(); |
| 92 | void CrashCallback(); |
| 93 | void ExitCallback(); |
| 94 | void CrashOnOverwrittenData(); |
| 95 | void InterruptCallback(); |
| 96 | void MutateAndTestOne(); |
alekseyshl | d995b55 | 2017-10-23 22:04:30 +0000 | [diff] [blame] | 97 | void PurgeAllocator(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 98 | void ReportNewCoverage(InputInfo *II, const Unit &U); |
| 99 | void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 113 | |
kcc | 1239a99 | 2017-11-09 20:30:19 +0000 | [diff] [blame] | 114 | bool GracefulExitRequested = false; |
| 115 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 116 | size_t TotalNumberOfRuns = 0; |
| 117 | size_t NumberOfNewUnitsAdded = 0; |
| 118 | |
| 119 | size_t LastCorpusUpdateRun = 0; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 120 | |
| 121 | bool HasMoreMallocsThanFrees = false; |
| 122 | size_t NumberOfLeakDetectionAttempts = 0; |
| 123 | |
alekseyshl | d995b55 | 2017-10-23 22:04:30 +0000 | [diff] [blame] | 124 | system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now(); |
| 125 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 126 | UserCallback CB; |
| 127 | InputCorpus &Corpus; |
| 128 | MutationDispatcher &MD; |
| 129 | FuzzingOptions Options; |
kcc | 86e4388 | 2018-06-06 01:23:29 +0000 | [diff] [blame] | 130 | DataFlowTrace DFT; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 141 | Vector<uint32_t> UniqFeatureSetTmp; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 142 | |
| 143 | // Need to know our own thread. |
| 144 | static thread_local bool IsMyThread; |
| 145 | }; |
| 146 | |
morehouse | 1467b79 | 2018-07-09 23:51:08 +0000 | [diff] [blame] | 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 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 169 | } // namespace fuzzer |
| 170 | |
| 171 | #endif // LLVM_FUZZER_INTERNAL_H |