blob: a54a8c1e99f9ad3b50f0aff62eb76ebe21cddef1 [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 Serebryany0873be22017-08-11 23:03:22 +0000129 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin())
130 Printf("INFO: %zd Clang Coverage Counters\n", NumClangCounters);
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() {
Kostya Serebryany0873be22017-08-11 23:03:22 +0000142 auto Observe = [&](uintptr_t PC) {
143 bool Inserted = ObservedPCs.insert(PC).second;
144 if (Inserted && DoPrintNewPCs)
145 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1);
146 };
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000147 if (NumPCsInPCTables) {
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000148 if (NumInline8bitCounters == NumPCsInPCTables) {
149 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
150 uint8_t *Beg = ModuleCounters[i].Start;
151 size_t Size = ModuleCounters[i].Stop - Beg;
152 assert(Size ==
153 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
154 for (size_t j = 0; j < Size; j++)
155 if (Beg[j])
156 Observe(ModulePCTable[i].Start[j]);
157 }
158 } else if (NumGuards == NumPCsInPCTables) {
159 size_t GuardIdx = 1;
160 for (size_t i = 0; i < NumModules; i++) {
161 uint32_t *Beg = Modules[i].Start;
162 size_t Size = Modules[i].Stop - Beg;
163 assert(Size ==
164 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
165 for (size_t j = 0; j < Size; j++, GuardIdx++)
166 if (Counters()[GuardIdx])
167 Observe(ModulePCTable[i].Start[j]);
168 }
169 }
170 }
Kostya Serebryany0873be22017-08-11 23:03:22 +0000171 if (size_t NumClangCounters =
172 ClangCountersEnd() - ClangCountersBegin()) {
173 auto P = ClangCountersBegin();
174 for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
175 if (P[Idx])
176 Observe((uintptr_t)Idx);
177 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000178}
179
Kostya Serebryany854be982017-08-08 00:12:09 +0000180inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
181 // TODO: this implementation is x86 only.
182 // see sanitizer_common GetPreviousInstructionPc for full implementation.
183 return PC - 1;
184}
185
186inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
187 // TODO: this implementation is x86 only.
188 // see sanitizer_common GetPreviousInstructionPc for full implementation.
189 return PC + 1;
190}
191
192static std::string GetModuleName(uintptr_t PC) {
193 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
194 void *OffsetRaw = nullptr;
195 if (!EF->__sanitizer_get_module_and_offset_for_pc(
196 reinterpret_cast<void *>(PC), ModulePathRaw,
197 sizeof(ModulePathRaw), &OffsetRaw))
198 return "";
199 return ModulePathRaw;
200}
201
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000202void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000203 if (!EF->__sanitizer_symbolize_pc ||
204 !EF->__sanitizer_get_module_and_offset_for_pc) {
205 Printf("INFO: __sanitizer_symbolize_pc or "
206 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000207 " not printing coverage\n");
208 return;
209 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000210 Printf("COVERAGE:\n");
Kostya Serebryany854be982017-08-08 00:12:09 +0000211 std::string LastFunctionName = "";
212 std::string LastFileStr = "";
213 std::set<size_t> UncoveredLines;
214 std::set<size_t> CoveredLines;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000215
Kostya Serebryany854be982017-08-08 00:12:09 +0000216 auto FunctionEndCallback = [&](const std::string &CurrentFunc,
217 const std::string &CurrentFile) {
218 if (LastFunctionName != CurrentFunc) {
219 if (CoveredLines.empty() && !UncoveredLines.empty()) {
220 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str());
221 } else {
222 for (auto Line : UncoveredLines) {
223 if (!CoveredLines.count(Line))
224 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(),
225 LastFileStr.c_str(), Line);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000226 }
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000227 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000228
Kostya Serebryany854be982017-08-08 00:12:09 +0000229 UncoveredLines.clear();
230 CoveredLines.clear();
231 LastFunctionName = CurrentFunc;
232 LastFileStr = CurrentFile;
233 }
234 };
235
236 for (size_t i = 0; i < NumPCTables; i++) {
237 auto &M = ModulePCTable[i];
238 assert(M.Start < M.Stop);
239 auto ModuleName = GetModuleName(*M.Start);
240 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) {
241 auto PC = *Ptr;
242 auto VisualizePC = GetNextInstructionPc(PC);
Kostya Serebryanye8637962017-08-08 00:17:20 +0000243 bool IsObserved = ObservedPCs.count(PC);
Kostya Serebryany854be982017-08-08 00:12:09 +0000244 std::string FileStr = DescribePC("%s", VisualizePC);
245 if (!IsInterestingCoverageFile(FileStr)) continue;
246 std::string FunctionStr = DescribePC("%F", VisualizePC);
247 FunctionEndCallback(FunctionStr, FileStr);
248 std::string LineStr = DescribePC("%l", VisualizePC);
249 size_t Line = std::stoul(LineStr);
250 if (IsObserved && CoveredLines.insert(Line).second)
251 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(),
252 Line);
253 else
254 UncoveredLines.insert(Line);
255 }
256 }
257 FunctionEndCallback("", "");
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000258}
259
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000260void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000261 if (EF->__sanitizer_dump_coverage) {
262 std::vector<uintptr_t> PCsCopy(GetNumPCs());
263 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000264 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000265 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
266 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000267}
268
Kostya Serebryany379359c2016-10-05 01:09:40 +0000269// Value profile.
270// We keep track of various values that affect control flow.
271// These values are inserted into a bit-set-based hash map.
272// Every new bit in the map is treated as a new coverage.
273//
274// For memcmp/strcmp/etc the interesting value is the length of the common
275// prefix of the parameters.
276// For cmp instructions the interesting value is a XOR of the parameters.
277// The interesting value is mixed up with the PC and is then added to the map.
278
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000279ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000280void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000281 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000282 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000283 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000284 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
285 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000286 uint8_t B1[Word::kMaxSize];
287 uint8_t B2[Word::kMaxSize];
288 // Copy the data into locals in this non-msan-instrumented function
289 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000290 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000291 for (size_t i = 0; i < Len; i++) {
292 B1[i] = A1[i];
293 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000294 size_t T = B1[i];
295 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000296 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000297 size_t I = 0;
298 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000299 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000300 break;
301 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000302 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000303 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000304 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000305}
306
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000307template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000308ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000309ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000310void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000311 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000312 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000313 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000314 if (sizeof(T) == 4)
315 TORC4.Insert(ArgXor, Arg1, Arg2);
316 else if (sizeof(T) == 8)
317 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000318 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000319}
320
Kostya Serebryany697f2152017-07-13 22:30:23 +0000321static size_t InternalStrnlen(const char *S, size_t MaxLen) {
322 size_t Len = 0;
323 for (; Len < MaxLen && S[Len]; Len++) {}
324 return Len;
325}
326
327// Finds min of (strlen(S1), strlen(S2)).
328// Needed bacause one of these strings may actually be non-zero terminated.
329static size_t InternalStrnlen2(const char *S1, const char *S2) {
330 size_t Len = 0;
331 for (; S1[Len] && S2[Len]; Len++) {}
332 return Len;
333}
334
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000335void TracePC::ClearInlineCounters() {
336 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
337 uint8_t *Beg = ModuleCounters[i].Start;
338 size_t Size = ModuleCounters[i].Stop - Beg;
339 memset(Beg, 0, Size);
340 }
341}
342
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000343} // namespace fuzzer
344
Dan Liew59144072016-06-06 20:27:09 +0000345extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000346ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000347ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000348void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000349 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000350 uint32_t Idx = *Guard;
351 __sancov_trace_pc_pcs[Idx] = PC;
352 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanye55828c2017-07-20 01:35:17 +0000353 // Uncomment the following line to get stack-depth profiling.
354 // fuzzer::TPC.RecordCurrentStack();
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000355}
Dan Liew59144072016-06-06 20:27:09 +0000356
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000357// Best-effort support for -fsanitize-coverage=trace-pc, which is available
358// in both Clang and GCC.
359ATTRIBUTE_INTERFACE
360ATTRIBUTE_NO_SANITIZE_ALL
361void __sanitizer_cov_trace_pc() {
362 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
363 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
364 __sancov_trace_pc_pcs[Idx] = PC;
365 __sancov_trace_pc_guard_8bit_counters[Idx]++;
366}
367
Marcos Pividori6137f982017-01-22 01:27:38 +0000368ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000369void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000370 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000371}
Kostya Serebryany09845172016-09-15 22:16:15 +0000372
Marcos Pividori6137f982017-01-22 01:27:38 +0000373ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000374void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
375 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
376}
377
378ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000379void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000380 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000381}
382
383ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000384ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000385void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000386 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000387 fuzzer::TPC.HandleCallerCallee(PC, Callee);
388}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000389
Marcos Pividori6137f982017-01-22 01:27:38 +0000390ATTRIBUTE_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_cmp8(uint64_t Arg1, uint64_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
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000401// Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic
402// the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however,
403// should be changed later to make full use of instrumentation.
404void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) {
405 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
406 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
407}
408
409ATTRIBUTE_INTERFACE
410ATTRIBUTE_NO_SANITIZE_ALL
411ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000412void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_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}
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
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000420void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) {
421 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
422 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
423}
424
425ATTRIBUTE_INTERFACE
426ATTRIBUTE_NO_SANITIZE_ALL
427ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000428void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
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, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000431}
Marcos Pividori6137f982017-01-22 01:27:38 +0000432
433ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000434ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000435ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000436void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) {
437 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
438 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
439}
440
441ATTRIBUTE_INTERFACE
442ATTRIBUTE_NO_SANITIZE_ALL
443ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000444void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
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, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000447}
448
Marcos Pividori6137f982017-01-22 01:27:38 +0000449ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000450ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000451ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000452void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) {
453 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
454 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
455}
456
457ATTRIBUTE_INTERFACE
458ATTRIBUTE_NO_SANITIZE_ALL
459ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000460void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000461 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000462 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000463 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000464 // Skip the most common and the most boring case.
465 if (Vals[N - 1] < 256 && Val < 256)
466 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000467 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000468 size_t i;
469 uint64_t Token = 0;
470 for (i = 0; i < N; i++) {
471 Token = Val ^ Vals[i];
472 if (Val < Vals[i])
473 break;
474 }
475
476 if (ValSizeInBits == 16)
477 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
478 else if (ValSizeInBits == 32)
479 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000480 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000481 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000482}
483
Marcos Pividori6137f982017-01-22 01:27:38 +0000484ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000485ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000486ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000487void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000488 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000489 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000490}
Marcos Pividori6137f982017-01-22 01:27:38 +0000491
492ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000493ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000494ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000495void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000496 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000497 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000498}
Marcos Pividori6137f982017-01-22 01:27:38 +0000499
500ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000501ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000502ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000503void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000504 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000505 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000506}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000507
508ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
509void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
510 const void *s2, size_t n, int result) {
511 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
512 if (result == 0) return; // No reason to mutate.
513 if (n <= 1) return; // Not interesting.
514 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
515}
516
517ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
518void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
519 const char *s2, size_t n, int result) {
520 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
521 if (result == 0) return; // No reason to mutate.
522 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
523 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
524 n = std::min(n, Len1);
525 n = std::min(n, Len2);
526 if (n <= 1) return; // Not interesting.
527 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
528}
529
530ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
531void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
532 const char *s2, int result) {
533 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
534 if (result == 0) return; // No reason to mutate.
535 size_t N = fuzzer::InternalStrnlen2(s1, s2);
536 if (N <= 1) return; // Not interesting.
537 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
538}
539
540ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
541void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
542 const char *s2, size_t n, int result) {
543 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
544 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
545}
546
547ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
548void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
549 const char *s2, int result) {
550 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
551 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
552}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000553
554ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
555void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
556 const char *s2, char *result) {
557 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
558 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
559}
560
561ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
562void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
563 const char *s2, char *result) {
564 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
565 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
566}
567
568ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
569void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
570 const void *s2, size_t len2, void *result) {
571 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
572 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
573}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000574} // extern "C"