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
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <cassert>
|
2015-02-05 06:20:09 +08:00
|
|
|
#include <climits>
|
2015-01-30 00:58:29 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-01-30 07:01:07 +08:00
|
|
|
#include <unordered_set>
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-02-20 02:45:37 +08:00
|
|
|
#include "FuzzerInterface.h"
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
namespace fuzzer {
|
|
|
|
typedef std::vector<uint8_t> Unit;
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
std::string FileToString(const std::string &Path);
|
|
|
|
Unit FileToVector(const std::string &Path);
|
2015-05-09 05:30:55 +08:00
|
|
|
void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V,
|
|
|
|
long *Epoch);
|
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);
|
|
|
|
|
2015-05-23 09:07:46 +08:00
|
|
|
void Printf(const char *Fmt, ...);
|
2015-01-30 00:58:29 +08: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-06 05:59:51 +08:00
|
|
|
void PrintFileAsBase64(const std::string &Path);
|
2015-05-19 05:34:20 +08:00
|
|
|
void ExecuteCommand(const std::string &Command);
|
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.
|
|
|
|
bool ToASCII(Unit &U);
|
2015-09-03 03:08:08 +08:00
|
|
|
bool IsASCII(const Unit &U);
|
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();
|
2015-05-13 02:51:57 +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);
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
class Fuzzer {
|
|
|
|
public:
|
|
|
|
struct FuzzingOptions {
|
|
|
|
int Verbosity = 1;
|
|
|
|
int MaxLen = 0;
|
2015-05-20 06:12:57 +08:00
|
|
|
int UnitTimeoutSec = 300;
|
2015-10-03 04:47:55 +08:00
|
|
|
int MaxTotalTimeSec = 0;
|
2015-01-30 00:58:29 +08:00
|
|
|
bool DoCrossOver = true;
|
2015-02-05 03:10:20 +08:00
|
|
|
int MutateDepth = 5;
|
2015-01-30 00:58:29 +08: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.
llvm-svn: 231166
2015-03-04 07:27:02 +08:00
|
|
|
bool UseCounters = false;
|
2015-10-23 07:55:39 +08:00
|
|
|
bool UseIndirCalls = true;
|
2015-05-12 04:51:19 +08:00
|
|
|
bool UseTraces = false;
|
2015-01-30 07:01:07 +08:00
|
|
|
bool UseFullCoverageSet = false;
|
2015-05-09 05:30:55 +08:00
|
|
|
bool Reload = true;
|
2015-10-17 12:38:26 +08:00
|
|
|
bool ShuffleAtStartUp = true;
|
2015-02-05 07:42:42 +08:00
|
|
|
int PreferSmallDuringInitialShuffle = -1;
|
2015-02-05 06:20:09 +08:00
|
|
|
size_t MaxNumberOfRuns = ULONG_MAX;
|
2015-05-19 05:34:20 +08:00
|
|
|
int SyncTimeout = 600;
|
2015-08-06 05:43:48 +08:00
|
|
|
int ReportSlowUnits = 10;
|
2015-08-11 09:44:42 +08:00
|
|
|
bool OnlyASCII = false;
|
2015-08-12 09:55:37 +08:00
|
|
|
int TBMDepth = 10;
|
|
|
|
int TBMWidth = 10;
|
2015-01-30 00:58:29 +08:00
|
|
|
std::string OutputCorpus;
|
2015-05-19 05:34:20 +08:00
|
|
|
std::string SyncCommand;
|
2015-10-09 11:57:59 +08:00
|
|
|
std::string ArtifactPrefix = "./";
|
2015-09-04 04:23:46 +08:00
|
|
|
std::vector<Unit> Dictionary;
|
2015-10-17 06:41:47 +08:00
|
|
|
bool SaveArtifacts = true;
|
2015-01-30 00:58:29 +08:00
|
|
|
};
|
2015-05-23 06:35:31 +08:00
|
|
|
Fuzzer(UserSuppliedFuzzer &USF, FuzzingOptions Options);
|
2015-01-30 00:58:29 +08:00
|
|
|
void AddToCorpus(const Unit &U) { Corpus.push_back(U); }
|
2015-09-09 01:30:35 +08:00
|
|
|
void Loop();
|
2015-01-30 00:58:29 +08:00
|
|
|
void ShuffleAndMinimize();
|
2015-05-12 05:16:27 +08:00
|
|
|
void InitializeTraceState();
|
2015-01-30 00:58:29 +08:00
|
|
|
size_t CorpusSize() const { return Corpus.size(); }
|
2015-05-09 05:30:55 +08:00
|
|
|
void ReadDir(const std::string &Path, long *Epoch) {
|
2015-10-24 09:16:40 +08:00
|
|
|
Printf("Loading corpus: %s\n", Path.c_str());
|
2015-05-09 05:30:55 +08:00
|
|
|
ReadDirToVectorOfUnits(Path.c_str(), &Corpus, Epoch);
|
2015-01-30 00:58:29 +08:00
|
|
|
}
|
2015-05-09 05:30:55 +08:00
|
|
|
void RereadOutputCorpus();
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getTotalNumberOfRuns() { return TotalNumberOfRuns; }
|
|
|
|
|
2015-04-01 04:13:20 +08:00
|
|
|
static void StaticAlarmCallback();
|
|
|
|
|
2015-10-02 07:23:06 +08:00
|
|
|
void ExecuteCallback(const Unit &U);
|
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);
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
private:
|
2015-04-01 04:13:20 +08:00
|
|
|
void AlarmCallback();
|
2015-05-08 02:32:29 +08:00
|
|
|
void MutateAndTestOne(Unit *U);
|
2015-10-23 06:50:47 +08:00
|
|
|
void ReportNewCoverage(const Unit &U);
|
|
|
|
bool RunOne(const Unit &U);
|
2015-08-11 09:44:42 +08:00
|
|
|
void RunOneAndUpdateCorpus(Unit &U);
|
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-10-23 05:48:09 +08:00
|
|
|
void PrintUnitInASCII(const Unit &U, const char *PrintAfter = "");
|
2015-01-30 00:58:29 +08:00
|
|
|
|
2015-05-19 05:34:20 +08:00
|
|
|
void SyncCorpus();
|
|
|
|
|
2015-10-23 06:50:47 +08:00
|
|
|
size_t RecordBlockCoverage();
|
2015-10-23 07:55:39 +08:00
|
|
|
size_t RecordCallerCalleeCoverage();
|
2015-10-23 06:50:47 +08:00
|
|
|
void PrepareCoverageBeforeRun();
|
|
|
|
bool CheckCoverageAfterRun();
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
// 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-30 00:58:29 +08:00
|
|
|
void SetDeathCallback();
|
2015-04-01 04:13:20 +08:00
|
|
|
static void StaticDeathCallback();
|
|
|
|
void DeathCallback();
|
|
|
|
Unit CurrentUnit;
|
2015-01-30 00:58:29 +08:00
|
|
|
|
|
|
|
size_t TotalNumberOfRuns = 0;
|
2015-08-12 09:55:37 +08:00
|
|
|
size_t TotalNumberOfExecutedTraceBasedMutations = 0;
|
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;
|
[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.
llvm-svn: 231166
2015-03-04 07:27:02 +08: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-23 06:35:31 +08:00
|
|
|
UserSuppliedFuzzer &USF;
|
2015-01-30 00:58:29 +08:00
|
|
|
FuzzingOptions Options;
|
|
|
|
system_clock::time_point ProcessStartTime = system_clock::now();
|
2015-05-19 05:34:20 +08:00
|
|
|
system_clock::time_point LastExternalSync = 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;
|
2015-10-23 06:50:47 +08:00
|
|
|
size_t LastRecordedBlockCoverage = 0;
|
2015-10-23 07:55:39 +08:00
|
|
|
size_t LastRecordedCallerCalleeCoverage = 0;
|
2015-01-30 00:58:29 +08:00
|
|
|
};
|
|
|
|
|
2015-05-23 06:35:31 +08:00
|
|
|
class SimpleUserSuppliedFuzzer: public UserSuppliedFuzzer {
|
|
|
|
public:
|
2015-07-24 09:06:40 +08:00
|
|
|
SimpleUserSuppliedFuzzer(FuzzerRandomBase *Rand, UserCallback Callback)
|
|
|
|
: UserSuppliedFuzzer(Rand), Callback(Callback) {}
|
2015-10-03 07:34:06 +08:00
|
|
|
|
|
|
|
virtual int TargetFunction(const uint8_t *Data, size_t Size) override {
|
2015-10-24 02:37:58 +08:00
|
|
|
return Callback(Data, Size);
|
2015-05-23 06:35:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-03 07:34:06 +08:00
|
|
|
UserCallback Callback = nullptr;
|
2015-05-23 06:35:31 +08:00
|
|
|
};
|
|
|
|
|
2015-01-30 00:58:29 +08:00
|
|
|
}; // namespace fuzzer
|
2015-08-11 00:37:40 +08:00
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_INTERNAL_H
|