diff --git a/test-app/app/src/main/assets/app/tests/messaging/uncloneableAfterGetterWorker.js b/test-app/app/src/main/assets/app/tests/messaging/uncloneableAfterGetterWorker.js new file mode 100644 index 000000000..13c536dea --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/messaging/uncloneableAfterGetterWorker.js @@ -0,0 +1,22 @@ +// A fresh isolate: the getter makes this isolate's first markAsUncloneable +// call mid-clone, on an object it then keeps to itself, so the marked object +// the clone must reject is one it only reaches afterwards. +var markAsUncloneable = require("node:worker_threads").markAsUncloneable; +var marked = { a: 1 }; +var graph = { + get first() { + markAsUncloneable(marked); + return { plain: true }; + }, + second: { plain: true }, + third: [{ plain: true }, { plain: true }], + last: marked, +}; +var result; +try { + structuredClone(graph); + result = { threw: false }; +} catch (e) { + result = { threw: true, name: e && e.name }; +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/messaging/uncloneableInGetterWorker.js b/test-app/app/src/main/assets/app/tests/messaging/uncloneableInGetterWorker.js new file mode 100644 index 000000000..f7101ab35 --- /dev/null +++ b/test-app/app/src/main/assets/app/tests/messaging/uncloneableInGetterWorker.js @@ -0,0 +1,19 @@ +// A fresh isolate: the markAsUncloneable call in the getter below is the first +// one this isolate has seen, and it happens while the clone that reaches the +// marked object is already being written. +var markAsUncloneable = require("node:worker_threads").markAsUncloneable; +var graph = { + get inner() { + var marked = { a: 1 }; + markAsUncloneable(marked); + return marked; + }, +}; +var result; +try { + structuredClone(graph); + result = { threw: false }; +} catch (e) { + result = { threw: true, name: e && e.name }; +} +postMessage(result); diff --git a/test-app/app/src/main/assets/app/tests/testMessaging.js b/test-app/app/src/main/assets/app/tests/testMessaging.js index 77a45ffc6..e4196e5af 100644 --- a/test-app/app/src/main/assets/app/tests/testMessaging.js +++ b/test-app/app/src/main/assets/app/tests/testMessaging.js @@ -296,6 +296,36 @@ describe("Messaging runtime edges", function () { }); }); + describe("markAsUncloneable", function () { + it("rejects an object marked while the clone that reaches it is written", function (done) { + var worker = new Worker("./messaging/uncloneableInGetterWorker.js"); + worker.onmessage = function (event) { + expect(event.data).toEqual({ threw: true, name: "DataCloneError" }); + worker.terminate(); + done(); + }; + worker.onerror = function (error) { + fail("worker error: " + error.message); + worker.terminate(); + done(); + }; + }); + + it("rejects a marked object reached after the getter that created the brand", function (done) { + var worker = new Worker("./messaging/uncloneableAfterGetterWorker.js"); + worker.onmessage = function (event) { + expect(event.data).toEqual({ threw: true, name: "DataCloneError" }); + worker.terminate(); + done(); + }; + worker.onerror = function (error) { + fail("worker error: " + error.message); + worker.terminate(); + done(); + }; + }); + }); + describe("worker error reporting", function () { // A worker boots on its own thread, so the first error arrives whenever // the runner gets to it; specs wait for it and only then settle for diff --git a/test-app/runtime/src/main/cpp/StructuredSerialization.cpp b/test-app/runtime/src/main/cpp/StructuredSerialization.cpp index 11461c77c..4d2e8c30e 100644 --- a/test-app/runtime/src/main/cpp/StructuredSerialization.cpp +++ b/test-app/runtime/src/main/cpp/StructuredSerialization.cpp @@ -172,9 +172,16 @@ class SerializerDelegate : public ValueSerializer::Delegate { if (object->InternalFieldCount() > 0) { return Just(true); } - if (!uncloneableBrand_.IsEmpty()) { + // markAsUncloneable creates the brand on its first call in an isolate, + // which a getter in the graph being written can make. A brand looked up + // here stays local: V8 calls IsHostObject inside a HandleScope of its + // own, so a handle made now is gone once this returns. + Local uncloneableBrand = uncloneableBrand_.IsEmpty() + ? messaging::UncloneableBrandIfAny(isolate) + : uncloneableBrand_; + if (!uncloneableBrand.IsEmpty()) { bool uncloneable = false; - if (!object->HasPrivate(isolate->GetCurrentContext(), uncloneableBrand_) + if (!object->HasPrivate(isolate->GetCurrentContext(), uncloneableBrand) .To(&uncloneable)) { return Nothing(); } @@ -635,7 +642,8 @@ MaybeLocal SerializedValue::Deserialize(Isolate* isolate, Local "A message carrying transferred objects can only be read once."); return MaybeLocal(); } - if (HasTransferables()) { + const bool singleReceiver = HasTransferables(); + if (singleReceiver) { consumed_ = true; } @@ -728,9 +736,12 @@ MaybeLocal SerializedValue::Deserialize(Isolate* isolate, Local ArrayBuffer::New(isolate, std::move(transferredBuffers_[i]))); } // Handed over above; the vectors would otherwise keep reporting - // transferables that are no longer here. - transferredBuffers_.clear(); - transferredPorts_.clear(); + // transferables that are no longer here. Only the single receiver writes + // them: the other readers share this value with no lock. + if (singleReceiver) { + transferredBuffers_.clear(); + transferredPorts_.clear(); + } Local result; {