Skip to content

Commit dd4c5d6

Browse files
targosRafaelGSS
authored andcommitted
src,test: add V8 API to test the hash seed
PR-URL: #58070 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 1d5d7b6 commit dd4c5d6

File tree

5 files changed

+38
-210
lines changed

5 files changed

+38
-210
lines changed

Makefile

Lines changed: 1 addition & 7 deletions
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)" && \
@@ -768,7 +762,7 @@ test-v8-benchmarks: v8 ## Run the v8 test suite, benchmarks.
768762
$(TAP_V8_BENCHMARKS)
769763
$(call convert_to_junit,$(TAP_V8_BENCHMARKS_JSON))
770764

771-
test-v8-updates: ## Run the v8 test suite, updates.
765+
test-v8-updates: all ## Run the v8 test suite, updates.
772766
$(PYTHON) tools/test.py $(PARALLEL_ARGS) --mode=$(BUILDTYPE_LOWER) v8-updates
773767

774768
test-v8-all: test-v8 test-v8-intl test-v8-benchmarks test-v8-updates ## Run the entire V8 test suite, including intl, benchmarks, and updates.

src/node_v8.cc

Lines changed: 10 additions & 0 deletions
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::CFunction;
3637
using v8::Context;
3738
using v8::FunctionCallbackInfo;
@@ -260,6 +261,12 @@ static bool FastIsStringOneByteRepresentation(Local<Value> receiver,
260261
CFunction fast_is_string_one_byte_representation_(
261262
CFunction::Make(FastIsStringOneByteRepresentation));
262263

264+
void GetHashSeed(const FunctionCallbackInfo<Value>& args) {
265+
Isolate* isolate = args.GetIsolate();
266+
uint64_t hash_seed = isolate->GetHashSeed();
267+
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, hash_seed));
268+
}
269+
263270
static const char* GetGCTypeName(v8::GCType gc_type) {
264271
switch (gc_type) {
265272
case v8::GCType::kGCTypeScavenge:
@@ -694,6 +701,8 @@ void Initialize(Local<Object> target,
694701
IsStringOneByteRepresentation,
695702
&fast_is_string_one_byte_representation_);
696703

704+
SetMethodNoSideEffect(context, target, "getHashSeed", GetHashSeed);
705+
697706
// GCProfiler
698707
Local<FunctionTemplate> t =
699708
NewFunctionTemplate(env->isolate(), GCProfiler::New);
@@ -721,6 +730,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
721730
registry->Register(UpdateHeapCodeStatisticsBuffer);
722731
registry->Register(UpdateHeapSpaceStatisticsBuffer);
723732
registry->Register(SetFlagsFromString);
733+
registry->Register(GetHashSeed);
724734
registry->Register(SetHeapSnapshotNearHeapLimit);
725735
registry->Register(GCProfiler::New);
726736
registry->Register(GCProfiler::Start);

test/fixtures/guess-hash-seed.js

Lines changed: 0 additions & 165 deletions
This file was deleted.

test/parallel/test-hash-seed.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import '../common/index.mjs';
2+
3+
import assert from 'node:assert';
4+
import { execFile } from 'node:child_process';
5+
import { promisify, debuglog } from 'node:util';
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+
}

test/pummel/test-hash-seed.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)