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 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 15 | #include <set> |
| 16 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 17 | #include "FuzzerDefs.h" |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 18 | #include "FuzzerValueBitMap.h" |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 19 | |
| 20 | namespace fuzzer { |
| 21 | |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 22 | // 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. |
| 29 | template<class T, size_t kSizeT> |
| 30 | struct TableOfRecentCompares { |
| 31 | static const size_t kSize = kSizeT; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 32 | struct Pair { |
| 33 | T A, B; |
| 34 | }; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 35 | void Insert(size_t Idx, T Arg1, T Arg2) { |
| 36 | Idx = Idx % kSize; |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 37 | Table[Idx].A = Arg1; |
| 38 | Table[Idx].B = Arg2; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 39 | } |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 40 | |
| 41 | Pair Get(size_t I) { return Table[I % kSize]; } |
| 42 | |
| 43 | Pair Table[kSize]; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 46 | class TracePC { |
| 47 | public: |
Kostya Serebryany | 2c55613 | 2016-09-30 01:19:56 +0000 | [diff] [blame] | 48 | static const size_t kFeatureSetSize = ValueBitMap::kNumberOfItems; |
| 49 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 50 | void HandleTrace(uint32_t *guard, uintptr_t PC); |
| 51 | void HandleInit(uint32_t *start, uint32_t *stop); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 52 | void HandleCallerCallee(uintptr_t Caller, uintptr_t Callee); |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 53 | void HandleValueProfile(size_t Value) { ValueProfileMap.AddValue(Value); } |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 54 | template <class T> void HandleCmp(void *PC, T Arg1, T Arg2); |
Kostya Serebryany | 275e260 | 2016-10-25 23:52:25 +0000 | [diff] [blame] | 55 | size_t GetTotalPCCoverage(); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 56 | void SetUseCounters(bool UC) { UseCounters = UC; } |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 57 | void SetUseValueProfile(bool VP) { UseValueProfile = VP; } |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 58 | void SetPrintNewPCs(bool P) { DoPrintNewPCs = P; } |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 59 | size_t FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink); |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 60 | bool UpdateValueProfileMap(ValueBitMap *MaxValueProfileMap) { |
| 61 | return UseValueProfile && MaxValueProfileMap->MergeFrom(ValueProfileMap); |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 62 | } |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 63 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 64 | void ResetMaps() { |
Kostya Serebryany | 16a145f | 2016-09-23 01:58:51 +0000 | [diff] [blame] | 65 | ValueProfileMap.Reset(); |
Kostya Serebryany | 0d26de3 | 2016-09-23 20:04:13 +0000 | [diff] [blame] | 66 | memset(Counters, 0, sizeof(Counters)); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 69 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 70 | void PrintFeatureSet(); |
| 71 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 72 | void ResetGuards(); |
| 73 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 74 | void PrintModuleInfo(); |
| 75 | |
| 76 | void PrintCoverage(); |
| 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, |
| 79 | size_t n); |
| 80 | void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2, |
| 81 | size_t n); |
| 82 | |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 83 | bool UsingTracePcGuard() const {return NumModules; } |
| 84 | |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 85 | static const size_t kTORCSize = 1 << 5; |
| 86 | TableOfRecentCompares<uint32_t, kTORCSize> TORC4; |
| 87 | TableOfRecentCompares<uint64_t, kTORCSize> TORC8; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 88 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 89 | void PrintNewPCs(); |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame^] | 90 | 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 Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 95 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 96 | private: |
| 97 | bool UseCounters = false; |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 98 | bool UseValueProfile = false; |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 99 | bool DoPrintNewPCs = false; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 100 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 101 | struct Module { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 102 | uint32_t *Start, *Stop; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 103 | }; |
| 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 Serebryany | 3ee6c21 | 2016-09-28 01:16:24 +0000 | [diff] [blame] | 110 | alignas(8) uint8_t Counters[kNumCounters]; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 111 | |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 112 | 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 Serebryany | d19919a | 2016-10-11 01:14:41 +0000 | [diff] [blame] | 125 | static const size_t kNumPCs = 1 << 24; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 126 | uintptr_t PCs[kNumPCs]; |
| 127 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 128 | std::set<uintptr_t> *PrintedPCs; |
| 129 | |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 130 | ValueBitMap ValueProfileMap; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | extern TracePC TPC; |
| 134 | |
| 135 | } // namespace fuzzer |
| 136 | |
| 137 | #endif // LLVM_FUZZER_TRACE_PC |