blob: 1d51cf91224b4835a079c2f58634b47bf01d1eb5 [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
74void SetTimer(int Seconds) {
75 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Aaron Ballmanef116982015-01-29 16:58:29 +000076 int Res = setitimer(ITIMER_REAL, &T, nullptr);
77 assert(Res == 0);
78 struct sigaction sigact;
79 memset(&sigact, 0, sizeof(sigact));
80 sigact.sa_sigaction = AlarmHandler;
81 Res = sigaction(SIGALRM, &sigact, 0);
82 assert(Res == 0);
83}
84
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000085int NumberOfCpuCores() {
86 FILE *F = popen("nproc", "r");
87 int N = 0;
88 fscanf(F, "%d", &N);
89 fclose(F);
90 return N;
91}
92
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000093int ExecuteCommand(const std::string &Command) {
94 return system(Command.c_str());
Kostya Serebryany2da7b842015-05-18 21:34:20 +000095}
96
Kostya Serebryany8a5bef02016-02-13 17:56:51 +000097bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000098 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +000099 for (size_t i = 0; i < Size; i++) {
100 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000101 auto NewX = X;
102 NewX &= 127;
103 if (!isspace(NewX) && !isprint(NewX))
104 NewX = ' ';
105 Changed |= NewX != X;
106 X = NewX;
107 }
108 return Changed;
109}
110
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000111bool IsASCII(const Unit &U) {
112 for (auto X : U)
113 if (!(isprint(X) || isspace(X))) return false;
114 return true;
115}
116
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000117bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
118 U->clear();
119 if (Str.empty()) return false;
120 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
121 // Skip spaces from both sides.
122 while (L < R && isspace(Str[L])) L++;
123 while (R > L && isspace(Str[R])) R--;
124 if (R - L < 2) return false;
125 // Check the closing "
126 if (Str[R] != '"') return false;
127 R--;
128 // Find the opening "
129 while (L < R && Str[L] != '"') L++;
130 if (L >= R) return false;
131 assert(Str[L] == '\"');
132 L++;
133 assert(L <= R);
134 for (size_t Pos = L; Pos <= R; Pos++) {
135 uint8_t V = (uint8_t)Str[Pos];
136 if (!isprint(V) && !isspace(V)) return false;
137 if (V =='\\') {
138 // Handle '\\'
139 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
140 U->push_back(Str[Pos + 1]);
141 Pos++;
142 continue;
143 }
144 // Handle '\xAB'
145 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
146 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
147 char Hex[] = "0xAA";
148 Hex[2] = Str[Pos + 2];
149 Hex[3] = Str[Pos + 3];
150 U->push_back(strtol(Hex, nullptr, 16));
151 Pos += 3;
152 continue;
153 }
154 return false; // Invalid escape.
155 } else {
156 // Any other character.
157 U->push_back(V);
158 }
159 }
160 return true;
161}
162
163bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
164 if (Text.empty()) {
165 Printf("ParseDictionaryFile: file does not exist or is empty\n");
166 return false;
167 }
168 std::istringstream ISS(Text);
169 Units->clear();
170 Unit U;
171 int LineNo = 0;
172 std::string S;
173 while (std::getline(ISS, S, '\n')) {
174 LineNo++;
175 size_t Pos = 0;
176 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
177 if (Pos == S.size()) continue; // Empty line.
178 if (S[Pos] == '#') continue; // Comment line.
179 if (ParseOneDictionaryEntry(S, &U)) {
180 Units->push_back(U);
181 } else {
182 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
183 S.c_str());
184 return false;
185 }
186 }
187 return true;
188}
189
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000190int GetPid() { return getpid(); }
191
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000192
193std::string Base64(const Unit &U) {
194 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
195 "abcdefghijklmnopqrstuvwxyz"
196 "0123456789+/";
197 std::string Res;
198 size_t i;
199 for (i = 0; i + 2 < U.size(); i += 3) {
200 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
201 Res += Table[(x >> 18) & 63];
202 Res += Table[(x >> 12) & 63];
203 Res += Table[(x >> 6) & 63];
204 Res += Table[x & 63];
205 }
206 if (i + 1 == U.size()) {
207 uint32_t x = (U[i] << 16);
208 Res += Table[(x >> 18) & 63];
209 Res += Table[(x >> 12) & 63];
210 Res += "==";
211 } else if (i + 2 == U.size()) {
212 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
213 Res += Table[(x >> 18) & 63];
214 Res += Table[(x >> 12) & 63];
215 Res += Table[(x >> 6) & 63];
216 Res += "=";
217 }
218 return Res;
219}
220
Kostya Serebryany66ff0752016-02-26 22:42:23 +0000221size_t GetPeakRSSMb() {
222 struct rusage usage;
223 if (getrusage(RUSAGE_SELF, &usage))
224 return 0;
225 return usage.ru_maxrss >> 10;
226}
227
Aaron Ballmanef116982015-01-29 16:58:29 +0000228} // namespace fuzzer