1 //===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "FuzzerInternal.h"
24 void Print(const Unit &v, const char *PrintAfter) {
26 Printf("0x%x,", (unsigned) x);
27 Printf("%s", PrintAfter);
30 void PrintASCIIByte(uint8_t Byte) {
35 else if (Byte >= 32 && Byte < 127)
38 Printf("\\x%02x", Byte);
41 void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) {
42 for (size_t i = 0; i < Size; i++)
43 PrintASCIIByte(Data[i]);
44 Printf("%s", PrintAfter);
47 void PrintASCII(const Unit &U, const char *PrintAfter) {
50 Printf("%s", PrintAfter);
53 std::string Hash(const Unit &U) {
54 uint8_t Hash[kSHA1NumBytes];
55 ComputeSHA1(U.data(), U.size(), Hash);
57 for (int i = 0; i < kSHA1NumBytes; i++)
58 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
62 static void AlarmHandler(int, siginfo_t *, void *) {
63 Fuzzer::StaticAlarmCallback();
66 void SetTimer(int Seconds) {
67 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
68 int Res = setitimer(ITIMER_REAL, &T, nullptr);
70 struct sigaction sigact;
71 memset(&sigact, 0, sizeof(sigact));
72 sigact.sa_sigaction = AlarmHandler;
73 Res = sigaction(SIGALRM, &sigact, 0);
77 int NumberOfCpuCores() {
78 FILE *F = popen("nproc", "r");
85 int ExecuteCommand(const std::string &Command) {
86 return system(Command.c_str());
89 bool ToASCII(Unit &U) {
94 if (!isspace(NewX) && !isprint(NewX))
102 bool IsASCII(const Unit &U) {
104 if (!(isprint(X) || isspace(X))) return false;
108 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) {
110 if (Str.empty()) return false;
111 size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R].
112 // Skip spaces from both sides.
113 while (L < R && isspace(Str[L])) L++;
114 while (R > L && isspace(Str[R])) R--;
115 if (R - L < 2) return false;
116 // Check the closing "
117 if (Str[R] != '"') return false;
119 // Find the opening "
120 while (L < R && Str[L] != '"') L++;
121 if (L >= R) return false;
122 assert(Str[L] == '\"');
125 for (size_t Pos = L; Pos <= R; Pos++) {
126 uint8_t V = (uint8_t)Str[Pos];
127 if (!isprint(V) && !isspace(V)) return false;
130 if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) {
131 U->push_back(Str[Pos + 1]);
136 if (Pos + 3 <= R && Str[Pos + 1] == 'x'
137 && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) {
139 Hex[2] = Str[Pos + 2];
140 Hex[3] = Str[Pos + 3];
141 U->push_back(strtol(Hex, nullptr, 16));
145 return false; // Invalid escape.
147 // Any other character.
154 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) {
156 Printf("ParseDictionaryFile: file does not exist or is empty\n");
159 std::istringstream ISS(Text);
164 while (std::getline(ISS, S, '\n')) {
167 while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces.
168 if (Pos == S.size()) continue; // Empty line.
169 if (S[Pos] == '#') continue; // Comment line.
170 if (ParseOneDictionaryEntry(S, &U)) {
173 Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo,
181 int GetPid() { return getpid(); }
184 std::string Base64(const Unit &U) {
185 static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
186 "abcdefghijklmnopqrstuvwxyz"
190 for (i = 0; i + 2 < U.size(); i += 3) {
191 uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2];
192 Res += Table[(x >> 18) & 63];
193 Res += Table[(x >> 12) & 63];
194 Res += Table[(x >> 6) & 63];
195 Res += Table[x & 63];
197 if (i + 1 == U.size()) {
198 uint32_t x = (U[i] << 16);
199 Res += Table[(x >> 18) & 63];
200 Res += Table[(x >> 12) & 63];
202 } else if (i + 2 == U.size()) {
203 uint32_t x = (U[i] << 16) + (U[i + 1] << 8);
204 Res += Table[(x >> 18) & 63];
205 Res += Table[(x >> 12) & 63];
206 Res += Table[(x >> 6) & 63];
212 } // namespace fuzzer