test-weakmaps.cc 9.21 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/global-handles.h"
31
#include "src/heap/factory.h"
32 33
#include "src/isolate.h"
#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 40
namespace v8 {
namespace internal {
41
namespace test_weakmaps {
42

43 44 45 46
static Isolate* GetIsolateFrom(LocalContext* context) {
  return reinterpret_cast<Isolate*>((*context)->GetIsolate());
}

47
static int NumberOfWeakCalls = 0;
48
static void WeakPointerCallback(const v8::WeakCallbackInfo<void>& data) {
49 50 51
  std::pair<v8::Persistent<v8::Value>*, int>* p =
      reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
          data.GetParameter());
52
  CHECK_EQ(1234, p->second);
53
  NumberOfWeakCalls++;
54
  p->first->Reset();
55 56 57 58
}


TEST(Weakness) {
59
  FLAG_incremental_marking = false;
60
  LocalContext context;
61 62
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
63
  HandleScope scope(isolate);
64
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
65
  GlobalHandles* global_handles = isolate->global_handles();
66 67 68 69

  // Keep global reference to the key.
  Handle<Object> key;
  {
70
    HandleScope scope(isolate);
71 72
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
    Handle<JSObject> object = factory->NewJSObjectFromMap(map);
73 74 75 76
    key = global_handles->Create(*object);
  }
  CHECK(!global_handles->IsWeak(key.location()));

77
  // Put two chained entries into weak map.
78
  {
79
    HandleScope scope(isolate);
80 81
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
    Handle<JSObject> object = factory->NewJSObjectFromMap(map);
yurys's avatar
yurys committed
82
    Handle<Smi> smi(Smi::FromInt(23), isolate);
83
    int32_t hash = key->GetOrCreateHash(isolate)->value();
84
    JSWeakCollection::Set(weakmap, key, object, hash);
85
    int32_t object_hash = object->GetOrCreateHash(isolate)->value();
86
    JSWeakCollection::Set(weakmap, object, smi, object_hash);
87
  }
88
  CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
89 90

  // Force a full GC.
91
  CcTest::PreciseCollectAllGarbage();
92
  CHECK_EQ(0, NumberOfWeakCalls);
93
  CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
94
  CHECK_EQ(
95
      0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
96 97

  // Make the global reference to the key weak.
98 99 100 101
  std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
  GlobalHandles::MakeWeak(
      key.location(), reinterpret_cast<void*>(&handle_and_id),
      &WeakPointerCallback, v8::WeakCallbackType::kParameter);
102 103
  CHECK(global_handles->IsWeak(key.location()));

104
  CcTest::PreciseCollectAllGarbage();
105
  CHECK_EQ(1, NumberOfWeakCalls);
106 107 108
  CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
  CHECK_EQ(
      2, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
109 110 111 112 113
}


TEST(Shrinking) {
  LocalContext context;
114 115
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
116
  HandleScope scope(isolate);
117
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
118 119

  // Check initial capacity.
120
  CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
121 122 123

  // Fill up weak map to trigger capacity change.
  {
124
    HandleScope scope(isolate);
125
    Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
126
    for (int i = 0; i < 32; i++) {
127
      Handle<JSObject> object = factory->NewJSObjectFromMap(map);
yurys's avatar
yurys committed
128
      Handle<Smi> smi(Smi::FromInt(i), isolate);
129
      int32_t object_hash = object->GetOrCreateHash(isolate)->value();
130
      JSWeakCollection::Set(weakmap, object, smi, object_hash);
131 132 133 134
    }
  }

  // Check increased capacity.
135
  CHECK_EQ(128, EphemeronHashTable::cast(weakmap->table())->Capacity());
136 137

  // Force a full GC.
138
  CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
139
  CHECK_EQ(
140
      0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
141
  CcTest::PreciseCollectAllGarbage();
142
  CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
143
  CHECK_EQ(
144 145
      32,
      EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
146 147

  // Check shrunk capacity.
148
  CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
149
}
150 151 152 153


// Test that weak map values on an evacuation candidate which are not reachable
// by other paths are correctly recorded in the slots buffer.
154
TEST(Regress2060a) {
155
  if (i::FLAG_never_compact) return;
156 157
  FLAG_always_compact = true;
  LocalContext context;
158 159 160
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
161
  HandleScope scope(isolate);
162 163
  Handle<JSFunction> function =
      factory->NewFunctionForTest(factory->function_string());
164
  Handle<JSObject> key = factory->NewJSObject(function);
165
  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
  heap::SimulateFullSpace(heap->old_space());
170 171 172

  // Fill up weak map with values on an evacuation candidate.
  {
173
    HandleScope scope(isolate);
174
    for (int i = 0; i < 32; i++) {
175
      Handle<JSObject> object = factory->NewJSObject(function, TENURED);
176
      CHECK(!Heap::InNewSpace(*object));
177
      CHECK(!first_page->Contains(object->address()));
178
      int32_t hash = key->GetOrCreateHash(isolate)->value();
179
      JSWeakCollection::Set(weakmap, key, object, hash);
180 181 182 183 184
    }
  }

  // Force compacting garbage collection.
  CHECK(FLAG_always_compact);
185
  CcTest::CollectAllGarbage();
186
}
187 188 189 190 191


// 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) {
192
  if (i::FLAG_never_compact) return;
193
  FLAG_always_compact = true;
194
#ifdef VERIFY_HEAP
195
  FLAG_verify_heap = true;
196
#endif
197

198
  LocalContext context;
199 200 201
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
202
  HandleScope scope(isolate);
203 204
  Handle<JSFunction> function =
      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
  heap::SimulateFullSpace(heap->old_space());
209 210 211 212

  // Fill up weak map with keys on an evacuation candidate.
  Handle<JSObject> keys[32];
  for (int i = 0; i < 32; i++) {
213
    keys[i] = factory->NewJSObject(function, TENURED);
214
    CHECK(!Heap::InNewSpace(*keys[i]));
215 216
    CHECK(!first_page->Contains(keys[i]->address()));
  }
217
  Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
218
  for (int i = 0; i < 32; i++) {
yurys's avatar
yurys committed
219
    Handle<Smi> smi(Smi::FromInt(i), isolate);
220
    int32_t hash = keys[i]->GetOrCreateHash(isolate)->value();
221
    JSWeakCollection::Set(weakmap, keys[i], smi, hash);
222 223 224 225 226
  }

  // Force compacting garbage collection. The subsequent collections are used
  // to verify that key references were actually updated.
  CHECK(FLAG_always_compact);
227 228 229
  CcTest::CollectAllGarbage();
  CcTest::CollectAllGarbage();
  CcTest::CollectAllGarbage();
230
}
231 232 233


TEST(Regress399527) {
234
  if (!FLAG_incremental_marking) return;
235 236 237 238 239 240
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Heap* heap = isolate->heap();
  {
    HandleScope scope(isolate);
241
    isolate->factory()->NewJSWeakMap();
242
    heap::SimulateIncrementalMarking(heap);
243 244 245 246
  }
  // 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.
247
  CcTest::CollectAllGarbage();
248
}
249

250
}  // namespace test_weakmaps
251 252
}  // namespace internal
}  // namespace v8