v8/test/unittests/objects/backing-store-unittest.cc
Ulan Degenbaev b6b7de0d60 Reland x6 [arraybuffer] Rearchitect backing store ownership
This reverts commit 9da3483136

Original change's description:
> "Reland x4 [arraybuffer] Rearchitect backing store ownership"
>
> This is a reland of bc33f5aeba
>
> Contributed by titzer@chromium.org
>
> Original change's description:
> > [arraybuffer] Rearchitect backing store ownership
> >
> > This CL completely rearchitects the ownership of array buffer backing stores,
> > consolidating ownership into a {BackingStore} C++ object that is tracked
> > throughout V8 using unique_ptr and shared_ptr where appropriate.
> >
> > Overall, lifetime management is simpler and more explicit. The numerous
> > ways that array buffers were initialized have been streamlined to one
> > Attach() method on JSArrayBuffer. The array buffer tracker in the
> > GC implementation now manages std::shared_ptr<BackingStore> pointers,
> > and the construction and destruction of the BackingStore object itself
> > handles the underlying page or embedder-allocated memory.
> >
> > The embedder API remains unchanged for now. We use the
> > v8::ArrayBuffer::Contents struct to hide an additional shared_ptr to
> > keep the backing store alive properly, even in the case of aliases
> > from live heap objects. Thus the embedder has a lower chance of making
> > a mistake. Long-term, we should move the embedder to a model where they
> > manage backing stores using shared_ptr to an opaque backing store object.
>
> TBR=yangguo@chromium.org
>
> BUG=v8:9380,v8:9221,chromium:986318
>
> Change-Id: If671a4a9ca0476e8f084efae46e0d2bf99ed99ef
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1731005
> Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
> Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
> Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>

> Cr-Commit-Position: refs/heads/master@{#63041}

TBR=yangguo@chromium.org

Change-Id: I3cc4bb80081c662b1751234bc16a821c20e744be
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1792166
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63617}
2019-09-09 13:07:42 +00:00

129 lines
4.2 KiB
C++

// Copyright 2019 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 "src/objects/backing-store.h"
#include "src/base/platform/platform.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
class BackingStoreTest : public TestWithIsolate {};
TEST_F(BackingStoreTest, GrowWasmMemoryInPlace) {
auto backing_store =
BackingStore::AllocateWasmMemory(isolate(), 1, 2, SharedFlag::kNotShared);
CHECK(backing_store);
EXPECT_TRUE(backing_store->is_wasm_memory());
EXPECT_EQ(1 * wasm::kWasmPageSize, backing_store->byte_length());
EXPECT_EQ(2 * wasm::kWasmPageSize, backing_store->byte_capacity());
bool success = backing_store->GrowWasmMemoryInPlace(isolate(), 1, 2);
EXPECT_TRUE(success);
EXPECT_EQ(2 * wasm::kWasmPageSize, backing_store->byte_length());
}
TEST_F(BackingStoreTest, GrowWasmMemoryInPlace_neg) {
auto backing_store =
BackingStore::AllocateWasmMemory(isolate(), 1, 2, SharedFlag::kNotShared);
CHECK(backing_store);
EXPECT_TRUE(backing_store->is_wasm_memory());
EXPECT_EQ(1 * wasm::kWasmPageSize, backing_store->byte_length());
EXPECT_EQ(2 * wasm::kWasmPageSize, backing_store->byte_capacity());
bool success = backing_store->GrowWasmMemoryInPlace(isolate(), 2, 2);
EXPECT_FALSE(success);
EXPECT_EQ(1 * wasm::kWasmPageSize, backing_store->byte_length());
}
TEST_F(BackingStoreTest, GrowSharedWasmMemoryInPlace) {
auto backing_store =
BackingStore::AllocateWasmMemory(isolate(), 2, 3, SharedFlag::kShared);
CHECK(backing_store);
EXPECT_TRUE(backing_store->is_wasm_memory());
EXPECT_EQ(2 * wasm::kWasmPageSize, backing_store->byte_length());
EXPECT_EQ(3 * wasm::kWasmPageSize, backing_store->byte_capacity());
bool success = backing_store->GrowWasmMemoryInPlace(isolate(), 1, 3);
EXPECT_TRUE(success);
EXPECT_EQ(3 * wasm::kWasmPageSize, backing_store->byte_length());
}
TEST_F(BackingStoreTest, CopyWasmMemory) {
auto bs1 =
BackingStore::AllocateWasmMemory(isolate(), 1, 2, SharedFlag::kNotShared);
CHECK(bs1);
EXPECT_TRUE(bs1->is_wasm_memory());
EXPECT_EQ(1 * wasm::kWasmPageSize, bs1->byte_length());
EXPECT_EQ(2 * wasm::kWasmPageSize, bs1->byte_capacity());
auto bs2 = bs1->CopyWasmMemory(isolate(), 3);
EXPECT_TRUE(bs2->is_wasm_memory());
EXPECT_EQ(3 * wasm::kWasmPageSize, bs2->byte_length());
EXPECT_EQ(3 * wasm::kWasmPageSize, bs2->byte_capacity());
}
class GrowerThread : public base::Thread {
public:
GrowerThread(Isolate* isolate, uint32_t increment, uint32_t max,
std::shared_ptr<BackingStore> backing_store)
: base::Thread(base::Thread::Options("GrowerThread")),
isolate_(isolate),
increment_(increment),
max_(max),
backing_store_(backing_store) {}
void Run() override {
size_t max_length = max_ * wasm::kWasmPageSize;
while (true) {
size_t current_length = backing_store_->byte_length();
if (current_length >= max_length) break;
bool result =
backing_store_->GrowWasmMemoryInPlace(isolate_, increment_, max_);
size_t new_length = backing_store_->byte_length();
if (result) {
CHECK_GE(new_length, current_length + increment_);
} else {
CHECK_EQ(max_length, new_length);
}
}
}
private:
Isolate* isolate_;
uint32_t increment_;
uint32_t max_;
std::shared_ptr<BackingStore> backing_store_;
};
TEST_F(BackingStoreTest, RacyGrowWasmMemoryInPlace) {
constexpr int kNumThreads = 10;
constexpr int kMaxPages = 1024;
GrowerThread* threads[kNumThreads];
std::shared_ptr<BackingStore> backing_store =
BackingStore::AllocateWasmMemory(isolate(), 0, kMaxPages,
SharedFlag::kShared);
for (int i = 0; i < kNumThreads; i++) {
threads[i] = new GrowerThread(isolate(), 1, kMaxPages, backing_store);
CHECK(threads[i]->Start());
}
for (int i = 0; i < kNumThreads; i++) {
threads[i]->Join();
}
EXPECT_EQ(kMaxPages * wasm::kWasmPageSize, backing_store->byte_length());
for (int i = 0; i < kNumThreads; i++) {
delete threads[i];
}
}
} // namespace internal
} // namespace v8