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