blob: 06db3d0dd422582416d6fd088f51ba3b7b34cb9b [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
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000015#include <set>
16
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000017#include "FuzzerDefs.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000018#include "FuzzerValueBitMap.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000019
20namespace fuzzer {
21
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000022// TableOfRecentCompares (TORC) remembers the most recently performed
23// comparisons of type T.
24// We record the arguments of CMP instructions in this table unconditionally
25// because it seems cheaper this way than to compute some expensive
26// conditions inside __sanitizer_cov_trace_cmp*.
27// After the unit has been executed we may decide to use the contents of
28// this table to populate a Dictionary.
29template<class T, size_t kSizeT>
30struct TableOfRecentCompares {
31 static const size_t kSize = kSizeT;
Kostya Serebryany3364f902016-10-25 02:04:43 +000032 struct Pair {
33 T A, B;
34 };
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000035 void Insert(size_t Idx, T Arg1, T Arg2) {
36 Idx = Idx % kSize;
Kostya Serebryany3364f902016-10-25 02:04:43 +000037 Table[Idx].A = Arg1;
38 Table[Idx].B = Arg2;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000039 }
Kostya Serebryany3364f902016-10-25 02:04:43 +000040
41 Pair Get(size_t I) { return Table[I % kSize]; }
42
43 Pair Table[kSize];
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000044};
45
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000046class TracePC {
47 public:
Kostya Serebryany2c556132016-09-30 01:19:56 +000048 static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
49
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000050 void HandleTrace(uint32_t *guard, uintptr_t PC);
51 void HandleInit(uint32_t *start, uint32_t *stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000052 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000053 void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
Kostya Serebryany17d176e12016-10-13 16:19:09 +000054 template <class T> void HandleCmp(void *PC, T Arg1, T Arg2);
Kostya Serebryany275e2602016-10-25 23:52:25 +000055 size_t GetTotalPCCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000056 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000057 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000058 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000059 size_t FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000060 bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
61 return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
Kostya Serebryany17d176e12016-10-13 16:19:09 +000062 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000063
Kostya Serebryany624f59f2016-09-22 01:34:58 +000064 size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
65 *NewPCIDsPtr = NewPCIDs;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000066 return Min(kMaxNewPCIDs, NumNewPCIDs);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000067 }
68
Kostya Serebryany624f59f2016-09-22 01:34:58 +000069 uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
70
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000071 void ResetMaps() {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000072 NumNewPCIDs = 0;
Kostya Serebryany16a145f2016-09-23 01:58:51 +000073 ValueProfileMap.Reset();
Kostya Serebryany0d26de32016-09-23 20:04:13 +000074 memset(Counters, 0, sizeof(Counters));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000075 }
76
Kostya Serebryany0800b812016-09-23 23:51:58 +000077 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
78 void PrintFeatureSet();
79
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000080 void ResetGuards();
81
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000082 void PrintModuleInfo();
83
84 void PrintCoverage();
85
Kostya Serebryany379359c2016-10-05 01:09:40 +000086 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
87 size_t n);
88 void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
89 size_t n);
90
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000091 bool UsingTracePcGuard() const {return NumModules; }
92
Kostya Serebryany3364f902016-10-25 02:04:43 +000093 static const size_t kTORCSize = 1 << 5;
94 TableOfRecentCompares<uint32_t, kTORCSize> TORC4;
95 TableOfRecentCompares<uint64_t, kTORCSize> TORC8;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000096
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000097 void PrintNewPCs();
98
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099private:
100 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000101 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000102 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000103
Kostya Serebryany5ff481f2016-09-27 00:10:20 +0000104 static const size_t kMaxNewPCIDs = 1024;
Kostya Serebryany624f59f2016-09-22 01:34:58 +0000105 uintptr_t NewPCIDs[kMaxNewPCIDs];
106 size_t NumNewPCIDs = 0;
107 void AddNewPCID(uintptr_t PCID) {
108 NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
109 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000110
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000111 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000112 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000113 };
114
115 Module Modules[4096];
116 size_t NumModules = 0;
117 size_t NumGuards = 0;
118
119 static const size_t kNumCounters = 1 << 14;
Kostya Serebryany3ee6c212016-09-28 01:16:24 +0000120 alignas(8) uint8_t Counters[kNumCounters];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000121
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000122 void TORCInsert(size_t Idx, uint8_t Arg1, uint8_t Arg2) {
123 // Do nothing, too small to be interesting.
124 }
125 void TORCInsert(size_t Idx, uint16_t Arg1, uint16_t Arg2) {
126 // Do nothing, these don't usually hapen.
127 }
128 void TORCInsert(size_t Idx, uint32_t Arg1, uint32_t Arg2) {
129 TORC4.Insert(Idx, Arg1, Arg2);
130 }
131 void TORCInsert(size_t Idx, uint64_t Arg1, uint64_t Arg2) {
132 TORC8.Insert(Idx, Arg1, Arg2);
133 }
134
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000135 static const size_t kNumPCs = 1 << 24;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000136 uintptr_t PCs[kNumPCs];
137
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000138 std::set<uintptr_t> *PrintedPCs;
139
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000140 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000141};
142
143extern TracePC TPC;
144
145} // namespace fuzzer
146
147#endif // LLVM_FUZZER_TRACE_PC