Commit c9205588 authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Make base::FastHash non-deterministic in DCHECK_IS_ON() builds.

This is a prerequisite for migrating more things over to base::FastHash,
to ensure they don't inadverdently have a dependency on a stable mapping
of inputs to hash values.

Also change the FastHash(const std::string&) overload to
FastHash(StringPiece) to avoid potentially constructing unnecessary
temporaries.

Bug: 1025358
Change-Id: I2e3d67ab74f869437ae2bdf12c9560f1be19688d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1919804Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715940}
parent 2b4c7a67
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "base/hash/hash.h" #include "base/hash/hash.h"
#include "base/rand_util.h"
#include "base/third_party/cityhash/city.h" #include "base/third_party/cityhash/city.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -17,7 +18,11 @@ namespace base { ...@@ -17,7 +18,11 @@ namespace base {
size_t FastHash(base::span<const uint8_t> data) { size_t FastHash(base::span<const uint8_t> data) {
// We use the updated CityHash within our namespace (not the deprecated // We use the updated CityHash within our namespace (not the deprecated
// version from third_party/smhasher). // version from third_party/smhasher).
#if defined(ARCH_CPU_64_BITS) #if DCHECK_IS_ON()
static const uint64_t seed = base::RandUint64();
return base::internal::cityhash_v111::CityHash64WithSeed(
reinterpret_cast<const char*>(data.data()), data.size(), seed);
#elif defined(ARCH_CPU_64_BITS)
return base::internal::cityhash_v111::CityHash64( return base::internal::cityhash_v111::CityHash64(
reinterpret_cast<const char*>(data.data()), data.size()); reinterpret_cast<const char*>(data.data()), data.size());
#else #else
...@@ -26,14 +31,6 @@ size_t FastHash(base::span<const uint8_t> data) { ...@@ -26,14 +31,6 @@ size_t FastHash(base::span<const uint8_t> data) {
#endif #endif
} }
size_t FastHash(const std::string& str) {
#if defined(ARCH_CPU_64_BITS)
return base::internal::cityhash_v111::CityHash64(str.data(), str.size());
#else
return base::internal::cityhash_v111::CityHash32(str.data(), str.size());
#endif
}
uint32_t Hash(const void* data, size_t length) { uint32_t Hash(const void* data, size_t length) {
// Currently our in-memory hash is the same as the persistent hash. The // Currently our in-memory hash is the same as the persistent hash. The
// split between in-memory and persistent hash functions is maintained to // split between in-memory and persistent hash functions is maintained to
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/string_piece.h"
namespace base { namespace base {
...@@ -25,7 +26,7 @@ namespace base { ...@@ -25,7 +26,7 @@ namespace base {
// Deprecated: Computes a hash of a memory buffer, use FastHash() instead. // Deprecated: Computes a hash of a memory buffer, use FastHash() instead.
// If you need to persist a change on disk or between computers, use // If you need to persist a change on disk or between computers, use
// PersistentHash(). // PersistentHash().
// TODO(cavalcantii): Migrate client code to new hash function. // TODO(https://crbug.com/1025358): Migrate client code to new hash function.
BASE_EXPORT uint32_t Hash(const void* data, size_t length); BASE_EXPORT uint32_t Hash(const void* data, size_t length);
BASE_EXPORT uint32_t Hash(const std::string& str); BASE_EXPORT uint32_t Hash(const std::string& str);
BASE_EXPORT uint32_t Hash(const string16& str); BASE_EXPORT uint32_t Hash(const string16& str);
...@@ -37,7 +38,9 @@ BASE_EXPORT uint32_t Hash(const string16& str); ...@@ -37,7 +38,9 @@ BASE_EXPORT uint32_t Hash(const string16& str);
// publicly available. // publicly available.
// May changed without warning, do not expect stability of outputs. // May changed without warning, do not expect stability of outputs.
BASE_EXPORT size_t FastHash(base::span<const uint8_t> data); BASE_EXPORT size_t FastHash(base::span<const uint8_t> data);
BASE_EXPORT size_t FastHash(const std::string& str); inline size_t FastHash(StringPiece str) {
return FastHash(as_bytes(make_span(str)));
}
// Computes a hash of a memory buffer. This hash function must not change so // Computes a hash of a memory buffer. This hash function must not change so
// that code can use the hashed values for persistent storage purposes or // that code can use the hashed values for persistent storage purposes or
......
...@@ -79,4 +79,10 @@ TEST(HashTest, CString) { ...@@ -79,4 +79,10 @@ TEST(HashTest, CString) {
EXPECT_EQ(2794219650u, Hash(str, strlen("hello world"))); EXPECT_EQ(2794219650u, Hash(str, strlen("hello world")));
} }
TEST(HashTest, FastHash) {
std::string s;
constexpr char kEmptyString[] = "";
EXPECT_EQ(FastHash(s), FastHash(kEmptyString));
}
} // namespace base } // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment