Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: support child_process synchronous methods in custom snapshot #50943

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ class ExternalReferenceRegistry {
V(permission) \
V(process_methods) \
V(process_object) \
V(process_wrap) \
V(report) \
V(task_queue) \
V(tcp_wrap) \
V(tty_wrap) \
V(udp_wrap) \
V(url) \
V(util) \
V(pipe_wrap) \
Expand All @@ -122,6 +124,7 @@ class ExternalReferenceRegistry {
V(string_decoder) \
V(stream_wrap) \
V(signal_wrap) \
V(spawn_sync) \
V(trace_events) \
V(timers) \
V(types) \
Expand Down
9 changes: 9 additions & 0 deletions src/process_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -366,6 +367,10 @@ void SyncProcessRunner::Initialize(Local<Object> target,
SetMethod(context, target, "spawn", Spawn);
}

void SyncProcessRunner::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(Spawn);
}

void SyncProcessRunner::Spawn(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/spawn_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@

namespace node {



class ExternalReferenceRegistry;
class SyncProcessOutputBuffer;
class SyncProcessStdioPipe;
class SyncProcessRunner;
Expand Down Expand Up @@ -140,6 +139,7 @@ class SyncProcessRunner {
};

public:
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void Initialize(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
Expand Down
39 changes: 38 additions & 1 deletion src/udp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -133,6 +134,12 @@ void UDPWrapBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
SetProtoMethod(env->isolate(), t, "recvStop", RecvStop);
}

void UDPWrapBase::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(RecvStart);
registry->Register(RecvStop);
}

UDPWrap::UDPWrap(Environment* env, Local<Object> object)
: HandleWrap(env,
object,
Expand Down Expand Up @@ -229,6 +236,34 @@ void UDPWrap::Initialize(Local<Object> 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<UDPWrap, uv_udp_getpeername>);
registry->Register(GetSockOrPeerName<UDPWrap, uv_udp_getsockname>);
registry->Register(AddMembership);
registry->Register(DropMembership);
registry->Register(AddSourceSpecificMembership);
registry->Register(DropSourceSpecificMembership);
registry->Register(SetMulticastInterface);
registry->Register(SetLibuvInt32<uv_udp_set_multicast_ttl>);
registry->Register(SetLibuvInt32<uv_udp_set_multicast_loop>);
registry->Register(SetLibuvInt32<uv_udp_set_broadcast>);
registry->Register(SetLibuvInt32<uv_udp_set_ttl>);
registry->Register(BufferSize);
registry->Register(GetSendQueueSize);
registry->Register(GetSendQueueCount);
}

void UDPWrap::New(const FunctionCallbackInfo<Value>& args) {
CHECK(args.IsConstructCall());
Expand Down Expand Up @@ -807,3 +842,5 @@ void UDPWrap::GetSendQueueCount(const FunctionCallbackInfo<Value>& args) {
} // namespace node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(udp_wrap, node::UDPWrap::Initialize)
NODE_BINDING_EXTERNAL_REFERENCE(udp_wrap,
node::UDPWrap::RegisterExternalReferences)
3 changes: 3 additions & 0 deletions src/udp_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

namespace node {

class ExternalReferenceRegistry;
class UDPWrapBase;

// A listener that can be attached to an `UDPWrapBase` object and generally
Expand Down Expand Up @@ -110,6 +111,7 @@ class UDPWrapBase {
static void RecvStart(const v8::FunctionCallbackInfo<v8::Value>& args);
static void RecvStop(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AddMethods(Environment* env, v8::Local<v8::FunctionTemplate> t);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

private:
UDPListener* listener_ = nullptr;
Expand All @@ -126,6 +128,7 @@ class UDPWrap final : public HandleWrap,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static void GetFD(const v8::FunctionCallbackInfo<v8::Value>& args);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Open(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/snapshot/child-process-sync.js
Original file line number Diff line number Diff line change
@@ -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();
});
}
53 changes: 53 additions & 0 deletions test/parallel/test-snapshot-child-process-sync.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
}