From ec075ac431b9575c233a7de3adfb4d23a5f6ff15 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Sun, 15 Nov 2026 16:20:19 -0500 Subject: [PATCH] child_process: harden native IPC framer Consume each frame before deserializing it so that an invalid frame neither wedges nor desynchronizes the channel, and deliver the messages decoded from the same read before throwing its error. Accumulate large frames that span reads into a BackingStore that host objects borrow, and small ones into a bounded reused buffer. Copy in bulk and avoid 32-bit overflow in the length check. Signed-off-by: Daniel Lemire --- lib/internal/child_process/serialization.js | 24 +- src/node_ipc_serdes.cc | 264 +++++++++++++----- test/cctest/test_node_ipc_serdes.cc | 139 ++++++++- ...-advanced-serialization-spanning-frames.js | 49 ++++ typings/internalBinding/ipc_serdes.d.ts | 7 +- 5 files changed, 403 insertions(+), 80 deletions(-) create mode 100644 test/parallel/test-child-process-advanced-serialization-spanning-frames.js diff --git a/lib/internal/child_process/serialization.js b/lib/internal/child_process/serialization.js index a33ac900d819..9aeb994b954e 100644 --- a/lib/internal/child_process/serialization.js +++ b/lib/internal/child_process/serialization.js @@ -8,7 +8,12 @@ const { } = primordials; const { Buffer } = require('buffer'); const { StringDecoder } = require('string_decoder'); -const { serialize, IPCChannelFramer } = internalBinding('ipc_serdes'); +const { + serialize, + IPCChannelFramer, + kFramerBuffering, + kFramerError, +} = internalBinding('ipc_serdes'); const { streamBaseState, kLastWriteWasAsync } = internalBinding('stream_wrap'); const kFramer = Symbol('kFramer'); @@ -26,6 +31,15 @@ const kStringDecoder = Symbol('kStringDecoder'); // 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[kFramer] = new IPCChannelFramer(); @@ -34,8 +48,12 @@ const advanced = { parseChannelMessages(channel, readData) { if (readData.length === 0) return []; - const messages = channel[kFramer].read(readData); - channel.buffering = channel[kFramer].buffering(); + 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; }, diff --git a/src/node_ipc_serdes.cc b/src/node_ipc_serdes.cc index 8963a7b19a4f..8556c0c7772f 100644 --- a/src/node_ipc_serdes.cc +++ b/src/node_ipc_serdes.cc @@ -7,7 +7,9 @@ #include "node_internals.h" #include "util-inl.h" +#include #include +#include #include #include @@ -49,6 +51,7 @@ using v8::Name; using v8::Nothing; using v8::Object; using v8::String; +using v8::TryCatch; using v8::Uint16Array; using v8::Uint32Array; using v8::Uint8Array; @@ -289,8 +292,9 @@ class IPCDeserializerDelegate : public ValueDeserializer::Delegate { // Reuse the backing ArrayBuffer when the data is suitably aligned, // otherwise copy into a fresh aligned buffer. Mirrors _readHostObject() - // in lib/v8.js. A frame reassembled across reads has no standalone - // ArrayBuffer to borrow from (`ab_` is empty), so always copy. + // 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()); @@ -351,8 +355,8 @@ static void Serialize(const FunctionCallbackInfo& 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 (a frame reassembled -// across reads) host objects are copied into fresh buffers. +// 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, @@ -369,8 +373,7 @@ static MaybeLocal DeserializeAdvancedPayload(Environment* env, return deserializer.ReadValue(context); } -static inline uint32_t ReadUInt32BE(const char* p) { - const uint8_t* b = reinterpret_cast(p); +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]); @@ -392,16 +395,30 @@ static void Deserialize(const FunctionCallbackInfo& args) { } } +// 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, owning the cross-read -// accumulation buffer that used to live in serialization.js (kMessageBuffer), -// so JavaScript receives whole messages and never reframes partial reads or -// concatenates partial frames itself. +// 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. // -// The `json` codec is intentionally not framed here: its StringDecoder + -// split('\n') pipeline in serialization.js already avoids copies (V8 rope -// concatenation and O(1) substrings) and is faster than reassembling the bytes -// in C++. +// 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) @@ -411,24 +428,54 @@ class IPCChannelFramer : public BaseObject { static void New(const FunctionCallbackInfo& args); static void Read(const FunctionCallbackInfo& args); - static void Buffering(const FunctionCallbackInfo& args); + static void Status(const FunctionCallbackInfo& args); + static void TakeError(const FunctionCallbackInfo& args); void MemoryInfo(MemoryTracker* tracker) const override { - tracker->TrackField("buffered", buffered_); + 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 complete deserialized messages found in `data` to `out`, buffering - // any trailing partial frame for the next read. Return false (with a pending - // exception) if deserialization fails. + // 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); - - std::vector buffered_; + // 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) { @@ -441,63 +488,118 @@ void IPCChannelFramer::New(const FunctionCallbackInfo& args) { 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) { - Environment* env = this->env(); + Isolate* isolate = env()->isolate(); + TryCatch try_catch(isolate); size_t pos = 0; - // Phase 1: finish a frame whose bytes started in a previous read. It is - // reassembled in `buffered_`, so it has no borrowable ArrayBuffer and its - // host objects are copied. - if (!buffered_.empty()) { - while (buffered_.size() < 4 && pos < length) { - buffered_.push_back(static_cast(data[pos++])); - } - if (buffered_.size() < 4) return true; // still buffering the length header - - const size_t frame_total = - static_cast(ReadUInt32BE(buffered_.data())) + 4; - while (buffered_.size() < frame_total && pos < length) { - buffered_.push_back(static_cast(data[pos++])); - } - if (buffered_.size() < frame_total) return true; // still buffering payload - - const uint8_t* payload = - reinterpret_cast(buffered_.data()) + 4; - Local message; - if (!DeserializeAdvancedPayload( - env, Local(), payload, frame_total - 4) - .ToLocal(&message)) { - return false; + 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; } - out->push_back(message); - buffered_.clear(); - } - // Phase 2: messages contained entirely within this chunk are deserialized in - // place, letting host objects borrow the chunk's ArrayBuffer (zero copy). - while (length - pos >= 4) { - const size_t frame_total = static_cast(ReadUInt32BE( - reinterpret_cast(data + pos))) + - 4; - if (length - pos < frame_total) break; // incomplete trailing frame - - Local message; - if (!DeserializeAdvancedPayload(env, ab, data + pos + 4, frame_total - 4) - .ToLocal(&message)) { - return false; + 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; + } } - out->push_back(message); - pos += frame_total; - } - if (pos < length) { - buffered_.assign(reinterpret_cast(data + pos), - reinterpret_cast(data + length)); + // 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; } - return true; } void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { @@ -514,17 +616,31 @@ void IPCChannelFramer::Read(const FunctionCallbackInfo& args) { LocalVector messages(isolate); if (!framer->ReadAdvanced(ab, data, length, &messages)) { - return; // a pending exception propagates to JavaScript + return; // execution is terminating } args.GetReturnValue().Set( Array::New(isolate, messages.data(), messages.size())); } -void IPCChannelFramer::Buffering(const FunctionCallbackInfo& args) { +void IPCChannelFramer::Status(const FunctionCallbackInfo& args) { IPCChannelFramer* framer; ASSIGN_OR_RETURN_UNWRAP(&framer, args.This()); - args.GetReturnValue().Set(!framer->buffered_.empty()); + 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, @@ -542,9 +658,12 @@ static void Initialize(Local target, framer->InstanceTemplate()->SetInternalFieldCount( IPCChannelFramer::kInternalFieldCount); SetProtoMethod(isolate, framer, "read", IPCChannelFramer::Read); - SetProtoMethod(isolate, framer, "buffering", IPCChannelFramer::Buffering); + 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) { @@ -552,7 +671,8 @@ static void RegisterExternalReferences(ExternalReferenceRegistry* registry) { registry->Register(Deserialize); registry->Register(IPCChannelFramer::New); registry->Register(IPCChannelFramer::Read); - registry->Register(IPCChannelFramer::Buffering); + 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 3899e42241c4..ebcc6923fdaa 100644 --- a/test/cctest/test_node_ipc_serdes.cc +++ b/test/cctest/test_node_ipc_serdes.cc @@ -4,6 +4,7 @@ #include "node_test_fixture.h" #include +#include #include #include @@ -251,12 +252,26 @@ v8::Local FramerRead(v8::Local context, .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) { - v8::Local buffering = GetFunction(context, framer, "buffering"); - return buffering->Call(context, framer, 0, nullptr) - .ToLocalChecked() - ->BooleanValue(v8::Isolate::GetCurrent()); + 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) { @@ -351,4 +366,120 @@ TEST_F(IPCSerdesTest, FramerBuffersPartialAdvancedFrame) { 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 99ef866ce16f..711c15ed4f53 100644 --- a/typings/internalBinding/ipc_serdes.d.ts +++ b/typings/internalBinding/ipc_serdes.d.ts @@ -7,7 +7,10 @@ declare namespace InternalIPCSerdesBinding { class IPCChannelFramer { constructor(); read(chunk: Uint8Array): unknown[]; - buffering(): boolean; + // Bitmask of kFramerBuffering and kFramerError. + status(): number; + // Returns and clears the error of a frame that failed to deserialize. + takeError(): unknown; } } @@ -15,4 +18,6 @@ export interface IPCSerdesBinding { serialize(message: unknown): InternalIPCSerdesBinding.Buffer; deserialize(buffer: ArrayBufferView): unknown; IPCChannelFramer: typeof InternalIPCSerdesBinding.IPCChannelFramer; + kFramerBuffering: number; + kFramerError: number; }