LCOV - code coverage report
Current view: top level - test/cctest - test-weakmaps.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 94 110 85.5 %
Date: 2019-03-21 Functions: 8 9 88.9 %

          Line data    Source code
       1             : // Copyright 2011 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 <utility>
      29             : 
      30             : #include "src/global-handles.h"
      31             : #include "src/heap/factory.h"
      32             : #include "src/heap/heap-inl.h"
      33             : #include "src/isolate.h"
      34             : #include "src/objects-inl.h"
      35             : #include "src/objects/hash-table-inl.h"
      36             : #include "src/objects/js-collection-inl.h"
      37             : #include "test/cctest/cctest.h"
      38             : #include "test/cctest/heap/heap-utils.h"
      39             : 
      40             : namespace v8 {
      41             : namespace internal {
      42             : namespace test_weakmaps {
      43             : 
      44             : static Isolate* GetIsolateFrom(LocalContext* context) {
      45          15 :   return reinterpret_cast<Isolate*>((*context)->GetIsolate());
      46             : }
      47             : 
      48             : static int NumberOfWeakCalls = 0;
      49           5 : static void WeakPointerCallback(const v8::WeakCallbackInfo<void>& data) {
      50             :   std::pair<v8::Persistent<v8::Value>*, int>* p =
      51             :       reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
      52             :           data.GetParameter());
      53           5 :   CHECK_EQ(1234, p->second);
      54           5 :   NumberOfWeakCalls++;
      55           5 :   p->first->Reset();
      56           5 : }
      57             : 
      58             : 
      59       26068 : TEST(Weakness) {
      60           5 :   FLAG_incremental_marking = false;
      61           5 :   LocalContext context;
      62             :   Isolate* isolate = GetIsolateFrom(&context);
      63             :   Factory* factory = isolate->factory();
      64             :   HandleScope scope(isolate);
      65           5 :   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
      66             :   GlobalHandles* global_handles = isolate->global_handles();
      67             : 
      68             :   // Keep global reference to the key.
      69             :   Handle<Object> key;
      70             :   {
      71             :     HandleScope scope(isolate);
      72           5 :     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
      73           5 :     Handle<JSObject> object = factory->NewJSObjectFromMap(map);
      74           5 :     key = global_handles->Create(*object);
      75             :   }
      76           5 :   CHECK(!global_handles->IsWeak(key.location()));
      77             : 
      78             :   // Put two chained entries into weak map.
      79             :   {
      80             :     HandleScope scope(isolate);
      81           5 :     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
      82           5 :     Handle<JSObject> object = factory->NewJSObjectFromMap(map);
      83             :     Handle<Smi> smi(Smi::FromInt(23), isolate);
      84          10 :     int32_t hash = key->GetOrCreateHash(isolate)->value();
      85           5 :     JSWeakCollection::Set(weakmap, key, object, hash);
      86          10 :     int32_t object_hash = object->GetOrCreateHash(isolate)->value();
      87           5 :     JSWeakCollection::Set(weakmap, object, smi, object_hash);
      88             :   }
      89           5 :   CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
      90             : 
      91             :   // Force a full GC.
      92           5 :   CcTest::PreciseCollectAllGarbage();
      93           5 :   CHECK_EQ(0, NumberOfWeakCalls);
      94           5 :   CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
      95           5 :   CHECK_EQ(
      96             :       0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
      97             : 
      98             :   // Make the global reference to the key weak.
      99             :   std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
     100             :   GlobalHandles::MakeWeak(
     101             :       key.location(), reinterpret_cast<void*>(&handle_and_id),
     102           5 :       &WeakPointerCallback, v8::WeakCallbackType::kParameter);
     103           5 :   CHECK(global_handles->IsWeak(key.location()));
     104             : 
     105           5 :   CcTest::PreciseCollectAllGarbage();
     106           5 :   CHECK_EQ(1, NumberOfWeakCalls);
     107           5 :   CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
     108           5 :   CHECK_EQ(
     109             :       2, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
     110           5 : }
     111             : 
     112             : 
     113       26063 : TEST(Shrinking) {
     114           0 :   LocalContext context;
     115             :   Isolate* isolate = GetIsolateFrom(&context);
     116             :   Factory* factory = isolate->factory();
     117             :   HandleScope scope(isolate);
     118           0 :   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
     119             : 
     120             :   // Check initial capacity.
     121           0 :   CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
     122             : 
     123             :   // Fill up weak map to trigger capacity change.
     124             :   {
     125             :     HandleScope scope(isolate);
     126           0 :     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
     127           0 :     for (int i = 0; i < 32; i++) {
     128           0 :       Handle<JSObject> object = factory->NewJSObjectFromMap(map);
     129             :       Handle<Smi> smi(Smi::FromInt(i), isolate);
     130           0 :       int32_t object_hash = object->GetOrCreateHash(isolate)->value();
     131           0 :       JSWeakCollection::Set(weakmap, object, smi, object_hash);
     132             :     }
     133             :   }
     134             : 
     135             :   // Check increased capacity.
     136           0 :   CHECK_EQ(128, EphemeronHashTable::cast(weakmap->table())->Capacity());
     137             : 
     138             :   // Force a full GC.
     139           0 :   CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
     140           0 :   CHECK_EQ(
     141             :       0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
     142           0 :   CcTest::PreciseCollectAllGarbage();
     143           0 :   CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
     144           0 :   CHECK_EQ(
     145             :       32,
     146             :       EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
     147             : 
     148             :   // Check shrunk capacity.
     149           0 :   CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
     150           0 : }
     151             : 
     152             : 
     153             : // Test that weak map values on an evacuation candidate which are not reachable
     154             : // by other paths are correctly recorded in the slots buffer.
     155       26068 : TEST(Regress2060a) {
     156           5 :   if (i::FLAG_never_compact) return;
     157           5 :   FLAG_always_compact = true;
     158           5 :   LocalContext context;
     159             :   Isolate* isolate = GetIsolateFrom(&context);
     160             :   Factory* factory = isolate->factory();
     161             :   Heap* heap = isolate->heap();
     162             :   HandleScope scope(isolate);
     163             :   Handle<JSFunction> function =
     164           5 :       factory->NewFunctionForTest(factory->function_string());
     165           5 :   Handle<JSObject> key = factory->NewJSObject(function);
     166           5 :   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
     167             : 
     168             :   // Start second old-space page so that values land on evacuation candidate.
     169             :   Page* first_page = heap->old_space()->first_page();
     170           5 :   heap::SimulateFullSpace(heap->old_space());
     171             : 
     172             :   // Fill up weak map with values on an evacuation candidate.
     173             :   {
     174             :     HandleScope scope(isolate);
     175         325 :     for (int i = 0; i < 32; i++) {
     176             :       Handle<JSObject> object =
     177         160 :           factory->NewJSObject(function, AllocationType::kOld);
     178         160 :       CHECK(!Heap::InYoungGeneration(*object));
     179         160 :       CHECK(!first_page->Contains(object->address()));
     180         320 :       int32_t hash = key->GetOrCreateHash(isolate)->value();
     181         160 :       JSWeakCollection::Set(weakmap, key, object, hash);
     182             :     }
     183             :   }
     184             : 
     185             :   // Force compacting garbage collection.
     186           5 :   CHECK(FLAG_always_compact);
     187           5 :   CcTest::CollectAllGarbage();
     188             : }
     189             : 
     190             : 
     191             : // Test that weak map keys on an evacuation candidate which are reachable by
     192             : // other strong paths are correctly recorded in the slots buffer.
     193       26068 : TEST(Regress2060b) {
     194           5 :   if (i::FLAG_never_compact) return;
     195           5 :   FLAG_always_compact = true;
     196             : #ifdef VERIFY_HEAP
     197             :   FLAG_verify_heap = true;
     198             : #endif
     199             : 
     200           5 :   LocalContext context;
     201             :   Isolate* isolate = GetIsolateFrom(&context);
     202             :   Factory* factory = isolate->factory();
     203             :   Heap* heap = isolate->heap();
     204             :   HandleScope scope(isolate);
     205             :   Handle<JSFunction> function =
     206           5 :       factory->NewFunctionForTest(factory->function_string());
     207             : 
     208             :   // Start second old-space page so that keys land on evacuation candidate.
     209             :   Page* first_page = heap->old_space()->first_page();
     210           5 :   heap::SimulateFullSpace(heap->old_space());
     211             : 
     212             :   // Fill up weak map with keys on an evacuation candidate.
     213         325 :   Handle<JSObject> keys[32];
     214         325 :   for (int i = 0; i < 32; i++) {
     215         160 :     keys[i] = factory->NewJSObject(function, AllocationType::kOld);
     216         320 :     CHECK(!Heap::InYoungGeneration(*keys[i]));
     217         160 :     CHECK(!first_page->Contains(keys[i]->address()));
     218             :   }
     219           5 :   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
     220         325 :   for (int i = 0; i < 32; i++) {
     221             :     Handle<Smi> smi(Smi::FromInt(i), isolate);
     222         480 :     int32_t hash = keys[i]->GetOrCreateHash(isolate)->value();
     223         160 :     JSWeakCollection::Set(weakmap, keys[i], smi, hash);
     224             :   }
     225             : 
     226             :   // Force compacting garbage collection. The subsequent collections are used
     227             :   // to verify that key references were actually updated.
     228           5 :   CHECK(FLAG_always_compact);
     229           5 :   CcTest::CollectAllGarbage();
     230           5 :   CcTest::CollectAllGarbage();
     231           5 :   CcTest::CollectAllGarbage();
     232             : }
     233             : 
     234             : 
     235       26068 : TEST(Regress399527) {
     236           5 :   if (!FLAG_incremental_marking) return;
     237           5 :   CcTest::InitializeVM();
     238          10 :   v8::HandleScope scope(CcTest::isolate());
     239             :   Isolate* isolate = CcTest::i_isolate();
     240             :   Heap* heap = isolate->heap();
     241             :   {
     242             :     HandleScope scope(isolate);
     243           5 :     isolate->factory()->NewJSWeakMap();
     244           5 :     heap::SimulateIncrementalMarking(heap);
     245             :   }
     246             :   // The weak map is marked black here but leaving the handle scope will make
     247             :   // the object unreachable. Aborting incremental marking will clear all the
     248             :   // marking bits which makes the weak map garbage.
     249           5 :   CcTest::CollectAllGarbage();
     250             : }
     251             : 
     252       26068 : TEST(WeakMapsWithChainedEntries) {
     253             :   ManualGCScope manual_gc_scope;
     254           5 :   CcTest::InitializeVM();
     255           5 :   v8::Isolate* isolate = CcTest::isolate();
     256             :   i::Isolate* i_isolate = CcTest::i_isolate();
     257          10 :   v8::HandleScope scope(isolate);
     258             : 
     259             :   const int initial_gc_count = i_isolate->heap()->gc_count();
     260           5 :   Handle<JSWeakMap> weakmap1 = i_isolate->factory()->NewJSWeakMap();
     261           5 :   Handle<JSWeakMap> weakmap2 = i_isolate->factory()->NewJSWeakMap();
     262             :   v8::Global<v8::Object> g1;
     263             :   v8::Global<v8::Object> g2;
     264             :   {
     265          10 :     v8::HandleScope scope(isolate);
     266           5 :     v8::Local<v8::Object> o1 = v8::Object::New(isolate);
     267             :     g1.Reset(isolate, o1);
     268             :     g1.SetWeak();
     269           5 :     v8::Local<v8::Object> o2 = v8::Object::New(isolate);
     270             :     g2.Reset(isolate, o2);
     271             :     g2.SetWeak();
     272             :     Handle<Object> i_o1 = v8::Utils::OpenHandle(*o1);
     273             :     Handle<Object> i_o2 = v8::Utils::OpenHandle(*o2);
     274          10 :     int32_t hash1 = i_o1->GetOrCreateHash(i_isolate)->value();
     275          10 :     int32_t hash2 = i_o2->GetOrCreateHash(i_isolate)->value();
     276           5 :     JSWeakCollection::Set(weakmap1, i_o1, i_o2, hash1);
     277           5 :     JSWeakCollection::Set(weakmap2, i_o2, i_o1, hash2);
     278             :   }
     279           5 :   CcTest::CollectGarbage(OLD_SPACE);
     280           5 :   CHECK(g1.IsEmpty());
     281           5 :   CHECK(g2.IsEmpty());
     282           5 :   CHECK_EQ(1, i_isolate->heap()->gc_count() - initial_gc_count);
     283           5 : }
     284             : 
     285             : }  // namespace test_weakmaps
     286             : }  // namespace internal
     287       78189 : }  // namespace v8

Generated by: LCOV version 1.10