blob: c2ae94c4d3dec8ab82c868991804f560a23a78a8 [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
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000058void PrintASCII(const Word &W, const char *PrintAfter) {
59 PrintASCII(W.data(), W.size(), PrintAfter);
60}
61
Aaron Ballmanef116982015-01-29 16:58:29 +000062void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000063 PrintASCII(U.data(), U.size(), PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000064}
65
Kostya Serebryany96eab652015-05-14 22:41:49 +000066std::string Hash(const Unit &U) {
67 uint8_t Hash[kSHA1NumBytes];
68 ComputeSHA1(U.data(), U.size(), Hash);
69 std::stringstream SS;
70 for (int i = 0; i < kSHA1NumBytes; i++)
71 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
72 return SS.str();
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 Serebryanydc3135d2015-11-12 01:02:01 +0000149int ExecuteCommand(const std::string &Command) {
150 return system(Command.c_str());
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000151}
152
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000153bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000154 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000155 for (size_t i = 0; i < Size; i++) {
156 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000157 auto NewX = X;
158 NewX &= 127;
159 if (!isspace(NewX) && !isprint(NewX))
160 NewX = ' ';
161 Changed |= NewX != X;
162 X = NewX;
163 }
164 return Changed;
165}
166
Kostya Serebryanyf1f3f932016-05-26 20:03:02 +0000167bool IsASCII(const Unit &U) { return IsASCII(U.data(), U.size()); }
168
169bool IsASCII(const uint8_t *Data, size_t Size) {
170 for (size_t i = 0; i < Size; i++)
171 if (!(isprint(Data[i]) || isspace(Data[i]))) return false;
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000172 return true;
173}
174
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000175bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
176 U->clear();
177 if (Str.empty()) return false;
178 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
179 // Skip spaces from both sides.
180 while (L < R && isspace(Str[L])) L++;
181 while (R > L && isspace(Str[R])) R--;
182 if (R - L < 2) return false;
183 // Check the closing "
184 if (Str[R] != '"') return false;
185 R--;
186 // Find the opening "
187 while (L < R && Str[L] != '"') L++;
188 if (L >= R) return false;
189 assert(Str[L] == '\"');
190 L++;
191 assert(L <= R);
192 for (size_t Pos = L; Pos <= R; Pos++) {
193 uint8_t V = (uint8_t)Str[Pos];
194 if (!isprint(V) && !isspace(V)) return false;
195 if (V =='\\') {
196 // Handle '\\'
197 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
198 U->push_back(Str[Pos + 1]);
199 Pos++;
200 continue;
201 }
202 // Handle '\xAB'
203 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
204 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
205 char Hex[] = "0xAA";
206 Hex[2] = Str[Pos + 2];
207 Hex[3] = Str[Pos + 3];
208 U->push_back(strtol(Hex, nullptr, 16));
209 Pos += 3;
210 continue;
211 }
212 return false; // Invalid escape.
213 } else {
214 // Any other character.
215 U->push_back(V);
216 }
217 }
218 return true;
219}
220
221bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
222 if (Text.empty()) {
223 Printf("ParseDictionaryFile: file does not exist or is empty\n");
224 return false;
225 }
226 std::istringstream ISS(Text);
227 Units->clear();
228 Unit U;
229 int LineNo = 0;
230 std::string S;
231 while (std::getline(ISS, S, '\n')) {
232 LineNo++;
233 size_t Pos = 0;
234 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
235 if (Pos == S.size()) continue; // Empty line.
236 if (S[Pos] == '#') continue; // Comment line.
237 if (ParseOneDictionaryEntry(S, &U)) {
238 Units->push_back(U);
239 } else {
240 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
241 S.c_str());
242 return false;
243 }
244 }
245 return true;
246}
247
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000248void SleepSeconds(int Seconds) {
249 std::this_thread::sleep_for(std::chrono::seconds(Seconds));
250}
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000251
Kostya Serebryany8b8f7a32016-05-06 23:38:07 +0000252int GetPid() { return getpid(); }
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000253
254std::string Base64(const Unit &U) {
255 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
256 "abcdefghijklmnopqrstuvwxyz"
257 "0123456789+/";
258 std::string Res;
259 size_t i;
260 for (i = 0; i + 2 < U.size(); i += 3) {
261 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
262 Res += Table[(x >> 18) & 63];
263 Res += Table[(x >> 12) & 63];
264 Res += Table[(x >> 6) & 63];
265 Res += Table[x & 63];
266 }
267 if (i + 1 == U.size()) {
268 uint32_t x = (U[i] << 16);
269 Res += Table[(x >> 18) & 63];
270 Res += Table[(x >> 12) & 63];
271 Res += "==";
272 } else if (i + 2 == U.size()) {
273 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
274 Res += Table[(x >> 18) & 63];
275 Res += Table[(x >> 12) & 63];
276 Res += Table[(x >> 6) & 63];
277 Res += "=";
278 }
279 return Res;
280}
281
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000282size_t GetPeakRSSMb() {
283 struct rusage usage;
284 if (getrusage(RUSAGE_SELF, &usage))
285 return 0;
Dan Liew11565442016-05-20 01:37:54 +0000286 if (LIBFUZZER_LINUX) {
287 // ru_maxrss is in KiB
288 return usage.ru_maxrss >> 10;
289 } else if (LIBFUZZER_APPLE) {
290 // ru_maxrss is in bytes
291 return usage.ru_maxrss >> 20;
292 }
293 assert(0 && "GetPeakRSSMb() is not implemented for your platform");
294 return 0;
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000295}
296
Aaron Ballmanef116982015-01-29 16:58:29 +0000297} // namespace fuzzer