LCOV - code coverage report
Current view: top level - test/cctest - test-weakmaps.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 100 116 86.2 %
Date: 2019-02-19 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       25880 : TEST(Weakness) {
      60           5 :   FLAG_incremental_marking = false;
      61           5 :   LocalContext context;
      62           5 :   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           5 :       2, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
     110           5 : }
     111             : 
     112             : 
     113       25875 : 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       25880 : 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           5 :   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         165 :     for (int i = 0; i < 32; i++) {
     176         160 :       Handle<JSObject> object = factory->NewJSObject(function, TENURED);
     177         160 :       CHECK(!Heap::InYoungGeneration(*object));
     178         160 :       CHECK(!first_page->Contains(object->address()));
     179         320 :       int32_t hash = key->GetOrCreateHash(isolate)->value();
     180         160 :       JSWeakCollection::Set(weakmap, key, object, hash);
     181             :     }
     182             :   }
     183             : 
     184             :   // Force compacting garbage collection.
     185           5 :   CHECK(FLAG_always_compact);
     186          10 :   CcTest::CollectAllGarbage();
     187             : }
     188             : 
     189             : 
     190             : // Test that weak map keys on an evacuation candidate which are reachable by
     191             : // other strong paths are correctly recorded in the slots buffer.
     192       25880 : TEST(Regress2060b) {
     193           5 :   if (i::FLAG_never_compact) return;
     194           5 :   FLAG_always_compact = true;
     195             : #ifdef VERIFY_HEAP
     196             :   FLAG_verify_heap = true;
     197             : #endif
     198             : 
     199           5 :   LocalContext context;
     200             :   Isolate* isolate = GetIsolateFrom(&context);
     201             :   Factory* factory = isolate->factory();
     202           5 :   Heap* heap = isolate->heap();
     203             :   HandleScope scope(isolate);
     204             :   Handle<JSFunction> function =
     205           5 :       factory->NewFunctionForTest(factory->function_string());
     206             : 
     207             :   // Start second old-space page so that keys land on evacuation candidate.
     208             :   Page* first_page = heap->old_space()->first_page();
     209           5 :   heap::SimulateFullSpace(heap->old_space());
     210             : 
     211             :   // Fill up weak map with keys on an evacuation candidate.
     212         165 :   Handle<JSObject> keys[32];
     213         160 :   for (int i = 0; i < 32; i++) {
     214         160 :     keys[i] = factory->NewJSObject(function, TENURED);
     215         320 :     CHECK(!Heap::InYoungGeneration(*keys[i]));
     216         160 :     CHECK(!first_page->Contains(keys[i]->address()));
     217             :   }
     218           5 :   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
     219         165 :   for (int i = 0; i < 32; i++) {
     220             :     Handle<Smi> smi(Smi::FromInt(i), isolate);
     221         480 :     int32_t hash = keys[i]->GetOrCreateHash(isolate)->value();
     222         160 :     JSWeakCollection::Set(weakmap, keys[i], smi, hash);
     223             :   }
     224             : 
     225             :   // Force compacting garbage collection. The subsequent collections are used
     226             :   // to verify that key references were actually updated.
     227           5 :   CHECK(FLAG_always_compact);
     228           5 :   CcTest::CollectAllGarbage();
     229           5 :   CcTest::CollectAllGarbage();
     230          10 :   CcTest::CollectAllGarbage();
     231             : }
     232             : 
     233             : 
     234       25880 : TEST(Regress399527) {
     235           5 :   if (!FLAG_incremental_marking) return;
     236           5 :   CcTest::InitializeVM();
     237           5 :   v8::HandleScope scope(CcTest::isolate());
     238             :   Isolate* isolate = CcTest::i_isolate();
     239           5 :   Heap* heap = isolate->heap();
     240             :   {
     241             :     HandleScope scope(isolate);
     242           5 :     isolate->factory()->NewJSWeakMap();
     243           5 :     heap::SimulateIncrementalMarking(heap);
     244             :   }
     245             :   // The weak map is marked black here but leaving the handle scope will make
     246             :   // the object unreachable. Aborting incremental marking will clear all the
     247             :   // marking bits which makes the weak map garbage.
     248           5 :   CcTest::CollectAllGarbage();
     249             : }
     250             : 
     251       25880 : TEST(WeakMapsWithChainedEntries) {
     252             :   ManualGCScope manual_gc_scope;
     253           5 :   CcTest::InitializeVM();
     254           5 :   v8::Isolate* isolate = CcTest::isolate();
     255             :   i::Isolate* i_isolate = CcTest::i_isolate();
     256          10 :   v8::HandleScope scope(isolate);
     257             : 
     258           5 :   const int initial_gc_count = i_isolate->heap()->gc_count();
     259           5 :   Handle<JSWeakMap> weakmap1 = i_isolate->factory()->NewJSWeakMap();
     260           5 :   Handle<JSWeakMap> weakmap2 = i_isolate->factory()->NewJSWeakMap();
     261             :   v8::Global<v8::Object> g1;
     262             :   v8::Global<v8::Object> g2;
     263             :   {
     264           5 :     v8::HandleScope scope(isolate);
     265           5 :     v8::Local<v8::Object> o1 = v8::Object::New(isolate);
     266             :     g1.Reset(isolate, o1);
     267             :     g1.SetWeak();
     268           5 :     v8::Local<v8::Object> o2 = v8::Object::New(isolate);
     269             :     g2.Reset(isolate, o2);
     270             :     g2.SetWeak();
     271             :     Handle<Object> i_o1 = v8::Utils::OpenHandle(*o1);
     272             :     Handle<Object> i_o2 = v8::Utils::OpenHandle(*o2);
     273          10 :     int32_t hash1 = i_o1->GetOrCreateHash(i_isolate)->value();
     274          10 :     int32_t hash2 = i_o2->GetOrCreateHash(i_isolate)->value();
     275           5 :     JSWeakCollection::Set(weakmap1, i_o1, i_o2, hash1);
     276           5 :     JSWeakCollection::Set(weakmap2, i_o2, i_o1, hash2);
     277             :   }
     278           5 :   CcTest::CollectGarbage(OLD_SPACE);
     279           5 :   CHECK(g1.IsEmpty());
     280           5 :   CHECK(g2.IsEmpty());
     281          10 :   CHECK_EQ(1, i_isolate->heap()->gc_count() - initial_gc_count);
     282           5 : }
     283             : 
     284             : }  // namespace test_weakmaps
     285             : }  // namespace internal
     286       77625 : }  // namespace v8

Generated by: LCOV version 1.10