blob: d533eac1501937b745abf8a631ef097fe9aebb7a [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
Kostya Serebryany86586182016-09-21 21:17:23 +000015#include "FuzzerDefs.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000016#include "FuzzerTracePC.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000017#include "FuzzerValueBitMap.h"
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000018
19namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000020
Kostya Serebryanya00b2432016-09-14 02:13:06 +000021TracePC TPC;
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 Serebryany0800b812016-09-23 23:51:58 +000026 uint8_t *CounterPtr = &Counters[Idx % kNumCounters];
27 uint8_t Counter = *CounterPtr;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000028 if (Counter == 0) {
Kostya Serebryany87a598e2016-09-23 01:20:07 +000029 if (!PCs[Idx]) {
Kostya Serebryany0800b812016-09-23 23:51:58 +000030 AddNewPCID(Idx);
Kostya Serebryany87a598e2016-09-23 01:20:07 +000031 TotalPCCoverage++;
32 PCs[Idx] = PC;
33 }
Kostya Serebryanya5277d52016-09-15 01:30:18 +000034 }
Kostya Serebryany0800b812016-09-23 23:51:58 +000035 if (UseCounters) {
36 if (Counter < 128)
37 *CounterPtr = Counter + 1;
38 else
39 *Guard = 0;
40 } else {
41 *CounterPtr = 1;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000042 *Guard = 0;
Kostya Serebryany0800b812016-09-23 23:51:58 +000043 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000044}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000045
Kostya Serebryany8e781a82016-09-18 04:52:23 +000046void TracePC::HandleInit(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000047 if (Start == Stop || *Start) return;
48 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryany8e781a82016-09-18 04:52:23 +000049 for (uintptr_t *P = Start; P < Stop; P++)
50 *P = ++NumGuards;
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000051 Modules[NumModules].Start = Start;
52 Modules[NumModules].Stop = Stop;
53 NumModules++;
54}
55
56void 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 Serebryanyda63c1d2016-02-26 21:33:56 +000061}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000062
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000063void TracePC::ResetGuards() {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000064 uintptr_t N = 0;
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000065 for (size_t M = 0; M < NumModules; M++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000066 for (uintptr_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
67 *X = ++N;
68 assert(N == NumGuards);
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000069}
70
Kostya Serebryanya5277d52016-09-15 01:30:18 +000071void TracePC::FinalizeTrace() {
Kostya Serebryany87a598e2016-09-23 01:20:07 +000072 if (TotalPCCoverage) {
Kostya Serebryany5ff481f2016-09-27 00:10:20 +000073 for (size_t Idx = 1, N = Min(kNumCounters, NumGuards + 1); Idx < N;
Kostya Serebryany8e781a82016-09-18 04:52:23 +000074 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 Serebryanya5277d52016-09-15 01:30:18 +000087 }
88 }
89}
90
Kostya Serebryany09845172016-09-15 22:16:15 +000091void 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 Serebryanyb706b482016-09-18 21:47:08 +000097void TracePC::PrintCoverage() {
98 Printf("COVERAGE:\n");
Kostya Serebryany5ff481f2016-09-27 00:10:20 +000099 for (size_t i = 0; i < Min(NumGuards + 1, kNumPCs); i++) {
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000100 if (PCs[i])
101 PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]);
102 }
103}
104
Kostya Serebryany0800b812016-09-23 23:51:58 +0000105
106void 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
119void 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 Serebryanyda63c1d2016-02-26 21:33:56 +0000130} // namespace fuzzer
131
Dan Liew59144072016-06-06 20:27:09 +0000132extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000133__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000134void __sanitizer_cov_trace_pc_guard(uintptr_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000135 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000136 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000137}
Dan Liew59144072016-06-06 20:27:09 +0000138
Kostya Serebryany32661f92016-08-18 20:52:52 +0000139__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000140void __sanitizer_cov_trace_pc_guard_init(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000141 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000142}
Kostya Serebryany09845172016-09-15 22:16:15 +0000143
144__attribute__((visibility("default")))
145void __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 Liew59144072016-06-06 20:27:09 +0000149}