blob: 1cfd3f3ecff1738fbdc472075b73a30067952206 [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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "FuzzerTracePC.h"
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000016#include "FuzzerCorpus.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000017#include "FuzzerDefs.h"
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000018#include "FuzzerDictionary.h"
Kostya Serebryany95b1a432016-10-19 00:12:03 +000019#include "FuzzerExtFunctions.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000020#include "FuzzerIO.h"
Marcos Pividori62c8fc52017-01-22 01:58:26 +000021#include "FuzzerUtil.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000022#include "FuzzerValueBitMap.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000023#include <map>
24#include <set>
25#include <sstream>
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000026
Kostya Serebryany68382d02017-02-02 19:56:01 +000027// The coverage counters and PCs.
28// These are declared as global variables named "__sancov_*" to simplify
29// experiments with inlined instrumentation.
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000030alignas(64) ATTRIBUTE_INTERFACE
Mike Aizatsky1b658122017-02-03 20:26:44 +000031uint8_t __sancov_trace_pc_guard_8bit_counters[fuzzer::TracePC::kNumPCs];
32
33ATTRIBUTE_INTERFACE
Kostya Serebryany68382d02017-02-02 19:56:01 +000034uintptr_t __sancov_trace_pc_pcs[fuzzer::TracePC::kNumPCs];
35
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000036namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000037
Kostya Serebryanya00b2432016-09-14 02:13:06 +000038TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000039
Kostya Serebryanyf64b8482017-07-14 00:06:27 +000040int ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr;
41
Kostya Serebryany68382d02017-02-02 19:56:01 +000042uint8_t *TracePC::Counters() const {
43 return __sancov_trace_pc_guard_8bit_counters;
44}
45
46uintptr_t *TracePC::PCs() const {
47 return __sancov_trace_pc_pcs;
48}
49
Kostya Serebryany275e2602016-10-25 23:52:25 +000050size_t TracePC::GetTotalPCCoverage() {
51 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000052 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000053 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000054 Res++;
55 return Res;
56}
57
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000058
59void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
60 if (Start == Stop) return;
61 if (NumModulesWithInline8bitCounters &&
62 ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return;
63 assert(NumModulesWithInline8bitCounters <
64 sizeof(ModuleCounters) / sizeof(ModuleCounters[0]));
65 ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop};
66 NumInline8bitCounters += Stop - Start;
67}
68
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000069void TracePC::HandlePCsInit(const uint8_t *Start, const uint8_t *Stop) {
70 const uintptr_t *B = reinterpret_cast<const uintptr_t *>(Start);
71 const uintptr_t *E = reinterpret_cast<const uintptr_t *>(Stop);
72 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return;
73 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0]));
74 ModulePCTable[NumPCTables++] = {B, E};
Kostya Serebryany4f297002017-08-01 00:48:44 +000075 NumPCsInPCTables += E - B;
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000076}
77
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000078void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000079 if (Start == Stop || *Start) return;
80 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000081 for (uint32_t *P = Start; P < Stop; P++) {
82 NumGuards++;
83 if (NumGuards == kNumPCs) {
84 RawPrint(
85 "WARNING: The binary has too many instrumented PCs.\n"
86 " You may want to reduce the size of the binary\n"
87 " for more efficient fuzzing and precise coverage data\n");
88 }
89 *P = NumGuards % kNumPCs;
90 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000091 Modules[NumModules].Start = Start;
92 Modules[NumModules].Stop = Stop;
93 NumModules++;
94}
95
96void TracePC::PrintModuleInfo() {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000097 if (NumGuards) {
Kostya Serebryany4f297002017-08-01 00:48:44 +000098 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000099 for (size_t i = 0; i < NumModules; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000100 Printf("%zd [%p, %p), ", Modules[i].Stop - Modules[i].Start,
101 Modules[i].Start, Modules[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000102 Printf("\n");
103 }
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000104 if (NumModulesWithInline8bitCounters) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000105 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ",
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000106 NumModulesWithInline8bitCounters, NumInline8bitCounters);
107 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000108 Printf("%zd [%p, %p), ", ModuleCounters[i].Stop - ModuleCounters[i].Start,
109 ModuleCounters[i].Start, ModuleCounters[i].Stop);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000110 Printf("\n");
111 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000112 if (NumPCTables) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000113 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables,
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000114 NumPCsInPCTables);
115 for (size_t i = 0; i < NumPCTables; i++) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000116 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start,
117 ModulePCTable[i].Start, ModulePCTable[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000118 }
119 Printf("\n");
Kostya Serebryany4f297002017-08-01 00:48:44 +0000120
121 if ((NumGuards && NumGuards != NumPCsInPCTables) ||
122 (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables)) {
123 Printf("ERROR: The size of coverage PC tables does not match the"
124 " number of instrumented PCs. This might be a bug in the compiler,"
125 " please contact the libFuzzer developers.\n");
126 _Exit(1);
127 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000128 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000129}
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000130
Kostya Serebryany7f058972017-01-27 00:09:59 +0000131ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000132void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
133 const uintptr_t kBits = 12;
134 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000135 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +0000136 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000137}
138
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000139void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000140 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000141 assert(!PrintedPCs);
142 PrintedPCs = new std::set<uintptr_t>;
143 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000144 if (PCs()[i])
145 PrintedPCs->insert(PCs()[i]);
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000146}
147
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000148void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000149 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000150 assert(PrintedPCs);
151 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000152 if (PCs()[i] && PrintedPCs->insert(PCs()[i]).second)
153 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs()[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000154}
155
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000156void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000157 if (!EF->__sanitizer_symbolize_pc ||
158 !EF->__sanitizer_get_module_and_offset_for_pc) {
159 Printf("INFO: __sanitizer_symbolize_pc or "
160 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000161 " not printing coverage\n");
162 return;
163 }
164 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
165 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000166 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
167 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000168 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000169 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000170 uintptr_t PC = PCs()[i];
171 if (!PC) continue;
172 std::string FileStr = DescribePC("%s", PC);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000173 if (!IsInterestingCoverageFile(FileStr)) continue;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000174 std::string FixedPCStr = DescribePC("%p", PC);
175 std::string FunctionStr = DescribePC("%F", PC);
176 std::string LineStr = DescribePC("%l", PC);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000177 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
178 void *OffsetRaw = nullptr;
179 if (!EF->__sanitizer_get_module_and_offset_for_pc(
Kostya Serebryany68382d02017-02-02 19:56:01 +0000180 reinterpret_cast<void *>(PC), ModulePathRaw,
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000181 sizeof(ModulePathRaw), &OffsetRaw))
182 continue;
183 std::string Module = ModulePathRaw;
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000184 uintptr_t FixedPC = std::stoull(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000185 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000186 ModuleOffsets[Module] = FixedPC - PcOffset;
187 CoveredPCsPerModule[Module].push_back(PcOffset);
188 CoveredFunctions.insert(FunctionStr);
189 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000190 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000191 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
192 continue;
193 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
194 FileStr.c_str(), LineStr.c_str());
195 }
196
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000197 std::string CoveredDirsStr;
198 for (auto &Dir : CoveredDirs) {
199 if (!CoveredDirsStr.empty())
200 CoveredDirsStr += ",";
201 CoveredDirsStr += Dir;
202 }
203 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
204
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000205 for (auto &M : CoveredPCsPerModule) {
206 std::set<std::string> UncoveredFiles, UncoveredFunctions;
207 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
208 auto &ModuleName = M.first;
209 auto &CoveredOffsets = M.second;
210 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
211 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
212 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
213 // sancov does not yet fully support DSOs.
214 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000215 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
216 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000217 std::string SanCovOutput;
218 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
219 Printf("INFO: Command failed: %s\n", Cmd.c_str());
220 continue;
221 }
222 std::istringstream ISS(SanCovOutput);
223 std::string S;
224 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000225 size_t PcOffsetEnd = S.find(':');
226 if (PcOffsetEnd == std::string::npos)
227 continue;
228 S.resize(PcOffsetEnd);
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000229 uintptr_t PcOffset = std::stoull(S, 0, 16);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000230 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
231 PcOffset)) {
232 uintptr_t PC = ModuleOffset + PcOffset;
233 auto FileStr = DescribePC("%s", PC);
234 if (!IsInterestingCoverageFile(FileStr)) continue;
235 if (CoveredFiles.count(FileStr) == 0) {
236 UncoveredFiles.insert(FileStr);
237 continue;
238 }
239 auto FunctionStr = DescribePC("%F", PC);
240 if (CoveredFunctions.count(FunctionStr) == 0) {
241 UncoveredFunctions.insert(FunctionStr);
242 continue;
243 }
244 std::string LineStr = DescribePC("%l", PC);
245 uintptr_t Line = std::stoi(LineStr);
246 std::string FileLineStr = FileStr + ":" + LineStr;
247 if (CoveredLines.count(FileLineStr) == 0)
248 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
249 }
250 }
251 for (auto &FileLine: UncoveredLines)
252 for (int Line : FileLine.second)
253 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
254 for (auto &Func : UncoveredFunctions)
255 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
256 for (auto &File : UncoveredFiles)
257 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000258 }
259}
260
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000261inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
262 // TODO: this implementation is x86 only.
263 // see sanitizer_common GetPreviousInstructionPc for full implementation.
264 return PC - 1;
265}
266
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000267void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000268 if (EF->__sanitizer_dump_coverage) {
269 std::vector<uintptr_t> PCsCopy(GetNumPCs());
270 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000271 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000272 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
273 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000274}
275
Kostya Serebryany379359c2016-10-05 01:09:40 +0000276// Value profile.
277// We keep track of various values that affect control flow.
278// These values are inserted into a bit-set-based hash map.
279// Every new bit in the map is treated as a new coverage.
280//
281// For memcmp/strcmp/etc the interesting value is the length of the common
282// prefix of the parameters.
283// For cmp instructions the interesting value is a XOR of the parameters.
284// The interesting value is mixed up with the PC and is then added to the map.
285
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000286ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000287void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000288 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000289 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000290 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000291 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
292 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000293 uint8_t B1[Word::kMaxSize];
294 uint8_t B2[Word::kMaxSize];
295 // Copy the data into locals in this non-msan-instrumented function
296 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000297 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000298 for (size_t i = 0; i < Len; i++) {
299 B1[i] = A1[i];
300 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000301 size_t T = B1[i];
302 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000303 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000304 size_t I = 0;
305 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000306 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000307 break;
308 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000309 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000310 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000311 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000312}
313
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000314template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000315ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000316ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000317void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000318 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000319 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000320 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000321 if (sizeof(T) == 4)
322 TORC4.Insert(ArgXor, Arg1, Arg2);
323 else if (sizeof(T) == 8)
324 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000325 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000326}
327
Kostya Serebryany697f2152017-07-13 22:30:23 +0000328static size_t InternalStrnlen(const char *S, size_t MaxLen) {
329 size_t Len = 0;
330 for (; Len < MaxLen && S[Len]; Len++) {}
331 return Len;
332}
333
334// Finds min of (strlen(S1), strlen(S2)).
335// Needed bacause one of these strings may actually be non-zero terminated.
336static size_t InternalStrnlen2(const char *S1, const char *S2) {
337 size_t Len = 0;
338 for (; S1[Len] && S2[Len]; Len++) {}
339 return Len;
340}
341
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000342void TracePC::ClearInlineCounters() {
343 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
344 uint8_t *Beg = ModuleCounters[i].Start;
345 size_t Size = ModuleCounters[i].Stop - Beg;
346 memset(Beg, 0, Size);
347 }
348}
349
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000350} // namespace fuzzer
351
Dan Liew59144072016-06-06 20:27:09 +0000352extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000353ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000354ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000355void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000356 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000357 uint32_t Idx = *Guard;
358 __sancov_trace_pc_pcs[Idx] = PC;
359 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000360 // Uncomment the following line to get stack-depth profiling.
361 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000362}
Dan Liew59144072016-06-06 20:27:09 +0000363
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000364// Best-effort support for -fsanitize-coverage=trace-pc, which is available
365// in both Clang and GCC.
366ATTRIBUTE_INTERFACE
367ATTRIBUTE_NO_SANITIZE_ALL
368void __sanitizer_cov_trace_pc() {
369 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
370 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
371 __sancov_trace_pc_pcs[Idx] = PC;
372 __sancov_trace_pc_guard_8bit_counters[Idx]++;
373}
374
Marcos Pividori6137f982017-01-22 01:27:38 +0000375ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000376void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000377 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000378}
Kostya Serebryany09845172016-09-15 22:16:15 +0000379
Marcos Pividori6137f982017-01-22 01:27:38 +0000380ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000381void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
382 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
383}
384
385ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000386void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000387 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000388}
389
390ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000391ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000392void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000393 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000394 fuzzer::TPC.HandleCallerCallee(PC, Callee);
395}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000396
Marcos Pividori6137f982017-01-22 01:27:38 +0000397ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000398ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000399ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000400void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000401 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000402 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000403}
Marcos Pividori6137f982017-01-22 01:27:38 +0000404
405ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000406ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000407ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000408void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000409 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000410 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000411}
Marcos Pividori6137f982017-01-22 01:27:38 +0000412
413ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000414ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000415ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000416void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000417 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000418 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000419}
Marcos Pividori6137f982017-01-22 01:27:38 +0000420
421ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000422ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000423ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000424void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000425 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000426 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000427}
428
Marcos Pividori6137f982017-01-22 01:27:38 +0000429ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000430ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000431ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000432void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000433 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000434 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000435 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000436 // Skip the most common and the most boring case.
437 if (Vals[N - 1] < 256 && Val < 256)
438 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000439 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000440 size_t i;
441 uint64_t Token = 0;
442 for (i = 0; i < N; i++) {
443 Token = Val ^ Vals[i];
444 if (Val < Vals[i])
445 break;
446 }
447
448 if (ValSizeInBits == 16)
449 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
450 else if (ValSizeInBits == 32)
451 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000452 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000453 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000454}
455
Marcos Pividori6137f982017-01-22 01:27:38 +0000456ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000457ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000458ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000459void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000460 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000461 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000462}
Marcos Pividori6137f982017-01-22 01:27:38 +0000463
464ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000465ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000466ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000467void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000468 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000469 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000470}
Marcos Pividori6137f982017-01-22 01:27:38 +0000471
472ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000473ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000474ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000475void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000476 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000477 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000478}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000479
480ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
481void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
482 const void *s2, size_t n, int result) {
483 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
484 if (result == 0) return; // No reason to mutate.
485 if (n <= 1) return; // Not interesting.
486 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
487}
488
489ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
490void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
491 const char *s2, size_t n, int result) {
492 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
493 if (result == 0) return; // No reason to mutate.
494 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
495 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
496 n = std::min(n, Len1);
497 n = std::min(n, Len2);
498 if (n <= 1) return; // Not interesting.
499 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
500}
501
502ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
503void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
504 const char *s2, int result) {
505 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
506 if (result == 0) return; // No reason to mutate.
507 size_t N = fuzzer::InternalStrnlen2(s1, s2);
508 if (N <= 1) return; // Not interesting.
509 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
510}
511
512ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
513void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
514 const char *s2, size_t n, int result) {
515 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
516 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
517}
518
519ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
520void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
521 const char *s2, int result) {
522 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
523 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
524}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000525
526ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
527void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
528 const char *s2, char *result) {
529 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
530 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
531}
532
533ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
534void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
535 const char *s2, char *result) {
536 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
537 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
538}
539
540ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
541void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
542 const void *s2, size_t len2, void *result) {
543 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
544 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
545}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000546} // extern "C"