xref: /llvm-project/compiler-rt/lib/fuzzer/FuzzerUtil.cpp (revision a2b677e8153758997a9043360cf51333eecc3c44)
1 //===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Misc utils.
9 //===----------------------------------------------------------------------===//
10 
11 #include "FuzzerUtil.h"
12 #include "FuzzerIO.h"
13 #include "FuzzerInternal.h"
14 #include <cassert>
15 #include <chrono>
16 #include <cstring>
17 #include <errno.h>
18 #include <mutex>
19 #include <signal.h>
20 #include <sstream>
21 #include <stdio.h>
22 #include <sys/auxv.h>
23 #include <sys/types.h>
24 #include <thread>
25 
26 namespace fuzzer {
27 
28 void PrintHexArray(const uint8_t *Data, size_t Size,
29                    const char *PrintAfter) {
30   for (size_t i = 0; i < Size; i++)
31     Printf("0x%x,", (unsigned)Data[i]);
32   Printf("%s", PrintAfter);
33 }
34 
35 void Print(const Unit &v, const char *PrintAfter) {
36   PrintHexArray(v.data(), v.size(), PrintAfter);
37 }
38 
39 void PrintASCIIByte(uint8_t Byte) {
40   if (Byte == '\\')
41     Printf("\\\\");
42   else if (Byte == '"')
43     Printf("\\\"");
44   else if (Byte >= 32 && Byte < 127)
45     Printf("%c", Byte);
46   else
47     Printf("\\%03o", Byte);
48 }
49 
50 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
51   for (size_t i = 0; i < Size; i++)
52     PrintASCIIByte(Data[i]);
53   Printf("%s", PrintAfter);
54 }
55 
56 void PrintASCII(const Unit &U, const char *PrintAfter) {
57   PrintASCII(U.data(), U.size(), PrintAfter);
58 }
59 
60 bool ToASCII(uint8_t *Data, size_t Size) {
61   bool Changed = false;
62   for (size_t i = 0; i < Size; i++) {
63     uint8_t &X = Data[i];
64     auto NewX = X;
65     NewX &= 127;
66     if (!isspace(NewX) && !isprint(NewX))
67       NewX = ' ';
68     Changed |= NewX != X;
69     X = NewX;
70   }
71   return Changed;
72 }
73 
74 bool IsASCII(const Unit &U) { return IsASCII(U.data(), U.size()); }
75 
76 bool IsASCII(const uint8_t *Data, size_t Size) {
77   for (size_t i = 0; i < Size; i++)
78     if (!(isprint(Data[i]) || isspace(Data[i]))) return false;
79   return true;
80 }
81 
82 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
83   U->clear();
84   if (Str.empty()) return false;
85   size_t L = 0, R = Str.size() - 1;  // We are parsing the range [L,R].
86   // Skip spaces from both sides.
87   while (L < R && isspace(Str[L])) L++;
88   while (R > L && isspace(Str[R])) R--;
89   if (R - L < 2) return false;
90   // Check the closing "
91   if (Str[R] != '"') return false;
92   R--;
93   // Find the opening "
94   while (L < R && Str[L] != '"') L++;
95   if (L >= R) return false;
96   assert(Str[L] == '\"');
97   L++;
98   assert(L <= R);
99   for (size_t Pos = L; Pos <= R; Pos++) {
100     uint8_t V = (uint8_t)Str[Pos];
101     if (!isprint(V) && !isspace(V)) return false;
102     if (V =='\\') {
103       // Handle '\\'
104       if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
105         U->push_back(Str[Pos + 1]);
106         Pos++;
107         continue;
108       }
109       // Handle '\xAB'
110       if (Pos + 3 <= R && Str[Pos + 1] == 'x'
111            && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
112         char Hex[] = "0xAA";
113         Hex[2] = Str[Pos + 2];
114         Hex[3] = Str[Pos + 3];
115         U->push_back(static_cast<uint8_t>(strtol(Hex, nullptr, 16)));
116         Pos += 3;
117         continue;
118       }
119       return false;  // Invalid escape.
120     } else {
121       // Any other character.
122       U->push_back(V);
123     }
124   }
125   return true;
126 }
127 
128 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
129   if (Text.empty()) {
130     Printf("ParseDictionaryFile: file does not exist or is empty\n");
131     return false;
132   }
133   std::istringstream ISS(Text);
134   Units->clear();
135   Unit U;
136   int LineNo = 0;
137   std::string S;
138   while (std::getline(ISS, S, '\n')) {
139     LineNo++;
140     size_t Pos = 0;
141     while (Pos < S.size() && isspace(S[Pos])) Pos++;  // Skip spaces.
142     if (Pos == S.size()) continue;  // Empty line.
143     if (S[Pos] == '#') continue;  // Comment line.
144     if (ParseOneDictionaryEntry(S, &U)) {
145       Units->push_back(U);
146     } else {
147       Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
148              S.c_str());
149       return false;
150     }
151   }
152   return true;
153 }
154 
155 // Code duplicated (and tested) in llvm/include/llvm/Support/Base64.h
156 std::string Base64(const Unit &U) {
157   static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
158                               "abcdefghijklmnopqrstuvwxyz"
159                               "0123456789+/";
160   std::string Buffer;
161   Buffer.resize(((U.size() + 2) / 3) * 4);
162 
163   size_t i = 0, j = 0;
164   for (size_t n = U.size() / 3 * 3; i < n; i += 3, j += 4) {
165     uint32_t x = ((unsigned char)U[i] << 16) | ((unsigned char)U[i + 1] << 8) |
166                  (unsigned char)U[i + 2];
167     Buffer[j + 0] = Table[(x >> 18) & 63];
168     Buffer[j + 1] = Table[(x >> 12) & 63];
169     Buffer[j + 2] = Table[(x >> 6) & 63];
170     Buffer[j + 3] = Table[x & 63];
171   }
172   if (i + 1 == U.size()) {
173     uint32_t x = ((unsigned char)U[i] << 16);
174     Buffer[j + 0] = Table[(x >> 18) & 63];
175     Buffer[j + 1] = Table[(x >> 12) & 63];
176     Buffer[j + 2] = '=';
177     Buffer[j + 3] = '=';
178   } else if (i + 2 == U.size()) {
179     uint32_t x = ((unsigned char)U[i] << 16) | ((unsigned char)U[i + 1] << 8);
180     Buffer[j + 0] = Table[(x >> 18) & 63];
181     Buffer[j + 1] = Table[(x >> 12) & 63];
182     Buffer[j + 2] = Table[(x >> 6) & 63];
183     Buffer[j + 3] = '=';
184   }
185   return Buffer;
186 }
187 
188 static std::mutex SymbolizeMutex;
189 
190 std::string DescribePC(const char *SymbolizedFMT, uintptr_t PC) {
191   std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock);
192   if (!EF->__sanitizer_symbolize_pc || !l.owns_lock())
193     return "<can not symbolize>";
194   char PcDescr[1024] = {};
195   EF->__sanitizer_symbolize_pc(reinterpret_cast<void*>(PC),
196                                SymbolizedFMT, PcDescr, sizeof(PcDescr));
197   PcDescr[sizeof(PcDescr) - 1] = 0;  // Just in case.
198   return PcDescr;
199 }
200 
201 void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC) {
202   if (EF->__sanitizer_symbolize_pc)
203     Printf("%s", DescribePC(SymbolizedFMT, PC).c_str());
204   else
205     Printf(FallbackFMT, PC);
206 }
207 
208 void PrintStackTrace() {
209   std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock);
210   if (EF->__sanitizer_print_stack_trace && l.owns_lock())
211     EF->__sanitizer_print_stack_trace();
212 }
213 
214 void PrintMemoryProfile() {
215   std::unique_lock<std::mutex> l(SymbolizeMutex, std::try_to_lock);
216   if (EF->__sanitizer_print_memory_profile && l.owns_lock())
217     EF->__sanitizer_print_memory_profile(95, 8);
218 }
219 
220 unsigned NumberOfCpuCores() {
221   unsigned N = std::thread::hardware_concurrency();
222   if (!N) {
223     Printf("WARNING: std::thread::hardware_concurrency not well defined for "
224            "your platform. Assuming CPU count of 1.\n");
225     N = 1;
226   }
227   return N;
228 }
229 
230 uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial) {
231   uint64_t Res = Initial;
232   const uint8_t *Bytes = static_cast<const uint8_t *>(Data);
233   for (size_t i = 0; i < Size; i++)
234     Res = Res * 11 + Bytes[i];
235   return Res;
236 }
237 
238 size_t PageSize() {
239   static size_t PageSizeCached = getauxval(AT_PAGESZ);
240   return PageSizeCached;
241 }
242 
243 }  // namespace fuzzer
244