Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 1 | //===- FuzzerTracePC.cpp - PC tracing--------------------------------------===// |
| 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 | // Trace PCs. |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 10 | // This module implements __sanitizer_cov_trace_pc_guard[_init], |
| 11 | // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation. |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 12 | // |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 15 | #include "FuzzerDefs.h" |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 16 | #include "FuzzerTracePC.h" |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 17 | #include "FuzzerValueBitMap.h" |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 18 | |
| 19 | namespace fuzzer { |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 20 | |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 21 | TracePC TPC; |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 22 | |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 23 | void TracePC::HandleTrace(uintptr_t *Guard, uintptr_t PC) { |
| 24 | uintptr_t Idx = *Guard; |
| 25 | if (!Idx) return; |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 26 | uint8_t *CounterPtr = &Counters[Idx % kNumCounters]; |
| 27 | uint8_t Counter = *CounterPtr; |
Kostya Serebryany | 87a598e | 2016-09-23 01:20:07 +0000 | [diff] [blame] | 28 | if (Counter == 0) { |
Kostya Serebryany | 87a598e | 2016-09-23 01:20:07 +0000 | [diff] [blame] | 29 | if (!PCs[Idx]) { |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 30 | AddNewPCID(Idx); |
Kostya Serebryany | 87a598e | 2016-09-23 01:20:07 +0000 | [diff] [blame] | 31 | TotalPCCoverage++; |
| 32 | PCs[Idx] = PC; |
| 33 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 34 | } |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 35 | if (UseCounters) { |
| 36 | if (Counter < 128) |
| 37 | *CounterPtr = Counter + 1; |
| 38 | else |
| 39 | *Guard = 0; |
| 40 | } else { |
| 41 | *CounterPtr = 1; |
Kostya Serebryany | 87a598e | 2016-09-23 01:20:07 +0000 | [diff] [blame] | 42 | *Guard = 0; |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 43 | } |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 44 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 45 | |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 46 | void TracePC::HandleInit(uintptr_t *Start, uintptr_t *Stop) { |
Kostya Serebryany | 3e36ec1 | 2016-09-17 05:04:47 +0000 | [diff] [blame] | 47 | if (Start == Stop || *Start) return; |
| 48 | assert(NumModules < sizeof(Modules) / sizeof(Modules[0])); |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 49 | for (uintptr_t *P = Start; P < Stop; P++) |
| 50 | *P = ++NumGuards; |
Kostya Serebryany | 3e36ec1 | 2016-09-17 05:04:47 +0000 | [diff] [blame] | 51 | Modules[NumModules].Start = Start; |
| 52 | Modules[NumModules].Stop = Stop; |
| 53 | NumModules++; |
| 54 | } |
| 55 | |
| 56 | void TracePC::PrintModuleInfo() { |
| 57 | Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards); |
| 58 | for (size_t i = 0; i < NumModules; i++) |
| 59 | Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop); |
| 60 | Printf("\n"); |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 61 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 62 | |
Kostya Serebryany | bc3789a | 2016-09-17 06:01:55 +0000 | [diff] [blame] | 63 | void TracePC::ResetGuards() { |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 64 | uintptr_t N = 0; |
Kostya Serebryany | bc3789a | 2016-09-17 06:01:55 +0000 | [diff] [blame] | 65 | for (size_t M = 0; M < NumModules; M++) |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 66 | for (uintptr_t *X = Modules[M].Start; X < Modules[M].Stop; X++) |
| 67 | *X = ++N; |
| 68 | assert(N == NumGuards); |
Kostya Serebryany | bc3789a | 2016-09-17 06:01:55 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 71 | void TracePC::FinalizeTrace() { |
Kostya Serebryany | 87a598e | 2016-09-23 01:20:07 +0000 | [diff] [blame] | 72 | if (TotalPCCoverage) { |
Kostya Serebryany | 5ff481f | 2016-09-27 00:10:20 +0000 | [diff] [blame] | 73 | for (size_t Idx = 1, N = Min(kNumCounters, NumGuards + 1); Idx < N; |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 74 | Idx++) { |
| 75 | uint8_t Counter = Counters[Idx]; |
| 76 | if (!Counter) continue; |
| 77 | Counters[Idx] = 0; |
| 78 | unsigned Bit = 0; |
| 79 | /**/ if (Counter >= 128) Bit = 7; |
| 80 | else if (Counter >= 32) Bit = 6; |
| 81 | else if (Counter >= 16) Bit = 5; |
| 82 | else if (Counter >= 8) Bit = 4; |
| 83 | else if (Counter >= 4) Bit = 3; |
| 84 | else if (Counter >= 3) Bit = 2; |
| 85 | else if (Counter >= 2) Bit = 1; |
| 86 | CounterMap.AddValue(Idx * 8 + Bit); |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame] | 91 | void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { |
| 92 | const uintptr_t kBits = 12; |
| 93 | const uintptr_t kMask = (1 << kBits) - 1; |
| 94 | CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits)); |
| 95 | } |
| 96 | |
Kostya Serebryany | b706b48 | 2016-09-18 21:47:08 +0000 | [diff] [blame] | 97 | void TracePC::PrintCoverage() { |
| 98 | Printf("COVERAGE:\n"); |
Kostya Serebryany | 5ff481f | 2016-09-27 00:10:20 +0000 | [diff] [blame] | 99 | for (size_t i = 0; i < Min(NumGuards + 1, kNumPCs); i++) { |
Kostya Serebryany | b706b48 | 2016-09-18 21:47:08 +0000 | [diff] [blame] | 100 | if (PCs[i]) |
| 101 | PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]); |
| 102 | } |
| 103 | } |
| 104 | |
Kostya Serebryany | 0800b81 | 2016-09-23 23:51:58 +0000 | [diff] [blame] | 105 | |
| 106 | void TracePC::UpdateFeatureSet(size_t CurrentElementIdx, size_t CurrentElementSize) { |
| 107 | if (!CurrentElementSize) return; |
| 108 | for (size_t Idx = 0; Idx < kFeatureSetSize; Idx++) { |
| 109 | if (!CounterMap.Get(Idx)) continue; |
| 110 | Feature &Fe = FeatureSet[Idx]; |
| 111 | Fe.Count++; |
| 112 | if (!Fe.SmallestElementSize || Fe.SmallestElementSize > CurrentElementSize) { |
| 113 | Fe.SmallestElementIdx = CurrentElementIdx; |
| 114 | Fe.SmallestElementSize = CurrentElementSize; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void TracePC::PrintFeatureSet() { |
| 120 | Printf("[id: cnt idx sz] "); |
| 121 | for (size_t i = 0; i < kFeatureSetSize; i++) { |
| 122 | auto &Fe = FeatureSet[i]; |
| 123 | if (!Fe.Count) continue; |
| 124 | Printf("[%zd: %zd %zd %zd] ", i, Fe.Count, Fe.SmallestElementIdx, |
| 125 | Fe.SmallestElementSize); |
| 126 | } |
| 127 | Printf("\n"); |
| 128 | } |
| 129 | |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 130 | } // namespace fuzzer |
| 131 | |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 132 | extern "C" { |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 133 | __attribute__((visibility("default"))) |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 134 | void __sanitizer_cov_trace_pc_guard(uintptr_t *Guard) { |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 135 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 136 | fuzzer::TPC.HandleTrace(Guard, PC); |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 137 | } |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 138 | |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 139 | __attribute__((visibility("default"))) |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 140 | void __sanitizer_cov_trace_pc_guard_init(uintptr_t *Start, uintptr_t *Stop) { |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 141 | fuzzer::TPC.HandleInit(Start, Stop); |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 142 | } |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame] | 143 | |
| 144 | __attribute__((visibility("default"))) |
| 145 | void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { |
| 146 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
| 147 | fuzzer::TPC.HandleCallerCallee(PC, Callee); |
| 148 | } |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 149 | } |