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 | |
| 15 | #include "FuzzerInternal.h" |
| 16 | |
| 17 | namespace fuzzer { |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 18 | |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 19 | TracePC TPC; |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 20 | |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 21 | void TracePC::HandleTrace(uint8_t *Guard, uintptr_t PC) { |
| 22 | if (UseCounters) { |
| 23 | uintptr_t GV = *Guard; |
Kostya Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 24 | if (GV == 0) { |
| 25 | size_t Idx = Guard - Start; |
| 26 | if (TotalCoverageMap.AddValue(Idx)) { |
| 27 | TotalCoverage++; |
| 28 | AddNewPC(PC); |
| 29 | } |
| 30 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 31 | if (GV < 255) |
| 32 | GV++; |
| 33 | *Guard = GV; |
| 34 | } else { |
| 35 | *Guard = 0xff; |
| 36 | TotalCoverage++; |
Kostya Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 37 | AddNewPC(PC); |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 38 | } |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 39 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 40 | |
| 41 | void TracePC::HandleInit(uint8_t *Start, uint8_t *Stop) { |
| 42 | // TODO: this handles only one DSO/binary. |
| 43 | this->Start = Start; |
| 44 | this->Stop = Stop; |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 45 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 46 | |
| 47 | void TracePC::FinalizeTrace() { |
| 48 | if (UseCounters && TotalCoverage) { |
| 49 | for (uint8_t *X = Start; X < Stop; X++) { |
| 50 | uint8_t Value = *X; |
| 51 | size_t Idx = X - Start; |
Kostya Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 52 | if (Value >= 1) { |
| 53 | unsigned Bit = 0; |
| 54 | /**/ if (Value >= 128) Bit = 7; |
| 55 | else if (Value >= 32) Bit = 6; |
| 56 | else if (Value >= 16) Bit = 5; |
| 57 | else if (Value >= 8) Bit = 4; |
| 58 | else if (Value >= 4) Bit = 3; |
| 59 | else if (Value >= 3) Bit = 2; |
| 60 | else if (Value >= 2) Bit = 1; |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 61 | CounterMap.AddValue(Idx * 8 + Bit); |
| 62 | } |
Kostya Serebryany | 5350178 | 2016-09-15 04:36:45 +0000 | [diff] [blame] | 63 | *X = 0; |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | size_t TracePC::UpdateCounterMap(ValueBitMap *Map) { |
| 69 | if (!TotalCoverage) return 0; |
| 70 | size_t NewTotalCounterBits = Map->MergeFrom(CounterMap); |
| 71 | size_t Delta = NewTotalCounterBits - TotalCounterBits; |
| 72 | TotalCounterBits = NewTotalCounterBits; |
| 73 | return Delta; |
| 74 | } |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 75 | |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame^] | 76 | void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { |
| 77 | const uintptr_t kBits = 12; |
| 78 | const uintptr_t kMask = (1 << kBits) - 1; |
| 79 | CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits)); |
| 80 | } |
| 81 | |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 82 | } // namespace fuzzer |
| 83 | |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 84 | extern "C" { |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 85 | __attribute__((visibility("default"))) |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 86 | void __sanitizer_cov_trace_pc_guard(uint8_t *Guard) { |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 87 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 88 | fuzzer::TPC.HandleTrace(Guard, PC); |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 89 | } |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 90 | |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 91 | __attribute__((visibility("default"))) |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 92 | void __sanitizer_cov_trace_pc_guard_init(uint8_t *Start, uint8_t *Stop) { |
| 93 | fuzzer::TPC.HandleInit(Start, Stop); |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 94 | } |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame^] | 95 | |
| 96 | __attribute__((visibility("default"))) |
| 97 | void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { |
| 98 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
| 99 | fuzzer::TPC.HandleCallerCallee(PC, Callee); |
| 100 | } |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 101 | } |