1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "include/v8-function.h"
29 #include "include/v8-locker.h"
30 #include "src/api/api-inl.h"
31 #include "src/execution/isolate.h"
32 #include "src/handles/global-handles.h"
33 #include "src/heap/factory.h"
34 #include "src/heap/heap-inl.h"
35 #include "src/objects/objects-inl.h"
36 #include "test/cctest/cctest.h"
37 #include "test/cctest/heap/heap-utils.h"
38
39 namespace v8 {
40 namespace internal {
41
42 namespace {
43
44 // Empty v8::EmbedderHeapTracer that never keeps objects alive on Scavenge. See
45 // |IsRootForNonTracingGC|.
46 class NonRootingEmbedderHeapTracer final : public v8::EmbedderHeapTracer {
47 public:
48 NonRootingEmbedderHeapTracer() = default;
49
RegisterV8References(const std::vector<std::pair<void *,void * >> & embedder_fields)50 void RegisterV8References(
51 const std::vector<std::pair<void*, void*>>& embedder_fields) final {}
AdvanceTracing(double deadline_in_ms)52 bool AdvanceTracing(double deadline_in_ms) final { return true; }
IsTracingDone()53 bool IsTracingDone() final { return true; }
TracePrologue(TraceFlags)54 void TracePrologue(TraceFlags) final {}
TraceEpilogue(TraceSummary *)55 void TraceEpilogue(TraceSummary*) final {}
EnterFinalPause(EmbedderStackState)56 void EnterFinalPause(EmbedderStackState) final {}
57
IsRootForNonTracingGC(const v8::TracedGlobal<v8::Value> & handle)58 bool IsRootForNonTracingGC(const v8::TracedGlobal<v8::Value>& handle) final {
59 return false;
60 }
61 };
62
InvokeScavenge()63 void InvokeScavenge() { CcTest::CollectGarbage(i::NEW_SPACE); }
64
InvokeMarkSweep()65 void InvokeMarkSweep() { CcTest::CollectAllGarbage(); }
66
SimpleCallback(const v8::FunctionCallbackInfo<v8::Value> & info)67 void SimpleCallback(const v8::FunctionCallbackInfo<v8::Value>& info) {
68 info.GetReturnValue().Set(v8_num(0));
69 }
70
71 struct FlagAndGlobal {
72 bool flag;
73 v8::Global<v8::Object> handle;
74 };
75
76 struct TracedGlobalWrapper {
77 v8::TracedGlobal<v8::Object> handle;
78 };
79
ResetHandleAndSetFlag(const v8::WeakCallbackInfo<FlagAndGlobal> & data)80 void ResetHandleAndSetFlag(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
81 data.GetParameter()->handle.Reset();
82 data.GetParameter()->flag = true;
83 }
84
85 template <typename HandleContainer>
ConstructJSObject(v8::Isolate * isolate,v8::Local<v8::Context> context,HandleContainer * flag_and_persistent)86 void ConstructJSObject(v8::Isolate* isolate, v8::Local<v8::Context> context,
87 HandleContainer* flag_and_persistent) {
88 v8::HandleScope handle_scope(isolate);
89 v8::Local<v8::Object> object(v8::Object::New(isolate));
90 CHECK(!object.IsEmpty());
91 flag_and_persistent->handle.Reset(isolate, object);
92 CHECK(!flag_and_persistent->handle.IsEmpty());
93 }
94
ConstructJSObject(v8::Isolate * isolate,v8::Global<v8::Object> * global)95 void ConstructJSObject(v8::Isolate* isolate, v8::Global<v8::Object>* global) {
96 v8::HandleScope scope(isolate);
97 v8::Local<v8::Object> object(v8::Object::New(isolate));
98 CHECK(!object.IsEmpty());
99 *global = v8::Global<v8::Object>(isolate, object);
100 CHECK(!global->IsEmpty());
101 }
102
ConstructJSObject(v8::Isolate * isolate,v8::TracedGlobal<v8::Object> * traced)103 void ConstructJSObject(v8::Isolate* isolate,
104 v8::TracedGlobal<v8::Object>* traced) {
105 v8::HandleScope scope(isolate);
106 v8::Local<v8::Object> object(v8::Object::New(isolate));
107 CHECK(!object.IsEmpty());
108 *traced = v8::TracedGlobal<v8::Object>(isolate, object);
109 CHECK(!traced->IsEmpty());
110 }
111
112 template <typename HandleContainer>
ConstructJSApiObject(v8::Isolate * isolate,v8::Local<v8::Context> context,HandleContainer * flag_and_persistent)113 void ConstructJSApiObject(v8::Isolate* isolate, v8::Local<v8::Context> context,
114 HandleContainer* flag_and_persistent) {
115 v8::HandleScope handle_scope(isolate);
116 v8::Local<v8::FunctionTemplate> fun =
117 v8::FunctionTemplate::New(isolate, SimpleCallback);
118 v8::Local<v8::Object> object = fun->GetFunction(context)
119 .ToLocalChecked()
120 ->NewInstance(context)
121 .ToLocalChecked();
122 CHECK(!object.IsEmpty());
123 flag_and_persistent->handle.Reset(isolate, object);
124 CHECK(!flag_and_persistent->handle.IsEmpty());
125 }
126
127 enum class SurvivalMode { kSurvives, kDies };
128
129 template <typename ConstructFunction, typename ModifierFunction,
130 typename GCFunction>
WeakHandleTest(v8::Isolate * isolate,ConstructFunction construct_function,ModifierFunction modifier_function,GCFunction gc_function,SurvivalMode survives)131 void WeakHandleTest(v8::Isolate* isolate, ConstructFunction construct_function,
132 ModifierFunction modifier_function, GCFunction gc_function,
133 SurvivalMode survives) {
134 v8::HandleScope scope(isolate);
135 v8::Local<v8::Context> context = v8::Context::New(isolate);
136 v8::Context::Scope context_scope(context);
137
138 FlagAndGlobal fp;
139 construct_function(isolate, context, &fp);
140 CHECK(heap::InCorrectGeneration(isolate, fp.handle));
141 fp.handle.SetWeak(&fp, &ResetHandleAndSetFlag,
142 v8::WeakCallbackType::kParameter);
143 fp.flag = false;
144 modifier_function(&fp);
145 gc_function();
146 CHECK_IMPLIES(survives == SurvivalMode::kSurvives, !fp.flag);
147 CHECK_IMPLIES(survives == SurvivalMode::kDies, fp.flag);
148 }
149
150 template <typename ConstructFunction, typename ModifierFunction,
151 typename GCFunction>
TracedGlobalTest(v8::Isolate * isolate,ConstructFunction construct_function,ModifierFunction modifier_function,GCFunction gc_function,SurvivalMode survives)152 void TracedGlobalTest(v8::Isolate* isolate,
153 ConstructFunction construct_function,
154 ModifierFunction modifier_function,
155 GCFunction gc_function, SurvivalMode survives) {
156 v8::HandleScope scope(isolate);
157 v8::Local<v8::Context> context = v8::Context::New(isolate);
158 v8::Context::Scope context_scope(context);
159
160 NonRootingEmbedderHeapTracer tracer;
161 heap::TemporaryEmbedderHeapTracerScope tracer_scope(isolate, &tracer);
162
163 auto fp = std::make_unique<TracedGlobalWrapper>();
164 construct_function(isolate, context, fp.get());
165 CHECK(heap::InCorrectGeneration(isolate, fp->handle));
166 modifier_function(fp.get());
167 gc_function();
168 CHECK_IMPLIES(survives == SurvivalMode::kSurvives, !fp->handle.IsEmpty());
169 CHECK_IMPLIES(survives == SurvivalMode::kDies, fp->handle.IsEmpty());
170 }
171
ResurrectingFinalizer(const v8::WeakCallbackInfo<v8::Global<v8::Object>> & data)172 void ResurrectingFinalizer(
173 const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
174 data.GetParameter()->ClearWeak();
175 }
176
ResettingFinalizer(const v8::WeakCallbackInfo<v8::Global<v8::Object>> & data)177 void ResettingFinalizer(
178 const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
179 data.GetParameter()->Reset();
180 }
181
EmptyWeakCallback(const v8::WeakCallbackInfo<void> & data)182 void EmptyWeakCallback(const v8::WeakCallbackInfo<void>& data) {}
183
ResurrectingFinalizerSettingProperty(const v8::WeakCallbackInfo<v8::Global<v8::Object>> & data)184 void ResurrectingFinalizerSettingProperty(
185 const v8::WeakCallbackInfo<v8::Global<v8::Object>>& data) {
186 data.GetParameter()->ClearWeak();
187 v8::Local<v8::Object> o =
188 v8::Local<v8::Object>::New(data.GetIsolate(), *data.GetParameter());
189 o->Set(data.GetIsolate()->GetCurrentContext(), v8_str("finalizer"),
190 v8_str("was here"))
191 .FromJust();
192 }
193
194 } // namespace
195
TEST(EternalHandles)196 TEST(EternalHandles) {
197 CcTest::InitializeVM();
198 Isolate* isolate = CcTest::i_isolate();
199 v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
200 EternalHandles* eternal_handles = isolate->eternal_handles();
201
202 // Create a number of handles that will not be on a block boundary
203 const int kArrayLength = 2048-1;
204 int indices[kArrayLength];
205 v8::Eternal<v8::Value> eternals[kArrayLength];
206
207 CHECK_EQ(0, eternal_handles->handles_count());
208 for (int i = 0; i < kArrayLength; i++) {
209 indices[i] = -1;
210 HandleScope scope(isolate);
211 v8::Local<v8::Object> object = v8::Object::New(v8_isolate);
212 object->Set(v8_isolate->GetCurrentContext(), i,
213 v8::Integer::New(v8_isolate, i))
214 .FromJust();
215 // Create with internal api
216 eternal_handles->Create(
217 isolate, *v8::Utils::OpenHandle(*object), &indices[i]);
218 // Create with external api
219 CHECK(eternals[i].IsEmpty());
220 eternals[i].Set(v8_isolate, object);
221 CHECK(!eternals[i].IsEmpty());
222 }
223
224 CcTest::CollectAllAvailableGarbage();
225
226 for (int i = 0; i < kArrayLength; i++) {
227 for (int j = 0; j < 2; j++) {
228 HandleScope scope(isolate);
229 v8::Local<v8::Value> local;
230 if (j == 0) {
231 // Test internal api
232 local = v8::Utils::ToLocal(eternal_handles->Get(indices[i]));
233 } else {
234 // Test external api
235 local = eternals[i].Get(v8_isolate);
236 }
237 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(local);
238 v8::Local<v8::Value> value =
239 object->Get(v8_isolate->GetCurrentContext(), i).ToLocalChecked();
240 CHECK(value->IsInt32());
241 CHECK_EQ(i,
242 value->Int32Value(v8_isolate->GetCurrentContext()).FromJust());
243 }
244 }
245
246 CHECK_EQ(2 * kArrayLength, eternal_handles->handles_count());
247
248 // Create an eternal via the constructor
249 {
250 HandleScope scope(isolate);
251 v8::Local<v8::Object> object = v8::Object::New(v8_isolate);
252 v8::Eternal<v8::Object> eternal(v8_isolate, object);
253 CHECK(!eternal.IsEmpty());
254 CHECK(object == eternal.Get(v8_isolate));
255 }
256
257 CHECK_EQ(2 * kArrayLength + 1, eternal_handles->handles_count());
258 }
259
260
TEST(PersistentBaseGetLocal)261 TEST(PersistentBaseGetLocal) {
262 CcTest::InitializeVM();
263 v8::Isolate* isolate = CcTest::isolate();
264
265 v8::HandleScope scope(isolate);
266 v8::Local<v8::Object> o = v8::Object::New(isolate);
267 CHECK(!o.IsEmpty());
268 v8::Persistent<v8::Object> p(isolate, o);
269 CHECK(o == p.Get(isolate));
270 CHECK(v8::Local<v8::Object>::New(isolate, p) == p.Get(isolate));
271
272 v8::Global<v8::Object> g(isolate, o);
273 CHECK(o == g.Get(isolate));
274 CHECK(v8::Local<v8::Object>::New(isolate, g) == g.Get(isolate));
275 }
276
TEST(WeakPersistentSmi)277 TEST(WeakPersistentSmi) {
278 CcTest::InitializeVM();
279 v8::Isolate* isolate = CcTest::isolate();
280
281 v8::HandleScope scope(isolate);
282 v8::Local<v8::Number> n = v8::Number::New(isolate, 0);
283 v8::Global<v8::Number> g(isolate, n);
284
285 // Should not crash.
286 g.SetWeak<void>(nullptr, &EmptyWeakCallback,
287 v8::WeakCallbackType::kParameter);
288 }
289
TEST(FinalizerWeakness)290 TEST(FinalizerWeakness) {
291 CcTest::InitializeVM();
292 v8::Isolate* isolate = CcTest::isolate();
293
294 v8::Global<v8::Object> g;
295 int identity;
296
297 {
298 v8::HandleScope scope(isolate);
299 v8::Local<v8::Object> o = v8::Object::New(isolate);
300 identity = o->GetIdentityHash();
301 g.Reset(isolate, o);
302 g.SetWeak(&g, &ResurrectingFinalizerSettingProperty,
303 v8::WeakCallbackType::kFinalizer);
304 }
305
306 CcTest::CollectAllAvailableGarbage();
307
308 CHECK(!g.IsEmpty());
309 v8::HandleScope scope(isolate);
310 v8::Local<v8::Object> o = v8::Local<v8::Object>::New(isolate, g);
311 CHECK_EQ(identity, o->GetIdentityHash());
312 CHECK(o->Has(isolate->GetCurrentContext(), v8_str("finalizer")).FromJust());
313 }
314
TEST(PhatomHandlesWithoutCallbacks)315 TEST(PhatomHandlesWithoutCallbacks) {
316 CcTest::InitializeVM();
317 v8::Isolate* isolate = CcTest::isolate();
318
319 v8::Global<v8::Object> g1, g2;
320 {
321 v8::HandleScope scope(isolate);
322 g1.Reset(isolate, v8::Object::New(isolate));
323 g1.SetWeak();
324 g2.Reset(isolate, v8::Object::New(isolate));
325 g2.SetWeak();
326 }
327
328 CHECK_EQ(0u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
329 CcTest::CollectAllAvailableGarbage();
330 CHECK_EQ(2u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
331 CHECK_EQ(0u, isolate->NumberOfPhantomHandleResetsSinceLastCall());
332 }
333
TEST(WeakHandleToUnmodifiedJSObjectDiesOnScavenge)334 TEST(WeakHandleToUnmodifiedJSObjectDiesOnScavenge) {
335 if (FLAG_single_generation) return;
336 CcTest::InitializeVM();
337 WeakHandleTest(
338 CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
339 [](FlagAndGlobal* fp) {}, []() { InvokeScavenge(); },
340 SurvivalMode::kDies);
341 }
342
TEST(TracedGlobalToUnmodifiedJSObjectSurvivesScavenge)343 TEST(TracedGlobalToUnmodifiedJSObjectSurvivesScavenge) {
344 if (FLAG_single_generation) return;
345 ManualGCScope manual_gc;
346 CcTest::InitializeVM();
347 TracedGlobalTest(
348 CcTest::isolate(), &ConstructJSObject<TracedGlobalWrapper>,
349 [](TracedGlobalWrapper* fp) {}, []() { InvokeScavenge(); },
350 SurvivalMode::kSurvives);
351 }
352
TEST(WeakHandleToUnmodifiedJSObjectDiesOnMarkCompact)353 TEST(WeakHandleToUnmodifiedJSObjectDiesOnMarkCompact) {
354 CcTest::InitializeVM();
355 WeakHandleTest(
356 CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
357 [](FlagAndGlobal* fp) {}, []() { InvokeMarkSweep(); },
358 SurvivalMode::kDies);
359 }
360
TEST(WeakHandleToUnmodifiedJSObjectSurvivesMarkCompactWhenInHandle)361 TEST(WeakHandleToUnmodifiedJSObjectSurvivesMarkCompactWhenInHandle) {
362 CcTest::InitializeVM();
363 WeakHandleTest(
364 CcTest::isolate(), &ConstructJSObject<FlagAndGlobal>,
365 [](FlagAndGlobal* fp) {
366 v8::Local<v8::Object> handle =
367 v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
368 USE(handle);
369 },
370 []() { InvokeMarkSweep(); }, SurvivalMode::kSurvives);
371 }
372
TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnScavenge)373 TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnScavenge) {
374 if (FLAG_single_generation) return;
375 CcTest::InitializeVM();
376 WeakHandleTest(
377 CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
378 [](FlagAndGlobal* fp) {}, []() { InvokeScavenge(); },
379 SurvivalMode::kDies);
380 }
381
TEST(TracedGlobalToUnmodifiedJSApiObjectDiesOnScavenge)382 TEST(TracedGlobalToUnmodifiedJSApiObjectDiesOnScavenge) {
383 if (FLAG_single_generation) return;
384 ManualGCScope manual_gc;
385 CcTest::InitializeVM();
386 TracedGlobalTest(
387 CcTest::isolate(), &ConstructJSApiObject<TracedGlobalWrapper>,
388 [](TracedGlobalWrapper* fp) {}, []() { InvokeScavenge(); },
389 SurvivalMode::kDies);
390 }
391
TEST(TracedGlobalToJSApiObjectWithIdentityHashSurvivesScavenge)392 TEST(TracedGlobalToJSApiObjectWithIdentityHashSurvivesScavenge) {
393 if (FLAG_single_generation) return;
394
395 ManualGCScope manual_gc;
396 CcTest::InitializeVM();
397 Isolate* i_isolate = CcTest::i_isolate();
398 HandleScope scope(i_isolate);
399 Handle<JSWeakMap> weakmap = i_isolate->factory()->NewJSWeakMap();
400
401 TracedGlobalTest(
402 CcTest::isolate(), &ConstructJSApiObject<TracedGlobalWrapper>,
403 [&weakmap, i_isolate](TracedGlobalWrapper* fp) {
404 v8::HandleScope scope(CcTest::isolate());
405 Handle<JSReceiver> key =
406 Utils::OpenHandle(*fp->handle.Get(CcTest::isolate()));
407 Handle<Smi> smi(Smi::FromInt(23), i_isolate);
408 int32_t hash = key->GetOrCreateHash(i_isolate).value();
409 JSWeakCollection::Set(weakmap, key, smi, hash);
410 },
411 []() { InvokeScavenge(); }, SurvivalMode::kSurvives);
412 }
413
TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesScavengeWhenInHandle)414 TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesScavengeWhenInHandle) {
415 if (FLAG_single_generation) return;
416 CcTest::InitializeVM();
417 WeakHandleTest(
418 CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
419 [](FlagAndGlobal* fp) {
420 v8::Local<v8::Object> handle =
421 v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
422 USE(handle);
423 },
424 []() { InvokeScavenge(); }, SurvivalMode::kSurvives);
425 }
426
TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnMarkCompact)427 TEST(WeakHandleToUnmodifiedJSApiObjectDiesOnMarkCompact) {
428 CcTest::InitializeVM();
429 WeakHandleTest(
430 CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
431 [](FlagAndGlobal* fp) {}, []() { InvokeMarkSweep(); },
432 SurvivalMode::kDies);
433 }
434
TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesMarkCompactWhenInHandle)435 TEST(WeakHandleToUnmodifiedJSApiObjectSurvivesMarkCompactWhenInHandle) {
436 CcTest::InitializeVM();
437 WeakHandleTest(
438 CcTest::isolate(), &ConstructJSApiObject<FlagAndGlobal>,
439 [](FlagAndGlobal* fp) {
440 v8::Local<v8::Object> handle =
441 v8::Local<v8::Object>::New(CcTest::isolate(), fp->handle);
442 USE(handle);
443 },
444 []() { InvokeMarkSweep(); }, SurvivalMode::kSurvives);
445 }
446
TEST(TracedGlobalToJSApiObjectWithModifiedMapSurvivesScavenge)447 TEST(TracedGlobalToJSApiObjectWithModifiedMapSurvivesScavenge) {
448 if (FLAG_single_generation) return;
449 CcTest::InitializeVM();
450 v8::Isolate* isolate = CcTest::isolate();
451 LocalContext context;
452
453 TracedGlobal<v8::Object> handle;
454 {
455 v8::HandleScope scope(isolate);
456 // Create an API object which does not have the same map as constructor.
457 auto function_template = FunctionTemplate::New(isolate);
458 auto instance_t = function_template->InstanceTemplate();
459 instance_t->Set(isolate, "a", v8::Number::New(isolate, 10));
460 auto function =
461 function_template->GetFunction(context.local()).ToLocalChecked();
462 auto i = function->NewInstance(context.local()).ToLocalChecked();
463 handle.Reset(isolate, i);
464 }
465 InvokeScavenge();
466 CHECK(!handle.IsEmpty());
467 }
468
TEST(TracedGlobalTOJsApiObjectWithElementsSurvivesScavenge)469 TEST(TracedGlobalTOJsApiObjectWithElementsSurvivesScavenge) {
470 if (FLAG_single_generation) return;
471 CcTest::InitializeVM();
472 v8::Isolate* isolate = CcTest::isolate();
473 LocalContext context;
474
475 TracedGlobal<v8::Object> handle;
476 {
477 v8::HandleScope scope(isolate);
478
479 // Create an API object which has elements.
480 auto function_template = FunctionTemplate::New(isolate);
481 auto instance_t = function_template->InstanceTemplate();
482 instance_t->Set(isolate, "1", v8::Number::New(isolate, 10));
483 instance_t->Set(isolate, "2", v8::Number::New(isolate, 10));
484 auto function =
485 function_template->GetFunction(context.local()).ToLocalChecked();
486 auto i = function->NewInstance(context.local()).ToLocalChecked();
487 handle.Reset(isolate, i);
488 }
489 InvokeScavenge();
490 CHECK(!handle.IsEmpty());
491 }
492
TEST(FinalizerOnUnmodifiedJSApiObjectDoesNotCrash)493 TEST(FinalizerOnUnmodifiedJSApiObjectDoesNotCrash) {
494 // See crbug.com/v8/8586.
495 CcTest::InitializeVM();
496 v8::Isolate* isolate = CcTest::isolate();
497 v8::HandleScope scope(isolate);
498 v8::Local<v8::Context> context = v8::Context::New(isolate);
499 v8::Context::Scope context_scope(context);
500
501 FlagAndGlobal fp;
502 ConstructJSApiObject(isolate, context, &fp);
503 fp.handle.SetWeak(&fp, &ResetHandleAndSetFlag,
504 v8::WeakCallbackType::kFinalizer);
505 fp.flag = false;
506 {
507 v8::HandleScope inner_scope(isolate);
508 v8::Local<v8::Object> tmp = v8::Local<v8::Object>::New(isolate, fp.handle);
509 USE(tmp);
510 InvokeScavenge();
511 }
512 }
513
514 namespace {
515
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)516 void ConstructFinalizerPointingPhantomHandle(
517 v8::Isolate* isolate, v8::Global<v8::Object>* g1,
518 v8::Global<v8::Object>* g2,
519 typename v8::WeakCallbackInfo<v8::Global<v8::Object>>::Callback
520 finalizer_for_g1) {
521 v8::HandleScope scope(isolate);
522 v8::Local<v8::Object> o1 =
523 v8::Local<v8::Object>::New(isolate, v8::Object::New(isolate));
524 v8::Local<v8::Object> o2 =
525 v8::Local<v8::Object>::New(isolate, v8::Object::New(isolate));
526 o1->Set(isolate->GetCurrentContext(), v8_str("link"), o2).FromJust();
527 g1->Reset(isolate, o1);
528 g2->Reset(isolate, o2);
529 // g1 will be finalized but resurrected.
530 g1->SetWeak(g1, finalizer_for_g1, v8::WeakCallbackType::kFinalizer);
531 // g2 will be a phantom handle that is dependent on the finalizer handle
532 // g1 as it is in its subgraph.
533 g2->SetWeak();
534 }
535
536 } // namespace
537
TEST(FinalizerResurrectsAndKeepsPhantomAliveOnMarkCompact)538 TEST(FinalizerResurrectsAndKeepsPhantomAliveOnMarkCompact) {
539 // See crbug.com/772299.
540 CcTest::InitializeVM();
541 v8::Global<v8::Object> g1, g2;
542 ConstructFinalizerPointingPhantomHandle(CcTest::isolate(), &g1, &g2,
543 ResurrectingFinalizer);
544 InvokeMarkSweep();
545 // Both, g1 and g2, should stay alive as the finalizer resurrects the root
546 // object that transitively keeps the other one alive.
547 CHECK(!g1.IsEmpty());
548 CHECK(!g2.IsEmpty());
549 InvokeMarkSweep();
550 // The finalizer handle is now strong, so it should keep the objects alive.
551 CHECK(!g1.IsEmpty());
552 CHECK(!g2.IsEmpty());
553 }
554
TEST(FinalizerDiesAndKeepsPhantomAliveOnMarkCompact)555 TEST(FinalizerDiesAndKeepsPhantomAliveOnMarkCompact) {
556 CcTest::InitializeVM();
557 v8::Global<v8::Object> g1, g2;
558 ConstructFinalizerPointingPhantomHandle(CcTest::isolate(), &g1, &g2,
559 ResettingFinalizer);
560 InvokeMarkSweep();
561 // Finalizer (g1) dies but the phantom handle (g2) is kept alive for one
562 // more round as the underlying object only dies on the next GC.
563 CHECK(g1.IsEmpty());
564 CHECK(!g2.IsEmpty());
565 InvokeMarkSweep();
566 // Phantom handle dies after one more round.
567 CHECK(g1.IsEmpty());
568 CHECK(g2.IsEmpty());
569 }
570
571 namespace {
572
ForceScavenge2(const v8::WeakCallbackInfo<FlagAndGlobal> & data)573 void ForceScavenge2(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
574 data.GetParameter()->flag = true;
575 InvokeScavenge();
576 }
577
ForceScavenge1(const v8::WeakCallbackInfo<FlagAndGlobal> & data)578 void ForceScavenge1(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
579 data.GetParameter()->handle.Reset();
580 data.SetSecondPassCallback(ForceScavenge2);
581 }
582
ForceMarkSweep2(const v8::WeakCallbackInfo<FlagAndGlobal> & data)583 void ForceMarkSweep2(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
584 data.GetParameter()->flag = true;
585 InvokeMarkSweep();
586 }
587
ForceMarkSweep1(const v8::WeakCallbackInfo<FlagAndGlobal> & data)588 void ForceMarkSweep1(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
589 data.GetParameter()->handle.Reset();
590 data.SetSecondPassCallback(ForceMarkSweep2);
591 }
592
593 } // namespace
594
TEST(GCFromWeakCallbacks)595 TEST(GCFromWeakCallbacks) {
596 v8::Isolate* isolate = CcTest::isolate();
597 v8::Locker locker(CcTest::isolate());
598 v8::HandleScope scope(isolate);
599 v8::Local<v8::Context> context = v8::Context::New(isolate);
600 v8::Context::Scope context_scope(context);
601
602 if (FLAG_single_generation) {
603 FlagAndGlobal fp;
604 ConstructJSApiObject(isolate, context, &fp);
605 CHECK(!heap::InYoungGeneration(isolate, fp.handle));
606 fp.flag = false;
607 fp.handle.SetWeak(&fp, &ForceMarkSweep1, v8::WeakCallbackType::kParameter);
608 InvokeMarkSweep();
609 EmptyMessageQueues(isolate);
610 CHECK(fp.flag);
611 return;
612 }
613
614 static const int kNumberOfGCTypes = 2;
615 using Callback = v8::WeakCallbackInfo<FlagAndGlobal>::Callback;
616 Callback gc_forcing_callback[kNumberOfGCTypes] = {&ForceScavenge1,
617 &ForceMarkSweep1};
618
619 using GCInvoker = void (*)();
620 GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep};
621
622 for (int outer_gc = 0; outer_gc < kNumberOfGCTypes; outer_gc++) {
623 for (int inner_gc = 0; inner_gc < kNumberOfGCTypes; inner_gc++) {
624 FlagAndGlobal fp;
625 ConstructJSApiObject(isolate, context, &fp);
626 CHECK(heap::InYoungGeneration(isolate, fp.handle));
627 fp.flag = false;
628 fp.handle.SetWeak(&fp, gc_forcing_callback[inner_gc],
629 v8::WeakCallbackType::kParameter);
630 invoke_gc[outer_gc]();
631 EmptyMessageQueues(isolate);
632 CHECK(fp.flag);
633 }
634 }
635 }
636
637 namespace {
638
SecondPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal> & data)639 void SecondPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
640 data.GetParameter()->flag = true;
641 }
642
FirstPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal> & data)643 void FirstPassCallback(const v8::WeakCallbackInfo<FlagAndGlobal>& data) {
644 data.GetParameter()->handle.Reset();
645 data.SetSecondPassCallback(SecondPassCallback);
646 }
647
648 } // namespace
649
TEST(SecondPassPhantomCallbacks)650 TEST(SecondPassPhantomCallbacks) {
651 v8::Isolate* isolate = CcTest::isolate();
652 v8::Locker locker(CcTest::isolate());
653 v8::HandleScope scope(isolate);
654 v8::Local<v8::Context> context = v8::Context::New(isolate);
655 v8::Context::Scope context_scope(context);
656 FlagAndGlobal fp;
657 ConstructJSApiObject(isolate, context, &fp);
658 fp.flag = false;
659 fp.handle.SetWeak(&fp, FirstPassCallback, v8::WeakCallbackType::kParameter);
660 CHECK(!fp.flag);
661 InvokeMarkSweep();
662 InvokeMarkSweep();
663 CHECK(fp.flag);
664 }
665
TEST(MoveStrongGlobal)666 TEST(MoveStrongGlobal) {
667 CcTest::InitializeVM();
668 v8::Isolate* isolate = CcTest::isolate();
669 v8::HandleScope scope(isolate);
670
671 v8::Global<v8::Object>* global = new Global<v8::Object>();
672 ConstructJSObject(isolate, global);
673 InvokeMarkSweep();
674 v8::Global<v8::Object> global2(std::move(*global));
675 delete global;
676 InvokeMarkSweep();
677 }
678
TEST(MoveWeakGlobal)679 TEST(MoveWeakGlobal) {
680 CcTest::InitializeVM();
681 v8::Isolate* isolate = CcTest::isolate();
682 v8::HandleScope scope(isolate);
683
684 v8::Global<v8::Object>* global = new Global<v8::Object>();
685 ConstructJSObject(isolate, global);
686 InvokeMarkSweep();
687 global->SetWeak();
688 v8::Global<v8::Object> global2(std::move(*global));
689 delete global;
690 InvokeMarkSweep();
691 }
692
TEST(TotalSizeRegularNode)693 TEST(TotalSizeRegularNode) {
694 CcTest::InitializeVM();
695 v8::Isolate* isolate = CcTest::isolate();
696 Isolate* i_isolate = CcTest::i_isolate();
697 v8::HandleScope scope(isolate);
698
699 v8::Global<v8::Object>* global = new Global<v8::Object>();
700 CHECK_EQ(i_isolate->global_handles()->TotalSize(), 0);
701 CHECK_EQ(i_isolate->global_handles()->UsedSize(), 0);
702 ConstructJSObject(isolate, global);
703 CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
704 CHECK_GT(i_isolate->global_handles()->UsedSize(), 0);
705 delete global;
706 CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
707 CHECK_EQ(i_isolate->global_handles()->UsedSize(), 0);
708 }
709
TEST(TotalSizeTracedNode)710 TEST(TotalSizeTracedNode) {
711 CcTest::InitializeVM();
712 v8::Isolate* isolate = CcTest::isolate();
713 Isolate* i_isolate = CcTest::i_isolate();
714 v8::HandleScope scope(isolate);
715
716 v8::TracedGlobal<v8::Object>* global = new TracedGlobal<v8::Object>();
717 CHECK_EQ(i_isolate->global_handles()->TotalSize(), 0);
718 CHECK_EQ(i_isolate->global_handles()->UsedSize(), 0);
719 ConstructJSObject(isolate, global);
720 CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
721 CHECK_GT(i_isolate->global_handles()->UsedSize(), 0);
722 delete global;
723 CHECK_GT(i_isolate->global_handles()->TotalSize(), 0);
724 CHECK_EQ(i_isolate->global_handles()->UsedSize(), 0);
725 }
726
727 } // namespace internal
728 } // namespace v8
729