2017-08-22 07:25:50 +08:00
|
|
|
//===- FuzzerTracePC.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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// fuzzer::TracePC
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_FUZZER_TRACE_PC
|
|
|
|
#define LLVM_FUZZER_TRACE_PC
|
|
|
|
|
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
#include "FuzzerDictionary.h"
|
|
|
|
#include "FuzzerValueBitMap.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
|
|
|
// TableOfRecentCompares (TORC) remembers the most recently performed
|
|
|
|
// comparisons of type T.
|
|
|
|
// We record the arguments of CMP instructions in this table unconditionally
|
|
|
|
// because it seems cheaper this way than to compute some expensive
|
|
|
|
// conditions inside __sanitizer_cov_trace_cmp*.
|
|
|
|
// After the unit has been executed we may decide to use the contents of
|
|
|
|
// this table to populate a Dictionary.
|
|
|
|
template<class T, size_t kSizeT>
|
|
|
|
struct TableOfRecentCompares {
|
|
|
|
static const size_t kSize = kSizeT;
|
|
|
|
struct Pair {
|
|
|
|
T A, B;
|
|
|
|
};
|
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
|
|
|
void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
|
|
|
|
Idx = Idx % kSize;
|
|
|
|
Table[Idx].A = Arg1;
|
|
|
|
Table[Idx].B = Arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pair Get(size_t I) { return Table[I % kSize]; }
|
|
|
|
|
|
|
|
Pair Table[kSize];
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t kSizeT>
|
|
|
|
struct MemMemTable {
|
|
|
|
static const size_t kSize = kSizeT;
|
|
|
|
Word MemMemWords[kSize];
|
|
|
|
Word EmptyWord;
|
|
|
|
|
|
|
|
void Add(const uint8_t *Data, size_t Size) {
|
|
|
|
if (Size <= 2) return;
|
|
|
|
Size = std::min(Size, Word::GetMaxSize());
|
|
|
|
size_t Idx = SimpleFastHash(Data, Size) % kSize;
|
|
|
|
MemMemWords[Idx].Set(Data, Size);
|
|
|
|
}
|
|
|
|
const Word &Get(size_t Idx) {
|
|
|
|
for (size_t i = 0; i < kSize; i++) {
|
|
|
|
const Word &W = MemMemWords[(Idx + i) % kSize];
|
|
|
|
if (W.size()) return W;
|
|
|
|
}
|
|
|
|
EmptyWord.Set(nullptr, 0);
|
|
|
|
return EmptyWord;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TracePC {
|
|
|
|
public:
|
|
|
|
static const size_t kNumPCs = 1 << 21;
|
|
|
|
// How many bits of PC are used from __sanitizer_cov_trace_pc.
|
|
|
|
static const size_t kTracePcBits = 18;
|
|
|
|
|
|
|
|
void HandleInit(uint32_t *Start, uint32_t *Stop);
|
|
|
|
void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
|
2017-08-26 03:29:47 +08:00
|
|
|
void HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop);
|
2017-08-22 07:25:50 +08:00
|
|
|
void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
|
|
|
|
template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
|
|
|
|
size_t GetTotalPCCoverage();
|
|
|
|
void SetUseCounters(bool UC) { UseCounters = UC; }
|
2017-10-06 06:41:03 +08:00
|
|
|
void SetUseClangCoverage(bool UCC) { UseClangCoverage = UCC; }
|
2017-08-22 07:25:50 +08:00
|
|
|
void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
|
|
|
|
void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
|
2017-08-29 06:52:22 +08:00
|
|
|
void SetPrintNewFuncs(size_t P) { NumPrintNewFuncs = P; }
|
2017-08-22 07:25:50 +08:00
|
|
|
void UpdateObservedPCs();
|
|
|
|
template <class Callback> void CollectFeatures(Callback CB) const;
|
|
|
|
|
|
|
|
void ResetMaps() {
|
|
|
|
ValueProfileMap.Reset();
|
|
|
|
if (NumModules)
|
|
|
|
memset(Counters(), 0, GetNumPCs());
|
|
|
|
ClearExtraCounters();
|
|
|
|
ClearInlineCounters();
|
2017-10-06 06:41:03 +08:00
|
|
|
if (UseClangCoverage)
|
|
|
|
ClearClangCounters();
|
2017-08-22 07:25:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearInlineCounters();
|
|
|
|
|
|
|
|
void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
|
|
|
|
void PrintFeatureSet();
|
|
|
|
|
|
|
|
void PrintModuleInfo();
|
|
|
|
|
|
|
|
void PrintCoverage();
|
|
|
|
void DumpCoverage();
|
|
|
|
|
|
|
|
void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
|
|
|
|
size_t n, bool StopAtZero);
|
|
|
|
|
|
|
|
TableOfRecentCompares<uint32_t, 32> TORC4;
|
|
|
|
TableOfRecentCompares<uint64_t, 32> TORC8;
|
|
|
|
TableOfRecentCompares<Word, 32> TORCW;
|
|
|
|
MemMemTable<1024> MMT;
|
|
|
|
|
|
|
|
size_t GetNumPCs() const {
|
|
|
|
return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
|
|
|
|
}
|
|
|
|
uintptr_t GetPC(size_t Idx) {
|
|
|
|
assert(Idx < GetNumPCs());
|
|
|
|
return PCs()[Idx];
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:28:32 +08:00
|
|
|
void RecordInitialStack();
|
|
|
|
uintptr_t GetMaxStackOffset() const;
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
template<class CallBack>
|
|
|
|
void ForEachObservedPC(CallBack CB) {
|
|
|
|
for (auto PC : ObservedPCs)
|
|
|
|
CB(PC);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool UseCounters = false;
|
|
|
|
bool UseValueProfile = false;
|
2017-10-06 06:41:03 +08:00
|
|
|
bool UseClangCoverage = false;
|
2017-08-22 07:25:50 +08:00
|
|
|
bool DoPrintNewPCs = false;
|
2017-08-29 06:52:22 +08:00
|
|
|
size_t NumPrintNewFuncs = 0;
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
struct Module {
|
|
|
|
uint32_t *Start, *Stop;
|
|
|
|
};
|
|
|
|
|
|
|
|
Module Modules[4096];
|
|
|
|
size_t NumModules; // linker-initialized.
|
|
|
|
size_t NumGuards; // linker-initialized.
|
|
|
|
|
|
|
|
struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
|
|
|
|
size_t NumModulesWithInline8bitCounters; // linker-initialized.
|
|
|
|
size_t NumInline8bitCounters;
|
|
|
|
|
2017-08-26 03:29:47 +08:00
|
|
|
struct PCTableEntry {
|
|
|
|
uintptr_t PC, PCFlags;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096];
|
2017-08-22 07:25:50 +08:00
|
|
|
size_t NumPCTables;
|
|
|
|
size_t NumPCsInPCTables;
|
|
|
|
|
|
|
|
uint8_t *Counters() const;
|
|
|
|
uintptr_t *PCs() const;
|
|
|
|
|
2017-08-28 07:20:09 +08:00
|
|
|
Set<uintptr_t> ObservedPCs;
|
|
|
|
Set<uintptr_t> ObservedFuncs;
|
2017-08-22 07:25:50 +08:00
|
|
|
|
|
|
|
ValueBitMap ValueProfileMap;
|
2017-08-22 09:28:32 +08:00
|
|
|
uintptr_t InitialStack;
|
2017-08-22 07:25:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Callback>
|
|
|
|
// void Callback(size_t FirstFeature, size_t Idx, uint8_t Value);
|
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
|
|
|
void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
|
|
|
|
size_t FirstFeature, Callback Handle8bitCounter) {
|
|
|
|
typedef uintptr_t LargeType;
|
|
|
|
const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
|
|
|
|
const size_t StepMask = Step - 1;
|
|
|
|
auto P = Begin;
|
|
|
|
// Iterate by 1 byte until either the alignment boundary or the end.
|
|
|
|
for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
|
|
|
|
if (uint8_t V = *P)
|
|
|
|
Handle8bitCounter(FirstFeature, P - Begin, V);
|
|
|
|
|
|
|
|
// Iterate by Step bytes at a time.
|
|
|
|
for (; P < End; P += Step)
|
|
|
|
if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
|
|
|
|
for (size_t I = 0; I < Step; I++, Bundle >>= 8)
|
|
|
|
if (uint8_t V = Bundle & 0xff)
|
|
|
|
Handle8bitCounter(FirstFeature, P - Begin + I, V);
|
|
|
|
|
|
|
|
// Iterate by 1 byte until the end.
|
|
|
|
for (; P < End; P++)
|
|
|
|
if (uint8_t V = *P)
|
|
|
|
Handle8bitCounter(FirstFeature, P - Begin, V);
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:41:58 +08:00
|
|
|
// Given a non-zero Counter returns a number in the range [0,7].
|
2017-08-22 09:28:32 +08:00
|
|
|
template<class T>
|
|
|
|
unsigned CounterToFeature(T Counter) {
|
2017-11-29 01:41:58 +08:00
|
|
|
// Returns a feature number by placing Counters into buckets as illustrated
|
|
|
|
// below.
|
|
|
|
//
|
|
|
|
// Counter bucket: [1] [2] [3] [4-7] [8-15] [16-31] [32-127] [128+]
|
|
|
|
// Feature number: 0 1 2 3 4 5 6 7
|
|
|
|
//
|
|
|
|
// This is a heuristic taken from AFL (see
|
|
|
|
// http://lcamtuf.coredump.cx/afl/technical_details.txt).
|
|
|
|
//
|
|
|
|
// This implementation may change in the future so clients should
|
|
|
|
// not rely on it.
|
2017-08-22 07:25:50 +08:00
|
|
|
assert(Counter);
|
|
|
|
unsigned Bit = 0;
|
|
|
|
/**/ if (Counter >= 128) Bit = 7;
|
|
|
|
else if (Counter >= 32) Bit = 6;
|
|
|
|
else if (Counter >= 16) Bit = 5;
|
|
|
|
else if (Counter >= 8) Bit = 4;
|
|
|
|
else if (Counter >= 4) Bit = 3;
|
|
|
|
else if (Counter >= 3) Bit = 2;
|
|
|
|
else if (Counter >= 2) Bit = 1;
|
2017-08-22 09:28:32 +08:00
|
|
|
return Bit;
|
|
|
|
}
|
|
|
|
|
2017-09-16 06:10:36 +08:00
|
|
|
template <class Callback> // void Callback(size_t Feature)
|
2017-08-22 09:28:32 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_ADDRESS
|
|
|
|
__attribute__((noinline))
|
|
|
|
void TracePC::CollectFeatures(Callback HandleFeature) const {
|
|
|
|
uint8_t *Counters = this->Counters();
|
|
|
|
size_t N = GetNumPCs();
|
|
|
|
auto Handle8bitCounter = [&](size_t FirstFeature,
|
|
|
|
size_t Idx, uint8_t Counter) {
|
|
|
|
HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter));
|
2017-08-22 07:25:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t FirstFeature = 0;
|
|
|
|
|
|
|
|
if (!NumInline8bitCounters) {
|
|
|
|
ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
|
|
|
|
FirstFeature += N * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumInline8bitCounters) {
|
|
|
|
for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
|
|
|
|
ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
|
|
|
|
FirstFeature, Handle8bitCounter);
|
|
|
|
FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:28:32 +08:00
|
|
|
if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) {
|
|
|
|
auto P = ClangCountersBegin();
|
|
|
|
for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
|
|
|
|
if (auto Cnt = P[Idx])
|
|
|
|
HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt));
|
|
|
|
FirstFeature += NumClangCounters;
|
|
|
|
}
|
|
|
|
|
2017-08-22 07:25:50 +08:00
|
|
|
ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
|
|
|
|
Handle8bitCounter);
|
|
|
|
FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8;
|
|
|
|
|
|
|
|
if (UseValueProfile) {
|
|
|
|
ValueProfileMap.ForEach([&](size_t Idx) {
|
|
|
|
HandleFeature(FirstFeature + Idx);
|
|
|
|
});
|
|
|
|
FirstFeature += ValueProfileMap.SizeInBits();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto MaxStackOffset = GetMaxStackOffset())
|
|
|
|
HandleFeature(FirstFeature + MaxStackOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern TracePC TPC;
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_TRACE_PC
|