2016-09-21 03:50:50 +02: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-21 23:17:23 +02:00
|
|
|
#include "FuzzerValueBitMap.h"
|
2016-09-21 03:50:50 +02:00
|
|
|
|
|
|
|
namespace fuzzer {
|
|
|
|
|
2016-10-14 22:20:33 +02: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 03:50:50 +02:00
|
|
|
class TracePC {
|
|
|
|
public:
|
2016-09-30 03:19:56 +02:00
|
|
|
static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
|
|
|
|
|
2016-09-29 19:43:24 +02:00
|
|
|
void HandleTrace(uint32_t *guard, uintptr_t PC);
|
|
|
|
void HandleInit(uint32_t *start, uint32_t *stop);
|
2016-09-21 03:50:50 +02:00
|
|
|
void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
|
2016-09-23 02:46:18 +02:00
|
|
|
void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
|
2016-10-13 18:19:09 +02:00
|
|
|
template <class T> void HandleCmp(void *PC, T Arg1, T Arg2);
|
2016-09-23 03:20:07 +02:00
|
|
|
size_t GetTotalPCCoverage() { return TotalPCCoverage; }
|
|
|
|
void ResetTotalPCCoverage() { TotalPCCoverage = 0; }
|
2016-09-21 03:50:50 +02:00
|
|
|
void SetUseCounters(bool UC) { UseCounters = UC; }
|
2016-09-23 02:46:18 +02:00
|
|
|
void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
|
2016-10-06 00:56:21 +02:00
|
|
|
size_t FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink);
|
2016-09-23 02:46:18 +02:00
|
|
|
bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
|
|
|
|
return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
|
2016-10-13 18:19:09 +02:00
|
|
|
}
|
2016-09-21 03:50:50 +02:00
|
|
|
|
2016-09-22 03:34:58 +02:00
|
|
|
size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
|
|
|
|
*NewPCIDsPtr = NewPCIDs;
|
2016-09-23 02:22:46 +02:00
|
|
|
return Min(kMaxNewPCIDs, NumNewPCIDs);
|
2016-09-21 03:50:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-22 03:34:58 +02:00
|
|
|
uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
|
|
|
|
|
2016-09-23 04:18:59 +02:00
|
|
|
void ResetMaps() {
|
2016-09-22 03:34:58 +02:00
|
|
|
NumNewPCIDs = 0;
|
2016-09-23 03:58:51 +02:00
|
|
|
ValueProfileMap.Reset();
|
2016-09-23 22:04:13 +02:00
|
|
|
memset(Counters, 0, sizeof(Counters));
|
2016-09-21 03:50:50 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 22:20:33 +02:00
|
|
|
void ResetTORC() {
|
|
|
|
TORC4.Clear();
|
|
|
|
TORC8.Clear();
|
|
|
|
}
|
|
|
|
|
2016-09-24 01:51:58 +02:00
|
|
|
void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
|
|
|
|
void PrintFeatureSet();
|
|
|
|
|
2016-09-23 04:18:59 +02:00
|
|
|
void ResetGuards();
|
|
|
|
|
2016-09-21 03:50:50 +02:00
|
|
|
void PrintModuleInfo();
|
|
|
|
|
|
|
|
void PrintCoverage();
|
|
|
|
|
2016-10-05 03:09:40 +02: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 00:56:21 +02:00
|
|
|
bool UsingTracePcGuard() const {return NumModules; }
|
|
|
|
|
2016-10-14 22:20:33 +02:00
|
|
|
void ProcessTORC(Dictionary *Dict, const uint8_t *Data, size_t Size);
|
|
|
|
|
2016-09-21 03:50:50 +02:00
|
|
|
private:
|
|
|
|
bool UseCounters = false;
|
2016-09-23 02:46:18 +02:00
|
|
|
bool UseValueProfile = false;
|
2016-09-23 03:20:07 +02:00
|
|
|
size_t TotalPCCoverage = 0;
|
2016-09-21 03:50:50 +02:00
|
|
|
|
2016-09-27 02:10:20 +02:00
|
|
|
static const size_t kMaxNewPCIDs = 1024;
|
2016-09-22 03:34:58 +02:00
|
|
|
uintptr_t NewPCIDs[kMaxNewPCIDs];
|
|
|
|
size_t NumNewPCIDs = 0;
|
|
|
|
void AddNewPCID(uintptr_t PCID) {
|
|
|
|
NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
|
|
|
|
}
|
2016-09-21 03:50:50 +02:00
|
|
|
|
|
|
|
struct Module {
|
2016-09-29 19:43:24 +02:00
|
|
|
uint32_t *Start, *Stop;
|
2016-09-21 03:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Module Modules[4096];
|
|
|
|
size_t NumModules = 0;
|
|
|
|
size_t NumGuards = 0;
|
|
|
|
|
|
|
|
static const size_t kNumCounters = 1 << 14;
|
2016-09-28 03:16:24 +02:00
|
|
|
alignas(8) uint8_t Counters[kNumCounters];
|
2016-09-21 03:50:50 +02:00
|
|
|
|
2016-10-14 22:20:33 +02: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 03:14:41 +02:00
|
|
|
static const size_t kNumPCs = 1 << 24;
|
2016-09-21 03:50:50 +02:00
|
|
|
uintptr_t PCs[kNumPCs];
|
|
|
|
|
2016-09-23 02:46:18 +02:00
|
|
|
ValueBitMap ValueProfileMap;
|
2016-09-21 03:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
extern TracePC TPC;
|
|
|
|
|
|
|
|
} // namespace fuzzer
|
|
|
|
|
|
|
|
#endif // LLVM_FUZZER_TRACE_PC
|