1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/hash/hash.h"
6 
7 #include "base/rand_util.h"
8 #include "base/third_party/cityhash/city.h"
9 #include "build/build_config.h"
10 
11 // Definition in base/third_party/superfasthash/superfasthash.c. (Third-party
12 // code did not come with its own header file, so declaring the function here.)
13 // Note: This algorithm is also in Blink under Source/wtf/StringHasher.h.
14 extern "C" uint32_t SuperFastHash(const char* data, int len);
15 
16 namespace base {
17 
18 namespace {
19 
FastHashImpl(base::span<const uint8_t> data)20 size_t FastHashImpl(base::span<const uint8_t> data) {
21   // We use the updated CityHash within our namespace (not the deprecated
22   // version from third_party/smhasher).
23 #if defined(ARCH_CPU_64_BITS)
24   return base::internal::cityhash_v111::CityHash64(
25       reinterpret_cast<const char*>(data.data()), data.size());
26 #else
27   return base::internal::cityhash_v111::CityHash32(
28       reinterpret_cast<const char*>(data.data()), data.size());
29 #endif
30 }
31 
32 // Implement hashing for pairs of at-most 32 bit integer values.
33 // When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using
34 // multiply-add hashing. This algorithm, as described in
35 // Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in
36 // eingeschränkten Branchingprogrammmodellen" by Woelfel, is:
37 //
38 //   h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32
39 //
40 // Contact danakj@chromium.org for any questions.
HashInts32Impl(uint32_t value1,uint32_t value2)41 size_t HashInts32Impl(uint32_t value1, uint32_t value2) {
42   uint64_t value1_64 = value1;
43   uint64_t hash64 = (value1_64 << 32) | value2;
44 
45   if (sizeof(size_t) >= sizeof(uint64_t))
46     return static_cast<size_t>(hash64);
47 
48   uint64_t odd_random = 481046412LL << 32 | 1025306955LL;
49   uint32_t shift_random = 10121U << 16;
50 
51   hash64 = hash64 * odd_random + shift_random;
52   size_t high_bits =
53       static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
54   return high_bits;
55 }
56 
57 // Implement hashing for pairs of up-to 64-bit integer values.
58 // We use the compound integer hash method to produce a 64-bit hash code, by
59 // breaking the two 64-bit inputs into 4 32-bit values:
60 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
61 // Then we reduce our result to 32 bits if required, similar to above.
HashInts64Impl(uint64_t value1,uint64_t value2)62 size_t HashInts64Impl(uint64_t value1, uint64_t value2) {
63   uint32_t short_random1 = 842304669U;
64   uint32_t short_random2 = 619063811U;
65   uint32_t short_random3 = 937041849U;
66   uint32_t short_random4 = 3309708029U;
67 
68   uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff);
69   uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff);
70   uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff);
71   uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff);
72 
73   uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1;
74   uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2;
75   uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3;
76   uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4;
77 
78   uint64_t hash64 = product1 + product2 + product3 + product4;
79 
80   if (sizeof(size_t) >= sizeof(uint64_t))
81     return static_cast<size_t>(hash64);
82 
83   uint64_t odd_random = 1578233944LL << 32 | 194370989LL;
84   uint32_t shift_random = 20591U << 16;
85 
86   hash64 = hash64 * odd_random + shift_random;
87   size_t high_bits =
88       static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t))));
89   return high_bits;
90 }
91 
92 // The random seed is used to perturb the output of base::FastHash() and
93 // base::HashInts() so that it is only deterministic within the lifetime of a
94 // process. This prevents inadvertent dependencies on the underlying
95 // implementation, e.g. anything that persists the hash value and expects it to
96 // be unchanging will break.
97 //
98 // Note: this is the same trick absl uses to generate a random seed. This is
99 // more robust than using base::RandBytes(), which can fail inside a sandboxed
100 // environment. Note that without ASLR, the seed won't be quite as random...
101 #if DCHECK_IS_ON()
102 constexpr const void* kSeed = &kSeed;
103 #endif
104 
105 template <typename T>
Scramble(T input)106 T Scramble(T input) {
107 #if DCHECK_IS_ON()
108   return HashInts64Impl(input, reinterpret_cast<uintptr_t>(kSeed));
109 #else
110   return input;
111 #endif
112 }
113 
114 }  // namespace
115 
FastHash(base::span<const uint8_t> data)116 size_t FastHash(base::span<const uint8_t> data) {
117   return Scramble(FastHashImpl(data));
118 }
119 
Hash(const void * data,size_t length)120 uint32_t Hash(const void* data, size_t length) {
121   // Currently our in-memory hash is the same as the persistent hash. The
122   // split between in-memory and persistent hash functions is maintained to
123   // allow the in-memory hash function to be updated in the future.
124   return PersistentHash(data, length);
125 }
126 
Hash(const std::string & str)127 uint32_t Hash(const std::string& str) {
128   return PersistentHash(as_bytes(make_span(str)));
129 }
130 
Hash(const string16 & str)131 uint32_t Hash(const string16& str) {
132   return PersistentHash(as_bytes(make_span(str)));
133 }
134 
PersistentHash(span<const uint8_t> data)135 uint32_t PersistentHash(span<const uint8_t> data) {
136   // This hash function must not change, since it is designed to be persistable
137   // to disk.
138   if (data.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
139     NOTREACHED();
140     return 0;
141   }
142   return ::SuperFastHash(reinterpret_cast<const char*>(data.data()),
143                          static_cast<int>(data.size()));
144 }
145 
PersistentHash(const void * data,size_t length)146 uint32_t PersistentHash(const void* data, size_t length) {
147   return PersistentHash(make_span(static_cast<const uint8_t*>(data), length));
148 }
149 
PersistentHash(const std::string & str)150 uint32_t PersistentHash(const std::string& str) {
151   return PersistentHash(str.data(), str.size());
152 }
153 
HashInts32(uint32_t value1,uint32_t value2)154 size_t HashInts32(uint32_t value1, uint32_t value2) {
155   return Scramble(HashInts32Impl(value1, value2));
156 }
157 
158 // Implement hashing for pairs of up-to 64-bit integer values.
159 // We use the compound integer hash method to produce a 64-bit hash code, by
160 // breaking the two 64-bit inputs into 4 32-bit values:
161 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
162 // Then we reduce our result to 32 bits if required, similar to above.
HashInts64(uint64_t value1,uint64_t value2)163 size_t HashInts64(uint64_t value1, uint64_t value2) {
164   return Scramble(HashInts64Impl(value1, value2));
165 }
166 
167 }  // namespace base
168