blob: 3635f39a10def348bec49580a346884229c1afc7 [file] [log] [blame]
Aaron Ballmanef116982015-01-29 16:58:29 +00001//===- 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
19namespace fuzzer {
20
21void Print(const Unit &v, const char *PrintAfter) {
Aaron Ballmanef116982015-01-29 16:58:29 +000022 for (auto x : v)
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000023 std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ",";
Aaron Ballmanef116982015-01-29 16:58:29 +000024 std::cerr << PrintAfter;
25}
26
27void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000028 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 }
Aaron Ballmanef116982015-01-29 16:58:29 +000034 std::cerr << PrintAfter;
35}
36
37std::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
48static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000049 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000050}
51
52void 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