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 <iostream> |
| 16 | #include <sys/time.h> |
| 17 | #include <cassert> |
| 18 | #include <cstring> |
| 19 | #include <signal.h> |
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 | 043ab1c | 2015-04-01 21:33:20 +0000 | [diff] [blame] | 26 | std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ","; |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 27 | std::cerr << PrintAfter; |
| 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)) |
| 33 | std::cerr << X; |
| 34 | else |
| 35 | std::cerr << "\\x" << std::hex << (int)(unsigned)X << std::dec; |
| 36 | } |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 37 | std::cerr << PrintAfter; |
| 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}}; |
| 55 | std::cerr << "SetTimer " << Seconds << "\n"; |
| 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 | |
Aaron Ballman | ef11698 | 2015-01-29 16:58:29 +0000 | [diff] [blame] | 73 | } // namespace fuzzer |