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 | |
| 24 | void Print(const Unit &v, const char *PrintAfter) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 25 | for (auto x : v) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 26 | Printf("0x%x,", (unsigned) x); |
| 27 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void PrintASCII(const Unit &U, const char *PrintAfter) { |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 31 | for (auto X : U) { |
| 32 | if (isprint(X)) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 33 | Printf("%c", X); |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 34 | else |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 35 | Printf("\\x%x", (unsigned)X); |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 36 | } |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 37 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kostya Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 40 | std::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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static void AlarmHandler(int, siginfo_t *, void *) { |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 50 | Fuzzer::StaticAlarmCallback(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void SetTimer(int Seconds) { |
| 54 | struct itimerval T {{Seconds, 0}, {Seconds, 0}}; |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 55 | Printf("SetTimer %d\n", Seconds); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 56 | 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 Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 65 | int 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 Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 73 | void ExecuteCommand(const std::string &Command) { |
| 74 | system(Command.c_str()); |
| 75 | } |
| 76 | |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 77 | bool 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 Serebryany | a9346c2 | 2015-09-02 19:08:08 +0000 | [diff] [blame] | 90 | bool IsASCII(const Unit &U) { |
| 91 | for (auto X : U) |
| 92 | if (!(isprint(X) || isspace(X))) return false; |
| 93 | return true; |
| 94 | } |
| 95 | |
Kostya Serebryany | 9838b2b | 2015-09-03 20:23:46 +0000 | [diff] [blame^] | 96 | bool 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 | |
| 142 | bool 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 Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 169 | } // namespace fuzzer |