镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit c566825

Browse files
committed
fix(clone): release consumed wire and root traversal state
Keep structuredClone inputs, transfers, serialized object indexes and deserialized values in authoritative roots. Give consumed wire, traversal indexes and field-name scratch an invocation lifetime while preserving caller-owned serialization and worker transfer manifests. Retain a bounded-heap public callback regression and moving/error cases. Focused Debug qualification passed 17/17; all-module ThreadSanitizer checks passed 18/18 without suppressions. The combined candidate also passed 2872 units and 6031 affected conformance cases. Refs #1120
1 parent 2b7cabe commit c566825

3 files changed

Lines changed: 271 additions & 190 deletions

File tree

‎src/context.zig‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23919,6 +23919,37 @@ test "Atomics.waitAsync reserves async waiter capacity chunks" {
2391923919
try std.testing.expect(ctx.async_waiters.capacity > first_capacity);
2392023920
}
2392123921

23922+
test "structuredClone scratch: retained callbacks reclaim operation buffers" {
23923+
const ctx = try Context.createWith(std.testing.allocator, .{
23924+
.enable_gc = true,
23925+
.heap_limit_bytes = 8 * 1024 * 1024,
23926+
});
23927+
defer ctx.destroy();
23928+
const callback = try ctx.evaluateProtected(
23929+
\\(function () {
23930+
\\ const input = {payload: []};
23931+
\\ for (let index = 0; index < 256; ++index) input.payload.push(index);
23932+
\\ return function () {
23933+
\\ const copy = structuredClone(input);
23934+
\\ if (copy === input || copy.payload === input.payload || copy.payload.length !== 256)
23935+
\\ throw new Error("clone identity or length differs");
23936+
\\ for (let index = 0; index < 256; ++index)
23937+
\\ if (copy.payload[index] !== index || input.payload[index] !== index)
23938+
\\ throw new Error("clone changed retained values");
23939+
\\ return 4;
23940+
\\ };
23941+
\\})()
23942+
);
23943+
defer std.debug.assert(ctx.unprotectValue(callback));
23944+
// The same retained input and callback isolate clone storage ownership.
23945+
// Automatic collection alone must keep invocation-local buffers bounded.
23946+
for (0..1000) |_| {
23947+
const result = try ctx.callProtected(callback, null, &.{});
23948+
defer std.debug.assert(ctx.unprotectValue(result));
23949+
try std.testing.expectEqual(@as(f64, 4), result.get().asNum());
23950+
}
23951+
}
23952+
2392223953
test "structuredClone: identity, cycles, types, SAB sharing, transfer" {
2392323954
const ctx = try Context.create(std.testing.allocator);
2392423955
defer ctx.destroy();

‎src/interpreter.zig‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39006,38 +39006,49 @@ fn queueMicrotaskFn(ctx: *anyopaque, this: Value, args: []const Value) value.Hos
3900639006
fn structuredCloneFn(ctx: *anyopaque, this: Value, args: []const Value) value.HostError!Value {
3900739007
_ = this;
3900839008
const self: *Interpreter = @ptrCast(@alignCast(ctx));
39009-
const v = arg0(args);
39009+
const argument_roots = try self.pushTempRootSlice(args);
39010+
defer self.restoreTempRoots(argument_roots);
39011+
// This consumer finishes with its wire bytes before returning. Worker and
39012+
// embedding callers keep their explicit, independently owned wire buffers.
39013+
var operation_arena = std.heap.ArenaAllocator.init(self.scratch_allocator orelse gc_mod.temporaryAllocator(self.arena));
39014+
defer operation_arena.deinit();
39015+
const operation_allocator = operation_arena.allocator();
3901039016
// Collect + validate the transfer list before serializing.
39011-
var transfer: std.ArrayListUnmanaged(*value.Object) = .empty;
39012-
if (args.len > 1 and !args[1].isUndefined() and !args[1].isNull()) {
39013-
if (!args[1].isObject()) return self.throwError("TypeError", "structuredClone options must be an object");
39014-
const tv = try self.getProperty(args[1], "transfer");
39017+
var transfer: std.ArrayListUnmanaged(struct { slot: usize, fallback: Value }) = .empty;
39018+
const options = if (args.len > 1) self.tempRoot(argument_roots + 1, args[1]) else Value.undef();
39019+
if (!options.isUndefined() and !options.isNull()) {
39020+
if (!options.isObject()) return self.throwError("TypeError", "structuredClone options must be an object");
39021+
const tv = try self.getProperty(options, "transfer");
3901539022
if (!tv.isUndefined() and !tv.isNull()) {
3901639023
if (!tv.isObject() or !tv.asObj().is_array)
3901739024
return self.throwError("TypeError", "structuredClone transfer must be an array");
3901839025
// Snapshot the transfer list before validating entries. The list is
3901939026
// ordinary JS input, so a shared-realm sibling thread may mutate its
3902039027
// element storage while this builtin runs; validation itself must
3902139028
// not borrow `elements.items` directly.
39022-
for (try tv.asObj().internalElementsSnapshot(self.arena)) |entry| {
39029+
const snapshot = try tv.asObj().internalElementsSnapshot(operation_allocator);
39030+
const transfer_roots = try self.pushTempRootSlice(snapshot);
39031+
for (snapshot, 0..) |fallback, index| {
39032+
const entry = self.tempRoot(transfer_roots + index, fallback);
3902339033
if (!entry.isObject() or entry.asObj().arrayBuffer() == null)
3902439034
return self.throwDOMException("DataCloneError", "only ArrayBuffers are transferable");
3902539035
const ab = entry.asObj().arrayBuffer().?;
3902639036
if (ab.is_shared) return self.throwDOMException("DataCloneError", "a SharedArrayBuffer is not transferable");
3902739037
if (ab.isDetached()) return self.throwDOMException("DataCloneError", "cannot transfer a detached ArrayBuffer");
3902839038
if (ab.immutable) return self.throwDOMException("DataCloneError", "cannot transfer an immutable ArrayBuffer");
3902939039
if (ab.is_wasm_memory) return self.throwDOMException("DataCloneError", "cannot transfer a WebAssembly.Memory buffer");
39030-
for (transfer.items) |seen| if (seen == entry.asObj())
39040+
for (transfer.items) |seen| if (self.tempRoot(seen.slot, seen.fallback).asObj() == entry.asObj())
3903139041
return self.throwDOMException("DataCloneError", "duplicate transferable");
39032-
try transfer.append(self.arena, entry.asObj());
39042+
try transfer.append(operation_allocator, .{ .slot = transfer_roots + index, .fallback = fallback });
3903339043
}
3903439044
}
3903539045
}
39036-
const bytes = try structured_clone.serialize(self, self.arena, v);
39046+
const bytes = try structured_clone.serialize(self, operation_allocator, if (args.len > 0) self.tempRoot(argument_roots, args[0]) else Value.undef());
39047+
defer structured_clone.releaseSerialized(bytes);
3903739048
// The move: the clone owns a copy of the bytes; the sources detach now
3903839049
// (after a fully successful serialize, so a DataCloneError mid-graph
3903939050
// leaves the inputs untouched).
39040-
for (transfer.items) |o| o.arrayBuffer().?.setDetached(true);
39051+
for (transfer.items) |entry| self.tempRoot(entry.slot, entry.fallback).asObj().arrayBuffer().?.setDetached(true);
3904139052
return structured_clone.deserialize(self, bytes);
3904239053
}
3904339054

0 commit comments

Comments
 (0)