diff --git a/CHANGELOG.md b/CHANGELOG.md index b4bbfc57ec52..b96ceb6c765a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Keep cause-free Error serialization on its terminal fast path with byte-identical v14 output; use a distinct v16 Error cause tag and capability negotiation with older IPC peers. +- Avoid an extra payload copy when structured cloning or messaging a Buffer or ArrayBuffer inside a container, without changing serialized bytes. + - 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. - Sync oven-sh/bun through `ad4089bf49d9f4c3ab7c8d95a58f3e0f0f186054`, including lint, format, binding, WebSocket and SQL updates, while retaining the fork runtime and CI contracts. diff --git a/src/jsc/bindings/webcore/SerializedScriptValue.cpp b/src/jsc/bindings/webcore/SerializedScriptValue.cpp index 4eb3f2e8bac4..7646271daf42 100644 --- a/src/jsc/bindings/webcore/SerializedScriptValue.cpp +++ b/src/jsc/bindings/webcore/SerializedScriptValue.cpp @@ -856,6 +856,7 @@ class CloneSerializer : public CloneBase { { if (!startObjectInternal(object)) return false; + m_pendingContainerBytes += sizeof(TerminatorTag); write(ObjectTag); return true; } @@ -865,6 +866,7 @@ class CloneSerializer : public CloneBase { if (!startObjectInternal(array)) return false; + m_pendingContainerBytes += sizeof(TerminatorTag); unsigned length = array->length(); write(ArrayTag); write(length); @@ -876,6 +878,7 @@ class CloneSerializer : public CloneBase { if (!startObjectInternal(set)) return false; + m_pendingContainerBytes += sizeof(uint8_t) + sizeof(TerminatorTag); write(SetObjectTag); return true; } @@ -885,12 +888,15 @@ class CloneSerializer : public CloneBase { if (!startObjectInternal(map)) return false; + m_pendingContainerBytes += sizeof(uint8_t) + sizeof(TerminatorTag); write(MapObjectTag); return true; } void endObject() { + ASSERT(m_pendingContainerBytes >= sizeof(TerminatorTag)); + m_pendingContainerBytes -= sizeof(TerminatorTag); write(TerminatorTag); } @@ -1386,7 +1392,7 @@ class CloneSerializer : public CloneBase { if (arrayBuffer->isResizableOrGrowableShared()) { uint64_t byteLength = arrayBuffer->byteLength(); - if (!m_buffer.tryReserveCapacity(static_cast(m_buffer.size()) + sizeof(uint8_t) + sizeof(uint64_t) * 2 + byteLength)) [[unlikely]] { + if (!m_buffer.tryReserveCapacity(static_cast(m_buffer.size()) + sizeof(uint8_t) + sizeof(uint64_t) * 2 + byteLength + m_pendingContainerBytes)) [[unlikely]] { code = SerializationReturnCode::DataCloneError; return true; } @@ -1399,7 +1405,9 @@ class CloneSerializer : public CloneBase { } uint64_t byteLength = arrayBuffer->byteLength(); - if (!m_buffer.tryReserveCapacity(static_cast(m_buffer.size()) + sizeof(uint8_t) + sizeof(uint64_t) + byteLength)) [[unlikely]] { + // Reserve mandatory container trailers before copying the payload so + // closing the containers cannot copy the payload again. + if (!m_buffer.tryReserveCapacity(static_cast(m_buffer.size()) + sizeof(uint8_t) + sizeof(uint64_t) + byteLength + m_pendingContainerBytes)) [[unlikely]] { code = SerializationReturnCode::DataCloneError; return true; } @@ -2144,6 +2152,7 @@ class CloneSerializer : public CloneBase { ErrorInstance* m_pendingError { nullptr }; JSValue m_pendingErrorCause; bool m_hasErrorCause { false }; + unsigned m_pendingContainerBytes { 0 }; ObjectPool m_objectPool; ObjectPool m_transferredMessagePorts; ObjectPool m_transferredArrayBuffers; @@ -2330,6 +2339,7 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) mapIteratorStack.removeLast(); RETURN_IF_EXCEPTION(scope, SerializationReturnCode::ExistingExceptionError); // Keep the legacy property-section markers, but clone only Map entries. + m_pendingContainerBytes -= sizeof(uint8_t); write(NonMapPropertiesTag); endObject(); inputObjectStack.removeLast(); @@ -2376,6 +2386,7 @@ SerializationReturnCode CloneSerializer::serialize(JSValue in) if (!hasNext) { setIteratorStack.removeLast(); // Older readers still expect an empty own-property section after Set entries. + m_pendingContainerBytes -= sizeof(uint8_t); write(NonSetPropertiesTag); endObject(); inputObjectStack.removeLast();