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