blob: ce0f7a47eee64bfdc6a0b6dee2aa741455e16667 [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.
Kostya Serebryany6ca44f92017-03-23 22:43:12 +000030alignas(64) ATTRIBUTE_INTERFACE
Mike Aizatsky1b658122017-02-03 20:26:44 +000031uint8_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 Serebryany275e2602016-10-25 23:52:25 +000048size_t TracePC::GetTotalPCCoverage() {
49 size_t Res = 0;
Kostya Serebryany7856fb32017-01-26 01:34:58 +000050 for (size_t i = 1, N = GetNumPCs(); i < N; i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000051 if (PCs()[i])
Kostya Serebryany275e2602016-10-25 23:52:25 +000052 Res++;
53 return Res;
54}
55
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +000056void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000057 if (Start == Stop || *Start) return;
58 assert(NumModules < sizeof(Modules) / sizeof(Modules[0]));
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +000059 for (uint32_t *P = Start; P < Stop; P++) {
60 NumGuards++;
61 if (NumGuards == kNumPCs) {
62 RawPrint(
63 "WARNING: The binary has too many instrumented PCs.\n"
64 " You may want to reduce the size of the binary\n"
65 " for more efficient fuzzing and precise coverage data\n");
66 }
67 *P = NumGuards % kNumPCs;
68 }
Kostya Serebryany3e36ec12016-09-17 05:04:47 +000069 Modules[NumModules].Start = Start;
70 Modules[NumModules].Stop = Stop;
71 NumModules++;
72}
73
74void TracePC::PrintModuleInfo() {
75 Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards);
76 for (size_t i = 0; i < NumModules; i++)
77 Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop);
78 Printf("\n");
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +000079}
Kostya Serebryanya5277d52016-09-15 01:30:18 +000080
Kostya Serebryany7f058972017-01-27 00:09:59 +000081ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +000082void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) {
83 const uintptr_t kBits = 12;
84 const uintptr_t kMask = (1 << kBits) - 1;
Kostya Serebryany1c73f1b2016-10-05 22:56:21 +000085 uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits);
Kostya Serebryany70182de2017-01-27 00:39:12 +000086 ValueProfileMap.AddValueModPrime(Idx);
Kostya Serebryany09845172016-09-15 22:16:15 +000087}
88
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000089void TracePC::InitializePrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +000090 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000091 assert(!PrintedPCs);
92 PrintedPCs = new std::set<uintptr_t>;
93 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +000094 if (PCs()[i])
95 PrintedPCs->insert(PCs()[i]);
Kostya Serebryany11a22bc2016-12-30 01:13:07 +000096}
97
Kostya Serebryanya5b2e542016-10-26 00:20:51 +000098void TracePC::PrintNewPCs() {
Kostya Serebryany4986e812017-01-03 18:51:28 +000099 if (!DoPrintNewPCs) return;
Kostya Serebryany11a22bc2016-12-30 01:13:07 +0000100 assert(PrintedPCs);
101 for (size_t i = 1; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000102 if (PCs()[i] && PrintedPCs->insert(PCs()[i]).second)
103 PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs()[i]);
Kostya Serebryanya5b2e542016-10-26 00:20:51 +0000104}
105
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000106void TracePC::PrintCoverage() {
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000107 if (!EF->__sanitizer_symbolize_pc ||
108 !EF->__sanitizer_get_module_and_offset_for_pc) {
109 Printf("INFO: __sanitizer_symbolize_pc or "
110 "__sanitizer_get_module_and_offset_for_pc is not available,"
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000111 " not printing coverage\n");
112 return;
113 }
114 std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule;
115 std::map<std::string, uintptr_t> ModuleOffsets;
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000116 std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions,
117 CoveredLines;
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000118 Printf("COVERAGE:\n");
Kostya Serebryany06b87572016-10-26 00:42:52 +0000119 for (size_t i = 1; i < GetNumPCs(); i++) {
Kostya Serebryany68382d02017-02-02 19:56:01 +0000120 uintptr_t PC = PCs()[i];
121 if (!PC) continue;
122 std::string FileStr = DescribePC("%s", PC);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000123 if (!IsInterestingCoverageFile(FileStr)) continue;
Kostya Serebryany68382d02017-02-02 19:56:01 +0000124 std::string FixedPCStr = DescribePC("%p", PC);
125 std::string FunctionStr = DescribePC("%F", PC);
126 std::string LineStr = DescribePC("%l", PC);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000127 char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++?
128 void *OffsetRaw = nullptr;
129 if (!EF->__sanitizer_get_module_and_offset_for_pc(
Kostya Serebryany68382d02017-02-02 19:56:01 +0000130 reinterpret_cast<void *>(PC), ModulePathRaw,
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000131 sizeof(ModulePathRaw), &OffsetRaw))
132 continue;
133 std::string Module = ModulePathRaw;
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000134 uintptr_t FixedPC = std::stoull(FixedPCStr, 0, 16);
Kostya Serebryany1394ce22016-12-10 01:19:35 +0000135 uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000136 ModuleOffsets[Module] = FixedPC - PcOffset;
137 CoveredPCsPerModule[Module].push_back(PcOffset);
138 CoveredFunctions.insert(FunctionStr);
139 CoveredFiles.insert(FileStr);
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000140 CoveredDirs.insert(DirName(FileStr));
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000141 if (!CoveredLines.insert(FileStr + ":" + LineStr).second)
142 continue;
143 Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(),
144 FileStr.c_str(), LineStr.c_str());
145 }
146
Kostya Serebryany1cba0a92016-11-30 21:53:32 +0000147 std::string CoveredDirsStr;
148 for (auto &Dir : CoveredDirs) {
149 if (!CoveredDirsStr.empty())
150 CoveredDirsStr += ",";
151 CoveredDirsStr += Dir;
152 }
153 Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str());
154
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000155 for (auto &M : CoveredPCsPerModule) {
156 std::set<std::string> UncoveredFiles, UncoveredFunctions;
157 std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines
158 auto &ModuleName = M.first;
159 auto &CoveredOffsets = M.second;
160 uintptr_t ModuleOffset = ModuleOffsets[ModuleName];
161 std::sort(CoveredOffsets.begin(), CoveredOffsets.end());
162 Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str());
163 // sancov does not yet fully support DSOs.
164 // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName;
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000165 std::string Cmd = DisassembleCmd(ModuleName) + " | " +
166 SearchRegexCmd("call.*__sanitizer_cov_trace_pc_guard");
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000167 std::string SanCovOutput;
168 if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) {
169 Printf("INFO: Command failed: %s\n", Cmd.c_str());
170 continue;
171 }
172 std::istringstream ISS(SanCovOutput);
173 std::string S;
174 while (std::getline(ISS, S, '\n')) {
Marcos Pividori62c8fc52017-01-22 01:58:26 +0000175 size_t PcOffsetEnd = S.find(':');
176 if (PcOffsetEnd == std::string::npos)
177 continue;
178 S.resize(PcOffsetEnd);
Marcos Pividorie81f9cc2017-02-10 18:44:14 +0000179 uintptr_t PcOffset = std::stoull(S, 0, 16);
Kostya Serebryany95b1a432016-10-19 00:12:03 +0000180 if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(),
181 PcOffset)) {
182 uintptr_t PC = ModuleOffset + PcOffset;
183 auto FileStr = DescribePC("%s", PC);
184 if (!IsInterestingCoverageFile(FileStr)) continue;
185 if (CoveredFiles.count(FileStr) == 0) {
186 UncoveredFiles.insert(FileStr);
187 continue;
188 }
189 auto FunctionStr = DescribePC("%F", PC);
190 if (CoveredFunctions.count(FunctionStr) == 0) {
191 UncoveredFunctions.insert(FunctionStr);
192 continue;
193 }
194 std::string LineStr = DescribePC("%l", PC);
195 uintptr_t Line = std::stoi(LineStr);
196 std::string FileLineStr = FileStr + ":" + LineStr;
197 if (CoveredLines.count(FileLineStr) == 0)
198 UncoveredLines[FunctionStr + " " + FileStr].insert(Line);
199 }
200 }
201 for (auto &FileLine: UncoveredLines)
202 for (int Line : FileLine.second)
203 Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line);
204 for (auto &Func : UncoveredFunctions)
205 Printf("UNCOVERED_FUNC: %s\n", Func.c_str());
206 for (auto &File : UncoveredFiles)
207 Printf("UNCOVERED_FILE: %s\n", File.c_str());
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000208 }
209}
210
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000211inline ALWAYS_INLINE uintptr_t GetPreviousInstructionPc(uintptr_t PC) {
212 // TODO: this implementation is x86 only.
213 // see sanitizer_common GetPreviousInstructionPc for full implementation.
214 return PC - 1;
215}
216
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000217void TracePC::DumpCoverage() {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000218 if (EF->__sanitizer_dump_coverage) {
219 std::vector<uintptr_t> PCsCopy(GetNumPCs());
220 for (size_t i = 0; i < GetNumPCs(); i++)
Kostya Serebryany68382d02017-02-02 19:56:01 +0000221 PCsCopy[i] = PCs()[i] ? GetPreviousInstructionPc(PCs()[i]) : 0;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000222 EF->__sanitizer_dump_coverage(PCsCopy.data(), PCsCopy.size());
223 }
Mike Aizatsky9b415be2016-12-19 22:18:08 +0000224}
225
Kostya Serebryany379359c2016-10-05 01:09:40 +0000226// Value profile.
227// We keep track of various values that affect control flow.
228// These values are inserted into a bit-set-based hash map.
229// Every new bit in the map is treated as a new coverage.
230//
231// For memcmp/strcmp/etc the interesting value is the length of the common
232// prefix of the parameters.
233// For cmp instructions the interesting value is a XOR of the parameters.
234// The interesting value is mixed up with the PC and is then added to the map.
235
Kostya Serebryany9f8e47b2017-02-03 22:51:38 +0000236ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany379359c2016-10-05 01:09:40 +0000237void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2,
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000238 size_t n, bool StopAtZero) {
Kostya Serebryany379359c2016-10-05 01:09:40 +0000239 if (!n) return;
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000240 size_t Len = std::min(n, Word::GetMaxSize());
Kostya Serebryany379359c2016-10-05 01:09:40 +0000241 const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1);
242 const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000243 uint8_t B1[Word::kMaxSize];
244 uint8_t B2[Word::kMaxSize];
245 // Copy the data into locals in this non-msan-instrumented function
246 // to avoid msan complaining further.
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000247 size_t Hash = 0; // Compute some simple hash of both strings.
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000248 for (size_t i = 0; i < Len; i++) {
249 B1[i] = A1[i];
250 B2[i] = A2[i];
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000251 size_t T = B1[i];
252 Hash ^= (T << 8) | B2[i];
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000253 }
Kostya Serebryany379359c2016-10-05 01:09:40 +0000254 size_t I = 0;
255 for (; I < Len; I++)
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000256 if (B1[I] != B2[I] || (StopAtZero && B1[I] == 0))
Kostya Serebryany379359c2016-10-05 01:09:40 +0000257 break;
258 size_t PC = reinterpret_cast<size_t>(caller_pc);
Kostya Serebryany1d8c2ce2017-01-17 23:09:05 +0000259 size_t Idx = (PC & 4095) | (I << 12);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000260 ValueProfileMap.AddValue(Idx);
Kostya Serebryany6bdd8fc2017-01-23 22:11:04 +0000261 TORCW.Insert(Idx ^ Hash, Word(B1, Len), Word(B2, Len));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000262}
263
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000264template <class T>
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000265ATTRIBUTE_TARGET_POPCNT ALWAYS_INLINE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000266ATTRIBUTE_NO_SANITIZE_ALL
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000267void TracePC::HandleCmp(uintptr_t PC, T Arg1, T Arg2) {
Kostya Serebryanya5f94fb2016-10-14 20:20:33 +0000268 uint64_t ArgXor = Arg1 ^ Arg2;
Marcos Pividori5a535672017-02-08 00:03:31 +0000269 uint64_t ArgDistance = __builtin_popcountll(ArgXor) + 1; // [1,65]
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000270 uintptr_t Idx = ((PC & 4095) + 1) * ArgDistance;
Kostya Serebryany94c427c2016-10-27 00:36:38 +0000271 if (sizeof(T) == 4)
272 TORC4.Insert(ArgXor, Arg1, Arg2);
273 else if (sizeof(T) == 8)
274 TORC8.Insert(ArgXor, Arg1, Arg2);
Kostya Serebryany7f058972017-01-27 00:09:59 +0000275 ValueProfileMap.AddValue(Idx);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000276}
277
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000278} // namespace fuzzer
279
Dan Liew59144072016-06-06 20:27:09 +0000280extern "C" {
Marcos Pividori6137f982017-01-22 01:27:38 +0000281ATTRIBUTE_INTERFACE
Kostya Serebryanyd0ecb4c2017-01-26 01:04:54 +0000282ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000283void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000284 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany7acabdc2017-03-17 01:45:15 +0000285 uint32_t Idx = *Guard;
286 __sancov_trace_pc_pcs[Idx] = PC;
287 __sancov_trace_pc_guard_8bit_counters[Idx]++;
Kostya Serebryanyda63c1d2016-02-26 21:33:56 +0000288}
Dan Liew59144072016-06-06 20:27:09 +0000289
Kostya Serebryanyd7d1d512017-03-30 01:27:20 +0000290// Best-effort support for -fsanitize-coverage=trace-pc, which is available
291// in both Clang and GCC.
292ATTRIBUTE_INTERFACE
293ATTRIBUTE_NO_SANITIZE_ALL
294void __sanitizer_cov_trace_pc() {
295 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
296 uintptr_t Idx = PC & (((uintptr_t)1 << fuzzer::TracePC::kTracePcBits) - 1);
297 __sancov_trace_pc_pcs[Idx] = PC;
298 __sancov_trace_pc_guard_8bit_counters[Idx]++;
299}
300
Marcos Pividori6137f982017-01-22 01:27:38 +0000301ATTRIBUTE_INTERFACE
Kostya Serebryanya9b0dd02016-09-29 17:43:24 +0000302void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) {
Kostya Serebryanya5277d52016-09-15 01:30:18 +0000303 fuzzer::TPC.HandleInit(Start, Stop);
Dan Liew59144072016-06-06 20:27:09 +0000304}
Kostya Serebryany09845172016-09-15 22:16:15 +0000305
Marcos Pividori6137f982017-01-22 01:27:38 +0000306ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000307ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany09845172016-09-15 22:16:15 +0000308void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000309 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryany09845172016-09-15 22:16:15 +0000310 fuzzer::TPC.HandleCallerCallee(PC, Callee);
311}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000312
Marcos Pividori6137f982017-01-22 01:27:38 +0000313ATTRIBUTE_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_cmp8(uint64_t Arg1, uint64_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}
Marcos Pividori6137f982017-01-22 01:27:38 +0000320
321ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000322ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000323ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000324void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000325 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000326 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000327}
Marcos Pividori6137f982017-01-22 01:27:38 +0000328
329ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000330ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000331ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000332void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000333 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000334 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000335}
Marcos Pividori6137f982017-01-22 01:27:38 +0000336
337ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000338ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000339ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany17d176e12016-10-13 16:19:09 +0000340void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000341 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000342 fuzzer::TPC.HandleCmp(PC, Arg1, Arg2);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000343}
344
Marcos Pividori6137f982017-01-22 01:27:38 +0000345ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000346ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000347ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000348void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) {
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000349 uint64_t N = Cases[0];
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000350 uint64_t ValSizeInBits = Cases[1];
Kostya Serebryanyd19919a2016-10-11 01:14:41 +0000351 uint64_t *Vals = Cases + 2;
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000352 // Skip the most common and the most boring case.
353 if (Vals[N - 1] < 256 && Val < 256)
354 return;
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000355 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000356 size_t i;
357 uint64_t Token = 0;
358 for (i = 0; i < N; i++) {
359 Token = Val ^ Vals[i];
360 if (Val < Vals[i])
361 break;
362 }
363
364 if (ValSizeInBits == 16)
365 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint16_t>(Token), (uint16_t)(0));
366 else if (ValSizeInBits == 32)
367 fuzzer::TPC.HandleCmp(PC + i, static_cast<uint32_t>(Token), (uint32_t)(0));
Kostya Serebryany00e638e2016-12-17 02:03:34 +0000368 else
Kostya Serebryanyd7238042016-12-29 02:50:35 +0000369 fuzzer::TPC.HandleCmp(PC + i, Token, (uint64_t)(0));
Kostya Serebryany379359c2016-10-05 01:09:40 +0000370}
371
Marcos Pividori6137f982017-01-22 01:27:38 +0000372ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000373ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000374ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000375void __sanitizer_cov_trace_div4(uint32_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000376 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000377 fuzzer::TPC.HandleCmp(PC, Val, (uint32_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000378}
Marcos Pividori6137f982017-01-22 01:27:38 +0000379
380ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000381ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000382ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000383void __sanitizer_cov_trace_div8(uint64_t Val) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000384 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000385 fuzzer::TPC.HandleCmp(PC, Val, (uint64_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000386}
Marcos Pividori6137f982017-01-22 01:27:38 +0000387
388ATTRIBUTE_INTERFACE
Kostya Serebryany7f058972017-01-27 00:09:59 +0000389ATTRIBUTE_NO_SANITIZE_ALL
Kostya Serebryany8e9ac422017-01-27 00:20:55 +0000390ATTRIBUTE_TARGET_POPCNT
Kostya Serebryany379359c2016-10-05 01:09:40 +0000391void __sanitizer_cov_trace_gep(uintptr_t Idx) {
Kostya Serebryany7c021af2017-01-26 00:22:08 +0000392 uintptr_t PC = reinterpret_cast<uintptr_t>(__builtin_return_address(0));
Mike Aizatsky0e37f8e2017-01-17 23:11:32 +0000393 fuzzer::TPC.HandleCmp(PC, Idx, (uintptr_t)0);
Kostya Serebryany379359c2016-10-05 01:09:40 +0000394}
Kostya Serebryany379359c2016-10-05 01:09:40 +0000395} // extern "C"