Skip to content

Commit 374b329

Browse files
committed
src,test: add V8 API to test the hash seed
1 parent 084e62a commit 374b329

File tree

5 files changed

+37
-209
lines changed

5 files changed

+37
-209
lines changed

Diff for: Makefile

-6
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,6 @@ test-internet: all ## Run internet tests.
654654
test-tick-processor: all ## Run tick processor tests.
655655
$(PYTHON) tools/test.py $(PARALLEL_ARGS) tick-processor
656656

657-
.PHONY: test-hash-seed
658-
test-hash-seed: all ## Verifu that the hash seed used by V8 for hashing is random.
659-
$(NODE) test/pummel/test-hash-seed.js
660-
661657
.PHONY: test-doc
662658
test-doc: doc-only lint-md ## Build, lint, and verify the docs.
663659
@if [ "$(shell $(node_use_openssl_and_icu))" != "true" ]; then \
@@ -751,8 +747,6 @@ test-v8: v8 ## Run the V8 test suite on deps/v8.
751747
mjsunit cctest debugger inspector message preparser \
752748
$(TAP_V8)
753749
$(call convert_to_junit,$(TAP_V8_JSON))
754-
$(info Testing hash seed)
755-
$(MAKE) test-hash-seed
756750

757751
test-v8-intl: v8 ## Run the v8 test suite, intl tests.
758752
export PATH="$(NO_BIN_OVERRIDE_PATH)" && \

Diff for: src/node_v8.cc

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
namespace node {
3333
namespace v8_utils {
3434
using v8::Array;
35+
using v8::BigInt;
3536
using v8::Context;
3637
using v8::FunctionCallbackInfo;
3738
using v8::FunctionTemplate;
@@ -238,6 +239,12 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
238239
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
239240
}
240241

242+
void GetHashSeed(const FunctionCallbackInfo<Value>& args) {
243+
Isolate* isolate = args.GetIsolate();
244+
uint64_t hash_seed = isolate->GetHashSeed();
245+
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, hash_seed));
246+
}
247+
241248
static const char* GetGCTypeName(v8::GCType gc_type) {
242249
switch (gc_type) {
243250
case v8::GCType::kGCTypeScavenge:
@@ -478,6 +485,8 @@ void Initialize(Local<Object> target,
478485
// Export symbols used by v8.setFlagsFromString()
479486
SetMethod(context, target, "setFlagsFromString", SetFlagsFromString);
480487

488+
SetMethod(context, target, "getHashSeed", GetHashSeed);
489+
481490
// GCProfiler
482491
Local<FunctionTemplate> t =
483492
NewFunctionTemplate(env->isolate(), GCProfiler::New);
@@ -493,6 +502,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
493502
registry->Register(UpdateHeapCodeStatisticsBuffer);
494503
registry->Register(UpdateHeapSpaceStatisticsBuffer);
495504
registry->Register(SetFlagsFromString);
505+
registry->Register(GetHashSeed);
496506
registry->Register(SetHeapSnapshotNearHeapLimit);
497507
registry->Register(GCProfiler::New);
498508
registry->Register(GCProfiler::Start);

Diff for: test/fixtures/guess-hash-seed.js

-165
This file was deleted.

Diff for: test/parallel/test-hash-seed.mjs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import assert from 'node:assert';
2+
import { execFile } from 'node:child_process';
3+
import { promisify, debuglog } from 'node:util';
4+
5+
import '../common/index.mjs';
6+
7+
// This test verifies that the V8 hash seed is random
8+
// and unique between child processes.
9+
10+
const execFilePromise = promisify(execFile);
11+
const debug = debuglog('test');
12+
13+
const kRepetitions = 3;
14+
15+
const seeds = await Promise.all(Array.from({ length: kRepetitions }, generateSeed));
16+
debug(`Seeds: ${seeds}`);
17+
assert.strictEqual(new Set(seeds).size, seeds.length);
18+
assert.strictEqual(seeds.length, kRepetitions);
19+
20+
async function generateSeed() {
21+
const output = await execFilePromise(process.execPath, [
22+
'--expose-internals',
23+
'--print',
24+
'require("internal/test/binding").internalBinding("v8").getHashSeed()'
25+
]);
26+
return output.stdout.trim();
27+
}

Diff for: test/pummel/test-hash-seed.js

-38
This file was deleted.

0 commit comments

Comments
 (0)