blob: f02b71dc9deca49de9618890bea4e2c40174a4bb [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 Serebryanya9b0dd02016-09-29 17:43:24 +000023void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
24 uint32_t Idx = *Guard;
Kostya Serebryany8e781a82016-09-18 04:52:23 +000025 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 Serebryanya9b0dd02016-09-29 17:43:24 +000046void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000047 if (Start == Stop || *Start) return;
48 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000049 for (uint32_t *P = Start; P < Stop; P++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000050 *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 Serebryanya9b0dd02016-09-29 17:43:24 +000064 uint32_t N = 0;
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000065 for (size_t M = 0; M < NumModules; M++)
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000066 for (uint32_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000067 *X = ++N;
68 assert(N == NumGuards);
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000069}
70
Kostya Serebryanyd2169222016-10-01 01:04:29 +000071bool TracePC::FinalizeTrace(size_t InputSize) {
72 bool Res = false;
Kostya Serebryany87a598e2016-09-23 01:20:07 +000073 if (TotalPCCoverage) {
Kostya Serebryany3ee6c212016-09-28 01:16:24 +000074 const size_t Step = 8;
75 assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0);
76 size_t N = Min(kNumCounters, NumGuards + 1);
77 N = (N + Step - 1) & ~(Step - 1); // Round up.
78 for (size_t Idx = 0; Idx < N; Idx += Step) {
79 uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]);
80 if (!Bundle) continue;
81 for (size_t i = Idx; i < Idx + Step; i++) {
82 uint8_t Counter = (Bundle >> (i * 8)) & 0xff;
83 if (!Counter) continue;
84 Counters[i] = 0;
85 unsigned Bit = 0;
86 /**/ if (Counter >= 128) Bit = 7;
87 else if (Counter >= 32) Bit = 6;
88 else if (Counter >= 16) Bit = 5;
89 else if (Counter >= 8) Bit = 4;
90 else if (Counter >= 4) Bit = 3;
91 else if (Counter >= 3) Bit = 2;
92 else if (Counter >= 2) Bit = 1;
Kostya Serebryanyd2169222016-10-01 01:04:29 +000093 size_t Feature = i * 8 + Bit;
94 CounterMap.AddValue(Feature);
95 uint32_t *SizePtr = &InputSizesPerFeature[Feature];
96 if (!*SizePtr || *SizePtr > InputSize) {
97 *SizePtr = InputSize;
98 Res = true;
99 }
Kostya Serebryany3ee6c212016-09-28 01:16:24 +0000100 }
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000101 }
102 }
Kostya Serebryanyd2169222016-10-01 01:04:29 +0000103 return Res;
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000104}
105
Kostya Serebryany09845172016-09-15 22:16:15 +0000106void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
107 const uintptr_t kBits = 12;
108 const uintptr_t kMask = (1 << kBits) - 1;
109 CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits));
110}
111
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000112void TracePC::PrintCoverage() {
113 Printf("COVERAGE:\n");
Kostya Serebryany5ff481f2016-09-27 00:10:20 +0000114 for (size_t i = 0; i < Min(NumGuards + 1, kNumPCs); i++) {
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000115 if (PCs[i])
116 PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]);
117 }
118}
119
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000120} // namespace fuzzer
121
Dan Liew59144072016-06-06 20:27:09 +0000122extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000123__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000124void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000125 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000126 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000127}
Dan Liew59144072016-06-06 20:27:09 +0000128
Kostya Serebryany32661f92016-08-18 20:52:52 +0000129__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000130void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000131 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000132}
Kostya Serebryany09845172016-09-15 22:16:15 +0000133
134__attribute__((visibility("default")))
135void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
136 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
137 fuzzer::TPC.HandleCallerCallee(PC, Callee);
138}
Dan Liew59144072016-06-06 20:27:09 +0000139}