[lib/Fuzzer] use sha1sum for the file hash
[oota-llvm.git] / lib / Fuzzer / FuzzerUtil.cpp
index 3f62a1f1d1e2fc5dd978887672c009a2b58d7b58..2fb6e0587cf40b58c7a61368502751b0f42a383c 100644 (file)
 #include <cassert>
 #include <cstring>
 #include <signal.h>
+#include <unistd.h>
 
 namespace fuzzer {
 
 void Print(const Unit &v, const char *PrintAfter) {
-  std::cerr << v.size() << ": ";
   for (auto x : v)
-    std::cerr << (unsigned) x << " ";
+    std::cerr << "0x" << std::hex << (unsigned) x << std::dec << ",";
   std::cerr << PrintAfter;
 }
 
 void PrintASCII(const Unit &U, const char *PrintAfter) {
-  for (auto X : U)
-    std::cerr << (char)((isascii(X) && X >= ' ') ? X : '?');
+  for (auto X : U) {
+    if (isprint(X))
+      std::cerr << X;
+    else
+      std::cerr << "\\x" << std::hex << (int)(unsigned)X << std::dec;
+  }
   std::cerr << PrintAfter;
 }
 
+// Try to compute a SHA1 sum of this Unit using an external 'sha1sum' command.
+// We can not use the SHA1 function from openssl directly because
+//  a) openssl may not be available,
+//  b) we may be fuzzing openssl itself.
+// This is all very sad, suggestions are welcome.
+static std::string TrySha1(const Unit &in) {
+  char TempPath[] = "/tmp/fuzzer-tmp-XXXXXX";
+  int FD = mkstemp(TempPath);
+  if (FD < 0) return "";
+  ssize_t Written = write(FD, in.data(), in.size());
+  close(FD);
+  if (static_cast<size_t>(Written) != in.size()) return "";
+
+  std::string Cmd = "sha1sum < ";
+  Cmd += TempPath;
+  FILE *F = popen(Cmd.c_str(), "r");
+  if (!F) return "";
+  char Sha1[41];
+  fgets(Sha1, sizeof(Sha1), F);
+  fclose(F);
+
+  unlink(TempPath);
+  return Sha1;
+}
+
 std::string Hash(const Unit &in) {
+  std::string Sha1 = TrySha1(in);
+  if (!Sha1.empty())
+    return Sha1;
+
   size_t h1 = 0, h2 = 0;
   for (auto x : in) {
     h1 += x;
@@ -58,4 +91,12 @@ void SetTimer(int Seconds) {
   assert(Res == 0);
 }
 
+int NumberOfCpuCores() {
+  FILE *F = popen("nproc", "r");
+  int N = 0;
+  fscanf(F, "%d", &N);
+  fclose(F);
+  return N;
+}
+
 }  // namespace fuzzer