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 | f47198a | 2015-05-12 22:03:34 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 20 | |
| 21 | namespace fuzzer { |
| 22 | |
| 23 | void Print(const Unit &v, const char *PrintAfter) { |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 24 | for (auto x : v) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 25 | Printf("0x%x,", (unsigned) x); |
| 26 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void PrintASCII(const Unit &U, const char *PrintAfter) { |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 30 | for (auto X : U) { |
| 31 | if (isprint(X)) |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 32 | Printf("%c", X); |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 33 | else |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 34 | Printf("\\x%x", (unsigned)X); |
Kostya Serebryany | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 35 | } |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 36 | Printf("%s", PrintAfter); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Kostya Serebryany | 96eab65 | 2015-05-14 22:41:49 +0000 | [diff] [blame] | 39 | std::string Hash(const Unit &U) { |
| 40 | uint8_t Hash[kSHA1NumBytes]; |
| 41 | ComputeSHA1(U.data(), U.size(), Hash); |
| 42 | std::stringstream SS; |
| 43 | for (int i = 0; i < kSHA1NumBytes; i++) |
| 44 | SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i]; |
| 45 | return SS.str(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void AlarmHandler(int, siginfo_t *, void *) { |
Kostya Serebryany | 52a788e | 2015-03-31 20:13:20 +0000 | [diff] [blame] | 49 | Fuzzer::StaticAlarmCallback(); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void SetTimer(int Seconds) { |
| 53 | struct itimerval T {{Seconds, 0}, {Seconds, 0}}; |
Kostya Serebryany | 7c180ea | 2015-05-23 01:22:35 +0000 | [diff] [blame] | 54 | Printf("SetTimer %d\n", Seconds); |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 55 | int Res = setitimer(ITIMER_REAL, &T, nullptr); |
| 56 | assert(Res == 0); |
| 57 | struct sigaction sigact; |
| 58 | memset(&sigact, 0, sizeof(sigact)); |
| 59 | sigact.sa_sigaction = AlarmHandler; |
| 60 | Res = sigaction(SIGALRM, &sigact, 0); |
| 61 | assert(Res == 0); |
| 62 | } |
| 63 | |
Kostya Serebryany | 9690fcf | 2015-05-12 18:51:57 +0000 | [diff] [blame] | 64 | int NumberOfCpuCores() { |
| 65 | FILE *F = popen("nproc", "r"); |
| 66 | int N = 0; |
| 67 | fscanf(F, "%d", &N); |
| 68 | fclose(F); |
| 69 | return N; |
| 70 | } |
| 71 | |
Kostya Serebryany | 2da7b84 | 2015-05-18 21:34:20 +0000 | [diff] [blame] | 72 | void ExecuteCommand(const std::string &Command) { |
| 73 | system(Command.c_str()); |
| 74 | } |
| 75 | |
Kostya Serebryany | bc7c0ad | 2015-08-11 01:44:42 +0000 | [diff] [blame] | 76 | bool ToASCII(Unit &U) { |
| 77 | bool Changed = false; |
| 78 | for (auto &X : U) { |
| 79 | auto NewX = X; |
| 80 | NewX &= 127; |
| 81 | if (!isspace(NewX) && !isprint(NewX)) |
| 82 | NewX = ' '; |
| 83 | Changed |= NewX != X; |
| 84 | X = NewX; |
| 85 | } |
| 86 | return Changed; |
| 87 | } |
| 88 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 89 | } // namespace fuzzer |