[Orc] Remove the OwnedModules list from OrcMCJITReplacement and use
[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   std::cerr << v.size() << ": ";
23   for (auto x : v)
24     std::cerr << (unsigned) x << " ";
25   std::cerr << PrintAfter;
26 }
27
28 void PrintASCII(const Unit &U, const char *PrintAfter) {
29   for (auto X : U)
30     std::cerr << (char)((isascii(X) && X >= ' ') ? X : '?');
31   std::cerr << PrintAfter;
32 }
33
34 std::string Hash(const Unit &in) {
35   size_t h1 = 0, h2 = 0;
36   for (auto x : in) {
37     h1 += x;
38     h1 *= 5;
39     h2 += x;
40     h2 *= 7;
41   }
42   return std::to_string(h1) + std::to_string(h2);
43 }
44
45 static void AlarmHandler(int, siginfo_t *, void *) {
46   Fuzzer::AlarmCallback();
47 }
48
49 void SetTimer(int Seconds) {
50   struct itimerval T {{Seconds, 0}, {Seconds, 0}};
51   std::cerr << "SetTimer " << Seconds << "\n";
52   int Res = setitimer(ITIMER_REAL, &T, nullptr);
53   assert(Res == 0);
54   struct sigaction sigact;
55   memset(&sigact, 0, sizeof(sigact));
56   sigact.sa_sigaction = AlarmHandler;
57   Res = sigaction(SIGALRM, &sigact, 0);
58   assert(Res == 0);
59 }
60
61 }  // namespace fuzzer