blob: d42bfc253fdc839421dc39aa1d88c5f34335f63a [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() {
Kostya Serebryanybe7a3572017-08-04 23:13:58 +000051 if (ObservedPCs)
52 return ObservedPCs->size();
Kostya Serebryany275e2602016-10-25 23:52:25 +000053 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000054 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000055 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000056 Res++;
57 return Res;
58}
59
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000060
61void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
62 if (Start == Stop) return;
63 if (NumModulesWithInline8bitCounters &&
64 ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return;
65 assert(NumModulesWithInline8bitCounters <
66 sizeof(ModuleCounters) / sizeof(ModuleCounters[0]));
67 ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop};
68 NumInline8bitCounters += Stop - Start;
69}
70
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000071void TracePC::HandlePCsInit(const uint8_t *Start, const uint8_t *Stop) {
72 const uintptr_t *B = reinterpret_cast<const uintptr_t *>(Start);
73 const uintptr_t *E = reinterpret_cast<const uintptr_t *>(Stop);
74 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return;
75 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0]));
76 ModulePCTable[NumPCTables++] = {B, E};
Kostya Serebryany4f297002017-08-01 00:48:44 +000077 NumPCsInPCTables += E - B;
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000078}
79
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000080void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000081 if (Start == Stop || *Start) return;
82 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000083 for (uint32_t *P = Start; P < Stop; P++) {
84 NumGuards++;
85 if (NumGuards == kNumPCs) {
86 RawPrint(
87 "WARNING: The binary has too many instrumented PCs.\n"
88 " You may want to reduce the size of the binary\n"
89 " for more efficient fuzzing and precise coverage data\n");
90 }
91 *P = NumGuards % kNumPCs;
92 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000093 Modules[NumModules].Start = Start;
94 Modules[NumModules].Stop = Stop;
95 NumModules++;
96}
97
98void TracePC::PrintModuleInfo() {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000099 if (NumGuards) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000100 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000101 for (size_t i = 0; i < NumModules; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000102 Printf("%zd [%p, %p), ", Modules[i].Stop - Modules[i].Start,
103 Modules[i].Start, Modules[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000104 Printf("\n");
105 }
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000106 if (NumModulesWithInline8bitCounters) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000107 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ",
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000108 NumModulesWithInline8bitCounters, NumInline8bitCounters);
109 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000110 Printf("%zd [%p, %p), ", ModuleCounters[i].Stop - ModuleCounters[i].Start,
111 ModuleCounters[i].Start, ModuleCounters[i].Stop);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000112 Printf("\n");
113 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000114 if (NumPCTables) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000115 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables,
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000116 NumPCsInPCTables);
117 for (size_t i = 0; i < NumPCTables; i++) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000118 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start,
119 ModulePCTable[i].Start, ModulePCTable[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000120 }
121 Printf("\n");
Kostya Serebryany4f297002017-08-01 00:48:44 +0000122
123 if ((NumGuards && NumGuards != NumPCsInPCTables) ||
124 (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables)) {
125 Printf("ERROR: The size of coverage PC tables does not match the"
126 " number of instrumented PCs. This might be a bug in the compiler,"
127 " please contact the libFuzzer developers.\n");
128 _Exit(1);
129 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000130 }
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000131}
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000132
Kostya Serebryany7f058972017-01-27 00:09:59 +0000133ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000134void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
135 const uintptr_t kBits = 12;
136 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000137 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +0000138 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000139}
140
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000141void TracePC::UpdateObservedPCs() {
142 if (NumPCsInPCTables) {
143 auto Observe = [&](uintptr_t PC) {
144 bool Inserted = ObservedPCs->insert(PC).second;
145 if (Inserted && DoPrintNewPCs)
146 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1);
147 };
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000148
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000149 if (!ObservedPCs)
150 ObservedPCs = new std::set<uintptr_t>;
151
152 if (NumInline8bitCounters == NumPCsInPCTables) {
153 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
154 uint8_t *Beg = ModuleCounters[i].Start;
155 size_t Size = ModuleCounters[i].Stop - Beg;
156 assert(Size ==
157 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
158 for (size_t j = 0; j < Size; j++)
159 if (Beg[j])
160 Observe(ModulePCTable[i].Start[j]);
161 }
162 } else if (NumGuards == NumPCsInPCTables) {
163 size_t GuardIdx = 1;
164 for (size_t i = 0; i < NumModules; i++) {
165 uint32_t *Beg = Modules[i].Start;
166 size_t Size = Modules[i].Stop - Beg;
167 assert(Size ==
168 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
169 for (size_t j = 0; j < Size; j++, GuardIdx++)
170 if (Counters()[GuardIdx])
171 Observe(ModulePCTable[i].Start[j]);
172 }
173 }
174 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000175}
176
Kostya Serebryany854be982017-08-08 00:12:09 +0000177inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
178 // TODO: this implementation is x86 only.
179 // see sanitizer_common GetPreviousInstructionPc for full implementation.
180 return PC - 1;
181}
182
183inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
184 // TODO: this implementation is x86 only.
185 // see sanitizer_common GetPreviousInstructionPc for full implementation.
186 return PC + 1;
187}
188
189static std::string GetModuleName(uintptr_t PC) {
190 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
191 void *OffsetRaw = nullptr;
192 if (!EF->__sanitizer_get_module_and_offset_for_pc(
193 reinterpret_cast<void *>(PC), ModulePathRaw,
194 sizeof(ModulePathRaw), &OffsetRaw))
195 return "";
196 return ModulePathRaw;
197}
198
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000199void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000200 if (!EF->__sanitizer_symbolize_pc ||
201 !EF->__sanitizer_get_module_and_offset_for_pc) {
202 Printf("INFO: __sanitizer_symbolize_pc or "
203 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000204 " not printing coverage\n");
205 return;
206 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000207 Printf("COVERAGE:\n");
Kostya Serebryany854be982017-08-08 00:12:09 +0000208 std::string LastFunctionName = "";
209 std::string LastFileStr = "";
210 std::set<size_t> UncoveredLines;
211 std::set<size_t> CoveredLines;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000212
Kostya Serebryany854be982017-08-08 00:12:09 +0000213 auto FunctionEndCallback = [&](const std::string &CurrentFunc,
214 const std::string &CurrentFile) {
215 if (LastFunctionName != CurrentFunc) {
216 if (CoveredLines.empty() && !UncoveredLines.empty()) {
217 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str());
218 } else {
219 for (auto Line : UncoveredLines) {
220 if (!CoveredLines.count(Line))
221 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(),
222 LastFileStr.c_str(), Line);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000223 }
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000224 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000225
Kostya Serebryany854be982017-08-08 00:12:09 +0000226 UncoveredLines.clear();
227 CoveredLines.clear();
228 LastFunctionName = CurrentFunc;
229 LastFileStr = CurrentFile;
230 }
231 };
232
233 for (size_t i = 0; i < NumPCTables; i++) {
234 auto &M = ModulePCTable[i];
235 assert(M.Start < M.Stop);
236 auto ModuleName = GetModuleName(*M.Start);
237 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) {
238 auto PC = *Ptr;
239 auto VisualizePC = GetNextInstructionPc(PC);
240 bool IsObserved = ObservedPCs->count(PC);
241 std::string FileStr = DescribePC("%s", VisualizePC);
242 if (!IsInterestingCoverageFile(FileStr)) continue;
243 std::string FunctionStr = DescribePC("%F", VisualizePC);
244 FunctionEndCallback(FunctionStr, FileStr);
245 std::string LineStr = DescribePC("%l", VisualizePC);
246 size_t Line = std::stoul(LineStr);
247 if (IsObserved && CoveredLines.insert(Line).second)
248 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(),
249 Line);
250 else
251 UncoveredLines.insert(Line);
252 }
253 }
254 FunctionEndCallback("", "");
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000255}
256
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000257void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000258 if (EF->__sanitizer_dump_coverage) {
259 std::vector<uintptr_t> PCsCopy(GetNumPCs());
260 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000261 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000262 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
263 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000264}
265
Kostya Serebryany379359c2016-10-05 01:09:40 +0000266// Value profile.
267// We keep track of various values that affect control flow.
268// These values are inserted into a bit-set-based hash map.
269// Every new bit in the map is treated as a new coverage.
270//
271// For memcmp/strcmp/etc the interesting value is the length of the common
272// prefix of the parameters.
273// For cmp instructions the interesting value is a XOR of the parameters.
274// The interesting value is mixed up with the PC and is then added to the map.
275
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000276ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000277void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000278 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000279 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000280 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000281 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
282 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000283 uint8_t B1[Word::kMaxSize];
284 uint8_t B2[Word::kMaxSize];
285 // Copy the data into locals in this non-msan-instrumented function
286 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000287 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000288 for (size_t i = 0; i < Len; i++) {
289 B1[i] = A1[i];
290 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000291 size_t T = B1[i];
292 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000293 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000294 size_t I = 0;
295 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000296 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000297 break;
298 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000299 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000300 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000301 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000302}
303
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000304template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000305ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000306ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000307void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000308 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000309 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000310 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000311 if (sizeof(T) == 4)
312 TORC4.Insert(ArgXor, Arg1, Arg2);
313 else if (sizeof(T) == 8)
314 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000315 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000316}
317
Kostya Serebryany697f2152017-07-13 22:30:23 +0000318static size_t InternalStrnlen(const char *S, size_t MaxLen) {
319 size_t Len = 0;
320 for (; Len < MaxLen && S[Len]; Len++) {}
321 return Len;
322}
323
324// Finds min of (strlen(S1), strlen(S2)).
325// Needed bacause one of these strings may actually be non-zero terminated.
326static size_t InternalStrnlen2(const char *S1, const char *S2) {
327 size_t Len = 0;
328 for (; S1[Len] && S2[Len]; Len++) {}
329 return Len;
330}
331
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000332void TracePC::ClearInlineCounters() {
333 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
334 uint8_t *Beg = ModuleCounters[i].Start;
335 size_t Size = ModuleCounters[i].Stop - Beg;
336 memset(Beg, 0, Size);
337 }
338}
339
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000340} // namespace fuzzer
341
Dan Liew59144072016-06-06 20:27:09 +0000342extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000343ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000344ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000345void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000346 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000347 uint32_t Idx = *Guard;
348 __sancov_trace_pc_pcs[Idx] = PC;
349 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000350 // Uncomment the following line to get stack-depth profiling.
351 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000352}
Dan Liew59144072016-06-06 20:27:09 +0000353
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000354// Best-effort support for -fsanitize-coverage=trace-pc, which is available
355// in both Clang and GCC.
356ATTRIBUTE_INTERFACE
357ATTRIBUTE_NO_SANITIZE_ALL
358void __sanitizer_cov_trace_pc() {
359 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
360 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
361 __sancov_trace_pc_pcs[Idx] = PC;
362 __sancov_trace_pc_guard_8bit_counters[Idx]++;
363}
364
Marcos Pividori6137f982017-01-22 01:27:38 +0000365ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000366void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000367 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000368}
Kostya Serebryany09845172016-09-15 22:16:15 +0000369
Marcos Pividori6137f982017-01-22 01:27:38 +0000370ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000371void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
372 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
373}
374
375ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000376void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000377 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000378}
379
380ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000381ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000382void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000383 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000384 fuzzer::TPC.HandleCallerCallee(PC, Callee);
385}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000386
Marcos Pividori6137f982017-01-22 01:27:38 +0000387ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000388ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000389ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000390void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000391 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000392 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000393}
Marcos Pividori6137f982017-01-22 01:27:38 +0000394
395ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000396ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000397ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000398void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000399 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000400 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000401}
Marcos Pividori6137f982017-01-22 01:27:38 +0000402
403ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000404ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000405ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000406void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000407 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000408 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000409}
Marcos Pividori6137f982017-01-22 01:27:38 +0000410
411ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000412ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000413ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000414void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000415 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000416 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000417}
418
Marcos Pividori6137f982017-01-22 01:27:38 +0000419ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000420ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000421ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000422void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000423 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000424 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000425 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000426 // Skip the most common and the most boring case.
427 if (Vals[N - 1] < 256 && Val < 256)
428 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000429 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000430 size_t i;
431 uint64_t Token = 0;
432 for (i = 0; i < N; i++) {
433 Token = Val ^ Vals[i];
434 if (Val < Vals[i])
435 break;
436 }
437
438 if (ValSizeInBits == 16)
439 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
440 else if (ValSizeInBits == 32)
441 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000442 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000443 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000444}
445
Marcos Pividori6137f982017-01-22 01:27:38 +0000446ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000447ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000448ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000449void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000450 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000451 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000452}
Marcos Pividori6137f982017-01-22 01:27:38 +0000453
454ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000455ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000456ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000457void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000458 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000459 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000460}
Marcos Pividori6137f982017-01-22 01:27:38 +0000461
462ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000463ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000464ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000465void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000466 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000467 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000468}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000469
470ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
471void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
472 const void *s2, size_t n, int result) {
473 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
474 if (result == 0) return; // No reason to mutate.
475 if (n <= 1) return; // Not interesting.
476 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
477}
478
479ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
480void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
481 const char *s2, size_t n, int result) {
482 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
483 if (result == 0) return; // No reason to mutate.
484 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
485 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
486 n = std::min(n, Len1);
487 n = std::min(n, Len2);
488 if (n <= 1) return; // Not interesting.
489 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
490}
491
492ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
493void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
494 const char *s2, int result) {
495 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
496 if (result == 0) return; // No reason to mutate.
497 size_t N = fuzzer::InternalStrnlen2(s1, s2);
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_strncasecmp(void *called_pc, const char *s1,
504 const char *s2, size_t n, int result) {
505 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
506 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
507}
508
509ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
510void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
511 const char *s2, int result) {
512 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
513 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
514}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000515
516ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
517void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
518 const char *s2, char *result) {
519 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
520 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
521}
522
523ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
524void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
525 const char *s2, char *result) {
526 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
527 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
528}
529
530ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
531void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
532 const void *s2, size_t len2, void *result) {
533 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
534 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
535}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000536} // extern "C"