mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-25 00:29:20 +00:00
Introduce -mllvm -sanitizer-coverage-8bit-counters=1 which adds imprecise thread-unfriendly 8-bit coverage counters. The run-time library maps these 8-bit counters to 8-bit bitsets in the same way AFL (http://lcamtuf.coredump.cx/afl/technical_details.txt) does: counter values are divided into 8 ranges and based on the counter value one of the bits in the bitset is set. The AFL ranges are used here: 1, 2, 3, 4-7, 8-15, 16-31, 32-127, 128+. These counters provide a search heuristic for single-threaded coverage-guided fuzzers, we do not expect them to be useful for other purposes. Depending on the value of -fsanitize-coverage=[123] flag, these counters will be added to the function entry blocks (=1), every basic block (=2), or every edge (=3). Use these counters as an optional search heuristic in the Fuzzer library. Add a test where this heuristic is critical. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@231166 91177308-0d34-0410-b5e6-96231b3b80d8
115 lines
3.5 KiB
C++
115 lines
3.5 KiB
C++
//===- 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.
|
|
//===----------------------------------------------------------------------===//
|
|
#include <cassert>
|
|
#include <climits>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdlib>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <unordered_set>
|
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
namespace fuzzer {
|
|
typedef std::vector<uint8_t> Unit;
|
|
using namespace std::chrono;
|
|
|
|
Unit ReadFile(const char *Path);
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V);
|
|
void WriteToFile(const Unit &U, const std::string &Path);
|
|
void CopyFileToErr(const std::string &Path);
|
|
// Returns "Dir/FileName" or equivalent for the current OS.
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
const std::string &FileName);
|
|
|
|
void Mutate(Unit *U, size_t MaxLen);
|
|
|
|
void CrossOver(const Unit &A, const Unit &B, Unit *U, size_t MaxLen);
|
|
|
|
void Print(const Unit &U, const char *PrintAfter = "");
|
|
void PrintASCII(const Unit &U, const char *PrintAfter = "");
|
|
std::string Hash(const Unit &U);
|
|
void SetTimer(int Seconds);
|
|
|
|
class Fuzzer {
|
|
public:
|
|
struct FuzzingOptions {
|
|
int Verbosity = 1;
|
|
int MaxLen = 0;
|
|
bool DoCrossOver = true;
|
|
int MutateDepth = 5;
|
|
bool ExitOnFirst = false;
|
|
bool UseCounters = false;
|
|
bool UseFullCoverageSet = false;
|
|
bool UseCoveragePairs = false;
|
|
int PreferSmallDuringInitialShuffle = -1;
|
|
size_t MaxNumberOfRuns = ULONG_MAX;
|
|
std::string OutputCorpus;
|
|
};
|
|
Fuzzer(UserCallback Callback, FuzzingOptions Options)
|
|
: Callback(Callback), Options(Options) {
|
|
SetDeathCallback();
|
|
}
|
|
void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
|
|
size_t Loop(size_t NumIterations);
|
|
void ShuffleAndMinimize();
|
|
size_t CorpusSize() const { return Corpus.size(); }
|
|
void ReadDir(const std::string &Path) {
|
|
ReadDirToVectorOfUnits(Path.c_str(), &Corpus);
|
|
}
|
|
// Save the current corpus to OutputCorpus.
|
|
void SaveCorpus();
|
|
|
|
size_t secondsSinceProcessStartUp() {
|
|
return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
|
|
.count();
|
|
}
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
static void AlarmCallback();
|
|
|
|
private:
|
|
size_t MutateAndTestOne(Unit *U);
|
|
size_t RunOne(const Unit &U);
|
|
size_t RunOneMaximizeTotalCoverage(const Unit &U);
|
|
size_t RunOneMaximizeFullCoverageSet(const Unit &U);
|
|
size_t RunOneMaximizeCoveragePairs(const Unit &U);
|
|
void WriteToOutputCorpus(const Unit &U);
|
|
static void WriteToCrash(const Unit &U, const char *Prefix);
|
|
|
|
void SetDeathCallback();
|
|
static void DeathCallback();
|
|
static Unit CurrentUnit;
|
|
|
|
size_t TotalNumberOfRuns = 0;
|
|
|
|
std::vector<Unit> Corpus;
|
|
std::unordered_set<uintptr_t> FullCoverageSets;
|
|
std::unordered_set<uint64_t> CoveragePairs;
|
|
|
|
// For UseCounters
|
|
std::vector<uint8_t> CounterBitmap;
|
|
size_t TotalBits() { // Slow. Call it only for printing stats.
|
|
size_t Res = 0;
|
|
for (auto x : CounterBitmap) Res += __builtin_popcount(x);
|
|
return Res;
|
|
}
|
|
|
|
UserCallback Callback;
|
|
FuzzingOptions Options;
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
|
static system_clock::time_point UnitStartTime;
|
|
};
|
|
|
|
}; // namespace fuzzer
|