blob: f690bb488519d8021dd4dd9feba7d096cabc0df2 [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 Serebryanyce1cab12016-09-23 02:18:59 +000064 void ResetMaps() {
Kostya Serebryany16a145f2016-09-23 01:58:51 +000065 ValueProfileMap.Reset();
Kostya Serebryany0d26de32016-09-23 20:04:13 +000066 memset(Counters, 0, sizeof(Counters));
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000067 }
68
Kostya Serebryany0800b812016-09-23 23:51:58 +000069 void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize);
70 void PrintFeatureSet();
71
Kostya Serebryanyce1cab12016-09-23 02:18:59 +000072 void ResetGuards();
73
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000074 void PrintModuleInfo();
75
76 void PrintCoverage();
77
Kostya Serebryany379359c2016-10-05 01:09:40 +000078 void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
79 size_t n);
80 void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
81 size_t n);
82
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000083 bool UsingTracePcGuard() const {return NumModules; }
84
Kostya Serebryany3364f902016-10-25 02:04:43 +000085 static const size_t kTORCSize = 1 << 5;
86 TableOfRecentCompares<uint32_t, kTORCSize> TORC4;
87 TableOfRecentCompares<uint64_t, kTORCSize> TORC8;
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000088
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000089 void PrintNewPCs();
Kostya Serebryany06b87572016-10-26 00:42:52 +000090 size_t GetNumPCs() const { return Min(kNumPCs, NumGuards + 1); }
91 uintptr_t GetPC(size_t Idx) {
92 assert(Idx < GetNumPCs());
93 return PCs[Idx];
94 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000095
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000096private:
97 bool UseCounters = false;
Kostya Serebryanyab73c692016-09-23 00:46:18 +000098 bool UseValueProfile = false;
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000099 bool DoPrintNewPCs = false;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000100
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000101 struct Module {
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000102 uint32_t *Start, *Stop;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000103 };
104
105 Module Modules[4096];
106 size_t NumModules = 0;
107 size_t NumGuards = 0;
108
109 static const size_t kNumCounters = 1 << 14;
Kostya Serebryany3ee6c212016-09-28 01:16:24 +0000110 alignas(8) uint8_t Counters[kNumCounters];
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000111
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000112 void TORCInsert(size_t Idx, uint8_t Arg1, uint8_t Arg2) {
113 // Do nothing, too small to be interesting.
114 }
115 void TORCInsert(size_t Idx, uint16_t Arg1, uint16_t Arg2) {
116 // Do nothing, these don't usually hapen.
117 }
118 void TORCInsert(size_t Idx, uint32_t Arg1, uint32_t Arg2) {
119 TORC4.Insert(Idx, Arg1, Arg2);
120 }
121 void TORCInsert(size_t Idx, uint64_t Arg1, uint64_t Arg2) {
122 TORC8.Insert(Idx, Arg1, Arg2);
123 }
124
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000125 static const size_t kNumPCs = 1 << 24;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000126 uintptr_t PCs[kNumPCs];
127
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000128 std::set<uintptr_t> *PrintedPCs;
129
Kostya Serebryanyab73c692016-09-23 00:46:18 +0000130 ValueBitMap ValueProfileMap;
Kostya Serebryany6f5a8042016-09-21 01:50:50 +0000131};
132
133extern TracePC TPC;
134
135} // namespace fuzzer
136
137#endif // LLVM_FUZZER_TRACE_PC