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.
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
#include "src/handles/global-handles.h"
|
|
|
|
|
2021-08-23 13:01:06 +00:00
|
|
|
#include "include/v8-function.h"
|
2019-05-17 12:13:44 +00:00
|
|
|
#include "src/api/api-inl.h"
|
2019-05-22 07:55:37 +00:00
|
|
|
#include "src/execution/isolate.h"
|
2022-09-23 13:47:28 +00:00
|
|
|
#include "src/flags/flags.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"
|
2019-05-23 08:51:46 +00:00
|
|
|
#include "src/objects/objects-inl.h"
|
2022-08-25 09:03:22 +00:00
|
|
|
#include "test/unittests/heap/heap-utils.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.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 {
|
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
struct TracedReferenceWrapper {
|
|
|
|
v8::TracedReference<v8::Object> handle;
|
|
|
|
};
|
|
|
|
|
2022-08-23 13:57:03 +00:00
|
|
|
START_ALLOW_USE_DEPRECATED()
|
|
|
|
|
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; }
|
2019-10-04 18:00:38 +00:00
|
|
|
void TracePrologue(TraceFlags) final {}
|
2019-10-18 07:15:01 +00:00
|
|
|
void TraceEpilogue(TraceSummary*) final {}
|
2019-04-23 15:04:17 +00:00
|
|
|
void EnterFinalPause(EmbedderStackState) final {}
|
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
bool IsRootForNonTracingGC(
|
|
|
|
const v8::TracedReference<v8::Value>& handle) final {
|
2019-04-23 15:04:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-23 09:24:36 +00:00
|
|
|
|
|
|
|
void ResetHandleInNonTracingGC(
|
|
|
|
const v8::TracedReference<v8::Value>& handle) final {
|
|
|
|
for (auto* wrapper : wrappers_) {
|
|
|
|
if (wrapper->handle == handle) {
|
|
|
|
wrapper->handle.Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Register(TracedReferenceWrapper* wrapper) {
|
|
|
|
wrappers_.push_back(wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<TracedReferenceWrapper*> wrappers_;
|
2019-04-23 15:04:17 +00:00
|
|
|
};
|
|
|
|
|
2022-08-23 13:57:03 +00:00
|
|
|
END_ALLOW_USE_DEPRECATED()
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
void SimpleCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2022-08-25 09:03:22 +00:00
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
|
|
|
info.GetReturnValue().Set(v8::Number::New(isolate, 0));
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2020-03-03 11:43:50 +00:00
|
|
|
void ConstructJSObject(v8::Isolate* isolate,
|
2022-03-23 09:24:36 +00:00
|
|
|
v8::TracedReference<v8::Object>* handle) {
|
2020-03-03 11:43:50 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Object> object(v8::Object::New(isolate));
|
|
|
|
CHECK(!object.IsEmpty());
|
2022-03-23 09:24:36 +00:00
|
|
|
*handle = v8::TracedReference<v8::Object>(isolate, object);
|
|
|
|
CHECK(!handle->IsEmpty());
|
2020-03-03 11:43:50 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2022-08-25 09:03:22 +00:00
|
|
|
CHECK(IsNewObjectInCorrectGeneration(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);
|
|
|
|
}
|
|
|
|
|
2022-08-23 07:16:01 +00:00
|
|
|
void EmptyWeakCallback(const v8::WeakCallbackInfo<void>& data) {}
|
2022-08-12 09:53:31 +00:00
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
class GlobalHandlesTest : public TestWithContext {
|
|
|
|
protected:
|
|
|
|
template <typename ConstructFunction, typename ModifierFunction>
|
|
|
|
void TracedReferenceTestWithScavenge(ConstructFunction construct_function,
|
|
|
|
ModifierFunction modifier_function,
|
|
|
|
SurvivalMode survives) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
|
|
|
NonRootingEmbedderHeapTracer tracer;
|
|
|
|
TemporaryEmbedderHeapTracerScope tracer_scope(isolate, &tracer);
|
|
|
|
|
|
|
|
auto fp = std::make_unique<TracedReferenceWrapper>();
|
|
|
|
tracer.Register(fp.get());
|
|
|
|
construct_function(isolate, context, fp.get());
|
|
|
|
CHECK(IsNewObjectInCorrectGeneration(isolate, fp->handle));
|
|
|
|
modifier_function(fp.get());
|
|
|
|
CollectGarbage(i::NEW_SPACE);
|
|
|
|
// Scavenge clear properly resets the original handle, so we can check the
|
|
|
|
// handle directly here.
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kSurvives, !fp->handle.IsEmpty());
|
|
|
|
CHECK_IMPLIES(survives == SurvivalMode::kDies, fp->handle.IsEmpty());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
} // namespace
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, EternalHandles) {
|
|
|
|
Isolate* isolate = 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
|
2022-08-25 09:03:22 +00:00
|
|
|
const int kArrayLength = 2048 - 1;
|
2013-08-05 09:46:23 +00:00
|
|
|
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);
|
2022-08-25 09:03:22 +00:00
|
|
|
object
|
|
|
|
->Set(v8_isolate->GetCurrentContext(), i,
|
|
|
|
v8::Integer::New(v8_isolate, i))
|
2015-11-17 12:42:14 +00:00
|
|
|
.FromJust();
|
2013-08-26 09:41:22 +00:00
|
|
|
// Create with internal api
|
2022-08-25 09:03:22 +00:00
|
|
|
eternal_handles->Create(isolate, *v8::Utils::OpenHandle(*object),
|
|
|
|
&indices[i]);
|
2013-08-26 09:41:22 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
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
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, PersistentBaseGetLocal) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2015-07-13 15:02:21 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, WeakPersistentSmi) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2016-01-25 13:09:27 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-21 12:12:55 +00:00
|
|
|
START_ALLOW_USE_DEPRECATED()
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, PhantomHandlesWithoutCallbacks) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2016-05-09 07:15:43 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2022-06-09 13:41:10 +00:00
|
|
|
CHECK(!g1.IsEmpty());
|
|
|
|
CHECK(!g2.IsEmpty());
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllAvailableGarbage();
|
2022-06-09 13:41:10 +00:00
|
|
|
CHECK(g1.IsEmpty());
|
|
|
|
CHECK(g2.IsEmpty());
|
2016-05-09 07:15:43 +00:00
|
|
|
}
|
2017-08-11 11:22:28 +00:00
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, WeakHandleToUnmodifiedJSObjectDiesOnScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSObject<FlagAndGlobal>, [](FlagAndGlobal* fp) {},
|
|
|
|
[this]() { CollectGarbage(i::NEW_SPACE); }, SurvivalMode::kDies);
|
2019-04-23 15:04:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, TracedReferenceToUnmodifiedJSObjectSurvivesScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
|
|
|
ManualGCScope manual_gc(i_isolate());
|
2022-03-23 09:24:36 +00:00
|
|
|
TracedReferenceTestWithScavenge(
|
2022-08-25 09:03:22 +00:00
|
|
|
&ConstructJSObject<TracedReferenceWrapper>,
|
2022-03-23 09:24:36 +00:00
|
|
|
[](TracedReferenceWrapper* fp) {}, SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
2017-11-02 17:38:23 +00:00
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, WeakHandleToUnmodifiedJSObjectDiesOnMarkCompact) {
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSObject<FlagAndGlobal>, [](FlagAndGlobal* fp) {},
|
|
|
|
[this]() { CollectAllGarbage(); }, SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
WeakHandleToUnmodifiedJSObjectSurvivesMarkCompactWhenInHandle) {
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSObject<FlagAndGlobal>,
|
|
|
|
[this](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
2022-08-25 09:03:22 +00:00
|
|
|
v8::Local<v8::Object>::New(v8_isolate(), fp->handle);
|
2017-11-09 16:27:34 +00:00
|
|
|
USE(handle);
|
|
|
|
},
|
2022-08-25 09:03:22 +00:00
|
|
|
[this]() { CollectAllGarbage(); }, SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, WeakHandleToUnmodifiedJSApiObjectDiesOnScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, [this]() { CollectGarbage(i::NEW_SPACE); },
|
2019-04-23 15:04:17 +00:00
|
|
|
SurvivalMode::kDies);
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
TracedReferenceToUnmodifiedJSApiObjectDiesOnScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
|
|
|
ManualGCScope manual_gc(i_isolate());
|
2022-03-23 09:24:36 +00:00
|
|
|
TracedReferenceTestWithScavenge(
|
2022-08-25 09:03:22 +00:00
|
|
|
&ConstructJSApiObject<TracedReferenceWrapper>,
|
2022-03-23 09:24:36 +00:00
|
|
|
[](TracedReferenceWrapper* fp) {}, SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
TracedReferenceToJSApiObjectWithIdentityHashSurvivesScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2021-05-03 11:42:06 +00:00
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
ManualGCScope manual_gc(i_isolate());
|
|
|
|
Isolate* isolate = i_isolate();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
|
2019-04-09 11:54:43 +00:00
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
TracedReferenceTestWithScavenge(
|
2022-08-25 09:03:22 +00:00
|
|
|
&ConstructJSApiObject<TracedReferenceWrapper>,
|
|
|
|
[this, &weakmap, isolate](TracedReferenceWrapper* fp) {
|
|
|
|
v8::HandleScope scope(v8_isolate());
|
2019-04-09 11:54:43 +00:00
|
|
|
Handle<JSReceiver> key =
|
2022-08-25 09:03:22 +00:00
|
|
|
Utils::OpenHandle(*fp->handle.Get(v8_isolate()));
|
|
|
|
Handle<Smi> smi(Smi::FromInt(23), isolate);
|
|
|
|
int32_t hash = key->GetOrCreateHash(isolate).value();
|
2019-04-09 11:54:43 +00:00
|
|
|
JSWeakCollection::Set(weakmap, key, smi, hash);
|
2019-04-08 09:43:10 +00:00
|
|
|
},
|
2022-03-23 09:24:36 +00:00
|
|
|
SurvivalMode::kSurvives);
|
2019-04-08 09:43:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
WeakHandleToUnmodifiedJSApiObjectSurvivesScavengeWhenInHandle) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[this](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
2022-08-25 09:03:22 +00:00
|
|
|
v8::Local<v8::Object>::New(v8_isolate(), fp->handle);
|
2017-11-09 16:27:34 +00:00
|
|
|
USE(handle);
|
|
|
|
},
|
2022-08-25 09:03:22 +00:00
|
|
|
[this]() { CollectGarbage(i::NEW_SPACE); }, SurvivalMode::kSurvives);
|
2017-11-02 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, WeakHandleToUnmodifiedJSApiObjectDiesOnMarkCompact) {
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[](FlagAndGlobal* fp) {}, [this]() { CollectAllGarbage(); },
|
2019-04-23 15:04:17 +00:00
|
|
|
SurvivalMode::kDies);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
WeakHandleToUnmodifiedJSApiObjectSurvivesMarkCompactWhenInHandle) {
|
2017-11-09 16:27:34 +00:00
|
|
|
WeakHandleTest(
|
2022-08-25 09:03:22 +00:00
|
|
|
v8_isolate(), &ConstructJSApiObject<FlagAndGlobal>,
|
|
|
|
[this](FlagAndGlobal* fp) {
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::Local<v8::Object> handle =
|
2022-08-25 09:03:22 +00:00
|
|
|
v8::Local<v8::Object>::New(v8_isolate(), fp->handle);
|
2017-11-09 16:27:34 +00:00
|
|
|
USE(handle);
|
|
|
|
},
|
2022-08-25 09:03:22 +00:00
|
|
|
[this]() { CollectAllGarbage(); }, SurvivalMode::kSurvives);
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
TracedReferenceToJSApiObjectWithModifiedMapSurvivesScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2017-11-09 16:27:34 +00:00
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
TracedReference<v8::Object> handle;
|
2019-04-23 15:04:17 +00:00
|
|
|
{
|
|
|
|
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();
|
2020-06-09 14:53:42 +00:00
|
|
|
instance_t->Set(isolate, "a", v8::Number::New(isolate, 10));
|
2019-04-23 15:04:17 +00:00
|
|
|
auto function =
|
2022-08-25 09:03:22 +00:00
|
|
|
function_template->GetFunction(v8_context()).ToLocalChecked();
|
|
|
|
auto i = function->NewInstance(v8_context()).ToLocalChecked();
|
2019-04-23 15:04:17 +00:00
|
|
|
handle.Reset(isolate, i);
|
|
|
|
}
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectGarbage(i::NEW_SPACE);
|
2019-04-23 15:04:17 +00:00
|
|
|
CHECK(!handle.IsEmpty());
|
2019-04-23 14:56:38 +00:00
|
|
|
}
|
2019-04-23 12:53:34 +00:00
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest,
|
|
|
|
TracedReferenceTOJsApiObjectWithElementsSurvivesScavenge) {
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) return;
|
2022-08-25 09:03:22 +00:00
|
|
|
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2019-04-23 15:04:17 +00:00
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
TracedReference<v8::Object> handle;
|
2019-04-23 15:04:17 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
// Create an API object which has elements.
|
|
|
|
auto function_template = FunctionTemplate::New(isolate);
|
|
|
|
auto instance_t = function_template->InstanceTemplate();
|
2020-06-09 14:53:42 +00:00
|
|
|
instance_t->Set(isolate, "1", v8::Number::New(isolate, 10));
|
|
|
|
instance_t->Set(isolate, "2", v8::Number::New(isolate, 10));
|
2019-04-23 15:04:17 +00:00
|
|
|
auto function =
|
2022-08-25 09:03:22 +00:00
|
|
|
function_template->GetFunction(v8_context()).ToLocalChecked();
|
|
|
|
auto i = function->NewInstance(v8_context()).ToLocalChecked();
|
2019-04-23 15:04:17 +00:00
|
|
|
handle.Reset(isolate, i);
|
|
|
|
}
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectGarbage(i::NEW_SPACE);
|
2019-04-23 15:04:17 +00:00
|
|
|
CHECK(!handle.IsEmpty());
|
2018-12-20 19:10:36 +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;
|
2022-08-25 09:03:22 +00:00
|
|
|
YoungGC(data.GetIsolate());
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2022-08-25 09:03:22 +00:00
|
|
|
FullGC(data.GetIsolate());
|
2017-11-09 16:27:34 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, GCFromWeakCallbacks) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2017-11-09 16:27:34 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = v8::Context::New(isolate);
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
|
2022-09-05 10:16:21 +00:00
|
|
|
if (v8_flags.single_generation) {
|
2020-07-02 23:40:05 +00:00
|
|
|
FlagAndGlobal fp;
|
|
|
|
ConstructJSApiObject(isolate, context, &fp);
|
2022-09-23 13:47:28 +00:00
|
|
|
CHECK_IMPLIES(!v8_flags.single_generation,
|
|
|
|
!InYoungGeneration(isolate, fp.handle));
|
2020-07-02 23:40:05 +00:00
|
|
|
fp.flag = false;
|
|
|
|
fp.handle.SetWeak(&fp, &ForceMarkSweep1, v8::WeakCallbackType::kParameter);
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
|
|
|
EmptyMessageQueues();
|
2020-07-02 23:40:05 +00:00
|
|
|
CHECK(fp.flag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:27:34 +00:00
|
|
|
static const int kNumberOfGCTypes = 2;
|
2019-05-27 11:31:49 +00:00
|
|
|
using Callback = v8::WeakCallbackInfo<FlagAndGlobal>::Callback;
|
2017-11-09 16:27:34 +00:00
|
|
|
Callback gc_forcing_callback[kNumberOfGCTypes] = {&ForceScavenge1,
|
|
|
|
&ForceMarkSweep1};
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
using GCInvoker = std::function<void(void)>;
|
|
|
|
GCInvoker invoke_gc[kNumberOfGCTypes] = {
|
|
|
|
[this]() { CollectGarbage(i::NEW_SPACE); },
|
|
|
|
[this]() { CollectAllGarbage(); }};
|
2017-11-09 16:27:34 +00:00
|
|
|
|
|
|
|
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);
|
2022-08-25 09:03:22 +00:00
|
|
|
CHECK(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]();
|
2022-08-25 09:03:22 +00:00
|
|
|
EmptyMessageQueues();
|
2017-11-09 16:27:34 +00:00
|
|
|
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
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, SecondPassPhantomCallbacks) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2018-07-02 17:01:34 +00:00
|
|
|
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);
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
|
|
|
CollectAllGarbage();
|
2018-07-02 17:01:34 +00:00
|
|
|
CHECK(fp.flag);
|
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, MoveStrongGlobal) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2019-01-23 18:31:00 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
v8::Global<v8::Object>* global = new Global<v8::Object>();
|
|
|
|
ConstructJSObject(isolate, global);
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
2019-01-23 18:31:00 +00:00
|
|
|
v8::Global<v8::Object> global2(std::move(*global));
|
|
|
|
delete global;
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
2019-01-23 18:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, MoveWeakGlobal) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2019-01-23 18:31:00 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
v8::Global<v8::Object>* global = new Global<v8::Object>();
|
|
|
|
ConstructJSObject(isolate, global);
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
2019-01-23 18:31:00 +00:00
|
|
|
global->SetWeak();
|
|
|
|
v8::Global<v8::Object> global2(std::move(*global));
|
|
|
|
delete global;
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
2019-01-23 18:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, TotalSizeRegularNode) {
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2020-03-03 11:43:50 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
|
|
|
v8::Global<v8::Object>* global = new Global<v8::Object>();
|
2022-08-25 09:03:22 +00:00
|
|
|
CHECK_EQ(i_isolate()->global_handles()->TotalSize(), 0);
|
|
|
|
CHECK_EQ(i_isolate()->global_handles()->UsedSize(), 0);
|
2020-03-03 11:43:50 +00:00
|
|
|
ConstructJSObject(isolate, global);
|
2022-08-25 09:03:22 +00:00
|
|
|
CHECK_GT(i_isolate()->global_handles()->TotalSize(), 0);
|
|
|
|
CHECK_GT(i_isolate()->global_handles()->UsedSize(), 0);
|
2020-03-03 11:43:50 +00:00
|
|
|
delete global;
|
2022-08-25 09:03:22 +00:00
|
|
|
CHECK_GT(i_isolate()->global_handles()->TotalSize(), 0);
|
|
|
|
CHECK_EQ(i_isolate()->global_handles()->UsedSize(), 0);
|
2020-03-03 11:43:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 09:03:22 +00:00
|
|
|
TEST_F(GlobalHandlesTest, TotalSizeTracedNode) {
|
|
|
|
ManualGCScope manual_gc(i_isolate());
|
|
|
|
v8::Isolate* isolate = v8_isolate();
|
2020-03-03 11:43:50 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
|
2022-03-23 09:24:36 +00:00
|
|
|
v8::TracedReference<v8::Object>* handle = new TracedReference<v8::Object>();
|
2022-10-21 13:31:46 +00:00
|
|
|
CHECK_EQ(i_isolate()->traced_handles()->total_size_bytes(), 0);
|
|
|
|
CHECK_EQ(i_isolate()->traced_handles()->used_size_bytes(), 0);
|
2022-03-23 09:24:36 +00:00
|
|
|
ConstructJSObject(isolate, handle);
|
2022-10-21 13:31:46 +00:00
|
|
|
CHECK_GT(i_isolate()->traced_handles()->total_size_bytes(), 0);
|
|
|
|
CHECK_GT(i_isolate()->traced_handles()->used_size_bytes(), 0);
|
2022-03-23 09:24:36 +00:00
|
|
|
delete handle;
|
2022-08-25 09:03:22 +00:00
|
|
|
CollectAllGarbage();
|
2022-10-21 13:31:46 +00:00
|
|
|
CHECK_GT(i_isolate()->traced_handles()->total_size_bytes(), 0);
|
|
|
|
CHECK_EQ(i_isolate()->traced_handles()->used_size_bytes(), 0);
|
2020-03-03 11:43:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 11:22:28 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|