blob: 9364955b3052795ca34e658985ccb76c4e37f5dc [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>
17#include <cassert>
18#include <cstring>
19#include <signal.h>
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000020#include <sstream>
Kostya Serebryanyf47198a2015-05-12 22:03:34 +000021#include <unistd.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000022
23namespace fuzzer {
24
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000025void PrintHexArray(const uint8_t *Data, size_t Size,
26 const char *PrintAfter) {
27 for (size_t i = 0; i < Size; i++)
28 Printf("0x%x,", (unsigned)Data[i]);
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000029 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000030}
31
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000032void Print(const Unit &v, const char *PrintAfter) {
33 PrintHexArray(v.data(), v.size(), PrintAfter);
34}
35
Kostya Serebryany41740052016-01-12 02:36:59 +000036void PrintASCIIByte(uint8_t Byte) {
37 if (Byte == '\\')
38 Printf("\\\\");
39 else if (Byte == '"')
40 Printf("\\\"");
41 else if (Byte >= 32 && Byte < 127)
42 Printf("%c", Byte);
43 else
44 Printf("\\x%02x", Byte);
45}
46
47void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
48 for (size_t i = 0; i < Size; i++)
49 PrintASCIIByte(Data[i]);
50 Printf("%s", PrintAfter);
51}
52
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000053void PrintASCII(const Word &W, const char *PrintAfter) {
54 PrintASCII(W.data(), W.size(), PrintAfter);
55}
56
Aaron Ballmanef116982015-01-29 16:58:29 +000057void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000058 PrintASCII(U.data(), U.size(), PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000059}
60
Kostya Serebryany96eab652015-05-14 22:41:49 +000061std::string Hash(const Unit &U) {
62 uint8_t Hash[kSHA1NumBytes];
63 ComputeSHA1(U.data(), U.size(), Hash);
64 std::stringstream SS;
65 for (int i = 0; i < kSHA1NumBytes; i++)
66 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
67 return SS.str();
Aaron Ballmanef116982015-01-29 16:58:29 +000068}
69
70static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000071 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000072}
73
Kostya Serebryany228d5b12016-03-01 22:19:21 +000074static void CrashHandler(int, siginfo_t *, void *) {
75 Fuzzer::StaticCrashSignalCallback();
76}
77
78static void InterruptHandler(int, siginfo_t *, void *) {
79 Fuzzer::StaticInterruptCallback();
80}
81
82static void SetSigaction(int signum,
83 void (*callback)(int, siginfo_t *, void *)) {
84 struct sigaction sigact;
85 memset(&sigact, 0, sizeof(sigact));
86 sigact.sa_sigaction = callback;
87 int Res = sigaction(signum, &sigact, 0);
88 assert(Res == 0);
89}
90
Aaron Ballmanef116982015-01-29 16:58:29 +000091void SetTimer(int Seconds) {
92 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Aaron Ballmanef116982015-01-29 16:58:29 +000093 int Res = setitimer(ITIMER_REAL, &T, nullptr);
94 assert(Res == 0);
Kostya Serebryany228d5b12016-03-01 22:19:21 +000095 SetSigaction(SIGALRM, AlarmHandler);
Aaron Ballmanef116982015-01-29 16:58:29 +000096}
97
Kostya Serebryany228d5b12016-03-01 22:19:21 +000098void SetSigSegvHandler() { SetSigaction(SIGSEGV, CrashHandler); }
99void SetSigBusHandler() { SetSigaction(SIGBUS, CrashHandler); }
100void SetSigAbrtHandler() { SetSigaction(SIGABRT, CrashHandler); }
101void SetSigIllHandler() { SetSigaction(SIGILL, CrashHandler); }
102void SetSigFpeHandler() { SetSigaction(SIGFPE, CrashHandler); }
103void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
104
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000105int NumberOfCpuCores() {
106 FILE *F = popen("nproc", "r");
107 int N = 0;
108 fscanf(F, "%d", &N);
109 fclose(F);
110 return N;
111}
112
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000113int ExecuteCommand(const std::string &Command) {
114 return system(Command.c_str());
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000115}
116
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000117bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000118 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000119 for (size_t i = 0; i < Size; i++) {
120 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000121 auto NewX = X;
122 NewX &= 127;
123 if (!isspace(NewX) && !isprint(NewX))
124 NewX = ' ';
125 Changed |= NewX != X;
126 X = NewX;
127 }
128 return Changed;
129}
130
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000131bool IsASCII(const Unit &U) {
132 for (auto X : U)
133 if (!(isprint(X) || isspace(X))) return false;
134 return true;
135}
136
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000137bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
138 U->clear();
139 if (Str.empty()) return false;
140 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
141 // Skip spaces from both sides.
142 while (L < R && isspace(Str[L])) L++;
143 while (R > L && isspace(Str[R])) R--;
144 if (R - L < 2) return false;
145 // Check the closing "
146 if (Str[R] != '"') return false;
147 R--;
148 // Find the opening "
149 while (L < R && Str[L] != '"') L++;
150 if (L >= R) return false;
151 assert(Str[L] == '\"');
152 L++;
153 assert(L <= R);
154 for (size_t Pos = L; Pos <= R; Pos++) {
155 uint8_t V = (uint8_t)Str[Pos];
156 if (!isprint(V) && !isspace(V)) return false;
157 if (V =='\\') {
158 // Handle '\\'
159 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
160 U->push_back(Str[Pos + 1]);
161 Pos++;
162 continue;
163 }
164 // Handle '\xAB'
165 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
166 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
167 char Hex[] = "0xAA";
168 Hex[2] = Str[Pos + 2];
169 Hex[3] = Str[Pos + 3];
170 U->push_back(strtol(Hex, nullptr, 16));
171 Pos += 3;
172 continue;
173 }
174 return false; // Invalid escape.
175 } else {
176 // Any other character.
177 U->push_back(V);
178 }
179 }
180 return true;
181}
182
183bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
184 if (Text.empty()) {
185 Printf("ParseDictionaryFile: file does not exist or is empty\n");
186 return false;
187 }
188 std::istringstream ISS(Text);
189 Units->clear();
190 Unit U;
191 int LineNo = 0;
192 std::string S;
193 while (std::getline(ISS, S, '\n')) {
194 LineNo++;
195 size_t Pos = 0;
196 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
197 if (Pos == S.size()) continue; // Empty line.
198 if (S[Pos] == '#') continue; // Comment line.
199 if (ParseOneDictionaryEntry(S, &U)) {
200 Units->push_back(U);
201 } else {
202 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
203 S.c_str());
204 return false;
205 }
206 }
207 return true;
208}
209
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000210int GetPid() { return getpid(); }
211
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000212
213std::string Base64(const Unit &U) {
214 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
215 "abcdefghijklmnopqrstuvwxyz"
216 "0123456789+/";
217 std::string Res;
218 size_t i;
219 for (i = 0; i + 2 < U.size(); i += 3) {
220 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
221 Res += Table[(x >> 18) & 63];
222 Res += Table[(x >> 12) & 63];
223 Res += Table[(x >> 6) & 63];
224 Res += Table[x & 63];
225 }
226 if (i + 1 == U.size()) {
227 uint32_t x = (U[i] << 16);
228 Res += Table[(x >> 18) & 63];
229 Res += Table[(x >> 12) & 63];
230 Res += "==";
231 } else if (i + 2 == U.size()) {
232 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
233 Res += Table[(x >> 18) & 63];
234 Res += Table[(x >> 12) & 63];
235 Res += Table[(x >> 6) & 63];
236 Res += "=";
237 }
238 return Res;
239}
240
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000241size_t GetPeakRSSMb() {
242 struct rusage usage;
243 if (getrusage(RUSAGE_SELF, &usage))
244 return 0;
245 return usage.ru_maxrss >> 10;
246}
247
Aaron Ballmanef116982015-01-29 16:58:29 +0000248} // namespace fuzzer