2015-01-30 00:58:29 +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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-08-11 00:37:40 +08:00
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_INTERNAL_H
|
|
|
|
#define LLVM_FUZZER_INTERNAL_H
|
|
|
|
|
2016-02-13 11:37:24 +08:00
|
|
|
#include <algorithm>
|
2016-05-27 08:54:15 +08:00
|
|
|
#include <atomic>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <chrono>
|
2016-01-23 06:28:27 +08:00
|
|
|
#include <climits>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <cstdlib>
|
2016-01-16 11:53:32 +08:00
|
|
|
#include <string.h>
|
2016-08-24 09:38:42 +08:00
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
#include "FuzzerDefs.h"
|
2016-08-24 09:38:42 +08:00
|
|
|
#include "FuzzerExtFunctions.h"
|
|
|
|
#include "FuzzerInterface.h"
|
2016-09-21 10:05:39 +08:00
|
|
|
#include "FuzzerOptions.h"
|
2016-08-24 09:38:42 +08:00
|
|
|
#include "FuzzerValueBitMap.h"
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
2016-05-14 02:04:35 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
using namespace std::chrono;
|
2015-05-13 02:51:57 +08:00
|
|
|
|
2016-08-17 03:33:51 +08:00
|
|
|
// See FuzzerTraceState.cpp
|
|
|
|
void EnableValueProfile();
|
|
|
|
size_t VPMapMergeFromCurrent(ValueBitMap &M);
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
class Fuzzer {
|
2016-01-23 06:28:27 +08:00
|
|
|
public:
|
2016-05-11 07:43:15 +08:00
|
|
|
|
|
|
|
// Aggregates all available coverage measurements.
|
|
|
|
struct Coverage {
|
|
|
|
Coverage() { Reset(); }
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
BlockCoverage = 0;
|
|
|
|
CallerCalleeCoverage = 0;
|
|
|
|
CounterBitmapBits = 0;
|
|
|
|
CounterBitmap.clear();
|
2016-08-17 03:33:51 +08:00
|
|
|
VPMap.Reset();
|
2016-09-15 09:30:18 +08:00
|
|
|
TPCMap.Reset();
|
2016-08-17 03:33:51 +08:00
|
|
|
VPMapBits = 0;
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DebugString() const;
|
|
|
|
|
|
|
|
size_t BlockCoverage;
|
|
|
|
size_t CallerCalleeCoverage;
|
|
|
|
// Precalculated number of bits in CounterBitmap.
|
|
|
|
size_t CounterBitmapBits;
|
|
|
|
std::vector<uint8_t> CounterBitmap;
|
2016-09-15 09:30:18 +08:00
|
|
|
ValueBitMap TPCMap;
|
2016-08-17 03:33:51 +08:00
|
|
|
ValueBitMap VPMap;
|
|
|
|
size_t VPMapBits;
|
2016-05-11 07:43:15 +08:00
|
|
|
};
|
|
|
|
|
2016-09-22 06:42:17 +08:00
|
|
|
Fuzzer(UserCallback CB, InputCorpus &Corpus, MutationDispatcher &MD,
|
|
|
|
FuzzingOptions Options);
|
2016-08-06 04:09:53 +08:00
|
|
|
~Fuzzer();
|
2015-09-09 01:30:35 +08:00
|
|
|
void Loop();
|
2016-09-21 09:04:43 +08:00
|
|
|
void ShuffleAndMinimize(UnitVector *V);
|
2015-05-12 05:16:27 +08:00
|
|
|
void InitializeTraceState();
|
2016-01-14 07:02:30 +08:00
|
|
|
void AssignTaintLabels(uint8_t *Data, size_t Size);
|
2016-03-12 09:57:04 +08:00
|
|
|
void RereadOutputCorpus(size_t MaxSize);
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-02-05 07:42:42 +08:00
|
|
|
size_t secondsSinceProcessStartUp() {
|
|
|
|
return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
|
|
|
|
.count();
|
|
|
|
}
|
2016-02-27 06:42:23 +08:00
|
|
|
size_t execPerSec() {
|
|
|
|
size_t Seconds = secondsSinceProcessStartUp();
|
|
|
|
return Seconds ? TotalNumberOfRuns / Seconds : 0;
|
|
|
|
}
|
2015-02-05 07:42:42 +08:00
|
|
|
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
static void StaticAlarmCallback();
|
2016-03-02 06:19:21 +08:00
|
|
|
static void StaticCrashSignalCallback();
|
|
|
|
static void StaticInterruptCallback();
|
2015-04-01 04:13:20 +08:00
|
|
|
|
2016-02-14 01:56:51 +08:00
|
|
|
void ExecuteCallback(const uint8_t *Data, size_t Size);
|
2016-05-05 04:44:50 +08:00
|
|
|
bool RunOne(const uint8_t *Data, size_t Size);
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-10-24 09:16:40 +08:00
|
|
|
// Merge Corpora[1:] into Corpora[0].
|
|
|
|
void Merge(const std::vector<std::string> &Corpora);
|
2016-03-18 08:23:29 +08:00
|
|
|
// Returns a subset of 'Extra' that adds coverage to 'Initial'.
|
|
|
|
UnitVector FindExtraUnits(const UnitVector &Initial, const UnitVector &Extra);
|
2016-02-13 14:24:18 +08:00
|
|
|
MutationDispatcher &GetMD() { return MD; }
|
2016-02-27 06:42:23 +08:00
|
|
|
void PrintFinalStats();
|
2016-03-12 09:57:04 +08:00
|
|
|
void SetMaxLen(size_t MaxLen);
|
2016-05-07 07:38:07 +08:00
|
|
|
void RssLimitCallback();
|
2015-10-24 09:16:40 +08:00
|
|
|
|
2016-05-25 07:14:29 +08:00
|
|
|
// Public for tests.
|
|
|
|
void ResetCoverage();
|
|
|
|
|
2016-05-27 05:32:30 +08:00
|
|
|
bool InFuzzingThread() const { return IsMyThread; }
|
2016-05-27 06:17:32 +08:00
|
|
|
size_t GetCurrentUnitInFuzzingThead(const uint8_t **Data) const;
|
2016-05-27 05:32:30 +08:00
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
private:
|
2015-04-01 04:13:20 +08:00
|
|
|
void AlarmCallback();
|
2016-03-02 06:19:21 +08:00
|
|
|
void CrashCallback();
|
|
|
|
void InterruptCallback();
|
2015-12-19 10:49:09 +08:00
|
|
|
void MutateAndTestOne();
|
2016-09-22 06:42:17 +08:00
|
|
|
void ReportNewCoverage(InputInfo *II, const Unit &U);
|
2016-08-26 06:35:08 +08:00
|
|
|
void PrintNewPCs();
|
2016-09-15 12:36:45 +08:00
|
|
|
void PrintOneNewPC(uintptr_t PC);
|
2016-02-14 01:56:51 +08:00
|
|
|
bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
|
2015-01-30 00:58:29 +08:00
|
|
|
void WriteToOutputCorpus(const Unit &U);
|
2015-07-24 02:37:22 +08:00
|
|
|
void WriteUnitToFileWithPrefix(const Unit &U, const char *Prefix);
|
2015-10-23 06:56:45 +08:00
|
|
|
void PrintStats(const char *Where, const char *End = "\n");
|
2015-11-12 09:02:01 +08:00
|
|
|
void PrintStatusForNewUnit(const Unit &U);
|
2016-03-18 08:23:29 +08:00
|
|
|
void ShuffleCorpus(UnitVector *V);
|
2016-05-27 04:25:49 +08:00
|
|
|
void TryDetectingAMemoryLeak(const uint8_t *Data, size_t Size,
|
|
|
|
bool DuringInitialCorpusExecution);
|
2016-03-18 08:23:29 +08:00
|
|
|
|
2016-05-11 07:43:15 +08:00
|
|
|
bool UpdateMaxCoverage();
|
2015-10-23 06:50:47 +08:00
|
|
|
|
2015-05-08 05:02:11 +08: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 11:08:58 +08:00
|
|
|
// Stop tracing.
|
|
|
|
void StopTraceRecording();
|
2015-05-08 05:02:11 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
void SetDeathCallback();
|
2015-04-01 04:13:20 +08:00
|
|
|
static void StaticDeathCallback();
|
2016-03-02 06:19:21 +08:00
|
|
|
void DumpCurrentUnit(const char *Prefix);
|
2015-04-01 04:13:20 +08:00
|
|
|
void DeathCallback();
|
2016-01-14 07:46:01 +08:00
|
|
|
|
2016-08-25 09:25:03 +08:00
|
|
|
void ResetEdgeCoverage();
|
2016-09-09 10:38:28 +08:00
|
|
|
void ResetCounters();
|
2016-08-25 09:25:03 +08:00
|
|
|
void PrepareCounters(Fuzzer::Coverage *C);
|
|
|
|
bool RecordMaxCoverage(Fuzzer::Coverage *C);
|
|
|
|
|
2016-05-27 08:21:33 +08:00
|
|
|
void LazyAllocateCurrentUnitData();
|
|
|
|
uint8_t *CurrentUnitData = nullptr;
|
2016-05-27 08:54:15 +08:00
|
|
|
std::atomic<size_t> CurrentUnitSize;
|
2016-08-18 04:45:23 +08:00
|
|
|
uint8_t BaseSha1[kSHA1NumBytes]; // Checksum of the base unit.
|
2015-01-30 00:58:29 +08:00
|
|
|
|
|
|
|
size_t TotalNumberOfRuns = 0;
|
2016-02-27 06:42:23 +08:00
|
|
|
size_t NumberOfNewUnitsAdded = 0;
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2016-04-20 08:24:21 +08:00
|
|
|
bool HasMoreMallocsThanFrees = false;
|
2016-04-28 03:52:34 +08:00
|
|
|
size_t NumberOfLeakDetectionAttempts = 0;
|
2016-04-20 08:24:21 +08:00
|
|
|
|
2016-02-13 11:25:16 +08:00
|
|
|
UserCallback CB;
|
2016-09-22 06:42:17 +08:00
|
|
|
InputCorpus &Corpus;
|
2016-02-13 11:25:16 +08:00
|
|
|
MutationDispatcher &MD;
|
2015-01-30 00:58:29 +08:00
|
|
|
FuzzingOptions Options;
|
2016-09-22 06:42:17 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
2016-09-22 09:34:58 +08:00
|
|
|
system_clock::time_point UnitStartTime, UnitStopTime;
|
2015-03-31 07:04:35 +08:00
|
|
|
long TimeOfLongestUnitInSeconds = 0;
|
2015-05-09 05:30:55 +08:00
|
|
|
long EpochOfLastReadOfOutputCorpus = 0;
|
2016-05-11 07:43:15 +08:00
|
|
|
|
|
|
|
// Maximum recorded coverage.
|
|
|
|
Coverage MaxCoverage;
|
2016-08-25 09:25:03 +08:00
|
|
|
|
2016-08-26 06:35:08 +08:00
|
|
|
// For -print_pcs
|
2016-08-25 09:25:03 +08:00
|
|
|
uintptr_t* PcBuffer = nullptr;
|
|
|
|
size_t PcBufferLen = 0;
|
2016-09-09 10:38:28 +08:00
|
|
|
size_t PcBufferPos = 0, PrevPcBufferPos = 0;
|
2016-05-27 05:32:30 +08:00
|
|
|
|
|
|
|
// Need to know our own thread.
|
|
|
|
static thread_local bool IsMyThread;
|
2016-09-10 08:15:41 +08:00
|
|
|
|
|
|
|
bool InMergeMode = false;
|
2015-01-30 00:58:29 +08:00
|
|
|
};
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
}; // namespace fuzzer
|
2015-08-11 00:37:40 +08:00
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_INTERNAL_H
|