blob: b33d38747a354464727c97268141c6440f2c3844 [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 Serebryanya5f94fb2016-10-14 20:20:33 +000037 void Insert(size_t Idx, T Arg1, T Arg2) {
38 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 Serebryany2c556132016-09-30 01:19:56 +000051
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000052 void HandleTrace(uint32_t *guard, uintptr_t PC);
53 void HandleInit(uint32_t *start, uint32_t *stop);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000054 void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee);
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +000055 template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2);
Kostya Serebryany275e2602016-10-25 23:52:25 +000056 size_t GetTotalPCCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000057 void SetUseCounters(bool UC) { UseCounters = UC; }
Kostya Serebryanyab73c692016-09-23 00:46:18 +000058 void SetUseValueProfile(bool VP) { UseValueProfile = VP; }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000059 void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; }
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +000060 template <class Callback> size_t CollectFeatures(Callback CB);
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000061
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000062 void ResetMaps() {
Kostya Serebryany16a145f2016-09-23 01:58:51 +000063 ValueProfileMap.Reset();
Kostya Serebryany68382d02017-02-02 19:56:01 +000064 memset(Counters(), 0, GetNumPCs());
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000065 }
66
Kostya Serebryany0800b812016-09-23 23:51:58 +000067 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
68 void PrintFeatureSet();
69
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000070 void PrintModuleInfo();
71
72 void PrintCoverage();
Mike Aizatsky9b415be2016-12-19 22:18:08 +000073 void DumpCoverage();
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000074
Kostya Serebryany379359c2016-10-05 01:09:40 +000075 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000076 size_t n, bool StopAtZero);
Kostya Serebryany379359c2016-10-05 01:09:40 +000077
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000078 bool UsingTracePcGuard() const {return NumModules; }
79
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +000080 TableOfRecentCompares<uint32_t, 32> TORC4;
81 TableOfRecentCompares<uint64_t, 32> TORC8;
82 TableOfRecentCompares<Word, 32> TORCW;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000083
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000084 void PrintNewPCs();
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000085 void InitializePrintNewPCs();
Kostya Serebryany06b87572016-10-26 00:42:52 +000086 size_t GetNumPCs() const { return Min(kNumPCs, NumGuards + 1); }
87 uintptr_t GetPC(size_t Idx) {
88 assert(Idx < GetNumPCs());
Kostya Serebryany68382d02017-02-02 19:56:01 +000089 return PCs()[Idx];
Kostya Serebryany06b87572016-10-26 00:42:52 +000090 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000091
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000092private:
93 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000094 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000095 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000096
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000097 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000098 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000099 };
100
101 Module Modules[4096];
Kostya Serebryany23567912016-11-11 23:06:53 +0000102 size_t NumModules; // linker-initialized.
103 size_t NumGuards; // linker-initialized.
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000104
Kostya Serebryany68382d02017-02-02 19:56:01 +0000105 uint8_t *Counters() const;
106 uintptr_t *PCs() const;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000107
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000108 std::set<uintptr_t> *PrintedPCs;
109
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000110 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000111};
112
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000113template <class Callback>
114size_t TracePC::CollectFeatures(Callback CB) {
115 if (!UsingTracePcGuard()) return 0;
116 size_t Res = 0;
117 const size_t Step = 8;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000118 uint8_t *Counters = this->Counters();
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000119 assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0);
Kostya Serebryany7856fb32017-01-26 01:34:58 +0000120 size_t N = GetNumPCs();
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000121 N = (N + Step - 1) & ~(Step - 1); // Round up.
122 for (size_t Idx = 0; Idx < N; Idx += Step) {
123 uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]);
124 if (!Bundle) continue;
125 for (size_t i = Idx; i < Idx + Step; i++) {
Kostya Serebryanyf6f82c22016-12-13 22:49:14 +0000126 uint8_t Counter = (Bundle >> ((i - Idx) * 8)) & 0xff;
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000127 if (!Counter) continue;
128 Counters[i] = 0;
129 unsigned Bit = 0;
130 /**/ if (Counter >= 128) Bit = 7;
131 else if (Counter >= 32) Bit = 6;
132 else if (Counter >= 16) Bit = 5;
133 else if (Counter >= 8) Bit = 4;
134 else if (Counter >= 4) Bit = 3;
135 else if (Counter >= 3) Bit = 2;
136 else if (Counter >= 2) Bit = 1;
137 size_t Feature = (i * 8 + Bit);
138 if (CB(Feature))
139 Res++;
140 }
141 }
142 if (UseValueProfile)
143 ValueProfileMap.ForEach([&](size_t Idx) {
Kostya Serebryany7856fb32017-01-26 01:34:58 +0000144 if (CB(N * 8 + Idx))
Kostya Serebryanyfe1094b2016-12-05 23:35:22 +0000145 Res++;
146 });
147 return Res;
148}
149
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000150extern TracePC TPC;
151
152} // namespace fuzzer
153
154#endif // LLVM_FUZZER_TRACE_PC