Skip to content

Commit 20dc172

Browse files
committed
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: #26207 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Backport-PR-URL: #26302 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 437bb25 commit 20dc172

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/node_messaging.cc

+15-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ MaybeLocal<Value> Message::Deserialize(Environment* env,
132132

133133
// Attach all transferred ArrayBuffers to their new Isolate.
134134
for (uint32_t i = 0; i < array_buffer_contents_.size(); ++i) {
135+
if (!env->isolate_data()->uses_node_allocator()) {
136+
// We don't use Node's allocator on the receiving side, so we have
137+
// to create the ArrayBuffer from a copy of the memory.
138+
AllocatedBuffer buf =
139+
env->AllocateManaged(array_buffer_contents_[i].size);
140+
memcpy(buf.data(),
141+
array_buffer_contents_[i].data,
142+
array_buffer_contents_[i].size);
143+
deserializer.TransferArrayBuffer(i, buf.ToArrayBuffer());
144+
continue;
145+
}
146+
135147
Local<ArrayBuffer> ab =
136148
ArrayBuffer::New(env->isolate(),
137149
array_buffer_contents_[i].release(),
@@ -288,8 +300,10 @@ Maybe<bool> Message::Serialize(Environment* env,
288300
Local<ArrayBuffer> ab = entry.As<ArrayBuffer>();
289301
// If we cannot render the ArrayBuffer unusable in this Isolate and
290302
// take ownership of its memory, copying the buffer will have to do.
291-
if (!ab->IsNeuterable() || ab->IsExternal())
303+
if (!ab->IsNeuterable() || ab->IsExternal() ||
304+
!env->isolate_data()->uses_node_allocator()) {
292305
continue;
306+
}
293307
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
294308
array_buffers.end()) {
295309
ThrowDataCloneException(

0 commit comments

Comments
 (0)