Skip to content

Commit c5a4534

Browse files
addaleaxtargos
authored andcommitted
deps: V8: backport 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 <ulan@chromium.org> Commit-Queue: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#67420} Refs: v8/v8@e29c62b PR-URL: #33125 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
1 parent 89ed7a5 commit c5a4534

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
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.32',
39+
'v8_embedder_string': '-node.33',
4040

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ void BackingStore::Clear() {
164164
BackingStore::~BackingStore() {
165165
GlobalBackingStoreRegistry::Unregister(this);
166166

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

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

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,46 @@ TEST(BackingStore_HoldAllocatorAlive_UntilIsolateShutdown) {
729729
CHECK(allocator_weak.expired());
730730
}
731731

732+
class NullptrAllocator final : public v8::ArrayBuffer::Allocator {
733+
public:
734+
void* Allocate(size_t length) override {
735+
CHECK_EQ(length, 0);
736+
return nullptr;
737+
}
738+
void* AllocateUninitialized(size_t length) override {
739+
CHECK_EQ(length, 0);
740+
return nullptr;
741+
}
742+
void Free(void* data, size_t length) override { CHECK_EQ(data, nullptr); }
743+
};
744+
745+
TEST(BackingStore_ReleaseAllocator_NullptrBackingStore) {
746+
std::shared_ptr<NullptrAllocator> allocator =
747+
std::make_shared<NullptrAllocator>();
748+
std::weak_ptr<NullptrAllocator> allocator_weak(allocator);
749+
750+
v8::Isolate::CreateParams create_params;
751+
create_params.array_buffer_allocator_shared = allocator;
752+
v8::Isolate* isolate = v8::Isolate::New(create_params);
753+
isolate->Enter();
754+
755+
allocator.reset();
756+
create_params.array_buffer_allocator_shared.reset();
757+
CHECK(!allocator_weak.expired());
758+
759+
{
760+
std::shared_ptr<v8::BackingStore> backing_store =
761+
v8::ArrayBuffer::NewBackingStore(isolate, 0);
762+
// This should release a reference to the allocator, even though the
763+
// buffer is empty/nullptr.
764+
backing_store.reset();
765+
}
766+
767+
isolate->Exit();
768+
isolate->Dispose();
769+
CHECK(allocator_weak.expired());
770+
}
771+
732772
TEST(BackingStore_HoldAllocatorAlive_AfterIsolateShutdown) {
733773
std::shared_ptr<DummyAllocator> allocator =
734774
std::make_shared<DummyAllocator>();

0 commit comments

Comments
 (0)