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 | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 64 | size_t GetNewPCIDs(uintptr_t **NewPCIDsPtr) { |
| 65 | *NewPCIDsPtr = NewPCIDs; |
Kostya Serebryany | d28099d | 2016-09-23 00:22:46 +0000 | [diff] [blame] | 66 | return Min(kMaxNewPCIDs, NumNewPCIDs); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 69 | uintptr_t GetPCbyPCID(uintptr_t PCID) { return PCs[PCID]; } |
| 70 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 71 | void ResetMaps() { |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 72 | NumNewPCIDs = 0; |
Kostya Serebryany | 16a145f | 2016-09-23 01:58:51 +0000 | [diff] [blame] | 73 | ValueProfileMap.Reset(); |
Kostya Serebryany | 0d26de3 | 2016-09-23 20:04:13 +0000 | [diff] [blame] | 74 | memset(Counters, 0, sizeof(Counters)); |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 77 | void UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize); |
| 78 | void PrintFeatureSet(); |
| 79 | |
Kostya Serebryany | ce1cab1 | 2016-09-23 02:18:59 +0000 | [diff] [blame] | 80 | void ResetGuards(); |
| 81 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 82 | void PrintModuleInfo(); |
| 83 | |
| 84 | void PrintCoverage(); |
| 85 | |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 86 | void AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 87 | size_t n); |
| 88 | void AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2, |
| 89 | size_t n); |
| 90 | |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 91 | bool UsingTracePcGuard() const {return NumModules; } |
| 92 | |
Kostya Serebryany | 3364f90 | 2016-10-25 02:04:43 +0000 | [diff] [blame] | 93 | static const size_t kTORCSize = 1 << 5; |
| 94 | TableOfRecentCompares<uint32_t, kTORCSize> TORC4; |
| 95 | TableOfRecentCompares<uint64_t, kTORCSize> TORC8; |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 96 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame^] | 97 | void PrintNewPCs(); |
| 98 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 99 | private: |
| 100 | bool UseCounters = false; |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 101 | bool UseValueProfile = false; |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame^] | 102 | bool DoPrintNewPCs = false; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 103 | |
Kostya Serebryany | 5ff481f | 2016-09-27 00:10:20 +0000 | [diff] [blame] | 104 | static const size_t kMaxNewPCIDs = 1024; |
Kostya Serebryany | 624f59f | 2016-09-22 01:34:58 +0000 | [diff] [blame] | 105 | uintptr_t NewPCIDs[kMaxNewPCIDs]; |
| 106 | size_t NumNewPCIDs = 0; |
| 107 | void AddNewPCID(uintptr_t PCID) { |
| 108 | NewPCIDs[(NumNewPCIDs++) % kMaxNewPCIDs] = PCID; |
| 109 | } |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 110 | |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 111 | struct Module { |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 112 | uint32_t *Start, *Stop; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | Module Modules[4096]; |
| 116 | size_t NumModules = 0; |
| 117 | size_t NumGuards = 0; |
| 118 | |
| 119 | static const size_t kNumCounters = 1 << 14; |
Kostya Serebryany | 3ee6c21 | 2016-09-28 01:16:24 +0000 | [diff] [blame] | 120 | alignas(8) uint8_t Counters[kNumCounters]; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 121 | |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 122 | void TORCInsert(size_t Idx, uint8_t Arg1, uint8_t Arg2) { |
| 123 | // Do nothing, too small to be interesting. |
| 124 | } |
| 125 | void TORCInsert(size_t Idx, uint16_t Arg1, uint16_t Arg2) { |
| 126 | // Do nothing, these don't usually hapen. |
| 127 | } |
| 128 | void TORCInsert(size_t Idx, uint32_t Arg1, uint32_t Arg2) { |
| 129 | TORC4.Insert(Idx, Arg1, Arg2); |
| 130 | } |
| 131 | void TORCInsert(size_t Idx, uint64_t Arg1, uint64_t Arg2) { |
| 132 | TORC8.Insert(Idx, Arg1, Arg2); |
| 133 | } |
| 134 | |
Kostya Serebryany | d19919a | 2016-10-11 01:14:41 +0000 | [diff] [blame] | 135 | static const size_t kNumPCs = 1 << 24; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 136 | uintptr_t PCs[kNumPCs]; |
| 137 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame^] | 138 | std::set<uintptr_t> *PrintedPCs; |
| 139 | |
Kostya Serebryany | ab73c69 | 2016-09-23 00:46:18 +0000 | [diff] [blame] | 140 | ValueBitMap ValueProfileMap; |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | extern TracePC TPC; |
| 144 | |
| 145 | } // namespace fuzzer |
| 146 | |
| 147 | #endif // LLVM_FUZZER_TRACE_PC |