2013-04-12 09:54:51 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2019-05-16 15:00:03 +00:00
|
|
|
#include "src/api-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/global-handles.h"
|
2018-04-09 19:11:22 +00:00
|
|
|
#include "src/heap/factory.h"
|
2019-02-14 21:10:30 +00:00
|
|
|
#include "src/heap/heap-inl.h"
|
2016-09-01 12:01:33 +00:00
|
|
|
#include "src/isolate.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2019-01-29 19:12:21 +00:00
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
2013-04-12 09:54:51 +00:00
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2013-04-12 09:54:51 +00:00
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
// Empty v8::EmbedderHeapTracer that never keeps objects alive on Scavenge. See
|
|
|
|
// |IsRootForNonTracingGC|.
|
|
|
|
class NonRootingEmbedderHeapTracer final : public v8::EmbedderHeapTracer {
|
|
|
|
public:
|
|
|
|
NonRootingEmbedderHeapTracer() = default;
|
|
|
|
|
|
|
|
void RegisterV8References(
|
|
|
|
const std::vector<std::pair<void*, void*>>& embedder_fields) final {}
|
|
|
|
bool AdvanceTracing(double deadline_in_ms) final { return true; }
|
|
|
|
bool IsTracingDone() final { return true; }
|
|
|
|
void TracePrologue() final {}
|
|
|
|
void TraceEpilogue() final {}
|
|
|
|
void EnterFinalPause(EmbedderStackState) final {}
|
|
|
|
|
|
|
|
bool IsRootForNonTracingGC(const v8::TracedGlobal<v8::Value>& handle) final {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-20 19:10:36 +00:00
|
|
|
void InvokeScavenge() { CcTest::CollectGarbage(i::NEW_SPACE); }
|
|
|
|
|
|
|
|
void InvokeMarkSweep() { CcTest::CollectAllGarbage(); }
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
void SimpleCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
|
info.GetReturnValue().Set(v8_num(0));
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
struct FlagAndGlobal {
|
2017-11-09 16:27:34 +00:00
|
|
|
bool flag;
|
|
|
|
v8::Global<v8::Object> handle;
|
|
|
|
};
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
struct TracedGlobalWrapper {
|
|
|
|
v8::TracedGlobal<v8::Object> handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
void ResetHandleAndSetFlag(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2017-11-09 16:27:34 +00:00
|
|
|
data.GetParameter()->handle.Reset();
|
|
|
|
data.GetParameter()->flag = true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
template <typename HandleContainer>
|
2017-11-09 16:27:34 +00:00
|
|
|
void ConstructJSObject(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
2019-04-23 15:04:17 +00:00
|
|
|
HandleContainer* flag_and_persistent) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Object> object(v8::Object::New(isolate));
|
|
|
|
CHECK(!object.IsEmpty());
|
|
|
|
flag_and_persistent->handle.Reset(isolate, object);
|
|
|
|
CHECK(!flag_and_persistent->handle.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:31:00 +00:00
|
|
|
void ConstructJSObject(v8::Isolate* isolate, v8::Global<v8::Object>* global) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> object(v8::Object::New(isolate));
|
|
|
|
CHECK(!object.IsEmpty());
|
|
|
|
*global = v8::Global<v8::Object>(isolate, object);
|
|
|
|
CHECK(!global->IsEmpty());
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
template <typename HandleContainer>
|
2017-11-09 16:27:34 +00:00
|
|
|
void ConstructJSApiObject(v8::Isolate* isolate, v8::Local<v8::Context> context,
|
2019-04-23 15:04:17 +00:00
|
|
|
HandleContainer* flag_and_persistent) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::FunctionTemplate> fun =
|
|
|
|
v8::FunctionTemplate::New(isolate, SimpleCallback);
|
|
|
|
v8::Local<v8::Object> object = fun->GetFunction(context)
|
|
|
|
.ToLocalChecked()
|
|
|
|
->NewInstance(context)
|
|
|
|
.ToLocalChecked();
|
|
|
|
CHECK(!object.IsEmpty());
|
|
|
|
flag_and_persistent->handle.Reset(isolate, object);
|
|
|
|
CHECK(!flag_and_persistent->handle.IsEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class SurvivalMode { kSurvives, kDies };
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
template <typename ConstructFunction, typename ModifierFunction,
|
|
|
|
typename GCFunction>
|
2017-11-09 16:27:34 +00:00
|
|
|
void WeakHandleTest(v8::Isolate* isolate, ConstructFunction construct_function,
|
|
|
|
ModifierFunction modifier_function, GCFunction gc_function,
|
|
|
|
SurvivalMode survives) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
FlagAndGlobal fp;
|
2017-11-09 16:27:34 +00:00
|
|
|
construct_function(isolate, context, &fp);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(heap::InYoungGeneration(isolate, fp.handle));
|
2017-11-09 16:27:34 +00:00
|
|
|
fp.handle.SetWeak(&fp, &ResetHandleAndSetFlag,
|
|
|
|
v8::WeakCallbackType::kParameter);
|
|
|
|
fp.flag = false;
|
|
|
|
modifier_function(&fp);
|
|
|
|
gc_function();
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kSurvives, !fp.flag);
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kDies, fp.flag);
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
template <typename ConstructFunction, typename ModifierFunction,
|
|
|
|
typename GCFunction>
|
|
|
|
void TracedGlobalTest(v8::Isolate* isolate,
|
|
|
|
ConstructFunction construct_function,
|
|
|
|
ModifierFunction modifier_function,
|
|
|
|
GCFunction gc_function, SurvivalMode survives) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
NonRootingEmbedderHeapTracer tracer;
|
|
|
|
heap::TemporaryEmbedderHeapTracerScope tracer_scope(isolate, &tracer);
|
|
|
|
|
|
|
|
TracedGlobalWrapper fp;
|
|
|
|
construct_function(isolate, context, &fp);
|
|
|
|
CHECK(heap::InYoungGeneration(isolate, fp.handle));
|
|
|
|
modifier_function(&fp);
|
|
|
|
gc_function();
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kSurvives, !fp.handle.IsEmpty());
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kDies, fp.handle.IsEmpty());
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
void ResurrectingFinalizer(
|
|
|
|
const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
|
|
|
|
data.GetParameter()->ClearWeak();
|
|
|
|
}
|
|
|
|
|
2017-11-20 14:07:15 +00:00
|
|
|
void ResettingFinalizer(
|
|
|
|
const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
|
|
|
|
data.GetParameter()->Reset();
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
void EmptyWeakCallback(const v8::WeakCallbackInfo<void>& data) {}
|
|
|
|
|
|
|
|
void ResurrectingFinalizerSettingProperty(
|
|
|
|
const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
|
|
|
|
data.GetParameter()->ClearWeak();
|
|
|
|
v8::Local<v8::Object> o =
|
|
|
|
v8::Local<v8::Object>::New(data.GetIsolate(), *data.GetParameter());
|
|
|
|
o->Set(data.GetIsolate()->GetCurrentContext(), v8_str("finalizer"),
|
|
|
|
v8_str("was here"))
|
|
|
|
.FromJust();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2013-08-05 09:46:23 +00:00
|
|
|
TEST(EternalHandles) {
|
|
|
|
CcTest::InitializeVM();
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-08-05 09:46:23 +00:00
|
|
|
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
2013-08-26 09:41:22 +00:00
|
|
|
EternalHandles* eternal_handles = isolate->eternal_handles();
|
2013-08-05 09:46:23 +00:00
|
|
|
|
|
|
|
// Create a number of handles that will not be on a block boundary
|
|
|
|
const int kArrayLength = 2048-1;
|
|
|
|
int indices[kArrayLength];
|
2013-08-26 09:41:22 +00:00
|
|
|
v8::Eternal<v8::Value> eternals[kArrayLength];
|
2013-08-05 09:46:23 +00:00
|
|
|
|
2019-01-21 12:51:17 +00:00
|
|
|
CHECK_EQ(0, eternal_handles->handles_count());
|
2013-08-05 09:46:23 +00:00
|
|
|
for (int i = 0; i < kArrayLength; i++) {
|
2013-08-26 10:11:43 +00:00
|
|
|
indices[i] = -1;
|
2013-08-05 09:46:23 +00:00
|
|
|
HandleScope scope(isolate);
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Local<v8::Object> object = v8::Object::New(v8_isolate);
|
2015-11-17 12:42:14 +00:00
|
|
|
object->Set(v8_isolate->GetCurrentContext(), i,
|
|
|
|
v8::Integer::New(v8_isolate, i))
|
|
|
|
.FromJust();
|
2013-08-26 09:41:22 +00:00
|
|
|
// Create with internal api
|
|
|
|
eternal_handles->Create(
|
|
|
|
isolate, *v8::Utils::OpenHandle(*object), &indices[i]);
|
|
|
|
// Create with external api
|
|
|
|
CHECK(eternals[i].IsEmpty());
|
2013-09-04 08:19:14 +00:00
|
|
|
eternals[i].Set(v8_isolate, object);
|
|
|
|
CHECK(!eternals[i].IsEmpty());
|
2013-08-05 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 10:02:58 +00:00
|
|
|
CcTest::CollectAllAvailableGarbage();
|
2013-08-05 09:46:23 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < kArrayLength; i++) {
|
2013-08-07 08:26:23 +00:00
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
HandleScope scope(isolate);
|
2013-08-26 09:41:22 +00:00
|
|
|
v8::Local<v8::Value> local;
|
2013-08-07 08:26:23 +00:00
|
|
|
if (j == 0) {
|
|
|
|
// Test internal api
|
2013-08-26 09:41:22 +00:00
|
|
|
local = v8::Utils::ToLocal(eternal_handles->Get(indices[i]));
|
2013-08-07 08:26:23 +00:00
|
|
|
} else {
|
|
|
|
// Test external api
|
2013-08-26 09:41:22 +00:00
|
|
|
local = eternals[i].Get(v8_isolate);
|
2013-08-07 08:26:23 +00:00
|
|
|
}
|
2015-11-17 12:42:14 +00:00
|
|
|
v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(local);
|
|
|
|
v8::Local<v8::Value> value =
|
|
|
|
object->Get(v8_isolate->GetCurrentContext(), i).ToLocalChecked();
|
2013-08-07 08:26:23 +00:00
|
|
|
CHECK(value->IsInt32());
|
2015-11-17 12:42:14 +00:00
|
|
|
CHECK_EQ(i,
|
|
|
|
value->Int32Value(v8_isolate->GetCurrentContext()).FromJust());
|
2013-08-07 08:26:23 +00:00
|
|
|
}
|
2013-08-05 09:46:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 12:51:17 +00:00
|
|
|
CHECK_EQ(2 * kArrayLength, eternal_handles->handles_count());
|
2013-08-26 09:41:22 +00:00
|
|
|
|
|
|
|
// Create an eternal via the constructor
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
2014-01-03 14:31:17 +00:00
|
|
|
v8::Local<v8::Object> object = v8::Object::New(v8_isolate);
|
2013-08-26 09:41:22 +00:00
|
|
|
v8::Eternal<v8::Object> eternal(v8_isolate, object);
|
2013-09-04 08:19:14 +00:00
|
|
|
CHECK(!eternal.IsEmpty());
|
2013-08-26 09:41:22 +00:00
|
|
|
CHECK(object == eternal.Get(v8_isolate));
|
|
|
|
}
|
|
|
|
|
2019-01-21 12:51:17 +00:00
|
|
|
CHECK_EQ(2 * kArrayLength + 1, eternal_handles->handles_count());
|
2013-08-05 09:46:23 +00:00
|
|
|
}
|
2015-07-13 15:02:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
TEST(PersistentBaseGetLocal) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> o = v8::Object::New(isolate);
|
|
|
|
CHECK(!o.IsEmpty());
|
|
|
|
v8::Persistent<v8::Object> p(isolate, o);
|
|
|
|
CHECK(o == p.Get(isolate));
|
|
|
|
CHECK(v8::Local<v8::Object>::New(isolate, p) == p.Get(isolate));
|
|
|
|
|
|
|
|
v8::Global<v8::Object> g(isolate, o);
|
|
|
|
CHECK(o == g.Get(isolate));
|
|
|
|
CHECK(v8::Local<v8::Object>::New(isolate, g) == g.Get(isolate));
|
|
|
|
}
|
2016-01-25 13:09:27 +00:00
|
|
|
|
|
|
|
TEST(WeakPersistentSmi) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Number> n = v8::Number::New(isolate, 0);
|
|
|
|
v8::Global<v8::Number> g(isolate, n);
|
|
|
|
|
|
|
|
// Should not crash.
|
2017-11-09 16:27:34 +00:00
|
|
|
g.SetWeak<void>(nullptr, &EmptyWeakCallback,
|
|
|
|
v8::WeakCallbackType::kParameter);
|
2016-04-15 12:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FinalizerWeakness) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
|
|
|
|
v8::Global<v8::Object> g;
|
|
|
|
int identity;
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> o = v8::Object::New(isolate);
|
|
|
|
identity = o->GetIdentityHash();
|
|
|
|
g.Reset(isolate, o);
|
2017-11-09 16:27:34 +00:00
|
|
|
g.SetWeak(&g, &ResurrectingFinalizerSettingProperty,
|
|
|
|
v8::WeakCallbackType::kFinalizer);
|
2016-04-15 12:57:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 10:02:58 +00:00
|
|
|
CcTest::CollectAllAvailableGarbage();
|
2016-04-15 12:57:41 +00:00
|
|
|
|
|
|
|
CHECK(!g.IsEmpty());
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> o = v8::Local<v8::Object>::New(isolate, g);
|
|
|
|
CHECK_EQ(identity, o->GetIdentityHash());
|
|
|
|
CHECK(o->Has(isolate->GetCurrentContext(), v8_str("finalizer")).FromJust());
|
|
|
|
}
|
2016-05-09 07:15:43 +00:00
|
|
|
|
|
|
|
TEST(PhatomHandlesWithoutCallbacks) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
|
|
|
|
v8::Global<v8::Object> g1, g2;
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
g1.Reset(isolate, v8::Object::New(isolate));
|
|
|
|
g1.SetWeak();
|
|
|
|
g2.Reset(isolate, v8::Object::New(isolate));
|
|
|
|
g2.SetWeak();
|
|
|
|
}
|
|
|
|
|
2016-11-11 14:55:47 +00:00
|
|
|
CHECK_EQ(0u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
|
2016-09-07 10:02:58 +00:00
|
|
|
CcTest::CollectAllAvailableGarbage();
|
2016-11-11 14:55:47 +00:00
|
|
|
CHECK_EQ(2u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
|
|
|
|
CHECK_EQ(0u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
|
2016-05-09 07:15:43 +00:00
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TEST(WeakHandleToUnmodifiedJSObjectDiesOnScavenge) {
|
2017-11-09 16:27:34 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, []() { InvokeScavenge(); },
|
|
|
|
SurvivalMode::kDies);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TracedGlobalToUnmodifiedJSObjectSurvivesScavenge) {
|
|
|
|
ManualGCScope manual_gc;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
TracedGlobalTest(
|
|
|
|
CcTest::isolate(), &ConstructJSObject<TracedGlobalWrapper>,
|
|
|
|
[](TracedGlobalWrapper* fp) {}, []() { InvokeScavenge(); },
|
|
|
|
SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
2017-11-02 17:38:23 +00:00
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
TEST(WeakHandleToUnmodifiedJSObjectDiesOnMarkCompact) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, []() { InvokeMarkSweep(); },
|
|
|
|
SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakHandleToUnmodifiedJSObjectSurvivesMarkCompactWhenInHandle) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
|
|
|
v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
|
|
|
|
USE(handle);
|
|
|
|
},
|
2018-12-20 19:10:36 +00:00
|
|
|
[]() { InvokeMarkSweep(); }, SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnScavenge) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, []() { InvokeScavenge(); },
|
|
|
|
SurvivalMode::kDies);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TracedGlobalToUnmodifiedJSApiObjectDiesOnScavenge) {
|
|
|
|
ManualGCScope manual_gc;
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
TracedGlobalTest(
|
|
|
|
CcTest::isolate(), &ConstructJSApiObject<TracedGlobalWrapper>,
|
|
|
|
[](TracedGlobalWrapper* fp) {}, []() { InvokeScavenge(); },
|
|
|
|
SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TEST(TracedGlobalToJSApiObjectWithIdentityHashSurvivesScavenge) {
|
|
|
|
ManualGCScope manual_gc;
|
2019-04-08 09:43:10 +00:00
|
|
|
CcTest::InitializeVM();
|
2019-04-09 11:54:43 +00:00
|
|
|
Isolate* i_isolate = CcTest::i_isolate();
|
|
|
|
HandleScope scope(i_isolate);
|
|
|
|
Handle<JSWeakMap> weakmap = i_isolate->factory()->NewJSWeakMap();
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TracedGlobalTest(
|
|
|
|
CcTest::isolate(), &ConstructJSApiObject<TracedGlobalWrapper>,
|
|
|
|
[&weakmap, i_isolate](TracedGlobalWrapper* fp) {
|
2019-04-08 09:43:10 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2019-04-09 11:54:43 +00:00
|
|
|
Handle<JSReceiver> key =
|
|
|
|
Utils::OpenHandle(*fp->handle.Get(CcTest::isolate()));
|
|
|
|
Handle<Smi> smi(Smi::FromInt(23), i_isolate);
|
|
|
|
int32_t hash = key->GetOrCreateHash(i_isolate)->value();
|
|
|
|
JSWeakCollection::Set(weakmap, key, smi, hash);
|
2019-04-08 09:43:10 +00:00
|
|
|
},
|
|
|
|
[]() { InvokeScavenge(); }, SurvivalMode::kSurvives);
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesScavengeWhenInHandle) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
|
|
|
v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
|
|
|
|
USE(handle);
|
|
|
|
},
|
2018-12-20 19:10:36 +00:00
|
|
|
[]() { InvokeScavenge(); }, SurvivalMode::kSurvives);
|
2017-11-02 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnMarkCompact) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, []() { InvokeMarkSweep(); },
|
|
|
|
SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesMarkCompactWhenInHandle) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
WeakHandleTest(
|
2019-04-23 15:04:17 +00:00
|
|
|
CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
|
|
|
v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
|
|
|
|
USE(handle);
|
|
|
|
},
|
2018-12-20 19:10:36 +00:00
|
|
|
[]() { InvokeMarkSweep(); }, SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TEST(TracedGlobalToJSApiObjectWithModifiedMapSurvivesScavenge) {
|
2017-11-09 16:27:34 +00:00
|
|
|
CcTest::InitializeVM();
|
2019-04-23 15:04:17 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
LocalContext context;
|
2017-11-09 16:27:34 +00:00
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TracedGlobal<v8::Object> handle;
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
// Create an API object which does not have the same map as constructor.
|
|
|
|
auto function_template = FunctionTemplate::New(isolate);
|
|
|
|
auto instance_t = function_template->InstanceTemplate();
|
|
|
|
instance_t->Set(
|
|
|
|
v8::String::NewFromUtf8(isolate, "a", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
v8::Number::New(isolate, 10));
|
|
|
|
auto function =
|
|
|
|
function_template->GetFunction(context.local()).ToLocalChecked();
|
|
|
|
auto i = function->NewInstance(context.local()).ToLocalChecked();
|
|
|
|
handle.Reset(isolate, i);
|
|
|
|
}
|
|
|
|
InvokeScavenge();
|
|
|
|
CHECK(!handle.IsEmpty());
|
2019-04-23 14:56:38 +00:00
|
|
|
}
|
2019-04-23 12:53:34 +00:00
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
TEST(TracedGlobalTOJsApiObjectWithElementsSurvivesScavenge) {
|
2019-04-23 14:56:38 +00:00
|
|
|
CcTest::InitializeVM();
|
2019-04-23 15:04:17 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
LocalContext context;
|
|
|
|
|
|
|
|
TracedGlobal<v8::Object> handle;
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
// Create an API object which has elements.
|
|
|
|
auto function_template = FunctionTemplate::New(isolate);
|
|
|
|
auto instance_t = function_template->InstanceTemplate();
|
|
|
|
instance_t->Set(
|
|
|
|
v8::String::NewFromUtf8(isolate, "1", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
v8::Number::New(isolate, 10));
|
|
|
|
instance_t->Set(
|
|
|
|
v8::String::NewFromUtf8(isolate, "2", NewStringType::kNormal)
|
|
|
|
.ToLocalChecked(),
|
|
|
|
v8::Number::New(isolate, 10));
|
|
|
|
auto function =
|
|
|
|
function_template->GetFunction(context.local()).ToLocalChecked();
|
|
|
|
auto i = function->NewInstance(context.local()).ToLocalChecked();
|
|
|
|
handle.Reset(isolate, i);
|
|
|
|
}
|
|
|
|
InvokeScavenge();
|
|
|
|
CHECK(!handle.IsEmpty());
|
2018-12-20 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(FinalizerOnUnmodifiedJSApiObjectDoesNotCrash) {
|
|
|
|
// See crbug.com/v8/8586.
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
FlagAndGlobal fp;
|
2018-12-20 19:10:36 +00:00
|
|
|
ConstructJSApiObject(isolate, context, &fp);
|
|
|
|
fp.handle.SetWeak(&fp, &ResetHandleAndSetFlag,
|
|
|
|
v8::WeakCallbackType::kFinalizer);
|
|
|
|
fp.flag = false;
|
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> tmp = v8::Local<v8::Object>::New(isolate, fp.handle);
|
|
|
|
USE(tmp);
|
|
|
|
InvokeScavenge();
|
|
|
|
}
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-11-20 14:07:15 +00:00
|
|
|
void ConstructFinalizerPointingPhantomHandle(
|
|
|
|
v8::Isolate* isolate, v8::Global<v8::Object>* g1,
|
|
|
|
v8::Global<v8::Object>* g2,
|
|
|
|
typename v8::WeakCallbackInfo<v8::Global<v8::Object>>::Callback
|
|
|
|
finalizer_for_g1) {
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> o1 =
|
|
|
|
v8::Local<v8::Object>::New(isolate, v8::Object::New(isolate));
|
|
|
|
v8::Local<v8::Object> o2 =
|
|
|
|
v8::Local<v8::Object>::New(isolate, v8::Object::New(isolate));
|
|
|
|
o1->Set(isolate->GetCurrentContext(), v8_str("link"), o2).FromJust();
|
|
|
|
g1->Reset(isolate, o1);
|
|
|
|
g2->Reset(isolate, o2);
|
|
|
|
// g1 will be finalized but resurrected.
|
|
|
|
g1->SetWeak(g1, finalizer_for_g1, v8::WeakCallbackType::kFinalizer);
|
|
|
|
// g2 will be a phantom handle that is dependent on the finalizer handle
|
|
|
|
// g1 as it is in its subgraph.
|
|
|
|
g2->SetWeak();
|
2017-11-02 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 18:12:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-11-20 14:07:15 +00:00
|
|
|
TEST(FinalizerResurrectsAndKeepsPhantomAliveOnMarkCompact) {
|
2017-11-06 18:12:34 +00:00
|
|
|
// See crbug.com/772299.
|
|
|
|
CcTest::InitializeVM();
|
2017-11-20 14:07:15 +00:00
|
|
|
v8::Global<v8::Object> g1, g2;
|
|
|
|
ConstructFinalizerPointingPhantomHandle(CcTest::isolate(), &g1, &g2,
|
|
|
|
ResurrectingFinalizer);
|
2018-12-20 19:10:36 +00:00
|
|
|
InvokeMarkSweep();
|
2017-11-20 14:07:15 +00:00
|
|
|
// Both, g1 and g2, should stay alive as the finalizer resurrects the root
|
|
|
|
// object that transitively keeps the other one alive.
|
|
|
|
CHECK(!g1.IsEmpty());
|
|
|
|
CHECK(!g2.IsEmpty());
|
2018-12-20 19:10:36 +00:00
|
|
|
InvokeMarkSweep();
|
2017-11-20 14:07:15 +00:00
|
|
|
// The finalizer handle is now strong, so it should keep the objects alive.
|
|
|
|
CHECK(!g1.IsEmpty());
|
|
|
|
CHECK(!g2.IsEmpty());
|
2017-11-06 18:12:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 14:07:15 +00:00
|
|
|
TEST(FinalizerDiesAndKeepsPhantomAliveOnMarkCompact) {
|
2017-11-06 18:12:34 +00:00
|
|
|
CcTest::InitializeVM();
|
2017-11-20 14:07:15 +00:00
|
|
|
v8::Global<v8::Object> g1, g2;
|
|
|
|
ConstructFinalizerPointingPhantomHandle(CcTest::isolate(), &g1, &g2,
|
|
|
|
ResettingFinalizer);
|
2018-12-20 19:10:36 +00:00
|
|
|
InvokeMarkSweep();
|
2017-11-20 14:07:15 +00:00
|
|
|
// Finalizer (g1) dies but the phantom handle (g2) is kept alive for one
|
|
|
|
// more round as the underlying object only dies on the next GC.
|
|
|
|
CHECK(g1.IsEmpty());
|
|
|
|
CHECK(!g2.IsEmpty());
|
2018-12-20 19:10:36 +00:00
|
|
|
InvokeMarkSweep();
|
2017-11-20 14:07:15 +00:00
|
|
|
// Phantom handle dies after one more round.
|
|
|
|
CHECK(g1.IsEmpty());
|
|
|
|
CHECK(g2.IsEmpty());
|
2017-11-06 18:12:34 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void ForceScavenge2(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2017-11-09 16:27:34 +00:00
|
|
|
data.GetParameter()->flag = true;
|
|
|
|
InvokeScavenge();
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void ForceScavenge1(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2017-11-09 16:27:34 +00:00
|
|
|
data.GetParameter()->handle.Reset();
|
|
|
|
data.SetSecondPassCallback(ForceScavenge2);
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void ForceMarkSweep2(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2017-11-09 16:27:34 +00:00
|
|
|
data.GetParameter()->flag = true;
|
|
|
|
InvokeMarkSweep();
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void ForceMarkSweep1(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2017-11-09 16:27:34 +00:00
|
|
|
data.GetParameter()->handle.Reset();
|
|
|
|
data.SetSecondPassCallback(ForceMarkSweep2);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(GCFromWeakCallbacks) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::Locker locker(CcTest::isolate());
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
static const int kNumberOfGCTypes = 2;
|
2019-04-23 15:04:17 +00:00
|
|
|
typedef v8::WeakCallbackInfo<FlagAndGlobal>::Callback Callback;
|
2017-11-09 16:27:34 +00:00
|
|
|
Callback gc_forcing_callback[kNumberOfGCTypes] = {&ForceScavenge1,
|
|
|
|
&ForceMarkSweep1};
|
|
|
|
|
|
|
|
typedef void (*GCInvoker)();
|
|
|
|
GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep};
|
|
|
|
|
|
|
|
for (int outer_gc = 0; outer_gc < kNumberOfGCTypes; outer_gc++) {
|
|
|
|
for (int inner_gc = 0; inner_gc < kNumberOfGCTypes; inner_gc++) {
|
2019-04-23 15:04:17 +00:00
|
|
|
FlagAndGlobal fp;
|
2017-11-09 16:27:34 +00:00
|
|
|
ConstructJSApiObject(isolate, context, &fp);
|
2019-02-11 15:07:56 +00:00
|
|
|
CHECK(heap::InYoungGeneration(isolate, fp.handle));
|
2017-11-09 16:27:34 +00:00
|
|
|
fp.flag = false;
|
|
|
|
fp.handle.SetWeak(&fp, gc_forcing_callback[inner_gc],
|
|
|
|
v8::WeakCallbackType::kParameter);
|
|
|
|
invoke_gc[outer_gc]();
|
|
|
|
EmptyMessageQueues(isolate);
|
|
|
|
CHECK(fp.flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 17:01:34 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void SecondPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2018-07-02 17:01:34 +00:00
|
|
|
data.GetParameter()->flag = true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:04:17 +00:00
|
|
|
void FirstPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
|
2018-07-02 17:01:34 +00:00
|
|
|
data.GetParameter()->handle.Reset();
|
|
|
|
data.SetSecondPassCallback(SecondPassCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(SecondPassPhantomCallbacks) {
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::Locker locker(CcTest::isolate());
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
2019-04-23 15:04:17 +00:00
|
|
|
FlagAndGlobal fp;
|
2018-07-02 17:01:34 +00:00
|
|
|
ConstructJSApiObject(isolate, context, &fp);
|
|
|
|
fp.flag = false;
|
|
|
|
fp.handle.SetWeak(&fp, FirstPassCallback, v8::WeakCallbackType::kParameter);
|
|
|
|
CHECK(!fp.flag);
|
2018-12-20 19:10:36 +00:00
|
|
|
InvokeMarkSweep();
|
|
|
|
InvokeMarkSweep();
|
2018-07-02 17:01:34 +00:00
|
|
|
CHECK(fp.flag);
|
|
|
|
}
|
|
|
|
|
2019-01-23 18:31:00 +00:00
|
|
|
TEST(MoveStrongGlobal) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
v8::Global<v8::Object>* global = new Global<v8::Object>();
|
|
|
|
ConstructJSObject(isolate, global);
|
|
|
|
InvokeMarkSweep();
|
|
|
|
v8::Global<v8::Object> global2(std::move(*global));
|
|
|
|
delete global;
|
|
|
|
InvokeMarkSweep();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MoveWeakGlobal) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
v8::Global<v8::Object>* global = new Global<v8::Object>();
|
|
|
|
ConstructJSObject(isolate, global);
|
|
|
|
InvokeMarkSweep();
|
|
|
|
global->SetWeak();
|
|
|
|
v8::Global<v8::Object> global2(std::move(*global));
|
|
|
|
delete global;
|
|
|
|
InvokeMarkSweep();
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|