Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 1 | //===- 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 Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 10 | // This module implements __sanitizer_cov_trace_pc_guard[_init], |
| 11 | // the callback required for -fsanitize-coverage=trace-pc-guard instrumentation. |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 12 | // |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 15 | #include "FuzzerCorpus.h" |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 16 | #include "FuzzerDefs.h" |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 17 | #include "FuzzerDictionary.h" |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 18 | #include "FuzzerExtFunctions.h" |
Zachary Turner | 24a148b | 2016-11-30 19:06:14 +0000 | [diff] [blame] | 19 | #include "FuzzerIO.h" |
Kostya Serebryany | 6f5a804 | 2016-09-21 01:50:50 +0000 | [diff] [blame] | 20 | #include "FuzzerTracePC.h" |
Kostya Serebryany | 8658618 | 2016-09-21 21:17:23 +0000 | [diff] [blame] | 21 | #include "FuzzerValueBitMap.h" |
Zachary Turner | 24a148b | 2016-11-30 19:06:14 +0000 | [diff] [blame] | 22 | #include <map> |
| 23 | #include <set> |
| 24 | #include <sstream> |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 25 | |
| 26 | namespace fuzzer { |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 27 | |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 28 | TracePC TPC; |
Mike Aizatsky | 1aa501e | 2016-05-10 23:43:15 +0000 | [diff] [blame] | 29 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 30 | void TracePC::HandleTrace(uint32_t *Guard, uintptr_t PC) { |
| 31 | uint32_t Idx = *Guard; |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 32 | if (!Idx) return; |
Kostya Serebryany | 2fabeca | 2016-10-26 18:52:04 +0000 | [diff] [blame] | 33 | PCs[Idx % kNumPCs] = PC; |
| 34 | Counters[Idx % kNumCounters]++; |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 35 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 36 | |
Kostya Serebryany | 275e260 | 2016-10-25 23:52:25 +0000 | [diff] [blame] | 37 | size_t TracePC::GetTotalPCCoverage() { |
| 38 | size_t Res = 0; |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 39 | for (size_t i = 1; i < GetNumPCs(); i++) |
Kostya Serebryany | 275e260 | 2016-10-25 23:52:25 +0000 | [diff] [blame] | 40 | if (PCs[i]) |
| 41 | Res++; |
| 42 | return Res; |
| 43 | } |
| 44 | |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 45 | void TracePC::HandleInit(uint32_t *Start, uint32_t *Stop) { |
Kostya Serebryany | 3e36ec1 | 2016-09-17 05:04:47 +0000 | [diff] [blame] | 46 | if (Start == Stop || *Start) return; |
| 47 | assert(NumModules < sizeof(Modules) / sizeof(Modules[0])); |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 48 | for (uint32_t *P = Start; P < Stop; P++) |
Kostya Serebryany | 8e781a8 | 2016-09-18 04:52:23 +0000 | [diff] [blame] | 49 | *P = ++NumGuards; |
Kostya Serebryany | 3e36ec1 | 2016-09-17 05:04:47 +0000 | [diff] [blame] | 50 | Modules[NumModules].Start = Start; |
| 51 | Modules[NumModules].Stop = Stop; |
| 52 | NumModules++; |
| 53 | } |
| 54 | |
| 55 | void TracePC::PrintModuleInfo() { |
| 56 | Printf("INFO: Loaded %zd modules (%zd guards): ", NumModules, NumGuards); |
| 57 | for (size_t i = 0; i < NumModules; i++) |
| 58 | Printf("[%p, %p), ", Modules[i].Start, Modules[i].Stop); |
| 59 | Printf("\n"); |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 60 | } |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 61 | |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame] | 62 | void TracePC::HandleCallerCallee(uintptr_t Caller, uintptr_t Callee) { |
| 63 | const uintptr_t kBits = 12; |
| 64 | const uintptr_t kMask = (1 << kBits) - 1; |
Kostya Serebryany | 1c73f1b | 2016-10-05 22:56:21 +0000 | [diff] [blame] | 65 | uintptr_t Idx = (Caller & kMask) | ((Callee & kMask) << kBits); |
| 66 | HandleValueProfile(Idx); |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 69 | static bool IsInterestingCoverageFile(std::string &File) { |
| 70 | if (File.find("compiler-rt/lib/") != std::string::npos) |
| 71 | return false; // sanitizer internal. |
| 72 | if (File.find("/usr/lib/") != std::string::npos) |
| 73 | return false; |
| 74 | if (File.find("/usr/include/") != std::string::npos) |
| 75 | return false; |
| 76 | if (File == "<null>") |
| 77 | return false; |
| 78 | return true; |
| 79 | } |
| 80 | |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 81 | void TracePC::PrintNewPCs() { |
| 82 | if (DoPrintNewPCs) { |
| 83 | if (!PrintedPCs) |
| 84 | PrintedPCs = new std::set<uintptr_t>; |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 85 | for (size_t i = 1; i < GetNumPCs(); i++) |
Kostya Serebryany | a5b2e54 | 2016-10-26 00:20:51 +0000 | [diff] [blame] | 86 | if (PCs[i] && PrintedPCs->insert(PCs[i]).second) |
| 87 | PrintPC("\tNEW_PC: %p %F %L\n", "\tNEW_PC: %p\n", PCs[i]); |
| 88 | } |
| 89 | } |
| 90 | |
Kostya Serebryany | b706b48 | 2016-09-18 21:47:08 +0000 | [diff] [blame] | 91 | void TracePC::PrintCoverage() { |
Kostya Serebryany | 1394ce2 | 2016-12-10 01:19:35 +0000 | [diff] [blame] | 92 | if (!EF->__sanitizer_symbolize_pc || |
| 93 | !EF->__sanitizer_get_module_and_offset_for_pc) { |
| 94 | Printf("INFO: __sanitizer_symbolize_pc or " |
| 95 | "__sanitizer_get_module_and_offset_for_pc is not available," |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 96 | " not printing coverage\n"); |
| 97 | return; |
| 98 | } |
| 99 | std::map<std::string, std::vector<uintptr_t>> CoveredPCsPerModule; |
| 100 | std::map<std::string, uintptr_t> ModuleOffsets; |
Kostya Serebryany | 1cba0a9 | 2016-11-30 21:53:32 +0000 | [diff] [blame] | 101 | std::set<std::string> CoveredDirs, CoveredFiles, CoveredFunctions, |
| 102 | CoveredLines; |
Kostya Serebryany | b706b48 | 2016-09-18 21:47:08 +0000 | [diff] [blame] | 103 | Printf("COVERAGE:\n"); |
Kostya Serebryany | 06b8757 | 2016-10-26 00:42:52 +0000 | [diff] [blame] | 104 | for (size_t i = 1; i < GetNumPCs(); i++) { |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 105 | if (!PCs[i]) continue; |
| 106 | std::string FileStr = DescribePC("%s", PCs[i]); |
| 107 | if (!IsInterestingCoverageFile(FileStr)) continue; |
| 108 | std::string FixedPCStr = DescribePC("%p", PCs[i]); |
| 109 | std::string FunctionStr = DescribePC("%F", PCs[i]); |
| 110 | std::string LineStr = DescribePC("%l", PCs[i]); |
Kostya Serebryany | 1394ce2 | 2016-12-10 01:19:35 +0000 | [diff] [blame] | 111 | char ModulePathRaw[4096] = ""; // What's PATH_MAX in portable C++? |
| 112 | void *OffsetRaw = nullptr; |
| 113 | if (!EF->__sanitizer_get_module_and_offset_for_pc( |
| 114 | reinterpret_cast<void *>(PCs[i]), ModulePathRaw, |
| 115 | sizeof(ModulePathRaw), &OffsetRaw)) |
| 116 | continue; |
| 117 | std::string Module = ModulePathRaw; |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 118 | uintptr_t FixedPC = std::stol(FixedPCStr, 0, 16); |
Kostya Serebryany | 1394ce2 | 2016-12-10 01:19:35 +0000 | [diff] [blame] | 119 | uintptr_t PcOffset = reinterpret_cast<uintptr_t>(OffsetRaw); |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 120 | ModuleOffsets[Module] = FixedPC - PcOffset; |
| 121 | CoveredPCsPerModule[Module].push_back(PcOffset); |
| 122 | CoveredFunctions.insert(FunctionStr); |
| 123 | CoveredFiles.insert(FileStr); |
Kostya Serebryany | 1cba0a9 | 2016-11-30 21:53:32 +0000 | [diff] [blame] | 124 | CoveredDirs.insert(DirName(FileStr)); |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 125 | if (!CoveredLines.insert(FileStr + ":" + LineStr).second) |
| 126 | continue; |
| 127 | Printf("COVERED: %s %s:%s\n", FunctionStr.c_str(), |
| 128 | FileStr.c_str(), LineStr.c_str()); |
| 129 | } |
| 130 | |
Kostya Serebryany | 1cba0a9 | 2016-11-30 21:53:32 +0000 | [diff] [blame] | 131 | std::string CoveredDirsStr; |
| 132 | for (auto &Dir : CoveredDirs) { |
| 133 | if (!CoveredDirsStr.empty()) |
| 134 | CoveredDirsStr += ","; |
| 135 | CoveredDirsStr += Dir; |
| 136 | } |
| 137 | Printf("COVERED_DIRS: %s\n", CoveredDirsStr.c_str()); |
| 138 | |
Kostya Serebryany | 95b1a43 | 2016-10-19 00:12:03 +0000 | [diff] [blame] | 139 | for (auto &M : CoveredPCsPerModule) { |
| 140 | std::set<std::string> UncoveredFiles, UncoveredFunctions; |
| 141 | std::map<std::string, std::set<int> > UncoveredLines; // Func+File => lines |
| 142 | auto &ModuleName = M.first; |
| 143 | auto &CoveredOffsets = M.second; |
| 144 | uintptr_t ModuleOffset = ModuleOffsets[ModuleName]; |
| 145 | std::sort(CoveredOffsets.begin(), CoveredOffsets.end()); |
| 146 | Printf("MODULE_WITH_COVERAGE: %s\n", ModuleName.c_str()); |
| 147 | // sancov does not yet fully support DSOs. |
| 148 | // std::string Cmd = "sancov -print-coverage-pcs " + ModuleName; |
| 149 | std::string Cmd = "objdump -d " + ModuleName + |
| 150 | " | grep 'call.*__sanitizer_cov_trace_pc_guard' | awk -F: '{print $1}'"; |
| 151 | std::string SanCovOutput; |
| 152 | if (!ExecuteCommandAndReadOutput(Cmd, &SanCovOutput)) { |
| 153 | Printf("INFO: Command failed: %s\n", Cmd.c_str()); |
| 154 | continue; |
| 155 | } |
| 156 | std::istringstream ISS(SanCovOutput); |
| 157 | std::string S; |
| 158 | while (std::getline(ISS, S, '\n')) { |
| 159 | uintptr_t PcOffset = std::stol(S, 0, 16); |
| 160 | if (!std::binary_search(CoveredOffsets.begin(), CoveredOffsets.end(), |
| 161 | PcOffset)) { |
| 162 | uintptr_t PC = ModuleOffset + PcOffset; |
| 163 | auto FileStr = DescribePC("%s", PC); |
| 164 | if (!IsInterestingCoverageFile(FileStr)) continue; |
| 165 | if (CoveredFiles.count(FileStr) == 0) { |
| 166 | UncoveredFiles.insert(FileStr); |
| 167 | continue; |
| 168 | } |
| 169 | auto FunctionStr = DescribePC("%F", PC); |
| 170 | if (CoveredFunctions.count(FunctionStr) == 0) { |
| 171 | UncoveredFunctions.insert(FunctionStr); |
| 172 | continue; |
| 173 | } |
| 174 | std::string LineStr = DescribePC("%l", PC); |
| 175 | uintptr_t Line = std::stoi(LineStr); |
| 176 | std::string FileLineStr = FileStr + ":" + LineStr; |
| 177 | if (CoveredLines.count(FileLineStr) == 0) |
| 178 | UncoveredLines[FunctionStr + " " + FileStr].insert(Line); |
| 179 | } |
| 180 | } |
| 181 | for (auto &FileLine: UncoveredLines) |
| 182 | for (int Line : FileLine.second) |
| 183 | Printf("UNCOVERED_LINE: %s:%d\n", FileLine.first.c_str(), Line); |
| 184 | for (auto &Func : UncoveredFunctions) |
| 185 | Printf("UNCOVERED_FUNC: %s\n", Func.c_str()); |
| 186 | for (auto &File : UncoveredFiles) |
| 187 | Printf("UNCOVERED_FILE: %s\n", File.c_str()); |
Kostya Serebryany | b706b48 | 2016-09-18 21:47:08 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 191 | // Value profile. |
| 192 | // We keep track of various values that affect control flow. |
| 193 | // These values are inserted into a bit-set-based hash map. |
| 194 | // Every new bit in the map is treated as a new coverage. |
| 195 | // |
| 196 | // For memcmp/strcmp/etc the interesting value is the length of the common |
| 197 | // prefix of the parameters. |
| 198 | // For cmp instructions the interesting value is a XOR of the parameters. |
| 199 | // The interesting value is mixed up with the PC and is then added to the map. |
| 200 | |
Kostya Serebryany | 3a4e2dd | 2016-12-16 22:45:25 +0000 | [diff] [blame] | 201 | ATTRIBUTE_NO_SANITIZE_MEMORY |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 202 | void TracePC::AddValueForMemcmp(void *caller_pc, const void *s1, const void *s2, |
| 203 | size_t n) { |
| 204 | if (!n) return; |
| 205 | size_t Len = std::min(n, (size_t)32); |
| 206 | const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); |
| 207 | const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); |
| 208 | size_t I = 0; |
| 209 | for (; I < Len; I++) |
| 210 | if (A1[I] != A2[I]) |
| 211 | break; |
| 212 | size_t PC = reinterpret_cast<size_t>(caller_pc); |
| 213 | size_t Idx = I; |
| 214 | // if (I < Len) |
| 215 | // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1; |
| 216 | TPC.HandleValueProfile((PC & 4095) | (Idx << 12)); |
| 217 | } |
| 218 | |
Kostya Serebryany | 3a4e2dd | 2016-12-16 22:45:25 +0000 | [diff] [blame] | 219 | ATTRIBUTE_NO_SANITIZE_MEMORY |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 220 | void TracePC::AddValueForStrcmp(void *caller_pc, const char *s1, const char *s2, |
| 221 | size_t n) { |
| 222 | if (!n) return; |
| 223 | size_t Len = std::min(n, (size_t)32); |
| 224 | const uint8_t *A1 = reinterpret_cast<const uint8_t *>(s1); |
| 225 | const uint8_t *A2 = reinterpret_cast<const uint8_t *>(s2); |
| 226 | size_t I = 0; |
| 227 | for (; I < Len; I++) |
| 228 | if (A1[I] != A2[I] || A1[I] == 0) |
| 229 | break; |
| 230 | size_t PC = reinterpret_cast<size_t>(caller_pc); |
| 231 | size_t Idx = I; |
| 232 | // if (I < Len && A1[I]) |
| 233 | // Idx += __builtin_popcountl((A1[I] ^ A2[I])) - 1; |
| 234 | TPC.HandleValueProfile((PC & 4095) | (Idx << 12)); |
| 235 | } |
| 236 | |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 237 | template <class T> |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 238 | ATTRIBUTE_TARGET_POPCNT |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 239 | #ifdef __clang__ // g++ can't handle this __attribute__ here :( |
| 240 | __attribute__((always_inline)) |
| 241 | #endif // __clang__ |
| 242 | void TracePC::HandleCmp(void *PC, T Arg1, T Arg2) { |
| 243 | uintptr_t PCuint = reinterpret_cast<uintptr_t>(PC); |
Kostya Serebryany | a5f94fb | 2016-10-14 20:20:33 +0000 | [diff] [blame] | 244 | uint64_t ArgXor = Arg1 ^ Arg2; |
| 245 | uint64_t ArgDistance = __builtin_popcountl(ArgXor) + 1; // [1,65] |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 246 | uintptr_t Idx = ((PCuint & 4095) + 1) * ArgDistance; |
Kostya Serebryany | 94c427c | 2016-10-27 00:36:38 +0000 | [diff] [blame] | 247 | if (sizeof(T) == 4) |
| 248 | TORC4.Insert(ArgXor, Arg1, Arg2); |
| 249 | else if (sizeof(T) == 8) |
| 250 | TORC8.Insert(ArgXor, Arg1, Arg2); |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 251 | HandleValueProfile(Idx); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 254 | } // namespace fuzzer |
| 255 | |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 256 | extern "C" { |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 257 | __attribute__((visibility("default"))) |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 258 | void __sanitizer_cov_trace_pc_guard(uint32_t *Guard) { |
Kostya Serebryany | a00b243 | 2016-09-14 02:13:06 +0000 | [diff] [blame] | 259 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 260 | fuzzer::TPC.HandleTrace(Guard, PC); |
Kostya Serebryany | da63c1d | 2016-02-26 21:33:56 +0000 | [diff] [blame] | 261 | } |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 262 | |
Kostya Serebryany | 32661f9 | 2016-08-18 20:52:52 +0000 | [diff] [blame] | 263 | __attribute__((visibility("default"))) |
Kostya Serebryany | a9b0dd0 | 2016-09-29 17:43:24 +0000 | [diff] [blame] | 264 | void __sanitizer_cov_trace_pc_guard_init(uint32_t *Start, uint32_t *Stop) { |
Kostya Serebryany | a5277d5 | 2016-09-15 01:30:18 +0000 | [diff] [blame] | 265 | fuzzer::TPC.HandleInit(Start, Stop); |
Dan Liew | 5914407 | 2016-06-06 20:27:09 +0000 | [diff] [blame] | 266 | } |
Kostya Serebryany | 0984517 | 2016-09-15 22:16:15 +0000 | [diff] [blame] | 267 | |
| 268 | __attribute__((visibility("default"))) |
| 269 | void __sanitizer_cov_trace_pc_indir(uintptr_t Callee) { |
| 270 | uintptr_t PC = (uintptr_t)__builtin_return_address(0); |
| 271 | fuzzer::TPC.HandleCallerCallee(PC, Callee); |
| 272 | } |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 273 | |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 274 | __attribute__((visibility("default"))) |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 275 | void __sanitizer_cov_trace_cmp8(uint64_t Arg1, uint64_t Arg2) { |
| 276 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 277 | } |
| 278 | __attribute__((visibility("default"))) |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 279 | void __sanitizer_cov_trace_cmp4(uint32_t Arg1, uint32_t Arg2) { |
| 280 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 281 | } |
| 282 | __attribute__((visibility("default"))) |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 283 | void __sanitizer_cov_trace_cmp2(uint16_t Arg1, uint16_t Arg2) { |
| 284 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 285 | } |
| 286 | __attribute__((visibility("default"))) |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 287 | void __sanitizer_cov_trace_cmp1(uint8_t Arg1, uint8_t Arg2) { |
| 288 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Arg1, Arg2); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | __attribute__((visibility("default"))) |
| 292 | void __sanitizer_cov_trace_switch(uint64_t Val, uint64_t *Cases) { |
Kostya Serebryany | 00e638e | 2016-12-17 02:03:34 +0000 | [diff] [blame^] | 293 | // Updates the value profile based on the relative position of Val and Cases. |
| 294 | // We want to handle one random case at every call (handling all is slow). |
| 295 | // Since none of the arguments contain any random bits we use a thread-local |
| 296 | // counter to choose the random case to handle. |
| 297 | static thread_local size_t Counter; |
| 298 | Counter++; |
Kostya Serebryany | d19919a | 2016-10-11 01:14:41 +0000 | [diff] [blame] | 299 | uint64_t N = Cases[0]; |
| 300 | uint64_t *Vals = Cases + 2; |
| 301 | char *PC = (char*)__builtin_return_address(0); |
Kostya Serebryany | 00e638e | 2016-12-17 02:03:34 +0000 | [diff] [blame^] | 302 | size_t Idx = Counter % N; |
| 303 | uint64_t TwoIn32 = 1ULL << 32; |
| 304 | if ((Val | Vals[Idx]) < TwoIn32) |
| 305 | fuzzer::TPC.HandleCmp(PC + Idx, static_cast<uint32_t>(Val), |
| 306 | static_cast<uint32_t>(Vals[Idx])); |
| 307 | else |
| 308 | fuzzer::TPC.HandleCmp(PC + Idx, Val, Vals[Idx]); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | __attribute__((visibility("default"))) |
| 312 | void __sanitizer_cov_trace_div4(uint32_t Val) { |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 313 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint32_t)0); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 314 | } |
| 315 | __attribute__((visibility("default"))) |
| 316 | void __sanitizer_cov_trace_div8(uint64_t Val) { |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 317 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Val, (uint64_t)0); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 318 | } |
| 319 | __attribute__((visibility("default"))) |
| 320 | void __sanitizer_cov_trace_gep(uintptr_t Idx) { |
Kostya Serebryany | 17d176e1 | 2016-10-13 16:19:09 +0000 | [diff] [blame] | 321 | fuzzer::TPC.HandleCmp(__builtin_return_address(0), Idx, (uintptr_t)0); |
Kostya Serebryany | 379359c | 2016-10-05 01:09:40 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | } // extern "C" |