diff options
author | Kostya Serebryany <kcc@google.com> | 2015-05-14 22:41:49 +0000 |
---|---|---|
committer | Kostya Serebryany <kcc@google.com> | 2015-05-14 22:41:49 +0000 |
commit | 96eab65d81db648d30c819dfa86e3af9c29e0566 (patch) | |
tree | 1def4806c4f7e511b665d7ce5ebaec6a35521d4d /llvm/lib/Fuzzer/FuzzerUtil.cpp | |
parent | 11e7da97a0f35fdd6f33283ea860f3036dcb39ab (diff) | |
download | llvm-96eab65d81db648d30c819dfa86e3af9c29e0566.zip llvm-96eab65d81db648d30c819dfa86e3af9c29e0566.tar.gz llvm-96eab65d81db648d30c819dfa86e3af9c29e0566.tar.bz2 |
[lib/Fuzzer] Add SHA1 implementation from public domain.
Summary:
This adds a SHA1 implementation taken from public domain code.
The change is trivial, but as it involves third-party code I'd like
a second pair of eyes before commit.
LibFuzzer can not use SHA1 from openssl because openssl may not be available
and because we may be fuzzing openssl itself.
Using sha1sum via a pipe is too slow.
Test Plan: n/a
Reviewers: chandlerc
Reviewed By: chandlerc
Subscribers: majnemer, llvm-commits
Differential Revision: http://reviews.llvm.org/D9733
llvm-svn: 237400
Diffstat (limited to 'llvm/lib/Fuzzer/FuzzerUtil.cpp')
-rw-r--r-- | llvm/lib/Fuzzer/FuzzerUtil.cpp | 47 |
1 files changed, 9 insertions, 38 deletions
diff --git a/llvm/lib/Fuzzer/FuzzerUtil.cpp b/llvm/lib/Fuzzer/FuzzerUtil.cpp index 2fb6e05..c4b0afa 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 *) { |