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

worker: throw for duplicates in transfer list #25815

Closed
wants to merge 1 commit 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
19 changes: 19 additions & 0 deletions src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ Maybe<bool> Message::Serialize(Environment* env,
// take ownership of its memory, copying the buffer will have to do.
if (!ab->IsNeuterable() || ab->IsExternal())
continue;
if (std::find(array_buffers.begin(), array_buffers.end(), ab) !=
array_buffers.end()) {
ThrowDataCloneException(
env,
FIXED_ONE_BYTE_STRING(
env->isolate(),
"Transfer list contains duplicate ArrayBuffer"));
return Nothing<bool>();
}
// We simply use the array index in the `array_buffers` list as the
// ID that we write into the serialized buffer.
uint32_t id = array_buffers.size();
Expand All @@ -314,6 +323,15 @@ Maybe<bool> Message::Serialize(Environment* env,
"MessagePort in transfer list is already detached"));
return Nothing<bool>();
}
if (std::find(delegate.ports_.begin(), delegate.ports_.end(), port) !=
delegate.ports_.end()) {
ThrowDataCloneException(
env,
FIXED_ONE_BYTE_STRING(
env->isolate(),
"Transfer list contains duplicate MessagePort"));
return Nothing<bool>();
}
delegate.ports_.push_back(port);
continue;
}
Expand Down Expand Up @@ -601,6 +619,7 @@ void MessagePort::OnClose() {
}

std::unique_ptr<MessagePortData> MessagePort::Detach() {
CHECK(data_);
Mutex::ScopedLock lock(data_->mutex_);
data_->owner_ = nullptr;
return std::move(data_);
Expand Down
29 changes: 29 additions & 0 deletions test/parallel/test-worker-message-port-transfer-duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel } = require('worker_threads');

// Test that passing duplicate transferrables in the transfer list throws
// DataCloneError exceptions.

{
const { port1, port2 } = new MessageChannel();
port2.once('message', common.mustNotCall());

const port3 = new MessageChannel().port1;
assert.throws(() => {
port1.postMessage(port3, [port3, port3]);
}, /^DataCloneError: Transfer list contains duplicate MessagePort$/);
port1.close();
}

{
const { port1, port2 } = new MessageChannel();
port2.once('message', common.mustNotCall());

const buf = new Uint8Array(10);
assert.throws(() => {
port1.postMessage(buf, [buf.buffer, buf.buffer]);
}, /^DataCloneError: Transfer list contains duplicate ArrayBuffer$/);
port1.close();
}