blob: 74eb854ef76e6bcb8b6c16a324f4327bd5ae000c [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 Serebryany1c73f1b2016-10-05 22:56:21 +000015#include "FuzzerCorpus.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000016#include "FuzzerDefs.h"
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000017#include "FuzzerDictionary.h"
Kostya Serebryany95b1a432016-10-19 00:12:03 +000018#include "FuzzerExtFunctions.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000019#include "FuzzerIO.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000020#include "FuzzerTracePC.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000021#include "FuzzerValueBitMap.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000022#include <map>
23#include <set>
24#include <sstream>
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000025
26namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000027
Kostya Serebryanya00b2432016-09-14 02:13:06 +000028TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000029
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000030void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
31 uint32_t Idx = *Guard;
Kostya Serebryany8e781a82016-09-18 04:52:23 +000032 if (!Idx) return;
Kostya Serebryany2fabeca2016-10-26 18:52:04 +000033 PCs[Idx % kNumPCs] = PC;
34 Counters[Idx % kNumCounters]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000035}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000036
Kostya Serebryany275e2602016-10-25 23:52:25 +000037size_t TracePC::GetTotalPCCoverage() {
38 size_t Res = 0;
Kostya Serebryany06b87572016-10-26 00:42:52 +000039 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany275e2602016-10-25 23:52:25 +000040 if (PCs[i])
41 Res++;
42 return Res;
43}
44
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000045void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000046 if (Start == Stop || *Start) return;
47 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000048 for (uint32_t *P = Start; P < Stop; P++)
Kostya Serebryany8e781a82016-09-18 04:52:23 +000049 *P = ++NumGuards;
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000050 Modules[NumModules].Start = Start;
51 Modules[NumModules].Stop = Stop;
52 NumModules++;
53}
54
55void TracePC::PrintModuleInfo() {
56 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
57 for (size_t i = 0; i < NumModules; i++)
58 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
59 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000060}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000061
Kostya Serebryany09845172016-09-15 22:16:15 +000062void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
63 const uintptr_t kBits = 12;
64 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000065 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
66 HandleValueProfile(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +000067}
68
Kostya Serebryany95b1a432016-10-19 00:12:03 +000069static bool IsInterestingCoverageFile(std::string &File) {
70 if (File.find("compiler-rt/lib/") != std::string::npos)
71 return false; // sanitizer internal.
72 if (File.find("/usr/lib/") != std::string::npos)
73 return false;
74 if (File.find("/usr/include/") != std::string::npos)
75 return false;
76 if (File == "<null>")
77 return false;
78 return true;
79}
80
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000081void TracePC::PrintNewPCs() {
82 if (DoPrintNewPCs) {
83 if (!PrintedPCs)
84 PrintedPCs = new std::set<uintptr_t>;
Kostya Serebryany06b87572016-10-26 00:42:52 +000085 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000086 if (PCs[i] && PrintedPCs->insert(PCs[i]).second)
87 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs[i]);
88 }
89}
90
Kostya Serebryanyb706b482016-09-18 21:47:08 +000091void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +000092 if (!EF->__sanitizer_symbolize_pc ||
93 !EF->__sanitizer_get_module_and_offset_for_pc) {
94 Printf("INFO: __sanitizer_symbolize_pc or "
95 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +000096 " not printing coverage\n");
97 return;
98 }
99 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
100 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000101 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
102 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000103 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000104 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000105 if (!PCs[i]) continue;
106 std::string FileStr = DescribePC("%s", PCs[i]);
107 if (!IsInterestingCoverageFile(FileStr)) continue;
108 std::string FixedPCStr = DescribePC("%p", PCs[i]);
109 std::string FunctionStr = DescribePC("%F", PCs[i]);
110 std::string LineStr = DescribePC("%l", PCs[i]);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000111 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
112 void *OffsetRaw = nullptr;
113 if (!EF->__sanitizer_get_module_and_offset_for_pc(
114 reinterpret_cast<void *>(PCs[i]), ModulePathRaw,
115 sizeof(ModulePathRaw), &OffsetRaw))
116 continue;
117 std::string Module = ModulePathRaw;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000118 uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000119 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000120 ModuleOffsets[Module] = FixedPC - PcOffset;
121 CoveredPCsPerModule[Module].push_back(PcOffset);
122 CoveredFunctions.insert(FunctionStr);
123 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000124 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000125 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
126 continue;
127 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
128 FileStr.c_str(), LineStr.c_str());
129 }
130
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000131 std::string CoveredDirsStr;
132 for (auto &Dir : CoveredDirs) {
133 if (!CoveredDirsStr.empty())
134 CoveredDirsStr += ",";
135 CoveredDirsStr += Dir;
136 }
137 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
138
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000139 for (auto &M : CoveredPCsPerModule) {
140 std::set<std::string> UncoveredFiles, UncoveredFunctions;
141 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
142 auto &ModuleName = M.first;
143 auto &CoveredOffsets = M.second;
144 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
145 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
146 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
147 // sancov does not yet fully support DSOs.
148 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
149 std::string Cmd = "objdump -d " + ModuleName +
150 " | grep 'call.*__sanitizer_cov_trace_pc_guard' | awk -F: '{print $1}'";
151 std::string SanCovOutput;
152 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
153 Printf("INFO: Command failed: %s\n", Cmd.c_str());
154 continue;
155 }
156 std::istringstream ISS(SanCovOutput);
157 std::string S;
158 while (std::getline(ISS, S, '\n')) {
159 uintptr_t PcOffset = std::stol(S, 0, 16);
160 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
161 PcOffset)) {
162 uintptr_t PC = ModuleOffset + PcOffset;
163 auto FileStr = DescribePC("%s", PC);
164 if (!IsInterestingCoverageFile(FileStr)) continue;
165 if (CoveredFiles.count(FileStr) == 0) {
166 UncoveredFiles.insert(FileStr);
167 continue;
168 }
169 auto FunctionStr = DescribePC("%F", PC);
170 if (CoveredFunctions.count(FunctionStr) == 0) {
171 UncoveredFunctions.insert(FunctionStr);
172 continue;
173 }
174 std::string LineStr = DescribePC("%l", PC);
175 uintptr_t Line = std::stoi(LineStr);
176 std::string FileLineStr = FileStr + ":" + LineStr;
177 if (CoveredLines.count(FileLineStr) == 0)
178 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
179 }
180 }
181 for (auto &FileLine: UncoveredLines)
182 for (int Line : FileLine.second)
183 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
184 for (auto &Func : UncoveredFunctions)
185 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
186 for (auto &File : UncoveredFiles)
187 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000188 }
189}
190
Kostya Serebryany379359c2016-10-05 01:09:40 +0000191// Value profile.
192// We keep track of various values that affect control flow.
193// These values are inserted into a bit-set-based hash map.
194// Every new bit in the map is treated as a new coverage.
195//
196// For memcmp/strcmp/etc the interesting value is the length of the common
197// prefix of the parameters.
198// For cmp instructions the interesting value is a XOR of the parameters.
199// The interesting value is mixed up with the PC and is then added to the map.
200
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +0000201ATTRIBUTE_NO_SANITIZE_MEMORY
Kostya Serebryany379359c2016-10-05 01:09:40 +0000202void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
203 size_t n) {
204 if (!n) return;
205 size_t Len = std::min(n, (size_t)32);
206 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
207 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
208 size_t I = 0;
209 for (; I < Len; I++)
210 if (A1[I] != A2[I])
211 break;
212 size_t PC = reinterpret_cast<size_t>(caller_pc);
213 size_t Idx = I;
214 // if (I < Len)
215 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
216 TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
217}
218
Kostya Serebryany3a4e2dd2016-12-16 22:45:25 +0000219ATTRIBUTE_NO_SANITIZE_MEMORY
Kostya Serebryany379359c2016-10-05 01:09:40 +0000220void TracePC::AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
221 size_t n) {
222 if (!n) return;
223 size_t Len = std::min(n, (size_t)32);
224 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
225 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
226 size_t I = 0;
227 for (; I < Len; I++)
228 if (A1[I] != A2[I] || A1[I] == 0)
229 break;
230 size_t PC = reinterpret_cast<size_t>(caller_pc);
231 size_t Idx = I;
232 // if (I < Len && A1[I])
233 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
234 TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
235}
236
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000237template <class T>
Kostya Serebryany379359c2016-10-05 01:09:40 +0000238ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000239#ifdef __clang__ // g++ can't handle this __attribute__ here :(
240__attribute__((always_inline))
241#endif // __clang__
242void TracePC::HandleCmp(void *PC, T Arg1, T Arg2) {
243 uintptr_t PCuint = reinterpret_cast<uintptr_t>(PC);
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000244 uint64_t ArgXor = Arg1 ^ Arg2;
245 uint64_t ArgDistance = __builtin_popcountl(ArgXor) + 1; // [1,65]
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000246 uintptr_t Idx = ((PCuint & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000247 if (sizeof(T) == 4)
248 TORC4.Insert(ArgXor, Arg1, Arg2);
249 else if (sizeof(T) == 8)
250 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000251 HandleValueProfile(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000252}
253
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000254} // namespace fuzzer
255
Dan Liew59144072016-06-06 20:27:09 +0000256extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000257__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000258void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000259 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000260 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000261}
Dan Liew59144072016-06-06 20:27:09 +0000262
Kostya Serebryany32661f92016-08-18 20:52:52 +0000263__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000264void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000265 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000266}
Kostya Serebryany09845172016-09-15 22:16:15 +0000267
268__attribute__((visibility("default")))
269void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
270 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
271 fuzzer::TPC.HandleCallerCallee(PC, Callee);
272}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000273
Kostya Serebryany379359c2016-10-05 01:09:40 +0000274__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000275void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
276 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000277}
278__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000279void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
280 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000281}
282__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000283void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
284 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000285}
286__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000287void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
288 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000289}
290
291__attribute__((visibility("default")))
292void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000293 // Updates the value profile based on the relative position of Val and Cases.
294 // We want to handle one random case at every call (handling all is slow).
295 // Since none of the arguments contain any random bits we use a thread-local
296 // counter to choose the random case to handle.
297 static thread_local size_t Counter;
298 Counter++;
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000299 uint64_t N = Cases[0];
300 uint64_t *Vals = Cases + 2;
301 char *PC = (char*)__builtin_return_address(0);
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000302 size_t Idx = Counter % N;
303 uint64_t TwoIn32 = 1ULL << 32;
304 if ((Val | Vals[Idx]) < TwoIn32)
305 fuzzer::TPC.HandleCmp(PC + Idx, static_cast<uint32_t>(Val),
306 static_cast<uint32_t>(Vals[Idx]));
307 else
308 fuzzer::TPC.HandleCmp(PC + Idx, Val, Vals[Idx]);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000309}
310
311__attribute__((visibility("default")))
312void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000313 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000314}
315__attribute__((visibility("default")))
316void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000317 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000318}
319__attribute__((visibility("default")))
320void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000321 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000322}
323
324} // extern "C"