[compiler-rt] 0889181 - Tweak SimpleFastHash

Marco Vanotti via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 1 23:26:44 PDT 2021


Author: Aaron Green
Date: 2021-04-01T23:26:03-07:00
New Revision: 0889181625bb570e463362ab8f53f9a14c886b2e

URL: https://github.com/llvm/llvm-project/commit/0889181625bb570e463362ab8f53f9a14c886b2e
DIFF: https://github.com/llvm/llvm-project/commit/0889181625bb570e463362ab8f53f9a14c886b2e.diff

LOG: Tweak SimpleFastHash

This change adds a SimpleFastHash64 variant of SimpleFastHash which allows call sites to specify a starting value and get a 64 bit hash in return. This allows a hash to be "resumed" with more data.

A later patch needs this to be able to hash a sequence of module-relative values one at a time, rather than just a region a memory.

Reviewed By: morehouse

Differential Revision: https://reviews.llvm.org/D94510

Added: 
    

Modified: 
    compiler-rt/lib/fuzzer/FuzzerTracePC.h
    compiler-rt/lib/fuzzer/FuzzerUtil.cpp
    compiler-rt/lib/fuzzer/FuzzerUtil.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/fuzzer/FuzzerTracePC.h b/compiler-rt/lib/fuzzer/FuzzerTracePC.h
index 1fa9ed11c6987..a93732972f7d7 100644
--- a/compiler-rt/lib/fuzzer/FuzzerTracePC.h
+++ b/compiler-rt/lib/fuzzer/FuzzerTracePC.h
@@ -54,7 +54,7 @@ struct MemMemTable {
   void Add(const uint8_t *Data, size_t Size) {
     if (Size <= 2) return;
     Size = std::min(Size, Word::GetMaxSize());
-    size_t Idx = SimpleFastHash(Data, Size) % kSize;
+    auto Idx = SimpleFastHash(Data, Size) % kSize;
     MemMemWords[Idx].Set(Data, Size);
   }
   const Word &Get(size_t Idx) {

diff  --git a/compiler-rt/lib/fuzzer/FuzzerUtil.cpp b/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
index 4b161c4139a52..05185499bdd1e 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
@@ -226,10 +226,11 @@ unsigned NumberOfCpuCores() {
   return N;
 }
 
-size_t SimpleFastHash(const uint8_t *Data, size_t Size) {
-  size_t Res = 0;
+uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial) {
+  uint64_t Res = Initial;
+  const uint8_t *Bytes = static_cast<const uint8_t *>(Data);
   for (size_t i = 0; i < Size; i++)
-    Res = Res * 11 + Data[i];
+    Res = Res * 11 + Bytes[i];
   return Res;
 }
 

diff  --git a/compiler-rt/lib/fuzzer/FuzzerUtil.h b/compiler-rt/lib/fuzzer/FuzzerUtil.h
index 2ae58102759c9..a188a7be32a53 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtil.h
+++ b/compiler-rt/lib/fuzzer/FuzzerUtil.h
@@ -88,7 +88,7 @@ std::string DisassembleCmd(const std::string &FileName);
 
 std::string SearchRegexCmd(const std::string &Regex);
 
-size_t SimpleFastHash(const uint8_t *Data, size_t Size);
+uint64_t SimpleFastHash(const void *Data, size_t Size, uint64_t Initial = 0);
 
 inline size_t Log(size_t X) {
   return static_cast<size_t>((sizeof(unsigned long long) * 8) - Clzll(X) - 1);


        


More information about the llvm-commits mailing list