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
GitOrigin-RevId: 0889181625bb570e463362ab8f53f9a14c886b2e
diff --git a/FuzzerTracePC.h b/FuzzerTracePC.h
index 1fa9ed1..a937329 100644
--- a/FuzzerTracePC.h
+++ b/FuzzerTracePC.h
@@ -54,7 +54,7 @@
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/FuzzerUtil.cpp b/FuzzerUtil.cpp
index 4b161c4..0518549 100644
--- a/FuzzerUtil.cpp
+++ b/FuzzerUtil.cpp
@@ -226,10 +226,11 @@
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/FuzzerUtil.h b/FuzzerUtil.h
index 2ae5810..a188a7b 100644
--- a/FuzzerUtil.h
+++ b/FuzzerUtil.h
@@ -88,7 +88,7 @@
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);