LLVM  3.7.0
FuzzerUtil.cpp
Go to the documentation of this file.
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"
13 #include <sstream>
14 #include <iomanip>
15 #include <sys/time.h>
16 #include <cassert>
17 #include <cstring>
18 #include <signal.h>
19 #include <unistd.h>
20 
21 namespace fuzzer {
22 
23 void Print(const Unit &v, const char *PrintAfter) {
24  for (auto x : v)
25  Printf("0x%x,", (unsigned) x);
26  Printf("%s", PrintAfter);
27 }
28 
29 void PrintASCII(const Unit &U, const char *PrintAfter) {
30  for (auto X : U) {
31  if (isprint(X))
32  Printf("%c", X);
33  else
34  Printf("\\x%x", (unsigned)X);
35  }
36  Printf("%s", PrintAfter);
37 }
38 
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();
46 }
47 
48 static void AlarmHandler(int, siginfo_t *, void *) {
50 }
51 
52 void SetTimer(int Seconds) {
53  struct itimerval T {{Seconds, 0}, {Seconds, 0}};
54  Printf("SetTimer %d\n", Seconds);
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 
65  FILE *F = popen("nproc", "r");
66  int N = 0;
67  fscanf(F, "%d", &N);
68  fclose(F);
69  return N;
70 }
71 
72 void ExecuteCommand(const std::string &Command) {
73  system(Command.c_str());
74 }
75 
76 } // namespace fuzzer
void PrintASCII(const Unit &U, const char *PrintAfter="")
Definition: FuzzerUtil.cpp:29
void Print(const Unit &U, const char *PrintAfter="")
Definition: FuzzerUtil.cpp:23
void SetTimer(int Seconds)
Definition: FuzzerUtil.cpp:52
void ExecuteCommand(const std::string &Command)
Definition: FuzzerUtil.cpp:72
static Fuzzer * F
Definition: FuzzerLoop.cpp:19
int NumberOfCpuCores()
Definition: FuzzerUtil.cpp:64
void Printf(const char *Fmt,...)
Definition: FuzzerIO.cpp:87
void ComputeSHA1(const uint8_t *Data, size_t Len, uint8_t *Out)
Definition: FuzzerSHA1.cpp:197
static PassOptionList PrintAfter("print-after", llvm::cl::desc("Print IR after specified passes"), cl::Hidden)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static void AlarmHandler(int, siginfo_t *, void *)
Definition: FuzzerUtil.cpp:48
#define N
static void StaticAlarmCallback()
Definition: FuzzerLoop.cpp:55
std::vector< uint8_t > Unit
std::string Hash(const Unit &U)
Definition: FuzzerUtil.cpp:39
static const int kSHA1NumBytes