Fix typo in comment.
[oota-llvm.git] / lib / Fuzzer / FuzzerUtil.cpp
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 <iostream>
14 #include <sys/time.h>
15 #include <cassert>
16 #include <cstring>
17 #include <signal.h>
18
19 namespace fuzzer {
20
21 void Print(const Unit &v, const char *PrintAfter) {
22   for (auto x : v)
23     std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ",";
24   std::cerr << PrintAfter;
25 }
26
27 void PrintASCII(const Unit &U, const char *PrintAfter) {
28   for (auto X : U) {
29     if (isprint(X))
30       std::cerr << X;
31     else
32       std::cerr << "\\x" << std::hex << (int)(unsigned)X << std::dec;
33   }
34   std::cerr << PrintAfter;
35 }
36
37 std::string Hash(const Unit &in) {
38   size_t h1 = 0, h2 = 0;
39   for (auto x : in) {
40     h1 += x;
41     h1 *= 5;
42     h2 += x;
43     h2 *= 7;
44   }
45   return std::to_string(h1) + std::to_string(h2);
46 }
47
48 static void AlarmHandler(int, siginfo_t *, void *) {
49   Fuzzer::StaticAlarmCallback();
50 }
51
52 void SetTimer(int Seconds) {
53   struct itimerval T {{Seconds, 0}, {Seconds, 0}};
54   std::cerr << "SetTimer " << Seconds << "\n";
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
64 }  // namespace fuzzer