2017-08-22 07:25:50 +08:00
|
|
|
//===- FuzzerInternal.h - Internal header for the Fuzzer --------*- C++ -* ===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Define the main class fuzzer::Fuzzer and most functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_INTERNAL_H
|
|
|
|
#define LLVM_FUZZER_INTERNAL_H
|
|
|
|
|
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
#include "FuzzerExtFunctions.h"
|
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
#include "FuzzerOptions.h"
|
|
|
|
#include "FuzzerSHA1.h"
|
|
|
|
#include "FuzzerValueBitMap.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
|
|
|
class Fuzzer {
|
|
|
|
public:
|
|
|
|
|
|
|
|
Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
|
|
|
|
FuzzingOptions Options);
|
|
|
|
~Fuzzer();
|
2017-08-29 10:05:01 +08:00
|
|
|
void Loop(const Vector<std::string> &CorpusDirs);
|
|
|
|
void ReadAndExecuteSeedCorpora(const Vector<std::string> &CorpusDirs);
|
2017-08-22 07:25:50 +08:00
|
|
|
void MinimizeCrashLoop(const Unit &U);
|
|
|
|
void RereadOutputCorpus(size_t MaxSize);
|
|
|
|
|
|
|
|
size_t secondsSinceProcessStartUp() {
|
|
|
|
return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TimedOut() {
|
|
|
|
return Options.MaxTotalTimeSec > 0 &&
|
|
|
|
secondsSinceProcessStartUp() >
|
|
|
|
static_cast<size_t>(Options.MaxTotalTimeSec);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t execPerSec() {
|
|
|
|
size_t Seconds = secondsSinceProcessStartUp();
|
|
|
|
return Seconds ? TotalNumberOfRuns / Seconds : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
|
|
|
|
static void StaticAlarmCallback();
|
|
|
|
static void StaticCrashSignalCallback();
|
|
|
|
static void StaticExitCallback();
|
|
|
|
static void StaticInterruptCallback();
|
|
|
|
static void StaticFileSizeExceedCallback();
|
2017-11-10 04:30:19 +08:00
|
|
|
static void StaticGracefulExitCallback();
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
void ExecuteCallback(const uint8_t *Data, size_t Size);
|
|
|
|
bool RunOne(const uint8_t *Data, size_t Size, bool MayDeleteFile = false,
|
2017-12-02 03:18:38 +08:00
|
|
|
InputInfo *II = nullptr, bool *FoundUniqFeatures = nullptr);
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
// Merge Corpora[1:] into Corpora[0].
|
2017-08-28 07:20:09 +08:00
|
|
|
void Merge(const Vector<std::string> &Corpora);
|
|
|
|
void CrashResistantMerge(const Vector<std::string> &Args,
|
|
|
|
const Vector<std::string> &Corpora,
|
2017-08-22 07:25:50 +08:00
|
|
|
const char *CoverageSummaryInputPathOrNull,
|
2017-11-09 09:05:29 +08:00
|
|
|
const char *CoverageSummaryOutputPathOrNull,
|
|
|
|
const char *MergeControlFilePathOrNull);
|
2017-08-22 07:25:50 +08:00
|
|
|
void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
|
|
|
|
MutationDispatcher &GetMD() { return MD; }
|
|
|
|
void PrintFinalStats();
|
|
|
|
void SetMaxInputLen(size_t MaxInputLen);
|
|
|
|
void SetMaxMutationLen(size_t MaxMutationLen);
|
|
|
|
void RssLimitCallback();
|
|
|
|
|
|
|
|
bool InFuzzingThread() const { return IsMyThread; }
|
|
|
|
size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
|
|
|
|
void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
|
|
|
|
bool DuringInitialCorpusExecution);
|
|
|
|
|
|
|
|
void HandleMalloc(size_t Size);
|
|
|
|
void AnnounceOutput(const uint8_t *Data, size_t Size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void AlarmCallback();
|
|
|
|
void CrashCallback();
|
|
|
|
void ExitCallback();
|
2017-11-10 04:30:19 +08:00
|
|
|
void MaybeExitGracefully();
|
2017-08-22 07:25:50 +08:00
|
|
|
void CrashOnOverwrittenData();
|
|
|
|
void InterruptCallback();
|
|
|
|
void MutateAndTestOne();
|
2017-10-24 06:04:30 +08:00
|
|
|
void PurgeAllocator();
|
2017-08-22 07:25:50 +08:00
|
|
|
void ReportNewCoverage(InputInfo *II, const Unit &U);
|
|
|
|
void PrintPulseAndReportSlowInput(const uint8_t *Data, size_t Size);
|
|
|
|
void WriteToOutputCorpus(const Unit &U);
|
|
|
|
void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
|
|
|
|
void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0);
|
|
|
|
void PrintStatusForNewUnit(const Unit &U, const char *Text);
|
|
|
|
void CheckExitOnSrcPosOrItem();
|
|
|
|
|
|
|
|
static void StaticDeathCallback();
|
|
|
|
void DumpCurrentUnit(const char *Prefix);
|
|
|
|
void DeathCallback();
|
|
|
|
|
|
|
|
void AllocateCurrentUnitData();
|
|
|
|
uint8_t *CurrentUnitData = nullptr;
|
|
|
|
std::atomic<size_t> CurrentUnitSize;
|
|
|
|
uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
|
|
|
|
bool RunningCB = false;
|
|
|
|
|
2017-11-10 04:30:19 +08:00
|
|
|
bool GracefulExitRequested = false;
|
|
|
|
|
2017-08-22 07:25:50 +08:00
|
|
|
size_t TotalNumberOfRuns = 0;
|
|
|
|
size_t NumberOfNewUnitsAdded = 0;
|
|
|
|
|
|
|
|
size_t LastCorpusUpdateRun = 0;
|
|
|
|
|
|
|
|
bool HasMoreMallocsThanFrees = false;
|
|
|
|
size_t NumberOfLeakDetectionAttempts = 0;
|
|
|
|
|
2017-10-24 06:04:30 +08:00
|
|
|
system_clock::time_point LastAllocatorPurgeAttemptTime = system_clock::now();
|
|
|
|
|
2017-08-22 07:25:50 +08:00
|
|
|
UserCallback CB;
|
|
|
|
InputCorpus &Corpus;
|
|
|
|
MutationDispatcher &MD;
|
|
|
|
FuzzingOptions Options;
|
|
|
|
|
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
|
|
|
system_clock::time_point UnitStartTime, UnitStopTime;
|
|
|
|
long TimeOfLongestUnitInSeconds = 0;
|
|
|
|
long EpochOfLastReadOfOutputCorpus = 0;
|
|
|
|
|
|
|
|
size_t MaxInputLen = 0;
|
|
|
|
size_t MaxMutationLen = 0;
|
|
|
|
size_t TmpMaxMutationLen = 0;
|
|
|
|
|
2017-08-28 07:20:09 +08:00
|
|
|
Vector<uint32_t> UniqFeatureSetTmp;
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
// Need to know our own thread.
|
|
|
|
static thread_local bool IsMyThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_INTERNAL_H
|