From 8450b7ff35cfb7c971732bacaa2305200f9ea97f Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 27 Nov 2023 14:35:25 +0100 Subject: [PATCH 1/4] src: register process_wrap external references --- src/node_external_reference.h | 1 + src/process_wrap.cc | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/src/node_external_reference.h b/src/node_external_reference.h index f15c97c03d2e9d..e516bf6471d4af 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -110,6 +110,7 @@ class ExternalReferenceRegistry { V(permission) \ V(process_methods) \ V(process_object) \ + V(process_wrap) \ V(report) \ V(task_queue) \ V(tcp_wrap) \ diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 42a746308ba8ac..a9b0cea29cbd15 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -20,6 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. #include "env-inl.h" +#include "node_external_reference.h" #include "permission/permission.h" #include "stream_base-inl.h" #include "stream_wrap.h" @@ -67,6 +68,12 @@ class ProcessWrap : public HandleWrap { SetConstructorFunction(context, target, "Process", constructor); } + static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { + registry->Register(New); + registry->Register(Spawn); + registry->Register(Kill); + } + SET_NO_MEMORY_INFO() SET_MEMORY_INFO_NAME(ProcessWrap) SET_SELF_SIZE(ProcessWrap) @@ -325,3 +332,5 @@ class ProcessWrap : public HandleWrap { } // namespace node NODE_BINDING_CONTEXT_AWARE_INTERNAL(process_wrap, node::ProcessWrap::Initialize) +NODE_BINDING_EXTERNAL_REFERENCE(process_wrap, + node::ProcessWrap::RegisterExternalReferences) From 63eeca2a5887ee7c85e4af9eed245ba9acb58e42 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 27 Nov 2023 14:35:48 +0100 Subject: [PATCH 2/4] src: register spawn_sync external references --- src/node_external_reference.h | 1 + src/spawn_sync.cc | 7 +++++++ src/spawn_sync.h | 4 ++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/node_external_reference.h b/src/node_external_reference.h index e516bf6471d4af..09a24aeef1b07f 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -123,6 +123,7 @@ class ExternalReferenceRegistry { V(string_decoder) \ V(stream_wrap) \ V(signal_wrap) \ + V(spawn_sync) \ V(trace_events) \ V(timers) \ V(types) \ diff --git a/src/spawn_sync.cc b/src/spawn_sync.cc index b3c0fabafdaad2..3b73dc4a614b9a 100644 --- a/src/spawn_sync.cc +++ b/src/spawn_sync.cc @@ -22,6 +22,7 @@ #include "spawn_sync.h" #include "debug_utils-inl.h" #include "env-inl.h" +#include "node_external_reference.h" #include "node_internals.h" #include "string_bytes.h" #include "util-inl.h" @@ -366,6 +367,10 @@ void SyncProcessRunner::Initialize(Local target, SetMethod(context, target, "spawn", Spawn); } +void SyncProcessRunner::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(Spawn); +} void SyncProcessRunner::Spawn(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); @@ -1107,3 +1112,5 @@ void SyncProcessRunner::KillTimerCloseCallback(uv_handle_t* handle) { NODE_BINDING_CONTEXT_AWARE_INTERNAL(spawn_sync, node::SyncProcessRunner::Initialize) +NODE_BINDING_EXTERNAL_REFERENCE( + spawn_sync, node::SyncProcessRunner::RegisterExternalReferences) diff --git a/src/spawn_sync.h b/src/spawn_sync.h index 81a2f2aaa899ba..d5b74e67d83094 100644 --- a/src/spawn_sync.h +++ b/src/spawn_sync.h @@ -30,8 +30,7 @@ namespace node { - - +class ExternalReferenceRegistry; class SyncProcessOutputBuffer; class SyncProcessStdioPipe; class SyncProcessRunner; @@ -140,6 +139,7 @@ class SyncProcessRunner { }; public: + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static void Initialize(v8::Local target, v8::Local unused, v8::Local context, From d25a83279acdb81ffa056cd5700912b54db0ee85 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 27 Nov 2023 14:36:04 +0100 Subject: [PATCH 3/4] src: register udp_wrap external references --- src/node_external_reference.h | 1 + src/udp_wrap.cc | 39 ++++++++++++++++++++++++++++++++++- src/udp_wrap.h | 3 +++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/node_external_reference.h b/src/node_external_reference.h index 09a24aeef1b07f..b1b6ca31766326 100644 --- a/src/node_external_reference.h +++ b/src/node_external_reference.h @@ -115,6 +115,7 @@ class ExternalReferenceRegistry { V(task_queue) \ V(tcp_wrap) \ V(tty_wrap) \ + V(udp_wrap) \ V(url) \ V(util) \ V(pipe_wrap) \ diff --git a/src/udp_wrap.cc b/src/udp_wrap.cc index 4208a213260e26..d4249d7aac56e0 100644 --- a/src/udp_wrap.cc +++ b/src/udp_wrap.cc @@ -21,10 +21,11 @@ #include "udp_wrap.h" #include "env-inl.h" +#include "handle_wrap.h" #include "node_buffer.h" #include "node_errors.h" +#include "node_external_reference.h" #include "node_sockaddr-inl.h" -#include "handle_wrap.h" #include "req_wrap-inl.h" #include "util-inl.h" @@ -133,6 +134,12 @@ void UDPWrapBase::AddMethods(Environment* env, Local t) { SetProtoMethod(env->isolate(), t, "recvStop", RecvStop); } +void UDPWrapBase::RegisterExternalReferences( + ExternalReferenceRegistry* registry) { + registry->Register(RecvStart); + registry->Register(RecvStop); +} + UDPWrap::UDPWrap(Environment* env, Local object) : HandleWrap(env, object, @@ -229,6 +236,34 @@ void UDPWrap::Initialize(Local target, constants).Check(); } +void UDPWrap::RegisterExternalReferences(ExternalReferenceRegistry* registry) { + UDPWrapBase::RegisterExternalReferences(registry); + registry->Register(New); + registry->Register(GetFD); + + registry->Register(Open); + registry->Register(Bind); + registry->Register(Connect); + registry->Register(Send); + registry->Register(Bind6); + registry->Register(Connect6); + registry->Register(Send6); + registry->Register(Disconnect); + registry->Register(GetSockOrPeerName); + registry->Register(GetSockOrPeerName); + registry->Register(AddMembership); + registry->Register(DropMembership); + registry->Register(AddSourceSpecificMembership); + registry->Register(DropSourceSpecificMembership); + registry->Register(SetMulticastInterface); + registry->Register(SetLibuvInt32); + registry->Register(SetLibuvInt32); + registry->Register(SetLibuvInt32); + registry->Register(SetLibuvInt32); + registry->Register(BufferSize); + registry->Register(GetSendQueueSize); + registry->Register(GetSendQueueCount); +} void UDPWrap::New(const FunctionCallbackInfo& args) { CHECK(args.IsConstructCall()); @@ -807,3 +842,5 @@ void UDPWrap::GetSendQueueCount(const FunctionCallbackInfo& args) { } // namespace node NODE_BINDING_CONTEXT_AWARE_INTERNAL(udp_wrap, node::UDPWrap::Initialize) +NODE_BINDING_EXTERNAL_REFERENCE(udp_wrap, + node::UDPWrap::RegisterExternalReferences) diff --git a/src/udp_wrap.h b/src/udp_wrap.h index 3b8ca7df351a36..c0914dbf3a7f3f 100644 --- a/src/udp_wrap.h +++ b/src/udp_wrap.h @@ -32,6 +32,7 @@ namespace node { +class ExternalReferenceRegistry; class UDPWrapBase; // A listener that can be attached to an `UDPWrapBase` object and generally @@ -110,6 +111,7 @@ class UDPWrapBase { static void RecvStart(const v8::FunctionCallbackInfo& args); static void RecvStop(const v8::FunctionCallbackInfo& args); static void AddMethods(Environment* env, v8::Local t); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); private: UDPListener* listener_ = nullptr; @@ -126,6 +128,7 @@ class UDPWrap final : public HandleWrap, v8::Local unused, v8::Local context, void* priv); + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); static void GetFD(const v8::FunctionCallbackInfo& args); static void New(const v8::FunctionCallbackInfo& args); static void Open(const v8::FunctionCallbackInfo& args); From 3c77d715ffc7375110148f84e227382ca24a6b04 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 27 Nov 2023 15:22:28 +0100 Subject: [PATCH 4/4] test: test syncrhnous methods of child_process in snapshot These currently work in snapshot builder scripts. Asynchronous methods are not supported yet. --- test/fixtures/snapshot/child-process-sync.js | 25 +++++++++ .../test-snapshot-child-process-sync.js | 53 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/fixtures/snapshot/child-process-sync.js create mode 100644 test/parallel/test-snapshot-child-process-sync.js diff --git a/test/fixtures/snapshot/child-process-sync.js b/test/fixtures/snapshot/child-process-sync.js new file mode 100644 index 00000000000000..5ffacb05357df6 --- /dev/null +++ b/test/fixtures/snapshot/child-process-sync.js @@ -0,0 +1,25 @@ +'use strict'; + +const { + setDeserializeMainFunction, + isBuildingSnapshot +} = require('v8').startupSnapshot; + +function spawn() { + const { spawnSync, execFileSync, execSync } = require('child_process'); + spawnSync(process.execPath, [ __filename, 'spawnSync' ], { stdio: 'inherit' }); + execSync(`"${process.execPath}" "${__filename}" "execSync"`, { stdio: 'inherit' }); + execFileSync(process.execPath, [ __filename, 'execFileSync' ], { stdio: 'inherit' }); +} + +if (process.argv[2] !== undefined) { + console.log('From child process', process.argv[2]); +} else { + spawn(); +} + +if (isBuildingSnapshot()) { + setDeserializeMainFunction(() => { + spawn(); + }); +} diff --git a/test/parallel/test-snapshot-child-process-sync.js b/test/parallel/test-snapshot-child-process-sync.js new file mode 100644 index 00000000000000..3ec7b2952853ba --- /dev/null +++ b/test/parallel/test-snapshot-child-process-sync.js @@ -0,0 +1,53 @@ +'use strict'; + +// This tests that process.cwd() is accurate when +// restoring state from a snapshot + +require('../common'); +const { spawnSyncAndExitWithoutError } = require('../common/child_process'); +const tmpdir = require('../common/tmpdir'); +const fixtures = require('../common/fixtures'); +const assert = require('assert'); + +tmpdir.refresh(); +const blobPath = tmpdir.resolve('snapshot.blob'); +const file = fixtures.path('snapshot', 'child-process-sync.js'); +const expected = [ + 'From child process spawnSync', + 'From child process execSync', + 'From child process execFileSync', +]; + +{ + // Create the snapshot. + spawnSyncAndExitWithoutError(process.execPath, [ + '--snapshot-blob', + blobPath, + '--build-snapshot', + file, + ], { + cwd: tmpdir.path, + }, { + trim: true, + stdout(output) { + assert.deepStrictEqual(output.split('\n'), expected); + return true; + } + }); +} + +{ + spawnSyncAndExitWithoutError(process.execPath, [ + '--snapshot-blob', + blobPath, + file, + ], { + cwd: tmpdir.path, + }, { + trim: true, + stdout(output) { + assert.deepStrictEqual(output.split('\n'), expected); + return true; + } + }); +}