1c5f01b2fSopenharmony_ci//===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
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// Misc utils.
10c5f01b2fSopenharmony_ci//===----------------------------------------------------------------------===//
11c5f01b2fSopenharmony_ci
12c5f01b2fSopenharmony_ci#include "FuzzerUtil.h"
13c5f01b2fSopenharmony_ci#include "FuzzerIO.h"
14c5f01b2fSopenharmony_ci#include "FuzzerInternal.h"
15c5f01b2fSopenharmony_ci#include <cassert>
16c5f01b2fSopenharmony_ci#include <chrono>
17c5f01b2fSopenharmony_ci#include <cstring>
18c5f01b2fSopenharmony_ci#include <errno.h>
19c5f01b2fSopenharmony_ci#include <signal.h>
20c5f01b2fSopenharmony_ci#include <sstream>
21c5f01b2fSopenharmony_ci#include <stdio.h>
22c5f01b2fSopenharmony_ci#include <sys/types.h>
23c5f01b2fSopenharmony_ci#include <thread>
24c5f01b2fSopenharmony_ci
25c5f01b2fSopenharmony_cinamespace fuzzer {
26c5f01b2fSopenharmony_ci
27c5f01b2fSopenharmony_civoid PrintHexArray(const uint8_t *Data, size_t Size,
28c5f01b2fSopenharmony_ci                   const char *PrintAfter) {
29c5f01b2fSopenharmony_ci  for (size_t i = 0; i < Size; i++)
30c5f01b2fSopenharmony_ci    Printf("0x%x,", (unsigned)Data[i]);
31c5f01b2fSopenharmony_ci  Printf("%s", PrintAfter);
32c5f01b2fSopenharmony_ci}
33c5f01b2fSopenharmony_ci
34c5f01b2fSopenharmony_civoid Print(const Unit &v, const char *PrintAfter) {
35c5f01b2fSopenharmony_ci  PrintHexArray(v.data(), v.size(), PrintAfter);
36c5f01b2fSopenharmony_ci}
37c5f01b2fSopenharmony_ci
38c5f01b2fSopenharmony_civoid PrintASCIIByte(uint8_t Byte) {
39c5f01b2fSopenharmony_ci  if (Byte == '\\')
40c5f01b2fSopenharmony_ci    Printf("\\\\");
41c5f01b2fSopenharmony_ci  else if (Byte == '"')
42c5f01b2fSopenharmony_ci    Printf("\\\"");
43c5f01b2fSopenharmony_ci  else if (Byte >= 32 && Byte < 127)
44c5f01b2fSopenharmony_ci    Printf("%c", Byte);
45c5f01b2fSopenharmony_ci  else
46c5f01b2fSopenharmony_ci    Printf("\\x%02x", Byte);
47c5f01b2fSopenharmony_ci}
48c5f01b2fSopenharmony_ci
49c5f01b2fSopenharmony_civoid PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
50c5f01b2fSopenharmony_ci  for (size_t i = 0; i < Size; i++)
51c5f01b2fSopenharmony_ci    PrintASCIIByte(Data[i]);
52c5f01b2fSopenharmony_ci  Printf("%s", PrintAfter);
53c5f01b2fSopenharmony_ci}
54c5f01b2fSopenharmony_ci
55c5f01b2fSopenharmony_civoid PrintASCII(const Unit &U, const char *PrintAfter) {
56c5f01b2fSopenharmony_ci  PrintASCII(U.data(), U.size(), PrintAfter);
57c5f01b2fSopenharmony_ci}
58c5f01b2fSopenharmony_ci
59c5f01b2fSopenharmony_cibool ToASCII(uint8_t *Data, size_t Size) {
60c5f01b2fSopenharmony_ci  bool Changed = false;
61c5f01b2fSopenharmony_ci  for (size_t i = 0; i < Size; i++) {
62c5f01b2fSopenharmony_ci    uint8_t &X = Data[i];
63c5f01b2fSopenharmony_ci    auto NewX = X;
64c5f01b2fSopenharmony_ci    NewX &= 127;
65c5f01b2fSopenharmony_ci    if (!isspace(NewX) && !isprint(NewX))
66c5f01b2fSopenharmony_ci      NewX = ' ';
67c5f01b2fSopenharmony_ci    Changed |= NewX != X;
68c5f01b2fSopenharmony_ci    X = NewX;
69c5f01b2fSopenharmony_ci  }
70c5f01b2fSopenharmony_ci  return Changed;
71c5f01b2fSopenharmony_ci}
72c5f01b2fSopenharmony_ci
73c5f01b2fSopenharmony_cibool IsASCII(const Unit &U) { return IsASCII(U.data(), U.size()); }
74c5f01b2fSopenharmony_ci
75c5f01b2fSopenharmony_cibool IsASCII(const uint8_t *Data, size_t Size) {
76c5f01b2fSopenharmony_ci  for (size_t i = 0; i < Size; i++)
77c5f01b2fSopenharmony_ci    if (!(isprint(Data[i]) || isspace(Data[i]))) return false;
78c5f01b2fSopenharmony_ci  return true;
79c5f01b2fSopenharmony_ci}
80c5f01b2fSopenharmony_ci
81c5f01b2fSopenharmony_cibool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
82c5f01b2fSopenharmony_ci  U->clear();
83c5f01b2fSopenharmony_ci  if (Str.empty()) return false;
84c5f01b2fSopenharmony_ci  size_t L = 0, R = Str.size() - 1;  // We are parsing the range [L,R].
85c5f01b2fSopenharmony_ci  // Skip spaces from both sides.
86c5f01b2fSopenharmony_ci  while (L < R && isspace(Str[L])) L++;
87c5f01b2fSopenharmony_ci  while (R > L && isspace(Str[R])) R--;
88c5f01b2fSopenharmony_ci  if (R - L < 2) return false;
89c5f01b2fSopenharmony_ci  // Check the closing "
90c5f01b2fSopenharmony_ci  if (Str[R] != '"') return false;
91c5f01b2fSopenharmony_ci  R--;
92c5f01b2fSopenharmony_ci  // Find the opening "
93c5f01b2fSopenharmony_ci  while (L < R && Str[L] != '"') L++;
94c5f01b2fSopenharmony_ci  if (L >= R) return false;
95c5f01b2fSopenharmony_ci  assert(Str[L] == '\"');
96c5f01b2fSopenharmony_ci  L++;
97c5f01b2fSopenharmony_ci  assert(L <= R);
98c5f01b2fSopenharmony_ci  for (size_t Pos = L; Pos <= R; Pos++) {
99c5f01b2fSopenharmony_ci    uint8_t V = (uint8_t)Str[Pos];
100c5f01b2fSopenharmony_ci    if (!isprint(V) && !isspace(V)) return false;
101c5f01b2fSopenharmony_ci    if (V =='\\') {
102c5f01b2fSopenharmony_ci      // Handle '\\'
103c5f01b2fSopenharmony_ci      if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
104c5f01b2fSopenharmony_ci        U->push_back(Str[Pos + 1]);
105c5f01b2fSopenharmony_ci        Pos++;
106c5f01b2fSopenharmony_ci        continue;
107c5f01b2fSopenharmony_ci      }
108c5f01b2fSopenharmony_ci      // Handle '\xAB'
109c5f01b2fSopenharmony_ci      if (Pos + 3 <= R && Str[Pos + 1] == 'x'
110c5f01b2fSopenharmony_ci           && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
111c5f01b2fSopenharmony_ci        char Hex[] = "0xAA";
112c5f01b2fSopenharmony_ci        Hex[2] = Str[Pos + 2];
113c5f01b2fSopenharmony_ci        Hex[3] = Str[Pos + 3];
114c5f01b2fSopenharmony_ci        U->push_back(strtol(Hex, nullptr, 16));
115c5f01b2fSopenharmony_ci        Pos += 3;
116c5f01b2fSopenharmony_ci        continue;
117c5f01b2fSopenharmony_ci      }
118c5f01b2fSopenharmony_ci      return false;  // Invalid escape.
119c5f01b2fSopenharmony_ci    } else {
120c5f01b2fSopenharmony_ci      // Any other character.
121c5f01b2fSopenharmony_ci      U->push_back(V);
122c5f01b2fSopenharmony_ci    }
123c5f01b2fSopenharmony_ci  }
124c5f01b2fSopenharmony_ci  return true;
125c5f01b2fSopenharmony_ci}
126c5f01b2fSopenharmony_ci
127c5f01b2fSopenharmony_cibool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
128c5f01b2fSopenharmony_ci  if (Text.empty()) {
129c5f01b2fSopenharmony_ci    Printf("ParseDictionaryFile: file does not exist or is empty\n");
130c5f01b2fSopenharmony_ci    return false;
131c5f01b2fSopenharmony_ci  }
132c5f01b2fSopenharmony_ci  std::istringstream ISS(Text);
133c5f01b2fSopenharmony_ci  Units->clear();
134c5f01b2fSopenharmony_ci  Unit U;
135c5f01b2fSopenharmony_ci  int LineNo = 0;
136c5f01b2fSopenharmony_ci  std::string S;
137c5f01b2fSopenharmony_ci  while (std::getline(ISS, S, '\n')) {
138c5f01b2fSopenharmony_ci    LineNo++;
139c5f01b2fSopenharmony_ci    size_t Pos = 0;
140c5f01b2fSopenharmony_ci    while (Pos < S.size() && isspace(S[Pos])) Pos++;  // Skip spaces.
141c5f01b2fSopenharmony_ci    if (Pos == S.size()) continue;  // Empty line.
142c5f01b2fSopenharmony_ci    if (S[Pos] == '#') continue;  // Comment line.
143c5f01b2fSopenharmony_ci    if (ParseOneDictionaryEntry(S, &U)) {
144c5f01b2fSopenharmony_ci      Units->push_back(U);
145c5f01b2fSopenharmony_ci    } else {
146c5f01b2fSopenharmony_ci      Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
147c5f01b2fSopenharmony_ci             S.c_str());
148c5f01b2fSopenharmony_ci      return false;
149c5f01b2fSopenharmony_ci    }
150c5f01b2fSopenharmony_ci  }
151c5f01b2fSopenharmony_ci  return true;
152c5f01b2fSopenharmony_ci}
153c5f01b2fSopenharmony_ci
154c5f01b2fSopenharmony_cistd::string Base64(const Unit &U) {
155c5f01b2fSopenharmony_ci  static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
156c5f01b2fSopenharmony_ci                              "abcdefghijklmnopqrstuvwxyz"
157c5f01b2fSopenharmony_ci                              "0123456789+/";
158c5f01b2fSopenharmony_ci  std::string Res;
159c5f01b2fSopenharmony_ci  size_t i;
160c5f01b2fSopenharmony_ci  for (i = 0; i + 2 < U.size(); i += 3) {
161c5f01b2fSopenharmony_ci    uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
162c5f01b2fSopenharmony_ci    Res += Table[(x >> 18) & 63];
163c5f01b2fSopenharmony_ci    Res += Table[(x >> 12) & 63];
164c5f01b2fSopenharmony_ci    Res += Table[(x >> 6) & 63];
165c5f01b2fSopenharmony_ci    Res += Table[x & 63];
166c5f01b2fSopenharmony_ci  }
167c5f01b2fSopenharmony_ci  if (i + 1 == U.size()) {
168c5f01b2fSopenharmony_ci    uint32_t x = (U[i] << 16);
169c5f01b2fSopenharmony_ci    Res += Table[(x >> 18) & 63];
170c5f01b2fSopenharmony_ci    Res += Table[(x >> 12) & 63];
171c5f01b2fSopenharmony_ci    Res += "==";
172c5f01b2fSopenharmony_ci  } else if (i + 2 == U.size()) {
173c5f01b2fSopenharmony_ci    uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
174c5f01b2fSopenharmony_ci    Res += Table[(x >> 18) & 63];
175c5f01b2fSopenharmony_ci    Res += Table[(x >> 12) & 63];
176c5f01b2fSopenharmony_ci    Res += Table[(x >> 6) & 63];
177c5f01b2fSopenharmony_ci    Res += "=";
178c5f01b2fSopenharmony_ci  }
179c5f01b2fSopenharmony_ci  return Res;
180c5f01b2fSopenharmony_ci}
181c5f01b2fSopenharmony_ci
182c5f01b2fSopenharmony_cistd::string DescribePC(const char *SymbolizedFMT, uintptr_t PC) {
183c5f01b2fSopenharmony_ci  if (!EF->__sanitizer_symbolize_pc) return "<can not symbolize>";
184c5f01b2fSopenharmony_ci  char PcDescr[1024];
185c5f01b2fSopenharmony_ci  EF->__sanitizer_symbolize_pc(reinterpret_cast<void*>(PC),
186c5f01b2fSopenharmony_ci                               SymbolizedFMT, PcDescr, sizeof(PcDescr));
187c5f01b2fSopenharmony_ci  PcDescr[sizeof(PcDescr) - 1] = 0;  // Just in case.
188c5f01b2fSopenharmony_ci  return PcDescr;
189c5f01b2fSopenharmony_ci}
190c5f01b2fSopenharmony_ci
191c5f01b2fSopenharmony_civoid PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC) {
192c5f01b2fSopenharmony_ci  if (EF->__sanitizer_symbolize_pc)
193c5f01b2fSopenharmony_ci    Printf("%s", DescribePC(SymbolizedFMT, PC).c_str());
194c5f01b2fSopenharmony_ci  else
195c5f01b2fSopenharmony_ci    Printf(FallbackFMT, PC);
196c5f01b2fSopenharmony_ci}
197c5f01b2fSopenharmony_ci
198c5f01b2fSopenharmony_ciunsigned NumberOfCpuCores() {
199c5f01b2fSopenharmony_ci  unsigned N = std::thread::hardware_concurrency();
200c5f01b2fSopenharmony_ci  if (!N) {
201c5f01b2fSopenharmony_ci    Printf("WARNING: std::thread::hardware_concurrency not well defined for "
202c5f01b2fSopenharmony_ci           "your platform. Assuming CPU count of 1.\n");
203c5f01b2fSopenharmony_ci    N = 1;
204c5f01b2fSopenharmony_ci  }
205c5f01b2fSopenharmony_ci  return N;
206c5f01b2fSopenharmony_ci}
207c5f01b2fSopenharmony_ci
208c5f01b2fSopenharmony_cibool ExecuteCommandAndReadOutput(const std::string &Command, std::string *Out) {
209c5f01b2fSopenharmony_ci  FILE *Pipe = OpenProcessPipe(Command.c_str(), "r");
210c5f01b2fSopenharmony_ci  if (!Pipe) return false;
211c5f01b2fSopenharmony_ci  char Buff[1024];
212c5f01b2fSopenharmony_ci  size_t N;
213c5f01b2fSopenharmony_ci  while ((N = fread(Buff, 1, sizeof(Buff), Pipe)) > 0)
214c5f01b2fSopenharmony_ci    Out->append(Buff, N);
215c5f01b2fSopenharmony_ci  return true;
216c5f01b2fSopenharmony_ci}
217c5f01b2fSopenharmony_ci
218c5f01b2fSopenharmony_ci}  // namespace fuzzer
219