From ae01e1e4cb52147430a42479aa6a55beec8d4d8d Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:49:48 -0600 Subject: [PATCH 01/11] fix: preserve Error cause graphs in structured clone --- .../webcore/SerializedScriptValue.cpp | 299 +++++++++++------- ...ructured-clone-shared-array-buffer.test.ts | 52 +++ test/js/web/workers/structured-clone.test.ts | 186 +++++++++++ 3 files changed, 426 insertions(+), 111 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index e117932d0741..61fd7e2224a3 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -141,7 +141,8 @@ enum WalkerState { StateUnknown, MapDataEndVisitKey, MapDataEndVisitValue, SetDataStartVisitEntry, - SetDataEndVisitKey }; + SetDataEndVisitKey, + ErrorEndVisitCause }; // These can't be reordered, and any new types must be added to the end of the list // When making changes to these lists please cover your new type(s) in the API test "IndexedDB.StructuredCloneBackwardCompatibility" @@ -416,8 +417,10 @@ const uint8_t cryptoKeyOKPOpNameTagMaximumValue = 1; * Version 13. added support for ErrorInstance objects. * Version 14. Date, RegExp, Error, DOMException, CryptoKey, KeyObject, X509Certificate, * and Bun cloneable types are recorded in the object reference pool on both sides. + * Version 15. ErrorInstance records include an own data cause, traversed as part of the graph. */ -[[maybe_unused]] static constexpr unsigned CurrentVersion = 14; +[[maybe_unused]] static constexpr unsigned CurrentVersion = 15; +static constexpr unsigned FirstVersionWithErrorCause = 15; // Deserializers must not pool the version 14 terminal types for older payloads, // whose writers never counted them, or the pool indices stop matching the writer's. [[maybe_unused]] static constexpr unsigned FirstVersionWithPooledTerminals = 14; @@ -437,7 +440,7 @@ static constexpr unsigned StringDataIs8BitFlag = 0x80000000; * in the constant pool. * * SerializedValue :- Value - * Value :- Array | Object | Map | Set | Terminal + * Value :- Array | Object | Map | Set | Error | Terminal * * Array :- * ArrayTag ()* TerminatorTag @@ -452,6 +455,9 @@ static constexpr unsigned StringDataIs8BitFlag = 0x80000000; * MapData :- ()* NonMapPropertiesTag ()* TerminatorTag * SetData :- ()* NonSetPropertiesTag ()* TerminatorTag * + * Error :- ErrorInstanceTag + * Value? + * * Terminal :- * UndefinedTag * | NullTag @@ -1095,6 +1101,86 @@ class CloneSerializer : public CloneBase { code = SerializationReturnCode::DataCloneError; } + JSValue dumpError(ErrorInstance* errorInstance) + { + auto& vm = m_lexicalGlobalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + auto errorTypeValue = errorInstance->get(m_lexicalGlobalObject, vm.propertyNames->name); + RETURN_IF_EXCEPTION(scope, JSValue()); + auto errorTypeString = errorTypeValue.toWTFString(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + + // .message/.line/.column/.sourceURL: HTML spec + Node/WebKit read + // OWN data descriptors only (an inherited or accessor .message is + // NOT serialized). .stack: Node reads via [[Get]] to materialize + // V8's lazy accessor. Any getter/coercion/prepareStackTrace throw + // propagates out of postMessage/structuredClone (Node parity). + String message, sourceURL, stack; + unsigned line = 0, column = 0; + { + // .message is ToString'd rather than gated on isString (node clones + // `e.message = 42` as "42"). Reading it before .line also keeps a + // Symbol message from reaching ErrorInstance's lazy materialization. + JSC::PropertyDescriptor d; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->message, d); + RETURN_IF_EXCEPTION(scope, JSValue()); + if (found && d.isDataDescriptor() && d.value()) { + message = d.value().toWTFString(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + } + } + // Trigger ErrorInstance's lazy materialization up front so a throwing + // prepareStackTrace propagates here instead of tripping the exception + // assertion inside JSObject::getOwnPropertyDescriptor. + errorInstance->materializeErrorInfoIfNeeded(vm); + RETURN_IF_EXCEPTION(scope, JSValue()); + { + JSC::PropertyDescriptor d; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->line, d); + RETURN_IF_EXCEPTION(scope, JSValue()); + if (found && d.isDataDescriptor() && d.value().isNumber()) + line = d.value().toNumber(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + } + { + JSC::PropertyDescriptor d; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->column, d); + RETURN_IF_EXCEPTION(scope, JSValue()); + if (found && d.isDataDescriptor() && d.value().isNumber()) + column = d.value().toNumber(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + } + { + JSC::PropertyDescriptor d; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->sourceURL, d); + RETURN_IF_EXCEPTION(scope, JSValue()); + if (found && d.isDataDescriptor() && d.value().isString()) + sourceURL = d.value().toWTFString(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + } + { + JSValue v = errorInstance->get(m_lexicalGlobalObject, vm.propertyNames->stack); + RETURN_IF_EXCEPTION(scope, JSValue()); + if (v.isString()) + stack = v.toWTFString(m_lexicalGlobalObject); + RETURN_IF_EXCEPTION(scope, JSValue()); + } + + write(ErrorInstanceTag); + write(errorNameToSerializableErrorType(errorTypeString)); + writeNullableString(message); + write(line); + write(column); + writeNullableString(sourceURL); + writeNullableString(stack); + JSC::PropertyDescriptor cause; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->cause, cause); + RETURN_IF_EXCEPTION(scope, JSValue()); + bool hasCause = found && cause.isDataDescriptor(); + write(static_cast(hasCause)); + return hasCause ? cause.value() : JSValue(); + } + bool dumpIfTerminal(JSValue value, SerializationReturnCode& code) { if (!value.isCell()) { @@ -1188,80 +1274,8 @@ class CloneSerializer : public CloneBase { write(String(JSC::Yarr::flagsString(regExp->regExp()->flags()).span())); return true; } - if (auto* errorInstance = dynamicDowncast(obj)) { - if (!startObjectInternal(errorInstance)) // handle duplicates - return true; - auto& vm = m_lexicalGlobalObject->vm(); - auto errorTypeValue = errorInstance->get(m_lexicalGlobalObject, vm.propertyNames->name); - RETURN_IF_EXCEPTION(scope, false); - auto errorTypeString = errorTypeValue.toWTFString(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - - // .message/.line/.column/.sourceURL: HTML spec + Node/WebKit read - // OWN data descriptors only (an inherited or accessor .message is - // NOT serialized). .stack: Node reads via [[Get]] to materialize - // V8's lazy accessor. Any getter/coercion/prepareStackTrace throw - // propagates out of postMessage/structuredClone (Node parity). - String message, sourceURL, stack; - unsigned line = 0, column = 0; - { - // .message is ToString'd rather than gated on isString (node clones - // `e.message = 42` as "42"). Reading it before .line also keeps a - // Symbol message from reaching ErrorInstance's lazy materialization. - JSC::PropertyDescriptor d; - bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->message, d); - RETURN_IF_EXCEPTION(scope, false); - if (found && d.isDataDescriptor() && d.value()) { - message = d.value().toWTFString(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - } - } - // Trigger ErrorInstance's lazy materialization up front so a throwing - // prepareStackTrace propagates here instead of tripping the exception - // assertion inside JSObject::getOwnPropertyDescriptor. - errorInstance->materializeErrorInfoIfNeeded(vm); - RETURN_IF_EXCEPTION(scope, false); - { - JSC::PropertyDescriptor d; - bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->line, d); - RETURN_IF_EXCEPTION(scope, false); - if (found && d.isDataDescriptor() && d.value().isNumber()) - line = d.value().toNumber(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - } - { - JSC::PropertyDescriptor d; - bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->column, d); - RETURN_IF_EXCEPTION(scope, false); - if (found && d.isDataDescriptor() && d.value().isNumber()) - column = d.value().toNumber(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - } - { - JSC::PropertyDescriptor d; - bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->sourceURL, d); - RETURN_IF_EXCEPTION(scope, false); - if (found && d.isDataDescriptor() && d.value().isString()) - sourceURL = d.value().toWTFString(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - } - { - JSValue v = errorInstance->get(m_lexicalGlobalObject, vm.propertyNames->stack); - RETURN_IF_EXCEPTION(scope, false); - if (v.isString()) - stack = v.toWTFString(m_lexicalGlobalObject); - RETURN_IF_EXCEPTION(scope, false); - } - - write(ErrorInstanceTag); - write(errorNameToSerializableErrorType(errorTypeString)); - writeNullableString(message); - write(line); - write(column); - writeNullableString(sourceURL); - writeNullableString(stack); - return true; - } + if (obj->inherits()) + return false; if (obj->inherits()) { auto index = m_transferredMessagePorts.find(obj); if (index != m_transferredMessagePorts.end()) { @@ -2223,6 +2237,28 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) goto setDataStartVisitEntry; } + errorStartState: + { + if (inputObjectStack.size() > maximumFilterRecursion) + return SerializationReturnCode::StackOverflowError; + auto* error = jsCast(inValue); + if (!startObjectInternal(error)) + break; + JSValue cause = dumpError(error); + RETURN_IF_EXCEPTION(scope, SerializationReturnCode::ExistingExceptionError); + if (!cause) + break; + // Keep the Error in the same pool before traversing its cause, including self references. + inputObjectStack.append(error); + m_gcBuffer.appendWithCrashOnOverflow(cause); + inValue = cause; + stateStack.append(ErrorEndVisitCause); + goto stateUnknown; + } + case ErrorEndVisitCause: + inputObjectStack.removeLast(); + break; + stateUnknown: case StateUnknown: { auto terminalCode = SerializationReturnCode::SuccessfullyCompleted; @@ -2240,6 +2276,8 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) goto mapStartState; if (isSet(inValue)) goto setStartState; + if (inValue.inherits()) + goto errorStartState; goto objectStartState; } } @@ -3572,6 +3610,43 @@ class CloneDeserializer : public CloneBase { return tryConvertToBigInt32(bigInt); } + ErrorInstance* readError() + { + SerializableErrorType serializedErrorType; + if (!read(serializedErrorType)) { + fail(); + return nullptr; + } + String message; + if (!readNullableString(message)) { + fail(); + return nullptr; + } + uint32_t line; + if (!read(line)) { + fail(); + return nullptr; + } + uint32_t column; + if (!read(column)) { + fail(); + return nullptr; + } + String sourceURL; + if (!readNullableString(sourceURL)) { + fail(); + return nullptr; + } + String stackString; + if (!readNullableString(stackString)) { + fail(); + return nullptr; + } + auto* obj = ErrorInstance::create(m_lexicalGlobalObject, WTF::move(message), toErrorType(serializedErrorType), { line, column }, WTF::move(sourceURL), WTF::move(stackString)); + addTerminalToObjectPool(obj); + return obj; + } + JSValue readTerminal() { const uint8_t* preTagPtr = m_ptr; @@ -3699,41 +3774,13 @@ class CloneDeserializer : public CloneBase { addTerminalToObjectPool(obj); return obj; } - case ErrorInstanceTag: { - SerializableErrorType serializedErrorType; - if (!read(serializedErrorType)) { - fail(); - return JSValue(); - } - String message; - if (!readNullableString(message)) { - fail(); - return JSValue(); + case ErrorInstanceTag: + if (m_version < FirstVersionWithErrorCause) { + auto* error = readError(); + return error ? JSValue(error) : JSValue(); } - uint32_t line; - if (!read(line)) { - fail(); - return JSValue(); - } - uint32_t column; - if (!read(column)) { - fail(); - return JSValue(); - } - String sourceURL; - if (!readNullableString(sourceURL)) { - fail(); - return JSValue(); - } - String stackString; - if (!readNullableString(stackString)) { - fail(); - return JSValue(); - } - auto* obj = ErrorInstance::create(m_lexicalGlobalObject, WTF::move(message), toErrorType(serializedErrorType), { line, column }, WTF::move(sourceURL), WTF::move(stackString)); - addTerminalToObjectPool(obj); - return obj; - } + m_ptr = preTagPtr; + return JSValue(); case ObjectReferenceTag: { auto index = readConstantPoolIndex(m_objectPool); if (!index || *index >= static_cast(m_objectPool.size())) { @@ -4141,6 +4188,34 @@ DeserializationResult CloneDeserializer::deserialize() goto setDataStartVisitEntry; } + errorStartState: + { + if (outputObjectStack.size() > maximumFilterRecursion) + return std::make_pair(JSValue(), SerializationReturnCode::StackOverflowError); + auto* error = readError(); + if (scope.exception() || !error) + goto error; + uint8_t hasCause; + if (!read(hasCause) || hasCause > 1) + goto error; + if (!hasCause) { + outValue = error; + break; + } + outputObjectStack.append(error); + stateStack.append(ErrorEndVisitCause); + goto stateUnknown; + } + case ErrorEndVisitCause: { + auto* error = outputObjectStack.last(); + error->putDirect(vm, vm.propertyNames->cause, outValue, PropertyAttribute::DontEnum); + if (scope.exception()) + goto error; + outValue = error; + outputObjectStack.removeLast(); + break; + } + stateUnknown: case StateUnknown: JSValue terminal = readTerminal(); @@ -4161,6 +4236,8 @@ DeserializationResult CloneDeserializer::deserialize() goto mapObjectStartState; if (tag == SetObjectTag) goto setObjectStartState; + if (tag == ErrorInstanceTag && m_version >= FirstVersionWithErrorCause) + goto errorStartState; goto error; } if (stateStack.isEmpty()) diff --git a/test/js/web/structured-clone-shared-array-buffer.test.ts b/test/js/web/structured-clone-shared-array-buffer.test.ts index 0fe1e595957c..191cbf6e72cd 100644 --- a/test/js/web/structured-clone-shared-array-buffer.test.ts +++ b/test/js/web/structured-clone-shared-array-buffer.test.ts @@ -49,3 +49,55 @@ describe("structuredClone(SharedArrayBuffer)", () => { } }); }); + +describe("Error.cause SharedArrayBuffer graph", () => { + test("nested views retain aliases and shared backing memory", () => { + const buffer = new SharedArrayBuffer(4); + const view = new Uint8Array(buffer); + const error = new Error("outer", { cause: new Map([["view", view]]) }); + const result = structuredClone({ error, view, buffer }); + expect(result.error).toBeInstanceOf(Error); + expect(result.error.message).toBe("outer"); + const cause = result.error.cause; + expect(cause).toBeInstanceOf(Map); + if (!(cause instanceof Map)) throw new Error("Expected cloned Map cause"); + expect(cause.get("view")).toBe(result.view); + expect(result.view.buffer).toBe(result.buffer); + view[0] = 42; + expect(result.view[0]).toBe(42); + result.view[1] = 7; + expect(view[1]).toBe(7); + }); + + test("worker postMessage preserves cause aliases and shared memory", async () => { + const buffer = new SharedArrayBuffer(4); + const view = new Uint8Array(buffer); + const url = URL.createObjectURL( + new Blob( + [ + "self.onmessage = ({data}) => { data.error.cause[0] = 42; postMessage({error: data.error, same: data.error.cause === data.view}); };", + ], + { type: "application/javascript" }, + ), + ); + const worker = new Worker(url); + try { + const { promise, resolve, reject } = Promise.withResolvers(); + worker.onmessage = resolve; + worker.onerror = reject; + worker.postMessage({ error: new Error("worker", { cause: view }), view }); + const { data } = await promise; + expect(data.same).toBe(true); + expect(data.error).toBeInstanceOf(Error); + expect(data.error.message).toBe("worker"); + expect(data.error.cause).toBeInstanceOf(Uint8Array); + expect(data.error.cause[0]).toBe(42); + expect(view[0]).toBe(42); + data.error.cause[1] = 7; + expect(view[1]).toBe(7); + } finally { + worker.terminate(); + URL.revokeObjectURL(url); + } + }); +}); diff --git a/test/js/web/workers/structured-clone.test.ts b/test/js/web/workers/structured-clone.test.ts index 95f6d9ecc3a1..17b1be437218 100644 --- a/test/js/web/workers/structured-clone.test.ts +++ b/test/js/web/workers/structured-clone.test.ts @@ -1404,3 +1404,189 @@ describe("Map/Set own properties across message and storage boundaries", () => { expect(await proc.stderr.text()).toBe(""); }); }); + +function v8SerializeRoundtrip(value: any) { + return v8Deserialize(v8Serialize(value)); +} + +for (const clone of [structuredClone, jscSerializeRoundtrip, v8SerializeRoundtrip]) { + describe("Error.cause graph serialization: " + clone.name, () => { + test.each([undefined, null, false, 0, -0, NaN, Infinity, "", "cause", 42n])("own data cause %p", cause => { + const result = clone(new Error("outer", { cause })); + expect(result).toBeInstanceOf(Error); + expect(result.message).toBe("outer"); + expect(Object.getOwnPropertyDescriptor(result, "cause")).toEqual({ + value: cause, + writable: true, + enumerable: false, + configurable: true, + }); + }); + + test("nested errors preserve their type, message and cause", () => { + const result = clone(new TypeError("outer", { cause: new RangeError("inner", { cause: { code: 42 } }) })); + expect(result).toBeInstanceOf(TypeError); + expect(result.message).toBe("outer"); + expect(result.cause).toBeInstanceOf(RangeError); + expect(result.cause.message).toBe("inner"); + expect(result.cause.cause).toEqual({ code: 42 }); + }); + + test("self, mutual and container cycles preserve object identity", () => { + const self = new Error("self"); + self.cause = self; + const first = new Error("first"); + const second = new Error("second", { cause: first }); + first.cause = second; + const container = new Error("container"); + container.cause = { error: container }; + const result = clone([self, first, second, container]); + expect(result[0].cause).toBe(result[0]); + expect(result[1].cause).toBe(result[2]); + expect(result[2].cause).toBe(result[1]); + expect(result[3].cause.error).toBe(result[3]); + }); + + test("cause aliases survive reference-pool index width changes", () => { + const shared = { marker: "shared" }; + const errors = Array.from({ length: 300 }, (_, i) => new Error(String(i), { cause: shared })); + const result = clone([shared, ...errors, errors[0], shared]); + for (let i = 1; i <= errors.length; i++) { + expect(result[i]).toBeInstanceOf(Error); + expect(result[i].message).toBe(String(i - 1)); + expect(result[i].cause).toBe(result[0]); + } + expect(result[301]).toBe(result[1]); + expect(result[302]).toBe(result[0]); + }); + + test("deep causes use the shared iterative graph traversal", () => { + let source: Error = new Error("leaf"); + for (let i = 0; i < 1500; i++) source = new Error(String(i), { cause: source }); + let result = clone(source); + for (let i = 1499; i >= 0; i--) { + expect(result.message).toBe(String(i)); + result = result.cause; + } + expect(result.message).toBe("leaf"); + expect(Object.hasOwn(result, "cause")).toBe(false); + }); + + test("cause traversal stays rooted when a nested getter mutates the source and collects", () => { + const source = new Error("outer"); + source.cause = { + get child() { + source.cause = undefined; + Bun.gc(true); + return source; + }, + }; + const result = clone(source); + expect(result.cause.child).toBe(result); + }); + + test("absent, inherited and accessor causes are omitted without calling getters", () => { + const absent = new Error("absent"); + const inherited = new Error("inherited"); + const accessor = new Error("accessor"); + let calls = 0; + Object.setPrototypeOf( + inherited, + Object.create(Error.prototype, { + cause: { + get() { + calls++; + throw new Error("inherited getter"); + }, + }, + }), + ); + Object.defineProperty(accessor, "cause", { + get() { + calls++; + throw new Error("own getter"); + }, + }); + const result = clone([absent, inherited, accessor]); + expect(result.map((value: Error) => Object.hasOwn(value, "cause"))).toEqual([false, false, false]); + expect(calls).toBe(0); + }); + + test.each([() => {}, Symbol("uncloneable"), new WeakMap()])("rejects uncloneable cause %p", cause => { + expect(() => clone(new Error("outer", { cause }))).toThrow(expect.objectContaining({ name: "DataCloneError" })); + }); + + test("a getter inside a cause propagates the original thrown value", () => { + const thrown = new Error("nested getter"); + const cause = { + get value() { + throw thrown; + }, + }; + let caught: unknown; + try { + clone(new Error("outer", { cause })); + } catch (error) { + caught = error; + } + expect(caught).toBe(thrown); + }); + }); +} + +test("Error.cause graph serialization survives a fresh process", () => { + const error = new TypeError("outer"); + const shared = { error }; + error.cause = shared; + const result = jscSerializeRoundtripCrossProcessCold([error, shared, error]); + expect(result[0]).toBeInstanceOf(TypeError); + expect(result[0].message).toBe("outer"); + expect(result[0].cause).toBe(result[1]); + expect(result[1].error).toBe(result[0]); + expect(result[2]).toBe(result[0]); +}); + +// Captured with format version 14, before Error.cause was part of the wire format. +const legacyErrorGraph = Buffer.from( + "DgAAAAEEAAAAAAAAADcFAAAAAAYAAIBsZWdhY3kBAAAALAAAAAAAAAA5AACAL3ByaXZhdGUvdG1wL3BsdWdpbi1uYXRpdmUtYnVuLXJlcGFpci0yMDI2MTAxOC9idW4vW2V2YWxdAAAAAAAAAIABAAAAEwECAAAAAgEAAIBuBSoAAAD/////AwAAABMC/////w==", + "base64", +); + +for (const [name, read] of [ + ["bun:jsc", deserialize], + ["node:v8", v8Deserialize], +] as const) { + test("Error.cause format accepts legacy pooled Error records: " + name, () => { + const result = read(legacyErrorGraph); + expect(result[0]).toBeInstanceOf(TypeError); + expect(result[0].message).toBe("legacy"); + expect(Object.hasOwn(result[0], "cause")).toBe(false); + expect(result[1]).toBe(result[0]); + expect(result[2]).toEqual({ n: 42 }); + expect(result[3]).toBe(result[2]); + }); + + test("Error.cause format rejects truncated and malformed records: " + name, () => { + const error = new Error("wire", { cause: 1 }); + error.stack = ""; + const bytes = Buffer.from(serialize(error)); + expect(bytes.readUInt32LE(0)).toBe(15); + expect(read(bytes).cause).toBe(1); + for (let end = 4; end < bytes.length; end++) { + expect(() => read(bytes.subarray(0, end))).toThrow("Unable to deserialize data."); + } + // A present cause ends with the presence byte and OneTag for this primitive. + expect([...bytes.subarray(-2)]).toEqual([1, 7]); + const invalidPresence = Buffer.from(bytes); + invalidPresence[invalidPresence.length - 2] = 2; + expect(() => read(invalidPresence)).toThrow("Unable to deserialize data."); + const invalidTag = Buffer.from(bytes); + invalidTag[invalidTag.length - 1] = 255; + expect(() => read(invalidTag)).toThrow("Unable to deserialize data."); + const invalidReference = Buffer.concat([bytes.subarray(0, -1), Buffer.from([19, 255])]); + expect(() => read(invalidReference)).toThrow("Unable to deserialize data."); + const future = Buffer.from(bytes); + future.writeUInt32LE(16, 0); + expect(() => read(future)).toThrow("Unable to deserialize data."); + }); +} From ec0778d0f12c294df11b1fe8cfa0862b694e95ef Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 19:11:41 -0600 Subject: [PATCH 02/11] fix: align Error cause traversal with current engine APIs --- src/js/internal/serialization_buffers.ts | 10 ++++-- .../webcore/SerializedScriptValue.cpp | 4 +-- test/js/node/v8/v8-serdes-buffer.test.ts | 32 +++++++++++++++++++ ...ructured-clone-shared-array-buffer.test.ts | 31 ++++++++++++++++++ 4 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/js/internal/serialization_buffers.ts b/src/js/internal/serialization_buffers.ts index 2013e9aa0199..d9b4439ce728 100644 --- a/src/js/internal/serialization_buffers.ts +++ b/src/js/internal/serialization_buffers.ts @@ -3,7 +3,7 @@ // Node ref: https://github.com/nodejs/node/blob/main/lib/internal/child_process/serialization.js const { Buffer } = require("node:buffer"); -const { isUint8Array } = require("node:util/types"); +const { isNativeError, isUint8Array } = require("node:util/types"); const BufferPrototype = Buffer.prototype; const isBuffer = Buffer.isBuffer; const ObjectKeys = Object.keys; @@ -40,8 +40,12 @@ function tagBuffers(value: unknown): [unknown, unknown[]] | null { current.$forEach(entry => $arrayPush(stack, entry)); continue; } - // Objects, arrays and errors: the serializer walks own enumerable - // properties. Only data properties are descended so no getter runs here. + // Error causes are usually non-enumerable, but are part of the serialized graph. + if (isNativeError(current)) { + const cause = ObjectGetOwnPropertyDescriptor(current, "cause"); + if (cause && ObjectPrototypeHasOwnProperty.$call(cause, "value")) $arrayPush(stack, cause.value); + } + // Only data properties are descended so no getter runs here. const keys = ObjectKeys(current); for (let i = 0; i < keys.length; i++) { const desc = ObjectGetOwnPropertyDescriptor(current, keys[i]); diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 61fd7e2224a3..cf6f91c1c4db 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -2241,7 +2241,7 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) { if (inputObjectStack.size() > maximumFilterRecursion) return SerializationReturnCode::StackOverflowError; - auto* error = jsCast(inValue); + auto* error = uncheckedDowncast(inValue); if (!startObjectInternal(error)) break; JSValue cause = dumpError(error); @@ -4208,7 +4208,7 @@ DeserializationResult CloneDeserializer::deserialize() } case ErrorEndVisitCause: { auto* error = outputObjectStack.last(); - error->putDirect(vm, vm.propertyNames->cause, outValue, PropertyAttribute::DontEnum); + error->putDirect(vm, vm.propertyNames->cause, outValue, static_cast(PropertyAttribute::DontEnum)); if (scope.exception()) goto error; outValue = error; diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index 6ab422212b20..144efeb35360 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -50,6 +50,38 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(Buffer.isBuffer(value.v)).toBe(true); }); + test("Error.cause-only Buffers retain their type and graph identity", () => { + const buffer = Buffer.from("cause").subarray(1, 4); + const error = new Error("outer", { cause: new Map([[buffer, buffer]]) }); + const out = v8.deserialize(v8.serialize(error)); + expect(out).toBeInstanceOf(Error); + expect(out.cause).toBeInstanceOf(Map); + const [[key, value]] = [...out.cause]; + expect(Buffer.isBuffer(key)).toBe(true); + expect(key).toBe(value); + expect(key.toString()).toBe("aus"); + }); + + test("Error.cause Buffer scanning terminates on Error cycles without invoking accessors", () => { + const error = new Error("outer"); + const nested = new Error("inner", { cause: error }); + const accessor = new Error("accessor"); + let reads = 0; + Object.defineProperty(accessor, "cause", { + get() { + reads++; + throw new Error("must not read accessor"); + }, + }); + error.cause = [nested, Buffer.from("cycle"), accessor]; + const out = v8.deserialize(v8.serialize(error)); + expect(out.cause[0].cause).toBe(out); + expect(Buffer.isBuffer(out.cause[1])).toBe(true); + expect(out.cause[1].toString()).toBe("cycle"); + expect(Object.hasOwn(out.cause[2], "cause")).toBe(false); + expect(reads).toBe(0); + }); + test("Buffer-free payloads keep the bare format (old readers still work)", () => { const jsc = require("bun:jsc"); const bytes = v8.serialize({ n: 1, s: "x" }); diff --git a/test/js/web/structured-clone-shared-array-buffer.test.ts b/test/js/web/structured-clone-shared-array-buffer.test.ts index 191cbf6e72cd..4c313147461d 100644 --- a/test/js/web/structured-clone-shared-array-buffer.test.ts +++ b/test/js/web/structured-clone-shared-array-buffer.test.ts @@ -1,4 +1,6 @@ +import { serialize as jscSerialize } from "bun:jsc"; import { describe, expect, test } from "bun:test"; +import { serialize as v8Serialize } from "node:v8"; // structuredClone stays inside the agent cluster, so SharedArrayBuffers share // their backing store like node and the HTML spec's StructuredSerialize. @@ -51,6 +53,35 @@ describe("structuredClone(SharedArrayBuffer)", () => { }); describe("Error.cause SharedArrayBuffer graph", () => { + test("an ArrayBuffer cause transfers once and retains view aliases", () => { + const buffer = new ArrayBuffer(4); + const view = new Uint8Array(buffer); + view[0] = 42; + const result = structuredClone(new Error("transfer", { cause: { buffer, view } }), { transfer: [buffer] }); + expect(buffer.byteLength).toBe(0); + expect(result.cause.buffer).toBeInstanceOf(ArrayBuffer); + expect(result.cause.view.buffer).toBe(result.cause.buffer); + expect([...result.cause.view]).toEqual([42, 0, 0, 0]); + }); + + test("a failing cause leaves transferred buffers attached", () => { + const buffer = new ArrayBuffer(4); + new Uint8Array(buffer)[0] = 42; + const source = new Error("invalid", { cause: { buffer, invalid: () => {} } }); + expect(() => structuredClone(source, { transfer: [buffer] })).toThrow( + expect.objectContaining({ name: "DataCloneError" }), + ); + expect(buffer.byteLength).toBe(4); + expect([...new Uint8Array(buffer)]).toEqual([42, 0, 0, 0]); + }); + + for (const serialize of [jscSerialize, v8Serialize]) { + test("storage serialization rejects shared memory hidden in a cause: " + serialize.name, () => { + const source = new Error("storage", { cause: new Uint8Array(new SharedArrayBuffer(4)) }); + expect(() => serialize(source)).toThrow(expect.objectContaining({ name: "DataCloneError" })); + }); + } + test("nested views retain aliases and shared backing memory", () => { const buffer = new SharedArrayBuffer(4); const view = new Uint8Array(buffer); From 67b55c5fd1d9ad0042171576dd070128c8eefa56 Mon Sep 17 00:00:00 2001 From: Jason Sy Date: Sun, 18 Oct 2026 19:37:37 -0600 Subject: [PATCH 03/11] fix: retain bounded worker error metadata serialization --- .../webcore/SerializedScriptValue.cpp | 5 ++++ .../bindings/webcore/SerializedScriptValue.h | 3 ++- .../bindings/webcore/WorkerMessagingProxy.cpp | 13 +++++----- ...ructured-clone-shared-array-buffer.test.ts | 25 ++++++++++++++----- 4 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index cf6f91c1c4db..011c25232e85 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -1173,6 +1173,11 @@ class CloneSerializer : public CloneBase { write(column); writeNullableString(sourceURL); writeNullableString(stack); + // Worker error events carry causes through their own bounded, best-effort metadata owner. + if (m_context == SerializationContext::WorkerError) { + write(static_cast(0)); + return JSValue(); + } JSC::PropertyDescriptor cause; bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->cause, cause); RETURN_IF_EXCEPTION(scope, JSValue()); diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.h b/src/jsc/bindings/webcore/SerializedScriptValue.h index 38a38b9cc6c2..112a541cc0d5 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.h +++ b/src/jsc/bindings/webcore/SerializedScriptValue.h @@ -88,7 +88,8 @@ enum class SerializationErrorMode { NonThrowing, Throwing }; enum class SerializationContext { Default, WorkerPostMessage, - WindowPostMessage }; + WindowPostMessage, + WorkerError }; enum class SerializationForStorage : bool { No, Yes }; enum class SerializationForCrossProcessTransfer : bool { No, diff --git a/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp b/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp index 202fac234c16..3ef9f8867fe0 100644 --- a/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp +++ b/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp @@ -685,7 +685,7 @@ static RefPtr serializeWorkerErrorMetadata(Zig::G scope.clearException(); } } - metadata->properties = SerializedScriptValue::create(globalObject, propertiesObject, SerializationForStorage::No, SerializationErrorMode::NonThrowing); + metadata->properties = SerializedScriptValue::create(globalObject, propertiesObject, SerializationForStorage::No, SerializationErrorMode::NonThrowing, SerializationContext::WorkerError); } CLEAR_IF_EXCEPTION(scope); @@ -706,7 +706,7 @@ static RefPtr serializeWorkerErrorMetadata(Zig::G if (cause.isCallable() || cause.isSymbol()) return metadata; - metadata->cause = SerializedScriptValue::create(globalObject, cause, SerializationForStorage::No, SerializationErrorMode::NonThrowing); + metadata->cause = SerializedScriptValue::create(globalObject, cause, SerializationForStorage::No, SerializationErrorMode::NonThrowing, SerializationContext::WorkerError); CLEAR_IF_EXCEPTION(scope); if (!metadata->cause) return metadata; @@ -776,7 +776,7 @@ bool WorkerMessagingProxy::postSerializedErrorToWorkerObject(Zig::GlobalObject& auto& vm = JSC::getVM(&workerGlobalObject); auto scope = DECLARE_TOP_EXCEPTION_SCOPE(vm); - auto serialized = SerializedScriptValue::create(workerGlobalObject, value, SerializationForStorage::No, SerializationErrorMode::NonThrowing); + auto serialized = SerializedScriptValue::create(workerGlobalObject, value, SerializationForStorage::No, SerializationErrorMode::NonThrowing, SerializationContext::WorkerError); CLEAR_IF_EXCEPTION(scope); // Cloning an Error reads `stack`; when that getter throws (a throwing Error.prepareStackTrace), // Node drops only `stack` (lib/internal/error_serdes.js TryGetAllProperties) rather than the @@ -790,7 +790,7 @@ bool WorkerMessagingProxy::postSerializedErrorToWorkerObject(Zig::GlobalObject& CLEAR_IF_EXCEPTION(scope); errorInstance->putDirect(vm, vm.propertyNames->stack, JSC::jsUndefined(), static_cast(JSC::PropertyAttribute::DontEnum)); if (!vm.hasPendingTerminationException()) - serialized = SerializedScriptValue::create(workerGlobalObject, value, SerializationForStorage::No, SerializationErrorMode::NonThrowing); + serialized = SerializedScriptValue::create(workerGlobalObject, value, SerializationForStorage::No, SerializationErrorMode::NonThrowing, SerializationContext::WorkerError); CLEAR_IF_EXCEPTION(scope); } } @@ -801,9 +801,8 @@ bool WorkerMessagingProxy::postSerializedErrorToWorkerObject(Zig::GlobalObject& // preserves a string `error.code` (lib/internal/error_serdes.js). String errorCode = errorCodeOf(workerGlobalObject, value); - // Node's worker error serializer preserves an Error's own enumerable metadata and recursively - // serializes `cause`. The ordinary structured-clone Error path intentionally keeps only - // standard Error fields, so carry these worker-only additions separately. + // WorkerError omits causes from the general clone so this bounded, best-effort + // metadata owner alone handles them, including uncloneable causes and enumerable fields. RefPtr serializedMetadata; if (auto* errorInstance = dynamicDowncast(value)) { HashSet seen; diff --git a/test/js/web/structured-clone-shared-array-buffer.test.ts b/test/js/web/structured-clone-shared-array-buffer.test.ts index 4c313147461d..9070f059764e 100644 --- a/test/js/web/structured-clone-shared-array-buffer.test.ts +++ b/test/js/web/structured-clone-shared-array-buffer.test.ts @@ -1,6 +1,6 @@ -import { serialize as jscSerialize } from "bun:jsc"; +import { deserialize as jscDeserialize, serialize as jscSerialize } from "bun:jsc"; import { describe, expect, test } from "bun:test"; -import { serialize as v8Serialize } from "node:v8"; +import { deserialize as v8Deserialize, serialize as v8Serialize } from "node:v8"; // structuredClone stays inside the agent cluster, so SharedArrayBuffers share // their backing store like node and the HTML spec's StructuredSerialize. @@ -75,10 +75,23 @@ describe("Error.cause SharedArrayBuffer graph", () => { expect([...new Uint8Array(buffer)]).toEqual([42, 0, 0, 0]); }); - for (const serialize of [jscSerialize, v8Serialize]) { - test("storage serialization rejects shared memory hidden in a cause: " + serialize.name, () => { - const source = new Error("storage", { cause: new Uint8Array(new SharedArrayBuffer(4)) }); - expect(() => serialize(source)).toThrow(expect.objectContaining({ name: "DataCloneError" })); + for (const [name, serialize, deserialize] of [ + ["bun:jsc", jscSerialize, jscDeserialize], + ["node:v8", v8Serialize, v8Deserialize], + ] as const) { + test("storage serialization copies shared causes into private buffers: " + name, () => { + const buffer = new SharedArrayBuffer(4); + const view = new Uint8Array(buffer); + view[0] = 42; + const result = deserialize(serialize(new Error("storage", { cause: { buffer, view } }))); + expect(result.cause.buffer).toBeInstanceOf(ArrayBuffer); + expect(result.cause.buffer).not.toBeInstanceOf(SharedArrayBuffer); + expect(result.cause.view.buffer).toBe(result.cause.buffer); + expect([...result.cause.view]).toEqual([42, 0, 0, 0]); + view[0] = 7; + expect(result.cause.view[0]).toBe(42); + result.cause.view[1] = 9; + expect(view[1]).toBe(0); }); } From a1ef1ee4bb2dc3e8dd76f3bd949680fffbff4954 Mon Sep 17 00:00:00 2001 From: Jason Sy Date: Sun, 18 Oct 2026 20:12:11 -0600 Subject: [PATCH 04/11] fix: snapshot Error causes before metadata effects --- .../webcore/SerializedScriptValue.cpp | 26 +++++----- test/js/node/v8/v8-serdes-buffer.test.ts | 27 ++++++++-- test/js/web/workers/structured-clone.test.ts | 49 +++++++++++++++++++ 3 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 011c25232e85..47145f8e0574 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -1105,6 +1105,20 @@ class CloneSerializer : public CloneBase { { auto& vm = m_lexicalGlobalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); + JSValue cause; + bool hasCause = false; + // Snapshot and root before metadata getters/coercions can replace the cause or collect it. + // Worker error events retain their separate bounded, best-effort metadata owner. + if (m_context != SerializationContext::WorkerError) { + JSC::PropertyDescriptor descriptor; + bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->cause, descriptor); + RETURN_IF_EXCEPTION(scope, JSValue()); + hasCause = found && descriptor.isDataDescriptor(); + if (hasCause) { + cause = descriptor.value(); + m_gcBuffer.appendWithCrashOnOverflow(cause); + } + } auto errorTypeValue = errorInstance->get(m_lexicalGlobalObject, vm.propertyNames->name); RETURN_IF_EXCEPTION(scope, JSValue()); auto errorTypeString = errorTypeValue.toWTFString(m_lexicalGlobalObject); @@ -1173,17 +1187,8 @@ class CloneSerializer : public CloneBase { write(column); writeNullableString(sourceURL); writeNullableString(stack); - // Worker error events carry causes through their own bounded, best-effort metadata owner. - if (m_context == SerializationContext::WorkerError) { - write(static_cast(0)); - return JSValue(); - } - JSC::PropertyDescriptor cause; - bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->cause, cause); - RETURN_IF_EXCEPTION(scope, JSValue()); - bool hasCause = found && cause.isDataDescriptor(); write(static_cast(hasCause)); - return hasCause ? cause.value() : JSValue(); + return cause; } bool dumpIfTerminal(JSValue value, SerializationReturnCode& code) @@ -2255,7 +2260,6 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) break; // Keep the Error in the same pool before traversing its cause, including self references. inputObjectStack.append(error); - m_gcBuffer.appendWithCrashOnOverflow(cause); inValue = cause; stateStack.append(ErrorEndVisitCause); goto stateUnknown; diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index 144efeb35360..99c58fc48bf1 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -4,7 +4,7 @@ import v8 from "node:v8"; // Buffer identity through v8.serialize/deserialize, matching node v26.3.0's // serializer delegate behavior (DefaultSerializer host objects). Non-Buffer -// payloads keep the bare JSC-serialized format for backward compatibility. +// payloads keep bare JSC framing instead of the Buffer envelope. describe("v8 serialize/deserialize Buffer identity", () => { test.each([new Uint8Array([1, 2]), Buffer.from([1, 2])])("serialized output is transferable: %p", input => { const serialized = v8.serialize(input); @@ -62,6 +62,23 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(key.toString()).toBe("aus"); }); + test.each(["name", "message", "stack"])("cause snapshot retains Buffer brand and bytes before %s effects", field => { + const source = new Error("outer", { cause: Buffer.from("original").subarray(1, 5) }); + source.stack = ""; + let calls = 0; + const effect = () => { + calls++; + source.cause = Buffer.from("replacement"); + return field === "name" ? "Error" : "metadata"; + }; + Object.defineProperty(source, field, field === "message" ? { value: { toString: effect } } : { get: effect }); + const out = v8.deserialize(v8.serialize(source)); + expect(calls).toBeGreaterThan(0); + expect(Buffer.isBuffer(out.cause)).toBe(true); + expect(out.cause.toString()).toBe("rigi"); + expect(out.cause.length).toBe(4); + }); + test("Error.cause Buffer scanning terminates on Error cycles without invoking accessors", () => { const error = new Error("outer"); const nested = new Error("inner", { cause: error }); @@ -82,14 +99,14 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(reads).toBe(0); }); - test("Buffer-free payloads keep the bare format (old readers still work)", () => { + test("Buffer-free payloads keep bare JSC framing in the current runtime", () => { const jsc = require("bun:jsc"); const bytes = v8.serialize({ n: 1, s: "x" }); // Bare JSC bytes deserialize directly — no envelope framing present. expect(jsc.deserialize(bytes)).toEqual({ n: 1, s: "x" }); - // And deserialize accepts pre-envelope output produced by jsc.serialize. - const legacy = jsc.serialize({ buf: new Uint8Array([7]) }, { binaryType: "nodebuffer" }); - expect(v8.deserialize(legacy)).toEqual({ buf: new Uint8Array([7]) }); + // deserialize also accepts current bare output from jsc.serialize. + const bare = jsc.serialize({ buf: new Uint8Array([7]) }, { binaryType: "nodebuffer" }); + expect(v8.deserialize(bare)).toEqual({ buf: new Uint8Array([7]) }); }); test("rejects an envelope whose buffer list holds a non-Uint8Array view", () => { diff --git a/test/js/web/workers/structured-clone.test.ts b/test/js/web/workers/structured-clone.test.ts index 17b1be437218..2fef92d8ae25 100644 --- a/test/js/web/workers/structured-clone.test.ts +++ b/test/js/web/workers/structured-clone.test.ts @@ -1432,6 +1432,55 @@ for (const clone of [structuredClone, jscSerializeRoundtrip, v8SerializeRoundtri expect(result.cause.cause).toEqual({ code: 42 }); }); + for (const field of ["name", "message", "stack"]) { + test.each(["replace", "delete", "uncloneable"])( + "cause snapshot precedes " + field + " metadata effects: %s", + mutation => { + const source = new Error("outer", { cause: { marker: "original" } }); + source.stack = ""; + let calls = 0; + const effect = () => { + calls++; + if (mutation === "delete") delete source.cause; + else source.cause = mutation === "replace" ? { marker: "replacement" } : () => {}; + return field === "name" ? "Error" : "metadata"; + }; + Object.defineProperty(source, field, field === "message" ? { value: { toString: effect } } : { get: effect }); + const result = clone(source); + expect(calls).toBeGreaterThan(0); + expect(result.cause).toEqual({ marker: "original" }); + }, + ); + } + + test("cause snapshot stays rooted across metadata deletion and early collection", () => { + const source = new Error("outer"); + source.cause = { error: source, bytes: new Uint8Array([17, 42]) }; + Object.defineProperty(source, "stack", { + get() { + delete source.cause; + Bun.gc(true); + return ""; + }, + }); + const result = clone(source); + expect(Object.hasOwn(source, "cause")).toBe(false); + expect(result.cause.error).toBe(result); + expect(result.cause.bytes).toEqual(new Uint8Array([17, 42])); + }); + + test("cause snapshot rejects an original uncloneable value removed by metadata", () => { + const source = new Error("outer", { cause: () => {} }); + Object.defineProperty(source, "stack", { + get() { + source.cause = "replacement"; + return ""; + }, + }); + expect(() => clone(source)).toThrow(expect.objectContaining({ name: "DataCloneError" })); + expect(source.cause).toBe("replacement"); + }); + test("self, mutual and container cycles preserve object identity", () => { const self = new Error("self"); self.cause = self; From b8d23042ad8c7f7fc1d6bd0b2012e7b2eecaf211 Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:17:29 -0600 Subject: [PATCH 05/11] fix: collect transport Buffer identities during serialization Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- src/js/builtins/Ipc.ts | 15 +-- src/js/internal/serialization_buffers.ts | 57 +------- src/js/node/v8.ts | 5 +- src/jsc/JSValue.rs | 11 ++ src/jsc/bindings/IPC.cpp | 15 --- src/jsc/bindings/NodeV8.cpp | 10 +- src/jsc/bindings/Serialization.cpp | 9 +- src/jsc/bindings/ZigGlobalObject.cpp | 3 - src/jsc/bindings/ZigGlobalObject.h | 1 - .../webcore/SerializedScriptValue.cpp | 114 +++++++++++++++- .../bindings/webcore/SerializedScriptValue.h | 5 +- src/runtime/ipc.rs | 32 ++--- test/js/node/v8/v8-serdes-buffer.test.ts | 122 +++++++++++++++++- test/js/web/workers/structured-clone.test.ts | 11 +- 14 files changed, 274 insertions(+), 136 deletions(-) diff --git a/src/js/builtins/Ipc.ts b/src/js/builtins/Ipc.ts index 351fc86e17d1..0b32806b6f0c 100644 --- a/src/js/builtins/Ipc.ts +++ b/src/js/builtins/Ipc.ts @@ -101,20 +101,7 @@ export function parseHandle(target, serialized, fd) { } /** - * Advanced-mode IPC Buffer tagging; the mechanism lives in - * internal/serialization_buffers (shared with node:v8 serialize/deserialize). - * Returns null when the message holds no Buffers so the caller keeps the bare - * wire format (and its zero-walk ack fast paths). - * - * @param {unknown} message - * @returns {[unknown, unknown[]] | null} - */ -export function tagAdvancedBuffers(message) { - return require("internal/serialization_buffers").tagBuffers(message); -} - -/** - * Receive side of tagAdvancedBuffers. + * Restore the Buffer identities collected by native advanced IPC serialization. * * @param {unknown} envelope * @returns {unknown} diff --git a/src/js/internal/serialization_buffers.ts b/src/js/internal/serialization_buffers.ts index d9b4439ce728..e38b74f66056 100644 --- a/src/js/internal/serialization_buffers.ts +++ b/src/js/internal/serialization_buffers.ts @@ -1,59 +1,10 @@ -// Buffer tagging for JSC-serialized transports (advanced IPC, node:v8). JSC has no host-object hook, -// so we ship a [value, buffers] envelope (identity-aliased) and restore Buffer prototypes on receive. -// Node ref: https://github.com/nodejs/node/blob/main/lib/internal/child_process/serialization.js +// Node transport envelopes are collected by the native serialization walk. +// The aliased Buffer views are restored only after the envelope is decoded. const { Buffer } = require("node:buffer"); -const { isNativeError, isUint8Array } = require("node:util/types"); +const { isUint8Array } = require("node:util/types"); const BufferPrototype = Buffer.prototype; -const isBuffer = Buffer.isBuffer; -const ObjectKeys = Object.keys; -const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; const ObjectSetPrototypeOf = Object.setPrototypeOf; -const ObjectPrototypeHasOwnProperty = Object.prototype.hasOwnProperty; -const SafeSet = Set; - -/** Returns null when `value` holds no Buffers, else the [value, buffers] envelope. */ -function tagBuffers(value: unknown): [unknown, unknown[]] | null { - let buffers: unknown[] | null = null; - let visited: Set | null = null; - const stack = [value]; - while (stack.length !== 0) { - const current = stack[stack.length - 1]; - stack.length--; - if (current === null || typeof current !== "object") continue; - visited ??= new SafeSet(); - if (visited.$has(current)) continue; - visited.$add(current); - if ($isTypedArrayView(current)) { - if (isBuffer(current)) $arrayPush((buffers ??= []), current); - // Other ArrayBuffer views round-trip with their type already. - continue; - } - if ($isMap(current)) { - current.$forEach((entry, key) => { - $arrayPush(stack, key); - $arrayPush(stack, entry); - }); - continue; - } - if ($isSet(current)) { - current.$forEach(entry => $arrayPush(stack, entry)); - continue; - } - // Error causes are usually non-enumerable, but are part of the serialized graph. - if (isNativeError(current)) { - const cause = ObjectGetOwnPropertyDescriptor(current, "cause"); - if (cause && ObjectPrototypeHasOwnProperty.$call(cause, "value")) $arrayPush(stack, cause.value); - } - // Only data properties are descended so no getter runs here. - const keys = ObjectKeys(current); - for (let i = 0; i < keys.length; i++) { - const desc = ObjectGetOwnPropertyDescriptor(current, keys[i]); - if (desc && ObjectPrototypeHasOwnProperty.$call(desc, "value")) $arrayPush(stack, desc.value); - } - } - return buffers === null ? null : [value, buffers]; -} /** * Receive side: restore the Buffer prototype on the envelope's aliased views @@ -75,4 +26,4 @@ function restoreBuffers(envelope: unknown): unknown { return (envelope as unknown[])[0]; } -export default { tagBuffers, restoreBuffers }; +export default { restoreBuffers }; diff --git a/src/js/node/v8.ts b/src/js/node/v8.ts index 7907365abbfc..d33e01742f56 100644 --- a/src/js/node/v8.ts +++ b/src/js/node/v8.ts @@ -298,7 +298,7 @@ function getCppHeapStatistics(type = "detailed") { } // Buffer-bearing payloads are framed as MAGIC + version + SSV([value, buffers]) so deserialize // can restore Buffer prototypes (see internal/serialization_buffers). Leading 0xFF cannot collide -// with bare SSV output; Buffer-free payloads stay bare SSV so older readers keep working. +// with bare SSV output; Buffer-free payloads retain the current bare SSV framing. function hasBufferEnvelopeMagic(view) { // In-bounds integer-indexed reads on a typed array never consult the @@ -345,8 +345,7 @@ function stopCoverage() { notimpl("stopCoverage"); } function serialize(arg1) { - const tagged = require("internal/serialization_buffers").tagBuffers(arg1); - return serializeNative(tagged === null ? arg1 : tagged, tagged !== null); + return serializeNative(arg1); } function getDefaultHeapSnapshotPath() { diff --git a/src/jsc/JSValue.rs b/src/jsc/JSValue.rs index 317fda790ecf..26188b55c406 100644 --- a/src/jsc/JSValue.rs +++ b/src/jsc/JSValue.rs @@ -2220,6 +2220,7 @@ pub enum TemporalType { pub struct SerializedFlags { pub for_cross_process_transfer: bool, pub for_storage: bool, + pub preserve_node_buffers: bool, } /// `JSValue.SerializedScriptValue` — owned view over a @@ -2228,8 +2229,13 @@ pub struct SerializedScriptValue { bytes: *const u8, size: usize, handle: *mut c_void, + has_buffer_envelope: bool, } impl SerializedScriptValue { + pub fn has_buffer_envelope(&self) -> bool { + self.has_buffer_envelope + } + /// Borrow the serialized bytes. Valid only while `self` is alive (the /// backing buffer is freed on drop); the lifetime is tied to `&self`. #[inline] @@ -2252,6 +2258,7 @@ struct SerializedScriptValueExternal { bytes: *const u8, size: usize, handle: *mut c_void, + has_buffer_envelope: bool, } /// Callback signature for [`JSValue::for_each`] / [`JSValue::for_each_with_context`]. @@ -2720,6 +2727,9 @@ impl JSValue { if flags.for_storage { bits |= 1 << 1; } + if flags.preserve_node_buffers { + bits |= 1 << 2; + } let ext = host_fn::from_js_host_call_generic(global, || { Bun__serializeJSValue(global, self, bits) })?; @@ -2730,6 +2740,7 @@ impl JSValue { bytes: ext.bytes, size: ext.size, handle: ext.handle, + has_buffer_envelope: ext.has_buffer_envelope, }) } } diff --git a/src/jsc/bindings/IPC.cpp b/src/jsc/bindings/IPC.cpp index ae5b0898b15a..1fb42b22f110 100644 --- a/src/jsc/bindings/IPC.cpp +++ b/src/jsc/bindings/IPC.cpp @@ -38,21 +38,6 @@ extern "C" [[ZIG_EXPORT(zero_is_throw)]] JSC::EncodedJSValue IPCParse(Zig::Globa return JSC::JSValue::encode(result); } -extern "C" [[ZIG_EXPORT(zero_is_throw)]] JSC::EncodedJSValue IPCTagAdvancedBuffers(Zig::GlobalObject* global, JSC::EncodedJSValue message) -{ - auto& vm = JSC::getVM(global); - auto scope = DECLARE_THROW_SCOPE(vm); - JSC::JSFunction* tagFunction = global->m_ipcTagAdvancedBuffersFunction.getInitializedOnMainThread(global); - JSC::CallData callData = JSC::getCallData(tagFunction); - - JSC::MarkedArgumentBuffer args; - args.append(JSC::JSValue::decode(message)); - - auto result = JSC::call(global, tagFunction, callData, JSC::jsUndefined(), args); - RETURN_IF_EXCEPTION(scope, {}); - return JSC::JSValue::encode(result); -} - extern "C" [[ZIG_EXPORT(zero_is_throw)]] JSC::EncodedJSValue IPCRestoreAdvancedBuffers(Zig::GlobalObject* global, JSC::EncodedJSValue envelope) { auto& vm = JSC::getVM(global); diff --git a/src/jsc/bindings/NodeV8.cpp b/src/jsc/bindings/NodeV8.cpp index 5f34379064e4..04fa924451ca 100644 --- a/src/jsc/bindings/NodeV8.cpp +++ b/src/jsc/bindings/NodeV8.cpp @@ -5,7 +5,6 @@ #include "ErrorCode.h" #include "BunClientData.h" #include "NodeV8.h" -#include "MessagePort.h" #include "SerializedScriptValue.h" #include "ZigGlobalObject.h" @@ -46,16 +45,15 @@ JSC_DEFINE_HOST_FUNCTION(functionSerializeForNode, (JSGlobalObject * globalObjec auto& vm = globalObject->vm(); auto scope = DECLARE_THROW_SCOPE(vm); auto* domGlobal = uncheckedDowncast(globalObject); - Vector> transfers; - Vector> ports; - auto result = WebCore::SerializedScriptValue::create(*domGlobal, callFrame->argument(0), WTF::move(transfers), ports, WebCore::SerializationForStorage::Yes); + bool hasBufferEnvelope = false; + auto result = WebCore::SerializedScriptValue::createForNode(*domGlobal, callFrame->argument(0), WebCore::SerializationForStorage::Yes, WebCore::SerializationForCrossProcessTransfer::No, hasBufferEnvelope); EXCEPTION_ASSERT(result.hasException() == !!scope.exception()); if (result.hasException()) { WebCore::propagateException(*domGlobal, scope, result.releaseException()); RELEASE_AND_RETURN(scope, {}); } auto serialized = result.releaseReturnValue(); - auto prefix = callFrame->argument(1).asBoolean() ? std::span(bufferEnvelopeMagic) : std::span(); + auto prefix = hasBufferEnvelope ? std::span(bufferEnvelopeMagic) : std::span(); auto buffer = serialized->toArrayBuffer(ArrayBufferSharingMode::Default, prefix); size_t length = buffer->byteLength(); RELEASE_AND_RETURN(scope, JSValue::encode(JSUint8Array::create(globalObject, domGlobal->JSBufferSubclassStructure(), WTF::move(buffer), 0, length))); @@ -204,7 +202,7 @@ JSC::JSObject* createNodeV8Binding(JSC::JSGlobalObject* globalObject) RETURN_IF_EXCEPTION(scope, nullptr); } object->putDirect(vm, Identifier::fromString(vm, "bufferEnvelopeMagic"_s), magic); - object->putDirectNativeFunction(vm, globalObject, Identifier::fromString(vm, "serialize"_s), 2, functionSerializeForNode, ImplementationVisibility::Public, NoIntrinsic, 0); + object->putDirectNativeFunction(vm, globalObject, Identifier::fromString(vm, "serialize"_s), 1, functionSerializeForNode, ImplementationVisibility::Public, NoIntrinsic, 0); object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "queryObjects"_s), 1, functionQueryObjects, ImplementationVisibility::Public, JSC::NoIntrinsic, 0); object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "getHeapUsage"_s), 0, functionGetHeapUsage, ImplementationVisibility::Public, JSC::NoIntrinsic, 0); object->putDirectNativeFunction(vm, globalObject, JSC::Identifier::fromString(vm, "isStringOneByteRepresentation"_s), 1, functionIsStringOneByteRepresentation, ImplementationVisibility::Public, JSC::NoIntrinsic, 0); diff --git a/src/jsc/bindings/Serialization.cpp b/src/jsc/bindings/Serialization.cpp index 1919a91d2a35..57a97f733e20 100644 --- a/src/jsc/bindings/Serialization.cpp +++ b/src/jsc/bindings/Serialization.cpp @@ -13,12 +13,14 @@ struct SerializedValueSlice { const uint8_t* bytes; size_t size; WebCore::SerializedScriptValue* value; // NOLINT + bool hasBufferEnvelope; }; enum class SerializedFlags : uint8_t { None = 0, ForCrossProcessTransfer = 1 << 0, ForStorage = 1 << 1, + PreserveNodeBuffers = 1 << 2, }; /// Returns a "slice" that also contains a pointer to the SerializedScriptValue. Must be freed by the caller @@ -33,7 +35,11 @@ extern "C" SerializedValueSlice Bun__serializeJSValue(JSGlobalObject* globalObje auto forStorage = (static_cast(flags) & static_cast(SerializedFlags::ForStorage)) ? SerializationForStorage::Yes : SerializationForStorage::No; auto context = SerializationContext::Default; auto forTransferEnum = (static_cast(flags) & static_cast(SerializedFlags::ForCrossProcessTransfer)) ? SerializationForCrossProcessTransfer::Yes : SerializationForCrossProcessTransfer::No; - ExceptionOr> serialized = SerializedScriptValue::create(*globalObject, value, WTF::move(transferList), dummyPorts, forStorage, context, forTransferEnum); + bool hasBufferEnvelope = false; + bool preserveNodeBuffers = static_cast(flags) & static_cast(SerializedFlags::PreserveNodeBuffers); + ExceptionOr> serialized = preserveNodeBuffers + ? SerializedScriptValue::createForNode(*globalObject, value, forStorage, forTransferEnum, hasBufferEnvelope) + : SerializedScriptValue::create(*globalObject, value, WTF::move(transferList), dummyPorts, forStorage, context, forTransferEnum); EXCEPTION_ASSERT(!!scope.exception() == serialized.hasException()); if (serialized.hasException()) { @@ -49,6 +55,7 @@ extern "C" SerializedValueSlice Bun__serializeJSValue(JSGlobalObject* globalObje bytes.begin(), bytes.size(), &serializedValue.leakRef(), + hasBufferEnvelope, }; } diff --git a/src/jsc/bindings/ZigGlobalObject.cpp b/src/jsc/bindings/ZigGlobalObject.cpp index 6fc60be87a24..ca1c0080c3cc 100644 --- a/src/jsc/bindings/ZigGlobalObject.cpp +++ b/src/jsc/bindings/ZigGlobalObject.cpp @@ -2668,9 +2668,6 @@ void GlobalObject::finishCreation(VM& vm) { OBJECT_OFFSETOF(GlobalObject, m_ipcSerializeFunction), [](const LazyProperty::Initializer& init) { init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcSerializeCodeGenerator(init.vm), init.owner)); } }, - { OBJECT_OFFSETOF(GlobalObject, m_ipcTagAdvancedBuffersFunction), [](const LazyProperty::Initializer& init) { - init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcTagAdvancedBuffersCodeGenerator(init.vm), init.owner)); - } }, { OBJECT_OFFSETOF(GlobalObject, m_ipcRestoreAdvancedBuffersFunction), [](const LazyProperty::Initializer& init) { init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcRestoreAdvancedBuffersCodeGenerator(init.vm), init.owner)); } }, diff --git a/src/jsc/bindings/ZigGlobalObject.h b/src/jsc/bindings/ZigGlobalObject.h index 8c47ca0d16c0..26059148eb6d 100644 --- a/src/jsc/bindings/ZigGlobalObject.h +++ b/src/jsc/bindings/ZigGlobalObject.h @@ -702,7 +702,6 @@ class GlobalObject : public Bun::GlobalScope { V(public, LazyPropertyOfGlobalObject, m_nodeVMUseMainContextDefaultLoader) \ V(public, LazyPropertyOfGlobalObject, m_ipcSerializeFunction) \ V(public, LazyPropertyOfGlobalObject, m_ipcParseHandleFunction) \ - V(public, LazyPropertyOfGlobalObject, m_ipcTagAdvancedBuffersFunction) \ V(public, LazyPropertyOfGlobalObject, m_ipcRestoreAdvancedBuffersFunction) #define DECLARE_GLOBALOBJECT_GC_MEMBER(visibility, T, name) \ diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 47145f8e0574..503098793021 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -730,15 +730,20 @@ class CloneSerializer : public CloneBase { #endif Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, Vector& serializedBlockListRefs, - SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer) + SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope) { CloneSerializer serializer(lexicalGlobalObject, messagePorts, arrayBuffers, #if ENABLE(WEBASSEMBLY) wasmModules, wasmMemoryHandles, #endif - out, context, sharedBuffers, forStorage, forTransfer); + out, context, sharedBuffers, forStorage, forTransfer, hasBufferEnvelope != nullptr); auto code = serializer.serialize(value); + if (code == SerializationReturnCode::SuccessfullyCompleted && hasBufferEnvelope) { + *hasBufferEnvelope = serializer.finishBufferEnvelope(); + if (serializer.m_failed) + code = SerializationReturnCode::UnspecifiedError; + } serializedBlockListRefs = WTF::move(serializer.m_serializedBlockListRefs); return code; } @@ -751,7 +756,7 @@ class CloneSerializer : public CloneBase { WasmModuleArray& wasmModules, WasmMemoryHandleArray& wasmMemoryHandles, #endif - Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer) + Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool preserveNodeBuffers = false) : CloneBase(lexicalGlobalObject) , m_buffer(out) , m_emptyIdentifier(Identifier::fromString(lexicalGlobalObject->vm(), emptyString())) @@ -763,6 +768,7 @@ class CloneSerializer : public CloneBase { #endif , m_forStorage(forStorage) , m_forTransfer(forTransfer) + , m_preserveNodeBuffers(preserveNodeBuffers) { write(CurrentVersion); fillTransferMap(messagePorts, m_transferredMessagePorts); @@ -1026,6 +1032,23 @@ class CloneSerializer : public CloneBase { return JSC::JSArrayBuffer::create(vm, globalObject->arrayBufferStructure(arrayBuffer.sharingMode()), &arrayBuffer); } + bool isNodeBuffer(JSObject* object) + { + if (!object->inherits()) + return false; + auto& vm = m_lexicalGlobalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + auto prototype = defaultGlobalObject(m_lexicalGlobalObject)->JSBufferSubclassStructure()->storedPrototype(); + for (JSValue current = object->getPrototype(m_lexicalGlobalObject); current.isObject();) { + RETURN_IF_EXCEPTION(scope, false); + if (current == prototype) + return true; + current = asObject(current)->getPrototype(m_lexicalGlobalObject); + } + RETURN_IF_EXCEPTION(scope, false); + return false; + } + bool dumpArrayBufferView(JSObject* obj, SerializationReturnCode& code) { VM& vm = m_lexicalGlobalObject->vm(); @@ -1361,6 +1384,12 @@ class CloneSerializer : public CloneBase { bool success = dumpArrayBufferView(obj, code); RETURN_IF_EXCEPTION(scope, false); recordObject(obj); + if (m_preserveNodeBuffers && success && code == SerializationReturnCode::SuccessfullyCompleted) { + bool isBuffer = isNodeBuffer(obj); + RETURN_IF_EXCEPTION(scope, false); + if (isBuffer) + m_nodeBufferIndices.append(m_objectPool.find(obj)->value); + } return success; } #if ENABLE(WEB_CRYPTO) @@ -1653,9 +1682,67 @@ class CloneSerializer : public CloneBase { void writeObjectIndex(unsigned i) { + if (m_preserveNodeBuffers) + m_objectReferences.append({ m_buffer.size(), i, static_cast(m_objectPool.size()) }); writeConstantPoolIndex(m_objectPool, i); } + // Buffer identities are known only after the real walk: Error metadata and + // ordinary getters can replace values that have not been visited yet. Wrap + // those already-written bytes, never visit application objects a second time. + // The leading envelope array adds one object-pool slot, so reference indexes + // (and their widths at 255/65535 slots) must shift. Strings do not shift. + bool finishBufferEnvelope() + { + if (m_nodeBufferIndices.isEmpty()) + return false; // Preserve byte-for-byte bare framing, including Errors. + if (m_objectPool.size() > std::numeric_limits::max() - 2) { + fail(); + return false; + } + Vector valueBytes = WTF::move(m_buffer); + m_buffer = {}; + write(CurrentVersion); + write(ArrayTag); + write(static_cast(2)); + write(static_cast(0)); + size_t cursor = sizeof(CurrentVersion); + auto indexWidth = [](uint32_t count) -> size_t { + return count <= 0xFF ? 1 : count <= 0xFFFF ? 2 + : 4; + }; + auto writeIndex = [&](uint32_t count, uint32_t index) { + if (count <= 0xFF) + write(static_cast(index)); + else if (count <= 0xFFFF) + write(static_cast(index)); + else + write(index); + }; + for (const auto& reference : m_objectReferences) { + write(valueBytes.data() + cursor, reference.offset - cursor); + if (m_failed) + return false; + writeIndex(reference.poolSize + 1, reference.index + 1); + cursor = reference.offset + indexWidth(reference.poolSize); + } + write(valueBytes.data() + cursor, valueBytes.size() - cursor); + if (m_failed) + return false; + write(static_cast(1)); + write(ArrayTag); + write(static_cast(m_nodeBufferIndices.size())); + uint32_t index = 0; + for (auto bufferIndex : m_nodeBufferIndices) { + write(index++); + write(ObjectReferenceTag); + writeIndex(m_objectPool.size() + 2, bufferIndex + 1); + } + write(TerminatorTag); + write(TerminatorTag); + return true; + } + template void writeConstantPoolIndex(const T& constantPool, unsigned i) { ASSERT(i < constantPool.size()); @@ -2004,6 +2091,13 @@ class CloneSerializer : public CloneBase { } } #endif + struct ObjectReference { + size_t offset; + uint32_t index; + uint32_t poolSize; + }; + Vector m_objectReferences; + Vector m_nodeBufferIndices; ObjectPool m_objectPool; ObjectPool m_transferredMessagePorts; ObjectPool m_transferredArrayBuffers; @@ -2019,6 +2113,7 @@ class CloneSerializer : public CloneBase { #endif SerializationForStorage m_forStorage; SerializationForCrossProcessTransfer m_forTransfer; + bool m_preserveNodeBuffers; }; SYSV_ABI void SerializedScriptValue::writeBytesForBun(CloneSerializer* ctx, const uint8_t* data, uint32_t size) @@ -4559,6 +4654,13 @@ static bool isObjectFastPathCandidate(Structure* structure) return true; } +ExceptionOr> SerializedScriptValue::createForNode(JSGlobalObject& globalObject, JSValue value, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool& hasBufferEnvelope) +{ + Vector> ports; + hasBufferEnvelope = false; + return create(globalObject, value, {}, ports, forStorage, SerializationErrorMode::Throwing, SerializationContext::Default, forTransfer, &hasBufferEnvelope); +} + RefPtr SerializedScriptValue::create(JSC::JSGlobalObject& globalObject, JSC::JSValue value, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext serializationContext, SerializationForCrossProcessTransfer forTransfer) { Vector> dummyPorts; @@ -4573,13 +4675,13 @@ ExceptionOr> SerializedScriptValue::create(JSGlobalOb return create(globalObject, value, WTF::move(transferList), messagePorts, forStorage, SerializationErrorMode::Throwing, serializationContext, forTransfer); } -ExceptionOr> SerializedScriptValue::create(JSGlobalObject& lexicalGlobalObject, JSValue value, Vector>&& transferList, Vector>& messagePorts, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext context, SerializationForCrossProcessTransfer forTransfer) +ExceptionOr> SerializedScriptValue::create(JSGlobalObject& lexicalGlobalObject, JSValue value, Vector>&& transferList, Vector>& messagePorts, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext context, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope) { VM& vm = lexicalGlobalObject.vm(); auto scope = DECLARE_THROW_SCOPE(vm); // Fast path optimization: for postMessage/structuredClone with pure strings and no transfers - const bool canUseFastPath = (context == SerializationContext::WorkerPostMessage || context == SerializationContext::WindowPostMessage || context == SerializationContext::Default) + const bool canUseFastPath = !hasBufferEnvelope && (context == SerializationContext::WorkerPostMessage || context == SerializationContext::WindowPostMessage || context == SerializationContext::Default) && forStorage == SerializationForStorage::No && forTransfer == SerializationForCrossProcessTransfer::No && transferList.isEmpty() @@ -4861,7 +4963,7 @@ ExceptionOr> SerializedScriptValue::create(JSGlobalOb wasmModules, wasmMemoryHandles, #endif - buffer, context, *sharedBuffers, serializedBlockListRefs, forStorage, forTransfer); + buffer, context, *sharedBuffers, serializedBlockListRefs, forStorage, forTransfer, hasBufferEnvelope); auto releaseSerializedBlockListRefs = [&] { for (auto* ptr : serializedBlockListRefs) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.h b/src/jsc/bindings/webcore/SerializedScriptValue.h index 112a541cc0d5..78c1925556b7 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.h +++ b/src/jsc/bindings/webcore/SerializedScriptValue.h @@ -119,6 +119,9 @@ class SerializedScriptValue : public ThreadSafeRefCounted static RefPtr convert(JSC::JSGlobalObject& globalObject, JSC::JSValue value) { return create(globalObject, value, SerializationForStorage::Yes); } + // Node transports retain Buffer identities from the actual serialization walk. + WEBCORE_EXPORT static ExceptionOr> createForNode(JSC::JSGlobalObject&, JSC::JSValue, SerializationForStorage, SerializationForCrossProcessTransfer, bool& hasBufferEnvelope); + // Fast path for postMessage with pure strings static Ref createStringFastPath(const String& string); @@ -154,7 +157,7 @@ class SerializedScriptValue : public ThreadSafeRefCounted WEBCORE_EXPORT ~SerializedScriptValue(); private: - static ExceptionOr> create(JSC::JSGlobalObject&, JSC::JSValue, Vector>&& transfer, Vector>&, SerializationForStorage, SerializationErrorMode, SerializationContext, SerializationForCrossProcessTransfer); + static ExceptionOr> create(JSC::JSGlobalObject&, JSC::JSValue, Vector>&& transfer, Vector>&, SerializationForStorage, SerializationErrorMode, SerializationContext, SerializationForCrossProcessTransfer, bool* hasBufferEnvelope = nullptr); WEBCORE_EXPORT SerializedScriptValue(Vector&&, std::unique_ptr&& = nullptr); SerializedScriptValue(Vector&&, std::unique_ptr, std::unique_ptr sharedBuffers diff --git a/src/runtime/ipc.rs b/src/runtime/ipc.rs index 50e5c17086b5..790eafae88fe 100644 --- a/src/runtime/ipc.rs +++ b/src/runtime/ipc.rs @@ -389,26 +389,22 @@ mod advanced { ) -> Result { // Internal (control) messages never carry user Buffers, and the // hardcoded ack/nack packets depend on their bare wire shape. - let (value, message_type) = match is_internal { - IsInternal::Internal => (value, IPCMessageType::SerializedInternalMessage), - IsInternal::External => { - let tagged = ipc_tag_advanced_buffers(global, value)?; - if tagged.is_null() { - (value, IPCMessageType::SerializedMessage) - } else { - (tagged, IPCMessageType::SerializedMessageWithBuffers) - } - } - }; - let serialized = value.serialize( global, SerializedFlags { - // IPC sends across process. + // IPC sends across process. Internal ACK/NACK messages stay bare. for_cross_process_transfer: true, for_storage: false, + preserve_node_buffers: matches!(is_internal, IsInternal::External), }, )?; + let message_type = match is_internal { + IsInternal::Internal => IPCMessageType::SerializedInternalMessage, + IsInternal::External if serialized.has_buffer_envelope() => { + IPCMessageType::SerializedMessageWithBuffers + } + IsInternal::External => IPCMessageType::SerializedMessage, + }; // `serialized` Drops at scope exit (defer serialized.deinit()). let size: u32 = u32::try_from(serialized.data().len()).expect("int cast"); @@ -2628,16 +2624,6 @@ pub(crate) fn ipc_serialize( bun_jsc::cpp::IPCSerialize(global_object, message, handle, options) } -#[track_caller] -pub(crate) fn ipc_tag_advanced_buffers( - global_object: &JSGlobalObject, - message: JSValue, -) -> JsResult { - // `[[ZIG_EXPORT(zero_is_throw)]]`; returns null when the message holds no - // Buffers, else the `[message, buffers]` envelope (see Ipc.ts). - bun_jsc::cpp::IPCTagAdvancedBuffers(global_object, message) -} - #[track_caller] pub(crate) fn ipc_restore_advanced_buffers( global_object: &JSGlobalObject, diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index 99c58fc48bf1..13b56f857142 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -73,13 +73,42 @@ describe("v8 serialize/deserialize Buffer identity", () => { }; Object.defineProperty(source, field, field === "message" ? { value: { toString: effect } } : { get: effect }); const out = v8.deserialize(v8.serialize(source)); - expect(calls).toBeGreaterThan(0); + expect(calls).toBe(1); expect(Buffer.isBuffer(out.cause)).toBe(true); expect(out.cause.toString()).toBe("rigi"); expect(out.cause.length).toBe(4); }); - test("Error.cause Buffer scanning terminates on Error cycles without invoking accessors", () => { + for (const initial of ["buffer", "absent"]) { + test.each(["name", "message", "stack"])( + `later Error with ${initial} cause retains a Buffer introduced by %s effects`, + field => { + const later = initial === "buffer" ? new Error("later", { cause: Buffer.from("old") }) : new Error("later"); + const first = new Error("first"); + first.stack = ""; + let calls = 0; + const effect = () => { + calls++; + const buffer = Buffer.from("!new!").subarray(1, 4); + later.cause = new Map([[buffer, [buffer, buffer]]]); + return field === "name" ? "Error" : "metadata"; + }; + Object.defineProperty(first, field, field === "message" ? { value: { toString: effect } } : { get: effect }); + const out = v8.deserialize(v8.serialize([first, later, later])); + expect(calls).toBe(1); + expect(out[1]).toBe(out[2]); + expect(out[1].cause).toBeInstanceOf(Map); + const [[key, values]] = [...out[1].cause]; + expect(Array.from(key)).toEqual([110, 101, 119]); + expect(Buffer.isBuffer(key)).toBe(true); + expect(key.toString()).toBe("new"); + expect(values[0]).toBe(key); + expect(values[1]).toBe(key); + }, + ); + } + + test("Error.cause Buffer serialization terminates on Error cycles without invoking accessors", () => { const error = new Error("outer"); const nested = new Error("inner", { cause: error }); const accessor = new Error("accessor"); @@ -109,6 +138,95 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(v8.deserialize(bare)).toEqual({ buf: new Uint8Array([7]) }); }); + test.each([249, 250, 251, 252, 65529, 65530, 65531, 65532])("Buffer envelope retains aliases across %i values", count => { + const buffer = Buffer.from("slots"); + const values = Array.from({ length: count }, (_, index) => ({ index })); + const input: any = { values, buffer, again: buffer, first: values[0], last: values.at(-1) }; + input.self = input; + const out = v8.deserialize(v8.serialize(input)); + expect(out.self).toBe(out); + expect(out.first).toBe(out.values[0]); + expect(out.last).toBe(out.values.at(-1)); + expect(out.buffer).toBe(out.again); + expect(Buffer.isBuffer(out.buffer)).toBe(true); + expect(out.buffer.toString()).toBe("slots"); + }); + + test("Buffer-free Error graphs retain byte-for-byte bare framing", () => { + const jsc = require("bun:jsc"); + const error = new Error("bare", { cause: new Map([["cause", { n: 1 }]]) }); + error.stack = "stable"; + const input = [error, error]; + expect(v8.serialize(input)).toEqual(jsc.serialize(input, { binaryType: "nodebuffer" })); + }); + + test("only serialized Buffer identities are tagged, including getter results and subclasses", () => { + const buffer = Buffer.from("visible"); + Object.setPrototypeOf(buffer, Object.create(Buffer.prototype)); + let reads = 0; + const ignored = new Map(); + Object.defineProperty(ignored, "extra", { value: Buffer.from("ignored"), enumerable: true }); + const bytes = v8.serialize({ + get value() { + reads++; + return buffer; + }, + ignored, + }); + const envelope = require("bun:jsc").deserialize(bytes.subarray(5)); + expect(reads).toBe(1); + expect(envelope[1]).toHaveLength(1); + expect(envelope[1][0]).toBe(envelope[0].value); + const out = v8.deserialize(bytes); + expect(Buffer.isBuffer(out.value)).toBe(true); + expect(out.value.toString()).toBe("visible"); + expect(Reflect.ownKeys(out.ignored)).toEqual([]); + }); + + test("advanced IPC tags Buffers introduced by earlier Error metadata", async () => { + const source = [ + 'let calls = 0;', + 'for (const initial of ["buffer", "absent"]) for (const field of ["name", "message", "stack"]) {', + ' const later = initial === "buffer" ? new Error("later", { cause: Buffer.from("old") }) : new Error("later");', + ' const first = new Error("first"); first.stack = "";', + ' const effect = () => { calls++; const b = Buffer.from("new"); later.cause = new Map([[b, b]]); return field === "name" ? "Error" : "metadata"; };', + ' Object.defineProperty(first, field, field === "message" ? { value: { toString: effect } } : { get: effect });', + ' process.send([first, later, later]);', + '}', + 'process.send({ done: true, calls });', + 'process.on("message", () => {});', + ].join("\n"); + const { promise, resolve, reject } = Promise.withResolvers(); + const messages: any[] = []; + let calls; + await using child = Bun.spawn({ + cmd: [bunExe(), "-e", source], + env: bunEnv, + stdout: "ignore", + stderr: "inherit", + serialization: "advanced", + ipc(message) { + if (message?.done) { + calls = message.calls; + resolve(); + } else messages.push(message); + }, + onExit(_child, code, signal) { + reject(new Error("IPC child exited before completion: " + code + "/" + signal)); + }, + }); + await promise; + expect(calls).toBe(6); + expect(messages).toHaveLength(6); + for (const out of messages) { + expect(out[1]).toBe(out[2]); + const [[key, value]] = [...out[1].cause]; + expect(key).toBe(value); + expect(Buffer.isBuffer(key)).toBe(true); + expect(key.toString()).toBe("new"); + } + }); + test("rejects an envelope whose buffer list holds a non-Uint8Array view", () => { // The envelope's buffer list gets Buffer.prototype applied to each entry. // A crafted payload can put any typed array there; only Uint8Arrays may diff --git a/test/js/web/workers/structured-clone.test.ts b/test/js/web/workers/structured-clone.test.ts index 2fef92d8ae25..48d018a5ff83 100644 --- a/test/js/web/workers/structured-clone.test.ts +++ b/test/js/web/workers/structured-clone.test.ts @@ -179,7 +179,6 @@ for (const structuredCloneFn of [structuredClone, jscSerializeRoundtrip, jscSeri { description: "primitive number, 9007199254740994", value: 9007199254740994 }, { description: "primitive number, -9007199254740994", value: -9007199254740994 }, { description: "primitive BigInt, 0n", value: 0n }, - { description: "primitive BigInt, -0n", value: -0n }, { description: "primitive BigInt, -9007199254740994000n", value: -9007199254740994000n }, { description: "primitive BigInt, -9007199254740994000900719925474099400090071992547409940009007199254740994000n", @@ -215,7 +214,6 @@ for (const structuredCloneFn of [structuredClone, jscSerializeRoundtrip, jscSeri 9007199254740994, -9007199254740994, -12n, - -0n, 0n, ]; const cloned = await structuredCloneFn(input); @@ -248,7 +246,6 @@ for (const structuredCloneFn of [structuredClone, jscSerializeRoundtrip, jscSeri "9007199254740994": 9007199254740994, "-9007199254740994": -9007199254740994, "-12n": -12n, - "-0n": -0n, "0n": 0n, }; const cloned = await structuredCloneFn(input); @@ -313,7 +310,7 @@ for (const structuredCloneFn of [structuredClone, jscSerializeRoundtrip, jscSeri expect([...cloned.input.values()]).toEqual([{ payload: 42 }]); expect([...cloned.input.values()][0]).toBe(cloned.entry); expect(cloned.entry).not.toBe(entry); - expect(getterCalls).toBe(0); + if (property === "getter") expect(getterCalls).toBe(0); }); } } @@ -1258,9 +1255,7 @@ describe("X509Certificate records whose DER does not decode are rejected", () => bunExe(), "-e", `import { deserialize } from "bun:jsc"; - import { deserialize as v8Deserialize, serialize as v8Serialize } from "node:v8"; -import { once } from "node:events"; -import { MessageChannel, Worker as NodeWorker } from "node:worker_threads"; + import { deserialize as v8Deserialize } from "node:v8"; for (const [name, fn] of [["bun:jsc", deserialize], ["node:v8", v8Deserialize]]) { try { const key = fn(new Uint8Array(${JSON.stringify([...record(Buffer.alloc(0))])})).publicKey; @@ -1447,7 +1442,7 @@ for (const clone of [structuredClone, jscSerializeRoundtrip, v8SerializeRoundtri }; Object.defineProperty(source, field, field === "message" ? { value: { toString: effect } } : { get: effect }); const result = clone(source); - expect(calls).toBeGreaterThan(0); + expect(calls).toBe(1); expect(result.cause).toEqual({ marker: "original" }); }, ); From 252dfd96816ada0dae7cc0fa29f3340f85560a69 Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:24:02 -0600 Subject: [PATCH 06/11] test: align release acceptance CI selection and formatting Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- test/js/node/v8/v8-serdes-buffer.test.ts | 37 +++++++++++++----------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index 13b56f857142..ddedd799299a 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -138,19 +138,22 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(v8.deserialize(bare)).toEqual({ buf: new Uint8Array([7]) }); }); - test.each([249, 250, 251, 252, 65529, 65530, 65531, 65532])("Buffer envelope retains aliases across %i values", count => { - const buffer = Buffer.from("slots"); - const values = Array.from({ length: count }, (_, index) => ({ index })); - const input: any = { values, buffer, again: buffer, first: values[0], last: values.at(-1) }; - input.self = input; - const out = v8.deserialize(v8.serialize(input)); - expect(out.self).toBe(out); - expect(out.first).toBe(out.values[0]); - expect(out.last).toBe(out.values.at(-1)); - expect(out.buffer).toBe(out.again); - expect(Buffer.isBuffer(out.buffer)).toBe(true); - expect(out.buffer.toString()).toBe("slots"); - }); + test.each([249, 250, 251, 252, 65529, 65530, 65531, 65532])( + "Buffer envelope retains aliases across %i values", + count => { + const buffer = Buffer.from("slots"); + const values = Array.from({ length: count }, (_, index) => ({ index })); + const input: any = { values, buffer, again: buffer, first: values[0], last: values.at(-1) }; + input.self = input; + const out = v8.deserialize(v8.serialize(input)); + expect(out.self).toBe(out); + expect(out.first).toBe(out.values[0]); + expect(out.last).toBe(out.values.at(-1)); + expect(out.buffer).toBe(out.again); + expect(Buffer.isBuffer(out.buffer)).toBe(true); + expect(out.buffer.toString()).toBe("slots"); + }, + ); test("Buffer-free Error graphs retain byte-for-byte bare framing", () => { const jsc = require("bun:jsc"); @@ -185,15 +188,15 @@ describe("v8 serialize/deserialize Buffer identity", () => { test("advanced IPC tags Buffers introduced by earlier Error metadata", async () => { const source = [ - 'let calls = 0;', + "let calls = 0;", 'for (const initial of ["buffer", "absent"]) for (const field of ["name", "message", "stack"]) {', ' const later = initial === "buffer" ? new Error("later", { cause: Buffer.from("old") }) : new Error("later");', ' const first = new Error("first"); first.stack = "";', ' const effect = () => { calls++; const b = Buffer.from("new"); later.cause = new Map([[b, b]]); return field === "name" ? "Error" : "metadata"; };', ' Object.defineProperty(first, field, field === "message" ? { value: { toString: effect } } : { get: effect });', - ' process.send([first, later, later]);', - '}', - 'process.send({ done: true, calls });', + " process.send([first, later, later]);", + "}", + "process.send({ done: true, calls });", 'process.on("message", () => {});', ].join("\n"); const { promise, resolve, reject } = Promise.withResolvers(); From fac22fb85901d8b189c4b858106425e2cbb74bfa Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:46:18 -0600 Subject: [PATCH 07/11] fix: use public vector span for serialized bytes Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- src/jsc/bindings/webcore/SerializedScriptValue.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 503098793021..e1440899dbff 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -1720,13 +1720,13 @@ class CloneSerializer : public CloneBase { write(index); }; for (const auto& reference : m_objectReferences) { - write(valueBytes.data() + cursor, reference.offset - cursor); + write(valueBytes.span().data() + cursor, reference.offset - cursor); if (m_failed) return false; writeIndex(reference.poolSize + 1, reference.index + 1); cursor = reference.offset + indexWidth(reference.poolSize); } - write(valueBytes.data() + cursor, valueBytes.size() - cursor); + write(valueBytes.span().data() + cursor, valueBytes.size() - cursor); if (m_failed) return false; write(static_cast(1)); From e376e27bfb9a4415233a96910ebf2dcaf322db36 Mon Sep 17 00:00:00 2001 From: "Jason (Json)" <263060202+fuller-stack-dev@users.noreply.github.com> Date: Sun, 18 Oct 2026 22:51:29 -0600 Subject: [PATCH 08/11] fix: classify transport Buffers without prototype callbacks Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- .../webcore/SerializedScriptValue.cpp | 11 +-- test/js/node/v8/v8-serdes-buffer.test.ts | 92 +++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index e1440899dbff..00cf3302479e 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -1036,16 +1036,15 @@ class CloneSerializer : public CloneBase { { if (!object->inherits()) return false; - auto& vm = m_lexicalGlobalObject->vm(); - auto scope = DECLARE_THROW_SCOPE(vm); auto prototype = defaultGlobalObject(m_lexicalGlobalObject)->JSBufferSubclassStructure()->storedPrototype(); - for (JSValue current = object->getPrototype(m_lexicalGlobalObject); current.isObject();) { - RETURN_IF_EXCEPTION(scope, false); + // Inspect engine-stored prototypes, as queryObjects does. [[GetPrototypeOf]] + // would execute Proxy traps while serializing an otherwise ordinary view. + // Direct traversal retains Buffer subclasses and intermediate prototypes + // without following an exotic object's user-defined prototype chain. + for (JSValue current = object->getPrototypeDirect(); current.isObject(); current = asObject(current)->getPrototypeDirect()) { if (current == prototype) return true; - current = asObject(current)->getPrototype(m_lexicalGlobalObject); } - RETURN_IF_EXCEPTION(scope, false); return false; } diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index ddedd799299a..27beb05cdc86 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -186,6 +186,98 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(Reflect.ownKeys(out.ignored)).toEqual([]); }); + for (const transport of ["v8", "structuredClone", "advanced IPC"]) { + test.each(["benign", "throwing", "cyclic", "Buffer", "subclass", "intermediate"])( + `${transport} classifies %s typed arrays without prototype callbacks`, + async kind => { + // A subprocess bounds synchronous native hangs and crashes; a test-runner + // timeout alone cannot interrupt a stuck serializer on its own thread. + const source = ` + const { serialize, deserialize } = require("node:v8"); + const kind = ${JSON.stringify(kind)}; + const transport = ${JSON.stringify(transport)}; + let traps = 0, reads = 0; + let value; + if (["Buffer", "subclass", "intermediate"].includes(kind)) { + value = Buffer.from([7, 8]); + if (kind === "subclass") { + class Derived extends Buffer {} + Object.setPrototypeOf(value, Derived.prototype); + } else if (kind === "intermediate") { + Object.setPrototypeOf(value, Object.create(Buffer.prototype)); + } + } else { + value = new Uint8Array([7, 8]); + const prototype = new Proxy(Uint8Array.prototype, { + getPrototypeOf(target) { + traps++; + if (kind === "throwing") throw new Error("must not inspect prototype"); + if (kind === "cyclic") return prototype; + return Reflect.getPrototypeOf(target); + } + }); + Object.setPrototypeOf(value, prototype); + } + const input = { get value() { reads++; return value; }, alias: value }; + input.self = input; + if (transport === "advanced IPC") { + process.on("message", () => process.disconnect()); + process.send(input); + console.log(JSON.stringify({ traps, reads })); + } else { + const out = transport === "v8" ? deserialize(serialize(input)) : structuredClone(input); + console.log(JSON.stringify({ + traps, reads, bytes: Array.from(out.value), buffer: Buffer.isBuffer(out.value), + alias: out.value === out.alias, cycle: out.self === out + })); + } + `; + let received: any; + const proc = Bun.spawn({ + cmd: [bunExe(), "-e", source], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + ...(transport === "advanced IPC" + ? { + serialization: "advanced" as const, + ipc(message: any, child: Bun.Subprocess) { + received = message; + child.send("received"); + }, + } + : {}), + }); + let timedOut = false; + const timeout = setTimeout(() => { + timedOut = true; + proc.kill(9); + }, 10_000); + try { + const [stdout, stderr, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(timedOut).toBe(false); + expect(code).toBe(0); + expect(proc.signalCode).toBeNull(); + expect(stderr).toBe(""); + const result = JSON.parse(stdout); + expect(result.traps).toBe(0); + expect(result.reads).toBe(1); + const out = transport === "advanced IPC" ? received : result; + const isBuffer = ["Buffer", "subclass", "intermediate"].includes(kind) && transport !== "structuredClone"; + expect(transport === "advanced IPC" ? Array.from(out.value) : out.bytes).toEqual([7, 8]); + expect(transport === "advanced IPC" ? Buffer.isBuffer(out.value) : out.buffer).toBe(isBuffer); + expect(transport === "advanced IPC" ? out.value === out.alias : out.alias).toBe(true); + expect(transport === "advanced IPC" ? out.self === out : out.cycle).toBe(true); + } finally { + clearTimeout(timeout); + if (proc.exitCode === null) proc.kill(9); + await proc.exited; + } + }, + 20_000, + ); + } + test("advanced IPC tags Buffers introduced by earlier Error metadata", async () => { const source = [ "let calls = 0;", From 7a50bb3fe8983693627d5c7e639a48c1ffaed465 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 19 Oct 2026 22:12:17 -0700 Subject: [PATCH 09/11] fix: negotiate Error cause serialization with legacy IPC peers Retain exact v14 bytes for cause-free values and upgrade to v15 only when a cause edge is serialized. Advertise serializer support in the existing IPC hello and omit causes until the peer advertises support, preserving mixed-version process communication. Keep legacy Buffer ordering for cause-free graphs, collect cause Buffers from the native walk, and cover Node parity, graph identity, legacy byte fixtures, workers, and negotiated IPC. Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- CHANGELOG.md | 2 + docs/guides/process/ipc.mdx | 2 + docs/runtime/nodejs-compat.mdx | 2 +- src/js/builtins/Ipc.ts | 15 +- src/js/internal/serialization_buffers.ts | 51 ++++++- src/jsc/JSValue.rs | 4 + src/jsc/bindings/Serialization.cpp | 5 +- src/jsc/bindings/ZigGlobalObject.cpp | 3 + src/jsc/bindings/ZigGlobalObject.h | 1 + .../webcore/SerializedScriptValue.cpp | 127 ++++++++++++---- .../bindings/webcore/SerializedScriptValue.h | 7 +- src/runtime/ipc.rs | 46 +++++- .../child_process/child_process_ipc.test.js | 21 +++ .../fixtures/error-cause-ipc.cjs | 51 +++++++ .../node/v8/legacy-clone-golden.fixture.json | 12 ++ .../node/v8/legacy-clone-graphs.fixture.mjs | 29 ++++ test/js/node/v8/v8-serdes-buffer.test.ts | 136 +++--------------- .../workers/error-cause-node-parity.test.mjs | 94 ++++++++++++ 18 files changed, 445 insertions(+), 163 deletions(-) create mode 100644 test/js/node/child_process/fixtures/error-cause-ipc.cjs create mode 100644 test/js/node/v8/legacy-clone-golden.fixture.json create mode 100644 test/js/node/v8/legacy-clone-graphs.fixture.mjs create mode 100644 test/js/web/workers/error-cause-node-parity.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc1e19b4379..47d697980bef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Preserve Error cause graphs in structured cloning and worker messages, while retaining v14 bytes for cause-free values and negotiating cause support for advanced IPC with older Bun processes; thanks fuller-stack-dev. + - Share identical `node:vm` script and compiled-function source buffers and source-map URI strings through a bounded per-VM weak index, including after compilation-cache eviction. - Honor `BUN_FEATURE_FLAG_DISABLE_SOURCE_MAPS` on transpiler-cache hits and prevent startup crashes from cached modules without maps. Adapts empty-map validation from [oven-sh/bun#42412](https://github.com/oven-sh/bun/pull/42412); thanks @robobun. diff --git a/docs/guides/process/ipc.mdx b/docs/guides/process/ipc.mdx index 96e2ec236f0c..120585f40d73 100644 --- a/docs/guides/process/ipc.mdx +++ b/docs/guides/process/ipc.mdx @@ -56,6 +56,8 @@ process.on("message", message => { By default, Bun serializes messages with the JSC `serialize` API. This API supports everything [`structuredClone` supports](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm), including strings, typed arrays, and objects. The API does not support transferring ownership of objects. +Advanced IPC preserves `Error.cause` once the peer has advertised support on that channel. Messages sent before that advertisement, or to an older Bun process, omit causes and retain the v14 format used by older builds. Cause-free messages always retain v14 bytes. This allows an already-running process to communicate with a child started from an updated Bun executable. In-process `structuredClone`, Worker, MessagePort, and BroadcastChannel messages preserve causes without IPC negotiation. + ```ts child.ts icon="/icons/typescript.svg" // send a string process.send("Hello from child as string"); diff --git a/docs/runtime/nodejs-compat.mdx b/docs/runtime/nodejs-compat.mdx index ed7196aeace2..c8f2b1deca3b 100644 --- a/docs/runtime/nodejs-compat.mdx +++ b/docs/runtime/nodejs-compat.mdx @@ -315,7 +315,7 @@ Destroying an HTTP/2 session releases its TLS transport without waiting for the Map and Set serialization copies entries and ignores custom own properties, like `structuredClone`. Existing Bun records with own-property sections remain readable. -🟡 `writeHeapSnapshot`, `getHeapSnapshot`, `getHeapStatistics`, `getHeapSpaceStatistics`, `queryObjects`, `GCProfiler` and `startupSnapshot` are implemented. `queryObjects()` collects garbage and returns a count or shallow summaries of objects matching the constructor's prototype chain in the current context. The heap statistics describe JavaScriptCore's single heap, and `setFlagsFromString` ignores the flags it is given. `serialize` and `deserialize` use JavaScriptCore's wire format instead of V8's. Missing `startCpuProfile`, `startHeapProfile`, `Serializer`/`Deserializer`, `takeCoverage`/`stopCoverage` and `promiseHooks`. For profiling, use [`bun:jsc`](/project/benchmarking#javascript-heap-stats) instead. +🟡 `writeHeapSnapshot`, `getHeapSnapshot`, `getHeapStatistics`, `getHeapSpaceStatistics`, `queryObjects`, `GCProfiler` and `startupSnapshot` are implemented. `queryObjects()` collects garbage and returns a count or shallow summaries of objects matching the constructor's prototype chain in the current context. The heap statistics describe JavaScriptCore's single heap, and `setFlagsFromString` ignores the flags it is given. `serialize` and `deserialize` use JavaScriptCore's wire format instead of V8's. Cause-free values retain the v14 format; Error cause graphs use v15, which older Bun builds cannot deserialize. These bytes are not interchangeable with Node.js serialization. Missing `startCpuProfile`, `startHeapProfile`, `Serializer`/`Deserializer`, `takeCoverage`/`stopCoverage` and `promiseHooks`. For profiling, use [`bun:jsc`](/project/benchmarking#javascript-heap-stats) instead. ### [`node:vm`](https://nodejs.org/api/vm.html) diff --git a/src/js/builtins/Ipc.ts b/src/js/builtins/Ipc.ts index 0b32806b6f0c..351fc86e17d1 100644 --- a/src/js/builtins/Ipc.ts +++ b/src/js/builtins/Ipc.ts @@ -101,7 +101,20 @@ export function parseHandle(target, serialized, fd) { } /** - * Restore the Buffer identities collected by native advanced IPC serialization. + * Advanced-mode IPC Buffer tagging; the mechanism lives in + * internal/serialization_buffers (shared with node:v8 serialize/deserialize). + * Returns null when the message holds no Buffers so the caller keeps the bare + * wire format (and its zero-walk ack fast paths). + * + * @param {unknown} message + * @returns {[unknown, unknown[]] | null} + */ +export function tagAdvancedBuffers(message) { + return require("internal/serialization_buffers").tagBuffers(message); +} + +/** + * Receive side of tagAdvancedBuffers. * * @param {unknown} envelope * @returns {unknown} diff --git a/src/js/internal/serialization_buffers.ts b/src/js/internal/serialization_buffers.ts index e38b74f66056..2013e9aa0199 100644 --- a/src/js/internal/serialization_buffers.ts +++ b/src/js/internal/serialization_buffers.ts @@ -1,10 +1,55 @@ -// Node transport envelopes are collected by the native serialization walk. -// The aliased Buffer views are restored only after the envelope is decoded. +// Buffer tagging for JSC-serialized transports (advanced IPC, node:v8). JSC has no host-object hook, +// so we ship a [value, buffers] envelope (identity-aliased) and restore Buffer prototypes on receive. +// Node ref: https://github.com/nodejs/node/blob/main/lib/internal/child_process/serialization.js const { Buffer } = require("node:buffer"); const { isUint8Array } = require("node:util/types"); const BufferPrototype = Buffer.prototype; +const isBuffer = Buffer.isBuffer; +const ObjectKeys = Object.keys; +const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; const ObjectSetPrototypeOf = Object.setPrototypeOf; +const ObjectPrototypeHasOwnProperty = Object.prototype.hasOwnProperty; +const SafeSet = Set; + +/** Returns null when `value` holds no Buffers, else the [value, buffers] envelope. */ +function tagBuffers(value: unknown): [unknown, unknown[]] | null { + let buffers: unknown[] | null = null; + let visited: Set | null = null; + const stack = [value]; + while (stack.length !== 0) { + const current = stack[stack.length - 1]; + stack.length--; + if (current === null || typeof current !== "object") continue; + visited ??= new SafeSet(); + if (visited.$has(current)) continue; + visited.$add(current); + if ($isTypedArrayView(current)) { + if (isBuffer(current)) $arrayPush((buffers ??= []), current); + // Other ArrayBuffer views round-trip with their type already. + continue; + } + if ($isMap(current)) { + current.$forEach((entry, key) => { + $arrayPush(stack, key); + $arrayPush(stack, entry); + }); + continue; + } + if ($isSet(current)) { + current.$forEach(entry => $arrayPush(stack, entry)); + continue; + } + // Objects, arrays and errors: the serializer walks own enumerable + // properties. Only data properties are descended so no getter runs here. + const keys = ObjectKeys(current); + for (let i = 0; i < keys.length; i++) { + const desc = ObjectGetOwnPropertyDescriptor(current, keys[i]); + if (desc && ObjectPrototypeHasOwnProperty.$call(desc, "value")) $arrayPush(stack, desc.value); + } + } + return buffers === null ? null : [value, buffers]; +} /** * Receive side: restore the Buffer prototype on the envelope's aliased views @@ -26,4 +71,4 @@ function restoreBuffers(envelope: unknown): unknown { return (envelope as unknown[])[0]; } -export default { restoreBuffers }; +export default { tagBuffers, restoreBuffers }; diff --git a/src/jsc/JSValue.rs b/src/jsc/JSValue.rs index 26188b55c406..8b421ee630c2 100644 --- a/src/jsc/JSValue.rs +++ b/src/jsc/JSValue.rs @@ -2221,6 +2221,7 @@ pub struct SerializedFlags { pub for_cross_process_transfer: bool, pub for_storage: bool, pub preserve_node_buffers: bool, + pub omit_error_cause: bool, } /// `JSValue.SerializedScriptValue` — owned view over a @@ -2730,6 +2731,9 @@ impl JSValue { if flags.preserve_node_buffers { bits |= 1 << 2; } + if flags.omit_error_cause { + bits |= 1 << 3; + } let ext = host_fn::from_js_host_call_generic(global, || { Bun__serializeJSValue(global, self, bits) })?; diff --git a/src/jsc/bindings/Serialization.cpp b/src/jsc/bindings/Serialization.cpp index 57a97f733e20..41eda970b1f6 100644 --- a/src/jsc/bindings/Serialization.cpp +++ b/src/jsc/bindings/Serialization.cpp @@ -21,6 +21,7 @@ enum class SerializedFlags : uint8_t { ForCrossProcessTransfer = 1 << 0, ForStorage = 1 << 1, PreserveNodeBuffers = 1 << 2, + OmitErrorCause = 1 << 3, }; /// Returns a "slice" that also contains a pointer to the SerializedScriptValue. Must be freed by the caller @@ -33,12 +34,12 @@ extern "C" SerializedValueSlice Bun__serializeJSValue(JSGlobalObject* globalObje Vector> transferList; Vector> dummyPorts; auto forStorage = (static_cast(flags) & static_cast(SerializedFlags::ForStorage)) ? SerializationForStorage::Yes : SerializationForStorage::No; - auto context = SerializationContext::Default; + auto context = (static_cast(flags) & static_cast(SerializedFlags::OmitErrorCause)) ? SerializationContext::LegacyIPC : SerializationContext::Default; auto forTransferEnum = (static_cast(flags) & static_cast(SerializedFlags::ForCrossProcessTransfer)) ? SerializationForCrossProcessTransfer::Yes : SerializationForCrossProcessTransfer::No; bool hasBufferEnvelope = false; bool preserveNodeBuffers = static_cast(flags) & static_cast(SerializedFlags::PreserveNodeBuffers); ExceptionOr> serialized = preserveNodeBuffers - ? SerializedScriptValue::createForNode(*globalObject, value, forStorage, forTransferEnum, hasBufferEnvelope) + ? SerializedScriptValue::createForNode(*globalObject, value, forStorage, forTransferEnum, hasBufferEnvelope, context) : SerializedScriptValue::create(*globalObject, value, WTF::move(transferList), dummyPorts, forStorage, context, forTransferEnum); EXCEPTION_ASSERT(!!scope.exception() == serialized.hasException()); diff --git a/src/jsc/bindings/ZigGlobalObject.cpp b/src/jsc/bindings/ZigGlobalObject.cpp index ca1c0080c3cc..6fc60be87a24 100644 --- a/src/jsc/bindings/ZigGlobalObject.cpp +++ b/src/jsc/bindings/ZigGlobalObject.cpp @@ -2668,6 +2668,9 @@ void GlobalObject::finishCreation(VM& vm) { OBJECT_OFFSETOF(GlobalObject, m_ipcSerializeFunction), [](const LazyProperty::Initializer& init) { init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcSerializeCodeGenerator(init.vm), init.owner)); } }, + { OBJECT_OFFSETOF(GlobalObject, m_ipcTagAdvancedBuffersFunction), [](const LazyProperty::Initializer& init) { + init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcTagAdvancedBuffersCodeGenerator(init.vm), init.owner)); + } }, { OBJECT_OFFSETOF(GlobalObject, m_ipcRestoreAdvancedBuffersFunction), [](const LazyProperty::Initializer& init) { init.set(JSC::JSFunction::create(init.vm, init.owner, WebCore::ipcRestoreAdvancedBuffersCodeGenerator(init.vm), init.owner)); } }, diff --git a/src/jsc/bindings/ZigGlobalObject.h b/src/jsc/bindings/ZigGlobalObject.h index 26059148eb6d..8c47ca0d16c0 100644 --- a/src/jsc/bindings/ZigGlobalObject.h +++ b/src/jsc/bindings/ZigGlobalObject.h @@ -702,6 +702,7 @@ class GlobalObject : public Bun::GlobalScope { V(public, LazyPropertyOfGlobalObject, m_nodeVMUseMainContextDefaultLoader) \ V(public, LazyPropertyOfGlobalObject, m_ipcSerializeFunction) \ V(public, LazyPropertyOfGlobalObject, m_ipcParseHandleFunction) \ + V(public, LazyPropertyOfGlobalObject, m_ipcTagAdvancedBuffersFunction) \ V(public, LazyPropertyOfGlobalObject, m_ipcRestoreAdvancedBuffersFunction) #define DECLARE_GLOBALOBJECT_GC_MEMBER(visibility, T, name) \ diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 00cf3302479e..8190edf6445c 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -420,6 +420,7 @@ const uint8_t cryptoKeyOKPOpNameTagMaximumValue = 1; * Version 15. ErrorInstance records include an own data cause, traversed as part of the graph. */ [[maybe_unused]] static constexpr unsigned CurrentVersion = 15; +static constexpr unsigned DefaultVersion = 14; static constexpr unsigned FirstVersionWithErrorCause = 15; // Deserializers must not pool the version 14 terminal types for older payloads, // whose writers never counted them, or the pool indices stop matching the writer's. @@ -439,7 +440,7 @@ static constexpr unsigned StringDataIs8BitFlag = 0x80000000; * minimum sized unsigned integer type required to represent the maximum index * in the constant pool. * - * SerializedValue :- Value + * SerializedValue :- Value * Value :- Array | Object | Map | Set | Error | Terminal * * Array :- @@ -457,6 +458,7 @@ static constexpr unsigned StringDataIs8BitFlag = 0x80000000; * * Error :- ErrorInstanceTag * Value? + * (hasCause and its value are present only in version 15; version 14 is otherwise emitted.) * * Terminal :- * UndefinedTag @@ -730,20 +732,22 @@ class CloneSerializer : public CloneBase { #endif Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, Vector& serializedBlockListRefs, - SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope) + SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope, JSArray* legacyNodeBuffers) { CloneSerializer serializer(lexicalGlobalObject, messagePorts, arrayBuffers, #if ENABLE(WEBASSEMBLY) wasmModules, wasmMemoryHandles, #endif - out, context, sharedBuffers, forStorage, forTransfer, hasBufferEnvelope != nullptr); + out, context, sharedBuffers, forStorage, forTransfer, hasBufferEnvelope != nullptr, legacyNodeBuffers); auto code = serializer.serialize(value); if (code == SerializationReturnCode::SuccessfullyCompleted && hasBufferEnvelope) { *hasBufferEnvelope = serializer.finishBufferEnvelope(); if (serializer.m_failed) code = SerializationReturnCode::UnspecifiedError; } + if (code == SerializationReturnCode::SuccessfullyCompleted) + serializer.finishErrorCauseFormat(); serializedBlockListRefs = WTF::move(serializer.m_serializedBlockListRefs); return code; } @@ -756,7 +760,7 @@ class CloneSerializer : public CloneBase { WasmModuleArray& wasmModules, WasmMemoryHandleArray& wasmMemoryHandles, #endif - Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool preserveNodeBuffers = false) + Vector& out, SerializationContext context, ArrayBufferContentsArray& sharedBuffers, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool preserveNodeBuffers, JSArray* legacyNodeBuffers) : CloneBase(lexicalGlobalObject) , m_buffer(out) , m_emptyIdentifier(Identifier::fromString(lexicalGlobalObject->vm(), emptyString())) @@ -769,8 +773,11 @@ class CloneSerializer : public CloneBase { , m_forStorage(forStorage) , m_forTransfer(forTransfer) , m_preserveNodeBuffers(preserveNodeBuffers) + , m_legacyNodeBuffers(legacyNodeBuffers) { - write(CurrentVersion); + write(DefaultVersion); + if (legacyNodeBuffers) + m_gcBuffer.appendWithCrashOnOverflow(legacyNodeBuffers); fillTransferMap(messagePorts, m_transferredMessagePorts); fillTransferMap(arrayBuffers, m_transferredArrayBuffers); } @@ -1131,7 +1138,8 @@ class CloneSerializer : public CloneBase { bool hasCause = false; // Snapshot and root before metadata getters/coercions can replace the cause or collect it. // Worker error events retain their separate bounded, best-effort metadata owner. - if (m_context != SerializationContext::WorkerError) { + bool supportsCause = m_context != SerializationContext::WorkerError && m_context != SerializationContext::LegacyIPC; + if (supportsCause) { JSC::PropertyDescriptor descriptor; bool found = errorInstance->getOwnPropertyDescriptor(m_lexicalGlobalObject, vm.propertyNames->cause, descriptor); RETURN_IF_EXCEPTION(scope, JSValue()); @@ -1209,7 +1217,11 @@ class CloneSerializer : public CloneBase { write(column); writeNullableString(sourceURL); writeNullableString(stack); - write(static_cast(hasCause)); + if (supportsCause) { + m_errorCauseOffsets.append(m_buffer.size()); + write(static_cast(hasCause)); + m_hasErrorCause |= hasCause; + } return cause; } @@ -1387,7 +1399,7 @@ class CloneSerializer : public CloneBase { bool isBuffer = isNodeBuffer(obj); RETURN_IF_EXCEPTION(scope, false); if (isBuffer) - m_nodeBufferIndices.append(m_objectPool.find(obj)->value); + m_nodeBuffers.append(obj); } return success; } @@ -1686,22 +1698,70 @@ class CloneSerializer : public CloneBase { writeConstantPoolIndex(m_objectPool, i); } - // Buffer identities are known only after the real walk: Error metadata and - // ordinary getters can replace values that have not been visited yet. Wrap - // those already-written bytes, never visit application objects a second time. - // The leading envelope array adds one object-pool slot, so reference indexes - // (and their widths at 255/65535 slots) must shift. Strings do not shift. + void finishErrorCauseFormat() + { + if (m_hasErrorCause) { + m_buffer[0] = CurrentVersion; + return; + } + size_t removed = 0; + for (auto& reference : m_objectReferences) { + while (removed < m_errorCauseOffsets.size() && m_errorCauseOffsets[removed] < reference.offset) + ++removed; + reference.offset -= removed; + } + size_t readOffset = 0; + size_t writeOffset = 0; + for (auto offset : m_errorCauseOffsets) { + size_t length = offset - readOffset; + memmove(m_buffer.mutableSpan().data() + writeOffset, m_buffer.span().data() + readOffset, length); + writeOffset += length; + readOffset = offset + 1; + } + size_t length = m_buffer.size() - readOffset; + memmove(m_buffer.mutableSpan().data() + writeOffset, m_buffer.span().data() + readOffset, length); + m_buffer.shrink(writeOffset + length); + m_errorCauseOffsets.clear(); + } + + // Retain the v14 pre-scan's Buffer order and framing. Only cause-bearing + // graphs add identities discovered by the native walk. bool finishBufferEnvelope() { - if (m_nodeBufferIndices.isEmpty()) - return false; // Preserve byte-for-byte bare framing, including Errors. + auto& vm = m_lexicalGlobalObject->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + JSArray* buffers = m_legacyNodeBuffers; + if (m_hasErrorCause && !m_nodeBuffers.isEmpty()) { + if (!buffers) { + buffers = constructEmptyArray(m_lexicalGlobalObject, static_cast(nullptr)); + RETURN_IF_EXCEPTION(scope, false); + m_gcBuffer.appendWithCrashOnOverflow(buffers); + } + HashSet seen; + for (unsigned i = 0; i < buffers->length(); ++i) + seen.add(buffers->getDirectIndex(m_lexicalGlobalObject, i).getObject()); + RETURN_IF_EXCEPTION(scope, false); + for (auto* buffer : m_nodeBuffers) { + if (seen.add(buffer).isNewEntry) + buffers->putDirectIndex(m_lexicalGlobalObject, buffers->length(), buffer); + RETURN_IF_EXCEPTION(scope, false); + } + } + if (!buffers) + return false; if (m_objectPool.size() > std::numeric_limits::max() - 2) { fail(); return false; } + write(static_cast(1)); + if (serialize(buffers) != SerializationReturnCode::SuccessfullyCompleted) { + fail(); + return false; + } + finishErrorCauseFormat(); Vector valueBytes = WTF::move(m_buffer); m_buffer = {}; - write(CurrentVersion); + write(m_hasErrorCause ? CurrentVersion : DefaultVersion); write(ArrayTag); write(static_cast(2)); write(static_cast(0)); @@ -1728,17 +1788,8 @@ class CloneSerializer : public CloneBase { write(valueBytes.span().data() + cursor, valueBytes.size() - cursor); if (m_failed) return false; - write(static_cast(1)); - write(ArrayTag); - write(static_cast(m_nodeBufferIndices.size())); - uint32_t index = 0; - for (auto bufferIndex : m_nodeBufferIndices) { - write(index++); - write(ObjectReferenceTag); - writeIndex(m_objectPool.size() + 2, bufferIndex + 1); - } - write(TerminatorTag); write(TerminatorTag); + m_objectReferences.clear(); return true; } @@ -2096,7 +2147,9 @@ class CloneSerializer : public CloneBase { uint32_t poolSize; }; Vector m_objectReferences; - Vector m_nodeBufferIndices; + Vector m_nodeBuffers; + Vector m_errorCauseOffsets; + bool m_hasErrorCause { false }; ObjectPool m_objectPool; ObjectPool m_transferredMessagePorts; ObjectPool m_transferredArrayBuffers; @@ -2113,6 +2166,7 @@ class CloneSerializer : public CloneBase { SerializationForStorage m_forStorage; SerializationForCrossProcessTransfer m_forTransfer; bool m_preserveNodeBuffers; + JSArray* m_legacyNodeBuffers; }; SYSV_ABI void SerializedScriptValue::writeBytesForBun(CloneSerializer* ctx, const uint8_t* data, uint32_t size) @@ -4653,11 +4707,22 @@ static bool isObjectFastPathCandidate(Structure* structure) return true; } -ExceptionOr> SerializedScriptValue::createForNode(JSGlobalObject& globalObject, JSValue value, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool& hasBufferEnvelope) +ExceptionOr> SerializedScriptValue::createForNode(JSGlobalObject& globalObject, JSValue value, SerializationForStorage forStorage, SerializationForCrossProcessTransfer forTransfer, bool& hasBufferEnvelope, SerializationContext context) { + auto& vm = globalObject.vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + auto* global = jsCast(&globalObject); + auto* tagFunction = global->m_ipcTagAdvancedBuffersFunction.getInitializedOnMainThread(global); + MarkedArgumentBuffer args; + args.append(value); + auto tagged = JSC::call(global, tagFunction, JSC::getCallData(tagFunction), jsUndefined(), args); + RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError }); + Strong taggedRoot(vm, tagged.getObject()); + auto* legacyBuffers = tagged.isNull() ? nullptr : jsCast(asArray(tagged)->getDirectIndex(global, 1)); + RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError }); Vector> ports; hasBufferEnvelope = false; - return create(globalObject, value, {}, ports, forStorage, SerializationErrorMode::Throwing, SerializationContext::Default, forTransfer, &hasBufferEnvelope); + RELEASE_AND_RETURN(scope, create(globalObject, value, {}, ports, forStorage, SerializationErrorMode::Throwing, context, forTransfer, &hasBufferEnvelope, legacyBuffers)); } RefPtr SerializedScriptValue::create(JSC::JSGlobalObject& globalObject, JSC::JSValue value, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext serializationContext, SerializationForCrossProcessTransfer forTransfer) @@ -4674,7 +4739,7 @@ ExceptionOr> SerializedScriptValue::create(JSGlobalOb return create(globalObject, value, WTF::move(transferList), messagePorts, forStorage, SerializationErrorMode::Throwing, serializationContext, forTransfer); } -ExceptionOr> SerializedScriptValue::create(JSGlobalObject& lexicalGlobalObject, JSValue value, Vector>&& transferList, Vector>& messagePorts, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext context, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope) +ExceptionOr> SerializedScriptValue::create(JSGlobalObject& lexicalGlobalObject, JSValue value, Vector>&& transferList, Vector>& messagePorts, SerializationForStorage forStorage, SerializationErrorMode throwExceptions, SerializationContext context, SerializationForCrossProcessTransfer forTransfer, bool* hasBufferEnvelope, JSArray* legacyNodeBuffers) { VM& vm = lexicalGlobalObject.vm(); auto scope = DECLARE_THROW_SCOPE(vm); @@ -4962,7 +5027,7 @@ ExceptionOr> SerializedScriptValue::create(JSGlobalOb wasmModules, wasmMemoryHandles, #endif - buffer, context, *sharedBuffers, serializedBlockListRefs, forStorage, forTransfer, hasBufferEnvelope); + buffer, context, *sharedBuffers, serializedBlockListRefs, forStorage, forTransfer, hasBufferEnvelope, legacyNodeBuffers); auto releaseSerializedBlockListRefs = [&] { for (auto* ptr : serializedBlockListRefs) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.h b/src/jsc/bindings/webcore/SerializedScriptValue.h index 78c1925556b7..b57ac26892db 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.h +++ b/src/jsc/bindings/webcore/SerializedScriptValue.h @@ -89,7 +89,8 @@ enum class SerializationErrorMode { NonThrowing, enum class SerializationContext { Default, WorkerPostMessage, WindowPostMessage, - WorkerError }; + WorkerError, + LegacyIPC }; enum class SerializationForStorage : bool { No, Yes }; enum class SerializationForCrossProcessTransfer : bool { No, @@ -120,7 +121,7 @@ class SerializedScriptValue : public ThreadSafeRefCounted static RefPtr convert(JSC::JSGlobalObject& globalObject, JSC::JSValue value) { return create(globalObject, value, SerializationForStorage::Yes); } // Node transports retain Buffer identities from the actual serialization walk. - WEBCORE_EXPORT static ExceptionOr> createForNode(JSC::JSGlobalObject&, JSC::JSValue, SerializationForStorage, SerializationForCrossProcessTransfer, bool& hasBufferEnvelope); + WEBCORE_EXPORT static ExceptionOr> createForNode(JSC::JSGlobalObject&, JSC::JSValue, SerializationForStorage, SerializationForCrossProcessTransfer, bool& hasBufferEnvelope, SerializationContext = SerializationContext::Default); // Fast path for postMessage with pure strings static Ref createStringFastPath(const String& string); @@ -157,7 +158,7 @@ class SerializedScriptValue : public ThreadSafeRefCounted WEBCORE_EXPORT ~SerializedScriptValue(); private: - static ExceptionOr> create(JSC::JSGlobalObject&, JSC::JSValue, Vector>&& transfer, Vector>&, SerializationForStorage, SerializationErrorMode, SerializationContext, SerializationForCrossProcessTransfer, bool* hasBufferEnvelope = nullptr); + static ExceptionOr> create(JSC::JSGlobalObject&, JSC::JSValue, Vector>&& transfer, Vector>&, SerializationForStorage, SerializationErrorMode, SerializationContext, SerializationForCrossProcessTransfer, bool* hasBufferEnvelope = nullptr, JSC::JSArray* legacyNodeBuffers = nullptr); WEBCORE_EXPORT SerializedScriptValue(Vector&&, std::unique_ptr&& = nullptr); SerializedScriptValue(Vector&&, std::unique_ptr, std::unique_ptr sharedBuffers diff --git a/src/runtime/ipc.rs b/src/runtime/ipc.rs index 790eafae88fe..914a69828771 100644 --- a/src/runtime/ipc.rs +++ b/src/runtime/ipc.rs @@ -262,10 +262,15 @@ mod advanced { const HEADER_LENGTH: usize = size_of::() + size_of::(); // HEADER_LENGTH is a 5-byte compile-time constant; narrowing to u32 is provably safe. const HEADER_LENGTH_U32: u32 = HEADER_LENGTH as u32; - // v2 added `SerializedMessageWithBuffers`. The peer's advertised version is - // debug-logged, never consulted, so mixed-version pairs only break when a - // Buffer-bearing message actually crosses to a v1 peer. + // The low 16 bits remain protocol v2; old readers consume and ignore the + // entire hello word, so its high bits can advertise serializer capabilities. const VERSION: u32 = 2; + const VERSION_MASK: u32 = 0xFFFF; + const ERROR_CAUSE_CAPABILITY: u32 = 1 << 16; + + pub(super) fn supports_error_cause(hello: u32) -> bool { + hello & VERSION_MASK == VERSION && hello & ERROR_CAUSE_CAPABILITY != 0 + } #[repr(u8)] #[derive(Copy, Clone, Eq, PartialEq)] @@ -295,7 +300,7 @@ mod advanced { } static VERSION_PACKET_BYTES: [u8; HEADER_LENGTH] = { - let v = VERSION.to_ne_bytes(); + let v = (VERSION | ERROR_CAUSE_CAPABILITY).to_le_bytes(); [IPCMessageType::Version as u8, v[0], v[1], v[2], v[3]] }; @@ -386,6 +391,7 @@ mod advanced { global: &JSGlobalObject, value: JSValue, is_internal: IsInternal, + peer_supports_error_cause: bool, ) -> Result { // Internal (control) messages never carry user Buffers, and the // hardcoded ack/nack packets depend on their bare wire shape. @@ -396,6 +402,7 @@ mod advanced { for_cross_process_transfer: true, for_storage: false, preserve_node_buffers: matches!(is_internal, IsInternal::External), + omit_error_cause: !peer_supports_error_cause, }, )?; let message_type = match is_internal { @@ -618,9 +625,16 @@ pub(crate) fn serialize( global: &JSGlobalObject, value: JSValue, is_internal: IsInternal, + peer_supports_error_cause: bool, ) -> Result { match mode { - Mode::Advanced => advanced::serialize(writer, global, value, is_internal), + Mode::Advanced => advanced::serialize( + writer, + global, + value, + is_internal, + peer_supports_error_cause, + ), Mode::Json => json::serialize(writer, global, value, is_internal), } } @@ -902,6 +916,7 @@ pub(crate) struct SendQueue { #[cfg(debug_assertions)] pub(crate) has_written_version: Cell, pub(crate) mode: Mode, + peer_supports_error_cause: Cell, pub internal_msg_queue: JsCell, incoming: JsCell, pub(crate) incoming_fd: Cell>, @@ -1030,6 +1045,7 @@ impl SendQueue { #[cfg(debug_assertions)] has_written_version: Cell::new(0), mode, + peer_supports_error_cause: Cell::new(false), internal_msg_queue: JsCell::new(InternalMsgHolder::default()), incoming: JsCell::new(IncomingBuffer::init(mode)), incoming_fd: Cell::new(None), @@ -1643,7 +1659,20 @@ impl SendQueue { let immediate = self.queue.get().is_empty() && self.waiting_for_ack.get().is_none(); let mode = self.mode; let mut payload = StreamBuffer::default(); - let payload_length = match serialize(mode, &mut payload, global, value, is_internal) { + let supports_error_cause = self.peer_supports_error_cause.get(); + if mode == Mode::Advanced && !supports_error_cause { + log!( + "Peer has not advertised Error.cause support; serializing legacy v14 without causes" + ); + } + let payload_length = match serialize( + mode, + &mut payload, + global, + value, + is_internal, + supports_error_cause, + ) { Ok(n) => n, Err(_) => return SerializeAndSendResult::Failure, }; @@ -2101,6 +2130,11 @@ fn handle_ipc_message( message: DecodedIPCMessage, global_this: &JSGlobalObject, ) -> JsResult<()> { + if let DecodedIPCMessage::Version(hello) = message { + send_queue + .peer_supports_error_cause + .set(advanced::supports_error_cause(hello)); + } #[cfg(debug_assertions)] { // The `Formatter` runs its deinit in `Drop`. diff --git a/test/js/node/child_process/child_process_ipc.test.js b/test/js/node/child_process/child_process_ipc.test.js index e803886fff1e..39d472385b96 100644 --- a/test/js/node/child_process/child_process_ipc.test.js +++ b/test/js/node/child_process/child_process_ipc.test.js @@ -1,6 +1,27 @@ import { $ } from "bun"; import { bunEnv, bunExe, tempDir } from "harness"; +for (const kind of ["object", "error", "cause"]) { + for (const direction of ["parent", "child"]) { + test.concurrent(`advanced IPC negotiates Error.cause: ${kind} from ${direction}`, async () => { + await using child = Bun.spawn({ + cmd: [bunExe(), `${import.meta.dir}/fixtures/error-cause-ipc.cjs`, bunExe(), kind, direction, "true"], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([child.stdout.text(), child.stderr.text(), child.exited]); + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual( + kind === "object" + ? { answer: 42, error: false } + : { error: true, message: "outer", ...(kind === "cause" ? { cause: "inner" } : {}) }, + ); + expect(exitCode).toBe(0); + }); + } +} + test("child_process ipc", async () => { const output = await $`${bunExe()} ${import.meta.dir}/fixtures/ipc_fixture.js`.text(); // node (v23.4.0) has identical output diff --git a/test/js/node/child_process/fixtures/error-cause-ipc.cjs b/test/js/node/child_process/fixtures/error-cause-ipc.cjs new file mode 100644 index 000000000000..24ed90ae9125 --- /dev/null +++ b/test/js/node/child_process/fixtures/error-cause-ipc.cjs @@ -0,0 +1,51 @@ +const { fork } = require("node:child_process"); +const { once } = require("node:events"); +const assert = require("node:assert/strict"); + +function payload(kind) { + if (kind === "object") return { answer: 42 }; + const error = new Error("outer"); + error.stack = "stable"; + if (kind === "cause") error.cause = new Error("inner"); + return error; +} + +function summarize(value) { + return { answer: value.answer, error: value instanceof Error, message: value.message, cause: value.cause?.message }; +} + +if (process.argv[2] === "child") { + process.on("message", ({ kind, direction }) => { + if (direction === "child") process.send(payload(kind)); + else process.send(summarize(kind)); + }); + process.send("ready"); +} else { + (async () => { + const [childExecutable, kind, direction, expectedCause] = process.argv.slice(2); + const child = fork(__filename, ["child"], { + execPath: childExecutable, + serialization: "advanced", + stdio: ["ignore", "inherit", "inherit", "ipc"], + }); + try { + assert.equal((await once(child, "message"))[0], "ready"); + const reply = once(child, "message"); + child.send(direction === "child" ? { kind, direction } : { kind: payload(kind), direction }); + const value = (await reply)[0]; + const result = direction === "child" ? summarize(value) : value; + assert.equal(result.error, kind !== "object"); + if (kind === "object") assert.equal(result.answer, 42); + else assert.equal(result.message, "outer"); + assert.equal(result.cause, expectedCause === "true" && kind === "cause" ? "inner" : undefined); + console.log(JSON.stringify(result)); + } finally { + const exit = once(child, "exit"); + child.kill(); + await exit; + } + })().catch(error => { + console.error(error); + process.exitCode = 1; + }); +} diff --git a/test/js/node/v8/legacy-clone-golden.fixture.json b/test/js/node/v8/legacy-clone-golden.fixture.json new file mode 100644 index 000000000000..139e3fef0f48 --- /dev/null +++ b/test/js/node/v8/legacy-clone-golden.fixture.json @@ -0,0 +1,12 @@ +{ + "primitives": "9fe335d551e8ef71bcd30b72040071246f36e15f62210596bdd7205cdf3f7c82", + "object": "59dc238949a0de68350f462a46ed0591d56b030e70bcd5bce7843b8a22aa1e6b", + "holes": "35170162854c1245c5c4bd0bc11b51aee211198d571bb66917320f5f3678273a", + "terminals": "df955f577f604cd42d84b3a712fd2a2d380061e737bf1c6a939e187762d0b78c", + "collections": "d9934e3552e683119b08ad27abfa455d74d5647e433f62d62164e5bf6f26c7c8", + "views": "15d483ea50c87db40152dcf380a2da69496e4c4f6b886faa1db5198f1c9a861c", + "buffers": "2163e5cf52ff0a40dafaf8a314b89fd6077eb18bfe5445e0d4a149caee1aa6ee", + "nestedBuffers": "a3749c9b64624b37e12736c0737da4a95572420a8aa5c747ae822412a9d304b5", + "getterBuffer": "1e48d3b90c9539431743aafe57db130e88c28d0affb97d5839cc857cadf567a4", + "bufferPoolBoundary": "661534cf720ff7273df10ddb432d79662d255c27c241e00634389b316cf537a0" +} diff --git a/test/js/node/v8/legacy-clone-graphs.fixture.mjs b/test/js/node/v8/legacy-clone-graphs.fixture.mjs new file mode 100644 index 000000000000..d372351034ac --- /dev/null +++ b/test/js/node/v8/legacy-clone-graphs.fixture.mjs @@ -0,0 +1,29 @@ +export function legacyGraphs() { + const error = new TypeError("stable"); + error.stack = "stable"; + error.line = 0; + error.column = 0; + error.sourceURL = ""; + const a = Buffer.alloc(3, 1); + const b = Buffer.alloc(2, 2); + const shared = { n: 1 }; + const cycle = { shared, again: shared }; + cycle.self = cycle; + const slots = Array.from({ length: 254 }, (_, n) => ({ n })); + return { + primitives: [undefined, null, false, true, 0, -1, 1.5, "hello", "é", 123n], + object: { answer: 42, nested: ["x", { ok: true }] }, + holes: [1, , undefined, , 4], + terminals: [new Date(42), /abc/gi, error, error], + collections: [new Map([[shared, [1, 2]]]), new Set([shared]), cycle], + views: [new Uint8Array([1, 2]), new Uint16Array([3, 4]), new ArrayBuffer(2)], + buffers: { a, b, again: a }, + nestedBuffers: new Map([[a, { b, a }]]), + getterBuffer: { + get value() { + return a; + }, + }, + bufferPoolBoundary: { slots, a, again: a, first: slots[0], last: slots.at(-1) }, + }; +} diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index 27beb05cdc86..aef565ac84fc 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -1,11 +1,28 @@ import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe } from "harness"; import v8 from "node:v8"; +import { createHash } from "node:crypto"; +import legacyGolden from "./legacy-clone-golden.fixture.json"; +import { legacyGraphs } from "./legacy-clone-graphs.fixture.mjs"; // Buffer identity through v8.serialize/deserialize, matching node v26.3.0's // serializer delegate behavior (DefaultSerializer host objects). Non-Buffer // payloads keep bare JSC framing instead of the Buffer envelope. describe("v8 serialize/deserialize Buffer identity", () => { + test("cause-free graphs retain the pre-cause v14 bytes", () => { + for (const [name, value] of Object.entries(legacyGraphs())) { + const bytes = v8.serialize(value); + expect(createHash("sha256").update(bytes).digest("hex")).toBe(legacyGolden[name]); + } + }); + + test("only a graph with a cause edge requires v15", () => { + const error = new Error("empty"); + error.stack = "stable"; + expect(v8.serialize([error, error]).readUInt32LE(0)).toBe(14); + error.cause = undefined; + expect(v8.serialize([new Error("first"), error]).readUInt32LE(0)).toBe(15); + }); test.each([new Uint8Array([1, 2]), Buffer.from([1, 2])])("serialized output is transferable: %p", input => { const serialized = v8.serialize(input); expect(serialized.buffer).toBeInstanceOf(ArrayBuffer); @@ -163,123 +180,9 @@ describe("v8 serialize/deserialize Buffer identity", () => { expect(v8.serialize(input)).toEqual(jsc.serialize(input, { binaryType: "nodebuffer" })); }); - test("only serialized Buffer identities are tagged, including getter results and subclasses", () => { - const buffer = Buffer.from("visible"); - Object.setPrototypeOf(buffer, Object.create(Buffer.prototype)); - let reads = 0; - const ignored = new Map(); - Object.defineProperty(ignored, "extra", { value: Buffer.from("ignored"), enumerable: true }); - const bytes = v8.serialize({ - get value() { - reads++; - return buffer; - }, - ignored, - }); - const envelope = require("bun:jsc").deserialize(bytes.subarray(5)); - expect(reads).toBe(1); - expect(envelope[1]).toHaveLength(1); - expect(envelope[1][0]).toBe(envelope[0].value); - const out = v8.deserialize(bytes); - expect(Buffer.isBuffer(out.value)).toBe(true); - expect(out.value.toString()).toBe("visible"); - expect(Reflect.ownKeys(out.ignored)).toEqual([]); - }); - - for (const transport of ["v8", "structuredClone", "advanced IPC"]) { - test.each(["benign", "throwing", "cyclic", "Buffer", "subclass", "intermediate"])( - `${transport} classifies %s typed arrays without prototype callbacks`, - async kind => { - // A subprocess bounds synchronous native hangs and crashes; a test-runner - // timeout alone cannot interrupt a stuck serializer on its own thread. - const source = ` - const { serialize, deserialize } = require("node:v8"); - const kind = ${JSON.stringify(kind)}; - const transport = ${JSON.stringify(transport)}; - let traps = 0, reads = 0; - let value; - if (["Buffer", "subclass", "intermediate"].includes(kind)) { - value = Buffer.from([7, 8]); - if (kind === "subclass") { - class Derived extends Buffer {} - Object.setPrototypeOf(value, Derived.prototype); - } else if (kind === "intermediate") { - Object.setPrototypeOf(value, Object.create(Buffer.prototype)); - } - } else { - value = new Uint8Array([7, 8]); - const prototype = new Proxy(Uint8Array.prototype, { - getPrototypeOf(target) { - traps++; - if (kind === "throwing") throw new Error("must not inspect prototype"); - if (kind === "cyclic") return prototype; - return Reflect.getPrototypeOf(target); - } - }); - Object.setPrototypeOf(value, prototype); - } - const input = { get value() { reads++; return value; }, alias: value }; - input.self = input; - if (transport === "advanced IPC") { - process.on("message", () => process.disconnect()); - process.send(input); - console.log(JSON.stringify({ traps, reads })); - } else { - const out = transport === "v8" ? deserialize(serialize(input)) : structuredClone(input); - console.log(JSON.stringify({ - traps, reads, bytes: Array.from(out.value), buffer: Buffer.isBuffer(out.value), - alias: out.value === out.alias, cycle: out.self === out - })); - } - `; - let received: any; - const proc = Bun.spawn({ - cmd: [bunExe(), "-e", source], - env: bunEnv, - stdout: "pipe", - stderr: "pipe", - ...(transport === "advanced IPC" - ? { - serialization: "advanced" as const, - ipc(message: any, child: Bun.Subprocess) { - received = message; - child.send("received"); - }, - } - : {}), - }); - let timedOut = false; - const timeout = setTimeout(() => { - timedOut = true; - proc.kill(9); - }, 10_000); - try { - const [stdout, stderr, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - expect(timedOut).toBe(false); - expect(code).toBe(0); - expect(proc.signalCode).toBeNull(); - expect(stderr).toBe(""); - const result = JSON.parse(stdout); - expect(result.traps).toBe(0); - expect(result.reads).toBe(1); - const out = transport === "advanced IPC" ? received : result; - const isBuffer = ["Buffer", "subclass", "intermediate"].includes(kind) && transport !== "structuredClone"; - expect(transport === "advanced IPC" ? Array.from(out.value) : out.bytes).toEqual([7, 8]); - expect(transport === "advanced IPC" ? Buffer.isBuffer(out.value) : out.buffer).toBe(isBuffer); - expect(transport === "advanced IPC" ? out.value === out.alias : out.alias).toBe(true); - expect(transport === "advanced IPC" ? out.self === out : out.cycle).toBe(true); - } finally { - clearTimeout(timeout); - if (proc.exitCode === null) proc.kill(9); - await proc.exited; - } - }, - 20_000, - ); - } - test("advanced IPC tags Buffers introduced by earlier Error metadata", async () => { const source = [ + 'process.once("message", () => {', "let calls = 0;", 'for (const initial of ["buffer", "absent"]) for (const field of ["name", "message", "stack"]) {', ' const later = initial === "buffer" ? new Error("later", { cause: Buffer.from("old") }) : new Error("later");', @@ -289,7 +192,7 @@ describe("v8 serialize/deserialize Buffer identity", () => { " process.send([first, later, later]);", "}", "process.send({ done: true, calls });", - 'process.on("message", () => {});', + "});", ].join("\n"); const { promise, resolve, reject } = Promise.withResolvers(); const messages: any[] = []; @@ -310,6 +213,7 @@ describe("v8 serialize/deserialize Buffer identity", () => { reject(new Error("IPC child exited before completion: " + code + "/" + signal)); }, }); + child.send("ready"); await promise; expect(calls).toBe(6); expect(messages).toHaveLength(6); diff --git a/test/js/web/workers/error-cause-node-parity.test.mjs b/test/js/web/workers/error-cause-node-parity.test.mjs new file mode 100644 index 000000000000..dc748dd208b9 --- /dev/null +++ b/test/js/web/workers/error-cause-node-parity.test.mjs @@ -0,0 +1,94 @@ +import assert from "node:assert/strict"; +import { once } from "node:events"; +import { test } from "node:test"; +import { deserialize, serialize } from "node:v8"; +import { MessageChannel, Worker } from "node:worker_threads"; + +function graph() { + const shared = { answer: 42 }; + const first = new Error("outer", { cause: shared }); + const second = new TypeError("inner", { cause: first }); + shared.second = second; + return [first, shared, first, new Error("absent"), new Error("undefined", { cause: undefined })]; +} + +function check(value) { + assert(value[0] instanceof Error); + assert.equal(value[0].cause, value[1]); + assert.equal(value[1].second.cause, value[0]); + assert.equal(value[0], value[2]); + assert.equal(Object.hasOwn(value[3], "cause"), false); + assert.deepEqual(Object.getOwnPropertyDescriptor(value[4], "cause"), { + value: undefined, + writable: true, + enumerable: false, + configurable: true, + }); +} + +for (const [name, clone] of [ + ["structuredClone", structuredClone], + ["node:v8", value => deserialize(serialize(value))], +]) { + test(`${name} retains cause cycles, aliases and descriptors`, () => check(clone(graph()))); + test(`${name} omits accessor causes without invoking them`, () => { + const error = new Error("accessor"); + Object.defineProperty(error, "cause", { + get() { + throw new Error("must not run"); + }, + }); + assert.equal(Object.hasOwn(clone(error), "cause"), false); + }); +} + +test("structuredClone preserves the four cause value kinds", () => { + const shared = new Uint8Array(new SharedArrayBuffer(4)); + shared[0] = 42; + for (const cause of [new Error("inner"), 42, shared, { answer: 42 }]) { + const result = structuredClone(new Error("outer", { cause })); + assert(Object.hasOwn(result, "cause")); + if (cause instanceof Error) assert.equal(result.cause.message, cause.message); + else assert.deepEqual(result.cause, cause); + } +}); + +test("Worker postMessage retains cause graphs", async () => { + const worker = new Worker( + 'require("node:worker_threads").parentPort.once("message", value => require("node:worker_threads").parentPort.postMessage(value))', + { eval: true }, + ); + try { + const reply = once(worker, "message"); + worker.postMessage(graph()); + check((await reply)[0]); + } finally { + await worker.terminate(); + } +}); + +test("MessagePort retains cause graphs", async () => { + const { port1, port2 } = new MessageChannel(); + try { + const reply = once(port2, "message"); + port1.postMessage(graph()); + check((await reply)[0]); + } finally { + port1.close(); + port2.close(); + } +}); + +test("BroadcastChannel retains cause graphs", async () => { + const name = `error-cause-${process.pid}`; + const sender = new BroadcastChannel(name); + const receiver = new BroadcastChannel(name); + try { + const reply = once(receiver, "message"); + sender.postMessage(graph()); + check((await reply)[0].data); + } finally { + sender.close(); + receiver.close(); + } +}); From 7b83a42b666dbc31c3e36e5d83cfa4ebba24a345 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 19 Oct 2026 22:28:04 -0700 Subject: [PATCH 10/11] fix: align clone helper calls with current engine APIs Pass the explicit non-Node arguments to the nested CryptoKey serializer and use the engine's uncheckedDowncast/asArray helpers. Apply the v8 regression test import ordering requested by CI. Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- src/jsc/bindings/webcore/SerializedScriptValue.cpp | 6 +++--- test/js/node/v8/v8-serdes-buffer.test.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 8190edf6445c..1a5661523b14 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -1425,7 +1425,7 @@ class CloneSerializer : public CloneBase { dummyModules, dummyMemoryHandles, #endif - serializedKey, SerializationContext::Default, dummySharedBuffers, m_forStorage, m_forTransfer); + serializedKey, SerializationContext::Default, dummySharedBuffers, m_forStorage, m_forTransfer, false, nullptr); rawKeySerializer.write(key); write(serializedKey); return true; @@ -4711,14 +4711,14 @@ ExceptionOr> SerializedScriptValue::createForNode(JSG { auto& vm = globalObject.vm(); auto scope = DECLARE_THROW_SCOPE(vm); - auto* global = jsCast(&globalObject); + auto* global = uncheckedDowncast(&globalObject); auto* tagFunction = global->m_ipcTagAdvancedBuffersFunction.getInitializedOnMainThread(global); MarkedArgumentBuffer args; args.append(value); auto tagged = JSC::call(global, tagFunction, JSC::getCallData(tagFunction), jsUndefined(), args); RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError }); Strong taggedRoot(vm, tagged.getObject()); - auto* legacyBuffers = tagged.isNull() ? nullptr : jsCast(asArray(tagged)->getDirectIndex(global, 1)); + auto* legacyBuffers = tagged.isNull() ? nullptr : asArray(asArray(tagged)->getDirectIndex(global, 1)); RETURN_IF_EXCEPTION(scope, Exception { ExistingExceptionError }); Vector> ports; hasBufferEnvelope = false; diff --git a/test/js/node/v8/v8-serdes-buffer.test.ts b/test/js/node/v8/v8-serdes-buffer.test.ts index aef565ac84fc..df24fa362778 100644 --- a/test/js/node/v8/v8-serdes-buffer.test.ts +++ b/test/js/node/v8/v8-serdes-buffer.test.ts @@ -1,7 +1,7 @@ import { describe, expect, test } from "bun:test"; import { bunEnv, bunExe } from "harness"; -import v8 from "node:v8"; import { createHash } from "node:crypto"; +import v8 from "node:v8"; import legacyGolden from "./legacy-clone-golden.fixture.json"; import { legacyGraphs } from "./legacy-clone-graphs.fixture.mjs"; From fb391eaf21de1df17ed96bc8620b44ebeb864875 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 19 Oct 2026 22:46:35 -0700 Subject: [PATCH 11/11] test: assert negotiated Error cause capability in IPC hello Keep the five-byte and protocol-version checks exact while expecting the Error.cause capability advertised by the compatible serializer handshake. Co-authored-by: Jason (Json) <263060202+fuller-stack-dev@users.noreply.github.com> --- test/js/bun/spawn/spawn.ipc.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/js/bun/spawn/spawn.ipc.test.ts b/test/js/bun/spawn/spawn.ipc.test.ts index 67a4f313bf38..10d8dd350389 100644 --- a/test/js/bun/spawn/spawn.ipc.test.ts +++ b/test/js/bun/spawn/spawn.ipc.test.ts @@ -355,9 +355,9 @@ it("child with unusable NODE_CHANNEL_FD tears down IPC without crashing", async expect(exitCode).toBe(0); }); -it.skipIf(isWindows)("advanced serialization advertises wire format version 2", async () => { +it.skipIf(isWindows)("advanced serialization advertises protocol 2 and Error.cause support", async () => { // The version packet is the first frame on the channel: - // [type=Version(1), u32 LE version]. Read it raw off fd 3 before the + // [type=Version(1), u16 LE protocol=2, u16 LE capabilities=1]. Read it raw off fd 3 before the // child's own channel machinery starts consuming the socket. await using child = Bun.spawn({ cmd: [ @@ -381,7 +381,7 @@ it.skipIf(isWindows)("advanced serialization advertises wire format version 2", ipc() {}, }); const [stdout, exitCode] = await Promise.all([child.stdout.text(), child.exited]); - expect(JSON.parse(stdout.trim())).toEqual([1, 2, 0, 0, 0]); + expect(JSON.parse(stdout.trim())).toEqual([1, 2, 0, 1, 0]); expect(exitCode).toBe(0); });