blob: 0df8aba18f679983f3844b2b8a95d3bf966a8722 [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 Serebryany87a598e2016-09-23 01:20:07 +000026 uint8_t Counter = Counters[Idx % kNumCounters];
27 if (Counter == 0) {
Kostya Serebryany624f59f2016-09-22 01:34:58 +000028 AddNewPCID(Idx);
Kostya Serebryany87a598e2016-09-23 01:20:07 +000029 if (!PCs[Idx]) {
30 TotalPCCoverage++;
31 PCs[Idx] = PC;
32 }
Kostya Serebryanya5277d52016-09-15 01:30:18 +000033 }
Kostya Serebryany87a598e2016-09-23 01:20:07 +000034 if (Counter < 128)
35 Counters[Idx % kNumCounters] = Counter + 1;
36 if (Counter >= 128 || !UseCounters)
37 *Guard = 0;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000038}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000039
Kostya Serebryany8e781a82016-09-18 04:52:23 +000040void TracePC::HandleInit(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000041 if (Start == Stop || *Start) return;
42 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryany8e781a82016-09-18 04:52:23 +000043 for (uintptr_t *P = Start; P < Stop; P++)
44 *P = ++NumGuards;
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000045 Modules[NumModules].Start = Start;
46 Modules[NumModules].Stop = Stop;
47 NumModules++;
48}
49
50void TracePC::PrintModuleInfo() {
51 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
52 for (size_t i = 0; i < NumModules; i++)
53 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
54 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000055}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000056
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000057void TracePC::ResetGuards() {
Kostya Serebryany8e781a82016-09-18 04:52:23 +000058 uintptr_t N = 0;
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000059 for (size_t M = 0; M < NumModules; M++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000060 for (uintptr_t *X = Modules[M].Start; X < Modules[M].Stop; X++)
61 *X = ++N;
62 assert(N == NumGuards);
Kostya Serebryanybc3789a2016-09-17 06:01:55 +000063}
64
Kostya Serebryanya5277d52016-09-15 01:30:18 +000065void TracePC::FinalizeTrace() {
Kostya Serebryany87a598e2016-09-23 01:20:07 +000066 if (TotalPCCoverage) {
67 for (size_t Idx = 1, N = Min(kNumCounters, NumGuards); Idx < N;
Kostya Serebryany8e781a82016-09-18 04:52:23 +000068 Idx++) {
69 uint8_t Counter = Counters[Idx];
70 if (!Counter) continue;
71 Counters[Idx] = 0;
72 unsigned Bit = 0;
73 /**/ if (Counter >= 128) Bit = 7;
74 else if (Counter >= 32) Bit = 6;
75 else if (Counter >= 16) Bit = 5;
76 else if (Counter >= 8) Bit = 4;
77 else if (Counter >= 4) Bit = 3;
78 else if (Counter >= 3) Bit = 2;
79 else if (Counter >= 2) Bit = 1;
80 CounterMap.AddValue(Idx * 8 + Bit);
Kostya Serebryanya5277d52016-09-15 01:30:18 +000081 }
82 }
83}
84
Kostya Serebryany09845172016-09-15 22:16:15 +000085void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
86 const uintptr_t kBits = 12;
87 const uintptr_t kMask = (1 << kBits) - 1;
88 CounterMap.AddValue((Caller & kMask) | ((Callee & kMask) << kBits));
89}
90
Kostya Serebryanyb706b482016-09-18 21:47:08 +000091void TracePC::PrintCoverage() {
92 Printf("COVERAGE:\n");
Kostya Serebryany87a598e2016-09-23 01:20:07 +000093 for (size_t i = 0; i < Min(NumGuards, kNumPCs); i++) {
Kostya Serebryanyb706b482016-09-18 21:47:08 +000094 if (PCs[i])
95 PrintPC("COVERED: %p %F %L\n", "COVERED: %p\n", PCs[i]);
96 }
97}
98
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000099} // namespace fuzzer
100
Dan Liew59144072016-06-06 20:27:09 +0000101extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000102__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000103void __sanitizer_cov_trace_pc_guard(uintptr_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000104 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000105 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000106}
Dan Liew59144072016-06-06 20:27:09 +0000107
Kostya Serebryany32661f92016-08-18 20:52:52 +0000108__attribute__((visibility("default")))
Kostya Serebryany8e781a82016-09-18 04:52:23 +0000109void __sanitizer_cov_trace_pc_guard_init(uintptr_t *Start, uintptr_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000110 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000111}
Kostya Serebryany09845172016-09-15 22:16:15 +0000112
113__attribute__((visibility("default")))
114void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
115 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
116 fuzzer::TPC.HandleCallerCallee(PC, Callee);
117}
Dan Liew59144072016-06-06 20:27:09 +0000118}