From 93f917dd130741570e7d314bd13894246a7b46ce Mon Sep 17 00:00:00 2001 From: Adrian Niculescu <15037449+adrian-niculescu@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:10:27 +0300 Subject: [PATCH 1/2] fix(runtime): keep clone-only message reads read-only, and see an uncloneable brand created mid-clone A clone-only message is read concurrently by every BroadcastChannel receiver and every getEnvironmentData call, and deserializing it cleared its transfer vectors, a write. Only the single receiver of a message with transferables writes them now. The serializer cached the uncloneable brand when it was created, so the first markAsUncloneable call in an isolate, made by a getter in the graph being written, went unseen and the marked object was cloned. It now picks the brand up once it exists. --- .../messaging/uncloneableInGetterWorker.js | 19 +++++++++++++++++++ .../main/assets/app/tests/testMessaging.js | 16 ++++++++++++++++ .../src/main/cpp/StructuredSerialization.cpp | 17 +++++++++++++---- 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/messaging/uncloneableInGetterWorker.js 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..32175bbac 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,22 @@ 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(); + }; + }); + }); + 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..96fe58058 100644 --- a/test-app/runtime/src/main/cpp/StructuredSerialization.cpp +++ b/test-app/runtime/src/main/cpp/StructuredSerialization.cpp @@ -172,6 +172,11 @@ 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. + uncloneableBrand_ = messaging::UncloneableBrandIfAny(isolate); + } if (!uncloneableBrand_.IsEmpty()) { bool uncloneable = false; if (!object->HasPrivate(isolate->GetCurrentContext(), uncloneableBrand_) @@ -635,7 +640,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 +734,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; { From 408846232c3c77bb463ec28a56b89d23bf6c1eee Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Thu, 5 Nov 2026 16:41:14 -0300 Subject: [PATCH 2/2] fix(runtime): don't cache an uncloneable brand handle made inside IsHostObject V8 calls the serializer delegate's IsHostObject inside a HandleScope that WriteJSReceiver opens per object. When a getter created the isolate's first uncloneable brand mid-clone, IsHostObject cached the brand's Local in the delegate, and every later object in the same clone checked HasPrivate against a handle whose scope had already closed: a marked object reached after that getter was cloned instead of rejected. A late lookup now stays local to the call; the brand cached at construction lives in Serialize's own scope and is still used when it exists. --- .../messaging/uncloneableAfterGetterWorker.js | 22 +++++++++++++++++++ .../main/assets/app/tests/testMessaging.js | 14 ++++++++++++ .../src/main/cpp/StructuredSerialization.cpp | 16 ++++++++------ 3 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 test-app/app/src/main/assets/app/tests/messaging/uncloneableAfterGetterWorker.js 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/testMessaging.js b/test-app/app/src/main/assets/app/tests/testMessaging.js index 32175bbac..e4196e5af 100644 --- a/test-app/app/src/main/assets/app/tests/testMessaging.js +++ b/test-app/app/src/main/assets/app/tests/testMessaging.js @@ -310,6 +310,20 @@ describe("Messaging runtime edges", function () { 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 () { diff --git a/test-app/runtime/src/main/cpp/StructuredSerialization.cpp b/test-app/runtime/src/main/cpp/StructuredSerialization.cpp index 96fe58058..4d2e8c30e 100644 --- a/test-app/runtime/src/main/cpp/StructuredSerialization.cpp +++ b/test-app/runtime/src/main/cpp/StructuredSerialization.cpp @@ -172,14 +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. - uncloneableBrand_ = messaging::UncloneableBrandIfAny(isolate); - } - 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(); }