blob: 3f62a1f1d1e2fc5dd978887672c009a2b58d7b58 [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) {
22 std::cerr << v.size() << ": ";
23 for (auto x : v)
24 std::cerr << (unsigned) x << " ";
25 std::cerr << PrintAfter;
26}
27
28void 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
34std::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
45static void AlarmHandler(int, siginfo_t *, void *) {
Kostya Serebryany52a788e2015-03-31 20:13:20 +000046 Fuzzer::StaticAlarmCallback();
Aaron Ballmanef116982015-01-29 16:58:29 +000047}
48
49void 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