blob: 909227ea7e0259895ae4002c0dd3b20743632723 [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 Serebryany68382d02017-02-02 19:56:01 +000040uint8_t *TracePC::Counters() const {
41 return __sancov_trace_pc_guard_8bit_counters;
42}
43
44uintptr_t *TracePC::PCs() const {
45 return __sancov_trace_pc_pcs;
46}
47
Kostya Serebryany275e2602016-10-25 23:52:25 +000048size_t TracePC::GetTotalPCCoverage() {
49 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000050 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000051 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000052 Res++;
53 return Res;
54}
55
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000056
57void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
58 if (Start == Stop) return;
59 if (NumModulesWithInline8bitCounters &&
60 ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return;
61 assert(NumModulesWithInline8bitCounters <
62 sizeof(ModuleCounters) / sizeof(ModuleCounters[0]));
63 ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop};
64 NumInline8bitCounters += Stop - Start;
65}
66
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000067void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000068 if (Start == Stop || *Start) return;
69 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000070 for (uint32_t *P = Start; P < Stop; P++) {
71 NumGuards++;
72 if (NumGuards == kNumPCs) {
73 RawPrint(
74 "WARNING: The binary has too many instrumented PCs.\n"
75 " You may want to reduce the size of the binary\n"
76 " for more efficient fuzzing and precise coverage data\n");
77 }
78 *P = NumGuards % kNumPCs;
79 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000080 Modules[NumModules].Start = Start;
81 Modules[NumModules].Stop = Stop;
82 NumModules++;
83}
84
85void TracePC::PrintModuleInfo() {
86 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
87 for (size_t i = 0; i < NumModules; i++)
88 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
89 Printf("\n");
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000090 if (NumModulesWithInline8bitCounters) {
91 Printf("INFO: Loaded %zd modules with %zd inline 8-bit counters\n",
92 NumModulesWithInline8bitCounters, NumInline8bitCounters);
93 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++)
94 Printf("[%p, %p), ", ModuleCounters[i].Start, ModuleCounters[i].Stop);
95 Printf("\n");
96 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000097}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000098
Kostya Serebryany7f058972017-01-27 00:09:59 +000099ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000100void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
101 const uintptr_t kBits = 12;
102 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000103 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +0000104 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000105}
106
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000107void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000108 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000109 assert(!PrintedPCs);
110 PrintedPCs = new std::set<uintptr_t>;
111 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000112 if (PCs()[i])
113 PrintedPCs->insert(PCs()[i]);
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000114}
115
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000116void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000117 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000118 assert(PrintedPCs);
119 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000120 if (PCs()[i] && PrintedPCs->insert(PCs()[i]).second)
121 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs()[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000122}
123
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000124void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000125 if (!EF->__sanitizer_symbolize_pc ||
126 !EF->__sanitizer_get_module_and_offset_for_pc) {
127 Printf("INFO: __sanitizer_symbolize_pc or "
128 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000129 " 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;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000134 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
135 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000136 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000137 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000138 uintptr_t PC = PCs()[i];
139 if (!PC) continue;
140 std::string FileStr = DescribePC("%s", PC);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000141 if (!IsInterestingCoverageFile(FileStr)) continue;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000142 std::string FixedPCStr = DescribePC("%p", PC);
143 std::string FunctionStr = DescribePC("%F", PC);
144 std::string LineStr = DescribePC("%l", PC);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000145 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
146 void *OffsetRaw = nullptr;
147 if (!EF->__sanitizer_get_module_and_offset_for_pc(
Kostya Serebryany68382d02017-02-02 19:56:01 +0000148 reinterpret_cast<void *>(PC), ModulePathRaw,
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000149 sizeof(ModulePathRaw), &OffsetRaw))
150 continue;
151 std::string Module = ModulePathRaw;
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000152 uintptr_t FixedPC = std::stoull(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000153 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000154 ModuleOffsets[Module] = FixedPC - PcOffset;
155 CoveredPCsPerModule[Module].push_back(PcOffset);
156 CoveredFunctions.insert(FunctionStr);
157 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000158 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000159 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
160 continue;
161 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
162 FileStr.c_str(), LineStr.c_str());
163 }
164
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000165 std::string CoveredDirsStr;
166 for (auto &Dir : CoveredDirs) {
167 if (!CoveredDirsStr.empty())
168 CoveredDirsStr += ",";
169 CoveredDirsStr += Dir;
170 }
171 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
172
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000173 for (auto &M : CoveredPCsPerModule) {
174 std::set<std::string> UncoveredFiles, UncoveredFunctions;
175 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
176 auto &ModuleName = M.first;
177 auto &CoveredOffsets = M.second;
178 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
179 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
180 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
181 // sancov does not yet fully support DSOs.
182 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000183 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
184 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000185 std::string SanCovOutput;
186 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
187 Printf("INFO: Command failed: %s\n", Cmd.c_str());
188 continue;
189 }
190 std::istringstream ISS(SanCovOutput);
191 std::string S;
192 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000193 size_t PcOffsetEnd = S.find(':');
194 if (PcOffsetEnd == std::string::npos)
195 continue;
196 S.resize(PcOffsetEnd);
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000197 uintptr_t PcOffset = std::stoull(S, 0, 16);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000198 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
199 PcOffset)) {
200 uintptr_t PC = ModuleOffset + PcOffset;
201 auto FileStr = DescribePC("%s", PC);
202 if (!IsInterestingCoverageFile(FileStr)) continue;
203 if (CoveredFiles.count(FileStr) == 0) {
204 UncoveredFiles.insert(FileStr);
205 continue;
206 }
207 auto FunctionStr = DescribePC("%F", PC);
208 if (CoveredFunctions.count(FunctionStr) == 0) {
209 UncoveredFunctions.insert(FunctionStr);
210 continue;
211 }
212 std::string LineStr = DescribePC("%l", PC);
213 uintptr_t Line = std::stoi(LineStr);
214 std::string FileLineStr = FileStr + ":" + LineStr;
215 if (CoveredLines.count(FileLineStr) == 0)
216 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
217 }
218 }
219 for (auto &FileLine: UncoveredLines)
220 for (int Line : FileLine.second)
221 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
222 for (auto &Func : UncoveredFunctions)
223 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
224 for (auto &File : UncoveredFiles)
225 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000226 }
227}
228
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000229inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
230 // TODO: this implementation is x86 only.
231 // see sanitizer_common GetPreviousInstructionPc for full implementation.
232 return PC - 1;
233}
234
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000235void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000236 if (EF->__sanitizer_dump_coverage) {
237 std::vector<uintptr_t> PCsCopy(GetNumPCs());
238 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000239 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000240 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
241 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000242}
243
Kostya Serebryany379359c2016-10-05 01:09:40 +0000244// Value profile.
245// We keep track of various values that affect control flow.
246// These values are inserted into a bit-set-based hash map.
247// Every new bit in the map is treated as a new coverage.
248//
249// For memcmp/strcmp/etc the interesting value is the length of the common
250// prefix of the parameters.
251// For cmp instructions the interesting value is a XOR of the parameters.
252// The interesting value is mixed up with the PC and is then added to the map.
253
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000254ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000255void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000256 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000257 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000258 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000259 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
260 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000261 uint8_t B1[Word::kMaxSize];
262 uint8_t B2[Word::kMaxSize];
263 // Copy the data into locals in this non-msan-instrumented function
264 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000265 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000266 for (size_t i = 0; i < Len; i++) {
267 B1[i] = A1[i];
268 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000269 size_t T = B1[i];
270 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000271 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000272 size_t I = 0;
273 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000274 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000275 break;
276 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000277 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000278 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000279 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000280}
281
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000282template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000283ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000284ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000285void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000286 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000287 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000288 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000289 if (sizeof(T) == 4)
290 TORC4.Insert(ArgXor, Arg1, Arg2);
291 else if (sizeof(T) == 8)
292 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000293 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000294}
295
Kostya Serebryany697f2152017-07-13 22:30:23 +0000296static size_t InternalStrnlen(const char *S, size_t MaxLen) {
297 size_t Len = 0;
298 for (; Len < MaxLen && S[Len]; Len++) {}
299 return Len;
300}
301
302// Finds min of (strlen(S1), strlen(S2)).
303// Needed bacause one of these strings may actually be non-zero terminated.
304static size_t InternalStrnlen2(const char *S1, const char *S2) {
305 size_t Len = 0;
306 for (; S1[Len] && S2[Len]; Len++) {}
307 return Len;
308}
309
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000310} // namespace fuzzer
311
Dan Liew59144072016-06-06 20:27:09 +0000312extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000313ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000314ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000315void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000316 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000317 uint32_t Idx = *Guard;
318 __sancov_trace_pc_pcs[Idx] = PC;
319 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000320}
Dan Liew59144072016-06-06 20:27:09 +0000321
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000322// Best-effort support for -fsanitize-coverage=trace-pc, which is available
323// in both Clang and GCC.
324ATTRIBUTE_INTERFACE
325ATTRIBUTE_NO_SANITIZE_ALL
326void __sanitizer_cov_trace_pc() {
327 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
328 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
329 __sancov_trace_pc_pcs[Idx] = PC;
330 __sancov_trace_pc_guard_8bit_counters[Idx]++;
331}
332
Marcos Pividori6137f982017-01-22 01:27:38 +0000333ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000334void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000335 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000336}
Kostya Serebryany09845172016-09-15 22:16:15 +0000337
Marcos Pividori6137f982017-01-22 01:27:38 +0000338ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000339void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
340 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
341}
342
343ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000344ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000345void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000346 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000347 fuzzer::TPC.HandleCallerCallee(PC, Callee);
348}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000349
Marcos Pividori6137f982017-01-22 01:27:38 +0000350ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000351ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000352ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000353void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000354 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000355 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000356}
Marcos Pividori6137f982017-01-22 01:27:38 +0000357
358ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000359ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000360ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000361void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000362 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000363 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000364}
Marcos Pividori6137f982017-01-22 01:27:38 +0000365
366ATTRIBUTE_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_cmp2(uint16_t Arg1, uint16_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_cmp1(uint8_t Arg1, uint8_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}
381
Marcos Pividori6137f982017-01-22 01:27:38 +0000382ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000383ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000384ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000385void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000386 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000387 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000388 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000389 // Skip the most common and the most boring case.
390 if (Vals[N - 1] < 256 && Val < 256)
391 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000392 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000393 size_t i;
394 uint64_t Token = 0;
395 for (i = 0; i < N; i++) {
396 Token = Val ^ Vals[i];
397 if (Val < Vals[i])
398 break;
399 }
400
401 if (ValSizeInBits == 16)
402 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
403 else if (ValSizeInBits == 32)
404 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000405 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000406 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000407}
408
Marcos Pividori6137f982017-01-22 01:27:38 +0000409ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000410ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000411ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000412void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000413 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000414 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000415}
Marcos Pividori6137f982017-01-22 01:27:38 +0000416
417ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000418ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000419ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000420void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000421 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000422 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000423}
Marcos Pividori6137f982017-01-22 01:27:38 +0000424
425ATTRIBUTE_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_gep(uintptr_t Idx) {
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, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000431}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000432
433ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
434void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
435 const void *s2, size_t n, int result) {
436 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
437 if (result == 0) return; // No reason to mutate.
438 if (n <= 1) return; // Not interesting.
439 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
440}
441
442ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
443void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
444 const char *s2, size_t n, int result) {
445 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
446 if (result == 0) return; // No reason to mutate.
447 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
448 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
449 n = std::min(n, Len1);
450 n = std::min(n, Len2);
451 if (n <= 1) return; // Not interesting.
452 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
453}
454
455ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
456void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
457 const char *s2, int result) {
458 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
459 if (result == 0) return; // No reason to mutate.
460 size_t N = fuzzer::InternalStrnlen2(s1, s2);
461 if (N <= 1) return; // Not interesting.
462 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
463}
464
465ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
466void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
467 const char *s2, size_t n, int result) {
468 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
469 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
470}
471
472ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
473void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
474 const char *s2, int result) {
475 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
476 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
477}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000478} // extern "C"