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> |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 15 | #include <sys/resource.h> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 16 | #include <sys/time.h> |
| 17 | #include <cassert> |
| 18 | #include <cstring> |
| 19 | #include <signal.h> |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame] | 20 | #include <sstream> |
Kostya Serebryany | f47198a | 2015-05-12 22:03:34 +0000 | [diff] [blame] | 21 | #include <unistd.h> |
Dmitry Vyukov | 2eed121 | 2016-03-02 09:54:40 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 23 | |
| 24 | namespace fuzzer { |
| 25 | |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 26 | void PrintHexArray(const uint8_t *Data, size_t Size, |
| 27 | const char *PrintAfter) { |
| 28 | for (size_t i = 0; i < Size; i++) |
| 29 | Printf("0x%x,", (unsigned)Data[i]); |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 30 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Kostya Serebryany | 98abb2c | 2016-01-13 23:46:01 +0000 | [diff] [blame] | 33 | void Print(const Unit &v, const char *PrintAfter) { |
| 34 | PrintHexArray(v.data(), v.size(), PrintAfter); |
| 35 | } |
| 36 | |
Kostya Serebryany | 4174005 | 2016-01-12 02:36:59 +0000 | [diff] [blame] | 37 | void PrintASCIIByte(uint8_t Byte) { |
| 38 | if (Byte == '\\') |
| 39 | Printf("\\\\"); |
| 40 | else if (Byte == '"') |
| 41 | Printf("\\\""); |
| 42 | else if (Byte >= 32 && Byte < 127) |
| 43 | Printf("%c", Byte); |
| 44 | else |
| 45 | Printf("\\x%02x", Byte); |
| 46 | } |
| 47 | |
| 48 | void PrintASCII(const uint8_t *Data, size_t Size, const char *PrintAfter) { |
| 49 | for (size_t i = 0; i < Size; i++) |
| 50 | PrintASCIIByte(Data[i]); |
| 51 | Printf("%s", PrintAfter); |
| 52 | } |
| 53 | |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 54 | void PrintASCII(const Word &W, const char *PrintAfter) { |
| 55 | PrintASCII(W.data(), W.size(), PrintAfter); |
| 56 | } |
| 57 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 58 | void PrintASCII(const Unit &U, const char *PrintAfter) { |
Kostya Serebryany | 476f0ce | 2016-01-16 03:53:32 +0000 | [diff] [blame] | 59 | PrintASCII(U.data(), U.size(), PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Kostya Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 62 | std::string Hash(const Unit &U) { |
| 63 | uint8_t Hash[kSHA1NumBytes]; |
| 64 | ComputeSHA1(U.data(), U.size(), Hash); |
| 65 | std::stringstream SS; |
| 66 | for (int i = 0; i < kSHA1NumBytes; i++) |
| 67 | SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i]; |
| 68 | return SS.str(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void AlarmHandler(int, siginfo_t *, void *) { |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 72 | Fuzzer::StaticAlarmCallback(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 75 | static void CrashHandler(int, siginfo_t *, void *) { |
| 76 | Fuzzer::StaticCrashSignalCallback(); |
| 77 | } |
| 78 | |
| 79 | static void InterruptHandler(int, siginfo_t *, void *) { |
| 80 | Fuzzer::StaticInterruptCallback(); |
| 81 | } |
| 82 | |
| 83 | static void SetSigaction(int signum, |
| 84 | void (*callback)(int, siginfo_t *, void *)) { |
| 85 | struct sigaction sigact; |
| 86 | memset(&sigact, 0, sizeof(sigact)); |
| 87 | sigact.sa_sigaction = callback; |
Dmitry Vyukov | 2eed121 | 2016-03-02 09:54:40 +0000 | [diff] [blame] | 88 | if (sigaction(signum, &sigact, 0)) { |
| 89 | Printf("libFuzzer: sigaction failed with %d\n", errno); |
| 90 | exit(1); |
| 91 | } |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 94 | void SetTimer(int Seconds) { |
| 95 | struct itimerval T {{Seconds, 0}, {Seconds, 0}}; |
Dmitry Vyukov | 2eed121 | 2016-03-02 09:54:40 +0000 | [diff] [blame] | 96 | if (setitimer(ITIMER_REAL, &T, nullptr)) { |
| 97 | Printf("libFuzzer: setitimer failed with %d\n", errno); |
| 98 | exit(1); |
| 99 | } |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 100 | SetSigaction(SIGALRM, AlarmHandler); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 103 | void SetSigSegvHandler() { SetSigaction(SIGSEGV, CrashHandler); } |
| 104 | void SetSigBusHandler() { SetSigaction(SIGBUS, CrashHandler); } |
| 105 | void SetSigAbrtHandler() { SetSigaction(SIGABRT, CrashHandler); } |
| 106 | void SetSigIllHandler() { SetSigaction(SIGILL, CrashHandler); } |
| 107 | void SetSigFpeHandler() { SetSigaction(SIGFPE, CrashHandler); } |
| 108 | void SetSigIntHandler() { SetSigaction(SIGINT, InterruptHandler); } |
Kostya Serebryany | f389ae1 | 2016-03-24 21:03:58 +0000 | [diff] [blame^] | 109 | void SetSigTermHandler() { SetSigaction(SIGTERM, InterruptHandler); } |
Kostya Serebryany | 228d5b1 | 2016-03-01 22:19:21 +0000 | [diff] [blame] | 110 | |
Kostya Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 111 | int NumberOfCpuCores() { |
| 112 | FILE *F = popen("nproc", "r"); |
| 113 | int N = 0; |
Dmitry Vyukov | 2eed121 | 2016-03-02 09:54:40 +0000 | [diff] [blame] | 114 | if (fscanf(F, "%d", &N) != 1) |
| 115 | N = 1; |
Kostya Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 116 | fclose(F); |
| 117 | return N; |
| 118 | } |
| 119 | |
Kostya Serebryany | dc3135d | 2015-11-12 01:02:01 +0000 | [diff] [blame] | 120 | int ExecuteCommand(const std::string &Command) { |
| 121 | return system(Command.c_str()); |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 124 | bool ToASCII(uint8_t *Data, size_t Size) { |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 125 | bool Changed = false; |
Kostya Serebryany | 8a5bef0 | 2016-02-13 17:56:51 +0000 | [diff] [blame] | 126 | for (size_t i = 0; i < Size; i++) { |
| 127 | uint8_t &X = Data[i]; |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 128 | auto NewX = X; |
| 129 | NewX &= 127; |
| 130 | if (!isspace(NewX) && !isprint(NewX)) |
| 131 | NewX = ' '; |
| 132 | Changed |= NewX != X; |
| 133 | X = NewX; |
| 134 | } |
| 135 | return Changed; |
| 136 | } |
| 137 | |
Kostya Serebryany | a9346c2 | 2015-09-02 19:08:08 +0000 | [diff] [blame] | 138 | bool IsASCII(const Unit &U) { |
| 139 | for (auto X : U) |
| 140 | if (!(isprint(X) || isspace(X))) return false; |
| 141 | return true; |
| 142 | } |
| 143 | |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame] | 144 | bool ParseOneDictionaryEntry(const std::string &Str, Unit *U) { |
| 145 | U->clear(); |
| 146 | if (Str.empty()) return false; |
| 147 | size_t L = 0, R = Str.size() - 1; // We are parsing the range [L,R]. |
| 148 | // Skip spaces from both sides. |
| 149 | while (L < R && isspace(Str[L])) L++; |
| 150 | while (R > L && isspace(Str[R])) R--; |
| 151 | if (R - L < 2) return false; |
| 152 | // Check the closing " |
| 153 | if (Str[R] != '"') return false; |
| 154 | R--; |
| 155 | // Find the opening " |
| 156 | while (L < R && Str[L] != '"') L++; |
| 157 | if (L >= R) return false; |
| 158 | assert(Str[L] == '\"'); |
| 159 | L++; |
| 160 | assert(L <= R); |
| 161 | for (size_t Pos = L; Pos <= R; Pos++) { |
| 162 | uint8_t V = (uint8_t)Str[Pos]; |
| 163 | if (!isprint(V) && !isspace(V)) return false; |
| 164 | if (V =='\\') { |
| 165 | // Handle '\\' |
| 166 | if (Pos + 1 <= R && (Str[Pos + 1] == '\\' || Str[Pos + 1] == '"')) { |
| 167 | U->push_back(Str[Pos + 1]); |
| 168 | Pos++; |
| 169 | continue; |
| 170 | } |
| 171 | // Handle '\xAB' |
| 172 | if (Pos + 3 <= R && Str[Pos + 1] == 'x' |
| 173 | && isxdigit(Str[Pos + 2]) && isxdigit(Str[Pos + 3])) { |
| 174 | char Hex[] = "0xAA"; |
| 175 | Hex[2] = Str[Pos + 2]; |
| 176 | Hex[3] = Str[Pos + 3]; |
| 177 | U->push_back(strtol(Hex, nullptr, 16)); |
| 178 | Pos += 3; |
| 179 | continue; |
| 180 | } |
| 181 | return false; // Invalid escape. |
| 182 | } else { |
| 183 | // Any other character. |
| 184 | U->push_back(V); |
| 185 | } |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | |
| 190 | bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units) { |
| 191 | if (Text.empty()) { |
| 192 | Printf("ParseDictionaryFile: file does not exist or is empty\n"); |
| 193 | return false; |
| 194 | } |
| 195 | std::istringstream ISS(Text); |
| 196 | Units->clear(); |
| 197 | Unit U; |
| 198 | int LineNo = 0; |
| 199 | std::string S; |
| 200 | while (std::getline(ISS, S, '\n')) { |
| 201 | LineNo++; |
| 202 | size_t Pos = 0; |
| 203 | while (Pos < S.size() && isspace(S[Pos])) Pos++; // Skip spaces. |
| 204 | if (Pos == S.size()) continue; // Empty line. |
| 205 | if (S[Pos] == '#') continue; // Comment line. |
| 206 | if (ParseOneDictionaryEntry(S, &U)) { |
| 207 | Units->push_back(U); |
| 208 | } else { |
| 209 | Printf("ParseDictionaryFile: error in line %d\n\t\t%s\n", LineNo, |
| 210 | S.c_str()); |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
Kostya Serebryany | d6edce9 | 2015-10-16 23:04:31 +0000 | [diff] [blame] | 217 | int GetPid() { return getpid(); } |
| 218 | |
Kostya Serebryany | 9e48cda | 2015-12-04 22:29:39 +0000 | [diff] [blame] | 219 | |
| 220 | std::string Base64(const Unit &U) { |
| 221 | static const char Table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 222 | "abcdefghijklmnopqrstuvwxyz" |
| 223 | "0123456789+/"; |
| 224 | std::string Res; |
| 225 | size_t i; |
| 226 | for (i = 0; i + 2 < U.size(); i += 3) { |
| 227 | uint32_t x = (U[i] << 16) + (U[i + 1] << 8) + U[i + 2]; |
| 228 | Res += Table[(x >> 18) & 63]; |
| 229 | Res += Table[(x >> 12) & 63]; |
| 230 | Res += Table[(x >> 6) & 63]; |
| 231 | Res += Table[x & 63]; |
| 232 | } |
| 233 | if (i + 1 == U.size()) { |
| 234 | uint32_t x = (U[i] << 16); |
| 235 | Res += Table[(x >> 18) & 63]; |
| 236 | Res += Table[(x >> 12) & 63]; |
| 237 | Res += "=="; |
| 238 | } else if (i + 2 == U.size()) { |
| 239 | uint32_t x = (U[i] << 16) + (U[i + 1] << 8); |
| 240 | Res += Table[(x >> 18) & 63]; |
| 241 | Res += Table[(x >> 12) & 63]; |
| 242 | Res += Table[(x >> 6) & 63]; |
| 243 | Res += "="; |
| 244 | } |
| 245 | return Res; |
| 246 | } |
| 247 | |
Kostya Serebryany | 66ff075 | 2016-02-26 22:42:23 +0000 | [diff] [blame] | 248 | size_t GetPeakRSSMb() { |
| 249 | struct rusage usage; |
| 250 | if (getrusage(RUSAGE_SELF, &usage)) |
| 251 | return 0; |
| 252 | return usage.ru_maxrss >> 10; |
| 253 | } |
| 254 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 255 | } // namespace fuzzer |