blob: cd057ea093af58ba71630a57ac51363933c058cf [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 Serebryany6f5a8042016-09-21 01:50:50 +000050 }
51
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000052 void ResetGuards();
53
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000054 void PrintModuleInfo();
55
56 void PrintCoverage();
57
58private:
59 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000060 bool UseValueProfile = false;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000061 size_t TotalPCCoverage = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000062
Kostya Serebryany624f59f2016-09-22 01:34:58 +000063 static const size_t kMaxNewPCIDs = 64;
64 uintptr_t NewPCIDs[kMaxNewPCIDs];
65 size_t NumNewPCIDs = 0;
66 void AddNewPCID(uintptr_t PCID) {
67 NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
68 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000069
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000070
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};
88
89extern TracePC TPC;
90
91} // namespace fuzzer
92
93#endif // LLVM_FUZZER_TRACE_PC