diff --git a/lib/internal/child_process/serialization.js b/lib/internal/child_process/serialization.js index 989f748734a2..9aeb994b954e 100644 --- a/lib/internal/child_process/serialization.js +++ b/lib/internal/child_process/serialization.js @@ -1,78 +1,60 @@ 'use strict'; const { - ArrayPrototypePush, JSONParse, JSONStringify, StringPrototypeSplit, Symbol, - TypedArrayPrototypeSubarray, } = primordials; const { Buffer } = require('buffer'); const { StringDecoder } = require('string_decoder'); -const { serialize, deserialize } = internalBinding('ipc_serdes'); +const { + serialize, + IPCChannelFramer, + kFramerBuffering, + kFramerError, +} = internalBinding('ipc_serdes'); const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap'); -const kMessageBuffer = Symbol('kMessageBuffer'); -const kMessageBufferSize = Symbol('kMessageBufferSize'); +const kFramer = Symbol('kFramer'); const kJSONBuffer = Symbol('kJSONBuffer'); const kStringDecoder = Symbol('kStringDecoder'); // Messages are parsed in either of the following formats: -// - Newline-delimited JSON, or // - V8-serialized buffers, prefixed with their length as a big endian uint32 -// (aka 'advanced') +// (aka 'advanced'), or +// - newline-delimited JSON. +// +// The 'advanced' read path is framed natively: IPCChannelFramer accumulates +// partial reads in C++ and hands back an array of complete, deserialized +// messages, so JavaScript never reframes the byte stream. The 'json' read path +// stays in JavaScript: its StringDecoder + split('\n') pipeline already avoids +// copies via V8 rope concatenation and O(1) substrings, and is measurably +// faster than reframing the bytes in C++. + +// A frame that failed to deserialize has already been consumed by the framer. +// Deliver the messages decoded from the same read, then throw its error. +function* yieldThenThrow(messages, error) { + for (let i = 0; i < messages.length; i++) + yield messages[i]; + throw error; +} + const advanced = { initMessageChannel(channel) { - channel[kMessageBuffer] = []; - channel[kMessageBufferSize] = 0; + channel[kFramer] = new IPCChannelFramer(); channel.buffering = false; }, - *parseChannelMessages(channel, readData) { - if (readData.length === 0) return; - - if (channel[kMessageBufferSize] && channel[kMessageBuffer][0].length < 4) { - // Message length split into two buffers, so let's concatenate it. - channel[kMessageBuffer][0] = Buffer.concat([channel[kMessageBuffer][0], readData]); - } else { - ArrayPrototypePush(channel[kMessageBuffer], readData); - } - channel[kMessageBufferSize] += readData.length; - - // Index 0 should always be present because we just pushed data into it. - let messageBufferHead = channel[kMessageBuffer][0]; - while (messageBufferHead.length >= 4) { - // We call `readUInt32BE` manually here, because this is faster than first converting - // it to a buffer and using `readUInt32BE` on that. - const fullMessageSize = (( - messageBufferHead[0] << 24 | - messageBufferHead[1] << 16 | - messageBufferHead[2] << 8 | - messageBufferHead[3] - ) >>> 0) + 4; - - if (channel[kMessageBufferSize] < fullMessageSize) break; - - const concatenatedBuffer = channel[kMessageBuffer].length === 1 ? - channel[kMessageBuffer][0] : - Buffer.concat( - channel[kMessageBuffer], - channel[kMessageBufferSize], - ); - - const serializedMessage = - TypedArrayPrototypeSubarray(concatenatedBuffer, 4, fullMessageSize); - - messageBufferHead = TypedArrayPrototypeSubarray(concatenatedBuffer, fullMessageSize); - channel[kMessageBufferSize] = messageBufferHead.length; - channel[kMessageBuffer] = - channel[kMessageBufferSize] !== 0 ? [messageBufferHead] : []; - - yield deserialize(serializedMessage); - } - - channel.buffering = channel[kMessageBufferSize] > 0; + parseChannelMessages(channel, readData) { + if (readData.length === 0) return []; + const framer = channel[kFramer]; + const messages = framer.read(readData); + const status = framer.status(); + channel.buffering = (status & kFramerBuffering) !== 0; + if ((status & kFramerError) !== 0) + return yieldThenThrow(messages, framer.takeError()); + return messages; }, writeChannelMessage(channel, req, message, handle) { diff --git a/src/node_ipc_serdes.cc b/src/node_ipc_serdes.cc index 05d5ae7ccffe..8556c0c7772f 100644 --- a/src/node_ipc_serdes.cc +++ b/src/node_ipc_serdes.cc @@ -1,12 +1,17 @@ +#include "base_object-inl.h" #include "env-inl.h" +#include "memory_tracker-inl.h" #include "node_buffer.h" #include "node_errors.h" #include "node_external_reference.h" #include "node_internals.h" #include "util-inl.h" +#include #include +#include #include +#include // Native implementation of the `advanced` child_process IPC serialization // codec previously implemented in lib/internal/child_process/serialization.js @@ -19,6 +24,7 @@ namespace node { +using v8::Array; using v8::ArrayBuffer; using v8::ArrayBufferView; using v8::BackingStore; @@ -31,18 +37,21 @@ using v8::Float16Array; using v8::Float32Array; using v8::Float64Array; using v8::FunctionCallbackInfo; +using v8::FunctionTemplate; using v8::Int16Array; using v8::Int32Array; using v8::Int8Array; using v8::Isolate; using v8::Just; using v8::Local; +using v8::LocalVector; using v8::Maybe; using v8::MaybeLocal; using v8::Name; using v8::Nothing; using v8::Object; using v8::String; +using v8::TryCatch; using v8::Uint16Array; using v8::Uint32Array; using v8::Uint8Array; @@ -280,14 +289,18 @@ class IPCDeserializerDelegate : public ValueDeserializer::Delegate { if (!deserializer_->ReadRawBytes(byte_length, &data)) return {}; const size_t bytes_per_element = BytesPerElement(type_index); - const size_t offset_in_ab = static_cast(data) - - static_cast(ab_->Data()); // Reuse the backing ArrayBuffer when the data is suitably aligned, // otherwise copy into a fresh aligned buffer. Mirrors _readHostObject() - // in lib/v8.js. - if (offset_in_ab % bytes_per_element == 0) { - return MakeView(env_, type_index, ab_, offset_in_ab, byte_length); + // in lib/v8.js. A small frame reassembled across reads lives in a buffer + // the framer reuses, with no ArrayBuffer to borrow (`ab_` is empty), so + // its views are always copied. + if (!ab_.IsEmpty()) { + const size_t offset_in_ab = static_cast(data) - + static_cast(ab_->Data()); + if (offset_in_ab % bytes_per_element == 0) { + return MakeView(env_, type_index, ab_, offset_in_ab, byte_length); + } } std::shared_ptr store = ArrayBuffer::NewBackingStore(isolate, byte_length); @@ -339,11 +352,36 @@ static void Serialize(const FunctionCallbackInfo& args) { } } -static void Deserialize(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); +// Deserializes a single `advanced` payload: the V8 wire header followed by the +// value, without the 4-byte big-endian length prefix. When `ab` is non-empty +// it is the ArrayBuffer backing `data`, which the delegate may borrow for +// zero-copy ArrayBufferView host objects; when it is empty, host objects are +// copied into fresh buffers. +static MaybeLocal DeserializeAdvancedPayload(Environment* env, + Local ab, + const uint8_t* data, + size_t length) { Isolate* isolate = env->isolate(); Local context = env->context(); + IPCDeserializerDelegate delegate(env, ab); + ValueDeserializer deserializer(isolate, data, length, &delegate); + delegate.set_deserializer(&deserializer); + + bool read_header; + if (!deserializer.ReadHeader(context).To(&read_header)) return {}; + return deserializer.ReadValue(context); +} + +static inline uint32_t ReadUInt32BE(const uint8_t* b) { + return (static_cast(b[0]) << 24) | + (static_cast(b[1]) << 16) | + (static_cast(b[2]) << 8) | static_cast(b[3]); +} + +static void Deserialize(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + CHECK(args[0]->IsArrayBufferView()); Local view = args[0].As(); Local ab = view->Buffer(); @@ -351,30 +389,290 @@ static void Deserialize(const FunctionCallbackInfo& args) { const size_t byte_length = view->ByteLength(); const uint8_t* data = static_cast(ab->Data()) + byte_offset; - IPCDeserializerDelegate delegate(env, ab); - ValueDeserializer deserializer(isolate, data, byte_length, &delegate); - delegate.set_deserializer(&deserializer); - - bool read_header; - if (!deserializer.ReadHeader(context).To(&read_header)) return; - Local value; - if (deserializer.ReadValue(context).ToLocal(&value)) { + if (DeserializeAdvancedPayload(env, ab, data, byte_length).ToLocal(&value)) { args.GetReturnValue().Set(value); } } +// Frames that span reads and whose payload is at most this many bytes are +// accumulated into a reused buffer rather than a fresh BackingStore. +static constexpr size_t kMaxSmallPayload = 64 * 1024; + +// Bits of the value returned by IPCChannelFramer.prototype.status(). +static constexpr int kFramerBuffering = 1; // a partial frame is buffered +static constexpr int kFramerError = 2; // takeError() has an error to throw + +// Per-channel native framer for the `advanced` codec. It turns the raw IPC +// byte stream into complete, deserialized messages, so JavaScript receives +// whole messages and never reframes partial reads itself. +// +// Frames contained entirely within one read are deserialized in place, and +// their ArrayBufferView host objects borrow the read's ArrayBuffer. A small +// frame that spans reads is accumulated into a buffer the framer reuses, whose +// size is bounded by kMaxSmallPayload. A larger one is accumulated into a +// BackingStore sized from its length prefix; once complete, that store becomes +// the frame's ArrayBuffer, so its host objects borrow it instead of copying, +// and the memory is released by the GC with them. +// +// Each frame is consumed before it is deserialized, so a frame that fails to +// deserialize never desynchronizes the stream. Its error is held until the +// rest of the read has been processed, and JavaScript throws it after +// delivering the messages decoded from the same read. +class IPCChannelFramer : public BaseObject { + public: + IPCChannelFramer(Environment* env, Local wrap) + : BaseObject(env, wrap) { + MakeWeak(); + } + + static void New(const FunctionCallbackInfo& args); + static void Read(const FunctionCallbackInfo& args); + static void Status(const FunctionCallbackInfo& args); + static void TakeError(const FunctionCallbackInfo& args); + + void MemoryInfo(MemoryTracker* tracker) const override { + tracker->TrackFieldWithSize("payload", + payload_ ? payload_->ByteLength() : 0); + tracker->TrackFieldWithSize("small_payload", small_payload_.capacity()); + tracker->TrackField("pending_error", pending_error_); + } + SET_MEMORY_INFO_NAME(IPCChannelFramer) + SET_SELF_SIZE(IPCChannelFramer) + + private: + // Append the messages decoded from `data` to `out`, buffering any trailing + // partial frame for the next read. Return false, with a pending exception, + // only if execution is terminating; other errors are held in + // `pending_error_`. + bool ReadAdvanced(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out); + // Deserialize one payload and append it to `out`. + bool Deliver(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out, + TryCatch* try_catch); + // Start accumulating a payload of `size` bytes that spans reads. + bool StartPayload(size_t size, TryCatch* try_catch); + // Keep the first caught error for takeError(). Return false if execution is + // terminating, in which case the exception is rethrown. + bool RecordError(TryCatch* try_catch); + + // Bytes of a length prefix split across reads. + uint8_t header_[4]; + size_t header_size_ = 0; + // Payload of a frame that spans reads, written to `payload_data_`: either + // `small_payload_` or, for a payload larger than kMaxSmallPayload, + // `payload_`. If allocating `payload_` failed, `discarding_` is set and the + // payload bytes are counted and dropped. + bool in_payload_ = false; + bool discarding_ = false; + uint8_t* payload_data_ = nullptr; + std::vector small_payload_; + std::unique_ptr payload_; + size_t payload_size_ = 0; + size_t payload_filled_ = 0; + v8::Global pending_error_; +}; + +void IPCChannelFramer::New(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + if (!args.IsConstructCall()) { + return THROW_ERR_CONSTRUCT_CALL_REQUIRED( + env, + "Class constructor IPCChannelFramer cannot be invoked without 'new'"); + } + new IPCChannelFramer(env, args.This()); +} + +bool IPCChannelFramer::RecordError(TryCatch* try_catch) { + if (try_catch->HasTerminated() || !try_catch->CanContinue()) { + try_catch->ReThrow(); + return false; + } + if (pending_error_.IsEmpty()) { + Isolate* isolate = env()->isolate(); + Local exception = try_catch->Exception(); + if (exception.IsEmpty()) { + exception = ERR_INVALID_STATE(isolate, "Invalid IPC message"); + } + pending_error_.Reset(isolate, exception); + } + try_catch->Reset(); + return true; +} + +bool IPCChannelFramer::Deliver(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out, + TryCatch* try_catch) { + Local message; + if (DeserializeAdvancedPayload(env(), ab, data, length).ToLocal(&message)) { + out->push_back(message); + return true; + } + return RecordError(try_catch); +} + +bool IPCChannelFramer::StartPayload(size_t size, TryCatch* try_catch) { + in_payload_ = true; + payload_size_ = size; + payload_filled_ = 0; + discarding_ = false; + if (size <= kMaxSmallPayload) { + small_payload_.resize(size); + payload_data_ = small_payload_.data(); + return true; + } + payload_ = ArrayBuffer::NewBackingStore( + env()->isolate(), + size, + v8::BackingStoreInitializationMode::kUninitialized, + v8::BackingStoreOnFailureMode::kReturnNull); + if (payload_) { + payload_data_ = static_cast(payload_->Data()); + return true; + } + discarding_ = true; + payload_data_ = nullptr; + THROW_ERR_MEMORY_ALLOCATION_FAILED(env()); + return RecordError(try_catch); +} + +bool IPCChannelFramer::ReadAdvanced(Local ab, + const uint8_t* data, + size_t length, + LocalVector* out) { + Isolate* isolate = env()->isolate(); + TryCatch try_catch(isolate); + size_t pos = 0; + + for (;;) { + if (in_payload_) { + // Continue a payload that started in an earlier read. + const size_t n = std::min(payload_size_ - payload_filled_, length - pos); + if (!discarding_ && n > 0) { + memcpy(payload_data_ + payload_filled_, data + pos, n); + } + payload_filled_ += n; + pos += n; + if (payload_filled_ < payload_size_) return true; + + in_payload_ = false; + payload_data_ = nullptr; + if (discarding_) continue; + Local frame; + const uint8_t* frame_data = small_payload_.data(); + if (payload_) { + frame = ArrayBuffer::New(isolate, std::move(payload_)); + frame_data = static_cast(frame->Data()); + } + if (!Deliver(frame, frame_data, payload_size_, out, &try_catch)) { + return false; + } + continue; + } + + const size_t available = length - pos; + if (available == 0) return true; + + if (header_size_ == 0 && available >= sizeof(header_)) { + const size_t size = ReadUInt32BE(data + pos); + if (available - sizeof(header_) >= size) { + // The whole frame is in this read: deserialize it in place. + pos += sizeof(header_); + if (!Deliver(ab, data + pos, size, out, &try_catch)) return false; + pos += size; + continue; + } + } + + // Accumulate the length prefix, which may itself span reads. + const size_t n = std::min(sizeof(header_) - header_size_, available); + memcpy(header_ + header_size_, data + pos, n); + header_size_ += n; + pos += n; + if (header_size_ < sizeof(header_)) return true; + header_size_ = 0; + if (!StartPayload(ReadUInt32BE(header_), &try_catch)) return false; + } +} + +void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + Isolate* isolate = framer->env()->isolate(); + + CHECK(args[0]->IsUint8Array()); + Local chunk = args[0].As(); + Local ab = chunk->Buffer(); + const size_t offset = chunk->ByteOffset(); + const size_t length = chunk->ByteLength(); + const uint8_t* data = static_cast(ab->Data()) + offset; + + LocalVector messages(isolate); + if (!framer->ReadAdvanced(ab, data, length, &messages)) { + return; // execution is terminating + } + + args.GetReturnValue().Set( + Array::New(isolate, messages.data(), messages.size())); +} + +void IPCChannelFramer::Status(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + int status = 0; + if (framer->in_payload_ || framer->header_size_ > 0) { + status |= kFramerBuffering; + } + if (!framer->pending_error_.IsEmpty()) status |= kFramerError; + args.GetReturnValue().Set(status); +} + +void IPCChannelFramer::TakeError(const FunctionCallbackInfo& args) { + IPCChannelFramer* framer; + ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); + if (framer->pending_error_.IsEmpty()) return; + args.GetReturnValue().Set( + framer->pending_error_.Get(framer->env()->isolate())); + framer->pending_error_.Reset(); +} + static void Initialize(Local target, Local unused, Local context, void* priv) { + Environment* env = Environment::GetCurrent(context); + Isolate* isolate = env->isolate(); + SetMethod(context, target, "serialize", Serialize); SetMethod(context, target, "deserialize", Deserialize); + + Local framer = + NewFunctionTemplate(isolate, IPCChannelFramer::New); + framer->InstanceTemplate()->SetInternalFieldCount( + IPCChannelFramer::kInternalFieldCount); + SetProtoMethod(isolate, framer, "read", IPCChannelFramer::Read); + SetProtoMethod(isolate, framer, "status", IPCChannelFramer::Status); + SetProtoMethod(isolate, framer, "takeError", IPCChannelFramer::TakeError); + framer->ReadOnlyPrototype(); + SetConstructorFunction(context, target, "IPCChannelFramer", framer); + NODE_DEFINE_CONSTANT(target, kFramerBuffering); + NODE_DEFINE_CONSTANT(target, kFramerError); } static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Serialize); registry->Register(Deserialize); + registry->Register(IPCChannelFramer::New); + registry->Register(IPCChannelFramer::Read); + registry->Register(IPCChannelFramer::Status); + registry->Register(IPCChannelFramer::TakeError); } } // namespace ipc_serdes diff --git a/test/cctest/test_node_ipc_serdes.cc b/test/cctest/test_node_ipc_serdes.cc index fa86e316e7a5..ebcc6923fdaa 100644 --- a/test/cctest/test_node_ipc_serdes.cc +++ b/test/cctest/test_node_ipc_serdes.cc @@ -3,7 +3,10 @@ #include "node_buffer.h" #include "node_test_fixture.h" +#include +#include #include +#include using node::Environment; @@ -207,4 +210,276 @@ TEST_F(IPCSerdesTest, BufferRoundTripsAsBuffer) { EXPECT_EQ(memcmp(node::Buffer::Data(out), "Hello!", 6), 0); } +// --- IPCChannelFramer (native read-path framing) --- + +// Serializes `input` into a complete `advanced` frame (4-byte length prefix +// included), as written on the wire by writeChannelMessage(). +v8::Local SerializeFrame(v8::Local context, + v8::Local serialize, + v8::Local input) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local buffer_ctor = + context->Global() + ->Get(context, v8::String::NewFromUtf8Literal(isolate, "Buffer")) + .ToLocalChecked(); + v8::Local args[] = {input, buffer_ctor}; + return serialize->Call(context, v8::Undefined(isolate), 2, args) + .ToLocalChecked(); +} + +v8::Local NewFramer(v8::Local context, + v8::Local binding) { + v8::Local ctor = + GetFunction(context, binding, "IPCChannelFramer"); + return ctor->NewInstance(context, 0, nullptr).ToLocalChecked(); +} + +v8::Local MakeChunk(v8::Local context, + const void* data, + size_t length) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::Local ab = v8::ArrayBuffer::New(isolate, length); + if (length > 0) memcpy(ab->Data(), data, length); + return v8::Uint8Array::New(ab, 0, length); +} + +v8::Local FramerRead(v8::Local context, + v8::Local framer, + v8::Local chunk) { + v8::Local read = GetFunction(context, framer, "read"); + return read->Call(context, framer, 1, &chunk) + .ToLocalChecked() + .As(); +} + +// Bits of IPCChannelFramer.prototype.status(), matching node_ipc_serdes.cc. +constexpr int kFramerBuffering = 1; +constexpr int kFramerError = 2; + +int FramerStatus(v8::Local context, v8::Local framer) { + v8::Local status = GetFunction(context, framer, "status"); + return status->Call(context, framer, 0, nullptr) + .ToLocalChecked() + ->Int32Value(context) + .FromJust(); +} + +bool FramerBuffering(v8::Local context, + v8::Local framer) { + return (FramerStatus(context, framer) & kFramerBuffering) != 0; +} + +std::vector FrameBytes(v8::Local frame) { + node::ArrayBufferViewContents bytes(frame); + return std::vector(bytes.data(), bytes.data() + bytes.length()); +} + +TEST_F(IPCSerdesTest, FramerRoundTripsSingleMessage) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local input = v8::Number::New(isolate_, 42); + v8::Local frame = SerializeFrame(context, serialize, input); + + v8::Local framer = NewFramer(context, binding); + v8::Local messages = FramerRead(context, framer, frame); + + ASSERT_EQ(messages->Length(), 1u); + EXPECT_TRUE(messages->Get(context, 0).ToLocalChecked()->StrictEquals(input)); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + +TEST_F(IPCSerdesTest, FramerSplitsMultipleMessagesInOneRead) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local frame1 = + SerializeFrame(context, serialize, v8::Number::New(isolate_, 1)); + v8::Local frame2 = SerializeFrame( + context, serialize, v8::String::NewFromUtf8Literal(isolate_, "two")); + + node::ArrayBufferViewContents b1(frame1); + node::ArrayBufferViewContents b2(frame2); + std::vector combined; + combined.insert(combined.end(), b1.data(), b1.data() + b1.length()); + combined.insert(combined.end(), b2.data(), b2.data() + b2.length()); + + v8::Local framer = NewFramer(context, binding); + v8::Local messages = FramerRead( + context, framer, MakeChunk(context, combined.data(), combined.size())); + + ASSERT_EQ(messages->Length(), 2u); + EXPECT_EQ(messages->Get(context, 0) + .ToLocalChecked() + ->Int32Value(context) + .FromJust(), + 1); + v8::String::Utf8Value second(isolate_, + messages->Get(context, 1).ToLocalChecked()); + EXPECT_STREQ(*second, "two"); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + +TEST_F(IPCSerdesTest, FramerBuffersPartialAdvancedFrame) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + v8::Local frame = SerializeFrame( + context, serialize, v8::String::NewFromUtf8Literal(isolate_, "spanning")); + node::ArrayBufferViewContents bytes(frame); + const size_t total = bytes.length(); + // Split inside the 4-byte length header to exercise that path too. + const size_t split = 2; + ASSERT_GT(total, split); + + v8::Local framer = NewFramer(context, binding); + + v8::Local first = + FramerRead(context, framer, MakeChunk(context, bytes.data(), split)); + EXPECT_EQ(first->Length(), 0u); + EXPECT_TRUE(FramerBuffering(context, framer)); + + v8::Local second = FramerRead( + context, framer, MakeChunk(context, bytes.data() + split, total - split)); + ASSERT_EQ(second->Length(), 1u); + v8::String::Utf8Value value(isolate_, + second->Get(context, 0).ToLocalChecked()); + EXPECT_STREQ(*value, "spanning"); + EXPECT_FALSE(FramerBuffering(context, framer)); +} + +TEST_F(IPCSerdesTest, FramerReassemblesBufferAcrossManyReads) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + // A Buffer host object in a frame that spans several reads, followed by a + // small message completed by the last read. The two sizes exercise the + // reused buffer for small payloads and the BackingStore for large ones. + for (const size_t payload_size : {1000u, 200000u}) { + std::string payload(payload_size, '\0'); + for (size_t i = 0; i < payload_size; i++) { + payload[i] = static_cast(i * 7); + } + v8::Local input = + node::Buffer::Copy(isolate_, payload.data(), payload.size()) + .ToLocalChecked(); + std::vector bytes = + FrameBytes(SerializeFrame(context, serialize, input)); + std::vector tail = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 7))); + bytes.insert(bytes.end(), tail.begin(), tail.end()); + + v8::Local framer = NewFramer(context, binding); + // Split inside the length prefix, then twice inside the payload. + const size_t splits[] = {0, 3, 200, payload_size / 2 + 100, bytes.size()}; + v8::LocalVector received(isolate_); + for (size_t i = 0; i + 1 < std::size(splits); i++) { + v8::Local messages = FramerRead( + context, + framer, + MakeChunk( + context, bytes.data() + splits[i], splits[i + 1] - splits[i])); + for (uint32_t j = 0; j < messages->Length(); j++) { + received.push_back(messages->Get(context, j).ToLocalChecked()); + } + if (i + 2 < std::size(splits)) { + EXPECT_TRUE(received.empty()); + EXPECT_TRUE(FramerBuffering(context, framer)); + } + } + + ASSERT_EQ(received.size(), 2u); + ASSERT_TRUE(node::Buffer::HasInstance(received[0])); + ASSERT_EQ(node::Buffer::Length(received[0]), payload_size); + EXPECT_EQ( + memcmp(node::Buffer::Data(received[0]), payload.data(), payload_size), + 0); + EXPECT_EQ(received[1]->Int32Value(context).FromJust(), 7); + EXPECT_EQ(FramerStatus(context, framer), 0); + } +} + +TEST_F(IPCSerdesTest, FramerRecoversFromInvalidFrame) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env test_env{handle_scope, argv}; + v8::Local context = isolate_->GetCurrentContext(); + + v8::Local binding = GetIpcSerdesBinding(*test_env, context); + v8::Local serialize = + GetFunction(context, binding, "serialize"); + + std::vector good1 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 1))); + std::vector good2 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 2))); + std::vector good3 = FrameBytes( + SerializeFrame(context, serialize, v8::Number::New(isolate_, 3))); + // A well-framed message whose payload is not a valid V8 serialization. + const std::vector bad = {0, 0, 0, 3, 0xde, 0xad, 0xbe}; + + // One read holding: good1, bad, good2, and the first half of good3. + std::vector bytes = good1; + bytes.insert(bytes.end(), bad.begin(), bad.end()); + bytes.insert(bytes.end(), good2.begin(), good2.end()); + const size_t half = good3.size() / 2; + bytes.insert(bytes.end(), good3.begin(), good3.begin() + half); + + v8::Local framer = NewFramer(context, binding); + v8::Local first = FramerRead( + context, framer, MakeChunk(context, bytes.data(), bytes.size())); + // Messages on both sides of the invalid frame are delivered, and its error + // is held for takeError(). + ASSERT_EQ(first->Length(), 2u); + EXPECT_EQ( + first->Get(context, 0).ToLocalChecked()->Int32Value(context).FromJust(), + 1); + EXPECT_EQ( + first->Get(context, 1).ToLocalChecked()->Int32Value(context).FromJust(), + 2); + EXPECT_EQ(FramerStatus(context, framer), kFramerBuffering | kFramerError); + + v8::Local take_error = + GetFunction(context, framer, "takeError"); + v8::Local error = + take_error->Call(context, framer, 0, nullptr).ToLocalChecked(); + EXPECT_TRUE(error->IsNativeError()); + EXPECT_EQ(FramerStatus(context, framer), kFramerBuffering); + + // The stream is still in sync: the rest of good3 completes it. + v8::Local second = + FramerRead(context, + framer, + MakeChunk(context, good3.data() + half, good3.size() - half)); + ASSERT_EQ(second->Length(), 1u); + EXPECT_EQ( + second->Get(context, 0).ToLocalChecked()->Int32Value(context).FromJust(), + 3); + EXPECT_EQ(FramerStatus(context, framer), 0); +} + } // namespace diff --git a/test/parallel/test-child-process-advanced-serialization-spanning-frames.js b/test/parallel/test-child-process-advanced-serialization-spanning-frames.js new file mode 100644 index 000000000000..b80c7e12f8b5 --- /dev/null +++ b/test/parallel/test-child-process-advanced-serialization-spanning-frames.js @@ -0,0 +1,49 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const child_process = require('child_process'); + +// Messages of varied sizes, many larger than one IPC read, carrying typed +// arrays of several element sizes. Frames that span reads are reassembled +// natively; check that their contents arrive intact and in order. +const sizes = [1, 7, 4096, 65535, 65536, 65537, 200003, 3, 1 << 20, 13]; + +function makeMessage(i, size) { + const bytes = Buffer.alloc(size); + for (let j = 0; j < size; j++) bytes[j] = (i * 31 + j) & 0xff; + const doubles = new Float64Array(size >>> 3 || 1); + for (let j = 0; j < doubles.length; j++) doubles[j] = i + j / 8; + const shorts = new Uint16Array(size >>> 1 || 1); + for (let j = 0; j < shorts.length; j++) shorts[j] = (i + j) & 0xffff; + return { i, bytes, doubles, shorts }; +} + +if (process.argv[2] === 'child') { + let i = 0; + (function send() { + if (i < sizes.length) { + process.send(makeMessage(i, sizes[i]), send); + i++; + } else { + process.disconnect(); + } + })(); +} else { + const child = child_process.spawn(process.execPath, [__filename, 'child'], { + stdio: ['inherit', 'inherit', 'inherit', 'ipc'], + serialization: 'advanced', + }); + let next = 0; + child.on('message', common.mustCall((message) => { + const expected = makeMessage(next, sizes[next]); + assert.strictEqual(message.i, next); + assert.ok(Buffer.isBuffer(message.bytes)); + assert.deepStrictEqual(message.bytes, expected.bytes); + assert.deepStrictEqual(message.doubles, expected.doubles); + assert.deepStrictEqual(message.shorts, expected.shorts); + next++; + }, sizes.length)); + child.on('exit', common.mustCall(() => { + assert.strictEqual(next, sizes.length); + })); +} diff --git a/typings/internalBinding/ipc_serdes.d.ts b/typings/internalBinding/ipc_serdes.d.ts index dc00f068e427..711c15ed4f53 100644 --- a/typings/internalBinding/ipc_serdes.d.ts +++ b/typings/internalBinding/ipc_serdes.d.ts @@ -1,8 +1,23 @@ declare namespace InternalIPCSerdesBinding { type Buffer = Uint8Array; + + // Per-channel native framer for the `advanced` codec. Accumulates partial + // reads in C++ and returns the complete, deserialized messages found in each + // chunk. See src/node_ipc_serdes.cc. + class IPCChannelFramer { + constructor(); + read(chunk: Uint8Array): unknown[]; + // Bitmask of kFramerBuffering and kFramerError. + status(): number; + // Returns and clears the error of a frame that failed to deserialize. + takeError(): unknown; + } } export interface IPCSerdesBinding { serialize(message: unknown): InternalIPCSerdesBinding.Buffer; deserialize(buffer: ArrayBufferView): unknown; + IPCChannelFramer: typeof InternalIPCSerdesBinding.IPCChannelFramer; + kFramerBuffering: number; + kFramerError: number; }