blob: e76adb37481ce400e64a6b3db06e6956fc79ae4a [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"
Kostya Serebryany96eab652015-05-14 22:41:49 +000013#include <sstream>
14#include <iomanip>
Aaron Ballmanef116982015-01-29 16:58:29 +000015#include <sys/time.h>
16#include <cassert>
17#include <cstring>
18#include <signal.h>
Kostya Serebryanyf47198a2015-05-12 22:03:34 +000019#include <unistd.h>
Aaron Ballmanef116982015-01-29 16:58:29 +000020
21namespace fuzzer {
22
23void Print(const Unit &v, const char *PrintAfter) {
Aaron Ballmanef116982015-01-29 16:58:29 +000024 for (auto x : v)
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000025 Printf("0x%x,", (unsigned) x);
26 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000027}
28
29void PrintASCII(const Unit &U, const char *PrintAfter) {
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000030 for (auto X : U) {
31 if (isprint(X))
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000032 Printf("%c", X);
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000033 else
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000034 Printf("\\x%x", (unsigned)X);
Kostya Serebryany043ab1c2015-04-01 21:33:20 +000035 }
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000036 Printf("%s", PrintAfter);
Aaron Ballmanef116982015-01-29 16:58:29 +000037}
38
Kostya Serebryany96eab652015-05-14 22:41:49 +000039std::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();
Aaron Ballmanef116982015-01-29 16:58:29 +000046}
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}};
Kostya Serebryany7c180ea2015-05-23 01:22:35 +000054 Printf("SetTimer %d\n", Seconds);
Aaron Ballmanef116982015-01-29 16:58:29 +000055 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
Kostya Serebryany9690fcf2015-05-12 18:51:57 +000064int NumberOfCpuCores() {
65 FILE *F = popen("nproc", "r");
66 int N = 0;
67 fscanf(F, "%d", &N);
68 fclose(F);
69 return N;
70}
71
Kostya Serebryany2da7b842015-05-18 21:34:20 +000072void ExecuteCommand(const std::string &Command) {
73 system(Command.c_str());
74}
75
Kostya Serebryanybc7c0ad2015-08-11 01:44:42 +000076bool ToASCII(Unit &U) {
77 bool Changed = false;
78 for (auto &X : U) {
79 auto NewX = X;
80 NewX &= 127;
81 if (!isspace(NewX) && !isprint(NewX))
82 NewX = ' ';
83 Changed |= NewX != X;
84 X = NewX;
85 }
86 return Changed;
87}
88
Aaron Ballmanef116982015-01-29 16:58:29 +000089} // namespace fuzzer