2
0

FuzzerUtil.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===- 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. #include "FuzzerInternal.h"
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <sys/time.h>
  15. #include <cassert>
  16. #include <cstring>
  17. #include <signal.h>
  18. #include <unistd.h>
  19. namespace fuzzer {
  20. void Print(const Unit &v, const char *PrintAfter) {
  21. for (auto x : v)
  22. Printf("0x%x,", (unsigned) x);
  23. Printf("%s", PrintAfter);
  24. }
  25. void PrintASCII(const Unit &U, const char *PrintAfter) {
  26. for (auto X : U) {
  27. if (isprint(X))
  28. Printf("%c", X);
  29. else
  30. Printf("\\x%x", (unsigned)X);
  31. }
  32. Printf("%s", PrintAfter);
  33. }
  34. std::string Hash(const Unit &U) {
  35. uint8_t Hash[kSHA1NumBytes];
  36. ComputeSHA1(U.data(), U.size(), Hash);
  37. std::stringstream SS;
  38. for (int i = 0; i < kSHA1NumBytes; i++)
  39. SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
  40. return SS.str();
  41. }
  42. static void AlarmHandler(int, siginfo_t *, void *) {
  43. Fuzzer::StaticAlarmCallback();
  44. }
  45. void SetTimer(int Seconds) {
  46. struct itimerval T {{Seconds, 0}, {Seconds, 0}};
  47. Printf("SetTimer %d\n", Seconds);
  48. int Res = setitimer(ITIMER_REAL, &T, nullptr);
  49. assert(Res == 0);
  50. struct sigaction sigact;
  51. memset(&sigact, 0, sizeof(sigact));
  52. sigact.sa_sigaction = AlarmHandler;
  53. Res = sigaction(SIGALRM, &sigact, 0);
  54. assert(Res == 0);
  55. }
  56. int NumberOfCpuCores() {
  57. FILE *F = popen("nproc", "r");
  58. int N = 0;
  59. fscanf(F, "%d", &N);
  60. fclose(F);
  61. return N;
  62. }
  63. void ExecuteCommand(const std::string &Command) {
  64. system(Command.c_str());
  65. }
  66. } // namespace fuzzer