Skip to content

Commit 0a8b1cd

Browse files
devsnekV8 LUCI CQ
authored andcommitted
implement rapidhash secret generation
Bug: 409717082 Change-Id: I471f33d66de32002f744aeba534c1d34f71e27d2 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6733490 Reviewed-by: Leszek Swirski <leszeks@chromium.org> Commit-Queue: snek <snek@chromium.org> Cr-Commit-Position: refs/heads/main@{#101499}
1 parent 8166a0b commit 0a8b1cd

37 files changed

Lines changed: 3807 additions & 3596 deletions

BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,7 @@ filegroup(
19881988
"src/numbers/conversions.cc",
19891989
"src/numbers/conversions.h",
19901990
"src/numbers/conversions-inl.h",
1991+
"src/numbers/hash-seed.h",
19911992
"src/numbers/hash-seed-inl.h",
19921993
"src/numbers/ieee754.cc",
19931994
"src/numbers/ieee754.h",
@@ -2629,6 +2630,7 @@ filegroup(
26292630
"src/zone/zone-type-traits.h",
26302631
"src/zone/zone-utils.h",
26312632
"third_party/rapidhash-v8/rapidhash.h",
2633+
"third_party/rapidhash-v8/secret.h",
26322634
"third_party/siphash/halfsiphash.cc",
26332635
"third_party/siphash/halfsiphash.h",
26342636
"third_party/utf8-decoder/utf8-decoder.h",

BUILD.gn

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ declare_args() {
523523

524524
# Expose F.p.caller and .arguments as own properties.
525525
v8_function_arguments_caller_are_own_props = false
526+
527+
# Use a hard-coded secret value when hashing.
528+
v8_use_default_hasher_secret = true
526529
}
527530

528531
# Derived defaults.
@@ -985,6 +988,7 @@ external_v8_defines = [
985988
"V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT=${v8_array_buffer_internal_field_count}",
986989
"V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT=${v8_array_buffer_view_internal_field_count}",
987990
"V8_PROMISE_INTERNAL_FIELD_COUNT=${v8_promise_internal_field_count}",
991+
"V8_USE_DEFAULT_HASHER_SECRET=${v8_use_default_hasher_secret}",
988992
"V8_ENABLE_CHECKS",
989993
"V8_ENABLE_MEMORY_ACCOUNTING_CHECKS",
990994
"V8_COMPRESS_POINTERS",
@@ -1013,6 +1017,7 @@ enabled_external_v8_defines = [
10131017
"V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT=${v8_array_buffer_internal_field_count}",
10141018
"V8_ARRAY_BUFFER_VIEW_INTERNAL_FIELD_COUNT=${v8_array_buffer_view_internal_field_count}",
10151019
"V8_PROMISE_INTERNAL_FIELD_COUNT=${v8_promise_internal_field_count}",
1020+
"V8_USE_DEFAULT_HASHER_SECRET=${v8_use_default_hasher_secret}",
10161021
]
10171022

10181023
if (v8_enable_v8_checks) {
@@ -2944,6 +2949,7 @@ generated_file("v8_generate_features_json") {
29442949
v8_random_seed = v8_random_seed
29452950
v8_use_perfetto = v8_use_perfetto
29462951
v8_use_siphash = v8_use_siphash
2952+
v8_use_default_hasher_secret = v8_use_default_hasher_secret
29472953
}
29482954
}
29492955

@@ -3988,6 +3994,7 @@ v8_header_set("v8_internal_headers") {
39883994
"src/numbers/conversions-inl.h",
39893995
"src/numbers/conversions.h",
39903996
"src/numbers/hash-seed-inl.h",
3997+
"src/numbers/hash-seed.h",
39913998
"src/numbers/ieee754.h",
39923999
"src/numbers/math-random.h",
39934000
"src/objects/all-objects-inl.h",
@@ -4405,6 +4412,7 @@ v8_header_set("v8_internal_headers") {
44054412
"src/zone/zone-utils.h",
44064413
"src/zone/zone.h",
44074414
"third_party/rapidhash-v8/rapidhash.h",
4415+
"third_party/rapidhash-v8/secret.h",
44084416
"third_party/siphash/halfsiphash.h",
44094417
"third_party/utf8-decoder/utf8-decoder.h",
44104418
]

src/DEPS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ specific_include_rules = {
134134
"string-hasher-inl\.h": [
135135
"+third_party/rapidhash-v8/rapidhash.h",
136136
],
137+
"heap\.cc": [
138+
"+third_party/rapidhash-v8/secret.h",
139+
],
140+
"hash-seed-inl\.h": [
141+
"+third_party/rapidhash-v8/secret.h",
142+
],
137143
}

src/api/api.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11017,7 +11017,7 @@ Maybe<std::string> Isolate::ValidateAndCanonicalizeUnicodeLocaleId(
1101711017

1101811018
uint64_t Isolate::GetHashSeed() {
1101911019
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(this);
11020-
return HashSeed(i_isolate);
11020+
return i::HashSeed(i_isolate).seed();
1102111021
}
1102211022

1102311023
#if defined(V8_ENABLE_ETW_STACK_WALKING)

src/ast/ast-value-factory.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ std::forward_list<const AstRawString*> AstConsString::ToRawStrings() const {
292292
return result;
293293
}
294294

295-
AstStringConstants::AstStringConstants(Isolate* isolate, uint64_t hash_seed)
295+
AstStringConstants::AstStringConstants(Isolate* isolate,
296+
const HashSeed hash_seed)
296297
: zone_(isolate->allocator(), ZONE_NAME),
297298
string_table_(),
298299
hash_seed_(hash_seed) {

src/ast/ast-value-factory.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "src/common/globals.h"
3636
#include "src/handles/handles.h"
3737
#include "src/numbers/conversions.h"
38+
#include "src/numbers/hash-seed.h"
3839
#include "src/objects/name.h"
3940
#include "src/zone/zone.h"
4041

@@ -299,7 +300,7 @@ class AstStringConstants final {
299300
0 SINGLE_CHARACTER_ASCII_AST_STRING_CONSTANTS(F);
300301
#undef F
301302

302-
AstStringConstants(Isolate* isolate, uint64_t hash_seed);
303+
AstStringConstants(Isolate* isolate, const HashSeed hash_seed);
303304
AstStringConstants(const AstStringConstants&) = delete;
304305
AstStringConstants& operator=(const AstStringConstants&) = delete;
305306

@@ -308,7 +309,7 @@ class AstStringConstants final {
308309
AST_STRING_CONSTANTS(F)
309310
#undef F
310311

311-
uint64_t hash_seed() const { return hash_seed_; }
312+
const HashSeed hash_seed() const { return hash_seed_; }
312313
const AstRawStringMap* string_table() const { return &string_table_; }
313314
const AstRawString* one_character_string(int c) const {
314315
DCHECK_GE(c, 0);
@@ -329,7 +330,7 @@ class AstStringConstants final {
329330
private:
330331
Zone zone_;
331332
AstRawStringMap string_table_;
332-
uint64_t hash_seed_;
333+
const HashSeed hash_seed_;
333334

334335
#define F(name, str) AstRawString* name##_;
335336
AST_STRING_CONSTANTS(F)
@@ -339,12 +340,12 @@ class AstStringConstants final {
339340
class AstValueFactory {
340341
public:
341342
AstValueFactory(Zone* zone, const AstStringConstants* string_constants,
342-
uint64_t hash_seed)
343+
const HashSeed hash_seed)
343344
: AstValueFactory(zone, zone, string_constants, hash_seed) {}
344345

345346
AstValueFactory(Zone* ast_raw_string_zone, Zone* single_parse_zone,
346347
const AstStringConstants* string_constants,
347-
uint64_t hash_seed)
348+
const HashSeed hash_seed)
348349
: string_table_(string_constants->string_table()),
349350
strings_(nullptr),
350351
strings_end_(&strings_),
@@ -433,7 +434,7 @@ class AstValueFactory {
433434
Zone* ast_raw_string_zone_;
434435
Zone* single_parse_zone_;
435436

436-
uint64_t hash_seed_;
437+
const HashSeed hash_seed_;
437438
};
438439

439440
extern template EXPORT_TEMPLATE_DECLARE(

src/codegen/external-reference.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,8 @@ FUNCTION_REFERENCE(jsreceiver_create_identity_hash,
14041404

14051405
static uint32_t ComputeSeededIntegerHash(Isolate* isolate, int32_t key) {
14061406
DisallowGarbageCollection no_gc;
1407-
return ComputeSeededHash(static_cast<uint32_t>(key), HashSeed(isolate));
1407+
return ComputeSeededHash(static_cast<uint32_t>(key),
1408+
HashSeed(isolate).seed());
14081409
}
14091410

14101411
FUNCTION_REFERENCE(compute_integer_hash, ComputeSeededIntegerHash)

src/debug/debug-interface.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ uint32_t WasmScript::GetFunctionHash(int function_index) {
926926
wire_bytes.GetFunctionBytes(&func);
927927
// TODO(herhut): Maybe also take module, name and signature into account.
928928
return i::StringHasher::HashSequentialString(function_bytes.begin(),
929-
function_bytes.length(), 0);
929+
function_bytes.length(),
930+
internal::HashSeed::Default());
930931
}
931932

932933
Maybe<v8::MemorySpan<const uint8_t>> WasmScript::GetModuleBuildId() const {

src/heap/heap.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
#include "src/tracing/trace-event.h"
129129
#include "src/utils/utils-inl.h"
130130
#include "src/utils/utils.h"
131+
#include "third_party/rapidhash-v8/secret.h"
131132

132133
#if V8_ENABLE_WEBASSEMBLY
133134
#include "src/wasm/wasm-engine.h"
@@ -6163,9 +6164,20 @@ void Heap::InitializeHashSeed() {
61636164
} else {
61646165
new_hash_seed = static_cast<uint64_t>(v8_flags.hash_seed);
61656166
}
6167+
61666168
Tagged<ByteArray> hash_seed = ReadOnlyRoots(this).hash_seed();
6169+
61676170
MemCopy(hash_seed->begin(), reinterpret_cast<uint8_t*>(&new_hash_seed),
61686171
kInt64Size);
6172+
6173+
#if V8_USE_DEFAULT_HASHER_SECRET
6174+
MemCopy(hash_seed->begin() + kInt64Size,
6175+
reinterpret_cast<const uint8_t*>(RAPIDHASH_DEFAULT_SECRET),
6176+
kInt64Size * 3);
6177+
#else
6178+
rapidhash_make_secret(new_hash_seed, reinterpret_cast<uint64_t*>(
6179+
hash_seed->begin() + kInt64Size));
6180+
#endif // V8_USE_DEFAULT_HASHER_SECRET
61696181
}
61706182

61716183
std::shared_ptr<v8::TaskRunner> Heap::GetForegroundTaskRunner(

src/heap/setup-heap-internal.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,8 @@ bool Heap::CreateImportantReadOnlyObjects() {
938938
// Hash seed for strings
939939

940940
Factory* factory = isolate()->factory();
941-
set_hash_seed(*factory->NewByteArray(kInt64Size, AllocationType::kReadOnly));
941+
set_hash_seed(
942+
*factory->NewByteArray(kInt64Size * 4, AllocationType::kReadOnly));
942943
InitializeHashSeed();
943944

944945
// Important strings and symbols

0 commit comments

Comments
 (0)