[llvm] r237198 - [lib/Fuzzer] use sha1sum for the file hash

Kostya Serebryany kcc at google.com
Tue May 12 15:03:34 PDT 2015


Author: kcc
Date: Tue May 12 17:03:34 2015
New Revision: 237198

URL: http://llvm.org/viewvc/llvm-project?rev=237198&view=rev
Log:
[lib/Fuzzer] use sha1sum for the file hash

Modified:
    llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp

Modified: llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp?rev=237198&r1=237197&r2=237198&view=diff
==============================================================================
--- llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp (original)
+++ llvm/trunk/lib/Fuzzer/FuzzerUtil.cpp Tue May 12 17:03:34 2015
@@ -15,6 +15,7 @@
 #include <cassert>
 #include <cstring>
 #include <signal.h>
+#include <unistd.h>
 
 namespace fuzzer {
 
@@ -34,7 +35,36 @@ void PrintASCII(const Unit &U, const cha
   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;





More information about the llvm-commits mailing list