blob: 5ec8c590b4df63fc1a5fc7af4aab7b06d71ea268 [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 Serebryany1d8c2ce2017-01-17 23:09:05 +000016#include "FuzzerDictionary.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000017#include "FuzzerValueBitMap.h"
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000018
Marcos Pividori178fe582016-12-13 17:46:11 +000019#include <set>
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000020
21namespace fuzzer {
22
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000023// TableOfRecentCompares (TORC) remembers the most recently performed
24// comparisons of type T.
25// We record the arguments of CMP instructions in this table unconditionally
26// because it seems cheaper this way than to compute some expensive
27// conditions inside __sanitizer_cov_trace_cmp*.
28// After the unit has been executed we may decide to use the contents of
29// this table to populate a Dictionary.
30template<class T, size_t kSizeT>
31struct TableOfRecentCompares {
32 static const size_t kSize = kSizeT;
Kostya Serebryany3364f902016-10-25 02:04:43 +000033 struct Pair {
34 T A, B;
35 };
Kostya Serebryany7f058972017-01-27 00:09:59 +000036 ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanyaf2dfce2017-03-31 02:21:28 +000037 void Insert(size_t Idx, const T &Arg1, const T &Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000038 Idx = Idx % kSize;
Kostya Serebryany3364f902016-10-25 02:04:43 +000039 Table[Idx].A = Arg1;
40 Table[Idx].B = Arg2;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000041 }
Kostya Serebryany3364f902016-10-25 02:04:43 +000042
43 Pair Get(size_t I) { return Table[I % kSize]; }
44
45 Pair Table[kSize];
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000046};
47
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000048class TracePC {
49 public:
Kostya Serebryany68382d02017-02-02 19:56:01 +000050 static const size_t kNumPCs = 1 << 21;
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +000051 // How many bits of PC are used from __sanitizer_cov_trace_pc.
52 static const size_t kTracePcBits = 18;
Kostya Serebryany2c556132016-09-30 01:19:56 +000053
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000054 void HandleInit(uint32_t *Start, uint32_t *Stop);
55 void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000056 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +000057 template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
Kostya Serebryany275e2602016-10-25 23:52:25 +000058 size_t GetTotalPCCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000059 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000060 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000061 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
Kostya Serebryanya617e162017-03-31 04:17:45 +000062 template <class Callback> void CollectFeatures(Callback CB) const;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000063
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000064 void ResetMaps() {
Kostya Serebryany16a145f2016-09-23 01:58:51 +000065 ValueProfileMap.Reset();
Kostya Serebryany68382d02017-02-02 19:56:01 +000066 memset(Counters(), 0, GetNumPCs());
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000067 ClearExtraCounters();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000068 }
69
Kostya Serebryany0800b812016-09-23 23:51:58 +000070 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
71 void PrintFeatureSet();
72
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000073 void PrintModuleInfo();
74
75 void PrintCoverage();
Mike Aizatsky9b415be2016-12-19 22:18:08 +000076 void DumpCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000077
Kostya Serebryany379359c2016-10-05 01:09:40 +000078 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000079 size_t n, bool StopAtZero);
Kostya Serebryany379359c2016-10-05 01:09:40 +000080
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000081 TableOfRecentCompares<uint32_t, 32> TORC4;
82 TableOfRecentCompares<uint64_t, 32> TORC8;
83 TableOfRecentCompares<Word, 32> TORCW;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000084
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000085 void PrintNewPCs();
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000086 void InitializePrintNewPCs();
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +000087 size_t GetNumPCs() const {
88 return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1);
89 }
Kostya Serebryany06b87572016-10-26 00:42:52 +000090 uintptr_t GetPC(size_t Idx) {
91 assert(Idx < GetNumPCs());
Kostya Serebryany68382d02017-02-02 19:56:01 +000092 return PCs()[Idx];
Kostya Serebryany06b87572016-10-26 00:42:52 +000093 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000094
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000095private:
96 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000097 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000098 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000100 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000101 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000102 };
103
104 Module Modules[4096];
Kostya Serebryany23567912016-11-11 23:06:53 +0000105 size_t NumModules; // linker-initialized.
106 size_t NumGuards; // linker-initialized.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000107
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000108 struct { uint8_t *Start, *Stop; } ModuleCounters[4096];
109 size_t NumModulesWithInline8bitCounters; // linker-initialized.
110 size_t NumInline8bitCounters;
111
Kostya Serebryany68382d02017-02-02 19:56:01 +0000112 uint8_t *Counters() const;
113 uintptr_t *PCs() const;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000114
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000115 std::set<uintptr_t> *PrintedPCs;
116
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000117 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000118};
119
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000120template <class Callback> // void Callback(size_t Idx, uint8_t Value);
121ATTRIBUTE_NO_SANITIZE_ALL
122void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End,
123 size_t FirstFeature, Callback Handle8bitCounter) {
124 typedef uintptr_t LargeType;
125 const size_t Step = sizeof(LargeType) / sizeof(uint8_t);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000126 const size_t StepMask = Step - 1;
127 auto P = Begin;
128 // Iterate by 1 byte until either the alignment boundary or the end.
129 for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++)
130 if (uint8_t V = *P)
131 Handle8bitCounter(FirstFeature + P - Begin, V);
132
133 // Iterate by Step bytes at a time.
134 for (; P < End; P += Step)
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000135 if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P))
136 for (size_t I = 0; I < Step; I++, Bundle >>= 8)
137 if (uint8_t V = Bundle & 0xff)
138 Handle8bitCounter(FirstFeature + P - Begin + I, V);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000139
140 // Iterate by 1 byte until the end.
141 for (; P < End; P++)
142 if (uint8_t V = *P)
143 Handle8bitCounter(FirstFeature + P - Begin, V);
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000144}
145
146template <class Callback> // bool Callback(size_t Feature)
147ATTRIBUTE_NO_SANITIZE_ALL
148__attribute__((noinline))
Kostya Serebryanya617e162017-03-31 04:17:45 +0000149void TracePC::CollectFeatures(Callback HandleFeature) const {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000150 uint8_t *Counters = this->Counters();
Kostya Serebryany7856fb32017-01-26 01:34:58 +0000151 size_t N = GetNumPCs();
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000152 auto Handle8bitCounter = [&](size_t Idx, uint8_t Counter) {
153 assert(Counter);
154 unsigned Bit = 0;
155 /**/ if (Counter >= 128) Bit = 7;
156 else if (Counter >= 32) Bit = 6;
157 else if (Counter >= 16) Bit = 5;
158 else if (Counter >= 8) Bit = 4;
159 else if (Counter >= 4) Bit = 3;
160 else if (Counter >= 3) Bit = 2;
161 else if (Counter >= 2) Bit = 1;
Kostya Serebryanya617e162017-03-31 04:17:45 +0000162 HandleFeature(Idx * 8 + Bit);
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000163 };
164
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000165 size_t FirstFeature = 0;
166 ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter);
167 FirstFeature += N * 8;
168 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
169 ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop,
170 FirstFeature, Handle8bitCounter);
171 FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start);
172 }
173
174 ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature,
Kostya Serebryany6ca44f92017-03-23 22:43:12 +0000175 Handle8bitCounter);
176
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000177 if (UseValueProfile)
178 ValueProfileMap.ForEach([&](size_t Idx) {
Kostya Serebryanya617e162017-03-31 04:17:45 +0000179 HandleFeature(N * 8 + Idx);
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000180 });
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000181}
182
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000183extern TracePC TPC;
184
185} // namespace fuzzer
186
187#endif // LLVM_FUZZER_TRACE_PC