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.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include <cassert>
|
2015-02-04 22:20:09 +00:00
|
|
|
#include <climits>
|
2015-01-29 16:58:29 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-01-29 23:01:07 +00:00
|
|
|
#include <unordered_set>
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-02-19 18:45:37 +00:00
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
namespace fuzzer {
|
|
|
|
typedef std::vector<uint8_t> Unit;
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
2015-03-31 20:13:20 +00:00
|
|
|
std::string FileToString(const std::string &Path);
|
|
|
|
Unit FileToVector(const std::string &Path);
|
2015-05-08 21:30:55 +00:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
|
|
|
long *Epoch);
|
2015-01-29 16:58:29 +00:00
|
|
|
void WriteToFile(const Unit &U, const std::string &Path);
|
2015-02-04 19:10:20 +00:00
|
|
|
void CopyFileToErr(const std::string &Path);
|
2015-01-29 16:58:29 +00:00
|
|
|
// Returns "Dir/FileName" or equivalent for the current OS.
|
|
|
|
std::string DirPlusFile(const std::string &DirPath,
|
|
|
|
const std::string &FileName);
|
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
size_t Mutate(uint8_t *Data, size_t Size, size_t MaxSize);
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
size_t CrossOver(const uint8_t *Data1, size_t Size1, const uint8_t *Data2,
|
|
|
|
size_t Size2, uint8_t *Out, size_t MaxOutSize);
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-05-23 01:07:46 +00:00
|
|
|
void Printf(const char *Fmt, ...);
|
2015-01-29 16:58:29 +00:00
|
|
|
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);
|
2015-05-05 21:59:51 +00:00
|
|
|
void PrintFileAsBase64(const std::string &Path);
|
2015-05-18 21:34:20 +00:00
|
|
|
void ExecuteCommand(const std::string &Command);
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-05-14 22:41:49 +00: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-05-12 18:51:57 +00:00
|
|
|
int NumberOfCpuCores();
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
class Fuzzer {
|
|
|
|
public:
|
|
|
|
struct FuzzingOptions {
|
|
|
|
int Verbosity = 1;
|
|
|
|
int MaxLen = 0;
|
2015-05-19 22:12:57 +00:00
|
|
|
int UnitTimeoutSec = 300;
|
2015-01-29 16:58:29 +00:00
|
|
|
bool DoCrossOver = true;
|
2015-02-04 19:10:20 +00:00
|
|
|
int MutateDepth = 5;
|
2015-01-29 16:58:29 +00:00
|
|
|
bool ExitOnFirst = false;
|
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
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
2015-03-03 23:27:02 +00:00
|
|
|
bool UseCounters = false;
|
2015-05-11 20:51:19 +00:00
|
|
|
bool UseTraces = false;
|
2015-01-29 23:01:07 +00:00
|
|
|
bool UseFullCoverageSet = false;
|
2015-05-08 21:30:55 +00:00
|
|
|
bool Reload = true;
|
2015-02-04 23:42:42 +00:00
|
|
|
int PreferSmallDuringInitialShuffle = -1;
|
2015-02-04 22:20:09 +00:00
|
|
|
size_t MaxNumberOfRuns = ULONG_MAX;
|
2015-05-18 21:34:20 +00:00
|
|
|
int SyncTimeout = 600;
|
2015-01-29 16:58:29 +00:00
|
|
|
std::string OutputCorpus;
|
2015-05-18 21:34:20 +00:00
|
|
|
std::string SyncCommand;
|
2015-03-31 20:13:20 +00:00
|
|
|
std::vector<std::string> Tokens;
|
2015-01-29 16:58:29 +00:00
|
|
|
};
|
2015-05-22 22:35:31 +00:00
|
|
|
Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
|
2015-01-29 16:58:29 +00:00
|
|
|
void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
|
2015-05-07 18:32:29 +00:00
|
|
|
void Loop(size_t NumIterations);
|
2015-01-29 16:58:29 +00:00
|
|
|
void ShuffleAndMinimize();
|
2015-05-11 21:16:27 +00:00
|
|
|
void InitializeTraceState();
|
2015-01-29 16:58:29 +00:00
|
|
|
size_t CorpusSize() const { return Corpus.size(); }
|
2015-05-08 21:30:55 +00:00
|
|
|
void ReadDir(const std::string &Path, long *Epoch) {
|
|
|
|
ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
|
2015-01-29 16:58:29 +00:00
|
|
|
}
|
2015-05-08 21:30:55 +00:00
|
|
|
void RereadOutputCorpus();
|
2015-01-29 16:58:29 +00:00
|
|
|
// Save the current corpus to OutputCorpus.
|
|
|
|
void SaveCorpus();
|
|
|
|
|
2015-02-04 23:42:42 +00:00
|
|
|
size_t secondsSinceProcessStartUp() {
|
|
|
|
return duration_cast<seconds>(system_clock::now() - ProcessStartTime)
|
|
|
|
.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
|
2015-03-31 20:13:20 +00:00
|
|
|
static void StaticAlarmCallback();
|
|
|
|
|
|
|
|
Unit SubstituteTokens(const Unit &U) const;
|
2015-01-29 16:58:29 +00:00
|
|
|
|
|
|
|
private:
|
2015-03-31 20:13:20 +00:00
|
|
|
void AlarmCallback();
|
|
|
|
void ExecuteCallback(const Unit &U);
|
2015-05-07 18:32:29 +00:00
|
|
|
void MutateAndTestOne(Unit *U);
|
|
|
|
void ReportNewCoverage(size_t NewCoverage, const Unit &U);
|
2015-01-29 16:58:29 +00:00
|
|
|
size_t RunOne(const Unit &U);
|
2015-05-07 18:32:29 +00:00
|
|
|
void RunOneAndUpdateCorpus(const Unit &U);
|
2015-01-29 23:01:07 +00:00
|
|
|
size_t RunOneMaximizeTotalCoverage(const Unit &U);
|
|
|
|
size_t RunOneMaximizeFullCoverageSet(const Unit &U);
|
2015-02-20 03:02:37 +00:00
|
|
|
size_t RunOneMaximizeCoveragePairs(const Unit &U);
|
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);
|
2015-03-30 22:44:03 +00:00
|
|
|
void PrintStats(const char *Where, size_t Cov, const char *End = "\n");
|
2015-03-31 20:13:20 +00:00
|
|
|
void PrintUnitInASCIIOrTokens(const Unit &U, const char *PrintAfter = "");
|
2015-01-29 16:58:29 +00:00
|
|
|
|
2015-05-18 21:34:20 +00:00
|
|
|
void SyncCorpus();
|
|
|
|
|
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();
|
|
|
|
// Stop tracing and return the number of proposed mutations.
|
|
|
|
size_t StopTraceRecording();
|
|
|
|
// Apply Idx-th trace-based mutation to U.
|
|
|
|
void ApplyTraceBasedMutation(size_t Idx, Unit *U);
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
void SetDeathCallback();
|
2015-03-31 20:13:20 +00:00
|
|
|
static void StaticDeathCallback();
|
|
|
|
void DeathCallback();
|
|
|
|
Unit CurrentUnit;
|
2015-01-29 16:58:29 +00:00
|
|
|
|
|
|
|
size_t TotalNumberOfRuns = 0;
|
|
|
|
|
|
|
|
std::vector<Unit> Corpus;
|
2015-05-19 01:06:07 +00:00
|
|
|
std::unordered_set<std::string> UnitHashesAddedToCorpus;
|
2015-01-29 23:01:07 +00:00
|
|
|
std::unordered_set<uintptr_t> FullCoverageSets;
|
[sanitizer/coverage] Add AFL-style coverage counters (search heuristic for fuzzing).
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
2015-03-03 23:27:02 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
UserSuppliedFuzzer &USF;
|
2015-01-29 16:58:29 +00:00
|
|
|
FuzzingOptions Options;
|
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
2015-05-18 21:34:20 +00:00
|
|
|
system_clock::time_point LastExternalSync = system_clock::now();
|
2015-03-31 20:13:20 +00:00
|
|
|
system_clock::time_point UnitStartTime;
|
2015-03-30 23:04:35 +00:00
|
|
|
long TimeOfLongestUnitInSeconds = 0;
|
2015-05-08 21:30:55 +00:00
|
|
|
long EpochOfLastReadOfOutputCorpus = 0;
|
2015-01-29 16:58:29 +00:00
|
|
|
};
|
|
|
|
|
2015-05-22 22:35:31 +00:00
|
|
|
class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
|
|
|
|
public:
|
|
|
|
SimpleUserSuppliedFuzzer(UserCallback Callback) : Callback(Callback) {}
|
|
|
|
virtual void TargetFunction(const uint8_t *Data, size_t Size) {
|
|
|
|
return Callback(Data, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UserCallback Callback;
|
|
|
|
};
|
|
|
|
|
2015-01-29 16:58:29 +00:00
|
|
|
}; // namespace fuzzer
|