1 //===- FuzzerUtil.cpp - Misc utils ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "FuzzerInternal.h"
24 void Print(const Unit &v, const char *PrintAfter) {
26 std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ",";
27 std::cerr << PrintAfter;
30 void PrintASCII(const Unit &U, const char *PrintAfter) {
35 std::cerr << "\\x" << std::hex << (int)(unsigned)X << std::dec;
37 std::cerr << PrintAfter;
40 std::string Hash(const Unit &U) {
41 uint8_t Hash[kSHA1NumBytes];
42 ComputeSHA1(U.data(), U.size(), Hash);
44 for (int i = 0; i < kSHA1NumBytes; i++)
45 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
49 static void AlarmHandler(int, siginfo_t *, void *) {
50 Fuzzer::StaticAlarmCallback();
53 void SetTimer(int Seconds) {
54 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
55 std::cerr << "SetTimer " << Seconds << "\n";
56 int Res = setitimer(ITIMER_REAL, &T, nullptr);
58 struct sigaction sigact;
59 memset(&sigact, 0, sizeof(sigact));
60 sigact.sa_sigaction = AlarmHandler;
61 Res = sigaction(SIGALRM, &sigact, 0);
65 int NumberOfCpuCores() {
66 FILE *F = popen("nproc", "r");