Home | History | Annotate | Download | only in Fuzzer
      1 //===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 // Define the main class fuzzer::Fuzzer and most functions.
     10 //===----------------------------------------------------------------------===//
     11 
     12 #ifndef LLVM_FUZZER_INTERNAL_H
     13 #define LLVM_FUZZER_INTERNAL_H
     14 
     15 #include <algorithm>
     16 #include <atomic>
     17 #include <chrono>
     18 #include <climits>
     19 #include <cstdlib>
     20 #include <string.h>
     21 
     22 #include "FuzzerDefs.h"
     23 #include "FuzzerExtFunctions.h"
     24 #include "FuzzerInterface.h"
     25 #include "FuzzerOptions.h"
     26 #include "FuzzerValueBitMap.h"
     27 
     28 namespace fuzzer {
     29 
     30 using namespace std::chrono;
     31 
     32 class Fuzzer {
     33 public:
     34 
     35   // Aggregates all available coverage measurements.
     36   struct Coverage {
     37     Coverage() { Reset(); }
     38 
     39     void Reset() {
     40       BlockCoverage = 0;
     41       CallerCalleeCoverage = 0;
     42       CounterBitmapBits = 0;
     43       CounterBitmap.clear();
     44       VPMap.Reset();
     45     }
     46 
     47     size_t BlockCoverage;
     48     size_t CallerCalleeCoverage;
     49     // Precalculated number of bits in CounterBitmap.
     50     size_t CounterBitmapBits;
     51     std::vector<uint8_t> CounterBitmap;
     52     ValueBitMap VPMap;
     53   };
     54 
     55   Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
     56          FuzzingOptions Options);
     57   ~Fuzzer();
     58   void Loop();
     59   void MinimizeCrashLoop(const Unit &U);
     60   void ShuffleAndMinimize(UnitVector *V);
     61   void InitializeTraceState();
     62   void RereadOutputCorpus(size_t MaxSize);
     63 
     64   size_t secondsSinceProcessStartUp() {
     65     return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
     66         .count();
     67   }
     68 
     69   bool TimedOut() {
     70     return Options.MaxTotalTimeSec > 0 &&
     71            secondsSinceProcessStartUp() >
     72                static_cast<size_t>(Options.MaxTotalTimeSec);
     73   }
     74 
     75   size_t execPerSec() {
     76     size_t Seconds = secondsSinceProcessStartUp();
     77     return Seconds ? TotalNumberOfRuns / Seconds : 0;
     78   }
     79 
     80   size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
     81 
     82   static void StaticAlarmCallback();
     83   static void StaticCrashSignalCallback();
     84   static void StaticInterruptCallback();
     85 
     86   void ExecuteCallback(const uint8_t *Data, size_t Size);
     87   size_t RunOne(const uint8_t *Data, size_t Size);
     88 
     89   // Merge Corpora[1:] into Corpora[0].
     90   void Merge(const std::vector<std::string> &Corpora);
     91   // Returns a subset of 'Extra' that adds coverage to 'Initial'.
     92   UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
     93   MutationDispatcher &GetMD() { return MD; }
     94   void PrintFinalStats();
     95   void SetMaxInputLen(size_t MaxInputLen);
     96   void SetMaxMutationLen(size_t MaxMutationLen);
     97   void RssLimitCallback();
     98 
     99   // Public for tests.
    100   void ResetCoverage();
    101 
    102   bool InFuzzingThread() const { return IsMyThread; }
    103   size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
    104   void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
    105                                bool DuringInitialCorpusExecution);
    106 
    107 private:
    108   void AlarmCallback();
    109   void CrashCallback();
    110   void InterruptCallback();
    111   void MutateAndTestOne();
    112   void ReportNewCoverage(InputInfo *II, const Unit &U);
    113   size_t RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
    114   void WriteToOutputCorpus(const Unit &U);
    115   void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
    116   void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0);
    117   void PrintStatusForNewUnit(const Unit &U);
    118   void ShuffleCorpus(UnitVector *V);
    119   void AddToCorpus(const Unit &U);
    120   void CheckExitOnSrcPosOrItem();
    121 
    122   // Trace-based fuzzing: we run a unit with some kind of tracing
    123   // enabled and record potentially useful mutations. Then
    124   // We apply these mutations one by one to the unit and run it again.
    125 
    126   // Start tracing; forget all previously proposed mutations.
    127   void StartTraceRecording();
    128   // Stop tracing.
    129   void StopTraceRecording();
    130 
    131   void SetDeathCallback();
    132   static void StaticDeathCallback();
    133   void DumpCurrentUnit(const char *Prefix);
    134   void DeathCallback();
    135 
    136   void ResetEdgeCoverage();
    137   void ResetCounters();
    138   void PrepareCounters(Fuzzer::Coverage *C);
    139   bool RecordMaxCoverage(Fuzzer::Coverage *C);
    140 
    141   void AllocateCurrentUnitData();
    142   uint8_t *CurrentUnitData = nullptr;
    143   std::atomic<size_t> CurrentUnitSize;
    144   uint8_t BaseSha1[kSHA1NumBytes];  // Checksum of the base unit.
    145 
    146   size_t TotalNumberOfRuns = 0;
    147   size_t NumberOfNewUnitsAdded = 0;
    148 
    149   bool HasMoreMallocsThanFrees = false;
    150   size_t NumberOfLeakDetectionAttempts = 0;
    151 
    152   UserCallback CB;
    153   InputCorpus &Corpus;
    154   MutationDispatcher &MD;
    155   FuzzingOptions Options;
    156 
    157   system_clock::time_point ProcessStartTime = system_clock::now();
    158   system_clock::time_point UnitStartTime, UnitStopTime;
    159   long TimeOfLongestUnitInSeconds = 0;
    160   long EpochOfLastReadOfOutputCorpus = 0;
    161 
    162   // Maximum recorded coverage.
    163   Coverage MaxCoverage;
    164 
    165   size_t MaxInputLen = 0;
    166   size_t MaxMutationLen = 0;
    167 
    168   // Need to know our own thread.
    169   static thread_local bool IsMyThread;
    170 
    171   bool InMergeMode = false;
    172 };
    173 
    174 }; // namespace fuzzer
    175 
    176 #endif // LLVM_FUZZER_INTERNAL_H
    177