Skip to content

Commit 411f6fe

Browse files
hashseedaddaleax
authored andcommitted
deps: cherry-pick c736883 from upstream V8
Original commit message: Optionally use halfsiphash for integer hashing. Change-Id: Ibd14f7b3fe78635675c76ae864112e3a3a7bc701 Reviewed-on: https://chromium-review.googlesource.com/c/1382463 Commit-Queue: Yang Guo <yangguo@chromium.org> Reviewed-by: Benedikt Meurer <bmeurer@chromium.org> Cr-Commit-Position: refs/heads/master@{#58674} Refs: v8/v8@c736883
1 parent 55c2702 commit 411f6fe

File tree

9 files changed

+309
-3
lines changed

9 files changed

+309
-3
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# Reset this number to 0 on major V8 upgrades.
3232
# Increment by one for each non-official patch applied to deps/v8.
33-
'v8_embedder_string': '-node.16',
33+
'v8_embedder_string': '-node.17',
3434

3535
# Enable disassembler for `--print-code` v8 options
3636
'v8_enable_disassembler': 1,

deps/v8/BUILD.gn

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ declare_args() {
119119
# tools/gen-postmortem-metadata.py for details.
120120
v8_postmortem_support = false
121121

122+
# Use Siphash as added protection against hash flooding attacks.
123+
v8_use_siphash = false
124+
122125
# Switches off inlining in V8.
123126
v8_no_inline = false
124127

@@ -385,6 +388,9 @@ config("features") {
385388
if (v8_use_multi_snapshots) {
386389
defines += [ "V8_MULTI_SNAPSHOTS" ]
387390
}
391+
if (v8_use_siphash) {
392+
defines += [ "V8_USE_SIPHASH" ]
393+
}
388394
}
389395

390396
config("toolchain") {
@@ -2446,6 +2452,8 @@ v8_source_set("v8_base") {
24462452
"src/string-stream.h",
24472453
"src/strtod.cc",
24482454
"src/strtod.h",
2455+
"src/third_party/siphash/halfsiphash.cc",
2456+
"src/third_party/siphash/halfsiphash.h",
24492457
"src/third_party/utf8-decoder/utf8-decoder.h",
24502458
"src/tracing/trace-event.cc",
24512459
"src/tracing/trace-event.h",

deps/v8/PRESUBMIT.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
r"^tools[\\\/].*",
4444
)
4545

46+
_LICENSE_FILE = (
47+
r"LICENSE"
48+
)
4649

4750
# Regular expression that matches code which should not be run through cpplint.
4851
_NO_LINT_PATHS = (
@@ -301,8 +304,13 @@ def _CommonChecks(input_api, output_api):
301304
results.extend(_CheckCommitMessageBugEntry(input_api, output_api))
302305
results.extend(input_api.canned_checks.CheckPatchFormatted(
303306
input_api, output_api))
307+
308+
# License files are taken as is, even if they include gendered pronouns.
309+
license_filter = lambda path: input_api.FilterSourceFile(
310+
path, black_list=_LICENSE_FILE)
304311
results.extend(input_api.canned_checks.CheckGenderNeutral(
305-
input_api, output_api))
312+
input_api, output_api, source_file_filter=license_filter))
313+
306314
results.extend(_V8PresubmitChecks(input_api, output_api))
307315
results.extend(_CheckUnwantedDependencies(input_api, output_api))
308316
results.extend(
+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
CC0 1.0 Universal
2+
3+
Statement of Purpose
4+
5+
The laws of most jurisdictions throughout the world automatically confer
6+
exclusive Copyright and Related Rights (defined below) upon the creator and
7+
subsequent owner(s) (each and all, an "owner") of an original work of
8+
authorship and/or a database (each, a "Work").
9+
10+
Certain owners wish to permanently relinquish those rights to a Work for the
11+
purpose of contributing to a commons of creative, cultural and scientific
12+
works ("Commons") that the public can reliably and without fear of later
13+
claims of infringement build upon, modify, incorporate in other works, reuse
14+
and redistribute as freely as possible in any form whatsoever and for any
15+
purposes, including without limitation commercial purposes. These owners may
16+
contribute to the Commons to promote the ideal of a free culture and the
17+
further production of creative, cultural and scientific works, or to gain
18+
reputation or greater distribution for their Work in part through the use and
19+
efforts of others.
20+
21+
For these and/or other purposes and motivations, and without any expectation
22+
of additional consideration or compensation, the person associating CC0 with a
23+
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24+
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25+
and publicly distribute the Work under its terms, with knowledge of his or her
26+
Copyright and Related Rights in the Work and the meaning and intended legal
27+
effect of CC0 on those rights.
28+
29+
1. Copyright and Related Rights. A Work made available under CC0 may be
30+
protected by copyright and related or neighboring rights ("Copyright and
31+
Related Rights"). Copyright and Related Rights include, but are not limited
32+
to, the following:
33+
34+
i. the right to reproduce, adapt, distribute, perform, display, communicate,
35+
and translate a Work;
36+
37+
ii. moral rights retained by the original author(s) and/or performer(s);
38+
39+
iii. publicity and privacy rights pertaining to a person's image or likeness
40+
depicted in a Work;
41+
42+
iv. rights protecting against unfair competition in regards to a Work,
43+
subject to the limitations in paragraph 4(a), below;
44+
45+
v. rights protecting the extraction, dissemination, use and reuse of data in
46+
a Work;
47+
48+
vi. database rights (such as those arising under Directive 96/9/EC of the
49+
European Parliament and of the Council of 11 March 1996 on the legal
50+
protection of databases, and under any national implementation thereof,
51+
including any amended or successor version of such directive); and
52+
53+
vii. other similar, equivalent or corresponding rights throughout the world
54+
based on applicable law or treaty, and any national implementations thereof.
55+
56+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
57+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59+
and Related Rights and associated claims and causes of action, whether now
60+
known or unknown (including existing as well as future claims and causes of
61+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
62+
duration provided by applicable law or treaty (including future time
63+
extensions), (iii) in any current or future medium and for any number of
64+
copies, and (iv) for any purpose whatsoever, including without limitation
65+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66+
the Waiver for the benefit of each member of the public at large and to the
67+
detriment of Affirmer's heirs and successors, fully intending that such Waiver
68+
shall not be subject to revocation, rescission, cancellation, termination, or
69+
any other legal or equitable action to disrupt the quiet enjoyment of the Work
70+
by the public as contemplated by Affirmer's express Statement of Purpose.
71+
72+
3. Public License Fallback. Should any part of the Waiver for any reason be
73+
judged legally invalid or ineffective under applicable law, then the Waiver
74+
shall be preserved to the maximum extent permitted taking into account
75+
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76+
is so judged Affirmer hereby grants to each affected person a royalty-free,
77+
non transferable, non sublicensable, non exclusive, irrevocable and
78+
unconditional license to exercise Affirmer's Copyright and Related Rights in
79+
the Work (i) in all territories worldwide, (ii) for the maximum duration
80+
provided by applicable law or treaty (including future time extensions), (iii)
81+
in any current or future medium and for any number of copies, and (iv) for any
82+
purpose whatsoever, including without limitation commercial, advertising or
83+
promotional purposes (the "License"). The License shall be deemed effective as
84+
of the date CC0 was applied by Affirmer to the Work. Should any part of the
85+
License for any reason be judged legally invalid or ineffective under
86+
applicable law, such partial invalidity or ineffectiveness shall not
87+
invalidate the remainder of the License, and in such case Affirmer hereby
88+
affirms that he or she will not (i) exercise any of his or her remaining
89+
Copyright and Related Rights in the Work or (ii) assert any associated claims
90+
and causes of action with respect to the Work, in either case contrary to
91+
Affirmer's express Statement of Purpose.
92+
93+
4. Limitations and Disclaimers.
94+
95+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
96+
surrendered, licensed or otherwise affected by this document.
97+
98+
b. Affirmer offers the Work as-is and makes no representations or warranties
99+
of any kind concerning the Work, express, implied, statutory or otherwise,
100+
including without limitation warranties of title, merchantability, fitness
101+
for a particular purpose, non infringement, or the absence of latent or
102+
other defects, accuracy, or the present or absence of errors, whether or not
103+
discoverable, all to the greatest extent permissible under applicable law.
104+
105+
c. Affirmer disclaims responsibility for clearing rights of other persons
106+
that may apply to the Work or any use thereof, including without limitation
107+
any person's Copyright and Related Rights in the Work. Further, Affirmer
108+
disclaims responsibility for obtaining any necessary consents, permissions
109+
or other rights required for any use of the Work.
110+
111+
d. Affirmer understands and acknowledges that Creative Commons is not a
112+
party to this document and has no duty or obligation with respect to this
113+
CC0 or use of the Work.
114+
115+
For more information, please see
116+
<http://creativecommons.org/publicdomain/zero/1.0/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* SipHash reference C implementation
3+
*
4+
* Copyright (c) 2016 Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all
7+
* copyright and related and neighboring rights to this software to the public
8+
* domain worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
/*
16+
* Originally taken from https://github.com/veorq/SipHash/
17+
* Altered to match V8's use case.
18+
*/
19+
20+
#include <stdint.h>
21+
22+
#include "src/base/logging.h"
23+
#include "src/base/v8-fallthrough.h"
24+
25+
#define ROTL(x, b) (uint32_t)(((x) << (b)) | ((x) >> (32 - (b))))
26+
27+
#define SIPROUND \
28+
do { \
29+
v0 += v1; \
30+
v1 = ROTL(v1, 5); \
31+
v1 ^= v0; \
32+
v0 = ROTL(v0, 16); \
33+
v2 += v3; \
34+
v3 = ROTL(v3, 8); \
35+
v3 ^= v2; \
36+
v0 += v3; \
37+
v3 = ROTL(v3, 7); \
38+
v3 ^= v0; \
39+
v2 += v1; \
40+
v1 = ROTL(v1, 13); \
41+
v1 ^= v2; \
42+
v2 = ROTL(v2, 16); \
43+
} while (0)
44+
45+
// Simplified half-siphash-2-4 implementation for 4 byte input.
46+
uint32_t halfsiphash(const uint32_t value, const uint64_t seed) {
47+
uint32_t v0 = 0;
48+
uint32_t v1 = 0;
49+
uint32_t v2 = 0x6c796765;
50+
uint32_t v3 = 0x74656462;
51+
uint32_t k[2];
52+
memcpy(k, &seed, sizeof(seed));
53+
uint32_t b = 4 << 24;
54+
v3 ^= k[1];
55+
v2 ^= k[0];
56+
v1 ^= k[1];
57+
v0 ^= k[0];
58+
59+
v3 ^= value;
60+
61+
// 2 c-rounds
62+
SIPROUND;
63+
SIPROUND;
64+
65+
v0 ^= value;
66+
v3 ^= b;
67+
68+
// 2 c-rounds
69+
SIPROUND;
70+
SIPROUND;
71+
72+
v0 ^= b;
73+
v2 ^= 0xff;
74+
75+
// 4 d-rounds
76+
SIPROUND;
77+
SIPROUND;
78+
SIPROUND;
79+
SIPROUND;
80+
81+
b = v1 ^ v3;
82+
return b;
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* SipHash reference C implementation
3+
*
4+
* Copyright (c) 2016 Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all
7+
* copyright and related and neighboring rights to this software to the public
8+
* domain worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
#include <cstdint>
16+
17+
uint32_t halfsiphash(const uint32_t value, const uint64_t seed);

deps/v8/src/utils.h

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "src/base/platform/platform.h"
2222
#include "src/base/v8-fallthrough.h"
2323
#include "src/globals.h"
24+
#include "src/third_party/siphash/halfsiphash.h"
2425
#include "src/vector.h"
2526

2627
#if defined(V8_OS_AIX)
@@ -500,7 +501,11 @@ inline uint32_t ComputeLongHash(uint64_t key) {
500501
}
501502

502503
inline uint32_t ComputeSeededHash(uint32_t key, uint64_t seed) {
504+
#ifdef V8_USE_SIPHASH
505+
return halfsiphash(key, seed);
506+
#else
503507
return ComputeLongHash(static_cast<uint64_t>(key) ^ seed);
508+
#endif // V8_USE_SIPHASH
504509
}
505510

506511
inline uint32_t ComputePointerHash(void* ptr) {

deps/v8/test/cctest/test-code-stub-assembler.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ TEST(ComputeIntegerHash) {
345345
Handle<Object> result = ft.Call(key).ToHandleChecked();
346346

347347
uint32_t hash = ComputeSeededHash(k, isolate->heap()->HashSeed());
348-
Smi* expected = Smi::FromInt(hash & Smi::kMaxValue);
348+
Smi* expected = Smi::FromInt(hash);
349349
CHECK_EQ(expected, Smi::cast(*result));
350350
}
351351
}

deps/v8/test/cctest/test-hashcode.cc

+69
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "src/objects-inl.h"
1010
#include "src/objects.h"
11+
#include "src/third_party/siphash/halfsiphash.h"
12+
#include "src/utils.h"
1113
#include "src/v8.h"
1214

1315
#include "test/cctest/cctest.h"
@@ -227,5 +229,72 @@ TEST(TransitionSlowToFastWithPropertyArray) {
227229
CheckFastObject(obj, hash);
228230
}
229231

232+
namespace {
233+
234+
typedef uint32_t (*HashFunction)(uint32_t key, uint64_t seed);
235+
236+
void TestIntegerHashQuality(const int samples_log2, int num_buckets_log2,
237+
uint64_t seed, double max_var,
238+
HashFunction hash_function) {
239+
int samples = 1 << samples_log2;
240+
int num_buckets = 1 << num_buckets_log2;
241+
int mean = samples / num_buckets;
242+
int* buckets = new int[num_buckets];
243+
244+
for (int i = 0; i < num_buckets; i++) buckets[i] = 0;
245+
246+
for (int i = 0; i < samples; i++) {
247+
uint32_t hash = hash_function(i, seed);
248+
buckets[hash % num_buckets]++;
249+
}
250+
251+
int sum_deviation = 0;
252+
for (int i = 0; i < num_buckets; i++) {
253+
int deviation = abs(buckets[i] - mean);
254+
sum_deviation += deviation * deviation;
255+
}
256+
delete[] buckets;
257+
258+
double variation_coefficient = sqrt(sum_deviation * 1.0 / num_buckets) / mean;
259+
260+
printf("samples: 1 << %2d, buckets: 1 << %2d, var_coeff: %0.3f\n",
261+
samples_log2, num_buckets_log2, variation_coefficient);
262+
CHECK_LT(variation_coefficient, max_var);
263+
}
264+
uint32_t HalfSipHash(uint32_t key, uint64_t seed) {
265+
return halfsiphash(key, seed);
266+
}
267+
268+
uint32_t JenkinsHash(uint32_t key, uint64_t seed) {
269+
return ComputeLongHash(static_cast<uint64_t>(key) ^ seed);
270+
}
271+
272+
uint32_t DefaultHash(uint32_t key, uint64_t seed) {
273+
return ComputeSeededHash(key, seed);
274+
}
275+
} // anonymous namespace
276+
277+
void TestIntegerHashQuality(HashFunction hash_function) {
278+
TestIntegerHashQuality(17, 13, 0x123456789ABCDEFU, 0.4, hash_function);
279+
TestIntegerHashQuality(16, 12, 0x123456789ABCDEFU, 0.4, hash_function);
280+
TestIntegerHashQuality(15, 11, 0xFEDCBA987654321U, 0.4, hash_function);
281+
TestIntegerHashQuality(14, 10, 0xFEDCBA987654321U, 0.4, hash_function);
282+
TestIntegerHashQuality(13, 9, 1, 0.4, hash_function);
283+
TestIntegerHashQuality(12, 8, 1, 0.4, hash_function);
284+
285+
TestIntegerHashQuality(17, 10, 0x123456789ABCDEFU, 0.2, hash_function);
286+
TestIntegerHashQuality(16, 9, 0x123456789ABCDEFU, 0.2, hash_function);
287+
TestIntegerHashQuality(15, 8, 0xFEDCBA987654321U, 0.2, hash_function);
288+
TestIntegerHashQuality(14, 7, 0xFEDCBA987654321U, 0.2, hash_function);
289+
TestIntegerHashQuality(13, 6, 1, 0.2, hash_function);
290+
TestIntegerHashQuality(12, 5, 1, 0.2, hash_function);
291+
}
292+
293+
TEST(HalfSipHashQuality) { TestIntegerHashQuality(HalfSipHash); }
294+
295+
TEST(JenkinsHashQuality) { TestIntegerHashQuality(JenkinsHash); }
296+
297+
TEST(DefaultHashQuality) { TestIntegerHashQuality(DefaultHash); }
298+
230299
} // namespace internal
231300
} // namespace v8

0 commit comments

Comments
 (0)