2016-09-21 09:50: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"
|
2017-01-18 07:09:05 +08:00
|
|
|
#include "FuzzerDictionary.h"
|
2016-09-22 05:17:23 +08:00
|
|
|
#include "FuzzerValueBitMap.h"
|
2017-01-18 07:09:05 +08:00
|
|
|
|
2016-12-14 01:46:11 +08:00
|
|
|
#include <set>
|
2016-09-21 09:50:50 +08:00
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-10-15 04:20:33 +08:00
|
|
|
// 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;
|
2016-10-25 10:04:43 +08:00
|
|
|
struct Pair {
|
|
|
|
T A, B;
|
|
|
|
};
|
2017-01-27 08:09:59 +08:00
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
2017-03-31 10:21:28 +08:00
|
|
|
void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
|
2016-10-15 04:20:33 +08:00
|
|
|
Idx = Idx % kSize;
|
2016-10-25 10:04:43 +08:00
|
|
|
Table[Idx].A = Arg1;
|
|
|
|
Table[Idx].B = Arg2;
|
2016-10-15 04:20:33 +08:00
|
|
|
}
|
2016-10-25 10:04:43 +08:00
|
|
|
|
|
|
|
Pair Get(size_t I) { return Table[I % kSize]; }
|
|
|
|
|
|
|
|
Pair Table[kSize];
|
2016-10-15 04:20:33 +08:00
|
|
|
};
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
class TracePC {
|
|
|
|
public:
|
2017-02-03 03:56:01 +08:00
|
|
|
static const size_t kNumPCs = 1 << 21;
|
2017-03-30 09:27:20 +08:00
|
|
|
// How many bits of PC are used from __sanitizer_cov_trace_pc.
|
|
|
|
static const size_t kTracePcBits = 18;
|
2016-09-30 09:19:56 +08:00
|
|
|
|
2016-09-30 01:43:24 +08:00
|
|
|
void HandleInit(uint32_t *start, uint32_t *stop);
|
2016-09-21 09:50:50 +08:00
|
|
|
void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
|
2017-01-18 07:11:32 +08:00
|
|
|
template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
|
2016-10-26 07:52:25 +08:00
|
|
|
size_t GetTotalPCCoverage();
|
2016-09-21 09:50:50 +08:00
|
|
|
void SetUseCounters(bool UC) { UseCounters = UC; }
|
2016-09-23 08:46:18 +08:00
|
|
|
void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
|
2016-10-26 08:20:51 +08:00
|
|
|
void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
|
2017-03-31 12:17:45 +08:00
|
|
|
template <class Callback> void CollectFeatures(Callback CB) const;
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-09-23 10:18:59 +08:00
|
|
|
void ResetMaps() {
|
2016-09-23 09:58:51 +08:00
|
|
|
ValueProfileMap.Reset();
|
2017-02-03 03:56:01 +08:00
|
|
|
memset(Counters(), 0, GetNumPCs());
|
2017-03-24 06:43:12 +08:00
|
|
|
ClearExtraCounters();
|
2016-09-21 09:50:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-24 07:51:58 +08:00
|
|
|
void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
|
|
|
|
void PrintFeatureSet();
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
void PrintModuleInfo();
|
|
|
|
|
|
|
|
void PrintCoverage();
|
2016-12-20 06:18:08 +08:00
|
|
|
void DumpCoverage();
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-10-05 09:09:40 +08:00
|
|
|
void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
|
2017-01-18 07:09:05 +08:00
|
|
|
size_t n, bool StopAtZero);
|
2016-10-05 09:09:40 +08:00
|
|
|
|
2017-01-18 07:09:05 +08:00
|
|
|
TableOfRecentCompares<uint32_t, 32> TORC4;
|
|
|
|
TableOfRecentCompares<uint64_t, 32> TORC8;
|
|
|
|
TableOfRecentCompares<Word, 32> TORCW;
|
2016-10-15 04:20:33 +08:00
|
|
|
|
2016-10-26 08:20:51 +08:00
|
|
|
void PrintNewPCs();
|
2016-12-30 09:13:07 +08:00
|
|
|
void InitializePrintNewPCs();
|
2017-03-30 09:27:20 +08:00
|
|
|
size_t GetNumPCs() const {
|
|
|
|
return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
|
|
|
|
}
|
2016-10-26 08:42:52 +08:00
|
|
|
uintptr_t GetPC(size_t Idx) {
|
|
|
|
assert(Idx < GetNumPCs());
|
2017-02-03 03:56:01 +08:00
|
|
|
return PCs()[Idx];
|
2016-10-26 08:42:52 +08:00
|
|
|
}
|
2016-10-26 08:20:51 +08:00
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
private:
|
|
|
|
bool UseCounters = false;
|
2016-09-23 08:46:18 +08:00
|
|
|
bool UseValueProfile = false;
|
2016-10-26 08:20:51 +08:00
|
|
|
bool DoPrintNewPCs = false;
|
2016-09-21 09:50:50 +08:00
|
|
|
|
|
|
|
struct Module {
|
2016-09-30 01:43:24 +08:00
|
|
|
uint32_t *Start, *Stop;
|
2016-09-21 09:50:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Module Modules[4096];
|
2016-11-12 07:06:53 +08:00
|
|
|
size_t NumModules; // linker-initialized.
|
|
|
|
size_t NumGuards; // linker-initialized.
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2017-02-03 03:56:01 +08:00
|
|
|
uint8_t *Counters() const;
|
|
|
|
uintptr_t *PCs() const;
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-10-26 08:20:51 +08:00
|
|
|
std::set<uintptr_t> *PrintedPCs;
|
|
|
|
|
2016-09-23 08:46:18 +08:00
|
|
|
ValueBitMap ValueProfileMap;
|
2016-09-21 09:50:50 +08:00
|
|
|
};
|
|
|
|
|
2017-03-24 06:43:12 +08:00
|
|
|
template <class Callback> // void Callback(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);
|
|
|
|
assert(!(reinterpret_cast<uintptr_t>(Begin) % 64));
|
|
|
|
for (auto P = Begin; 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Callback> // bool Callback(size_t Feature)
|
|
|
|
ATTRIBUTE_NO_SANITIZE_ALL
|
|
|
|
__attribute__((noinline))
|
2017-03-31 12:17:45 +08:00
|
|
|
void TracePC::CollectFeatures(Callback HandleFeature) const {
|
2017-02-03 03:56:01 +08:00
|
|
|
uint8_t *Counters = this->Counters();
|
2017-01-26 09:34:58 +08:00
|
|
|
size_t N = GetNumPCs();
|
2017-03-24 06:43:12 +08:00
|
|
|
auto Handle8bitCounter = [&](size_t Idx, uint8_t Counter) {
|
|
|
|
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-03-31 12:17:45 +08:00
|
|
|
HandleFeature(Idx * 8 + Bit);
|
2017-03-24 06:43:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ForEachNonZeroByte(Counters, Counters + N, 0, Handle8bitCounter);
|
|
|
|
ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), N * 8,
|
|
|
|
Handle8bitCounter);
|
|
|
|
|
2016-12-06 07:35:22 +08:00
|
|
|
if (UseValueProfile)
|
|
|
|
ValueProfileMap.ForEach([&](size_t Idx) {
|
2017-03-31 12:17:45 +08:00
|
|
|
HandleFeature(N * 8 + Idx);
|
2016-12-06 07:35:22 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
extern TracePC TPC;
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_TRACE_PC
|