blob: 68827f80cbda0f13facb988c93c39e47c71ecea6 [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"
Marcos Pividori178fe582016-12-13 17:46:11 +000017#include <set>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000018
19namespace fuzzer {
20
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000021// TableOfRecentCompares (TORC) remembers the most recently performed
22// comparisons of type T.
23// We record the arguments of CMP instructions in this table unconditionally
24// because it seems cheaper this way than to compute some expensive
25// conditions inside __sanitizer_cov_trace_cmp*.
26// After the unit has been executed we may decide to use the contents of
27// this table to populate a Dictionary.
28template<class T, size_t kSizeT>
29struct TableOfRecentCompares {
30 static const size_t kSize = kSizeT;
Kostya Serebryany3364f902016-10-25 02:04:43 +000031 struct Pair {
32 T A, B;
33 };
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000034 void Insert(size_t Idx, T Arg1, T Arg2) {
35 Idx = Idx % kSize;
Kostya Serebryany3364f902016-10-25 02:04:43 +000036 Table[Idx].A = Arg1;
37 Table[Idx].B = Arg2;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000038 }
Kostya Serebryany3364f902016-10-25 02:04:43 +000039
40 Pair Get(size_t I) { return Table[I % kSize]; }
41
42 Pair Table[kSize];
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000043};
44
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000045class TracePC {
46 public:
Kostya Serebryany2c556132016-09-30 01:19:56 +000047 static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems;
48
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000049 void HandleTrace(uint32_t *guard, uintptr_t PC);
50 void HandleInit(uint32_t *start, uint32_t *stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000051 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000052 void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); }
Kostya Serebryany17d176e12016-10-13 16:19:09 +000053 template <class T> void HandleCmp(void *PC, T Arg1, T Arg2);
Kostya Serebryany275e2602016-10-25 23:52:25 +000054 size_t GetTotalPCCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000055 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000056 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000057 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +000058 template <class Callback> size_t CollectFeatures(Callback CB);
Kostya Serebryanyab73c692016-09-23 00:46:18 +000059 bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) {
60 return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap);
Kostya Serebryany17d176e12016-10-13 16:19:09 +000061 }
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000062
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000063 void ResetMaps() {
Kostya Serebryany16a145f2016-09-23 01:58:51 +000064 ValueProfileMap.Reset();
Kostya Serebryany0d26de32016-09-23 20:04:13 +000065 memset(Counters, 0, sizeof(Counters));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000066 }
67
Kostya Serebryany0800b812016-09-23 23:51:58 +000068 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
69 void PrintFeatureSet();
70
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000071 void PrintModuleInfo();
72
73 void PrintCoverage();
Mike Aizatsky9b415be2016-12-19 22:18:08 +000074 void DumpCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000075
Kostya Serebryany379359c2016-10-05 01:09:40 +000076 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
77 size_t n);
78 void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
79 size_t n);
80
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000081 bool UsingTracePcGuard() const {return NumModules; }
82
Kostya Serebryany3364f902016-10-25 02:04:43 +000083 static const size_t kTORCSize = 1 << 5;
84 TableOfRecentCompares<uint32_t, kTORCSize> TORC4;
85 TableOfRecentCompares<uint64_t, kTORCSize> TORC8;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000086
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000087 void PrintNewPCs();
Kostya Serebryany06b87572016-10-26 00:42:52 +000088 size_t GetNumPCs() const { return Min(kNumPCs, NumGuards + 1); }
89 uintptr_t GetPC(size_t Idx) {
90 assert(Idx < GetNumPCs());
91 return PCs[Idx];
92 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000093
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000094private:
95 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000096 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000097 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000098
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000100 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000101 };
102
103 Module Modules[4096];
Kostya Serebryany23567912016-11-11 23:06:53 +0000104 size_t NumModules; // linker-initialized.
105 size_t NumGuards; // linker-initialized.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000106
107 static const size_t kNumCounters = 1 << 14;
Kostya Serebryany3ee6c212016-09-28 01:16:24 +0000108 alignas(8) uint8_t Counters[kNumCounters];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000109
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000110 static const size_t kNumPCs = 1 << 24;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000111 uintptr_t PCs[kNumPCs];
112
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000113 std::set<uintptr_t> *PrintedPCs;
114
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000115 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000116};
117
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000118template <class Callback>
119size_t TracePC::CollectFeatures(Callback CB) {
120 if (!UsingTracePcGuard()) return 0;
121 size_t Res = 0;
122 const size_t Step = 8;
123 assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0);
124 size_t N = Min(kNumCounters, NumGuards + 1);
125 N = (N + Step - 1) & ~(Step - 1); // Round up.
126 for (size_t Idx = 0; Idx < N; Idx += Step) {
127 uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]);
128 if (!Bundle) continue;
129 for (size_t i = Idx; i < Idx + Step; i++) {
Kostya Serebryanyf6f82c22016-12-13 22:49:14 +0000130 uint8_t Counter = (Bundle >> ((i - Idx) * 8)) & 0xff;
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000131 if (!Counter) continue;
132 Counters[i] = 0;
133 unsigned Bit = 0;
134 /**/ if (Counter >= 128) Bit = 7;
135 else if (Counter >= 32) Bit = 6;
136 else if (Counter >= 16) Bit = 5;
137 else if (Counter >= 8) Bit = 4;
138 else if (Counter >= 4) Bit = 3;
139 else if (Counter >= 3) Bit = 2;
140 else if (Counter >= 2) Bit = 1;
141 size_t Feature = (i * 8 + Bit);
142 if (CB(Feature))
143 Res++;
144 }
145 }
146 if (UseValueProfile)
147 ValueProfileMap.ForEach([&](size_t Idx) {
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000148 if (CB(NumGuards * 8 + Idx))
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000149 Res++;
150 });
151 return Res;
152}
153
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000154extern TracePC TPC;
155
156} // namespace fuzzer
157
158#endif // LLVM_FUZZER_TRACE_PC