Skip to content

Commit 7a1913e

Browse files
targosnodejs-github-bot
authored andcommitted
src,test: add V8 API to test the hash seed
1 parent 2cc6d54 commit 7a1913e

File tree

5 files changed

+37
-209
lines changed

5 files changed

+37
-209
lines changed

Makefile

Lines changed: 0 additions & 6 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)" && \

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;
@@ -256,6 +257,12 @@ static bool FastIsStringOneByteRepresentation(Local<Value> receiver,
256257
CFunction fast_is_string_one_byte_representation_(
257258
CFunction::Make(FastIsStringOneByteRepresentation));
258259

260+
void GetHashSeed(const FunctionCallbackInfo<Value>& args) {
261+
Isolate* isolate = args.GetIsolate();
262+
uint64_t hash_seed = isolate->GetHashSeed();
263+
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, hash_seed));
264+
}
265+
259266
static const char* GetGCTypeName(v8::GCType gc_type) {
260267
switch (gc_type) {
261268
case v8::GCType::kGCTypeScavenge:
@@ -503,6 +510,8 @@ void Initialize(Local<Object> target,
503510
IsStringOneByteRepresentation,
504511
&fast_is_string_one_byte_representation_);
505512

513+
SetMethodNoSideEffect(context, target, "getHashSeed", GetHashSeed);
514+
506515
// GCProfiler
507516
Local<FunctionTemplate> t =
508517
NewFunctionTemplate(env->isolate(), GCProfiler::New);
@@ -518,6 +527,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
518527
registry->Register(UpdateHeapCodeStatisticsBuffer);
519528
registry->Register(UpdateHeapSpaceStatisticsBuffer);
520529
registry->Register(SetFlagsFromString);
530+
registry->Register(GetHashSeed);
521531
registry->Register(SetHeapSnapshotNearHeapLimit);
522532
registry->Register(GCProfiler::New);
523533
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)