george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- 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" |
| 16 | #include "FuzzerDictionary.h" |
| 17 | #include "FuzzerValueBitMap.h" |
| 18 | |
| 19 | #include <set> |
| 20 | |
| 21 | namespace fuzzer { |
| 22 | |
| 23 | // 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. |
| 30 | template<class T, size_t kSizeT> |
| 31 | struct TableOfRecentCompares { |
| 32 | static const size_t kSize = kSizeT; |
| 33 | struct Pair { |
| 34 | T A, B; |
| 35 | }; |
| 36 | ATTRIBUTE_NO_SANITIZE_ALL |
| 37 | void Insert(size_t Idx, const T &Arg1, const T &Arg2) { |
| 38 | Idx = Idx % kSize; |
| 39 | Table[Idx].A = Arg1; |
| 40 | Table[Idx].B = Arg2; |
| 41 | } |
| 42 | |
| 43 | Pair Get(size_t I) { return Table[I % kSize]; } |
| 44 | |
| 45 | Pair Table[kSize]; |
| 46 | }; |
| 47 | |
| 48 | template <size_t kSizeT> |
| 49 | struct MemMemTable { |
| 50 | static const size_t kSize = kSizeT; |
| 51 | Word MemMemWords[kSize]; |
| 52 | Word EmptyWord; |
| 53 | |
| 54 | void Add(const uint8_t *Data, size_t Size) { |
| 55 | if (Size <= 2) return; |
| 56 | Size = std::min(Size, Word::GetMaxSize()); |
| 57 | size_t Idx = SimpleFastHash(Data, Size) % kSize; |
| 58 | MemMemWords[Idx].Set(Data, Size); |
| 59 | } |
| 60 | const Word &Get(size_t Idx) { |
| 61 | for (size_t i = 0; i < kSize; i++) { |
| 62 | const Word &W = MemMemWords[(Idx + i) % kSize]; |
| 63 | if (W.size()) return W; |
| 64 | } |
| 65 | EmptyWord.Set(nullptr, 0); |
| 66 | return EmptyWord; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | class TracePC { |
| 71 | public: |
| 72 | static const size_t kNumPCs = 1 << 21; |
| 73 | // How many bits of PC are used from __sanitizer_cov_trace_pc. |
| 74 | static const size_t kTracePcBits = 18; |
| 75 | |
| 76 | void HandleInit(uint32_t *Start, uint32_t *Stop); |
| 77 | void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop); |
| 78 | void HandlePCsInit(const uint8_t *Start, const uint8_t *Stop); |
| 79 | void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); |
| 80 | template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2); |
| 81 | size_t GetTotalPCCoverage(); |
| 82 | void SetUseCounters(bool UC) { UseCounters = UC; } |
| 83 | void SetUseValueProfile(bool VP) { UseValueProfile = VP; } |
| 84 | void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } |
| 85 | void UpdateObservedPCs(); |
| 86 | template <class Callback> void CollectFeatures(Callback CB) const; |
| 87 | |
| 88 | void ResetMaps() { |
| 89 | ValueProfileMap.Reset(); |
| 90 | if (NumModules) |
| 91 | memset(Counters(), 0, GetNumPCs()); |
| 92 | ClearExtraCounters(); |
| 93 | ClearInlineCounters(); |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 94 | ClearClangCounters(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ClearInlineCounters(); |
| 98 | |
| 99 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 100 | void PrintFeatureSet(); |
| 101 | |
| 102 | void PrintModuleInfo(); |
| 103 | |
| 104 | void PrintCoverage(); |
| 105 | void DumpCoverage(); |
| 106 | |
| 107 | void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 108 | size_t n, bool StopAtZero); |
| 109 | |
| 110 | TableOfRecentCompares<uint32_t, 32> TORC4; |
| 111 | TableOfRecentCompares<uint64_t, 32> TORC8; |
| 112 | TableOfRecentCompares<Word, 32> TORCW; |
| 113 | MemMemTable<1024> MMT; |
| 114 | |
| 115 | size_t GetNumPCs() const { |
| 116 | return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1); |
| 117 | } |
| 118 | uintptr_t GetPC(size_t Idx) { |
| 119 | assert(Idx < GetNumPCs()); |
| 120 | return PCs()[Idx]; |
| 121 | } |
| 122 | |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 123 | void RecordInitialStack(); |
| 124 | uintptr_t GetMaxStackOffset() const; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 125 | |
| 126 | template<class CallBack> |
| 127 | void ForEachObservedPC(CallBack CB) { |
| 128 | for (auto PC : ObservedPCs) |
| 129 | CB(PC); |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | bool UseCounters = false; |
| 134 | bool UseValueProfile = false; |
| 135 | bool DoPrintNewPCs = false; |
| 136 | |
| 137 | struct Module { |
| 138 | uint32_t *Start, *Stop; |
| 139 | }; |
| 140 | |
| 141 | Module Modules[4096]; |
| 142 | size_t NumModules; // linker-initialized. |
| 143 | size_t NumGuards; // linker-initialized. |
| 144 | |
| 145 | struct { uint8_t *Start, *Stop; } ModuleCounters[4096]; |
| 146 | size_t NumModulesWithInline8bitCounters; // linker-initialized. |
| 147 | size_t NumInline8bitCounters; |
| 148 | |
| 149 | struct { const uintptr_t *Start, *Stop; } ModulePCTable[4096]; |
| 150 | size_t NumPCTables; |
| 151 | size_t NumPCsInPCTables; |
| 152 | |
| 153 | uint8_t *Counters() const; |
| 154 | uintptr_t *PCs() const; |
| 155 | |
| 156 | std::set<uintptr_t> ObservedPCs; |
| 157 | |
| 158 | ValueBitMap ValueProfileMap; |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 159 | uintptr_t InitialStack; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | template <class Callback> |
| 163 | // void Callback(size_t FirstFeature, size_t Idx, uint8_t Value); |
| 164 | ATTRIBUTE_NO_SANITIZE_ALL |
| 165 | void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End, |
| 166 | size_t FirstFeature, Callback Handle8bitCounter) { |
| 167 | typedef uintptr_t LargeType; |
| 168 | const size_t Step = sizeof(LargeType) / sizeof(uint8_t); |
| 169 | const size_t StepMask = Step - 1; |
| 170 | auto P = Begin; |
| 171 | // Iterate by 1 byte until either the alignment boundary or the end. |
| 172 | for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++) |
| 173 | if (uint8_t V = *P) |
| 174 | Handle8bitCounter(FirstFeature, P - Begin, V); |
| 175 | |
| 176 | // Iterate by Step bytes at a time. |
| 177 | for (; P < End; P += Step) |
| 178 | if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) |
| 179 | for (size_t I = 0; I < Step; I++, Bundle >>= 8) |
| 180 | if (uint8_t V = Bundle & 0xff) |
| 181 | Handle8bitCounter(FirstFeature, P - Begin + I, V); |
| 182 | |
| 183 | // Iterate by 1 byte until the end. |
| 184 | for (; P < End; P++) |
| 185 | if (uint8_t V = *P) |
| 186 | Handle8bitCounter(FirstFeature, P - Begin, V); |
| 187 | } |
| 188 | |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 189 | // Given a non-zero Counters returns a number in [0,7]. |
| 190 | template<class T> |
| 191 | unsigned CounterToFeature(T Counter) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 192 | assert(Counter); |
| 193 | unsigned Bit = 0; |
| 194 | /**/ if (Counter >= 128) Bit = 7; |
| 195 | else if (Counter >= 32) Bit = 6; |
| 196 | else if (Counter >= 16) Bit = 5; |
| 197 | else if (Counter >= 8) Bit = 4; |
| 198 | else if (Counter >= 4) Bit = 3; |
| 199 | else if (Counter >= 3) Bit = 2; |
| 200 | else if (Counter >= 2) Bit = 1; |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 201 | return Bit; |
| 202 | } |
| 203 | |
| 204 | template <class Callback> // bool Callback(size_t Feature) |
| 205 | ATTRIBUTE_NO_SANITIZE_ADDRESS |
| 206 | __attribute__((noinline)) |
| 207 | void TracePC::CollectFeatures(Callback HandleFeature) const { |
| 208 | uint8_t *Counters = this->Counters(); |
| 209 | size_t N = GetNumPCs(); |
| 210 | auto Handle8bitCounter = [&](size_t FirstFeature, |
| 211 | size_t Idx, uint8_t Counter) { |
| 212 | HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter)); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | size_t FirstFeature = 0; |
| 216 | |
| 217 | if (!NumInline8bitCounters) { |
| 218 | ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter); |
| 219 | FirstFeature += N * 8; |
| 220 | } |
| 221 | |
| 222 | if (NumInline8bitCounters) { |
| 223 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { |
| 224 | ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop, |
| 225 | FirstFeature, Handle8bitCounter); |
| 226 | FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start); |
| 227 | } |
| 228 | } |
| 229 | |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame^] | 230 | if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin()) { |
| 231 | auto P = ClangCountersBegin(); |
| 232 | for (size_t Idx = 0; Idx < NumClangCounters; Idx++) |
| 233 | if (auto Cnt = P[Idx]) |
| 234 | HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Cnt)); |
| 235 | FirstFeature += NumClangCounters; |
| 236 | } |
| 237 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 238 | ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature, |
| 239 | Handle8bitCounter); |
| 240 | FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8; |
| 241 | |
| 242 | if (UseValueProfile) { |
| 243 | ValueProfileMap.ForEach([&](size_t Idx) { |
| 244 | HandleFeature(FirstFeature + Idx); |
| 245 | }); |
| 246 | FirstFeature += ValueProfileMap.SizeInBits(); |
| 247 | } |
| 248 | |
| 249 | if (auto MaxStackOffset = GetMaxStackOffset()) |
| 250 | HandleFeature(FirstFeature + MaxStackOffset); |
| 251 | } |
| 252 | |
| 253 | extern TracePC TPC; |
| 254 | |
| 255 | } // namespace fuzzer |
| 256 | |
| 257 | #endif // LLVM_FUZZER_TRACE_PC |