blob: d5335616a83b14c3bc1fe3b6b9a8f94050e3ec52 [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>
Dmitry Vyukov2eed1212016-03-02 09:54:40 +000022#include <errno.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000023
24namespace fuzzer {
25
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000026void PrintHexArray(const uint8_t *Data, size_t Size,
27 const char *PrintAfter) {
28 for (size_t i = 0; i < Size; i++)
29 Printf("0x%x,", (unsigned)Data[i]);
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000030 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000031}
32
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000033void Print(const Unit &v, const char *PrintAfter) {
34 PrintHexArray(v.data(), v.size(), PrintAfter);
35}
36
Kostya Serebryany41740052016-01-12 02:36:59 +000037void PrintASCIIByte(uint8_t Byte) {
38 if (Byte == '\\')
39 Printf("\\\\");
40 else if (Byte == '"')
41 Printf("\\\"");
42 else if (Byte >= 32 && Byte < 127)
43 Printf("%c", Byte);
44 else
45 Printf("\\x%02x", Byte);
46}
47
48void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
49 for (size_t i = 0; i < Size; i++)
50 PrintASCIIByte(Data[i]);
51 Printf("%s", PrintAfter);
52}
53
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000054void PrintASCII(const Word &W, const char *PrintAfter) {
55 PrintASCII(W.data(), W.size(), 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 Serebryany96eab652015-05-14 22:41:49 +000062std::string Hash(const Unit &U) {
63 uint8_t Hash[kSHA1NumBytes];
64 ComputeSHA1(U.data(), U.size(), Hash);
65 std::stringstream SS;
66 for (int i = 0; i < kSHA1NumBytes; i++)
67 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
68 return SS.str();
Aaron Ballmanef116982015-01-29 16:58:29 +000069}
70
71static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000072 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000073}
74
Kostya Serebryany228d5b12016-03-01 22:19:21 +000075static void CrashHandler(int, siginfo_t *, void *) {
76 Fuzzer::StaticCrashSignalCallback();
77}
78
79static void InterruptHandler(int, siginfo_t *, void *) {
80 Fuzzer::StaticInterruptCallback();
81}
82
83static void SetSigaction(int signum,
84 void (*callback)(int, siginfo_t *, void *)) {
85 struct sigaction sigact;
86 memset(&sigact, 0, sizeof(sigact));
87 sigact.sa_sigaction = callback;
Dmitry Vyukov2eed1212016-03-02 09:54:40 +000088 if (sigaction(signum, &sigact, 0)) {
89 Printf("libFuzzer: sigaction failed with %d\n", errno);
90 exit(1);
91 }
Kostya Serebryany228d5b12016-03-01 22:19:21 +000092}
93
Aaron Ballmanef116982015-01-29 16:58:29 +000094void SetTimer(int Seconds) {
95 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Dmitry Vyukov2eed1212016-03-02 09:54:40 +000096 if (setitimer(ITIMER_REAL, &T, nullptr)) {
97 Printf("libFuzzer: setitimer failed with %d\n", errno);
98 exit(1);
99 }
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000100 SetSigaction(SIGALRM, AlarmHandler);
Aaron Ballmanef116982015-01-29 16:58:29 +0000101}
102
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000103void SetSigSegvHandler() { SetSigaction(SIGSEGV, CrashHandler); }
104void SetSigBusHandler() { SetSigaction(SIGBUS, CrashHandler); }
105void SetSigAbrtHandler() { SetSigaction(SIGABRT, CrashHandler); }
106void SetSigIllHandler() { SetSigaction(SIGILL, CrashHandler); }
107void SetSigFpeHandler() { SetSigaction(SIGFPE, CrashHandler); }
108void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); }
Kostya Serebryanyf389ae12016-03-24 21:03:58 +0000109void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); }
Kostya Serebryany228d5b12016-03-01 22:19:21 +0000110
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000111int NumberOfCpuCores() {
112 FILE *F = popen("nproc", "r");
113 int N = 0;
Dmitry Vyukov2eed1212016-03-02 09:54:40 +0000114 if (fscanf(F, "%d", &N) != 1)
115 N = 1;
Kostya Serebryany9690fcf2015-05-12 18:51:57 +0000116 fclose(F);
117 return N;
118}
119
Kostya Serebryanydc3135d2015-11-12 01:02:01 +0000120int ExecuteCommand(const std::string &Command) {
121 return system(Command.c_str());
Kostya Serebryany2da7b842015-05-18 21:34:20 +0000122}
123
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000124bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000125 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +0000126 for (size_t i = 0; i < Size; i++) {
127 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000128 auto NewX = X;
129 NewX &= 127;
130 if (!isspace(NewX) && !isprint(NewX))
131 NewX = ' ';
132 Changed |= NewX != X;
133 X = NewX;
134 }
135 return Changed;
136}
137
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000138bool IsASCII(const Unit &U) {
139 for (auto X : U)
140 if (!(isprint(X) || isspace(X))) return false;
141 return true;
142}
143
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000144bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
145 U->clear();
146 if (Str.empty()) return false;
147 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
148 // Skip spaces from both sides.
149 while (L < R && isspace(Str[L])) L++;
150 while (R > L && isspace(Str[R])) R--;
151 if (R - L < 2) return false;
152 // Check the closing "
153 if (Str[R] != '"') return false;
154 R--;
155 // Find the opening "
156 while (L < R && Str[L] != '"') L++;
157 if (L >= R) return false;
158 assert(Str[L] == '\"');
159 L++;
160 assert(L <= R);
161 for (size_t Pos = L; Pos <= R; Pos++) {
162 uint8_t V = (uint8_t)Str[Pos];
163 if (!isprint(V) && !isspace(V)) return false;
164 if (V =='\\') {
165 // Handle '\\'
166 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
167 U->push_back(Str[Pos + 1]);
168 Pos++;
169 continue;
170 }
171 // Handle '\xAB'
172 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
173 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
174 char Hex[] = "0xAA";
175 Hex[2] = Str[Pos + 2];
176 Hex[3] = Str[Pos + 3];
177 U->push_back(strtol(Hex, nullptr, 16));
178 Pos += 3;
179 continue;
180 }
181 return false; // Invalid escape.
182 } else {
183 // Any other character.
184 U->push_back(V);
185 }
186 }
187 return true;
188}
189
190bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
191 if (Text.empty()) {
192 Printf("ParseDictionaryFile: file does not exist or is empty\n");
193 return false;
194 }
195 std::istringstream ISS(Text);
196 Units->clear();
197 Unit U;
198 int LineNo = 0;
199 std::string S;
200 while (std::getline(ISS, S, '\n')) {
201 LineNo++;
202 size_t Pos = 0;
203 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
204 if (Pos == S.size()) continue; // Empty line.
205 if (S[Pos] == '#') continue; // Comment line.
206 if (ParseOneDictionaryEntry(S, &U)) {
207 Units->push_back(U);
208 } else {
209 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
210 S.c_str());
211 return false;
212 }
213 }
214 return true;
215}
216
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000217int GetPid() { return getpid(); }
218
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000219
220std::string Base64(const Unit &U) {
221 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
222 "abcdefghijklmnopqrstuvwxyz"
223 "0123456789+/";
224 std::string Res;
225 size_t i;
226 for (i = 0; i + 2 < U.size(); i += 3) {
227 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
228 Res += Table[(x >> 18) & 63];
229 Res += Table[(x >> 12) & 63];
230 Res += Table[(x >> 6) & 63];
231 Res += Table[x & 63];
232 }
233 if (i + 1 == U.size()) {
234 uint32_t x = (U[i] << 16);
235 Res += Table[(x >> 18) & 63];
236 Res += Table[(x >> 12) & 63];
237 Res += "==";
238 } else if (i + 2 == U.size()) {
239 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
240 Res += Table[(x >> 18) & 63];
241 Res += Table[(x >> 12) & 63];
242 Res += Table[(x >> 6) & 63];
243 Res += "=";
244 }
245 return Res;
246}
247
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000248size_t GetPeakRSSMb() {
249 struct rusage usage;
250 if (getrusage(RUSAGE_SELF, &usage))
251 return 0;
252 return usage.ru_maxrss >> 10;
253}
254
Aaron Ballmanef116982015-01-29 16:58:29 +0000255} // namespace fuzzer