Skip to content

Commit

Permalink
deps: V8: cherry-pick 9df5ef70ff18
Browse files Browse the repository at this point in the history
Original commit message:

    Add an `v8::ArrayBuffer::WasDetached` method to the C++ API

    V8's C++ API does not give a way to tell whether an ArrayBuffer has
    been detached from the `v8::ArrayBuffer` class. In fact, as far as can
    be told from the C++ API without running JS code, detached
    ArrayBuffers behave the same as zero-sized ArrayBuffers and there is
    no way to observe the difference. However, this difference can be
    observed in JS because constructing a TypedArray from a detached
    ArrayBuffer will throw.

    This change adds a `WasDetached` method to the `v8::ArrayBuffer` class
    to give embedders access to this information without having to run JS
    code.

    Bug: v8:13159
    Change-Id: I2bb1e380cee1cecd31f6d48ec3d9f28c03a8a673
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3810345
    Commit-Queue: Toon Verwaest <[email protected]>
    Reviewed-by: Toon Verwaest <[email protected]>
    Cr-Commit-Position: refs/heads/main@{#83963}

Refs: v8/v8@9df5ef7
PR-URL: nodejs#45474
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Daeyeon Jeong <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Jiawen Geng <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
anonrig authored and marco-ippolito committed Nov 23, 2022
1 parent a1c85ce commit 35e5090
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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.19',
'v8_embedder_string': '-node.20',

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

Expand Down
1 change: 1 addition & 0 deletions deps/v8/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Allan Sandfeld Jensen <[email protected]>
Amos Lim <[email protected]>
Andreas Anyuru <[email protected]>
Andrei Kashcha <[email protected]>
Andreu Botella <[email protected]>
Andrew Paprocki <[email protected]>
Anna Henningsen <[email protected]>
Antoine du Hamel <[email protected]>
Expand Down
8 changes: 8 additions & 0 deletions deps/v8/include/v8-array-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ class V8_EXPORT ArrayBuffer : public Object {
*/
bool IsDetachable() const;

/**
* Returns true if this ArrayBuffer has been detached.
*/
bool WasDetached() const;

/**
* Detaches this ArrayBuffer and all its views (typed arrays).
* Detaching sets the byte length of the buffer and all typed arrays to zero,
Expand All @@ -253,6 +258,9 @@ class V8_EXPORT ArrayBuffer : public Object {
* pointer coordinates the lifetime management of the internal storage
* with any live ArrayBuffers on the heap, even across isolates. The embedder
* should not attempt to manage lifetime of the storage through other means.
*
* The returned shared pointer will not be empty, even if the ArrayBuffer has
* been detached. Use |WasDetached| to tell if it has been detached instead.
*/
std::shared_ptr<BackingStore> GetBackingStore();

Expand Down
4 changes: 4 additions & 0 deletions deps/v8/src/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8064,6 +8064,10 @@ bool v8::ArrayBuffer::IsDetachable() const {
return Utils::OpenHandle(this)->is_detachable();
}

bool v8::ArrayBuffer::WasDetached() const {
return Utils::OpenHandle(this)->was_detached();
}

namespace {
std::shared_ptr<i::BackingStore> ToInternal(
std::shared_ptr<i::BackingStoreBase> backing_store) {
Expand Down
1 change: 1 addition & 0 deletions deps/v8/test/cctest/cctest.status
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
'test-api/WasmI32AtomicWaitCallback': [SKIP],
'test-api/WasmI64AtomicWaitCallback': [SKIP],
'test-api/WasmSetJitCodeEventHandler': [SKIP],
'test-api-array-buffer/ArrayBuffer_NonDetachableWasDetached': [SKIP],
'test-backing-store/Run_WasmModule_Buffer_Externalized_Regression_UseAfterFree': [SKIP],
'test-c-wasm-entry/*': [SKIP],
'test-compilation-cache/*': [SKIP],
Expand Down
31 changes: 31 additions & 0 deletions deps/v8/test/cctest/test-api-array-buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@ THREADED_TEST(ArrayBuffer_DetachingScript) {
CheckDataViewIsDetached(dv);
}

THREADED_TEST(ArrayBuffer_WasDetached) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope handle_scope(isolate);

Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 0);
CHECK(!ab->WasDetached());

ab->Detach();
CHECK(ab->WasDetached());
}

THREADED_TEST(ArrayBuffer_NonDetachableWasDetached) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
v8::HandleScope handle_scope(isolate);

CompileRun(R"JS(
var wasmMemory = new WebAssembly.Memory({initial: 1, maximum: 2});
)JS");

Local<v8::ArrayBuffer> non_detachable =
CompileRun("wasmMemory.buffer").As<v8::ArrayBuffer>();
CHECK(!non_detachable->IsDetachable());
CHECK(!non_detachable->WasDetached());

CompileRun("wasmMemory.grow(1)");
CHECK(!non_detachable->IsDetachable());
CHECK(non_detachable->WasDetached());
}

THREADED_TEST(ArrayBuffer_ExternalizeEmpty) {
LocalContext env;
v8::Isolate* isolate = env->GetIsolate();
Expand Down

0 comments on commit 35e5090

Please sign in to comment.