From 780e65c5c5369ae77fe46ea2ff59e025d544ef6e Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Wed, 14 Jan 2026 14:55:26 -0500 Subject: [PATCH] deps: V8: cherry-pick c5ff7c4d6cde MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Original commit message: [builtins] disallow ArrayBuffer transfer with a detach key This allows embedder to disallow `ArrayBuffer.prototype.transfer()` on an arraybuffer that is not detachable. This also fix the check on `ArrayBufferCopyAndDetach` step 8 of `ArrayBuffer.prototype.transfer`. Refs: https://github.com/nodejs/node/issues/61362 Refs: https://tc39.es/ecma262/#sec-arraybuffercopyanddetach Change-Id: I3c6e156a8fad007fd100218d8b16aed5c4e1db68 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7454288 Commit-Queue: Chengzhong Wu Reviewed-by: Olivier Flückiger Cr-Commit-Position: refs/heads/main@{#104697} Refs: https://github.com/v8/v8/commit/c5ff7c4d6cdea06d0319609875c41eaa77b70db0 PR-URL: https://github.com/nodejs/node/pull/61372 Fixes: https://github.com/nodejs/node/issues/61362 Reviewed-By: Anna Henningsen Reviewed-By: René --- common.gypi | 2 +- deps/v8/src/builtins/builtins-arraybuffer.cc | 3 +- .../array-buffer-transfer-detach-key.js | 22 ++++++++++ deps/v8/test/unittests/BUILD.gn | 1 + .../unittests/api/v8-array-buffer-unittest.cc | 40 +++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 deps/v8/test/mjsunit/array-buffer-transfer-detach-key.js create mode 100644 deps/v8/test/unittests/api/v8-array-buffer-unittest.cc diff --git a/common.gypi b/common.gypi index 5e1d6d8b695..fec31779ece 100644 --- a/common.gypi +++ b/common.gypi @@ -38,7 +38,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.18', + 'v8_embedder_string': '-node.19', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/src/builtins/builtins-arraybuffer.cc b/deps/v8/src/builtins/builtins-arraybuffer.cc index cf458b46348..f066c83d1cd 100644 --- a/deps/v8/src/builtins/builtins-arraybuffer.cc +++ b/deps/v8/src/builtins/builtins-arraybuffer.cc @@ -637,7 +637,8 @@ Tagged ArrayBufferTransfer(Isolate* isolate, // 8. If arrayBuffer.[[ArrayBufferDetachKey]] is not undefined, throw a // TypeError exception. - if (!array_buffer->is_detachable()) { + if (!IsUndefined(array_buffer->detach_key()) || + !array_buffer->is_detachable()) { THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kDataCloneErrorNonDetachableArrayBuffer)); diff --git a/deps/v8/test/mjsunit/array-buffer-transfer-detach-key.js b/deps/v8/test/mjsunit/array-buffer-transfer-detach-key.js new file mode 100644 index 00000000000..3c262fa8843 --- /dev/null +++ b/deps/v8/test/mjsunit/array-buffer-transfer-detach-key.js @@ -0,0 +1,22 @@ +// Copyright 2026 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Flags: --allow-natives-syntax + +function TestTransferSucceeds() { + const ab = new ArrayBuffer(100); + %ArrayBufferSetDetachKey(ab, undefined); + ab.transfer(); + assertEquals(0, ab.byteLength); // Detached. +} + +function TestTransferFails() { + const ab = new ArrayBuffer(100); + %ArrayBufferSetDetachKey(ab, Symbol()); + assertThrows(() => { ab.transfer(); }, TypeError); + assertEquals(100, ab.byteLength); // Not detached. +} + +TestTransferSucceeds(); +TestTransferFails(); diff --git a/deps/v8/test/unittests/BUILD.gn b/deps/v8/test/unittests/BUILD.gn index c18c9b2e6b6..090a5178317 100644 --- a/deps/v8/test/unittests/BUILD.gn +++ b/deps/v8/test/unittests/BUILD.gn @@ -263,6 +263,7 @@ v8_source_set("v8_unittests_sources") { "api/remote-object-unittest.cc", "api/resource-constraints-unittest.cc", "api/smi-tagging-unittest.cc", + "api/v8-array-buffer-unittest.cc", "api/v8-array-unittest.cc", "api/v8-maybe-unittest.cc", "api/v8-memory-span-unittest.cc", diff --git a/deps/v8/test/unittests/api/v8-array-buffer-unittest.cc b/deps/v8/test/unittests/api/v8-array-buffer-unittest.cc new file mode 100644 index 00000000000..4aaec1617f6 --- /dev/null +++ b/deps/v8/test/unittests/api/v8-array-buffer-unittest.cc @@ -0,0 +1,40 @@ +// Copyright 2026 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "include/v8-array-buffer.h" + +#include "test/unittests/test-utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace v8 { +namespace { + +using ArrayBufferTest = TestWithContext; + +TEST_F(ArrayBufferTest, TransferWithDetachKey) { + Local ab = ArrayBuffer::New(isolate(), 1); + Local key = Symbol::New(isolate()); + ab->SetDetachKey(key); + Local global = context()->Global(); + Local property_name = + String::NewFromUtf8Literal(isolate(), "test_ab"); + global->Set(context(), property_name, ab).ToChecked(); + + { + TryCatch try_catch(isolate()); + CHECK(TryRunJS("globalThis.test_ab.transfer()").IsEmpty()); + } + + // Didnot transfer. + EXPECT_EQ(ab->ByteLength(), 1u); + + ab->SetDetachKey(Undefined(isolate())); + RunJS("globalThis.test_ab.transfer()"); + + // Transferred. + EXPECT_EQ(ab->ByteLength(), 0u); +} + +} // namespace +} // namespace v8