Skip to content

Commit 4088462

Browse files
committed
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.
1 parent 93f917d commit 4088462

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// A fresh isolate: the getter makes this isolate's first markAsUncloneable
2+
// call mid-clone, on an object it then keeps to itself, so the marked object
3+
// the clone must reject is one it only reaches afterwards.
4+
var markAsUncloneable = require("node:worker_threads").markAsUncloneable;
5+
var marked = { a: 1 };
6+
var graph = {
7+
get first() {
8+
markAsUncloneable(marked);
9+
return { plain: true };
10+
},
11+
second: { plain: true },
12+
third: [{ plain: true }, { plain: true }],
13+
last: marked,
14+
};
15+
var result;
16+
try {
17+
structuredClone(graph);
18+
result = { threw: false };
19+
} catch (e) {
20+
result = { threw: true, name: e && e.name };
21+
}
22+
postMessage(result);

‎test-app/app/src/main/assets/app/tests/testMessaging.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ describe("Messaging runtime edges", function () {
310310
done();
311311
};
312312
});
313+
314+
it("rejects a marked object reached after the getter that created the brand", function (done) {
315+
var worker = new Worker("./messaging/uncloneableAfterGetterWorker.js");
316+
worker.onmessage = function (event) {
317+
expect(event.data).toEqual({ threw: true, name: "DataCloneError" });
318+
worker.terminate();
319+
done();
320+
};
321+
worker.onerror = function (error) {
322+
fail("worker error: " + error.message);
323+
worker.terminate();
324+
done();
325+
};
326+
});
313327
});
314328

315329
describe("worker error reporting", function () {

‎test-app/runtime/src/main/cpp/StructuredSerialization.cpp‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,16 @@ class SerializerDelegate : public ValueSerializer::Delegate {
172172
if (object->InternalFieldCount() > 0) {
173173
return Just(true);
174174
}
175-
if (uncloneableBrand_.IsEmpty()) {
176-
// markAsUncloneable creates the brand on its first call in an
177-
// isolate, which a getter in the graph being written can make.
178-
uncloneableBrand_ = messaging::UncloneableBrandIfAny(isolate);
179-
}
180-
if (!uncloneableBrand_.IsEmpty()) {
175+
// markAsUncloneable creates the brand on its first call in an isolate,
176+
// which a getter in the graph being written can make. A brand looked up
177+
// here stays local: V8 calls IsHostObject inside a HandleScope of its
178+
// own, so a handle made now is gone once this returns.
179+
Local<Private> uncloneableBrand = uncloneableBrand_.IsEmpty()
180+
? messaging::UncloneableBrandIfAny(isolate)
181+
: uncloneableBrand_;
182+
if (!uncloneableBrand.IsEmpty()) {
181183
bool uncloneable = false;
182-
if (!object->HasPrivate(isolate->GetCurrentContext(), uncloneableBrand_)
184+
if (!object->HasPrivate(isolate->GetCurrentContext(), uncloneableBrand)
183185
.To(&uncloneable)) {
184186
return Nothing<bool>();
185187
}

0 commit comments

Comments
 (0)