blob: 81462a456e135a383bdd70392803eba6dab46c15 [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>
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <sys/time.h>
16#include <cassert>
17#include <cstring>
18#include <signal.h>
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000019#include <sstream>
Kostya Serebryanyf47198a2015-05-12 22:03:34 +000020#include <unistd.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000021
22namespace fuzzer {
23
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000024void PrintHexArray(const uint8_t *Data, size_t Size,
25 const char *PrintAfter) {
26 for (size_t i = 0; i < Size; i++)
27 Printf("0x%x,", (unsigned)Data[i]);
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000028 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000029}
30
Kostya Serebryany98abb2c2016-01-13 23:46:01 +000031void Print(const Unit &v, const char *PrintAfter) {
32 PrintHexArray(v.data(), v.size(), PrintAfter);
33}
34
Kostya Serebryany41740052016-01-12 02:36:59 +000035void PrintASCIIByte(uint8_t Byte) {
36 if (Byte == '\\')
37 Printf("\\\\");
38 else if (Byte == '"')
39 Printf("\\\"");
40 else if (Byte >= 32 && Byte < 127)
41 Printf("%c", Byte);
42 else
43 Printf("\\x%02x", Byte);
44}
45
46void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
47 for (size_t i = 0; i < Size; i++)
48 PrintASCIIByte(Data[i]);
49 Printf("%s", PrintAfter);
50}
51
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000052void PrintASCII(const Word &W, const char *PrintAfter) {
53 PrintASCII(W.data(), W.size(), PrintAfter);
54}
55
Aaron Ballmanef116982015-01-29 16:58:29 +000056void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany476f0ce2016-01-16 03:53:32 +000057 PrintASCII(U.data(), U.size(), PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000058}
59
Kostya Serebryany96eab652015-05-14 22:41:49 +000060std::string Hash(const Unit &U) {
61 uint8_t Hash[kSHA1NumBytes];
62 ComputeSHA1(U.data(), U.size(), Hash);
63 std::stringstream SS;
64 for (int i = 0; i < kSHA1NumBytes; i++)
65 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
66 return SS.str();
Aaron Ballmanef116982015-01-29 16:58:29 +000067}
68
69static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000070 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000071}
72
73void SetTimer(int Seconds) {
74 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Aaron Ballmanef116982015-01-29 16:58:29 +000075 int Res = setitimer(ITIMER_REAL, &T, nullptr);
76 assert(Res == 0);
77 struct sigaction sigact;
78 memset(&sigact, 0, sizeof(sigact));
79 sigact.sa_sigaction = AlarmHandler;
80 Res = sigaction(SIGALRM, &sigact, 0);
81 assert(Res == 0);
82}
83
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000084int NumberOfCpuCores() {
85 FILE *F = popen("nproc", "r");
86 int N = 0;
87 fscanf(F, "%d", &N);
88 fclose(F);
89 return N;
90}
91
Kostya Serebryanydc3135d2015-11-12 01:02:01 +000092int ExecuteCommand(const std::string &Command) {
93 return system(Command.c_str());
Kostya Serebryany2da7b842015-05-18 21:34:20 +000094}
95
Kostya Serebryany8a5bef02016-02-13 17:56:51 +000096bool ToASCII(uint8_t *Data, size_t Size) {
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000097 bool Changed = false;
Kostya Serebryany8a5bef02016-02-13 17:56:51 +000098 for (size_t i = 0; i < Size; i++) {
99 uint8_t &X = Data[i];
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +0000100 auto NewX = X;
101 NewX &= 127;
102 if (!isspace(NewX) && !isprint(NewX))
103 NewX = ' ';
104 Changed |= NewX != X;
105 X = NewX;
106 }
107 return Changed;
108}
109
Kostya Serebryanya9346c22015-09-02 19:08:08 +0000110bool IsASCII(const Unit &U) {
111 for (auto X : U)
112 if (!(isprint(X) || isspace(X))) return false;
113 return true;
114}
115
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000116bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
117 U->clear();
118 if (Str.empty()) return false;
119 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
120 // Skip spaces from both sides.
121 while (L < R && isspace(Str[L])) L++;
122 while (R > L && isspace(Str[R])) R--;
123 if (R - L < 2) return false;
124 // Check the closing "
125 if (Str[R] != '"') return false;
126 R--;
127 // Find the opening "
128 while (L < R && Str[L] != '"') L++;
129 if (L >= R) return false;
130 assert(Str[L] == '\"');
131 L++;
132 assert(L <= R);
133 for (size_t Pos = L; Pos <= R; Pos++) {
134 uint8_t V = (uint8_t)Str[Pos];
135 if (!isprint(V) && !isspace(V)) return false;
136 if (V =='\\') {
137 // Handle '\\'
138 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
139 U->push_back(Str[Pos + 1]);
140 Pos++;
141 continue;
142 }
143 // Handle '\xAB'
144 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
145 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
146 char Hex[] = "0xAA";
147 Hex[2] = Str[Pos + 2];
148 Hex[3] = Str[Pos + 3];
149 U->push_back(strtol(Hex, nullptr, 16));
150 Pos += 3;
151 continue;
152 }
153 return false; // Invalid escape.
154 } else {
155 // Any other character.
156 U->push_back(V);
157 }
158 }
159 return true;
160}
161
162bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
163 if (Text.empty()) {
164 Printf("ParseDictionaryFile: file does not exist or is empty\n");
165 return false;
166 }
167 std::istringstream ISS(Text);
168 Units->clear();
169 Unit U;
170 int LineNo = 0;
171 std::string S;
172 while (std::getline(ISS, S, '\n')) {
173 LineNo++;
174 size_t Pos = 0;
175 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
176 if (Pos == S.size()) continue; // Empty line.
177 if (S[Pos] == '#') continue; // Comment line.
178 if (ParseOneDictionaryEntry(S, &U)) {
179 Units->push_back(U);
180 } else {
181 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
182 S.c_str());
183 return false;
184 }
185 }
186 return true;
187}
188
Kostya Serebryanyd6edce92015-10-16 23:04:31 +0000189int GetPid() { return getpid(); }
190
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000191
192std::string Base64(const Unit &U) {
193 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
194 "abcdefghijklmnopqrstuvwxyz"
195 "0123456789+/";
196 std::string Res;
197 size_t i;
198 for (i = 0; i + 2 < U.size(); i += 3) {
199 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
200 Res += Table[(x >> 18) & 63];
201 Res += Table[(x >> 12) & 63];
202 Res += Table[(x >> 6) & 63];
203 Res += Table[x & 63];
204 }
205 if (i + 1 == U.size()) {
206 uint32_t x = (U[i] << 16);
207 Res += Table[(x >> 18) & 63];
208 Res += Table[(x >> 12) & 63];
209 Res += "==";
210 } else if (i + 2 == U.size()) {
211 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
212 Res += Table[(x >> 18) & 63];
213 Res += Table[(x >> 12) & 63];
214 Res += Table[(x >> 6) & 63];
215 Res += "=";
216 }
217 return Res;
218}
219
Aaron Ballmanef116982015-01-29 16:58:29 +0000220} // namespace fuzzer