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"
|
2016-09-22 05:17:23 +08:00
|
|
|
#include "FuzzerValueBitMap.h"
|
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;
|
|
|
|
void Insert(size_t Idx, T Arg1, T Arg2) {
|
|
|
|
Idx = Idx % kSize;
|
|
|
|
Table[Idx][0] = Arg1;
|
|
|
|
Table[Idx][1] = Arg2;
|
|
|
|
}
|
|
|
|
void Clear() { memset(Table, 0, sizeof(Table)); }
|
|
|
|
T Table[kSize][2];
|
|
|
|
};
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
class TracePC {
|
|
|
|
public:
|
2016-09-30 09:19:56 +08:00
|
|
|
static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
|
|
|
|
|
2016-09-30 01:43:24 +08:00
|
|
|
void HandleTrace(uint32_t *guard, uintptr_t PC);
|
|
|
|
void HandleInit(uint32_t *start, uint32_t *stop);
|
2016-09-21 09:50:50 +08:00
|
|
|
void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
|
2016-09-23 08:46:18 +08:00
|
|
|
void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
|
2016-10-14 00:19:09 +08:00
|
|
|
template <class T> void HandleCmp(void *PC, T Arg1, T Arg2);
|
2016-09-23 09:20:07 +08:00
|
|
|
size_t GetTotalPCCoverage() { return TotalPCCoverage; }
|
|
|
|
void ResetTotalPCCoverage() { TotalPCCoverage = 0; }
|
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-06 06:56:21 +08:00
|
|
|
size_t FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink);
|
2016-09-23 08:46:18 +08:00
|
|
|
bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
|
|
|
|
return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
|
2016-10-14 00:19:09 +08:00
|
|
|
}
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-09-22 09:34:58 +08:00
|
|
|
size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
|
|
|
|
*NewPCIDsPtr = NewPCIDs;
|
2016-09-23 08:22:46 +08:00
|
|
|
return Min(kMaxNewPCIDs, NumNewPCIDs);
|
2016-09-21 09:50:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-22 09:34:58 +08:00
|
|
|
uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
|
|
|
|
|
2016-09-23 10:18:59 +08:00
|
|
|
void ResetMaps() {
|
2016-09-22 09:34:58 +08:00
|
|
|
NumNewPCIDs = 0;
|
2016-09-23 09:58:51 +08:00
|
|
|
ValueProfileMap.Reset();
|
2016-09-24 04:04:13 +08:00
|
|
|
memset(Counters, 0, sizeof(Counters));
|
2016-09-21 09:50:50 +08:00
|
|
|
}
|
|
|
|
|
2016-10-15 04:20:33 +08:00
|
|
|
void ResetTORC() {
|
|
|
|
TORC4.Clear();
|
|
|
|
TORC8.Clear();
|
|
|
|
}
|
|
|
|
|
2016-09-24 07:51:58 +08:00
|
|
|
void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
|
|
|
|
void PrintFeatureSet();
|
|
|
|
|
2016-09-23 10:18:59 +08:00
|
|
|
void ResetGuards();
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
void PrintModuleInfo();
|
|
|
|
|
|
|
|
void PrintCoverage();
|
|
|
|
|
2016-10-05 09:09:40 +08:00
|
|
|
void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
|
|
|
|
size_t n);
|
|
|
|
void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
|
|
|
|
size_t n);
|
|
|
|
|
2016-10-06 06:56:21 +08:00
|
|
|
bool UsingTracePcGuard() const {return NumModules; }
|
|
|
|
|
2016-10-15 04:20:33 +08:00
|
|
|
void ProcessTORC(Dictionary *Dict, const uint8_t *Data, size_t Size);
|
|
|
|
|
2016-09-21 09:50:50 +08:00
|
|
|
private:
|
|
|
|
bool UseCounters = false;
|
2016-09-23 08:46:18 +08:00
|
|
|
bool UseValueProfile = false;
|
2016-09-23 09:20:07 +08:00
|
|
|
size_t TotalPCCoverage = 0;
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-09-27 08:10:20 +08:00
|
|
|
static const size_t kMaxNewPCIDs = 1024;
|
2016-09-22 09:34:58 +08:00
|
|
|
uintptr_t NewPCIDs[kMaxNewPCIDs];
|
|
|
|
size_t NumNewPCIDs = 0;
|
|
|
|
void AddNewPCID(uintptr_t PCID) {
|
|
|
|
NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
|
|
|
|
}
|
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];
|
|
|
|
size_t NumModules = 0;
|
|
|
|
size_t NumGuards = 0;
|
|
|
|
|
|
|
|
static const size_t kNumCounters = 1 << 14;
|
2016-09-28 09:16:24 +08:00
|
|
|
alignas(8) uint8_t Counters[kNumCounters];
|
2016-09-21 09:50:50 +08:00
|
|
|
|
2016-10-15 04:20:33 +08:00
|
|
|
static const size_t kTORCSize = 1 << 12;
|
|
|
|
TableOfRecentCompares<uint32_t, kTORCSize> TORC4;
|
|
|
|
TableOfRecentCompares<uint64_t, kTORCSize> TORC8;
|
|
|
|
void TORCInsert(size_t Idx, uint8_t Arg1, uint8_t Arg2) {
|
|
|
|
// Do nothing, too small to be interesting.
|
|
|
|
}
|
|
|
|
void TORCInsert(size_t Idx, uint16_t Arg1, uint16_t Arg2) {
|
|
|
|
// Do nothing, these don't usually hapen.
|
|
|
|
}
|
|
|
|
void TORCInsert(size_t Idx, uint32_t Arg1, uint32_t Arg2) {
|
|
|
|
TORC4.Insert(Idx, Arg1, Arg2);
|
|
|
|
}
|
|
|
|
void TORCInsert(size_t Idx, uint64_t Arg1, uint64_t Arg2) {
|
|
|
|
TORC8.Insert(Idx, Arg1, Arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void TORCToDict(const TableOfRecentCompares<T, kTORCSize> &TORC,
|
|
|
|
Dictionary *Dict, const uint8_t *Data, size_t Size);
|
|
|
|
template <class T>
|
|
|
|
void TORCToDict(Dictionary *Dict, T FindInData, T Substitute,
|
|
|
|
const uint8_t *Data, size_t Size);
|
|
|
|
|
2016-10-11 09:14:41 +08:00
|
|
|
static const size_t kNumPCs = 1 << 24;
|
2016-09-21 09:50:50 +08:00
|
|
|
uintptr_t PCs[kNumPCs];
|
|
|
|
|
2016-09-23 08:46:18 +08:00
|
|
|
ValueBitMap ValueProfileMap;
|
2016-09-21 09:50:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TracePC TPC;
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_TRACE_PC
|