2015-01-29 16:58:29 +00: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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-08-10 16:37:40 +00:00
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_INTERNAL_H
|
|
|
|
#define LLVM_FUZZER_INTERNAL_H
|
|
|
|
|
2016-09-21 01:50:50 +00:00
|
|
|
#include "FuzzerDefs.h"
|
2016-08-24 01:38:42 +00:00
|
|
|
#include "FuzzerExtFunctions.h"
|
|
|
|
#include "FuzzerInterface.h"
|
2016-09-21 02:05:39 +00:00
|
|
|
#include "FuzzerOptions.h"
|
2016-11-30 19:06:14 +00:00
|
|
|
#include "FuzzerSHA1.h"
|
2016-08-24 01:38:42 +00:00
|
|
|
#include "FuzzerValueBitMap.h"
|
2016-12-13 17:46:11 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <climits>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string.h>
|
2016-08-24 01:38:42 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
namespace fuzzer {
|
2016-05-13 18:04:35 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
using namespace std::chrono;
|
2015-05-12 18:51:57 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
class Fuzzer {
|
2016-01-22 22:28:27 +00:00
|
|
|
public:
|
2016-05-10 23:43:15 +00:00
|
|
|
|
|
|
|
// Aggregates all available coverage measurements.
|
|
|
|
struct Coverage {
|
|
|
|
Coverage() { Reset(); }
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
BlockCoverage = 0;
|
|
|
|
CallerCalleeCoverage = 0;
|
|
|
|
CounterBitmapBits = 0;
|
|
|
|
CounterBitmap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t BlockCoverage;
|
|
|
|
size_t CallerCalleeCoverage;
|
|
|
|
// Precalculated number of bits in CounterBitmap.
|
|
|
|
size_t CounterBitmapBits;
|
|
|
|
std::vector<uint8_t> CounterBitmap;
|
|
|
|
};
|
|
|
|
|
2016-09-21 22:42:17 +00:00
|
|
|
Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
|
|
|
|
FuzzingOptions Options);
|
2016-08-05 20:09:53 +00:00
|
|
|
~Fuzzer();
|
2015-09-08 17:30:35 +00:00
|
|
|
void Loop();
|
2016-10-15 01:00:24 +00:00
|
|
|
void MinimizeCrashLoop(const Unit &U);
|
2016-09-21 01:04:43 +00:00
|
|
|
void ShuffleAndMinimize(UnitVector *V);
|
2015-05-11 21:16:27 +00:00
|
|
|
void InitializeTraceState();
|
2016-03-12 01:57:04 +00:00
|
|
|
void RereadOutputCorpus(size_t MaxSize);
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-02-04 23:42:42 +00:00
|
|
|
size_t secondsSinceProcessStartUp() {
|
|
|
|
return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
|
|
|
|
.count();
|
|
|
|
}
|
2016-10-15 01:00:24 +00:00
|
|
|
|
|
|
|
bool TimedOut() {
|
|
|
|
return Options.MaxTotalTimeSec > 0 &&
|
|
|
|
secondsSinceProcessStartUp() >
|
|
|
|
static_cast<size_t>(Options.MaxTotalTimeSec);
|
|
|
|
}
|
|
|
|
|
2016-02-26 22:42:23 +00:00
|
|
|
size_t execPerSec() {
|
|
|
|
size_t Seconds = secondsSinceProcessStartUp();
|
|
|
|
return Seconds ? TotalNumberOfRuns / Seconds : 0;
|
|
|
|
}
|
2015-02-04 23:42:42 +00:00
|
|
|
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
|
2015-03-31 20:13:20 +00:00
|
|
|
static void StaticAlarmCallback();
|
2016-03-01 22:19:21 +00:00
|
|
|
static void StaticCrashSignalCallback();
|
|
|
|
static void StaticInterruptCallback();
|
2017-01-05 22:05:47 +00:00
|
|
|
static void StaticFileSizeExceedCallback();
|
2015-03-31 20:13:20 +00:00
|
|
|
|
2016-02-13 17:56:51 +00:00
|
|
|
void ExecuteCallback(const uint8_t *Data, size_t Size);
|
2016-10-05 22:56:21 +00:00
|
|
|
size_t RunOne(const uint8_t *Data, size_t Size);
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-10-24 01:16:40 +00:00
|
|
|
// Merge Corpora[1:] into Corpora[0].
|
|
|
|
void Merge(const std::vector<std::string> &Corpora);
|
2016-12-09 01:17:24 +00:00
|
|
|
void CrashResistantMerge(const std::vector<std::string> &Args,
|
|
|
|
const std::vector<std::string> &Corpora);
|
|
|
|
void CrashResistantMergeInternalStep(const std::string &ControlFilePath);
|
2016-03-18 00:23:29 +00:00
|
|
|
// Returns a subset of 'Extra' that adds coverage to 'Initial'.
|
|
|
|
UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
|
2016-02-13 06:24:18 +00:00
|
|
|
MutationDispatcher &GetMD() { return MD; }
|
2016-02-26 22:42:23 +00:00
|
|
|
void PrintFinalStats();
|
2016-09-22 23:16:36 +00:00
|
|
|
void SetMaxInputLen(size_t MaxInputLen);
|
|
|
|
void SetMaxMutationLen(size_t MaxMutationLen);
|
2016-05-06 23:38:07 +00:00
|
|
|
void RssLimitCallback();
|
2015-10-24 01:16:40 +00:00
|
|
|
|
2016-05-24 23:14:29 +00:00
|
|
|
// Public for tests.
|
|
|
|
void ResetCoverage();
|
|
|
|
|
2016-05-26 21:32:30 +00:00
|
|
|
bool InFuzzingThread() const { return IsMyThread; }
|
2016-05-26 22:17:32 +00:00
|
|
|
size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
|
2016-10-18 18:38:08 +00:00
|
|
|
void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
|
|
|
|
bool DuringInitialCorpusExecution);
|
2016-05-26 21:32:30 +00:00
|
|
|
|
2016-11-30 22:39:35 +00:00
|
|
|
void HandleMalloc(size_t Size);
|
2017-01-20 20:57:07 +00:00
|
|
|
void AnnounceOutput(const uint8_t *Data, size_t Size);
|
2016-11-30 22:39:35 +00:00
|
|
|
|
2016-01-22 22:28:27 +00:00
|
|
|
private:
|
2015-03-31 20:13:20 +00:00
|
|
|
void AlarmCallback();
|
2016-03-01 22:19:21 +00:00
|
|
|
void CrashCallback();
|
|
|
|
void InterruptCallback();
|
2015-12-19 02:49:09 +00:00
|
|
|
void MutateAndTestOne();
|
2016-09-21 22:42:17 +00:00
|
|
|
void ReportNewCoverage(InputInfo *II, const Unit &U);
|
2016-10-05 22:56:21 +00:00
|
|
|
size_t RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
|
2015-01-29 16:58:29 +00:00
|
|
|
void WriteToOutputCorpus(const Unit &U);
|
2015-07-23 18:37:22 +00:00
|
|
|
void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
|
2016-09-23 01:58:51 +00:00
|
|
|
void PrintStats(const char *Where, const char *End = "\n", size_t Units = 0);
|
2015-11-12 01:02:01 +00:00
|
|
|
void PrintStatusForNewUnit(const Unit &U);
|
2016-03-18 00:23:29 +00:00
|
|
|
void ShuffleCorpus(UnitVector *V);
|
2016-09-30 23:29:27 +00:00
|
|
|
void AddToCorpus(const Unit &U);
|
2016-10-18 18:06:05 +00:00
|
|
|
void CheckExitOnSrcPosOrItem();
|
2016-03-18 00:23:29 +00:00
|
|
|
|
2015-05-07 21:02:11 +00:00
|
|
|
// Trace-based fuzzing: we run a unit with some kind of tracing
|
|
|
|
// enabled and record potentially useful mutations. Then
|
|
|
|
// We apply these mutations one by one to the unit and run it again.
|
|
|
|
|
|
|
|
// Start tracing; forget all previously proposed mutations.
|
|
|
|
void StartTraceRecording();
|
2016-01-09 03:08:58 +00:00
|
|
|
// Stop tracing.
|
|
|
|
void StopTraceRecording();
|
2015-05-07 21:02:11 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
void SetDeathCallback();
|
2015-03-31 20:13:20 +00:00
|
|
|
static void StaticDeathCallback();
|
2016-03-01 22:19:21 +00:00
|
|
|
void DumpCurrentUnit(const char *Prefix);
|
2015-03-31 20:13:20 +00:00
|
|
|
void DeathCallback();
|
2016-01-13 23:46:01 +00:00
|
|
|
|
2016-09-22 23:16:36 +00:00
|
|
|
void AllocateCurrentUnitData();
|
2016-05-27 00:21:33 +00:00
|
|
|
uint8_t *CurrentUnitData = nullptr;
|
2016-05-27 00:54:15 +00:00
|
|
|
std::atomic<size_t> CurrentUnitSize;
|
2016-08-17 20:45:23 +00:00
|
|
|
uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
|
2016-12-13 17:46:25 +00:00
|
|
|
bool RunningCB = false;
|
2015-01-29 16:58:29 +00:00
|
|
|
|
|
|
|
size_t TotalNumberOfRuns = 0;
|
2016-02-26 22:42:23 +00:00
|
|
|
size_t NumberOfNewUnitsAdded = 0;
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2016-04-20 00:24:21 +00:00
|
|
|
bool HasMoreMallocsThanFrees = false;
|
2016-04-27 19:52:34 +00:00
|
|
|
size_t NumberOfLeakDetectionAttempts = 0;
|
2016-04-20 00:24:21 +00:00
|
|
|
|
2016-02-13 03:25:16 +00:00
|
|
|
UserCallback CB;
|
2016-09-21 22:42:17 +00:00
|
|
|
InputCorpus &Corpus;
|
2016-02-13 03:25:16 +00:00
|
|
|
MutationDispatcher &MD;
|
2015-01-29 16:58:29 +00:00
|
|
|
FuzzingOptions Options;
|
2016-09-21 22:42:17 +00:00
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
2016-09-22 01:34:58 +00:00
|
|
|
system_clock::time_point UnitStartTime, UnitStopTime;
|
2015-03-30 23:04:35 +00:00
|
|
|
long TimeOfLongestUnitInSeconds = 0;
|
2015-05-08 21:30:55 +00:00
|
|
|
long EpochOfLastReadOfOutputCorpus = 0;
|
2016-05-10 23:43:15 +00:00
|
|
|
|
|
|
|
// Maximum recorded coverage.
|
|
|
|
Coverage MaxCoverage;
|
2016-08-25 01:25:03 +00:00
|
|
|
|
2016-09-22 23:16:36 +00:00
|
|
|
size_t MaxInputLen = 0;
|
|
|
|
size_t MaxMutationLen = 0;
|
|
|
|
|
2016-05-26 21:32:30 +00:00
|
|
|
// Need to know our own thread.
|
|
|
|
static thread_local bool IsMyThread;
|
2016-09-10 00:15:41 +00:00
|
|
|
|
|
|
|
bool InMergeMode = false;
|
2015-01-29 16:58:29 +00:00
|
|
|
};
|
|
|
|
|
2016-01-22 22:28:27 +00:00
|
|
|
}; // namespace fuzzer
|
2015-08-10 16:37:40 +00:00
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_INTERNAL_H
|