blob: facd98d526243b1065281d260419a9c1394f36b5 [file] [log] [blame]
Kostya Serebryany6f5a8042016-09-21 01:50:50 +00001//===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// fuzzer::TracePC
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_FUZZER_TRACE_PC
13#define LLVM_FUZZER_TRACE_PC
14
15#include "FuzzerDefs.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000016#include "FuzzerValueBitMap.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000017
18namespace fuzzer {
19
20class TracePC {
21 public:
22 void HandleTrace(uintptr_t *guard, uintptr_t PC);
23 void HandleInit(uintptr_t *start, uintptr_t *stop);
24 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000025 void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
Kostya Serebryany87a598e2016-09-23 01:20:07 +000026 size_t GetTotalPCCoverage() { return TotalPCCoverage; }
27 void ResetTotalPCCoverage() { TotalPCCoverage = 0; }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000028 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000029 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000030 bool UpdateCounterMap(ValueBitMap *MaxCounterMap) {
Kostya Serebryany87a598e2016-09-23 01:20:07 +000031 return MaxCounterMap->MergeFrom(CounterMap);
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000032 }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000033 bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
34 return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
35 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000036 void FinalizeTrace();
37
Kostya Serebryany624f59f2016-09-22 01:34:58 +000038 size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
39 *NewPCIDsPtr = NewPCIDs;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000040 return Min(kMaxNewPCIDs, NumNewPCIDs);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000041 }
42
Kostya Serebryany624f59f2016-09-22 01:34:58 +000043 void ResetNewPCIDs() { NumNewPCIDs = 0; }
44 uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
45
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000046 void ResetMaps() {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000047 NumNewPCIDs = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000048 CounterMap.Reset();
Kostya Serebryany16a145f2016-09-23 01:58:51 +000049 ValueProfileMap.Reset();
Kostya Serebryany0d26de32016-09-23 20:04:13 +000050 memset(Counters, 0, sizeof(Counters));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000051 }
52
Kostya Serebryany0800b812016-09-23 23:51:58 +000053 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
54 void PrintFeatureSet();
55
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000056 void ResetGuards();
57
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000058 void PrintModuleInfo();
59
60 void PrintCoverage();
61
62private:
63 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000064 bool UseValueProfile = false;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000065 size_t TotalPCCoverage = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000066
Kostya Serebryany624f59f2016-09-22 01:34:58 +000067 static const size_t kMaxNewPCIDs = 64;
68 uintptr_t NewPCIDs[kMaxNewPCIDs];
69 size_t NumNewPCIDs = 0;
70 void AddNewPCID(uintptr_t PCID) {
71 NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
72 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000073
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000074 struct Module {
75 uintptr_t *Start, *Stop;
76 };
77
78 Module Modules[4096];
79 size_t NumModules = 0;
80 size_t NumGuards = 0;
81
82 static const size_t kNumCounters = 1 << 14;
83 uint8_t Counters[kNumCounters];
84
85 static const size_t kNumPCs = 1 << 20;
86 uintptr_t PCs[kNumPCs];
87
88 ValueBitMap CounterMap;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000089 ValueBitMap ValueProfileMap;
Kostya Serebryany0800b812016-09-23 23:51:58 +000090
91 struct Feature {
92 size_t Count;
93 size_t SmallestElementIdx;
94 size_t SmallestElementSize;
95 };
96
97 static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
98 Feature FeatureSet[kFeatureSetSize];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099};
100
101extern TracePC TPC;
102
103} // namespace fuzzer
104
105#endif // LLVM_FUZZER_TRACE_PC