george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 1 | //===- FuzzerTracePC.h - Internal header for the Fuzzer ---------*- C++ -* ===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // fuzzer::TracePC |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef LLVM_FUZZER_TRACE_PC |
| 12 | #define LLVM_FUZZER_TRACE_PC |
| 13 | |
| 14 | #include "FuzzerDefs.h" |
| 15 | #include "FuzzerDictionary.h" |
| 16 | #include "FuzzerValueBitMap.h" |
| 17 | |
| 18 | #include <set> |
kcc | b3080d0 | 2018-07-19 22:00:48 +0000 | [diff] [blame] | 19 | #include <unordered_map> |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 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 { |
dor1s | e6729cb | 2018-07-16 15:15:34 +0000 | [diff] [blame] | 71 | public: |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 72 | void HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop); |
kcc | 98957a1 | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 73 | void HandlePCsInit(const uintptr_t *Start, const uintptr_t *Stop); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 74 | void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); |
| 75 | template <class T> void HandleCmp(uintptr_t PC, T Arg1, T Arg2); |
| 76 | size_t GetTotalPCCoverage(); |
| 77 | void SetUseCounters(bool UC) { UseCounters = UC; } |
kcc | 3850d06 | 2018-07-03 22:33:09 +0000 | [diff] [blame] | 78 | void SetUseValueProfileMask(uint32_t VPMask) { UseValueProfileMask = VPMask; } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 79 | void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 80 | void SetPrintNewFuncs(size_t P) { NumPrintNewFuncs = P; } |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 81 | void UpdateObservedPCs(); |
| 82 | template <class Callback> void CollectFeatures(Callback CB) const; |
| 83 | |
| 84 | void ResetMaps() { |
| 85 | ValueProfileMap.Reset(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 86 | ClearExtraCounters(); |
| 87 | ClearInlineCounters(); |
| 88 | } |
| 89 | |
| 90 | void ClearInlineCounters(); |
| 91 | |
| 92 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 93 | void PrintFeatureSet(); |
| 94 | |
| 95 | void PrintModuleInfo(); |
| 96 | |
| 97 | void PrintCoverage(); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 98 | |
kcc | 85cad3d | 2018-05-11 01:17:52 +0000 | [diff] [blame] | 99 | template<class CallBack> |
| 100 | void IterateCoveredFunctions(CallBack CB); |
| 101 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 102 | void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 103 | size_t n, bool StopAtZero); |
| 104 | |
| 105 | TableOfRecentCompares<uint32_t, 32> TORC4; |
| 106 | TableOfRecentCompares<uint64_t, 32> TORC8; |
| 107 | TableOfRecentCompares<Word, 32> TORCW; |
| 108 | MemMemTable<1024> MMT; |
| 109 | |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 110 | void RecordInitialStack(); |
| 111 | uintptr_t GetMaxStackOffset() const; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 112 | |
| 113 | template<class CallBack> |
| 114 | void ForEachObservedPC(CallBack CB) { |
| 115 | for (auto PC : ObservedPCs) |
| 116 | CB(PC); |
| 117 | } |
| 118 | |
kcc | 3acbe07 | 2018-05-16 23:26:37 +0000 | [diff] [blame] | 119 | void SetFocusFunction(const std::string &FuncName); |
| 120 | bool ObservedFocusFunction(); |
| 121 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 122 | private: |
| 123 | bool UseCounters = false; |
kcc | 3850d06 | 2018-07-03 22:33:09 +0000 | [diff] [blame] | 124 | uint32_t UseValueProfileMask = false; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 125 | bool DoPrintNewPCs = false; |
kcc | ec9da66 | 2017-08-28 22:52:22 +0000 | [diff] [blame] | 126 | size_t NumPrintNewFuncs = 0; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 127 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 128 | struct { uint8_t *Start, *Stop; } ModuleCounters[4096]; |
| 129 | size_t NumModulesWithInline8bitCounters; // linker-initialized. |
| 130 | size_t NumInline8bitCounters; |
| 131 | |
kcc | 98957a1 | 2017-08-25 19:29:47 +0000 | [diff] [blame] | 132 | struct PCTableEntry { |
| 133 | uintptr_t PC, PCFlags; |
| 134 | }; |
| 135 | |
| 136 | struct { const PCTableEntry *Start, *Stop; } ModulePCTable[4096]; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 137 | size_t NumPCTables; |
| 138 | size_t NumPCsInPCTables; |
| 139 | |
george.karpenkov | fbfa45c | 2017-08-27 23:20:09 +0000 | [diff] [blame] | 140 | Set<uintptr_t> ObservedPCs; |
kcc | b3080d0 | 2018-07-19 22:00:48 +0000 | [diff] [blame] | 141 | std::unordered_map<uintptr_t, uintptr_t> ObservedFuncs; // PC => Counter. |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 142 | |
kcc | 3acbe07 | 2018-05-16 23:26:37 +0000 | [diff] [blame] | 143 | std::pair<size_t, size_t> FocusFunction = {-1, -1}; // Module and PC IDs. |
| 144 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 145 | ValueBitMap ValueProfileMap; |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 146 | uintptr_t InitialStack; |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | template <class Callback> |
| 150 | // void Callback(size_t FirstFeature, size_t Idx, uint8_t Value); |
| 151 | ATTRIBUTE_NO_SANITIZE_ALL |
| 152 | void ForEachNonZeroByte(const uint8_t *Begin, const uint8_t *End, |
| 153 | size_t FirstFeature, Callback Handle8bitCounter) { |
| 154 | typedef uintptr_t LargeType; |
| 155 | const size_t Step = sizeof(LargeType) / sizeof(uint8_t); |
| 156 | const size_t StepMask = Step - 1; |
| 157 | auto P = Begin; |
| 158 | // Iterate by 1 byte until either the alignment boundary or the end. |
| 159 | for (; reinterpret_cast<uintptr_t>(P) & StepMask && P < End; P++) |
| 160 | if (uint8_t V = *P) |
| 161 | Handle8bitCounter(FirstFeature, P - Begin, V); |
| 162 | |
| 163 | // Iterate by Step bytes at a time. |
| 164 | for (; P < End; P += Step) |
| 165 | if (LargeType Bundle = *reinterpret_cast<const LargeType *>(P)) |
| 166 | for (size_t I = 0; I < Step; I++, Bundle >>= 8) |
| 167 | if (uint8_t V = Bundle & 0xff) |
| 168 | Handle8bitCounter(FirstFeature, P - Begin + I, V); |
| 169 | |
| 170 | // Iterate by 1 byte until the end. |
| 171 | for (; P < End; P++) |
| 172 | if (uint8_t V = *P) |
| 173 | Handle8bitCounter(FirstFeature, P - Begin, V); |
| 174 | } |
| 175 | |
delcypher | 64c0334 | 2017-11-28 17:41:58 +0000 | [diff] [blame] | 176 | // Given a non-zero Counter returns a number in the range [0,7]. |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 177 | template<class T> |
| 178 | unsigned CounterToFeature(T Counter) { |
dor1s | e6729cb | 2018-07-16 15:15:34 +0000 | [diff] [blame] | 179 | // Returns a feature number by placing Counters into buckets as illustrated |
| 180 | // below. |
| 181 | // |
| 182 | // Counter bucket: [1] [2] [3] [4-7] [8-15] [16-31] [32-127] [128+] |
| 183 | // Feature number: 0 1 2 3 4 5 6 7 |
| 184 | // |
| 185 | // This is a heuristic taken from AFL (see |
| 186 | // http://lcamtuf.coredump.cx/afl/technical_details.txt). |
| 187 | // |
| 188 | // This implementation may change in the future so clients should |
| 189 | // not rely on it. |
| 190 | assert(Counter); |
| 191 | unsigned Bit = 0; |
| 192 | /**/ if (Counter >= 128) Bit = 7; |
| 193 | else if (Counter >= 32) Bit = 6; |
| 194 | else if (Counter >= 16) Bit = 5; |
| 195 | else if (Counter >= 8) Bit = 4; |
| 196 | else if (Counter >= 4) Bit = 3; |
| 197 | else if (Counter >= 3) Bit = 2; |
| 198 | else if (Counter >= 2) Bit = 1; |
| 199 | return Bit; |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 200 | } |
| 201 | |
kcc | c924e38 | 2017-09-15 22:10:36 +0000 | [diff] [blame] | 202 | template <class Callback> // void Callback(size_t Feature) |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 203 | ATTRIBUTE_NO_SANITIZE_ADDRESS |
metzman | 2fe66e6 | 2019-01-17 16:36:05 +0000 | [diff] [blame] | 204 | ATTRIBUTE_NOINLINE |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 205 | void TracePC::CollectFeatures(Callback HandleFeature) const { |
kcc | 1c0379f | 2017-08-22 01:28:32 +0000 | [diff] [blame] | 206 | auto Handle8bitCounter = [&](size_t FirstFeature, |
| 207 | size_t Idx, uint8_t Counter) { |
kcc | 1f5638d | 2017-12-08 22:21:42 +0000 | [diff] [blame] | 208 | if (UseCounters) |
| 209 | HandleFeature(FirstFeature + Idx * 8 + CounterToFeature(Counter)); |
| 210 | else |
| 211 | HandleFeature(FirstFeature + Idx); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | size_t FirstFeature = 0; |
| 215 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 216 | if (NumInline8bitCounters) { |
| 217 | for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) { |
| 218 | ForEachNonZeroByte(ModuleCounters[i].Start, ModuleCounters[i].Stop, |
| 219 | FirstFeature, Handle8bitCounter); |
| 220 | FirstFeature += 8 * (ModuleCounters[i].Stop - ModuleCounters[i].Start); |
| 221 | } |
| 222 | } |
| 223 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 224 | ForEachNonZeroByte(ExtraCountersBegin(), ExtraCountersEnd(), FirstFeature, |
| 225 | Handle8bitCounter); |
| 226 | FirstFeature += (ExtraCountersEnd() - ExtraCountersBegin()) * 8; |
| 227 | |
kcc | 3850d06 | 2018-07-03 22:33:09 +0000 | [diff] [blame] | 228 | if (UseValueProfileMask) { |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 229 | ValueProfileMap.ForEach([&](size_t Idx) { |
| 230 | HandleFeature(FirstFeature + Idx); |
| 231 | }); |
| 232 | FirstFeature += ValueProfileMap.SizeInBits(); |
| 233 | } |
| 234 | |
kcc | d804ddb | 2017-12-09 19:18:10 +0000 | [diff] [blame] | 235 | // Step function, grows similar to 8 * Log_2(A). |
| 236 | auto StackDepthStepFunction = [](uint32_t A) -> uint32_t { |
dor1s | a66e776 | 2017-12-20 19:31:51 +0000 | [diff] [blame] | 237 | if (!A) return A; |
kcc | e29d7e3 | 2017-12-12 23:11:28 +0000 | [diff] [blame] | 238 | uint32_t Log2 = Log(A); |
kcc | d804ddb | 2017-12-09 19:18:10 +0000 | [diff] [blame] | 239 | if (Log2 < 3) return A; |
| 240 | Log2 -= 3; |
| 241 | return (Log2 + 1) * 8 + ((A >> Log2) & 7); |
| 242 | }; |
| 243 | assert(StackDepthStepFunction(1024) == 64); |
| 244 | assert(StackDepthStepFunction(1024 * 4) == 80); |
| 245 | assert(StackDepthStepFunction(1024 * 1024) == 144); |
| 246 | |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 247 | if (auto MaxStackOffset = GetMaxStackOffset()) |
kcc | d804ddb | 2017-12-09 19:18:10 +0000 | [diff] [blame] | 248 | HandleFeature(FirstFeature + StackDepthStepFunction(MaxStackOffset / 8)); |
george.karpenkov | 29efa6d | 2017-08-21 23:25:50 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | extern TracePC TPC; |
| 252 | |
| 253 | } // namespace fuzzer |
| 254 | |
| 255 | #endif // LLVM_FUZZER_TRACE_PC |