From a1d63fa9c99271910d855899f8f4d8621ac1d8c8 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Tue, 15 Nov 2022 10:09:55 -0500 Subject: [PATCH 1/2] deps: V8: cherry-pick 9df5ef70ff18 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 Reviewed-by: Toon Verwaest Cr-Commit-Position: refs/heads/main@{#83963} Refs: https://github.com/v8/v8/commit/9df5ef70ff18977b157028fc55ced5af4bcee535 --- common.gypi | 2 +- deps/v8/AUTHORS | 1 + deps/v8/include/v8-array-buffer.h | 8 +++++ deps/v8/src/api/api.cc | 4 +++ deps/v8/test/cctest/cctest.status | 1 + deps/v8/test/cctest/test-api-array-buffer.cc | 31 ++++++++++++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/common.gypi b/common.gypi index da2e7b0462dfa9..96d7ebe61acfeb 100644 --- a/common.gypi +++ b/common.gypi @@ -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 ##### diff --git a/deps/v8/AUTHORS b/deps/v8/AUTHORS index 3ef613e0f37062..74d6f3b07ff643 100644 --- a/deps/v8/AUTHORS +++ b/deps/v8/AUTHORS @@ -60,6 +60,7 @@ Allan Sandfeld Jensen Amos Lim Andreas Anyuru Andrei Kashcha +Andreu Botella Andrew Paprocki Anna Henningsen Antoine du Hamel diff --git a/deps/v8/include/v8-array-buffer.h b/deps/v8/include/v8-array-buffer.h index bab840f82c1a3d..cc5d2d4323000a 100644 --- a/deps/v8/include/v8-array-buffer.h +++ b/deps/v8/include/v8-array-buffer.h @@ -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, @@ -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 GetBackingStore(); diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index cf755cafc2cb41..b54e554217329b 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -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 ToInternal( std::shared_ptr backing_store) { diff --git a/deps/v8/test/cctest/cctest.status b/deps/v8/test/cctest/cctest.status index 7bfa3867d63dc1..b225460be093bf 100644 --- a/deps/v8/test/cctest/cctest.status +++ b/deps/v8/test/cctest/cctest.status @@ -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], diff --git a/deps/v8/test/cctest/test-api-array-buffer.cc b/deps/v8/test/cctest/test-api-array-buffer.cc index b087274b31137d..44ca7e3004e599 100644 --- a/deps/v8/test/cctest/test-api-array-buffer.cc +++ b/deps/v8/test/cctest/test-api-array-buffer.cc @@ -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 ab = v8::ArrayBuffer::New(isolate, 0); + CHECK(!ab->WasDetached()); + + ab->Detach(v8::Local()).Check(); + 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 non_detachable = + CompileRun("wasmMemory.buffer").As(); + 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(); From 4562373508e7453bac0121cfca5c10ce0d18f6e0 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 16 Nov 2022 10:20:54 -0500 Subject: [PATCH 2/2] deps: V8: remove detach check --- deps/v8/test/cctest/test-api-array-buffer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/v8/test/cctest/test-api-array-buffer.cc b/deps/v8/test/cctest/test-api-array-buffer.cc index 44ca7e3004e599..dff69296908cca 100644 --- a/deps/v8/test/cctest/test-api-array-buffer.cc +++ b/deps/v8/test/cctest/test-api-array-buffer.cc @@ -253,7 +253,7 @@ THREADED_TEST(ArrayBuffer_WasDetached) { Local ab = v8::ArrayBuffer::New(isolate, 0); CHECK(!ab->WasDetached()); - ab->Detach(v8::Local()).Check(); + ab->Detach(); CHECK(ab->WasDetached()); }