blob: a8856ab3bce8d66f5ea0b6dadd9cb9a397119f2f [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
24void Print(const Unit &v, const char *PrintAfter) {
Aaron Ballmanef116982015-01-29 16:58:29 +000025 for (auto x : v)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000026 Printf("0x%x,", (unsigned) x);
27 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000028}
29
30void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000031 for (auto X : U) {
32 if (isprint(X))
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000033 Printf("%c", X);
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000034 else
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000035 Printf("\\x%x", (unsigned)X);
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000036 }
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000037 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000038}
39
Kostya Serebryany96eab652015-05-14 22:41:49 +000040std::string Hash(const Unit &U) {
41 uint8_t Hash[kSHA1NumBytes];
42 ComputeSHA1(U.data(), U.size(), Hash);
43 std::stringstream SS;
44 for (int i = 0; i < kSHA1NumBytes; i++)
45 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
46 return SS.str();
Aaron Ballmanef116982015-01-29 16:58:29 +000047}
48
49static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000050 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000051}
52
53void SetTimer(int Seconds) {
54 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000055 Printf("SetTimer %d\n", Seconds);
Aaron Ballmanef116982015-01-29 16:58:29 +000056 int Res = setitimer(ITIMER_REAL, &T, nullptr);
57 assert(Res == 0);
58 struct sigaction sigact;
59 memset(&sigact, 0, sizeof(sigact));
60 sigact.sa_sigaction = AlarmHandler;
61 Res = sigaction(SIGALRM, &sigact, 0);
62 assert(Res == 0);
63}
64
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000065int NumberOfCpuCores() {
66 FILE *F = popen("nproc", "r");
67 int N = 0;
68 fscanf(F, "%d", &N);
69 fclose(F);
70 return N;
71}
72
Kostya Serebryany2da7b842015-05-18 21:34:20 +000073void ExecuteCommand(const std::string &Command) {
74 system(Command.c_str());
75}
76
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000077bool ToASCII(Unit &U) {
78 bool Changed = false;
79 for (auto &X : U) {
80 auto NewX = X;
81 NewX &= 127;
82 if (!isspace(NewX) && !isprint(NewX))
83 NewX = ' ';
84 Changed |= NewX != X;
85 X = NewX;
86 }
87 return Changed;
88}
89
Kostya Serebryanya9346c22015-09-02 19:08:08 +000090bool IsASCII(const Unit &U) {
91 for (auto X : U)
92 if (!(isprint(X) || isspace(X))) return false;
93 return true;
94}
95
Kostya Serebryany9838b2b2015-09-03 20:23:46 +000096bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
97 U->clear();
98 if (Str.empty()) return false;
99 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
100 // Skip spaces from both sides.
101 while (L < R && isspace(Str[L])) L++;
102 while (R > L && isspace(Str[R])) R--;
103 if (R - L < 2) return false;
104 // Check the closing "
105 if (Str[R] != '"') return false;
106 R--;
107 // Find the opening "
108 while (L < R && Str[L] != '"') L++;
109 if (L >= R) return false;
110 assert(Str[L] == '\"');
111 L++;
112 assert(L <= R);
113 for (size_t Pos = L; Pos <= R; Pos++) {
114 uint8_t V = (uint8_t)Str[Pos];
115 if (!isprint(V) && !isspace(V)) return false;
116 if (V =='\\') {
117 // Handle '\\'
118 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
119 U->push_back(Str[Pos + 1]);
120 Pos++;
121 continue;
122 }
123 // Handle '\xAB'
124 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
125 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
126 char Hex[] = "0xAA";
127 Hex[2] = Str[Pos + 2];
128 Hex[3] = Str[Pos + 3];
129 U->push_back(strtol(Hex, nullptr, 16));
130 Pos += 3;
131 continue;
132 }
133 return false; // Invalid escape.
134 } else {
135 // Any other character.
136 U->push_back(V);
137 }
138 }
139 return true;
140}
141
142bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
143 if (Text.empty()) {
144 Printf("ParseDictionaryFile: file does not exist or is empty\n");
145 return false;
146 }
147 std::istringstream ISS(Text);
148 Units->clear();
149 Unit U;
150 int LineNo = 0;
151 std::string S;
152 while (std::getline(ISS, S, '\n')) {
153 LineNo++;
154 size_t Pos = 0;
155 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
156 if (Pos == S.size()) continue; // Empty line.
157 if (S[Pos] == '#') continue; // Comment line.
158 if (ParseOneDictionaryEntry(S, &U)) {
159 Units->push_back(U);
160 } else {
161 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
162 S.c_str());
163 return false;
164 }
165 }
166 return true;
167}
168
Aaron Ballmanef116982015-01-29 16:58:29 +0000169} // namespace fuzzer