Kostya Serebryany | 6f5a804 | 2016-09-21 01:50: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" |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 16 | #include "FuzzerDictionary.h" |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 17 | #include "FuzzerValueBitMap.h" |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 18 | |
Marcos Pividori | 178fe58 | 2016-12-13 17:46:11 +0000 | [diff] [blame] | 19 | #include <set> |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 20 | |
| 21 | namespace fuzzer { |
| 22 | |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 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; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 33 | struct Pair { |
| 34 | T A, B; |
| 35 | }; |
Kostya Serebryany | 7f05897 | 2017-01-27 00:09:59 +0000 | [diff] [blame] | 36 | ATTRIBUTE_NO_SANITIZE_ALL |
Kostya Serebryany | af2dfce | 2017-03-31 02:21:28 +0000 | [diff] [blame] | 37 | void Insert(size_t Idx, const T &Arg1, const T &Arg2) { |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 38 | Idx = Idx % kSize; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 39 | Table[Idx].A = Arg1; |
| 40 | Table[Idx].B = Arg2; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 41 | } |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 42 | |
| 43 | Pair Get(size_t I) { return Table[I % kSize]; } |
| 44 | |
| 45 | Pair Table[kSize]; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Kostya Serebryany | f64b848 | 2017-07-14 00:06:27 +0000 | [diff] [blame] | 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 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 70 | class TracePC { |
| 71 | public: |
Kostya Serebryany | 68382d0 | 2017-02-02 19:56:01 +0000 | [diff] [blame] | 72 | static const size_t kNumPCs = 1 << 21; |
Kostya Serebryany | d7d1d51 | 2017-03-30 01:27:20 +0000 | [diff] [blame] | 73 | // How many bits of PC are used from __sanitizer_cov_trace_pc. |
| 74 | static const size_t kTracePcBits = 18; |
Kostya Serebryany | 2c55613 | 2016-09-30 01:19:56 +0000 | [diff] [blame] | 75 | |
Kostya Serebryany | f2d4dcb | 2017-06-13 22:31:21 +0000 | [diff] [blame] | 76 | void HandleInit(uint32_t *Start, uint32_t *Stop); |
| 77 | void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 78 | void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); |
Mike Aizatsky | 0e37f8e | 2017-01-17 23:11:32 +0000 | [diff] [blame] | 79 | template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2); |
Kostya Serebryany | 275e260 | 2016-10-25 23:52:25 +0000 | [diff] [blame] | 80 | size_t GetTotalPCCoverage(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 81 | void SetUseCounters(bool UC) { UseCounters = UC; } |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 82 | void SetUseValueProfile(bool VP) { UseValueProfile = VP; } |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 83 | void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } |
Kostya Serebryany | a617e16 | 2017-03-31 04:17:45 +0000 | [diff] [blame] | 84 | template <class Callback> void CollectFeatures(Callback CB) const; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 85 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 86 | void ResetMaps() { |
Kostya Serebryany | 16a145f | 2016-09-23 01:58:51 +0000 | [diff] [blame] | 87 | ValueProfileMap.Reset(); |
Kostya Serebryany | 68382d0 | 2017-02-02 19:56:01 +0000 | [diff] [blame] | 88 | memset(Counters(), 0, GetNumPCs()); |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 89 | ClearExtraCounters(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 92 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 93 | void PrintFeatureSet(); |
| 94 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 95 | void PrintModuleInfo(); |
| 96 | |
| 97 | void PrintCoverage(); |
Mike Aizatsky | 9b415be | 2016-12-19 22:18:08 +0000 | [diff] [blame] | 98 | void DumpCoverage(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 99 | |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 100 | void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 101 | size_t n, bool StopAtZero); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 102 | |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 103 | TableOfRecentCompares<uint32_t, 32> TORC4; |
| 104 | TableOfRecentCompares<uint64_t, 32> TORC8; |
| 105 | TableOfRecentCompares<Word, 32> TORCW; |
Kostya Serebryany | f64b848 | 2017-07-14 00:06:27 +0000 | [diff] [blame] | 106 | MemMemTable<1024> MMT; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 107 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 108 | void PrintNewPCs(); |
Kostya Serebryany | 11a22bc | 2016-12-30 01:13:07 +0000 | [diff] [blame] | 109 | void InitializePrintNewPCs(); |
Kostya Serebryany | d7d1d51 | 2017-03-30 01:27:20 +0000 | [diff] [blame] | 110 | size_t GetNumPCs() const { |
| 111 | return NumGuards == 0 ? (1 << kTracePcBits) : Min(kNumPCs, NumGuards + 1); |
| 112 | } |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 113 | uintptr_t GetPC(size_t Idx) { |
| 114 | assert(Idx < GetNumPCs()); |
Kostya Serebryany | 68382d0 | 2017-02-02 19:56:01 +0000 | [diff] [blame] | 115 | return PCs()[Idx]; |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 116 | } |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 117 | |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 118 | void RecordCurrentStack() { |
| 119 | uintptr_t Stack = GetCurrentStack(); |
| 120 | if (Stack < LowestStack) |
| 121 | LowestStack = Stack; |
| 122 | } |
| 123 | void RecordInitialStack() { |
| 124 | InitialStack = GetCurrentStack(); |
| 125 | LowestStack = InitialStack; |
| 126 | } |
| 127 | uintptr_t GetCurrentStack() const { |
| 128 | return reinterpret_cast<uintptr_t>(__builtin_frame_address(0)); |
| 129 | } |
| 130 | uintptr_t GetMaxStackOffset() const { return InitialStack - LowestStack; } |
| 131 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 132 | private: |
| 133 | bool UseCounters = false; |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 134 | bool UseValueProfile = false; |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 135 | bool DoPrintNewPCs = false; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 136 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 137 | struct Module { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 138 | uint32_t *Start, *Stop; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | Module Modules[4096]; |
Kostya Serebryany | 2356791 | 2016-11-11 23:06:53 +0000 | [diff] [blame] | 142 | size_t NumModules; // linker-initialized. |
| 143 | size_t NumGuards; // linker-initialized. |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 144 | |
Kostya Serebryany | f2d4dcb | 2017-06-13 22:31:21 +0000 | [diff] [blame] | 145 | struct { uint8_t *Start, *Stop; } ModuleCounters[4096]; |
| 146 | size_t NumModulesWithInline8bitCounters; // linker-initialized. |
| 147 | size_t NumInline8bitCounters; |
| 148 | |
Kostya Serebryany | 68382d0 | 2017-02-02 19:56:01 +0000 | [diff] [blame] | 149 | uint8_t *Counters() const; |
| 150 | uintptr_t *PCs() const; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 151 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 152 | std::set<uintptr_t> *PrintedPCs; |
| 153 | |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 154 | ValueBitMap ValueProfileMap; |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 155 | uintptr_t InitialStack, LowestStack; // Assume stack grows down. |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 158 | template <class Callback> // void Callback(size_t Idx, uint8_t Value); |
| 159 | ATTRIBUTE_NO_SANITIZE_ALL |
| 160 | void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End, |
| 161 | size_t FirstFeature, Callback Handle8bitCounter) { |
| 162 | typedef uintptr_t LargeType; |
| 163 | const size_t Step = sizeof(LargeType) / sizeof(uint8_t); |
Kostya Serebryany | f2d4dcb | 2017-06-13 22:31:21 +0000 | [diff] [blame] | 164 | const size_t StepMask = Step - 1; |
| 165 | auto P = Begin; |
| 166 | // Iterate by 1 byte until either the alignment boundary or the end. |
| 167 | for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++) |
| 168 | if (uint8_t V = *P) |
| 169 | Handle8bitCounter(FirstFeature + P - Begin, V); |
| 170 | |
| 171 | // Iterate by Step bytes at a time. |
| 172 | for (; P < End; P += Step) |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 173 | if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) |
| 174 | for (size_t I = 0; I < Step; I++, Bundle >>= 8) |
| 175 | if (uint8_t V = Bundle & 0xff) |
| 176 | Handle8bitCounter(FirstFeature + P - Begin + I, V); |
Kostya Serebryany | f2d4dcb | 2017-06-13 22:31:21 +0000 | [diff] [blame] | 177 | |
| 178 | // Iterate by 1 byte until the end. |
| 179 | for (; P < End; P++) |
| 180 | if (uint8_t V = *P) |
| 181 | Handle8bitCounter(FirstFeature + P - Begin, V); |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | template <class Callback> // bool Callback(size_t Feature) |
Kostya Serebryany | 6eab1a8 | 2017-07-25 22:05:31 +0000 | [diff] [blame] | 185 | ATTRIBUTE_NO_SANITIZE_ADDRESS |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 186 | __attribute__((noinline)) |
Kostya Serebryany | a617e16 | 2017-03-31 04:17:45 +0000 | [diff] [blame] | 187 | void TracePC::CollectFeatures(Callback HandleFeature) const { |
Kostya Serebryany | 68382d0 | 2017-02-02 19:56:01 +0000 | [diff] [blame] | 188 | uint8_t *Counters = this->Counters(); |
Kostya Serebryany | 7856fb3 | 2017-01-26 01:34:58 +0000 | [diff] [blame] | 189 | size_t N = GetNumPCs(); |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 190 | auto Handle8bitCounter = [&](size_t Idx, uint8_t Counter) { |
| 191 | assert(Counter); |
| 192 | unsigned Bit = 0; |
| 193 | /**/ if (Counter >= 128) Bit = 7; |
| 194 | else if (Counter >= 32) Bit = 6; |
| 195 | else if (Counter >= 16) Bit = 5; |
| 196 | else if (Counter >= 8) Bit = 4; |
| 197 | else if (Counter >= 4) Bit = 3; |
| 198 | else if (Counter >= 3) Bit = 2; |
| 199 | else if (Counter >= 2) Bit = 1; |
Kostya Serebryany | a617e16 | 2017-03-31 04:17:45 +0000 | [diff] [blame] | 200 | HandleFeature(Idx * 8 + Bit); |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
Kostya Serebryany | f2d4dcb | 2017-06-13 22:31:21 +0000 | [diff] [blame] | 203 | size_t FirstFeature = 0; |
| 204 | ForEachNonZeroByte(Counters, Counters + N, FirstFeature, Handle8bitCounter); |
| 205 | FirstFeature += N * 8; |
| 206 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { |
| 207 | ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop, |
| 208 | FirstFeature, Handle8bitCounter); |
| 209 | FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start); |
| 210 | } |
| 211 | |
| 212 | ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature, |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 213 | Handle8bitCounter); |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 214 | FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8; |
Kostya Serebryany | 6ca44f9 | 2017-03-23 22:43:12 +0000 | [diff] [blame] | 215 | |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 216 | if (UseValueProfile) { |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 217 | ValueProfileMap.ForEach([&](size_t Idx) { |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 218 | HandleFeature(FirstFeature + Idx); |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 219 | }); |
Kostya Serebryany | e55828c | 2017-07-20 01:35:17 +0000 | [diff] [blame] | 220 | FirstFeature += ValueProfileMap.SizeInBits(); |
| 221 | } |
| 222 | |
| 223 | if (auto MaxStackOffset = GetMaxStackOffset()) |
| 224 | HandleFeature(FirstFeature + MaxStackOffset); |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 227 | extern TracePC TPC; |
| 228 | |
| 229 | } // namespace fuzzer |
| 230 | |
| 231 | #endif // LLVM_FUZZER_TRACE_PC |