2020-06-17 07:54:15 +00:00
|
|
|
// Copyright 2020 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 "include/cppgc/allocation.h"
|
|
|
|
#include "include/cppgc/garbage-collected.h"
|
2021-01-21 15:19:19 +00:00
|
|
|
#include "include/cppgc/persistent.h"
|
2020-06-17 07:54:15 +00:00
|
|
|
#include "include/cppgc/platform.h"
|
2021-01-21 11:03:56 +00:00
|
|
|
#include "include/v8-cppgc.h"
|
2021-01-21 15:19:19 +00:00
|
|
|
#include "include/v8.h"
|
2020-06-17 07:54:15 +00:00
|
|
|
#include "src/api/api-inl.h"
|
|
|
|
#include "src/heap/cppgc-js/cpp-heap.h"
|
2021-02-09 09:02:40 +00:00
|
|
|
#include "src/heap/cppgc/sweeper.h"
|
2020-06-17 07:54:15 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
|
|
|
#include "test/unittests/heap/heap-utils.h"
|
2020-10-22 07:02:17 +00:00
|
|
|
#include "test/unittests/heap/unified-heap-utils.h"
|
2020-06-17 07:54:15 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Wrappable final : public cppgc::GarbageCollected<Wrappable> {
|
|
|
|
public:
|
|
|
|
static size_t destructor_callcount;
|
|
|
|
|
|
|
|
~Wrappable() { destructor_callcount++; }
|
|
|
|
|
2021-01-21 15:19:19 +00:00
|
|
|
void Trace(cppgc::Visitor* visitor) const { visitor->Trace(wrapper_); }
|
|
|
|
|
|
|
|
void SetWrapper(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) {
|
|
|
|
wrapper_.Reset(isolate, wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
TracedReference<v8::Object>& wrapper() { return wrapper_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
TracedReference<v8::Object> wrapper_;
|
2020-06-17 07:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t Wrappable::destructor_callcount = 0;
|
|
|
|
|
2021-02-09 09:02:40 +00:00
|
|
|
using UnifiedHeapDetachedTest = TestWithHeapInternals;
|
|
|
|
|
2020-06-17 07:54:15 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-10-23 10:39:23 +00:00
|
|
|
TEST_F(UnifiedHeapTest, OnlyGC) { CollectGarbageWithEmbedderStack(); }
|
2020-06-17 07:54:15 +00:00
|
|
|
|
|
|
|
TEST_F(UnifiedHeapTest, FindingV8ToBlinkReference) {
|
|
|
|
v8::HandleScope scope(v8_isolate());
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(v8_isolate());
|
|
|
|
v8::Context::Scope context_scope(context);
|
2020-10-28 08:13:33 +00:00
|
|
|
v8::Local<v8::Object> api_object = WrapperHelper::CreateWrapper(
|
2020-06-17 07:54:15 +00:00
|
|
|
context, cppgc::MakeGarbageCollected<Wrappable>(allocation_handle()));
|
2021-01-21 11:03:56 +00:00
|
|
|
Wrappable::destructor_callcount = 0;
|
2020-06-17 07:54:15 +00:00
|
|
|
EXPECT_FALSE(api_object.IsEmpty());
|
|
|
|
EXPECT_EQ(0u, Wrappable::destructor_callcount);
|
2021-01-21 15:19:19 +00:00
|
|
|
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
|
2020-06-17 07:54:15 +00:00
|
|
|
EXPECT_EQ(0u, Wrappable::destructor_callcount);
|
2020-10-28 08:13:33 +00:00
|
|
|
WrapperHelper::ResetWrappableConnection(api_object);
|
2021-01-21 15:19:19 +00:00
|
|
|
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
|
2020-06-17 07:54:15 +00:00
|
|
|
EXPECT_EQ(1u, Wrappable::destructor_callcount);
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:19:19 +00:00
|
|
|
TEST_F(UnifiedHeapTest, WriteBarrierV8ToCppReference) {
|
2021-01-21 11:03:56 +00:00
|
|
|
v8::HandleScope scope(v8_isolate());
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(v8_isolate());
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
void* wrappable = cppgc::MakeGarbageCollected<Wrappable>(allocation_handle());
|
|
|
|
v8::Local<v8::Object> api_object =
|
|
|
|
WrapperHelper::CreateWrapper(context, wrappable);
|
|
|
|
Wrappable::destructor_callcount = 0;
|
|
|
|
WrapperHelper::ResetWrappableConnection(api_object);
|
|
|
|
SimulateIncrementalMarking();
|
|
|
|
{
|
|
|
|
// The following snippet shows the embedder code for implementing a GC-safe
|
|
|
|
// setter for JS to C++ references.
|
|
|
|
WrapperHelper::SetWrappableConnection(api_object, wrappable, wrappable);
|
|
|
|
JSHeapConsistency::WriteBarrierParams params;
|
|
|
|
auto barrier_type = JSHeapConsistency::GetWriteBarrierType(
|
2021-02-05 14:05:48 +00:00
|
|
|
api_object, 1, wrappable, params,
|
|
|
|
[this]() -> cppgc::HeapHandle& { return cpp_heap().GetHeapHandle(); });
|
2021-01-21 11:03:56 +00:00
|
|
|
EXPECT_EQ(JSHeapConsistency::WriteBarrierType::kMarking, barrier_type);
|
|
|
|
JSHeapConsistency::DijkstraMarkingBarrier(
|
|
|
|
params, cpp_heap().GetHeapHandle(), wrappable);
|
|
|
|
}
|
2021-01-21 15:19:19 +00:00
|
|
|
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
|
|
|
|
EXPECT_EQ(0u, Wrappable::destructor_callcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(UnifiedHeapTest, WriteBarrierCppToV8Reference) {
|
|
|
|
v8::HandleScope scope(v8_isolate());
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(v8_isolate());
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
cppgc::Persistent<Wrappable> wrappable =
|
|
|
|
cppgc::MakeGarbageCollected<Wrappable>(allocation_handle());
|
|
|
|
Wrappable::destructor_callcount = 0;
|
|
|
|
SimulateIncrementalMarking();
|
|
|
|
// Pick a sentinel to compare against.
|
|
|
|
void* kMagicAddress = &Wrappable::destructor_callcount;
|
|
|
|
{
|
|
|
|
// The following snippet shows the embedder code for implementing a GC-safe
|
|
|
|
// setter for C++ to JS references.
|
|
|
|
v8::HandleScope nested_scope(v8_isolate());
|
|
|
|
v8::Local<v8::Object> api_object =
|
|
|
|
WrapperHelper::CreateWrapper(context, nullptr);
|
|
|
|
// Setting only one field to avoid treating this as wrappable backref, see
|
|
|
|
// `LocalEmbedderHeapTracer::ExtractWrapperInfo`.
|
|
|
|
api_object->SetAlignedPointerInInternalField(1, kMagicAddress);
|
|
|
|
wrappable->SetWrapper(v8_isolate(), api_object);
|
|
|
|
JSHeapConsistency::WriteBarrierParams params;
|
2021-02-05 14:05:48 +00:00
|
|
|
auto barrier_type = JSHeapConsistency::GetWriteBarrierType(
|
|
|
|
wrappable->wrapper(), params,
|
|
|
|
[this]() -> cppgc::HeapHandle& { return cpp_heap().GetHeapHandle(); });
|
2021-01-21 15:19:19 +00:00
|
|
|
EXPECT_EQ(JSHeapConsistency::WriteBarrierType::kMarking, barrier_type);
|
|
|
|
JSHeapConsistency::DijkstraMarkingBarrier(
|
|
|
|
params, cpp_heap().GetHeapHandle(), wrappable->wrapper());
|
|
|
|
}
|
|
|
|
CollectGarbageWithoutEmbedderStack(cppgc::Heap::SweepingType::kAtomic);
|
2021-01-21 11:03:56 +00:00
|
|
|
EXPECT_EQ(0u, Wrappable::destructor_callcount);
|
2021-01-21 15:19:19 +00:00
|
|
|
EXPECT_EQ(kMagicAddress,
|
|
|
|
wrappable->wrapper()->GetAlignedPointerFromInternalField(1));
|
2021-01-21 11:03:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 09:02:40 +00:00
|
|
|
TEST_F(UnifiedHeapDetachedTest, AllocationBeforeConfigureHeap) {
|
|
|
|
auto heap =
|
|
|
|
v8::CppHeap::Create(V8::GetCurrentPlatform(), CppHeapCreateParams{});
|
|
|
|
auto* object =
|
|
|
|
cppgc::MakeGarbageCollected<Wrappable>(heap->GetAllocationHandle());
|
|
|
|
cppgc::WeakPersistent<Wrappable> weak_holder{object};
|
|
|
|
|
|
|
|
auto& js_heap = *isolate()->heap();
|
|
|
|
js_heap.AttachCppHeap(heap.get());
|
|
|
|
auto& cpp_heap = *CppHeap::From(isolate()->heap()->cpp_heap());
|
|
|
|
{
|
|
|
|
CollectGarbage(OLD_SPACE);
|
|
|
|
cpp_heap.AsBase().sweeper().FinishIfRunning();
|
|
|
|
EXPECT_TRUE(weak_holder);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
js_heap.SetEmbedderStackStateForNextFinalization(
|
|
|
|
EmbedderHeapTracer::EmbedderStackState::kNoHeapPointers);
|
|
|
|
CollectGarbage(OLD_SPACE);
|
|
|
|
cpp_heap.AsBase().sweeper().FinishIfRunning();
|
|
|
|
EXPECT_FALSE(weak_holder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:54:15 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|