diff options
author | Kostya Serebryany <kcc@google.com> | 2015-05-12 22:03:34 +0000 |
---|---|---|
committer | Kostya Serebryany <kcc@google.com> | 2015-05-12 22:03:34 +0000 |
commit | f47198aa363c2cebb0fac3edb75785b03d17f604 (patch) | |
tree | ce693c3f23f9a72d8d2b5157a70a6b6e21ff2660 | |
parent | a9522c4c5b75e3489f300ca6b07979d9d81af9a2 (diff) | |
download | bcm5719-llvm-f47198aa363c2cebb0fac3edb75785b03d17f604.tar.gz bcm5719-llvm-f47198aa363c2cebb0fac3edb75785b03d17f604.zip |
[lib/Fuzzer] use sha1sum for the file hash
llvm-svn: 237198
-rw-r--r-- | llvm/lib/Fuzzer/FuzzerUtil.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerUtil.cpp b/llvm/lib/Fuzzer/FuzzerUtil.cpp index dc7154ca385..2fb6e0587cf 100644 --- a/llvm/lib/Fuzzer/FuzzerUtil.cpp +++ b/llvm/lib/Fuzzer/FuzzerUtil.cpp @@ -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 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; |