blob: 661ad23a8e34edfe3591ef265e77b340258d01f6 [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
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000015#include "FuzzerCorpus.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000016#include "FuzzerDefs.h"
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +000017#include "FuzzerDictionary.h"
Kostya Serebryany95b1a432016-10-19 00:12:03 +000018#include "FuzzerExtFunctions.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000019#include "FuzzerIO.h"
Kostya Serebryany6f5a8042016-09-21 01:50:50 +000020#include "FuzzerTracePC.h"
Marcos Pividori62c8fc52017-01-22 01:58:26 +000021#include "FuzzerUtil.h"
Kostya Serebryany86586182016-09-21 21:17:23 +000022#include "FuzzerValueBitMap.h"
Zachary Turner24a148b2016-11-30 19:06:14 +000023#include <map>
24#include <set>
25#include <sstream>
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000026
Kostya Serebryany68382d02017-02-02 19:56:01 +000027// The coverage counters and PCs.
28// These are declared as global variables named "__sancov_*" to simplify
29// experiments with inlined instrumentation.
Mike Aizatsky1b658122017-02-03 20:26:44 +000030alignas(8) ATTRIBUTE_INTERFACE
31uint8_t __sancov_trace_pc_guard_8bit_counters[fuzzer::TracePC::kNumPCs];
32
33ATTRIBUTE_INTERFACE
Kostya Serebryany68382d02017-02-02 19:56:01 +000034uintptr_t __sancov_trace_pc_pcs[fuzzer::TracePC::kNumPCs];
35
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000036namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000037
Kostya Serebryanya00b2432016-09-14 02:13:06 +000038TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000039
Kostya 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 Serebryanyd0ecb4c2017-01-26 01:04:54 +000048ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000049void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
50 uint32_t Idx = *Guard;
Kostya Serebryany68382d02017-02-02 19:56:01 +000051 __sancov_trace_pc_pcs[Idx] = PC;
52 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000053}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000054
Kostya Serebryany275e2602016-10-25 23:52:25 +000055size_t TracePC::GetTotalPCCoverage() {
56 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000057 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000058 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000059 Res++;
60 return Res;
61}
62
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000063void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000064 if (Start == Stop || *Start) return;
65 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000066 for (uint32_t *P = Start; P < Stop; P++) {
67 NumGuards++;
68 if (NumGuards == kNumPCs) {
69 RawPrint(
70 "WARNING: The binary has too many instrumented PCs.\n"
71 " You may want to reduce the size of the binary\n"
72 " for more efficient fuzzing and precise coverage data\n");
73 }
74 *P = NumGuards % kNumPCs;
75 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000076 Modules[NumModules].Start = Start;
77 Modules[NumModules].Stop = Stop;
78 NumModules++;
79}
80
81void TracePC::PrintModuleInfo() {
82 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
83 for (size_t i = 0; i < NumModules; i++)
84 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
85 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000086}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000087
Kostya Serebryany7f058972017-01-27 00:09:59 +000088ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +000089void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
90 const uintptr_t kBits = 12;
91 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000092 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +000093 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +000094}
95
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000096void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +000097 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000098 assert(!PrintedPCs);
99 PrintedPCs = new std::set<uintptr_t>;
100 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000101 if (PCs()[i])
102 PrintedPCs->insert(PCs()[i]);
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000103}
104
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000105void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +0000106 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000107 assert(PrintedPCs);
108 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000109 if (PCs()[i] && PrintedPCs->insert(PCs()[i]).second)
110 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs()[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000111}
112
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000113void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000114 if (!EF->__sanitizer_symbolize_pc ||
115 !EF->__sanitizer_get_module_and_offset_for_pc) {
116 Printf("INFO: __sanitizer_symbolize_pc or "
117 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000118 " not printing coverage\n");
119 return;
120 }
121 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
122 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000123 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
124 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000125 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000126 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000127 uintptr_t PC = PCs()[i];
128 if (!PC) continue;
129 std::string FileStr = DescribePC("%s", PC);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000130 if (!IsInterestingCoverageFile(FileStr)) continue;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000131 std::string FixedPCStr = DescribePC("%p", PC);
132 std::string FunctionStr = DescribePC("%F", PC);
133 std::string LineStr = DescribePC("%l", PC);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000134 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
135 void *OffsetRaw = nullptr;
136 if (!EF->__sanitizer_get_module_and_offset_for_pc(
Kostya Serebryany68382d02017-02-02 19:56:01 +0000137 reinterpret_cast<void *>(PC), ModulePathRaw,
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000138 sizeof(ModulePathRaw), &OffsetRaw))
139 continue;
140 std::string Module = ModulePathRaw;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000141 uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000142 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000143 ModuleOffsets[Module] = FixedPC - PcOffset;
144 CoveredPCsPerModule[Module].push_back(PcOffset);
145 CoveredFunctions.insert(FunctionStr);
146 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000147 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000148 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
149 continue;
150 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
151 FileStr.c_str(), LineStr.c_str());
152 }
153
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000154 std::string CoveredDirsStr;
155 for (auto &Dir : CoveredDirs) {
156 if (!CoveredDirsStr.empty())
157 CoveredDirsStr += ",";
158 CoveredDirsStr += Dir;
159 }
160 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
161
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000162 for (auto &M : CoveredPCsPerModule) {
163 std::set<std::string> UncoveredFiles, UncoveredFunctions;
164 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
165 auto &ModuleName = M.first;
166 auto &CoveredOffsets = M.second;
167 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
168 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
169 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
170 // sancov does not yet fully support DSOs.
171 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000172 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
173 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000174 std::string SanCovOutput;
175 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
176 Printf("INFO: Command failed: %s\n", Cmd.c_str());
177 continue;
178 }
179 std::istringstream ISS(SanCovOutput);
180 std::string S;
181 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000182 size_t PcOffsetEnd = S.find(':');
183 if (PcOffsetEnd == std::string::npos)
184 continue;
185 S.resize(PcOffsetEnd);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000186 uintptr_t PcOffset = std::stol(S, 0, 16);
187 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
188 PcOffset)) {
189 uintptr_t PC = ModuleOffset + PcOffset;
190 auto FileStr = DescribePC("%s", PC);
191 if (!IsInterestingCoverageFile(FileStr)) continue;
192 if (CoveredFiles.count(FileStr) == 0) {
193 UncoveredFiles.insert(FileStr);
194 continue;
195 }
196 auto FunctionStr = DescribePC("%F", PC);
197 if (CoveredFunctions.count(FunctionStr) == 0) {
198 UncoveredFunctions.insert(FunctionStr);
199 continue;
200 }
201 std::string LineStr = DescribePC("%l", PC);
202 uintptr_t Line = std::stoi(LineStr);
203 std::string FileLineStr = FileStr + ":" + LineStr;
204 if (CoveredLines.count(FileLineStr) == 0)
205 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
206 }
207 }
208 for (auto &FileLine: UncoveredLines)
209 for (int Line : FileLine.second)
210 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
211 for (auto &Func : UncoveredFunctions)
212 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
213 for (auto &File : UncoveredFiles)
214 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000215 }
216}
217
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000218inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
219 // TODO: this implementation is x86 only.
220 // see sanitizer_common GetPreviousInstructionPc for full implementation.
221 return PC - 1;
222}
223
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000224void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000225 if (EF->__sanitizer_dump_coverage) {
226 std::vector<uintptr_t> PCsCopy(GetNumPCs());
227 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000228 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000229 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
230 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000231}
232
Kostya Serebryany379359c2016-10-05 01:09:40 +0000233// Value profile.
234// We keep track of various values that affect control flow.
235// These values are inserted into a bit-set-based hash map.
236// Every new bit in the map is treated as a new coverage.
237//
238// For memcmp/strcmp/etc the interesting value is the length of the common
239// prefix of the parameters.
240// For cmp instructions the interesting value is a XOR of the parameters.
241// The interesting value is mixed up with the PC and is then added to the map.
242
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000243ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000244void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000245 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000246 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000247 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000248 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
249 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000250 uint8_t B1[Word::kMaxSize];
251 uint8_t B2[Word::kMaxSize];
252 // Copy the data into locals in this non-msan-instrumented function
253 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000254 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000255 for (size_t i = 0; i < Len; i++) {
256 B1[i] = A1[i];
257 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000258 size_t T = B1[i];
259 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000260 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000261 size_t I = 0;
262 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000263 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000264 break;
265 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000266 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000267 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000268 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000269}
270
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000271template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000272ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000273ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000274void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000275 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000276 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000277 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000278 if (sizeof(T) == 4)
279 TORC4.Insert(ArgXor, Arg1, Arg2);
280 else if (sizeof(T) == 8)
281 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000282 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000283}
284
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000285} // namespace fuzzer
286
Dan Liew59144072016-06-06 20:27:09 +0000287extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000288ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000289ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000290void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000291 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000292 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000293}
Dan Liew59144072016-06-06 20:27:09 +0000294
Marcos Pividori6137f982017-01-22 01:27:38 +0000295ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000296void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000297 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000298}
Kostya Serebryany09845172016-09-15 22:16:15 +0000299
Marcos Pividori6137f982017-01-22 01:27:38 +0000300ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000301ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000302void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000303 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000304 fuzzer::TPC.HandleCallerCallee(PC, Callee);
305}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000306
Marcos Pividori6137f982017-01-22 01:27:38 +0000307ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000308ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000309ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000310void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000311 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000312 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000313}
Marcos Pividori6137f982017-01-22 01:27:38 +0000314
315ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000316ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000317ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000318void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000319 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000320 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000321}
Marcos Pividori6137f982017-01-22 01:27:38 +0000322
323ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000324ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000325ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000326void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000327 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000328 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000329}
Marcos Pividori6137f982017-01-22 01:27:38 +0000330
331ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000332ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000333ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000334void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000335 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000336 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000337}
338
Marcos Pividori6137f982017-01-22 01:27:38 +0000339ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000340ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000341ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000342void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000343 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000344 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000345 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000346 // Skip the most common and the most boring case.
347 if (Vals[N - 1] < 256 && Val < 256)
348 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000349 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000350 size_t i;
351 uint64_t Token = 0;
352 for (i = 0; i < N; i++) {
353 Token = Val ^ Vals[i];
354 if (Val < Vals[i])
355 break;
356 }
357
358 if (ValSizeInBits == 16)
359 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
360 else if (ValSizeInBits == 32)
361 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000362 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000363 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000364}
365
Marcos Pividori6137f982017-01-22 01:27:38 +0000366ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000367ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000368ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000369void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000370 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000371 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000372}
Marcos Pividori6137f982017-01-22 01:27:38 +0000373
374ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000375ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000376ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000377void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000378 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000379 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000380}
Marcos Pividori6137f982017-01-22 01:27:38 +0000381
382ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000383ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000384ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000385void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000386 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000387 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000388}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000389} // extern "C"