summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Fuzzer/FuzzerUtil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerUtil.cpp')
-rw-r--r--llvm/lib/Fuzzer/FuzzerUtil.cpp47
1 files changed, 9 insertions, 38 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerUtil.cpp b/llvm/lib/Fuzzer/FuzzerUtil.cpp
index 2fb6e0587cf..c4b0afa55d5 100644
--- a/llvm/lib/Fuzzer/FuzzerUtil.cpp
+++ b/llvm/lib/Fuzzer/FuzzerUtil.cpp
@@ -10,6 +10,8 @@
//===----------------------------------------------------------------------===//
#include "FuzzerInternal.h"
+#include <sstream>
+#include <iomanip>
#include <iostream>
#include <sys/time.h>
#include <cassert>
@@ -35,44 +37,13 @@ void PrintASCII(const Unit &U, const char *PrintAfter) {
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;
- h1 *= 5;
- h2 += x;
- h2 *= 7;
- }
- return std::to_string(h1) + std::to_string(h2);
+std::string Hash(const Unit &U) {
+ uint8_t Hash[kSHA1NumBytes];
+ ComputeSHA1(U.data(), U.size(), Hash);
+ std::stringstream SS;
+ for (int i = 0; i < kSHA1NumBytes; i++)
+ SS << std::hex << std::setfill('0') << std::setw(2) << (unsigned)Hash[i];
+ return SS.str();
}
static void AlarmHandler(int, siginfo_t *, void *) {
OpenPOWER on IntegriCloud