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 | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 36 | void Insert(size_t Idx, T Arg1, T Arg2) { |
| 37 | Idx = Idx % kSize; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 38 | Table[Idx].A = Arg1; |
| 39 | Table[Idx].B = Arg2; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 40 | } |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 41 | |
| 42 | Pair Get(size_t I) { return Table[I % kSize]; } |
| 43 | |
| 44 | Pair Table[kSize]; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 47 | class TracePC { |
| 48 | public: |
Kostya Serebryany | 2c55613 | 2016-09-30 01:19:56 +0000 | [diff] [blame] | 49 | static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems; |
| 50 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 51 | void HandleTrace(uint32_t *guard, uintptr_t PC); |
| 52 | void HandleInit(uint32_t *start, uint32_t *stop); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 53 | void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 54 | void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); } |
Mike Aizatsky | 0e37f8e | 2017-01-17 23:11:32 +0000 | [diff] [blame] | 55 | template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2); |
Kostya Serebryany | 275e260 | 2016-10-25 23:52:25 +0000 | [diff] [blame] | 56 | size_t GetTotalPCCoverage(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 57 | void SetUseCounters(bool UC) { UseCounters = UC; } |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 58 | void SetUseValueProfile(bool VP) { UseValueProfile = VP; } |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 59 | void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 60 | template <class Callback> size_t CollectFeatures(Callback CB); |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 61 | bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) { |
| 62 | return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap); |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 63 | } |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 64 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 65 | void ResetMaps() { |
Kostya Serebryany | 16a145f | 2016-09-23 01:58:51 +0000 | [diff] [blame] | 66 | ValueProfileMap.Reset(); |
Kostya Serebryany | 0d26de3 | 2016-09-23 20:04:13 +0000 | [diff] [blame] | 67 | memset(Counters, 0, sizeof(Counters)); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 70 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 71 | void PrintFeatureSet(); |
| 72 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 73 | void PrintModuleInfo(); |
| 74 | |
| 75 | void PrintCoverage(); |
Mike Aizatsky | 9b415be | 2016-12-19 22:18:08 +0000 | [diff] [blame] | 76 | void DumpCoverage(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 77 | |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 78 | void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 79 | size_t n, bool StopAtZero); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 80 | |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 81 | bool UsingTracePcGuard() const {return NumModules; } |
| 82 | |
Kostya Serebryany | 1d8c2ce | 2017-01-17 23:09:05 +0000 | [diff] [blame] | 83 | TableOfRecentCompares<uint32_t, 32> TORC4; |
| 84 | TableOfRecentCompares<uint64_t, 32> TORC8; |
| 85 | TableOfRecentCompares<Word, 32> TORCW; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 86 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 87 | void PrintNewPCs(); |
Kostya Serebryany | 11a22bc | 2016-12-30 01:13:07 +0000 | [diff] [blame] | 88 | void InitializePrintNewPCs(); |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 89 | size_t GetNumPCs() const { return Min(kNumPCs, NumGuards + 1); } |
| 90 | uintptr_t GetPC(size_t Idx) { |
| 91 | assert(Idx < GetNumPCs()); |
| 92 | return PCs[Idx]; |
| 93 | } |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 94 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 95 | private: |
| 96 | bool UseCounters = false; |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 97 | bool UseValueProfile = false; |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 98 | bool DoPrintNewPCs = false; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 99 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 100 | struct Module { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 101 | uint32_t *Start, *Stop; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | Module Modules[4096]; |
Kostya Serebryany | 2356791 | 2016-11-11 23:06:53 +0000 | [diff] [blame] | 105 | size_t NumModules; // linker-initialized. |
| 106 | size_t NumGuards; // linker-initialized. |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 107 | |
| 108 | static const size_t kNumCounters = 1 << 14; |
Kostya Serebryany | 3ee6c21 | 2016-09-28 01:16:24 +0000 | [diff] [blame] | 109 | alignas(8) uint8_t Counters[kNumCounters]; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 110 | |
Kostya Serebryany | d19919a | 2016-10-11 01:14:41 +0000 | [diff] [blame] | 111 | static const size_t kNumPCs = 1 << 24; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 112 | uintptr_t PCs[kNumPCs]; |
| 113 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 114 | std::set<uintptr_t> *PrintedPCs; |
| 115 | |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 116 | ValueBitMap ValueProfileMap; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 119 | template <class Callback> |
| 120 | size_t TracePC::CollectFeatures(Callback CB) { |
| 121 | if (!UsingTracePcGuard()) return 0; |
| 122 | size_t Res = 0; |
| 123 | const size_t Step = 8; |
| 124 | assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0); |
| 125 | size_t N = Min(kNumCounters, NumGuards + 1); |
| 126 | N = (N + Step - 1) & ~(Step - 1); // Round up. |
| 127 | for (size_t Idx = 0; Idx < N; Idx += Step) { |
| 128 | uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]); |
| 129 | if (!Bundle) continue; |
| 130 | for (size_t i = Idx; i < Idx + Step; i++) { |
Kostya Serebryany | f6f82c2 | 2016-12-13 22:49:14 +0000 | [diff] [blame] | 131 | uint8_t Counter = (Bundle >> ((i - Idx) * 8)) & 0xff; |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 132 | if (!Counter) continue; |
| 133 | Counters[i] = 0; |
| 134 | unsigned Bit = 0; |
| 135 | /**/ if (Counter >= 128) Bit = 7; |
| 136 | else if (Counter >= 32) Bit = 6; |
| 137 | else if (Counter >= 16) Bit = 5; |
| 138 | else if (Counter >= 8) Bit = 4; |
| 139 | else if (Counter >= 4) Bit = 3; |
| 140 | else if (Counter >= 3) Bit = 2; |
| 141 | else if (Counter >= 2) Bit = 1; |
| 142 | size_t Feature = (i * 8 + Bit); |
| 143 | if (CB(Feature)) |
| 144 | Res++; |
| 145 | } |
| 146 | } |
| 147 | if (UseValueProfile) |
| 148 | ValueProfileMap.ForEach([&](size_t Idx) { |
Kostya Serebryany | 00e638e | 2016-12-17 02:03:34 +0000 | [diff] [blame] | 149 | if (CB(NumGuards * 8 + Idx)) |
Kostya Serebryany | fe1094b | 2016-12-05 23:35:22 +0000 | [diff] [blame] | 150 | Res++; |
| 151 | }); |
| 152 | return Res; |
| 153 | } |
| 154 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 155 | extern TracePC TPC; |
| 156 | |
| 157 | } // namespace fuzzer |
| 158 | |
| 159 | #endif // LLVM_FUZZER_TRACE_PC |