blob: 632cd541e2bea1279b28493c871cf57cd8be1a9e [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
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000020// TableOfRecentCompares (TORC) remembers the most recently performed
21// comparisons of type T.
22// We record the arguments of CMP instructions in this table unconditionally
23// because it seems cheaper this way than to compute some expensive
24// conditions inside __sanitizer_cov_trace_cmp*.
25// After the unit has been executed we may decide to use the contents of
26// this table to populate a Dictionary.
27template<class T, size_t kSizeT>
28struct TableOfRecentCompares {
29 static const size_t kSize = kSizeT;
30 void Insert(size_t Idx, T Arg1, T Arg2) {
31 Idx = Idx % kSize;
32 Table[Idx][0] = Arg1;
33 Table[Idx][1] = Arg2;
34 }
35 void Clear() { memset(Table, 0, sizeof(Table)); }
36 T Table[kSize][2];
37};
38
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000039class TracePC {
40 public:
Kostya Serebryany2c556132016-09-30 01:19:56 +000041 static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
42
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000043 void HandleTrace(uint32_t *guard, uintptr_t PC);
44 void HandleInit(uint32_t *start, uint32_t *stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000046 void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
Kostya Serebryany17d176e12016-10-13 16:19:09 +000047 template <class T> void HandleCmp(void *PC, T Arg1, T Arg2);
Kostya Serebryany87a598e2016-09-23 01:20:07 +000048 size_t GetTotalPCCoverage() { return TotalPCCoverage; }
49 void ResetTotalPCCoverage() { TotalPCCoverage = 0; }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000050 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000051 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000052 size_t FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000053 bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
54 return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
Kostya Serebryany17d176e12016-10-13 16:19:09 +000055 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000056
Kostya Serebryany624f59f2016-09-22 01:34:58 +000057 size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) {
58 *NewPCIDsPtr = NewPCIDs;
Kostya Serebryanyd28099d2016-09-23 00:22:46 +000059 return Min(kMaxNewPCIDs, NumNewPCIDs);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000060 }
61
Kostya Serebryany624f59f2016-09-22 01:34:58 +000062 uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; }
63
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000064 void ResetMaps() {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000065 NumNewPCIDs = 0;
Kostya Serebryany16a145f2016-09-23 01:58:51 +000066 ValueProfileMap.Reset();
Kostya Serebryany0d26de32016-09-23 20:04:13 +000067 memset(Counters, 0, sizeof(Counters));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000068 }
69
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000070 void ResetTORC() {
71 TORC4.Clear();
72 TORC8.Clear();
73 }
74
Kostya Serebryany0800b812016-09-23 23:51:58 +000075 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
76 void PrintFeatureSet();
77
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000078 void ResetGuards();
79
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000080 void PrintModuleInfo();
81
82 void PrintCoverage();
83
Kostya Serebryany379359c2016-10-05 01:09:40 +000084 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
85 size_t n);
86 void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
87 size_t n);
88
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000089 bool UsingTracePcGuard() const {return NumModules; }
90
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000091 void ProcessTORC(Dictionary *Dict, const uint8_t *Data, size_t Size);
92
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000093private:
94 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000095 bool UseValueProfile = false;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000096 size_t TotalPCCoverage = 0;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000097
Kostya Serebryany5ff481f2016-09-27 00:10:20 +000098 static const size_t kMaxNewPCIDs = 1024;
Kostya Serebryany624f59f2016-09-22 01:34:58 +000099 uintptr_t NewPCIDs[kMaxNewPCIDs];
100 size_t NumNewPCIDs = 0;
101 void AddNewPCID(uintptr_t PCID) {
102 NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID;
103 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000104
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000105 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000106 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000107 };
108
109 Module Modules[4096];
110 size_t NumModules = 0;
111 size_t NumGuards = 0;
112
113 static const size_t kNumCounters = 1 << 14;
Kostya Serebryany3ee6c212016-09-28 01:16:24 +0000114 alignas(8) uint8_t Counters[kNumCounters];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000115
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000116 static const size_t kTORCSize = 1 << 12;
117 TableOfRecentCompares<uint32_t, kTORCSize> TORC4;
118 TableOfRecentCompares<uint64_t, kTORCSize> TORC8;
119 void TORCInsert(size_t Idx, uint8_t Arg1, uint8_t Arg2) {
120 // Do nothing, too small to be interesting.
121 }
122 void TORCInsert(size_t Idx, uint16_t Arg1, uint16_t Arg2) {
123 // Do nothing, these don't usually hapen.
124 }
125 void TORCInsert(size_t Idx, uint32_t Arg1, uint32_t Arg2) {
126 TORC4.Insert(Idx, Arg1, Arg2);
127 }
128 void TORCInsert(size_t Idx, uint64_t Arg1, uint64_t Arg2) {
129 TORC8.Insert(Idx, Arg1, Arg2);
130 }
131
132 template <class T>
133 void TORCToDict(const TableOfRecentCompares<T, kTORCSize> &TORC,
134 Dictionary *Dict, const uint8_t *Data, size_t Size);
135 template <class T>
136 void TORCToDict(Dictionary *Dict, T FindInData, T Substitute,
137 const uint8_t *Data, size_t Size);
138
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000139 static const size_t kNumPCs = 1 << 24;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000140 uintptr_t PCs[kNumPCs];
141
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000142 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000143};
144
145extern TracePC TPC;
146
147} // namespace fuzzer
148
149#endif // LLVM_FUZZER_TRACE_PC