test-weakmaps.cc 9.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2011 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.

28 29
#include <utility>

30
#include "src/v8.h"
31

32 33
#include "src/global-handles.h"
#include "test/cctest/cctest.h"
34 35 36 37

using namespace v8::internal;


38 39 40 41 42
static Isolate* GetIsolateFrom(LocalContext* context) {
  return reinterpret_cast<Isolate*>((*context)->GetIsolate());
}


43
static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) {
yurys's avatar
yurys committed
44
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
45 46 47 48 49 50
  // Do not leak handles for the hash table, it would make entries strong.
  {
    HandleScope scope(isolate);
    Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
    weakmap->set_table(*table);
  }
51 52 53 54
  return weakmap;
}

static int NumberOfWeakCalls = 0;
55 56 57 58 59
static void WeakPointerCallback(
    const v8::WeakCallbackData<v8::Value, void>& data) {
  std::pair<v8::Persistent<v8::Value>*, int>* p =
      reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
          data.GetParameter());
60
  DCHECK_EQ(1234, p->second);
61
  NumberOfWeakCalls++;
62
  p->first->Reset();
63 64 65 66
}


TEST(Weakness) {
67
  FLAG_incremental_marking = false;
68
  LocalContext context;
69 70 71
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
72
  HandleScope scope(isolate);
73 74
  Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
  GlobalHandles* global_handles = isolate->global_handles();
75 76 77 78

  // Keep global reference to the key.
  Handle<Object> key;
  {
79
    HandleScope scope(isolate);
80 81
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
    Handle<JSObject> object = factory->NewJSObjectFromMap(map);
82 83 84 85
    key = global_handles->Create(*object);
  }
  CHECK(!global_handles->IsWeak(key.location()));

86
  // Put two chained entries into weak map.
87
  {
88
    HandleScope scope(isolate);
89 90
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
    Handle<JSObject> object = factory->NewJSObjectFromMap(map);
yurys's avatar
yurys committed
91 92 93
    Handle<Smi> smi(Smi::FromInt(23), isolate);
    Runtime::WeakCollectionSet(weakmap, key, object);
    Runtime::WeakCollectionSet(weakmap, object, smi);
94
  }
95
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
96 97

  // Force a full GC.
98
  heap->CollectAllGarbage(false);
99
  CHECK_EQ(0, NumberOfWeakCalls);
100
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
101 102
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
103 104 105

  // Make the global reference to the key weak.
  {
106
    HandleScope scope(isolate);
107 108 109 110
    std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
    GlobalHandles::MakeWeak(key.location(),
                            reinterpret_cast<void*>(&handle_and_id),
                            &WeakPointerCallback);
111 112 113 114 115 116
  }
  CHECK(global_handles->IsWeak(key.location()));

  // Force a full GC.
  // Perform two consecutive GCs because the first one will only clear
  // weak references whereas the second one will also clear weak maps.
117
  heap->CollectAllGarbage(false);
118
  CHECK_EQ(1, NumberOfWeakCalls);
119
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
120 121
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
122
  heap->CollectAllGarbage(false);
123
  CHECK_EQ(1, NumberOfWeakCalls);
124
  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
125 126
  CHECK_EQ(2,
           ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
127 128 129 130 131
}


TEST(Shrinking) {
  LocalContext context;
132 133 134
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
135
  HandleScope scope(isolate);
136
  Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
137 138

  // Check initial capacity.
139
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
140 141 142

  // Fill up weak map to trigger capacity change.
  {
143
    HandleScope scope(isolate);
144
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
145
    for (int i = 0; i < 32; i++) {
146
      Handle<JSObject> object = factory->NewJSObjectFromMap(map);
yurys's avatar
yurys committed
147 148
      Handle<Smi> smi(Smi::FromInt(i), isolate);
      Runtime::WeakCollectionSet(weakmap, object, smi);
149 150 151 152
    }
  }

  // Check increased capacity.
153
  CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
154 155

  // Force a full GC.
156 157 158
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
159
  heap->CollectAllGarbage(false);
160 161 162
  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
  CHECK_EQ(
      32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
163 164

  // Check shrunk capacity.
165
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
166
}
167 168 169 170


// Test that weak map values on an evacuation candidate which are not reachable
// by other paths are correctly recorded in the slots buffer.
171
TEST(Regress2060a) {
172
  if (i::FLAG_never_compact) return;
173 174
  FLAG_always_compact = true;
  LocalContext context;
175 176 177
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
178
  HandleScope scope(isolate);
179 180
  Handle<JSFunction> function = factory->NewFunction(
      factory->function_string());
181 182
  Handle<JSObject> key = factory->NewJSObject(function);
  Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
183 184

  // Start second old-space page so that values land on evacuation candidate.
185
  Page* first_page = heap->old_space()->anchor()->next_page();
186 187
  int dummy_array_size = Page::kMaxRegularHeapObjectSize - 92 * KB;
  factory->NewFixedArray(dummy_array_size / kPointerSize, TENURED);
188 189 190

  // Fill up weak map with values on an evacuation candidate.
  {
191
    HandleScope scope(isolate);
192
    for (int i = 0; i < 32; i++) {
193 194
      Handle<JSObject> object = factory->NewJSObject(function, TENURED);
      CHECK(!heap->InNewSpace(object->address()));
195
      CHECK(!first_page->Contains(object->address()));
yurys's avatar
yurys committed
196
      Runtime::WeakCollectionSet(weakmap, key, object);
197 198 199 200 201
    }
  }

  // Force compacting garbage collection.
  CHECK(FLAG_always_compact);
202
  heap->CollectAllGarbage();
203
}
204 205 206 207 208


// Test that weak map keys on an evacuation candidate which are reachable by
// other strong paths are correctly recorded in the slots buffer.
TEST(Regress2060b) {
209
  if (i::FLAG_never_compact) return;
210
  FLAG_always_compact = true;
211
#ifdef VERIFY_HEAP
212
  FLAG_verify_heap = true;
213
#endif
214

215
  LocalContext context;
216 217 218
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
219
  HandleScope scope(isolate);
220 221
  Handle<JSFunction> function = factory->NewFunction(
      factory->function_string());
222 223

  // Start second old-space page so that keys land on evacuation candidate.
224
  Page* first_page = heap->old_space()->anchor()->next_page();
225 226
  int dummy_array_size = Page::kMaxRegularHeapObjectSize - 92 * KB;
  factory->NewFixedArray(dummy_array_size / kPointerSize, TENURED);
227 228 229 230

  // Fill up weak map with keys on an evacuation candidate.
  Handle<JSObject> keys[32];
  for (int i = 0; i < 32; i++) {
231 232
    keys[i] = factory->NewJSObject(function, TENURED);
    CHECK(!heap->InNewSpace(keys[i]->address()));
233 234
    CHECK(!first_page->Contains(keys[i]->address()));
  }
235
  Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
236
  for (int i = 0; i < 32; i++) {
yurys's avatar
yurys committed
237 238
    Handle<Smi> smi(Smi::FromInt(i), isolate);
    Runtime::WeakCollectionSet(weakmap, keys[i], smi);
239 240 241 242 243
  }

  // Force compacting garbage collection. The subsequent collections are used
  // to verify that key references were actually updated.
  CHECK(FLAG_always_compact);
244 245 246
  heap->CollectAllGarbage();
  heap->CollectAllGarbage();
  heap->CollectAllGarbage();
247
}
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262


TEST(Regress399527) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Heap* heap = isolate->heap();
  {
    HandleScope scope(isolate);
    AllocateJSWeakMap(isolate);
    SimulateIncrementalMarking(heap);
  }
  // The weak map is marked black here but leaving the handle scope will make
  // the object unreachable. Aborting incremental marking will clear all the
  // marking bits which makes the weak map garbage.
263
  heap->CollectAllGarbage();
264
}