blob: eabf0d087f4a2064cd9b0214ae839ac9a056f893 [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 Serebryany1c73f1b2016-10-05 22:56:21 +000062size_t TracePC::FinalizeTrace(InputCorpus *C, size_t InputSize, bool Shrink) {
63 if (!UsingTracePcGuard()) return 0;
64 size_t Res = 0;
65 const size_t Step = 8;
66 assert(reinterpret_cast<uintptr_t>(Counters) % Step == 0);
67 size_t N = Min(kNumCounters, NumGuards + 1);
68 N = (N + Step - 1) & ~(Step - 1); // Round up.
69 for (size_t Idx = 0; Idx < N; Idx += Step) {
70 uint64_t Bundle = *reinterpret_cast<uint64_t*>(&Counters[Idx]);
71 if (!Bundle) continue;
72 for (size_t i = Idx; i < Idx + Step; i++) {
73 uint8_t Counter = (Bundle >> (i * 8)) & 0xff;
74 if (!Counter) continue;
75 Counters[i] = 0;
76 unsigned Bit = 0;
77 /**/ if (Counter >= 128) Bit = 7;
78 else if (Counter >= 32) Bit = 6;
79 else if (Counter >= 16) Bit = 5;
80 else if (Counter >= 8) Bit = 4;
81 else if (Counter >= 4) Bit = 3;
82 else if (Counter >= 3) Bit = 2;
83 else if (Counter >= 2) Bit = 1;
84 size_t Feature = (i * 8 + Bit);
85 if (C->AddFeature(Feature, InputSize, Shrink))
86 Res++;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000087 }
88 }
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000089 if (UseValueProfile)
90 ValueProfileMap.ForEach([&](size_t Idx) {
91 if (C->AddFeature(NumGuards + Idx, InputSize, Shrink))
92 Res++;
93 });
Kostya Serebryanyd2169222016-10-01 01:04:29 +000094 return Res;
Kostya Serebryanya5277d52016-09-15 01:30:18 +000095}
96
Kostya Serebryany09845172016-09-15 22:16:15 +000097void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
98 const uintptr_t kBits = 12;
99 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000100 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
101 HandleValueProfile(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000102}
103
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000104static bool IsInterestingCoverageFile(std::string &File) {
105 if (File.find("compiler-rt/lib/") != std::string::npos)
106 return false; // sanitizer internal.
107 if (File.find("/usr/lib/") != std::string::npos)
108 return false;
109 if (File.find("/usr/include/") != std::string::npos)
110 return false;
111 if (File == "<null>")
112 return false;
113 return true;
114}
115
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000116void TracePC::PrintNewPCs() {
117 if (DoPrintNewPCs) {
118 if (!PrintedPCs)
119 PrintedPCs = new std::set<uintptr_t>;
Kostya Serebryany06b87572016-10-26 00:42:52 +0000120 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000121 if (PCs[i] && PrintedPCs->insert(PCs[i]).second)
122 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs[i]);
123 }
124}
125
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000126void TracePC::PrintCoverage() {
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000127 if (!EF->__sanitizer_symbolize_pc) {
128 Printf("INFO: __sanitizer_symbolize_pc is not available,"
129 " not printing coverage\n");
130 return;
131 }
132 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
133 std::map<std::string, uintptr_t> ModuleOffsets;
134 std::set<std::string> CoveredFiles, CoveredFunctions, CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000135 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000136 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000137 if (!PCs[i]) continue;
138 std::string FileStr = DescribePC("%s", PCs[i]);
139 if (!IsInterestingCoverageFile(FileStr)) continue;
140 std::string FixedPCStr = DescribePC("%p", PCs[i]);
141 std::string FunctionStr = DescribePC("%F", PCs[i]);
142 std::string LineStr = DescribePC("%l", PCs[i]);
143 // TODO(kcc): get the module using some other way since this
144 // does not work with ASAN_OPTIONS=strip_path_prefix=something.
145 std::string Module = DescribePC("%m", PCs[i]);
146 std::string OffsetStr = DescribePC("%o", PCs[i]);
147 uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16);
148 uintptr_t PcOffset = std::stol(OffsetStr, 0, 16);
149 ModuleOffsets[Module] = FixedPC - PcOffset;
150 CoveredPCsPerModule[Module].push_back(PcOffset);
151 CoveredFunctions.insert(FunctionStr);
152 CoveredFiles.insert(FileStr);
153 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
154 continue;
155 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
156 FileStr.c_str(), LineStr.c_str());
157 }
158
159 for (auto &M : CoveredPCsPerModule) {
160 std::set<std::string> UncoveredFiles, UncoveredFunctions;
161 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
162 auto &ModuleName = M.first;
163 auto &CoveredOffsets = M.second;
164 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
165 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
166 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
167 // sancov does not yet fully support DSOs.
168 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
169 std::string Cmd = "objdump -d " + ModuleName +
170 " | grep 'call.*__sanitizer_cov_trace_pc_guard' | awk -F: '{print $1}'";
171 std::string SanCovOutput;
172 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
173 Printf("INFO: Command failed: %s\n", Cmd.c_str());
174 continue;
175 }
176 std::istringstream ISS(SanCovOutput);
177 std::string S;
178 while (std::getline(ISS, S, '\n')) {
179 uintptr_t PcOffset = std::stol(S, 0, 16);
180 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
181 PcOffset)) {
182 uintptr_t PC = ModuleOffset + PcOffset;
183 auto FileStr = DescribePC("%s", PC);
184 if (!IsInterestingCoverageFile(FileStr)) continue;
185 if (CoveredFiles.count(FileStr) == 0) {
186 UncoveredFiles.insert(FileStr);
187 continue;
188 }
189 auto FunctionStr = DescribePC("%F", PC);
190 if (CoveredFunctions.count(FunctionStr) == 0) {
191 UncoveredFunctions.insert(FunctionStr);
192 continue;
193 }
194 std::string LineStr = DescribePC("%l", PC);
195 uintptr_t Line = std::stoi(LineStr);
196 std::string FileLineStr = FileStr + ":" + LineStr;
197 if (CoveredLines.count(FileLineStr) == 0)
198 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
199 }
200 }
201 for (auto &FileLine: UncoveredLines)
202 for (int Line : FileLine.second)
203 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
204 for (auto &Func : UncoveredFunctions)
205 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
206 for (auto &File : UncoveredFiles)
207 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000208 }
209}
210
Kostya Serebryany379359c2016-10-05 01:09:40 +0000211// Value profile.
212// We keep track of various values that affect control flow.
213// These values are inserted into a bit-set-based hash map.
214// Every new bit in the map is treated as a new coverage.
215//
216// For memcmp/strcmp/etc the interesting value is the length of the common
217// prefix of the parameters.
218// For cmp instructions the interesting value is a XOR of the parameters.
219// The interesting value is mixed up with the PC and is then added to the map.
220
Kostya Serebryany23567912016-11-11 23:06:53 +0000221#ifdef __clang__ // avoid gcc warning.
222__attribute__((no_sanitize("memory")))
223#endif
Kostya Serebryany379359c2016-10-05 01:09:40 +0000224void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
225 size_t n) {
226 if (!n) return;
227 size_t Len = std::min(n, (size_t)32);
228 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
229 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
230 size_t I = 0;
231 for (; I < Len; I++)
232 if (A1[I] != A2[I])
233 break;
234 size_t PC = reinterpret_cast<size_t>(caller_pc);
235 size_t Idx = I;
236 // if (I < Len)
237 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
238 TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
239}
240
241void TracePC::AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2,
242 size_t n) {
243 if (!n) return;
244 size_t Len = std::min(n, (size_t)32);
245 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
246 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
247 size_t I = 0;
248 for (; I < Len; I++)
249 if (A1[I] != A2[I] || A1[I] == 0)
250 break;
251 size_t PC = reinterpret_cast<size_t>(caller_pc);
252 size_t Idx = I;
253 // if (I < Len && A1[I])
254 // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1;
255 TPC.HandleValueProfile((PC & 4095) | (Idx << 12));
256}
257
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000258template <class T>
Kostya Serebryany379359c2016-10-05 01:09:40 +0000259ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000260#ifdef __clang__ // g++ can't handle this __attribute__ here :(
261__attribute__((always_inline))
262#endif // __clang__
263void TracePC::HandleCmp(void *PC, T Arg1, T Arg2) {
264 uintptr_t PCuint = reinterpret_cast<uintptr_t>(PC);
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000265 uint64_t ArgXor = Arg1 ^ Arg2;
266 uint64_t ArgDistance = __builtin_popcountl(ArgXor) + 1; // [1,65]
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000267 uintptr_t Idx = ((PCuint & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000268 if (sizeof(T) == 4)
269 TORC4.Insert(ArgXor, Arg1, Arg2);
270 else if (sizeof(T) == 8)
271 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000272 HandleValueProfile(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000273}
274
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000275} // namespace fuzzer
276
Dan Liew59144072016-06-06 20:27:09 +0000277extern "C" {
Kostya Serebryany32661f92016-08-18 20:52:52 +0000278__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000279void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryanya00b2432016-09-14 02:13:06 +0000280 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000281 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000282}
Dan Liew59144072016-06-06 20:27:09 +0000283
Kostya Serebryany32661f92016-08-18 20:52:52 +0000284__attribute__((visibility("default")))
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000285void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000286 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000287}
Kostya Serebryany09845172016-09-15 22:16:15 +0000288
289__attribute__((visibility("default")))
290void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
291 uintptr_t PC = (uintptr_t)__builtin_return_address(0);
292 fuzzer::TPC.HandleCallerCallee(PC, Callee);
293}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000294
Kostya Serebryany379359c2016-10-05 01:09:40 +0000295__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000296void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
297 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000298}
299__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000300void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
301 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000302}
303__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000304void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
305 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000306}
307__attribute__((visibility("default")))
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000308void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
309 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000310}
311
312__attribute__((visibility("default")))
313void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000314 uint64_t N = Cases[0];
315 uint64_t *Vals = Cases + 2;
316 char *PC = (char*)__builtin_return_address(0);
317 for (size_t i = 0; i < N; i++)
318 if (Val != Vals[i])
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000319 fuzzer::TPC.HandleCmp(PC + i, Val, Vals[i]);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000320}
321
322__attribute__((visibility("default")))
323void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000324 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000325}
326__attribute__((visibility("default")))
327void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000328 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000329}
330__attribute__((visibility("default")))
331void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000332 fuzzer::TPC.HandleCmp(__builtin_return_address(0), Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000333}
334
335} // extern "C"