Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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.10',
'v8_embedder_string': '-node.11',

##### V8 defaults for Node.js #####

Expand Down
3 changes: 2 additions & 1 deletion deps/v8/src/builtins/builtins-arraybuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,8 @@ Tagged<Object> 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));
Expand Down
22 changes: 22 additions & 0 deletions deps/v8/test/mjsunit/array-buffer-transfer-detach-key.js
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions deps/v8/test/unittests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 40 additions & 0 deletions deps/v8/test/unittests/api/v8-array-buffer-unittest.cc
Original file line number Diff line number Diff line change
@@ -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<ArrayBuffer> ab = ArrayBuffer::New(isolate(), 1);
Local<Value> key = Symbol::New(isolate());
ab->SetDetachKey(key);
Local<Object> global = context()->Global();
Local<String> 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
2 changes: 2 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ In particular, this makes sense for objects that can be cloned, rather than
transferred, and which are used by other objects on the sending side.
For example, Node.js marks the `ArrayBuffer`s it uses for its
[`Buffer` pool][`Buffer.allocUnsafe()`] with this.
`ArrayBuffer.prototype.transfer()` is disallowed on such array buffer
instances.

This operation cannot be undone.

Expand Down
14 changes: 14 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ const {
createUnsafeBuffer,
} = require('internal/buffer');

const {
namespace: {
addDeserializeCallback,
isBuildingSnapshot,
},
} = require('internal/v8/startup_snapshot');

FastBuffer.prototype.constructor = Buffer;
Buffer.prototype = FastBuffer.prototype;
addBufferPrototypeMethods(Buffer.prototype);
Expand Down Expand Up @@ -159,6 +166,13 @@ function createPool() {
poolOffset = 0;
}
createPool();
if (isBuildingSnapshot()) {
addDeserializeCallback(() => {
// TODO(legendecas): ArrayBuffer.[[ArrayBufferDetachKey]] is not been serialized.
// Remove this callback when snapshot serialization supports it.
createPool();
});
}

function alignPool() {
// Ensure aligned slices
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
Float64Array,
MathFloor,
Number,
Symbol,
Uint8Array,
} = primordials;

Expand All @@ -15,6 +16,8 @@ const {
ERR_OUT_OF_RANGE,
} = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');
const { isArrayBuffer } = require('util/types');

const {
asciiSlice,
base64Slice,
Expand All @@ -31,6 +34,7 @@ const {
ucs2Write,
utf8WriteStatic,
createUnsafeArrayBuffer,
setDetachKey,
} = internalBinding('buffer');

const {
Expand Down Expand Up @@ -1068,6 +1072,10 @@ function markAsUntransferable(obj) {
if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null)
return; // This object is a primitive and therefore already untransferable.
obj[untransferable_object_private_symbol] = true;

if (isArrayBuffer(obj)) {
setDetachKey(obj, Symbol('unique_detach_key_for_untransferable_arraybuffer'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ linter comment seems applicable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is there any disadvantage to making this a static key, rather than generating a new symbol for each invocation?

}
}

// This simply checks if the object is marked as untransferable and doesn't
Expand Down
13 changes: 13 additions & 0 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,15 @@ static void Atob(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(error_code);
}

static void SetDetachKey(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 2);
CHECK(args[0]->IsArrayBuffer());

Local<ArrayBuffer> ab = args[0].As<ArrayBuffer>();
Local<Value> key = args[1];
ab->SetDetachKey(key);
}

namespace {

std::pair<void*, size_t> DecomposeBufferToParts(Local<Value> buffer) {
Expand Down Expand Up @@ -1638,6 +1647,8 @@ void Initialize(Local<Object> target,
"utf8WriteStatic",
SlowWriteString<UTF8>,
&fast_write_string_utf8);

SetMethod(context, target, "setDetachKey", SetDetachKey);
}

} // anonymous namespace
Expand Down Expand Up @@ -1692,6 +1703,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {

registry->Register(Atob);
registry->Register(Btoa);

registry->Register(SetDetachKey);
}

} // namespace Buffer
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ expected.beforePreExec = new Set([
'NativeModule events',
'Internal Binding buffer',
'Internal Binding string_decoder',
'NativeModule util/types',
'NativeModule internal/buffer',
'NativeModule buffer',
'Internal Binding messaging',
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-buffer-pool-untransferable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ assert.throws(() => port1.postMessage(a, [ a.buffer ]), {
// Verify that the pool ArrayBuffer has not actually been transferred:
assert.strictEqual(a.buffer, b.buffer);
assert.strictEqual(a.length, length);

// Verify that ArrayBuffer.prototype.transfer() also throws.
assert.throws(() => a.buffer.transfer(), {
name: 'TypeError',
});
assert.strictEqual(a.buffer, b.buffer);
Loading