blob: a83289fd44cc43e6e6c683a6716bf0eca7d07802 [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 <set>
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000024
Kostya Serebryany68382d02017-02-02 19:56:01 +000025// The coverage counters and PCs.
26// These are declared as global variables named "__sancov_*" to simplify
27// experiments with inlined instrumentation.
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000028alignas(64) ATTRIBUTE_INTERFACE
Mike Aizatsky1b658122017-02-03 20:26:44 +000029uint8_t __sancov_trace_pc_guard_8bit_counters[fuzzer::TracePC::kNumPCs];
30
31ATTRIBUTE_INTERFACE
Kostya Serebryany68382d02017-02-02 19:56:01 +000032uintptr_t __sancov_trace_pc_pcs[fuzzer::TracePC::kNumPCs];
33
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000034namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000035
Kostya Serebryanya00b2432016-09-14 02:13:06 +000036TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000037
Kostya Serebryanyf64b8482017-07-14 00:06:27 +000038int ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr;
39
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() {
Kostya Serebryanye8637962017-08-08 00:17:20 +000049 if (ObservedPCs.size())
50 return ObservedPCs.size();
Kostya Serebryany275e2602016-10-25 23:52:25 +000051 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 Serebryanybe7a3572017-08-04 23:13:58 +0000139void TracePC::UpdateObservedPCs() {
140 if (NumPCsInPCTables) {
141 auto Observe = [&](uintptr_t PC) {
Kostya Serebryanye8637962017-08-08 00:17:20 +0000142 bool Inserted = ObservedPCs.insert(PC).second;
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000143 if (Inserted && DoPrintNewPCs)
144 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1);
145 };
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000146
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000147 if (NumInline8bitCounters == NumPCsInPCTables) {
148 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
149 uint8_t *Beg = ModuleCounters[i].Start;
150 size_t Size = ModuleCounters[i].Stop - Beg;
151 assert(Size ==
152 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
153 for (size_t j = 0; j < Size; j++)
154 if (Beg[j])
155 Observe(ModulePCTable[i].Start[j]);
156 }
157 } else if (NumGuards == NumPCsInPCTables) {
158 size_t GuardIdx = 1;
159 for (size_t i = 0; i < NumModules; i++) {
160 uint32_t *Beg = Modules[i].Start;
161 size_t Size = Modules[i].Stop - Beg;
162 assert(Size ==
163 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
164 for (size_t j = 0; j < Size; j++, GuardIdx++)
165 if (Counters()[GuardIdx])
166 Observe(ModulePCTable[i].Start[j]);
167 }
168 }
169 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000170}
171
Kostya Serebryany854be982017-08-08 00:12:09 +0000172inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
173 // TODO: this implementation is x86 only.
174 // see sanitizer_common GetPreviousInstructionPc for full implementation.
175 return PC - 1;
176}
177
178inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
179 // TODO: this implementation is x86 only.
180 // see sanitizer_common GetPreviousInstructionPc for full implementation.
181 return PC + 1;
182}
183
184static std::string GetModuleName(uintptr_t PC) {
185 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
186 void *OffsetRaw = nullptr;
187 if (!EF->__sanitizer_get_module_and_offset_for_pc(
188 reinterpret_cast<void *>(PC), ModulePathRaw,
189 sizeof(ModulePathRaw), &OffsetRaw))
190 return "";
191 return ModulePathRaw;
192}
193
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000194void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000195 if (!EF->__sanitizer_symbolize_pc ||
196 !EF->__sanitizer_get_module_and_offset_for_pc) {
197 Printf("INFO: __sanitizer_symbolize_pc or "
198 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000199 " not printing coverage\n");
200 return;
201 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000202 Printf("COVERAGE:\n");
Kostya Serebryany854be982017-08-08 00:12:09 +0000203 std::string LastFunctionName = "";
204 std::string LastFileStr = "";
205 std::set<size_t> UncoveredLines;
206 std::set<size_t> CoveredLines;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000207
Kostya Serebryany854be982017-08-08 00:12:09 +0000208 auto FunctionEndCallback = [&](const std::string &CurrentFunc,
209 const std::string &CurrentFile) {
210 if (LastFunctionName != CurrentFunc) {
211 if (CoveredLines.empty() && !UncoveredLines.empty()) {
212 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str());
213 } else {
214 for (auto Line : UncoveredLines) {
215 if (!CoveredLines.count(Line))
216 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(),
217 LastFileStr.c_str(), Line);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000218 }
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000219 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000220
Kostya Serebryany854be982017-08-08 00:12:09 +0000221 UncoveredLines.clear();
222 CoveredLines.clear();
223 LastFunctionName = CurrentFunc;
224 LastFileStr = CurrentFile;
225 }
226 };
227
228 for (size_t i = 0; i < NumPCTables; i++) {
229 auto &M = ModulePCTable[i];
230 assert(M.Start < M.Stop);
231 auto ModuleName = GetModuleName(*M.Start);
232 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) {
233 auto PC = *Ptr;
234 auto VisualizePC = GetNextInstructionPc(PC);
Kostya Serebryanye8637962017-08-08 00:17:20 +0000235 bool IsObserved = ObservedPCs.count(PC);
Kostya Serebryany854be982017-08-08 00:12:09 +0000236 std::string FileStr = DescribePC("%s", VisualizePC);
237 if (!IsInterestingCoverageFile(FileStr)) continue;
238 std::string FunctionStr = DescribePC("%F", VisualizePC);
239 FunctionEndCallback(FunctionStr, FileStr);
240 std::string LineStr = DescribePC("%l", VisualizePC);
241 size_t Line = std::stoul(LineStr);
242 if (IsObserved && CoveredLines.insert(Line).second)
243 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(),
244 Line);
245 else
246 UncoveredLines.insert(Line);
247 }
248 }
249 FunctionEndCallback("", "");
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000250}
251
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000252void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000253 if (EF->__sanitizer_dump_coverage) {
254 std::vector<uintptr_t> PCsCopy(GetNumPCs());
255 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000256 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000257 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
258 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000259}
260
Kostya Serebryany379359c2016-10-05 01:09:40 +0000261// Value profile.
262// We keep track of various values that affect control flow.
263// These values are inserted into a bit-set-based hash map.
264// Every new bit in the map is treated as a new coverage.
265//
266// For memcmp/strcmp/etc the interesting value is the length of the common
267// prefix of the parameters.
268// For cmp instructions the interesting value is a XOR of the parameters.
269// The interesting value is mixed up with the PC and is then added to the map.
270
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000271ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000272void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000273 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000274 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000275 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000276 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
277 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000278 uint8_t B1[Word::kMaxSize];
279 uint8_t B2[Word::kMaxSize];
280 // Copy the data into locals in this non-msan-instrumented function
281 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000282 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000283 for (size_t i = 0; i < Len; i++) {
284 B1[i] = A1[i];
285 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000286 size_t T = B1[i];
287 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000288 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000289 size_t I = 0;
290 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000291 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000292 break;
293 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000294 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000295 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000296 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000297}
298
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000299template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000300ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000301ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000302void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000303 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000304 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000305 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000306 if (sizeof(T) == 4)
307 TORC4.Insert(ArgXor, Arg1, Arg2);
308 else if (sizeof(T) == 8)
309 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000310 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000311}
312
Kostya Serebryany697f2152017-07-13 22:30:23 +0000313static size_t InternalStrnlen(const char *S, size_t MaxLen) {
314 size_t Len = 0;
315 for (; Len < MaxLen && S[Len]; Len++) {}
316 return Len;
317}
318
319// Finds min of (strlen(S1), strlen(S2)).
320// Needed bacause one of these strings may actually be non-zero terminated.
321static size_t InternalStrnlen2(const char *S1, const char *S2) {
322 size_t Len = 0;
323 for (; S1[Len] && S2[Len]; Len++) {}
324 return Len;
325}
326
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000327void TracePC::ClearInlineCounters() {
328 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
329 uint8_t *Beg = ModuleCounters[i].Start;
330 size_t Size = ModuleCounters[i].Stop - Beg;
331 memset(Beg, 0, Size);
332 }
333}
334
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000335} // namespace fuzzer
336
Dan Liew59144072016-06-06 20:27:09 +0000337extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000338ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000339ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000340void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000341 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000342 uint32_t Idx = *Guard;
343 __sancov_trace_pc_pcs[Idx] = PC;
344 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000345 // Uncomment the following line to get stack-depth profiling.
346 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000347}
Dan Liew59144072016-06-06 20:27:09 +0000348
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000349// Best-effort support for -fsanitize-coverage=trace-pc, which is available
350// in both Clang and GCC.
351ATTRIBUTE_INTERFACE
352ATTRIBUTE_NO_SANITIZE_ALL
353void __sanitizer_cov_trace_pc() {
354 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
355 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
356 __sancov_trace_pc_pcs[Idx] = PC;
357 __sancov_trace_pc_guard_8bit_counters[Idx]++;
358}
359
Marcos Pividori6137f982017-01-22 01:27:38 +0000360ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000361void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000362 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000363}
Kostya Serebryany09845172016-09-15 22:16:15 +0000364
Marcos Pividori6137f982017-01-22 01:27:38 +0000365ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000366void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
367 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
368}
369
370ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000371void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000372 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000373}
374
375ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000376ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000377void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000378 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000379 fuzzer::TPC.HandleCallerCallee(PC, Callee);
380}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000381
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 Serebryany17d176e12016-10-13 16:19:09 +0000385void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_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_cmp4(uint32_t Arg1, uint32_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}
Marcos Pividori6137f982017-01-22 01:27:38 +0000397
398ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000399ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000400ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000401void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000402 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000403 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000404}
Marcos Pividori6137f982017-01-22 01:27:38 +0000405
406ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000407ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000408ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000409void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000410 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000411 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000412}
413
Marcos Pividori6137f982017-01-22 01:27:38 +0000414ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000415ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000416ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000417void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000418 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000419 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000420 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000421 // Skip the most common and the most boring case.
422 if (Vals[N - 1] < 256 && Val < 256)
423 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000424 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000425 size_t i;
426 uint64_t Token = 0;
427 for (i = 0; i < N; i++) {
428 Token = Val ^ Vals[i];
429 if (Val < Vals[i])
430 break;
431 }
432
433 if (ValSizeInBits == 16)
434 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
435 else if (ValSizeInBits == 32)
436 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000437 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000438 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000439}
440
Marcos Pividori6137f982017-01-22 01:27:38 +0000441ATTRIBUTE_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_div4(uint32_t Val) {
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, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000447}
Marcos Pividori6137f982017-01-22 01:27:38 +0000448
449ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000450ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000451ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000452void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000453 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000454 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000455}
Marcos Pividori6137f982017-01-22 01:27:38 +0000456
457ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000458ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000459ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000460void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000461 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000462 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000463}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000464
465ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
466void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
467 const void *s2, size_t n, int result) {
468 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
469 if (result == 0) return; // No reason to mutate.
470 if (n <= 1) return; // Not interesting.
471 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
472}
473
474ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
475void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
476 const char *s2, size_t n, int result) {
477 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
478 if (result == 0) return; // No reason to mutate.
479 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
480 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
481 n = std::min(n, Len1);
482 n = std::min(n, Len2);
483 if (n <= 1) return; // Not interesting.
484 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
485}
486
487ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
488void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
489 const char *s2, int result) {
490 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
491 if (result == 0) return; // No reason to mutate.
492 size_t N = fuzzer::InternalStrnlen2(s1, s2);
493 if (N <= 1) return; // Not interesting.
494 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
495}
496
497ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
498void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
499 const char *s2, size_t n, int result) {
500 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
501 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
502}
503
504ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
505void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
506 const char *s2, int result) {
507 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
508 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
509}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000510
511ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
512void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
513 const char *s2, char *result) {
514 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
515 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
516}
517
518ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
519void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
520 const char *s2, char *result) {
521 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
522 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
523}
524
525ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
526void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
527 const void *s2, size_t len2, void *result) {
528 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
529 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
530}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000531} // extern "C"