Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 1 | //===- 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 Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 13 | #include <sstream> |
| 14 | #include <iomanip> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <cassert> |
| 17 | #include <cstring> |
| 18 | #include <signal.h> |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame] | 19 | #include <sstream> |
Kostya Serebryany | f47198a | 2015-05-12 22:03:34 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 21 | |
| 22 | namespace fuzzer { |
| 23 | |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 24 | void 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 Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 28 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 31 | void Print(const Unit &v, const char *PrintAfter) { |
| 32 | PrintHexArray(v.data(), v.size(), PrintAfter); |
| 33 | } |
| 34 | |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 35 | void 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 | |
| 46 | void 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 Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 52 | void PrintASCII(const Word &W, const char *PrintAfter) { |
| 53 | PrintASCII(W.data(), W.size(), PrintAfter); |
| 54 | } |
| 55 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 56 | void PrintASCII(const Unit &U, const char *PrintAfter) { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 57 | PrintASCII(U.data(), U.size(), PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kostya Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 60 | std::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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void AlarmHandler(int, siginfo_t *, void *) { |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 70 | Fuzzer::StaticAlarmCallback(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void SetTimer(int Seconds) { |
| 74 | struct itimerval T {{Seconds, 0}, {Seconds, 0}}; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 75 | 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 Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 84 | int 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 Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 92 | int ExecuteCommand(const std::string &Command) { |
| 93 | return system(Command.c_str()); |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame^] | 96 | bool ToASCII(uint8_t *Data, size_t Size) { |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 97 | bool Changed = false; |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame^] | 98 | for (size_t i = 0; i < Size; i++) { |
| 99 | uint8_t &X = Data[i]; |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 100 | 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 Serebryany | a9346c2 | 2015-09-02 19:08:08 +0000 | [diff] [blame] | 110 | bool IsASCII(const Unit &U) { |
| 111 | for (auto X : U) |
| 112 | if (!(isprint(X) || isspace(X))) return false; |
| 113 | return true; |
| 114 | } |
| 115 | |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame] | 116 | bool 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 | |
| 162 | bool 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 Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 189 | int GetPid() { return getpid(); } |
| 190 | |
Kostya Serebryany | 9e48cda | 2015-12-04 22:29:39 +0000 | [diff] [blame] | 191 | |
| 192 | std::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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 220 | } // namespace fuzzer |