blob: 6c1d1ffaf7d258c1910f21fd7a7211e576c63fa0 [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
27namespace fuzzer {
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000028
Kostya Serebryanya00b2432016-09-14 02:13:06 +000029TracePC TPC;
Mike Aizatsky1aa501e2016-05-10 23:43:15 +000030
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000031ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000032void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) {
33 uint32_t Idx = *Guard;
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000034 PCs[Idx] = PC;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000035 Counters[Idx]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000036}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000037
Kostya Serebryany275e2602016-10-25 23:52:25 +000038size_t TracePC::GetTotalPCCoverage() {
39 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000040 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany275e2602016-10-25 23:52:25 +000041 if (PCs[i])
42 Res++;
43 return Res;
44}
45
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000046void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000047 if (Start == Stop || *Start) return;
48 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000049 for (uint32_t *P = Start; P < Stop; P++) {
50 NumGuards++;
51 if (NumGuards == kNumPCs) {
52 RawPrint(
53 "WARNING: The binary has too many instrumented PCs.\n"
54 " You may want to reduce the size of the binary\n"
55 " for more efficient fuzzing and precise coverage data\n");
56 }
57 *P = NumGuards % kNumPCs;
58 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000059 Modules[NumModules].Start = Start;
60 Modules[NumModules].Stop = Stop;
61 NumModules++;
62}
63
64void TracePC::PrintModuleInfo() {
65 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
66 for (size_t i = 0; i < NumModules; i++)
67 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
68 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000069}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000070
Kostya Serebryany7f058972017-01-27 00:09:59 +000071ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +000072void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
73 const uintptr_t kBits = 12;
74 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000075 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany7f058972017-01-27 00:09:59 +000076 ValueProfileMap.AddValue(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +000077}
78
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000079void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +000080 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000081 assert(!PrintedPCs);
82 PrintedPCs = new std::set<uintptr_t>;
83 for (size_t i = 1; i < GetNumPCs(); i++)
84 if (PCs[i])
85 PrintedPCs->insert(PCs[i]);
86}
87
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000088void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +000089 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000090 assert(PrintedPCs);
91 for (size_t i = 1; i < GetNumPCs(); i++)
92 if (PCs[i] && PrintedPCs->insert(PCs[i]).second)
93 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000094}
95
Kostya Serebryanyb706b482016-09-18 21:47:08 +000096void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +000097 if (!EF->__sanitizer_symbolize_pc ||
98 !EF->__sanitizer_get_module_and_offset_for_pc) {
99 Printf("INFO: __sanitizer_symbolize_pc or "
100 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000101 " not printing coverage\n");
102 return;
103 }
104 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
105 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000106 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
107 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000108 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000109 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000110 if (!PCs[i]) continue;
111 std::string FileStr = DescribePC("%s", PCs[i]);
112 if (!IsInterestingCoverageFile(FileStr)) continue;
113 std::string FixedPCStr = DescribePC("%p", PCs[i]);
114 std::string FunctionStr = DescribePC("%F", PCs[i]);
115 std::string LineStr = DescribePC("%l", PCs[i]);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000116 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
117 void *OffsetRaw = nullptr;
118 if (!EF->__sanitizer_get_module_and_offset_for_pc(
119 reinterpret_cast<void *>(PCs[i]), ModulePathRaw,
120 sizeof(ModulePathRaw), &OffsetRaw))
121 continue;
122 std::string Module = ModulePathRaw;
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000123 uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000124 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000125 ModuleOffsets[Module] = FixedPC - PcOffset;
126 CoveredPCsPerModule[Module].push_back(PcOffset);
127 CoveredFunctions.insert(FunctionStr);
128 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000129 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000130 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
131 continue;
132 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
133 FileStr.c_str(), LineStr.c_str());
134 }
135
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000136 std::string CoveredDirsStr;
137 for (auto &Dir : CoveredDirs) {
138 if (!CoveredDirsStr.empty())
139 CoveredDirsStr += ",";
140 CoveredDirsStr += Dir;
141 }
142 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
143
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000144 for (auto &M : CoveredPCsPerModule) {
145 std::set<std::string> UncoveredFiles, UncoveredFunctions;
146 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
147 auto &ModuleName = M.first;
148 auto &CoveredOffsets = M.second;
149 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
150 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
151 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
152 // sancov does not yet fully support DSOs.
153 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000154 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
155 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000156 std::string SanCovOutput;
157 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
158 Printf("INFO: Command failed: %s\n", Cmd.c_str());
159 continue;
160 }
161 std::istringstream ISS(SanCovOutput);
162 std::string S;
163 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000164 size_t PcOffsetEnd = S.find(':');
165 if (PcOffsetEnd == std::string::npos)
166 continue;
167 S.resize(PcOffsetEnd);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000168 uintptr_t PcOffset = std::stol(S, 0, 16);
169 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
170 PcOffset)) {
171 uintptr_t PC = ModuleOffset + PcOffset;
172 auto FileStr = DescribePC("%s", PC);
173 if (!IsInterestingCoverageFile(FileStr)) continue;
174 if (CoveredFiles.count(FileStr) == 0) {
175 UncoveredFiles.insert(FileStr);
176 continue;
177 }
178 auto FunctionStr = DescribePC("%F", PC);
179 if (CoveredFunctions.count(FunctionStr) == 0) {
180 UncoveredFunctions.insert(FunctionStr);
181 continue;
182 }
183 std::string LineStr = DescribePC("%l", PC);
184 uintptr_t Line = std::stoi(LineStr);
185 std::string FileLineStr = FileStr + ":" + LineStr;
186 if (CoveredLines.count(FileLineStr) == 0)
187 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
188 }
189 }
190 for (auto &FileLine: UncoveredLines)
191 for (int Line : FileLine.second)
192 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
193 for (auto &Func : UncoveredFunctions)
194 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
195 for (auto &File : UncoveredFiles)
196 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000197 }
198}
199
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000200inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
201 // TODO: this implementation is x86 only.
202 // see sanitizer_common GetPreviousInstructionPc for full implementation.
203 return PC - 1;
204}
205
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000206void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000207 if (EF->__sanitizer_dump_coverage) {
208 std::vector<uintptr_t> PCsCopy(GetNumPCs());
209 for (size_t i = 0; i < GetNumPCs(); i++)
210 PCsCopy[i] = PCs[i] ? GetPreviousInstructionPc(PCs[i]) : 0;
211 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
212 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000213}
214
Kostya Serebryany379359c2016-10-05 01:09:40 +0000215// Value profile.
216// We keep track of various values that affect control flow.
217// These values are inserted into a bit-set-based hash map.
218// Every new bit in the map is treated as a new coverage.
219//
220// For memcmp/strcmp/etc the interesting value is the length of the common
221// prefix of the parameters.
222// For cmp instructions the interesting value is a XOR of the parameters.
223// The interesting value is mixed up with the PC and is then added to the map.
224
Kostya Serebryany7f058972017-01-27 00:09:59 +0000225ATTRIBUTE_NO_SANITIZE_ADDRESS
Kostya Serebryany379359c2016-10-05 01:09:40 +0000226void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000227 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000228 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000229 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000230 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
231 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000232 uint8_t B1[Word::kMaxSize];
233 uint8_t B2[Word::kMaxSize];
234 // Copy the data into locals in this non-msan-instrumented function
235 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000236 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000237 for (size_t i = 0; i < Len; i++) {
238 B1[i] = A1[i];
239 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000240 size_t T = B1[i];
241 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000242 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000243 size_t I = 0;
244 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000245 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000246 break;
247 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000248 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000249 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000250 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000251}
252
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000253template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000254ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000255ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000256void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000257 uint64_t ArgXor = Arg1 ^ Arg2;
258 uint64_t ArgDistance = __builtin_popcountl(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000259 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000260 if (sizeof(T) == 4)
261 TORC4.Insert(ArgXor, Arg1, Arg2);
262 else if (sizeof(T) == 8)
263 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000264 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000265}
266
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000267} // namespace fuzzer
268
Dan Liew59144072016-06-06 20:27:09 +0000269extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000270ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000271ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000272void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000273 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000274 fuzzer::TPC.HandleTrace(Guard, PC);
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000275}
Dan Liew59144072016-06-06 20:27:09 +0000276
Marcos Pividori6137f982017-01-22 01:27:38 +0000277ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000278void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000279 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000280}
Kostya Serebryany09845172016-09-15 22:16:15 +0000281
Marcos Pividori6137f982017-01-22 01:27:38 +0000282ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000283ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000284void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000285 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000286 fuzzer::TPC.HandleCallerCallee(PC, Callee);
287}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000288
Marcos Pividori6137f982017-01-22 01:27:38 +0000289ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000290ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000291ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000292void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000293 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000294 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000295}
Marcos Pividori6137f982017-01-22 01:27:38 +0000296
297ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000298ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000299ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000300void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000301 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000302 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000303}
Marcos Pividori6137f982017-01-22 01:27:38 +0000304
305ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000306ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000307ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000308void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000309 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000310 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000311}
Marcos Pividori6137f982017-01-22 01:27:38 +0000312
313ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000314ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000315ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000316void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000317 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000318 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000319}
320
Marcos Pividori6137f982017-01-22 01:27:38 +0000321ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000322ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000323ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000324void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000325 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000326 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000327 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000328 // Skip the most common and the most boring case.
329 if (Vals[N - 1] < 256 && Val < 256)
330 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000331 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000332 size_t i;
333 uint64_t Token = 0;
334 for (i = 0; i < N; i++) {
335 Token = Val ^ Vals[i];
336 if (Val < Vals[i])
337 break;
338 }
339
340 if (ValSizeInBits == 16)
341 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
342 else if (ValSizeInBits == 32)
343 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000344 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000345 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000346}
347
Marcos Pividori6137f982017-01-22 01:27:38 +0000348ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000349ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000350ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000351void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000352 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000353 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000354}
Marcos Pividori6137f982017-01-22 01:27:38 +0000355
356ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000357ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000358ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000359void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000360 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000361 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000362}
Marcos Pividori6137f982017-01-22 01:27:38 +0000363
364ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000365ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000366ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000367void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000368 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000369 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000370}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000371} // extern "C"