LCOV - code coverage report
Current view: top level - test/cctest - test-weakmaps.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 81 97 83.5 %
Date: 2019-01-20 Functions: 7 8 87.5 %

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

Generated by: LCOV version 1.10