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"
23 void Print(const Unit &v, const char *PrintAfter) {
25 Printf("0x%x,", (unsigned) x);
26 Printf("%s", PrintAfter);
29 void PrintASCII(const Unit &U, const char *PrintAfter) {
34 Printf("\\x%x", (unsigned)X);
36 Printf("%s", PrintAfter);
39 std::string Hash(const Unit &U) {
40 uint8_t Hash[kSHA1NumBytes];
41 ComputeSHA1(U.data(), U.size(), Hash);
43 for (int i = 0; i < kSHA1NumBytes; i++)
44 SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
48 static void AlarmHandler(int, siginfo_t *, void *) {
49 Fuzzer::StaticAlarmCallback();
52 void SetTimer(int Seconds) {
53 struct itimerval T {{Seconds, 0}, {Seconds, 0}};
54 Printf("SetTimer %d\n", Seconds);
55 int Res = setitimer(ITIMER_REAL, &T, nullptr);
57 struct sigaction sigact;
58 memset(&sigact, 0, sizeof(sigact));
59 sigact.sa_sigaction = AlarmHandler;
60 Res = sigaction(SIGALRM, &sigact, 0);
64 int NumberOfCpuCores() {
65 FILE *F = popen("nproc", "r");
72 void ExecuteCommand(const std::string &Command) {
73 system(Command.c_str());