Skip to content

Commit aa7267a

Browse files
committed
deps: V8: cherry-pick e29c62b74854
Original commit message: [arraybuffer] Clean up BackingStore even if it pointer to nullptr For a zero-length BackingStore allocation, it is valid for the underlying memory to be a null pointer. However, some cleanup is still necessary, since the BackingStore may hold a reference to the allocator itself, which needs to be released when destroying the `BackingStore` instance. Change-Id: I1f168079d39e4592d2fde31fbe5f705586690e85 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2169646 Reviewed-by: Ulan Degenbaev <[email protected]> Commit-Queue: Ulan Degenbaev <[email protected]> Cr-Commit-Position: refs/heads/master@{#67420} Refs: v8/v8@e29c62b Backport-PR-URL: #33376 PR-URL: #32831 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 1512757 commit aa7267a

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.11',
39+
'v8_embedder_string': '-node.12',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/src/objects/backing-store.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ void BackingStore::Clear() {
165165
BackingStore::~BackingStore() {
166166
GlobalBackingStoreRegistry::Unregister(this);
167167

168-
if (buffer_start_ == nullptr) return; // nothing to deallocate
168+
if (buffer_start_ == nullptr) {
169+
Clear();
170+
return;
171+
}
169172

170173
if (is_wasm_memory_) {
171174
DCHECK(free_on_destruct_);

deps/v8/test/cctest/test-api-array-buffer.cc

+40
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,46 @@ TEST(BackingStore_HoldAllocatorAlive_AfterIsolateShutdown) {
799799
CHECK(allocator_weak.expired());
800800
}
801801

802+
class NullptrAllocator final : public v8::ArrayBuffer::Allocator {
803+
public:
804+
void* Allocate(size_t length) override {
805+
CHECK_EQ(length, 0);
806+
return nullptr;
807+
}
808+
void* AllocateUninitialized(size_t length) override {
809+
CHECK_EQ(length, 0);
810+
return nullptr;
811+
}
812+
void Free(void* data, size_t length) override { CHECK_EQ(data, nullptr); }
813+
};
814+
815+
TEST(BackingStore_ReleaseAllocator_NullptrBackingStore) {
816+
std::shared_ptr<NullptrAllocator> allocator =
817+
std::make_shared<NullptrAllocator>();
818+
std::weak_ptr<NullptrAllocator> allocator_weak(allocator);
819+
820+
v8::Isolate::CreateParams create_params;
821+
create_params.array_buffer_allocator_shared = allocator;
822+
v8::Isolate* isolate = v8::Isolate::New(create_params);
823+
isolate->Enter();
824+
825+
allocator.reset();
826+
create_params.array_buffer_allocator_shared.reset();
827+
CHECK(!allocator_weak.expired());
828+
829+
{
830+
std::shared_ptr<v8::BackingStore> backing_store =
831+
v8::ArrayBuffer::NewBackingStore(isolate, 0);
832+
// This should release a reference to the allocator, even though the
833+
// buffer is empty/nullptr.
834+
backing_store.reset();
835+
}
836+
837+
isolate->Exit();
838+
isolate->Dispose();
839+
CHECK(allocator_weak.expired());
840+
}
841+
802842
TEST(BackingStore_ReallocateExpand) {
803843
LocalContext env;
804844
v8::Isolate* isolate = env->GetIsolate();

0 commit comments

Comments
 (0)