blob: a646c250225839dcd258f1af82e84fa439021a9e [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 Serebryanybe7a3572017-08-04 23:13:58 +000049 if (ObservedPCs)
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) {
142 bool Inserted = ObservedPCs->insert(PC).second;
143 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 (!ObservedPCs)
148 ObservedPCs = new std::set<uintptr_t>;
149
150 if (NumInline8bitCounters == NumPCsInPCTables) {
151 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
152 uint8_t *Beg = ModuleCounters[i].Start;
153 size_t Size = ModuleCounters[i].Stop - Beg;
154 assert(Size ==
155 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
156 for (size_t j = 0; j < Size; j++)
157 if (Beg[j])
158 Observe(ModulePCTable[i].Start[j]);
159 }
160 } else if (NumGuards == NumPCsInPCTables) {
161 size_t GuardIdx = 1;
162 for (size_t i = 0; i < NumModules; i++) {
163 uint32_t *Beg = Modules[i].Start;
164 size_t Size = Modules[i].Stop - Beg;
165 assert(Size ==
166 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
167 for (size_t j = 0; j < Size; j++, GuardIdx++)
168 if (Counters()[GuardIdx])
169 Observe(ModulePCTable[i].Start[j]);
170 }
171 }
172 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000173}
174
Kostya Serebryany854be982017-08-08 00:12:09 +0000175inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
176 // TODO: this implementation is x86 only.
177 // see sanitizer_common GetPreviousInstructionPc for full implementation.
178 return PC - 1;
179}
180
181inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
182 // TODO: this implementation is x86 only.
183 // see sanitizer_common GetPreviousInstructionPc for full implementation.
184 return PC + 1;
185}
186
187static std::string GetModuleName(uintptr_t PC) {
188 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
189 void *OffsetRaw = nullptr;
190 if (!EF->__sanitizer_get_module_and_offset_for_pc(
191 reinterpret_cast<void *>(PC), ModulePathRaw,
192 sizeof(ModulePathRaw), &OffsetRaw))
193 return "";
194 return ModulePathRaw;
195}
196
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000197void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000198 if (!EF->__sanitizer_symbolize_pc ||
199 !EF->__sanitizer_get_module_and_offset_for_pc) {
200 Printf("INFO: __sanitizer_symbolize_pc or "
201 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000202 " not printing coverage\n");
203 return;
204 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000205 Printf("COVERAGE:\n");
Kostya Serebryany854be982017-08-08 00:12:09 +0000206 std::string LastFunctionName = "";
207 std::string LastFileStr = "";
208 std::set<size_t> UncoveredLines;
209 std::set<size_t> CoveredLines;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000210
Kostya Serebryany854be982017-08-08 00:12:09 +0000211 auto FunctionEndCallback = [&](const std::string &CurrentFunc,
212 const std::string &CurrentFile) {
213 if (LastFunctionName != CurrentFunc) {
214 if (CoveredLines.empty() && !UncoveredLines.empty()) {
215 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str());
216 } else {
217 for (auto Line : UncoveredLines) {
218 if (!CoveredLines.count(Line))
219 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(),
220 LastFileStr.c_str(), Line);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000221 }
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000222 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000223
Kostya Serebryany854be982017-08-08 00:12:09 +0000224 UncoveredLines.clear();
225 CoveredLines.clear();
226 LastFunctionName = CurrentFunc;
227 LastFileStr = CurrentFile;
228 }
229 };
230
231 for (size_t i = 0; i < NumPCTables; i++) {
232 auto &M = ModulePCTable[i];
233 assert(M.Start < M.Stop);
234 auto ModuleName = GetModuleName(*M.Start);
235 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) {
236 auto PC = *Ptr;
237 auto VisualizePC = GetNextInstructionPc(PC);
238 bool IsObserved = ObservedPCs->count(PC);
239 std::string FileStr = DescribePC("%s", VisualizePC);
240 if (!IsInterestingCoverageFile(FileStr)) continue;
241 std::string FunctionStr = DescribePC("%F", VisualizePC);
242 FunctionEndCallback(FunctionStr, FileStr);
243 std::string LineStr = DescribePC("%l", VisualizePC);
244 size_t Line = std::stoul(LineStr);
245 if (IsObserved && CoveredLines.insert(Line).second)
246 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(),
247 Line);
248 else
249 UncoveredLines.insert(Line);
250 }
251 }
252 FunctionEndCallback("", "");
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000253}
254
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000255void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000256 if (EF->__sanitizer_dump_coverage) {
257 std::vector<uintptr_t> PCsCopy(GetNumPCs());
258 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000259 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000260 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
261 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000262}
263
Kostya Serebryany379359c2016-10-05 01:09:40 +0000264// Value profile.
265// We keep track of various values that affect control flow.
266// These values are inserted into a bit-set-based hash map.
267// Every new bit in the map is treated as a new coverage.
268//
269// For memcmp/strcmp/etc the interesting value is the length of the common
270// prefix of the parameters.
271// For cmp instructions the interesting value is a XOR of the parameters.
272// The interesting value is mixed up with the PC and is then added to the map.
273
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000274ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000275void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000276 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000277 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000278 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000279 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
280 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000281 uint8_t B1[Word::kMaxSize];
282 uint8_t B2[Word::kMaxSize];
283 // Copy the data into locals in this non-msan-instrumented function
284 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000285 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000286 for (size_t i = 0; i < Len; i++) {
287 B1[i] = A1[i];
288 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000289 size_t T = B1[i];
290 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000291 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000292 size_t I = 0;
293 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000294 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000295 break;
296 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000297 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000298 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000299 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000300}
301
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000302template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000303ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000304ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000305void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000306 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000307 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000308 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000309 if (sizeof(T) == 4)
310 TORC4.Insert(ArgXor, Arg1, Arg2);
311 else if (sizeof(T) == 8)
312 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000313 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000314}
315
Kostya Serebryany697f2152017-07-13 22:30:23 +0000316static size_t InternalStrnlen(const char *S, size_t MaxLen) {
317 size_t Len = 0;
318 for (; Len < MaxLen && S[Len]; Len++) {}
319 return Len;
320}
321
322// Finds min of (strlen(S1), strlen(S2)).
323// Needed bacause one of these strings may actually be non-zero terminated.
324static size_t InternalStrnlen2(const char *S1, const char *S2) {
325 size_t Len = 0;
326 for (; S1[Len] && S2[Len]; Len++) {}
327 return Len;
328}
329
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000330void TracePC::ClearInlineCounters() {
331 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
332 uint8_t *Beg = ModuleCounters[i].Start;
333 size_t Size = ModuleCounters[i].Stop - Beg;
334 memset(Beg, 0, Size);
335 }
336}
337
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000338} // namespace fuzzer
339
Dan Liew59144072016-06-06 20:27:09 +0000340extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000341ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000342ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000343void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000344 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000345 uint32_t Idx = *Guard;
346 __sancov_trace_pc_pcs[Idx] = PC;
347 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000348 // Uncomment the following line to get stack-depth profiling.
349 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000350}
Dan Liew59144072016-06-06 20:27:09 +0000351
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000352// Best-effort support for -fsanitize-coverage=trace-pc, which is available
353// in both Clang and GCC.
354ATTRIBUTE_INTERFACE
355ATTRIBUTE_NO_SANITIZE_ALL
356void __sanitizer_cov_trace_pc() {
357 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
358 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
359 __sancov_trace_pc_pcs[Idx] = PC;
360 __sancov_trace_pc_guard_8bit_counters[Idx]++;
361}
362
Marcos Pividori6137f982017-01-22 01:27:38 +0000363ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000364void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000365 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000366}
Kostya Serebryany09845172016-09-15 22:16:15 +0000367
Marcos Pividori6137f982017-01-22 01:27:38 +0000368ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000369void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
370 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
371}
372
373ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000374void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000375 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000376}
377
378ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000379ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000380void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000381 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000382 fuzzer::TPC.HandleCallerCallee(PC, Callee);
383}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000384
Marcos Pividori6137f982017-01-22 01:27:38 +0000385ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000386ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000387ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000388void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000389 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000390 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000391}
Marcos Pividori6137f982017-01-22 01:27:38 +0000392
393ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000394ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000395ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000396void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000397 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000398 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000399}
Marcos Pividori6137f982017-01-22 01:27:38 +0000400
401ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000402ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000403ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000404void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000405 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000406 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000407}
Marcos Pividori6137f982017-01-22 01:27:38 +0000408
409ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000410ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000411ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000412void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
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, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000415}
416
Marcos Pividori6137f982017-01-22 01:27:38 +0000417ATTRIBUTE_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_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000421 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000422 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000423 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000424 // Skip the most common and the most boring case.
425 if (Vals[N - 1] < 256 && Val < 256)
426 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000427 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000428 size_t i;
429 uint64_t Token = 0;
430 for (i = 0; i < N; i++) {
431 Token = Val ^ Vals[i];
432 if (Val < Vals[i])
433 break;
434 }
435
436 if (ValSizeInBits == 16)
437 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
438 else if (ValSizeInBits == 32)
439 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000440 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000441 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000442}
443
Marcos Pividori6137f982017-01-22 01:27:38 +0000444ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000445ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000446ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000447void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000448 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000449 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000450}
Marcos Pividori6137f982017-01-22 01:27:38 +0000451
452ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000453ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000454ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000455void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000456 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000457 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000458}
Marcos Pividori6137f982017-01-22 01:27:38 +0000459
460ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000461ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000462ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000463void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000464 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000465 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000466}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000467
468ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
469void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
470 const void *s2, size_t n, int result) {
471 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
472 if (result == 0) return; // No reason to mutate.
473 if (n <= 1) return; // Not interesting.
474 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
475}
476
477ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
478void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
479 const char *s2, size_t n, int result) {
480 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
481 if (result == 0) return; // No reason to mutate.
482 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
483 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
484 n = std::min(n, Len1);
485 n = std::min(n, Len2);
486 if (n <= 1) return; // Not interesting.
487 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
488}
489
490ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
491void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
492 const char *s2, int result) {
493 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
494 if (result == 0) return; // No reason to mutate.
495 size_t N = fuzzer::InternalStrnlen2(s1, s2);
496 if (N <= 1) return; // Not interesting.
497 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
498}
499
500ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
501void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
502 const char *s2, size_t n, int result) {
503 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
504 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
505}
506
507ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
508void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
509 const char *s2, int result) {
510 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
511 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
512}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000513
514ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
515void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
516 const char *s2, char *result) {
517 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
518 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
519}
520
521ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
522void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
523 const char *s2, char *result) {
524 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
525 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
526}
527
528ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
529void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
530 const void *s2, size_t len2, void *result) {
531 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
532 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
533}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000534} // extern "C"