blob: ebd33d3ec886fc0db6bc72c3d6e4c625684723a4 [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
Matt Morehouse5c7fc762017-08-18 18:43:30 +000034// Used by -fsanitize-coverage=stack-depth to track stack depth
35ATTRIBUTE_INTERFACE thread_local uintptr_t __sancov_lowest_stack;
36
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000037namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000038
Kostya Serebryanya00b2432016-09-14 02:13:06 +000039TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000040
Kostya Serebryanyf64b8482017-07-14 00:06:27 +000041int ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr;
42
Kostya Serebryany68382d02017-02-02 19:56:01 +000043uint8_t *TracePC::Counters() const {
44 return __sancov_trace_pc_guard_8bit_counters;
45}
46
47uintptr_t *TracePC::PCs() const {
48 return __sancov_trace_pc_pcs;
49}
50
Kostya Serebryany275e2602016-10-25 23:52:25 +000051size_t TracePC::GetTotalPCCoverage() {
Kostya Serebryanye8637962017-08-08 00:17:20 +000052 if (ObservedPCs.size())
53 return ObservedPCs.size();
Kostya Serebryany275e2602016-10-25 23:52:25 +000054 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000055 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000056 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000057 Res++;
58 return Res;
59}
60
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +000061
62void TracePC::HandleInline8bitCountersInit(uint8_t *Start, uint8_t *Stop) {
63 if (Start == Stop) return;
64 if (NumModulesWithInline8bitCounters &&
65 ModuleCounters[NumModulesWithInline8bitCounters-1].Start == Start) return;
66 assert(NumModulesWithInline8bitCounters <
67 sizeof(ModuleCounters) / sizeof(ModuleCounters[0]));
68 ModuleCounters[NumModulesWithInline8bitCounters++] = {Start, Stop};
69 NumInline8bitCounters += Stop - Start;
70}
71
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000072void TracePC::HandlePCsInit(const uint8_t *Start, const uint8_t *Stop) {
73 const uintptr_t *B = reinterpret_cast<const uintptr_t *>(Start);
74 const uintptr_t *E = reinterpret_cast<const uintptr_t *>(Stop);
75 if (NumPCTables && ModulePCTable[NumPCTables - 1].Start == B) return;
76 assert(NumPCTables < sizeof(ModulePCTable) / sizeof(ModulePCTable[0]));
77 ModulePCTable[NumPCTables++] = {B, E};
Kostya Serebryany4f297002017-08-01 00:48:44 +000078 NumPCsInPCTables += E - B;
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +000079}
80
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000081void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000082 if (Start == Stop || *Start) return;
83 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000084 for (uint32_t *P = Start; P < Stop; P++) {
85 NumGuards++;
86 if (NumGuards == kNumPCs) {
87 RawPrint(
88 "WARNING: The binary has too many instrumented PCs.\n"
89 " You may want to reduce the size of the binary\n"
90 " for more efficient fuzzing and precise coverage data\n");
91 }
92 *P = NumGuards % kNumPCs;
93 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000094 Modules[NumModules].Start = Start;
95 Modules[NumModules].Stop = Stop;
96 NumModules++;
97}
98
99void TracePC::PrintModuleInfo() {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000100 if (NumGuards) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000101 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000102 for (size_t i = 0; i < NumModules; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000103 Printf("%zd [%p, %p), ", Modules[i].Stop - Modules[i].Start,
104 Modules[i].Start, Modules[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000105 Printf("\n");
106 }
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000107 if (NumModulesWithInline8bitCounters) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000108 Printf("INFO: Loaded %zd modules (%zd inline 8-bit counters): ",
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000109 NumModulesWithInline8bitCounters, NumInline8bitCounters);
110 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++)
Kostya Serebryany4f297002017-08-01 00:48:44 +0000111 Printf("%zd [%p, %p), ", ModuleCounters[i].Stop - ModuleCounters[i].Start,
112 ModuleCounters[i].Start, ModuleCounters[i].Stop);
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000113 Printf("\n");
114 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000115 if (NumPCTables) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000116 Printf("INFO: Loaded %zd PC tables (%zd PCs): ", NumPCTables,
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000117 NumPCsInPCTables);
118 for (size_t i = 0; i < NumPCTables; i++) {
Kostya Serebryany4f297002017-08-01 00:48:44 +0000119 Printf("%zd [%p,%p), ", ModulePCTable[i].Stop - ModulePCTable[i].Start,
120 ModulePCTable[i].Start, ModulePCTable[i].Stop);
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000121 }
122 Printf("\n");
Kostya Serebryany4f297002017-08-01 00:48:44 +0000123
124 if ((NumGuards && NumGuards != NumPCsInPCTables) ||
125 (NumInline8bitCounters && NumInline8bitCounters != NumPCsInPCTables)) {
126 Printf("ERROR: The size of coverage PC tables does not match the"
127 " number of instrumented PCs. This might be a bug in the compiler,"
128 " please contact the libFuzzer developers.\n");
129 _Exit(1);
130 }
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000131 }
Kostya Serebryany0873be22017-08-11 23:03:22 +0000132 if (size_t NumClangCounters = ClangCountersEnd() - ClangCountersBegin())
133 Printf("INFO: %zd Clang Coverage Counters\n", NumClangCounters);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000134}
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000135
Kostya Serebryany7f058972017-01-27 00:09:59 +0000136ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000137void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
138 const uintptr_t kBits = 12;
139 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +0000140 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +0000141 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +0000142}
143
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000144void TracePC::UpdateObservedPCs() {
Kostya Serebryany0873be22017-08-11 23:03:22 +0000145 auto Observe = [&](uintptr_t PC) {
146 bool Inserted = ObservedPCs.insert(PC).second;
147 if (Inserted && DoPrintNewPCs)
148 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PC + 1);
149 };
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000150 if (NumPCsInPCTables) {
Kostya Serebryanybe7a3572017-08-04 23:13:58 +0000151 if (NumInline8bitCounters == NumPCsInPCTables) {
152 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
153 uint8_t *Beg = ModuleCounters[i].Start;
154 size_t Size = ModuleCounters[i].Stop - Beg;
155 assert(Size ==
156 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
157 for (size_t j = 0; j < Size; j++)
158 if (Beg[j])
159 Observe(ModulePCTable[i].Start[j]);
160 }
161 } else if (NumGuards == NumPCsInPCTables) {
162 size_t GuardIdx = 1;
163 for (size_t i = 0; i < NumModules; i++) {
164 uint32_t *Beg = Modules[i].Start;
165 size_t Size = Modules[i].Stop - Beg;
166 assert(Size ==
167 (size_t)(ModulePCTable[i].Stop - ModulePCTable[i].Start));
168 for (size_t j = 0; j < Size; j++, GuardIdx++)
169 if (Counters()[GuardIdx])
170 Observe(ModulePCTable[i].Start[j]);
171 }
172 }
173 }
Kostya Serebryany0873be22017-08-11 23:03:22 +0000174 if (size_t NumClangCounters =
175 ClangCountersEnd() - ClangCountersBegin()) {
176 auto P = ClangCountersBegin();
177 for (size_t Idx = 0; Idx < NumClangCounters; Idx++)
178 if (P[Idx])
179 Observe((uintptr_t)Idx);
180 }
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000181}
182
Kostya Serebryany854be982017-08-08 00:12:09 +0000183inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
184 // TODO: this implementation is x86 only.
185 // see sanitizer_common GetPreviousInstructionPc for full implementation.
186 return PC - 1;
187}
188
189inline ALWAYS_INLINE uintptr_t GetNextInstructionPc(uintptr_t PC) {
190 // TODO: this implementation is x86 only.
191 // see sanitizer_common GetPreviousInstructionPc for full implementation.
192 return PC + 1;
193}
194
195static std::string GetModuleName(uintptr_t PC) {
196 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
197 void *OffsetRaw = nullptr;
198 if (!EF->__sanitizer_get_module_and_offset_for_pc(
199 reinterpret_cast<void *>(PC), ModulePathRaw,
200 sizeof(ModulePathRaw), &OffsetRaw))
201 return "";
202 return ModulePathRaw;
203}
204
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000205void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000206 if (!EF->__sanitizer_symbolize_pc ||
207 !EF->__sanitizer_get_module_and_offset_for_pc) {
208 Printf("INFO: __sanitizer_symbolize_pc or "
209 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000210 " not printing coverage\n");
211 return;
212 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000213 Printf("COVERAGE:\n");
Kostya Serebryany854be982017-08-08 00:12:09 +0000214 std::string LastFunctionName = "";
215 std::string LastFileStr = "";
216 std::set<size_t> UncoveredLines;
217 std::set<size_t> CoveredLines;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000218
Kostya Serebryany854be982017-08-08 00:12:09 +0000219 auto FunctionEndCallback = [&](const std::string &CurrentFunc,
220 const std::string &CurrentFile) {
221 if (LastFunctionName != CurrentFunc) {
222 if (CoveredLines.empty() && !UncoveredLines.empty()) {
223 Printf("UNCOVERED_FUNC: %s\n", LastFunctionName.c_str());
224 } else {
225 for (auto Line : UncoveredLines) {
226 if (!CoveredLines.count(Line))
227 Printf("UNCOVERED_LINE: %s %s:%zd\n", LastFunctionName.c_str(),
228 LastFileStr.c_str(), Line);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000229 }
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000230 }
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000231
Kostya Serebryany854be982017-08-08 00:12:09 +0000232 UncoveredLines.clear();
233 CoveredLines.clear();
234 LastFunctionName = CurrentFunc;
235 LastFileStr = CurrentFile;
236 }
237 };
238
239 for (size_t i = 0; i < NumPCTables; i++) {
240 auto &M = ModulePCTable[i];
241 assert(M.Start < M.Stop);
242 auto ModuleName = GetModuleName(*M.Start);
243 for (auto Ptr = M.Start; Ptr < M.Stop; Ptr++) {
244 auto PC = *Ptr;
245 auto VisualizePC = GetNextInstructionPc(PC);
Kostya Serebryanye8637962017-08-08 00:17:20 +0000246 bool IsObserved = ObservedPCs.count(PC);
Kostya Serebryany854be982017-08-08 00:12:09 +0000247 std::string FileStr = DescribePC("%s", VisualizePC);
248 if (!IsInterestingCoverageFile(FileStr)) continue;
249 std::string FunctionStr = DescribePC("%F", VisualizePC);
250 FunctionEndCallback(FunctionStr, FileStr);
251 std::string LineStr = DescribePC("%l", VisualizePC);
252 size_t Line = std::stoul(LineStr);
253 if (IsObserved && CoveredLines.insert(Line).second)
254 Printf("COVERED: %s %s:%zd\n", FunctionStr.c_str(), FileStr.c_str(),
255 Line);
256 else
257 UncoveredLines.insert(Line);
258 }
259 }
260 FunctionEndCallback("", "");
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000261}
262
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000263void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000264 if (EF->__sanitizer_dump_coverage) {
265 std::vector<uintptr_t> PCsCopy(GetNumPCs());
266 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000267 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000268 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
269 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000270}
271
Kostya Serebryany379359c2016-10-05 01:09:40 +0000272// Value profile.
273// We keep track of various values that affect control flow.
274// These values are inserted into a bit-set-based hash map.
275// Every new bit in the map is treated as a new coverage.
276//
277// For memcmp/strcmp/etc the interesting value is the length of the common
278// prefix of the parameters.
279// For cmp instructions the interesting value is a XOR of the parameters.
280// The interesting value is mixed up with the PC and is then added to the map.
281
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000282ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000283void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000284 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000285 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000286 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000287 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
288 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000289 uint8_t B1[Word::kMaxSize];
290 uint8_t B2[Word::kMaxSize];
291 // Copy the data into locals in this non-msan-instrumented function
292 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000293 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000294 for (size_t i = 0; i < Len; i++) {
295 B1[i] = A1[i];
296 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000297 size_t T = B1[i];
298 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000299 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000300 size_t I = 0;
301 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000302 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000303 break;
304 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000305 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000306 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000307 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000308}
309
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000310template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000311ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000312ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000313void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000314 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000315 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000316 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000317 if (sizeof(T) == 4)
318 TORC4.Insert(ArgXor, Arg1, Arg2);
319 else if (sizeof(T) == 8)
320 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000321 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000322}
323
Kostya Serebryany697f2152017-07-13 22:30:23 +0000324static size_t InternalStrnlen(const char *S, size_t MaxLen) {
325 size_t Len = 0;
326 for (; Len < MaxLen && S[Len]; Len++) {}
327 return Len;
328}
329
330// Finds min of (strlen(S1), strlen(S2)).
331// Needed bacause one of these strings may actually be non-zero terminated.
332static size_t InternalStrnlen2(const char *S1, const char *S2) {
333 size_t Len = 0;
334 for (; S1[Len] && S2[Len]; Len++) {}
335 return Len;
336}
337
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000338void TracePC::ClearInlineCounters() {
339 for (size_t i = 0; i < NumModulesWithInline8bitCounters; i++) {
340 uint8_t *Beg = ModuleCounters[i].Start;
341 size_t Size = ModuleCounters[i].Stop - Beg;
342 memset(Beg, 0, Size);
343 }
344}
345
Matt Morehouse5c7fc762017-08-18 18:43:30 +0000346void TracePC::RecordInitialStack() {
347 InitialStack = __sancov_lowest_stack;
348}
349
350uintptr_t TracePC::GetMaxStackOffset() const {
351 return InitialStack - __sancov_lowest_stack; // Stack grows down
352}
353
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000354} // namespace fuzzer
355
Dan Liew59144072016-06-06 20:27:09 +0000356extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000357ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000358ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000359void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000360 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000361 uint32_t Idx = *Guard;
362 __sancov_trace_pc_pcs[Idx] = PC;
363 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000364}
Dan Liew59144072016-06-06 20:27:09 +0000365
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000366// Best-effort support for -fsanitize-coverage=trace-pc, which is available
367// in both Clang and GCC.
368ATTRIBUTE_INTERFACE
369ATTRIBUTE_NO_SANITIZE_ALL
370void __sanitizer_cov_trace_pc() {
371 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
372 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
373 __sancov_trace_pc_pcs[Idx] = PC;
374 __sancov_trace_pc_guard_8bit_counters[Idx]++;
375}
376
Marcos Pividori6137f982017-01-22 01:27:38 +0000377ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000378void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000379 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000380}
Kostya Serebryany09845172016-09-15 22:16:15 +0000381
Marcos Pividori6137f982017-01-22 01:27:38 +0000382ATTRIBUTE_INTERFACE
Kostya Serebryanyf2d4dcb2017-06-13 22:31:21 +0000383void __sanitizer_cov_8bit_counters_init(uint8_t *Start, uint8_t *Stop) {
384 fuzzer::TPC.HandleInline8bitCountersInit(Start, Stop);
385}
386
387ATTRIBUTE_INTERFACE
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000388void __sanitizer_cov_pcs_init(const uint8_t *pcs_beg, const uint8_t *pcs_end) {
Kostya Serebryanyb2a1eba2017-07-31 20:20:59 +0000389 fuzzer::TPC.HandlePCsInit(pcs_beg, pcs_end);
Kostya Serebryanyf14996b2017-07-28 22:00:56 +0000390}
391
392ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000393ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000394void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000395 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000396 fuzzer::TPC.HandleCallerCallee(PC, Callee);
397}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000398
Marcos Pividori6137f982017-01-22 01:27:38 +0000399ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000400ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000401ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000402void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000403 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000404 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000405}
Marcos Pividori6137f982017-01-22 01:27:38 +0000406
407ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000408ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000409ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000410// Now the __sanitizer_cov_trace_const_cmp[1248] callbacks just mimic
411// the behaviour of __sanitizer_cov_trace_cmp[1248] ones. This, however,
412// should be changed later to make full use of instrumentation.
413void __sanitizer_cov_trace_const_cmp8(uint64_t Arg1, uint64_t Arg2) {
414 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
415 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
416}
417
418ATTRIBUTE_INTERFACE
419ATTRIBUTE_NO_SANITIZE_ALL
420ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000421void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000422 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000423 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000424}
Marcos Pividori6137f982017-01-22 01:27:38 +0000425
426ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000427ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000428ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000429void __sanitizer_cov_trace_const_cmp4(uint32_t Arg1, uint32_t Arg2) {
430 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
431 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
432}
433
434ATTRIBUTE_INTERFACE
435ATTRIBUTE_NO_SANITIZE_ALL
436ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000437void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000438 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000439 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000440}
Marcos Pividori6137f982017-01-22 01:27:38 +0000441
442ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000443ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000444ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000445void __sanitizer_cov_trace_const_cmp2(uint16_t Arg1, uint16_t Arg2) {
446 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
447 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
448}
449
450ATTRIBUTE_INTERFACE
451ATTRIBUTE_NO_SANITIZE_ALL
452ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000453void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000454 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000455 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000456}
457
Marcos Pividori6137f982017-01-22 01:27:38 +0000458ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000459ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000460ATTRIBUTE_TARGET_POPCNT
Alexander Potapenko7235bcd2017-08-10 14:01:45 +0000461void __sanitizer_cov_trace_const_cmp1(uint8_t Arg1, uint8_t Arg2) {
462 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
463 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
464}
465
466ATTRIBUTE_INTERFACE
467ATTRIBUTE_NO_SANITIZE_ALL
468ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000469void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000470 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000471 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000472 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000473 // Skip the most common and the most boring case.
474 if (Vals[N - 1] < 256 && Val < 256)
475 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000476 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000477 size_t i;
478 uint64_t Token = 0;
479 for (i = 0; i < N; i++) {
480 Token = Val ^ Vals[i];
481 if (Val < Vals[i])
482 break;
483 }
484
485 if (ValSizeInBits == 16)
486 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
487 else if (ValSizeInBits == 32)
488 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000489 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000490 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000491}
492
Marcos Pividori6137f982017-01-22 01:27:38 +0000493ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000494ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000495ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000496void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000497 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000498 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000499}
Marcos Pividori6137f982017-01-22 01:27:38 +0000500
501ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000502ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000503ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000504void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000505 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000506 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000507}
Marcos Pividori6137f982017-01-22 01:27:38 +0000508
509ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000510ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000511ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000512void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000513 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000514 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000515}
Kostya Serebryany697f2152017-07-13 22:30:23 +0000516
517ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
518void __sanitizer_weak_hook_memcmp(void *caller_pc, const void *s1,
519 const void *s2, size_t n, int result) {
520 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
521 if (result == 0) return; // No reason to mutate.
522 if (n <= 1) return; // Not interesting.
523 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/false);
524}
525
526ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
527void __sanitizer_weak_hook_strncmp(void *caller_pc, const char *s1,
528 const char *s2, size_t n, int result) {
529 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
530 if (result == 0) return; // No reason to mutate.
531 size_t Len1 = fuzzer::InternalStrnlen(s1, n);
532 size_t Len2 = fuzzer::InternalStrnlen(s2, n);
533 n = std::min(n, Len1);
534 n = std::min(n, Len2);
535 if (n <= 1) return; // Not interesting.
536 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, n, /*StopAtZero*/true);
537}
538
539ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
540void __sanitizer_weak_hook_strcmp(void *caller_pc, const char *s1,
541 const char *s2, int result) {
542 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
543 if (result == 0) return; // No reason to mutate.
544 size_t N = fuzzer::InternalStrnlen2(s1, s2);
545 if (N <= 1) return; // Not interesting.
546 fuzzer::TPC.AddValueForMemcmp(caller_pc, s1, s2, N, /*StopAtZero*/true);
547}
548
549ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
550void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
551 const char *s2, size_t n, int result) {
552 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
553 return __sanitizer_weak_hook_strncmp(called_pc, s1, s2, n, result);
554}
555
556ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
557void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
558 const char *s2, int result) {
559 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
560 return __sanitizer_weak_hook_strcmp(called_pc, s1, s2, result);
561}
Kostya Serebryanyf64b8482017-07-14 00:06:27 +0000562
563ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
564void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
565 const char *s2, char *result) {
566 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
567 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
568}
569
570ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
571void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
572 const char *s2, char *result) {
573 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
574 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), strlen(s2));
575}
576
577ATTRIBUTE_INTERFACE ATTRIBUTE_NO_SANITIZE_MEMORY
578void __sanitizer_weak_hook_memmem(void *called_pc, const void *s1, size_t len1,
579 const void *s2, size_t len2, void *result) {
580 if (fuzzer::ScopedDoingMyOwnMemOrStr::DoingMyOwnMemOrStr) return;
581 fuzzer::TPC.MMT.Add(reinterpret_cast<const uint8_t *>(s2), len2);
582}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000583} // extern "C"