blob: e4d8352ee82212ca0485ef1e66f5ce559737e759 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
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// Misc utils.
10//===----------------------------------------------------------------------===//
11
12#include "FuzzerInternal.h"
Kostya Serebryany96eab652015-05-14 22:41:49 +000013#include <sstream>
14#include <iomanip>
Kostya Serebryany66ff0752016-02-26 22:42:23 +000015#include <sys/resource.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000016#include <sys/time.h>
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +000017#include <sys/types.h>
18#include <sys/syscall.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000019#include <cassert>
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +000020#include <chrono>
Aaron Ballmanef116982015-01-29 16:58:29 +000021#include <cstring>
22#include <signal.h>
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000023#include <sstream>
Kostya Serebryanyf47198a2015-05-12 22:03:34 +000024#include <unistd.h>
Dmitry Vyukov2eed1212016-03-02 09:54:40 +000025#include <errno.h>
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +000026#include <thread>
Aaron Ballmanef116982015-01-29 16:58:29 +000027
28namespace fuzzer {
29
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000030void PrintHexArray(const uint8_t *Data, size_t Size,
31 const char *PrintAfter) {
32 for (size_t i = 0; i < Size; i++)
33 Printf("0x%x,", (unsigned)Data[i]);
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000034 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000035}
36
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000037void Print(const Unit &v, const char *PrintAfter) {
38 PrintHexArray(v.data(), v.size(), PrintAfter);
39}
40
Kostya Serebryany41740052016-01-12 02:36:59 +000041void PrintASCIIByte(uint8_t Byte) {
42 if (Byte == '\\')
43 Printf("\\\\");
44 else if (Byte == '"')
45 Printf("\\\"");
46 else if (Byte >= 32 && Byte < 127)
47 Printf("%c", Byte);
48 else
49 Printf("\\x%02x", Byte);
50}
51
52void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
53 for (size_t i = 0; i < Size; i++)
54 PrintASCIIByte(Data[i]);
55 Printf("%s", PrintAfter);
56}
57
Aaron Ballmanef116982015-01-29 16:58:29 +000058void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000059 PrintASCII(U.data(), U.size(), PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000060}
61
Kostya Serebryanya9a54802016-08-17 20:45:23 +000062std::string Sha1ToString(uint8_t Sha1[kSHA1NumBytes]) {
63 std::stringstream SS;
64 for (int i = 0; i < kSHA1NumBytes; i++)
65 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Sha1[i];
66 return SS.str();
67}
68
Kostya Serebryany96eab652015-05-14 22:41:49 +000069std::string Hash(const Unit &U) {
70 uint8_t Hash[kSHA1NumBytes];
71 ComputeSHA1(U.data(), U.size(), Hash);
Kostya Serebryanya9a54802016-08-17 20:45:23 +000072 return Sha1ToString(Hash);
Aaron Ballmanef116982015-01-29 16:58:29 +000073}
74
75static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000076 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000077}
78
Kostya Serebryany228d5b12016-03-01 22:19:21 +000079static void CrashHandler(int, siginfo_t *, void *) {
80 Fuzzer::StaticCrashSignalCallback();
81}
82
83static void InterruptHandler(int, siginfo_t *, void *) {
84 Fuzzer::StaticInterruptCallback();
85}
86
87static void SetSigaction(int signum,
88 void (*callback)(int, siginfo_t *, void *)) {
89 struct sigaction sigact;
90 memset(&sigact, 0, sizeof(sigact));
91 sigact.sa_sigaction = callback;
Dmitry Vyukov2eed1212016-03-02 09:54:40 +000092 if (sigaction(signum, &sigact, 0)) {
93 Printf("libFuzzer: sigaction failed with %d\n", errno);
94 exit(1);
95 }
Kostya Serebryany228d5b12016-03-01 22:19:21 +000096}
97
Aaron Ballmanef116982015-01-29 16:58:29 +000098void SetTimer(int Seconds) {
99 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Dmitry Vyukov2eed1212016-03-02 09:54:40 +0000100 if (setitimer(ITIMER_REAL, &T, nullptr)) {
101 Printf("libFuzzer: setitimer failed with %d\n", errno);
102 exit(1);
103 }
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000104 SetSigaction(SIGALRM, AlarmHandler);
Aaron Ballmanef116982015-01-29 16:58:29 +0000105}
106
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000107void SetSigSegvHandler() { SetSigaction(SIGSEGV, CrashHandler); }
108void SetSigBusHandler() { SetSigaction(SIGBUS, CrashHandler); }
109void SetSigAbrtHandler() { SetSigaction(SIGABRT, CrashHandler); }
110void SetSigIllHandler() { SetSigaction(SIGILL, CrashHandler); }
111void SetSigFpeHandler() { SetSigaction(SIGFPE, CrashHandler); }
112void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000113void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000114
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000115int NumberOfCpuCores() {
Dan Liewe6ac1fd2016-05-20 01:30:36 +0000116 const char *CmdLine = nullptr;
117 if (LIBFUZZER_LINUX) {
118 CmdLine = "nproc";
119 } else if (LIBFUZZER_APPLE) {
120 CmdLine = "sysctl -n hw.ncpu";
121 } else {
122 assert(0 && "NumberOfCpuCores() is not implemented for your platform");
123 }
124
125 FILE *F = popen(CmdLine, "r");
126 int N = 1;
127 if (!F || fscanf(F, "%d", &N) != 1) {
128 Printf("WARNING: Failed to parse output of command \"%s\" in %s(). "
129 "Assuming CPU count of 1.\n",
130 CmdLine, __func__);
Dmitry Vyukov2eed1212016-03-02 09:54:40 +0000131 N = 1;
Dan Liewe6ac1fd2016-05-20 01:30:36 +0000132 }
133
134 if (pclose(F)) {
135 Printf("WARNING: Executing command \"%s\" failed in %s(). "
136 "Assuming CPU count of 1.\n",
137 CmdLine, __func__);
138 N = 1;
139 }
140 if (N < 1) {
141 Printf("WARNING: Reported CPU count (%d) from command \"%s\" was invalid "
142 "in %s(). Assuming CPU count of 1.\n",
143 N, CmdLine, __func__);
144 N = 1;
145 }
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000146 return N;
147}
148
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000149bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000150 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000151 for (size_t i = 0; i < Size; i++) {
152 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000153 auto NewX = X;
154 NewX &= 127;
155 if (!isspace(NewX) && !isprint(NewX))
156 NewX = ' ';
157 Changed |= NewX != X;
158 X = NewX;
159 }
160 return Changed;
161}
162
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000163bool IsASCII(const Unit &U) { return IsASCII(U.data(), U.size()); }
164
165bool IsASCII(const uint8_t *Data, size_t Size) {
166 for (size_t i = 0; i < Size; i++)
167 if (!(isprint(Data[i]) || isspace(Data[i]))) return false;
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000168 return true;
169}
170
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000171bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
172 U->clear();
173 if (Str.empty()) return false;
174 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
175 // Skip spaces from both sides.
176 while (L < R && isspace(Str[L])) L++;
177 while (R > L && isspace(Str[R])) R--;
178 if (R - L < 2) return false;
179 // Check the closing "
180 if (Str[R] != '"') return false;
181 R--;
182 // Find the opening "
183 while (L < R && Str[L] != '"') L++;
184 if (L >= R) return false;
185 assert(Str[L] == '\"');
186 L++;
187 assert(L <= R);
188 for (size_t Pos = L; Pos <= R; Pos++) {
189 uint8_t V = (uint8_t)Str[Pos];
190 if (!isprint(V) && !isspace(V)) return false;
191 if (V =='\\') {
192 // Handle '\\'
193 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
194 U->push_back(Str[Pos + 1]);
195 Pos++;
196 continue;
197 }
198 // Handle '\xAB'
199 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
200 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
201 char Hex[] = "0xAA";
202 Hex[2] = Str[Pos + 2];
203 Hex[3] = Str[Pos + 3];
204 U->push_back(strtol(Hex, nullptr, 16));
205 Pos += 3;
206 continue;
207 }
208 return false; // Invalid escape.
209 } else {
210 // Any other character.
211 U->push_back(V);
212 }
213 }
214 return true;
215}
216
217bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
218 if (Text.empty()) {
219 Printf("ParseDictionaryFile: file does not exist or is empty\n");
220 return false;
221 }
222 std::istringstream ISS(Text);
223 Units->clear();
224 Unit U;
225 int LineNo = 0;
226 std::string S;
227 while (std::getline(ISS, S, '\n')) {
228 LineNo++;
229 size_t Pos = 0;
230 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
231 if (Pos == S.size()) continue; // Empty line.
232 if (S[Pos] == '#') continue; // Comment line.
233 if (ParseOneDictionaryEntry(S, &U)) {
234 Units->push_back(U);
235 } else {
236 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
237 S.c_str());
238 return false;
239 }
240 }
241 return true;
242}
243
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000244void SleepSeconds(int Seconds) {
Kostya Serebryany3750c042016-09-19 20:32:34 +0000245 sleep(Seconds); // Use C API to avoid coverage from instrumented libc++.
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000246}
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000247
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000248int GetPid() { return getpid(); }
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000249
250std::string Base64(const Unit &U) {
251 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
252 "abcdefghijklmnopqrstuvwxyz"
253 "0123456789+/";
254 std::string Res;
255 size_t i;
256 for (i = 0; i + 2 < U.size(); i += 3) {
257 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
258 Res += Table[(x >> 18) & 63];
259 Res += Table[(x >> 12) & 63];
260 Res += Table[(x >> 6) & 63];
261 Res += Table[x & 63];
262 }
263 if (i + 1 == U.size()) {
264 uint32_t x = (U[i] << 16);
265 Res += Table[(x >> 18) & 63];
266 Res += Table[(x >> 12) & 63];
267 Res += "==";
268 } else if (i + 2 == U.size()) {
269 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
270 Res += Table[(x >> 18) & 63];
271 Res += Table[(x >> 12) & 63];
272 Res += Table[(x >> 6) & 63];
273 Res += "=";
274 }
275 return Res;
276}
277
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000278size_t GetPeakRSSMb() {
279 struct rusage usage;
280 if (getrusage(RUSAGE_SELF, &usage))
281 return 0;
Dan Liew11565442016-05-20 01:37:54 +0000282 if (LIBFUZZER_LINUX) {
283 // ru_maxrss is in KiB
284 return usage.ru_maxrss >> 10;
285 } else if (LIBFUZZER_APPLE) {
286 // ru_maxrss is in bytes
287 return usage.ru_maxrss >> 20;
288 }
289 assert(0 && "GetPeakRSSMb() is not implemented for your platform");
290 return 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000291}
292
Kostya Serebryanyb706b482016-09-18 21:47:08 +0000293void PrintPC(const char *SymbolizedFMT, const char *FallbackFMT, uintptr_t PC) {
294 if (EF->__sanitizer_symbolize_pc) {
295 char PcDescr[1024];
296 EF->__sanitizer_symbolize_pc(reinterpret_cast<void*>(PC),
297 SymbolizedFMT, PcDescr, sizeof(PcDescr));
298 PcDescr[sizeof(PcDescr) - 1] = 0; // Just in case.
299 Printf("%s", PcDescr);
300 } else {
301 Printf(FallbackFMT, PC);
302 }
303}
304
Aaron Ballmanef116982015-01-29 16:58:29 +0000305} // namespace fuzzer