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 <cassert>
|
|
|
|
#include <chrono>
|
2016-01-23 06:28:27 +08:00
|
|
|
#include <climits>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2016-08-06 04:09:53 +08:00
|
|
|
#include <memory>
|
2016-01-23 06:28:27 +08:00
|
|
|
#include <random>
|
2016-01-16 11:53:32 +08:00
|
|
|
#include <string.h>
|
2016-01-23 06:28:27 +08:00
|
|
|
#include <string>
|
2015-01-30 07:01:07 +08:00
|
|
|
#include <unordered_set>
|
2016-01-23 06:28:27 +08:00
|
|
|
#include <vector>
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2016-06-02 13:48:02 +08:00
|
|
|
#include "FuzzerExtFunctions.h"
|
2015-02-20 02:45:37 +08:00
|
|
|
#include "FuzzerInterface.h"
|
2016-08-17 01:37:13 +08:00
|
|
|
#include "FuzzerValueBitMap.h"
|
2015-02-20 02:45:37 +08:00
|
|
|
|
2016-05-20 06:00:33 +08:00
|
|
|
// Platform detection.
|
|
|
|
#ifdef __linux__
|
|
|
|
#define LIBFUZZER_LINUX 1
|
|
|
|
#define LIBFUZZER_APPLE 0
|
|
|
|
#elif __APPLE__
|
|
|
|
#define LIBFUZZER_LINUX 0
|
|
|
|
#define LIBFUZZER_APPLE 1
|
|
|
|
#else
|
|
|
|
#error "Support for your platform has not been implemented"
|
|
|
|
#endif
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
2016-05-14 02:04:35 +08:00
|
|
|
|
|
|
|
typedef int (*UserCallback)(const uint8_t *Data, size_t Size);
|
2016-06-02 13:48:02 +08:00
|
|
|
int FuzzerDriver(int *argc, char ***argv, UserCallback Callback);
|
2016-05-14 02:04:35 +08:00
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
using namespace std::chrono;
|
2016-01-16 09:23:12 +08:00
|
|
|
typedef std::vector<uint8_t> Unit;
|
2016-03-18 08:23:29 +08:00
|
|
|
typedef std::vector<Unit> UnitVector;
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2016-01-16 11:53:32 +08:00
|
|
|
// A simple POD sized array of bytes.
|
2016-01-23 06:28:27 +08:00
|
|
|
template <size_t kMaxSize> class FixedWord {
|
|
|
|
public:
|
2016-01-23 07:55:14 +08:00
|
|
|
FixedWord() {}
|
2016-01-16 11:53:32 +08:00
|
|
|
FixedWord(const uint8_t *B, uint8_t S) { Set(B, S); }
|
|
|
|
|
|
|
|
void Set(const uint8_t *B, uint8_t S) {
|
|
|
|
assert(S <= kMaxSize);
|
|
|
|
memcpy(Data, B, S);
|
|
|
|
Size = S;
|
|
|
|
}
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
bool operator==(const FixedWord<kMaxSize> &w) const {
|
2016-01-16 11:53:32 +08:00
|
|
|
return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
|
|
|
|
}
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
bool operator<(const FixedWord<kMaxSize> &w) const {
|
|
|
|
if (Size != w.Size)
|
|
|
|
return Size < w.Size;
|
2016-01-16 11:53:32 +08:00
|
|
|
return memcmp(Data, w.Data, Size) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t GetMaxSize() { return kMaxSize; }
|
|
|
|
const uint8_t *data() const { return Data; }
|
|
|
|
uint8_t size() const { return Size; }
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
private:
|
2016-01-23 07:55:14 +08:00
|
|
|
uint8_t Size = 0;
|
2016-01-16 11:53:32 +08:00
|
|
|
uint8_t Data[kMaxSize];
|
|
|
|
};
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
typedef FixedWord<27> Word; // 28 bytes.
|
2016-01-16 11:53:32 +08:00
|
|
|
|
2016-02-02 11:03:47 +08:00
|
|
|
bool IsFile(const std::string &Path);
|
2015-04-01 04:13:20 +08:00
|
|
|
std::string FileToString(const std::string &Path);
|
2016-02-19 05:49:10 +08:00
|
|
|
Unit FileToVector(const std::string &Path, size_t MaxSize = 0);
|
2015-05-09 05:30:55 +08:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
2016-02-19 05:49:10 +08:00
|
|
|
long *Epoch, size_t MaxSize);
|
2015-01-30 00:58:29 +08:00
|
|
|
void WriteToFile(const Unit &U, const std::string &Path);
|
2015-02-05 03:10:20 +08:00
|
|
|
void CopyFileToErr(const std::string &Path);
|
2015-01-30 00:58:29 +08:00
|
|
|
// Returns "Dir/FileName" or equivalent for the current OS.
|
|
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
|
|
const std::string &FileName);
|
|
|
|
|
2016-03-19 04:58:29 +08:00
|
|
|
void DupAndCloseStderr();
|
|
|
|
void CloseStdout();
|
2015-05-23 09:07:46 +08:00
|
|
|
void Printf(const char *Fmt, ...);
|
2016-01-14 07:46:01 +08:00
|
|
|
void PrintHexArray(const Unit &U, const char *PrintAfter = "");
|
|
|
|
void PrintHexArray(const uint8_t *Data, size_t Size,
|
|
|
|
const char *PrintAfter = "");
|
2016-01-12 10:36:59 +08:00
|
|
|
void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter = "");
|
2015-01-30 00:58:29 +08:00
|
|
|
void PrintASCII(const Unit &U, const char *PrintAfter = "");
|
2016-01-16 11:53:32 +08:00
|
|
|
void PrintASCII(const Word &W, const char *PrintAfter = "");
|
2015-01-30 00:58:29 +08:00
|
|
|
std::string Hash(const Unit &U);
|
|
|
|
void SetTimer(int Seconds);
|
2016-03-02 06:19:21 +08:00
|
|
|
void SetSigSegvHandler();
|
|
|
|
void SetSigBusHandler();
|
|
|
|
void SetSigAbrtHandler();
|
|
|
|
void SetSigIllHandler();
|
|
|
|
void SetSigFpeHandler();
|
|
|
|
void SetSigIntHandler();
|
2016-03-25 05:03:58 +08:00
|
|
|
void SetSigTermHandler();
|
2015-12-05 06:29:39 +08:00
|
|
|
std::string Base64(const Unit &U);
|
2015-11-12 09:02:01 +08:00
|
|
|
int ExecuteCommand(const std::string &Command);
|
2016-02-27 06:42:23 +08:00
|
|
|
size_t GetPeakRSSMb();
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-05-15 06:41:49 +08:00
|
|
|
// Private copy of SHA1 implementation.
|
|
|
|
static const int kSHA1NumBytes = 20;
|
|
|
|
// Computes SHA1 hash of 'Len' bytes in 'Data', writes kSHA1NumBytes to 'Out'.
|
|
|
|
void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out);
|
|
|
|
|
2015-08-11 09:44:42 +08:00
|
|
|
// Changes U to contain only ASCII (isprint+isspace) characters.
|
|
|
|
// Returns true iff U has been changed.
|
2016-02-14 01:56:51 +08:00
|
|
|
bool ToASCII(uint8_t *Data, size_t Size);
|
2015-09-03 03:08:08 +08:00
|
|
|
bool IsASCII(const Unit &U);
|
2016-05-27 04:03:02 +08:00
|
|
|
bool IsASCII(const uint8_t *Data, size_t Size);
|
2015-08-11 09:44:42 +08:00
|
|
|
|
2015-05-13 02:51:57 +08:00
|
|
|
int NumberOfCpuCores();
|
2015-10-17 07:04:31 +08:00
|
|
|
int GetPid();
|
2016-05-07 07:38:07 +08:00
|
|
|
void SleepSeconds(int Seconds);
|
2015-05-13 02:51:57 +08:00
|
|
|
|
2016-08-17 01:37:13 +08:00
|
|
|
// See FuzzerTracePC.cpp
|
|
|
|
size_t PCMapMergeFromCurrent(ValueBitMap &M);
|
|
|
|
|
2016-08-17 03:33:51 +08:00
|
|
|
// See FuzzerTraceState.cpp
|
|
|
|
void EnableValueProfile();
|
|
|
|
size_t VPMapMergeFromCurrent(ValueBitMap &M);
|
|
|
|
|
2016-02-13 11:00:53 +08:00
|
|
|
class Random {
|
2016-02-13 10:39:30 +08:00
|
|
|
public:
|
2016-02-13 11:00:53 +08:00
|
|
|
Random(unsigned int seed) : R(seed) {}
|
|
|
|
size_t Rand() { return R(); }
|
|
|
|
size_t RandBool() { return Rand() % 2; }
|
2016-02-13 10:39:30 +08:00
|
|
|
size_t operator()(size_t n) { return n ? Rand() % n : 0; }
|
2016-02-13 11:25:16 +08:00
|
|
|
std::mt19937 &Get_mt19937() { return R; }
|
2016-02-13 10:39:30 +08:00
|
|
|
private:
|
2016-02-13 11:00:53 +08:00
|
|
|
std::mt19937 R;
|
2016-02-13 10:39:30 +08:00
|
|
|
};
|
|
|
|
|
2015-09-04 04:23:46 +08:00
|
|
|
// Dictionary.
|
|
|
|
|
|
|
|
// Parses one dictionary entry.
|
|
|
|
// If successfull, write the enty to Unit and returns true,
|
|
|
|
// otherwise returns false.
|
|
|
|
bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
|
|
|
|
// Parses the dictionary file, fills Units, returns true iff all lines
|
|
|
|
// were parsed succesfully.
|
|
|
|
bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
|
|
|
|
|
2016-02-13 11:37:24 +08:00
|
|
|
class DictionaryEntry {
|
|
|
|
public:
|
|
|
|
DictionaryEntry() {}
|
|
|
|
DictionaryEntry(Word W) : W(W) {}
|
|
|
|
DictionaryEntry(Word W, size_t PositionHint) : W(W), PositionHint(PositionHint) {}
|
|
|
|
const Word &GetW() const { return W; }
|
|
|
|
|
|
|
|
bool HasPositionHint() const { return PositionHint != std::numeric_limits<size_t>::max(); }
|
|
|
|
size_t GetPositionHint() const {
|
|
|
|
assert(HasPositionHint());
|
|
|
|
return PositionHint;
|
|
|
|
}
|
|
|
|
void IncUseCount() { UseCount++; }
|
|
|
|
void IncSuccessCount() { SuccessCount++; }
|
|
|
|
size_t GetUseCount() const { return UseCount; }
|
|
|
|
size_t GetSuccessCount() const {return SuccessCount; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Word W;
|
|
|
|
size_t PositionHint = std::numeric_limits<size_t>::max();
|
|
|
|
size_t UseCount = 0;
|
|
|
|
size_t SuccessCount = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Dictionary {
|
|
|
|
public:
|
|
|
|
static const size_t kMaxDictSize = 1 << 14;
|
|
|
|
|
|
|
|
bool ContainsWord(const Word &W) const {
|
|
|
|
return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
|
|
|
|
return DE.GetW() == W;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const DictionaryEntry *begin() const { return &DE[0]; }
|
|
|
|
const DictionaryEntry *end() const { return begin() + Size; }
|
|
|
|
DictionaryEntry & operator[] (size_t Idx) {
|
|
|
|
assert(Idx < Size);
|
|
|
|
return DE[Idx];
|
|
|
|
}
|
|
|
|
void push_back(DictionaryEntry DE) {
|
|
|
|
if (Size < kMaxDictSize)
|
|
|
|
this->DE[Size++] = DE;
|
|
|
|
}
|
|
|
|
void clear() { Size = 0; }
|
|
|
|
bool empty() const { return Size == 0; }
|
|
|
|
size_t size() const { return Size; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DictionaryEntry DE[kMaxDictSize];
|
|
|
|
size_t Size = 0;
|
|
|
|
};
|
|
|
|
|
2016-06-24 04:44:48 +08:00
|
|
|
struct FuzzingOptions {
|
|
|
|
int Verbosity = 1;
|
|
|
|
size_t MaxLen = 0;
|
|
|
|
int UnitTimeoutSec = 300;
|
|
|
|
int TimeoutExitCode = 77;
|
|
|
|
int ErrorExitCode = 77;
|
|
|
|
int MaxTotalTimeSec = 0;
|
|
|
|
int RssLimitMb = 0;
|
|
|
|
bool DoCrossOver = true;
|
|
|
|
int MutateDepth = 5;
|
|
|
|
bool UseCounters = false;
|
|
|
|
bool UseIndirCalls = true;
|
|
|
|
bool UseTraces = false;
|
|
|
|
bool UseMemcmp = true;
|
2016-07-16 07:27:19 +08:00
|
|
|
bool UseMemmem = true;
|
2016-06-24 04:44:48 +08:00
|
|
|
bool UseFullCoverageSet = false;
|
|
|
|
bool Reload = true;
|
|
|
|
bool ShuffleAtStartUp = true;
|
|
|
|
bool PreferSmall = true;
|
|
|
|
size_t MaxNumberOfRuns = ULONG_MAX;
|
|
|
|
int ReportSlowUnits = 10;
|
|
|
|
bool OnlyASCII = false;
|
|
|
|
std::string OutputCorpus;
|
|
|
|
std::string ArtifactPrefix = "./";
|
|
|
|
std::string ExactArtifactPath;
|
|
|
|
bool SaveArtifacts = true;
|
|
|
|
bool PrintNEW = true; // Print a status line when new units are found;
|
|
|
|
bool OutputCSV = false;
|
|
|
|
bool PrintNewCovPcs = false;
|
|
|
|
bool PrintFinalStats = false;
|
|
|
|
bool DetectLeaks = true;
|
|
|
|
bool TruncateUnits = false;
|
|
|
|
bool PruneCorpus = true;
|
|
|
|
};
|
|
|
|
|
2016-01-16 08:04:36 +08:00
|
|
|
class MutationDispatcher {
|
2016-01-23 06:28:27 +08:00
|
|
|
public:
|
2016-06-24 04:44:48 +08:00
|
|
|
MutationDispatcher(Random &Rand, const FuzzingOptions &Options);
|
2016-02-13 11:37:24 +08:00
|
|
|
~MutationDispatcher() {}
|
2016-01-16 08:04:36 +08:00
|
|
|
/// Indicate that we are about to start a new sequence of mutations.
|
|
|
|
void StartMutationSequence();
|
|
|
|
/// Print the current sequence of mutations.
|
|
|
|
void PrintMutationSequence();
|
|
|
|
/// Indicate that the current sequence of mutations was successfull.
|
|
|
|
void RecordSuccessfulMutationSequence();
|
2016-06-04 05:34:29 +08:00
|
|
|
/// Mutates data by invoking user-provided mutator.
|
|
|
|
size_t Mutate_Custom(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-06-08 04:22:15 +08:00
|
|
|
/// Mutates data by invoking user-provided crossover.
|
|
|
|
size_t Mutate_CustomCrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-01-16 08:04:36 +08:00
|
|
|
/// Mutates data by shuffling bytes.
|
|
|
|
size_t Mutate_ShuffleBytes(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-08-16 01:48:28 +08:00
|
|
|
/// Mutates data by erasing bytes.
|
|
|
|
size_t Mutate_EraseBytes(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-01-16 08:04:36 +08:00
|
|
|
/// Mutates data by inserting a byte.
|
|
|
|
size_t Mutate_InsertByte(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-08-16 01:48:28 +08:00
|
|
|
/// Mutates data by inserting several repeated bytes.
|
|
|
|
size_t Mutate_InsertRepeatedBytes(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-01-16 08:04:36 +08:00
|
|
|
/// Mutates data by chanding one byte.
|
|
|
|
size_t Mutate_ChangeByte(uint8_t *Data, size_t Size, size_t MaxSize);
|
|
|
|
/// Mutates data by chanding one bit.
|
|
|
|
size_t Mutate_ChangeBit(uint8_t *Data, size_t Size, size_t MaxSize);
|
|
|
|
|
|
|
|
/// Mutates data by adding a word from the manual dictionary.
|
|
|
|
size_t Mutate_AddWordFromManualDictionary(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize);
|
|
|
|
|
|
|
|
/// Mutates data by adding a word from the temporary automatic dictionary.
|
|
|
|
size_t Mutate_AddWordFromTemporaryAutoDictionary(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize);
|
|
|
|
|
|
|
|
/// Mutates data by adding a word from the persistent automatic dictionary.
|
|
|
|
size_t Mutate_AddWordFromPersistentAutoDictionary(uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize);
|
|
|
|
|
|
|
|
/// Tries to find an ASCII integer in Data, changes it to another ASCII int.
|
|
|
|
size_t Mutate_ChangeASCIIInteger(uint8_t *Data, size_t Size, size_t MaxSize);
|
|
|
|
|
|
|
|
/// CrossOver Data with some other element of the corpus.
|
|
|
|
size_t Mutate_CrossOver(uint8_t *Data, size_t Size, size_t MaxSize);
|
|
|
|
|
2016-06-04 05:34:29 +08:00
|
|
|
/// Applies one of the configured mutations.
|
2016-01-16 08:04:36 +08:00
|
|
|
/// Returns the new size of data which could be up to MaxSize.
|
|
|
|
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-06-04 05:34:29 +08:00
|
|
|
/// Applies one of the default mutations. Provided as a service
|
|
|
|
/// to mutation authors.
|
|
|
|
size_t DefaultMutate(uint8_t *Data, size_t Size, size_t MaxSize);
|
2016-01-16 08:04:36 +08:00
|
|
|
|
|
|
|
/// Creates a cross-over of two pieces of Data, returns its size.
|
|
|
|
size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
|
|
|
|
size_t Size2, uint8_t *Out, size_t MaxOutSize);
|
|
|
|
|
2016-01-16 11:53:32 +08:00
|
|
|
void AddWordToManualDictionary(const Word &W);
|
2016-01-16 08:04:36 +08:00
|
|
|
|
2016-07-16 07:27:19 +08:00
|
|
|
void AddWordToAutoDictionary(DictionaryEntry DE);
|
2016-01-16 08:04:36 +08:00
|
|
|
void ClearAutoDictionary();
|
|
|
|
void PrintRecommendedDictionary();
|
|
|
|
|
2016-02-13 11:37:24 +08:00
|
|
|
void SetCorpus(const std::vector<Unit> *Corpus) { this->Corpus = Corpus; }
|
2016-01-16 08:04:36 +08:00
|
|
|
|
2016-02-13 11:25:16 +08:00
|
|
|
Random &GetRand() { return Rand; }
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
private:
|
2016-02-13 11:37:24 +08:00
|
|
|
|
|
|
|
struct Mutator {
|
|
|
|
size_t (MutationDispatcher::*Fn)(uint8_t *Data, size_t Size, size_t Max);
|
|
|
|
const char *Name;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t AddWordFromDictionary(Dictionary &D, uint8_t *Data, size_t Size,
|
|
|
|
size_t MaxSize);
|
2016-06-04 05:34:29 +08:00
|
|
|
size_t MutateImpl(uint8_t *Data, size_t Size, size_t MaxSize,
|
|
|
|
const std::vector<Mutator> &Mutators);
|
|
|
|
|
2016-02-13 11:00:53 +08:00
|
|
|
Random &Rand;
|
2016-06-24 04:44:48 +08:00
|
|
|
const FuzzingOptions Options;
|
|
|
|
|
2016-02-13 11:37:24 +08:00
|
|
|
// Dictionary provided by the user via -dict=DICT_FILE.
|
|
|
|
Dictionary ManualDictionary;
|
|
|
|
// Temporary dictionary modified by the fuzzer itself,
|
|
|
|
// recreated periodically.
|
|
|
|
Dictionary TempAutoDictionary;
|
|
|
|
// Persistent dictionary modified by the fuzzer, consists of
|
|
|
|
// entries that led to successfull discoveries in the past mutations.
|
|
|
|
Dictionary PersistentAutoDictionary;
|
|
|
|
std::vector<Mutator> CurrentMutatorSequence;
|
|
|
|
std::vector<DictionaryEntry *> CurrentDictionaryEntrySequence;
|
|
|
|
const std::vector<Unit> *Corpus = nullptr;
|
2016-02-14 01:56:51 +08:00
|
|
|
std::vector<uint8_t> MutateInPlaceHere;
|
2016-02-13 11:46:26 +08:00
|
|
|
|
2016-06-04 05:34:29 +08:00
|
|
|
std::vector<Mutator> Mutators;
|
|
|
|
std::vector<Mutator> DefaultMutators;
|
2016-01-16 08:04:36 +08:00
|
|
|
};
|
|
|
|
|
2016-08-06 04:09:53 +08:00
|
|
|
class CoverageController;
|
|
|
|
|
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();
|
|
|
|
PCMap.Reset();
|
2016-08-17 01:37:13 +08:00
|
|
|
PCMapBits = 0;
|
2016-08-17 03:33:51 +08:00
|
|
|
VPMap.Reset();
|
|
|
|
VPMapBits = 0;
|
2016-08-06 04:09:53 +08:00
|
|
|
PcBufferPos = 0;
|
2016-05-11 07:43:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DebugString() const;
|
|
|
|
|
|
|
|
size_t BlockCoverage;
|
|
|
|
size_t CallerCalleeCoverage;
|
2016-08-06 04:09:53 +08:00
|
|
|
size_t PcBufferPos;
|
2016-05-11 07:43:15 +08:00
|
|
|
// Precalculated number of bits in CounterBitmap.
|
|
|
|
size_t CounterBitmapBits;
|
|
|
|
std::vector<uint8_t> CounterBitmap;
|
2016-08-17 01:37:13 +08:00
|
|
|
ValueBitMap PCMap;
|
|
|
|
size_t PCMapBits;
|
2016-08-17 03:33:51 +08:00
|
|
|
ValueBitMap VPMap;
|
|
|
|
size_t VPMapBits;
|
2016-05-11 07:43:15 +08:00
|
|
|
};
|
|
|
|
|
2016-02-13 11:25:16 +08:00
|
|
|
Fuzzer(UserCallback CB, MutationDispatcher &MD, FuzzingOptions Options);
|
2016-08-06 04:09:53 +08:00
|
|
|
~Fuzzer();
|
2016-01-23 06:28:27 +08:00
|
|
|
void AddToCorpus(const Unit &U) {
|
|
|
|
Corpus.push_back(U);
|
|
|
|
UpdateCorpusDistribution();
|
|
|
|
}
|
2015-11-12 09:02:01 +08:00
|
|
|
size_t ChooseUnitIdxToMutate();
|
|
|
|
const Unit &ChooseUnitToMutate() { return Corpus[ChooseUnitIdxToMutate()]; };
|
2016-05-25 07:14:29 +08:00
|
|
|
void TruncateUnits(std::vector<Unit> *NewCorpus);
|
2015-09-09 01:30:35 +08:00
|
|
|
void Loop();
|
2015-11-12 09:02:01 +08:00
|
|
|
void Drill();
|
2015-01-30 00:58:29 +08:00
|
|
|
void ShuffleAndMinimize();
|
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);
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t CorpusSize() const { return Corpus.size(); }
|
2016-03-12 09:57:04 +08:00
|
|
|
size_t MaxUnitSizeInCorpus() const;
|
2016-02-19 05:49:10 +08:00
|
|
|
void ReadDir(const std::string &Path, long *Epoch, size_t MaxSize) {
|
2015-10-24 09:16:40 +08:00
|
|
|
Printf("Loading corpus: %s\n", Path.c_str());
|
2016-02-19 05:49:10 +08:00
|
|
|
ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch, MaxSize);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
2016-03-12 09:57:04 +08:00
|
|
|
void RereadOutputCorpus(size_t MaxSize);
|
2015-01-30 00:58:29 +08:00
|
|
|
// Save the current corpus to OutputCorpus.
|
|
|
|
void SaveCorpus();
|
|
|
|
|
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();
|
2015-10-23 06:50:47 +08:00
|
|
|
void ReportNewCoverage(const Unit &U);
|
2016-02-14 01:56:51 +08:00
|
|
|
bool RunOne(const Unit &U) { return RunOne(U.data(), U.size()); }
|
2016-05-27 04:03:02 +08:00
|
|
|
void RunOneAndUpdateCorpus(const uint8_t *Data, size_t 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-01-23 06:28:27 +08:00
|
|
|
// Updates the probability distribution for the units in the corpus.
|
|
|
|
// Must be called whenever the corpus or unit weights are changed.
|
|
|
|
void UpdateCorpusDistribution();
|
2015-01-30 00:58: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-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;
|
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
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
std::vector<Unit> Corpus;
|
2015-05-19 09:06:07 +08:00
|
|
|
std::unordered_set<std::string> UnitHashesAddedToCorpus;
|
2016-02-14 01:56:51 +08:00
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
std::piecewise_constant_distribution<double> CorpusDistribution;
|
2016-02-13 11:25:16 +08:00
|
|
|
UserCallback CB;
|
|
|
|
MutationDispatcher &MD;
|
2015-01-30 00:58:29 +08:00
|
|
|
FuzzingOptions Options;
|
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
2015-04-01 04:13:20 +08:00
|
|
|
system_clock::time_point UnitStartTime;
|
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-06 04:09:53 +08:00
|
|
|
std::unique_ptr<CoverageController> CController;
|
2016-05-27 05:32:30 +08:00
|
|
|
|
|
|
|
// Need to know our own thread.
|
|
|
|
static thread_local bool IsMyThread;
|
2015-01-30 00:58:29 +08:00
|
|
|
};
|
|
|
|
|
2016-06-08 07:32:50 +08:00
|
|
|
// Global interface to functions that may or may not be available.
|
|
|
|
extern ExternalFunctions *EF;
|
|
|
|
|
2016-01-23 06:28:27 +08:00
|
|
|
}; // namespace fuzzer
|
2015-08-11 00:37:40 +08:00
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_INTERNAL_H
|