From a8ba838e1a94c17ecf7a290c1951040d2957cb62 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 19 Feb 2019 14:56:52 +0100 Subject: [PATCH] worker: copy transferList ArrayBuffers on unknown allocator If the `ArrayBuffer::Allocator` used to create `ArrayBuffer`s in the current `Isolate` is not a Node.js `ArrayBufferAllocator`, we cannot know that it is `malloc()`-based, an in particular it might not be compatible with the `ArrayBuffer::Allocator` on the receiving end of the connection. PR-URL: https://github.com/nodejs/node/pull/26207 Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- src/node_messaging.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/node_messaging.cc b/src/node_messaging.cc index 16e46f9dd02409..c659ac06f1d41d 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -132,6 +132,18 @@ MaybeLocal Message::Deserialize(Environment* env, // Attach all transferred ArrayBuffers to their new Isolate. for (uint32_t i = 0; i < array_buffer_contents_.size(); ++i) { + if (!env->isolate_data()->uses_node_allocator()) { + // We don't use Node's allocator on the receiving side, so we have + // to create the ArrayBuffer from a copy of the memory. + AllocatedBuffer buf = + env->AllocateManaged(array_buffer_contents_[i].size); + memcpy(buf.data(), + array_buffer_contents_[i].data, + array_buffer_contents_[i].size); + deserializer.TransferArrayBuffer(i, buf.ToArrayBuffer()); + continue; + } + Local ab = ArrayBuffer::New(env->isolate(), array_buffer_contents_[i].release(), @@ -288,8 +300,10 @@ Maybe Message::Serialize(Environment* env, Local ab = entry.As(); // If we cannot render the ArrayBuffer unusable in this Isolate and // take ownership of its memory, copying the buffer will have to do. - if (!ab->IsNeuterable() || ab->IsExternal()) + if (!ab->IsNeuterable() || ab->IsExternal() || + !env->isolate_data()->uses_node_allocator()) { continue; + } if (std::find(array_buffers.begin(), array_buffers.end(), ab) != array_buffers.end()) { ThrowDataCloneException(