test-weakmaps.cc 9.73 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
#include "test/cctest/heap/utils-inl.h"
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
  CHECK_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
    Handle<Smi> smi(Smi::FromInt(23), isolate);
92
    int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
93
    JSWeakCollection::Set(weakmap, key, object, hash);
94
    int32_t object_hash = Object::GetOrCreateHash(isolate, object)->value();
95
    JSWeakCollection::Set(weakmap, object, smi, object_hash);
96
  }
97
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
98 99

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

  // Make the global reference to the key weak.
  {
108
    HandleScope scope(isolate);
109 110 111 112
    std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
    GlobalHandles::MakeWeak(key.location(),
                            reinterpret_cast<void*>(&handle_and_id),
                            &WeakPointerCallback);
113 114 115 116 117 118
  }
  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.
119
  heap->CollectAllGarbage(false);
120
  CHECK_EQ(1, NumberOfWeakCalls);
121
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
122 123
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
124
  heap->CollectAllGarbage(false);
125
  CHECK_EQ(1, NumberOfWeakCalls);
126
  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
127 128
  CHECK_EQ(2,
           ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
129 130 131 132 133
}


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

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

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

  // Check increased capacity.
156
  CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
157 158

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

  // Check shrunk capacity.
168
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
169
}
170 171 172 173


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

  // Start second old-space page so that values land on evacuation candidate.
188
  Page* first_page = heap->old_space()->anchor()->next_page();
189
  SimulateFullSpace(heap->old_space());
190 191 192

  // Fill up weak map with values on an evacuation candidate.
  {
193
    HandleScope scope(isolate);
194
    for (int i = 0; i < 32; i++) {
195
      Handle<JSObject> object = factory->NewJSObject(function, TENURED);
196
      CHECK(!heap->InNewSpace(*object));
197
      CHECK(!first_page->Contains(object->address()));
198
      int32_t hash = Object::GetOrCreateHash(isolate, key)->value();
199
      JSWeakCollection::Set(weakmap, key, object, hash);
200 201 202 203 204
    }
  }

  // Force compacting garbage collection.
  CHECK(FLAG_always_compact);
205
  heap->CollectAllGarbage();
206
}
207 208 209 210 211


// 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) {
212
  if (i::FLAG_never_compact) return;
213
  FLAG_always_compact = true;
214
#ifdef VERIFY_HEAP
215
  FLAG_verify_heap = true;
216
#endif
217

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

  // Start second old-space page so that keys land on evacuation candidate.
227
  Page* first_page = heap->old_space()->anchor()->next_page();
228
  SimulateFullSpace(heap->old_space());
229 230 231 232

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

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


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.
266
  heap->CollectAllGarbage();
267
}