blob: db62345e272fa53835bb405baf24a74fd4359c45 [file] [log] [blame]
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +00001//===- 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 Serebryanya00b2432016-09-14 02:13:06 +000010// This module implements __sanitizer_cov_trace_pc_guard[_init],
11// the callback required for -fsanitize-coverage=trace-pc-guard instrumentation.
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000012//
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000013//===----------------------------------------------------------------------===//
14
15#include "FuzzerInternal.h"
16
17namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000018
Kostya Serebryanya00b2432016-09-14 02:13:06 +000019TracePC TPC;
Kostya Serebryany8e781a82016-09-18 04:52:23 +000020const size_t TracePC::kNumCounters;
Kostya Serebryanyb706b482016-09-18 21:47:08 +000021const size_t TracePC::kNumPCs;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000022
Kostya Serebryany8e781a82016-09-18 04:52:23 +000023void TracePC::HandleTrace(uintptr_t *Guard, uintptr_t PC) {
24 uintptr_t Idx = *Guard;
25 if (!Idx) return;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000026 if (UseCounters) {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000027 uint8_t Counter = Counters[Idx % kNumCounters];
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000028 if (Counter == 0) {
Kostya Serebryanyb706b482016-09-18 21:47:08 +000029 PCs[Idx] = PC;
Kostya Serebryany53501782016-09-15 04:36:45 +000030 if (TotalCoverageMap.AddValue(Idx)) {
31 TotalCoverage++;
32 AddNewPC(PC);
33 }
34 }
Kostya Serebryany8e781a82016-09-18 04:52:23 +000035 if (Counter < 128)
36 Counters[Idx % kNumCounters] = Counter + 1;
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000037 else
Kostya Serebryany8e781a82016-09-18 04:52:23 +000038 *Guard = 0;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000039 } else {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000040 *Guard = 0;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000041 TotalCoverage++;
Kostya Serebryany53501782016-09-15 04:36:45 +000042 AddNewPC(PC);
Kostya Serebryanyb706b482016-09-18 21:47:08 +000043 PCs[Idx] = PC;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000044 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000045}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000046
Kostya Serebryany8e781a82016-09-18 04:52:23 +000047void TracePC::HandleInit(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000048 if (Start == Stop || *Start) return;
49 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryany8e781a82016-09-18 04:52:23 +000050 for (uintptr_t *P = Start; P < Stop; P++)
51 *P = ++NumGuards;
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000052 Modules[NumModules].Start = Start;
53 Modules[NumModules].Stop = Stop;
54 NumModules++;
55}
56
57void TracePC::PrintModuleInfo() {
58 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
59 for (size_t i = 0; i < NumModules; i++)
60 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
61 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000062}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000063
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000064void TracePC::ResetGuards() {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000065 uintptr_t N = 0;
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000066 for (size_t M = 0; M < NumModules; M++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000067 for (uintptr_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
68 *X = ++N;
69 assert(N == NumGuards);
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000070}
71
Kostya Serebryanya5277d52016-09-15 01:30:18 +000072void TracePC::FinalizeTrace() {
73 if (UseCounters && TotalCoverage) {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000074 for (size_t Idx = 1, N = std::min(kNumCounters, NumGuards); Idx < N;
75 Idx++) {
76 uint8_t Counter = Counters[Idx];
77 if (!Counter) continue;
78 Counters[Idx] = 0;
79 unsigned Bit = 0;
80 /**/ if (Counter >= 128) Bit = 7;
81 else if (Counter >= 32) Bit = 6;
82 else if (Counter >= 16) Bit = 5;
83 else if (Counter >= 8) Bit = 4;
84 else if (Counter >= 4) Bit = 3;
85 else if (Counter >= 3) Bit = 2;
86 else if (Counter >= 2) Bit = 1;
87 CounterMap.AddValue(Idx * 8 + Bit);
Kostya Serebryanya5277d52016-09-15 01:30:18 +000088 }
89 }
90}
91
92size_t TracePC::UpdateCounterMap(ValueBitMap *Map) {
93 if (!TotalCoverage) return 0;
94 size_t NewTotalCounterBits = Map->MergeFrom(CounterMap);
95 size_t Delta = NewTotalCounterBits - TotalCounterBits;
96 TotalCounterBits = NewTotalCounterBits;
97 return Delta;
98}
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000099
Kostya Serebryany09845172016-09-15 22:16:15 +0000100void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
101 const uintptr_t kBits = 12;
102 const uintptr_t kMask = (1 << kBits) - 1;
103 CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits));
104}
105
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000106void TracePC::PrintCoverage() {
107 Printf("COVERAGE:\n");
108 for (size_t i = 0; i < std::min(NumGuards, kNumPCs); i++) {
109 if (PCs[i])
110 PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]);
111 }
112}
113
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000114} // namespace fuzzer
115
Dan Liew59144072016-06-06 20:27:09 +0000116extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000117__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000118void __sanitizer_cov_trace_pc_guard(uintptr_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000119 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000120 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000121}
Dan Liew59144072016-06-06 20:27:09 +0000122
Kostya Serebryany32661f92016-08-18 20:52:52 +0000123__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000124void __sanitizer_cov_trace_pc_guard_init(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000125 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000126}
Kostya Serebryany09845172016-09-15 22:16:15 +0000127
128__attribute__((visibility("default")))
129void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
130 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
131 fuzzer::TPC.HandleCallerCallee(PC, Callee);
132}
Dan Liew59144072016-06-06 20:27:09 +0000133}