FuzzerInternal.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef LLVM_FUZZER_INTERNAL_H
  11. #define LLVM_FUZZER_INTERNAL_H
  12. #include "FuzzerDataFlowTrace.h"
  13. #include "FuzzerDefs.h"
  14. #include "FuzzerExtFunctions.h"
  15. #include "FuzzerInterface.h"
  16. #include "FuzzerOptions.h"
  17. #include "FuzzerSHA1.h"
  18. #include "FuzzerValueBitMap.h"
  19. #include <algorithm>
  20. #include <atomic>
  21. #include <chrono>
  22. #include <climits>
  23. #include <cstdlib>
  24. #include <string.h>
  25. namespace fuzzer {
  26. using namespace std::chrono;
  27. class Fuzzer {
  28. public:
  29. Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
  30. FuzzingOptions Options);
  31. ~Fuzzer();
  32. void Loop(Vector<SizedFile> &CorporaFiles);
  33. void ReadAndExecuteSeedCorpora(Vector<SizedFile> &CorporaFiles);
  34. void MinimizeCrashLoop(const Unit &U);
  35. void RereadOutputCorpus(size_t MaxSize);
  36. size_t secondsSinceProcessStartUp() {
  37. return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
  38. .count();
  39. }
  40. bool TimedOut() {
  41. return Options.MaxTotalTimeSec > 0 &&
  42. secondsSinceProcessStartUp() >
  43. static_cast<size_t>(Options.MaxTotalTimeSec);
  44. }
  45. size_t execPerSec() {
  46. size_t Seconds = secondsSinceProcessStartUp();
  47. return Seconds ? TotalNumberOfRuns / Seconds : 0;
  48. }
  49. size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
  50. static void StaticAlarmCallback();
  51. static void StaticCrashSignalCallback();
  52. static void StaticExitCallback();
  53. static void StaticInterruptCallback();
  54. static void StaticFileSizeExceedCallback();
  55. static void StaticGracefulExitCallback();
  56. void ExecuteCallback(const uint8_t *Data, size_t Size);
  57. bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false,
  58. InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr);
  59. // Merge Corpora[1:] into Corpora[0].
  60. void Merge(const Vector<std::string> &Corpora);
  61. void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
  62. MutationDispatcher &GetMD() { return MD; }
  63. void PrintFinalStats();
  64. void SetMaxInputLen(size_t MaxInputLen);
  65. void SetMaxMutationLen(size_t MaxMutationLen);
  66. void RssLimitCallback();
  67. bool InFuzzingThread() const { return IsMyThread; }
  68. size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
  69. void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
  70. bool DuringInitialCorpusExecution);
  71. void HandleMalloc(size_t Size);
  72. static void MaybeExitGracefully();
  73. std::string WriteToOutputCorpus(const Unit &U);
  74. private:
  75. void AlarmCallback();
  76. void CrashCallback();
  77. void ExitCallback();
  78. void CrashOnOverwrittenData();
  79. void InterruptCallback();
  80. void MutateAndTestOne();
  81. void PurgeAllocator();
  82. void ReportNewCoverage(InputInfo *II, const Unit &U);
  83. void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size);
  84. void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
  85. void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0,
  86. size_t Features = 0);
  87. void PrintStatusForNewUnit(const Unit &U, const char *Text);
  88. void CheckExitOnSrcPosOrItem();
  89. static void StaticDeathCallback();
  90. void DumpCurrentUnit(const char *Prefix);
  91. void DeathCallback();
  92. void AllocateCurrentUnitData();
  93. uint8_t *CurrentUnitData = nullptr;
  94. std::atomic<size_t> CurrentUnitSize;
  95. uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
  96. bool GracefulExitRequested = false;
  97. size_t TotalNumberOfRuns = 0;
  98. size_t NumberOfNewUnitsAdded = 0;
  99. size_t LastCorpusUpdateRun = 0;
  100. bool HasMoreMallocsThanFrees = false;
  101. size_t NumberOfLeakDetectionAttempts = 0;
  102. system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now();
  103. UserCallback CB;
  104. InputCorpus &Corpus;
  105. MutationDispatcher &MD;
  106. FuzzingOptions Options;
  107. DataFlowTrace DFT;
  108. system_clock::time_point ProcessStartTime = system_clock::now();
  109. system_clock::time_point UnitStartTime, UnitStopTime;
  110. long TimeOfLongestUnitInSeconds = 0;
  111. long EpochOfLastReadOfOutputCorpus = 0;
  112. size_t MaxInputLen = 0;
  113. size_t MaxMutationLen = 0;
  114. size_t TmpMaxMutationLen = 0;
  115. Vector<uint32_t> UniqFeatureSetTmp;
  116. // Need to know our own thread.
  117. static thread_local bool IsMyThread;
  118. };
  119. struct ScopedEnableMsanInterceptorChecks {
  120. ScopedEnableMsanInterceptorChecks() {
  121. if (EF->__msan_scoped_enable_interceptor_checks)
  122. EF->__msan_scoped_enable_interceptor_checks();
  123. }
  124. ~ScopedEnableMsanInterceptorChecks() {
  125. if (EF->__msan_scoped_disable_interceptor_checks)
  126. EF->__msan_scoped_disable_interceptor_checks();
  127. }
  128. };
  129. struct ScopedDisableMsanInterceptorChecks {
  130. ScopedDisableMsanInterceptorChecks() {
  131. if (EF->__msan_scoped_disable_interceptor_checks)
  132. EF->__msan_scoped_disable_interceptor_checks();
  133. }
  134. ~ScopedDisableMsanInterceptorChecks() {
  135. if (EF->__msan_scoped_enable_interceptor_checks)
  136. EF->__msan_scoped_enable_interceptor_checks();
  137. }
  138. };
  139. } // namespace fuzzer
  140. #endif // LLVM_FUZZER_INTERNAL_H