blob: f3da724c269286123ce8f5c17e3d122adb4de006 [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 Serebryany6f5a8042016-09-21 01:50:50 +000026 size_t GetTotalCoverage() { return TotalCoverage; }
27 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000028 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000029 bool UpdateCounterMap(ValueBitMap *MaxCounterMap) {
30 return UseCounters && MaxCounterMap->MergeFrom(CounterMap);
31 }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000032 bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
33 return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
34 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000035 void FinalizeTrace();
36
Kostya Serebryany624f59f2016-09-22 01:34:58 +000037 size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
38 *NewPCIDsPtr = NewPCIDs;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000039 return Min(kMaxNewPCIDs, NumNewPCIDs);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000040 }
41
Kostya Serebryany624f59f2016-09-22 01:34:58 +000042 void ResetNewPCIDs() { NumNewPCIDs = 0; }
43 uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
44
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045 void Reset() {
46 TotalCoverage = 0;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000047 NumNewPCIDs = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000048 CounterMap.Reset();
49 TotalCoverageMap.Reset();
50 ResetGuards();
51 }
52
53 void PrintModuleInfo();
54
55 void PrintCoverage();
56
57private:
58 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000059 bool UseValueProfile = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000060 size_t TotalCoverage = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000061
Kostya Serebryany624f59f2016-09-22 01:34:58 +000062 static const size_t kMaxNewPCIDs = 64;
63 uintptr_t NewPCIDs[kMaxNewPCIDs];
64 size_t NumNewPCIDs = 0;
65 void AddNewPCID(uintptr_t PCID) {
66 NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
67 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000068
69 void ResetGuards();
70
71 struct Module {
72 uintptr_t *Start, *Stop;
73 };
74
75 Module Modules[4096];
76 size_t NumModules = 0;
77 size_t NumGuards = 0;
78
79 static const size_t kNumCounters = 1 << 14;
80 uint8_t Counters[kNumCounters];
81
82 static const size_t kNumPCs = 1 << 20;
83 uintptr_t PCs[kNumPCs];
84
85 ValueBitMap CounterMap;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000086 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000087 ValueBitMap TotalCoverageMap;
88};
89
90extern TracePC TPC;
91
92} // namespace fuzzer
93
94#endif // LLVM_FUZZER_TRACE_PC