-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
buffer: disallow ArrayBuffer transfer on pooled buffer #61372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
legendecas
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
legendecas:buffer-transfer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ const { | |
| Float64Array, | ||
| MathFloor, | ||
| Number, | ||
| Symbol, | ||
| Uint8Array, | ||
| } = primordials; | ||
|
|
||
|
|
@@ -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, | ||
|
|
@@ -31,6 +34,7 @@ const { | |
| ucs2Write, | ||
| utf8WriteStatic, | ||
| createUnsafeArrayBuffer, | ||
| setDetachKey, | ||
| } = internalBinding('buffer'); | ||
|
|
||
| const { | ||
|
|
@@ -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')); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
^ linter comment seems applicable