blob: bf2a6110e853cd979334cb63e02194ac127ec641 [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 Serebryanya9b0dd02016-09-29 17:43:24 +000069void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000070 if (Start == Stop || *Start) return;
71 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000072 for (uint32_t *P = Start; P < Stop; P++) {
73 NumGuards++;
74 if (NumGuards == kNumPCs) {
75 RawPrint(
76 "WARNING: The binary has too many instrumented PCs.\n"
77 " You may want to reduce the size of the binary\n"
78 " for more efficient fuzzing and precise coverage data\n");
79 }
80 *P = NumGuards % kNumPCs;
81 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000082 Modules[NumModules].Start = Start;
83 Modules[NumModules].Stop = Stop;
84 NumModules++;
85}
86
87void TracePC::PrintModuleInfo() {
88 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
89 for (size_t i = 0; i < NumModules; i++)
90 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
91 Printf("\n");
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000092 if (NumModulesWithInline8bitCounters) {
93 Printf("INFO: Loaded %zd modules with %zd inline 8-bit counters\n",
94 NumModulesWithInline8bitCounters, NumInline8bitCounters);
95 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++)
96 Printf("[%p, %p), ", ModuleCounters[i].Start, ModuleCounters[i].Stop);
97 Printf("\n");
98 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000099}
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000100
Kostya Serebryany7f058972017-01-27 00:09:59 +0000101ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000102void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
103 const uintptr_t kBits = 12;
104 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000105 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +0000106 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000107}
108
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000109void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000110 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000111 assert(!PrintedPCs);
112 PrintedPCs = new std::set<uintptr_t>;
113 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000114 if (PCs()[i])
115 PrintedPCs->insert(PCs()[i]);
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000116}
117
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000118void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000119 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000120 assert(PrintedPCs);
121 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000122 if (PCs()[i] && PrintedPCs->insert(PCs()[i]).second)
123 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs()[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000124}
125
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000126void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000127 if (!EF->__sanitizer_symbolize_pc ||
128 !EF->__sanitizer_get_module_and_offset_for_pc) {
129 Printf("INFO: __sanitizer_symbolize_pc or "
130 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000131 " not printing coverage\n");
132 return;
133 }
134 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
135 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000136 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
137 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000138 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000139 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000140 uintptr_t PC = PCs()[i];
141 if (!PC) continue;
142 std::string FileStr = DescribePC("%s", PC);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000143 if (!IsInterestingCoverageFile(FileStr)) continue;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000144 std::string FixedPCStr = DescribePC("%p", PC);
145 std::string FunctionStr = DescribePC("%F", PC);
146 std::string LineStr = DescribePC("%l", PC);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000147 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
148 void *OffsetRaw = nullptr;
149 if (!EF->__sanitizer_get_module_and_offset_for_pc(
Kostya Serebryany68382d02017-02-02 19:56:01 +0000150 reinterpret_cast<void *>(PC), ModulePathRaw,
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000151 sizeof(ModulePathRaw), &OffsetRaw))
152 continue;
153 std::string Module = ModulePathRaw;
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000154 uintptr_t FixedPC = std::stoull(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000155 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000156 ModuleOffsets[Module] = FixedPC - PcOffset;
157 CoveredPCsPerModule[Module].push_back(PcOffset);
158 CoveredFunctions.insert(FunctionStr);
159 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000160 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000161 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
162 continue;
163 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
164 FileStr.c_str(), LineStr.c_str());
165 }
166
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000167 std::string CoveredDirsStr;
168 for (auto &Dir : CoveredDirs) {
169 if (!CoveredDirsStr.empty())
170 CoveredDirsStr += ",";
171 CoveredDirsStr += Dir;
172 }
173 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
174
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000175 for (auto &M : CoveredPCsPerModule) {
176 std::set<std::string> UncoveredFiles, UncoveredFunctions;
177 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
178 auto &ModuleName = M.first;
179 auto &CoveredOffsets = M.second;
180 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
181 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
182 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
183 // sancov does not yet fully support DSOs.
184 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000185 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
186 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000187 std::string SanCovOutput;
188 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
189 Printf("INFO: Command failed: %s\n", Cmd.c_str());
190 continue;
191 }
192 std::istringstream ISS(SanCovOutput);
193 std::string S;
194 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000195 size_t PcOffsetEnd = S.find(':');
196 if (PcOffsetEnd == std::string::npos)
197 continue;
198 S.resize(PcOffsetEnd);
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000199 uintptr_t PcOffset = std::stoull(S, 0, 16);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000200 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
201 PcOffset)) {
202 uintptr_t PC = ModuleOffset + PcOffset;
203 auto FileStr = DescribePC("%s", PC);
204 if (!IsInterestingCoverageFile(FileStr)) continue;
205 if (CoveredFiles.count(FileStr) == 0) {
206 UncoveredFiles.insert(FileStr);
207 continue;
208 }
209 auto FunctionStr = DescribePC("%F", PC);
210 if (CoveredFunctions.count(FunctionStr) == 0) {
211 UncoveredFunctions.insert(FunctionStr);
212 continue;
213 }
214 std::string LineStr = DescribePC("%l", PC);
215 uintptr_t Line = std::stoi(LineStr);
216 std::string FileLineStr = FileStr + ":" + LineStr;
217 if (CoveredLines.count(FileLineStr) == 0)
218 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
219 }
220 }
221 for (auto &FileLine: UncoveredLines)
222 for (int Line : FileLine.second)
223 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
224 for (auto &Func : UncoveredFunctions)
225 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
226 for (auto &File : UncoveredFiles)
227 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000228 }
229}
230
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000231inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
232 // TODO: this implementation is x86 only.
233 // see sanitizer_common GetPreviousInstructionPc for full implementation.
234 return PC - 1;
235}
236
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000237void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000238 if (EF->__sanitizer_dump_coverage) {
239 std::vector<uintptr_t> PCsCopy(GetNumPCs());
240 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000241 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000242 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
243 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000244}
245
Kostya Serebryany379359c2016-10-05 01:09:40 +0000246// Value profile.
247// We keep track of various values that affect control flow.
248// These values are inserted into a bit-set-based hash map.
249// Every new bit in the map is treated as a new coverage.
250//
251// For memcmp/strcmp/etc the interesting value is the length of the common
252// prefix of the parameters.
253// For cmp instructions the interesting value is a XOR of the parameters.
254// The interesting value is mixed up with the PC and is then added to the map.
255
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000256ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000257void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000258 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000259 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000260 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000261 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
262 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000263 uint8_t B1[Word::kMaxSize];
264 uint8_t B2[Word::kMaxSize];
265 // Copy the data into locals in this non-msan-instrumented function
266 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000267 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000268 for (size_t i = 0; i < Len; i++) {
269 B1[i] = A1[i];
270 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000271 size_t T = B1[i];
272 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000273 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000274 size_t I = 0;
275 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000276 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000277 break;
278 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000279 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000280 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000281 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000282}
283
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000284template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000285ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000286ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000287void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000288 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000289 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000290 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000291 if (sizeof(T) == 4)
292 TORC4.Insert(ArgXor, Arg1, Arg2);
293 else if (sizeof(T) == 8)
294 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000295 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000296}
297
Kostya Serebryany697f2152017-07-13 22:30:23 +0000298static size_t InternalStrnlen(const char *S, size_t MaxLen) {
299 size_t Len = 0;
300 for (; Len < MaxLen && S[Len]; Len++) {}
301 return Len;
302}
303
304// Finds min of (strlen(S1), strlen(S2)).
305// Needed bacause one of these strings may actually be non-zero terminated.
306static size_t InternalStrnlen2(const char *S1, const char *S2) {
307 size_t Len = 0;
308 for (; S1[Len] && S2[Len]; Len++) {}
309 return Len;
310}
311
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000312void TracePC::ClearInlineCounters() {
313 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
314 uint8_t *Beg = ModuleCounters[i].Start;
315 size_t Size = ModuleCounters[i].Stop - Beg;
316 memset(Beg, 0, Size);
317 }
318}
319
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000320} // namespace fuzzer
321
Dan Liew59144072016-06-06 20:27:09 +0000322extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000323ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000324ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000325void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000326 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000327 uint32_t Idx = *Guard;
328 __sancov_trace_pc_pcs[Idx] = PC;
329 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000330 // Uncomment the following line to get stack-depth profiling.
331 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000332}
Dan Liew59144072016-06-06 20:27:09 +0000333
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000334// Best-effort support for -fsanitize-coverage=trace-pc, which is available
335// in both Clang and GCC.
336ATTRIBUTE_INTERFACE
337ATTRIBUTE_NO_SANITIZE_ALL
338void __sanitizer_cov_trace_pc() {
339 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
340 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
341 __sancov_trace_pc_pcs[Idx] = PC;
342 __sancov_trace_pc_guard_8bit_counters[Idx]++;
343}
344
Marcos Pividori6137f982017-01-22 01:27:38 +0000345ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000346void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000347 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000348}
Kostya Serebryany09845172016-09-15 22:16:15 +0000349
Marcos Pividori6137f982017-01-22 01:27:38 +0000350ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000351void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
352 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
353}
354
355ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000356void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
357}
358
359ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000360ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000361void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000362 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000363 fuzzer::TPC.HandleCallerCallee(PC, Callee);
364}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000365
Marcos Pividori6137f982017-01-22 01:27:38 +0000366ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000367ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000368ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000369void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000370 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000371 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000372}
Marcos Pividori6137f982017-01-22 01:27:38 +0000373
374ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000375ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000376ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000377void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000378 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000379 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000380}
Marcos Pividori6137f982017-01-22 01:27:38 +0000381
382ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000383ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000384ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000385void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000386 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000387 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000388}
Marcos Pividori6137f982017-01-22 01:27:38 +0000389
390ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000391ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000392ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000393void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000394 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000395 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000396}
397
Marcos Pividori6137f982017-01-22 01:27:38 +0000398ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000399ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000400ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000401void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000402 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000403 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000404 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000405 // Skip the most common and the most boring case.
406 if (Vals[N - 1] < 256 && Val < 256)
407 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000408 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000409 size_t i;
410 uint64_t Token = 0;
411 for (i = 0; i < N; i++) {
412 Token = Val ^ Vals[i];
413 if (Val < Vals[i])
414 break;
415 }
416
417 if (ValSizeInBits == 16)
418 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
419 else if (ValSizeInBits == 32)
420 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000421 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000422 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000423}
424
Marcos Pividori6137f982017-01-22 01:27:38 +0000425ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000426ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000427ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000428void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000429 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000430 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000431}
Marcos Pividori6137f982017-01-22 01:27:38 +0000432
433ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000434ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000435ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000436void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000437 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000438 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000439}
Marcos Pividori6137f982017-01-22 01:27:38 +0000440
441ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000442ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000443ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000444void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000445 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000446 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000447}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000448
449ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
450void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
451 const void *s2, size_t n, int result) {
452 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
453 if (result == 0) return; // No reason to mutate.
454 if (n <= 1) return; // Not interesting.
455 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
456}
457
458ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
459void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
460 const char *s2, size_t n, int result) {
461 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
462 if (result == 0) return; // No reason to mutate.
463 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
464 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
465 n = std::min(n, Len1);
466 n = std::min(n, Len2);
467 if (n <= 1) return; // Not interesting.
468 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
469}
470
471ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
472void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
473 const char *s2, int result) {
474 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
475 if (result == 0) return; // No reason to mutate.
476 size_t N = fuzzer::InternalStrnlen2(s1, s2);
477 if (N <= 1) return; // Not interesting.
478 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
479}
480
481ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
482void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
483 const char *s2, size_t n, int result) {
484 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
485 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
486}
487
488ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
489void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
490 const char *s2, int result) {
491 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
492 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
493}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000494
495ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
496void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
497 const char *s2, char *result) {
498 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
499 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
500}
501
502ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
503void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
504 const char *s2, char *result) {
505 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
506 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
507}
508
509ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
510void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
511 const void *s2, size_t len2, void *result) {
512 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
513 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
514}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000515} // extern "C"