mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-06-25 00:24:26 +00:00
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237400 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@ -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 *) {
|
||||
|
Reference in New Issue
Block a user