test-weakmaps.cc 9.19 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/factory.h"
31
#include "src/global-handles.h"
32 33
#include "src/isolate.h"
#include "src/objects-inl.h"
34
#include "test/cctest/cctest.h"
35
#include "test/cctest/heap/heap-utils.h"
36

37 38
namespace v8 {
namespace internal {
39
namespace test_weakmaps {
40

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

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


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

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

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

  // Force a full GC.
89
  CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
90
  CHECK_EQ(0, NumberOfWeakCalls);
91
  CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
92 93
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
94 95

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

102
  CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
103
  CHECK_EQ(1, NumberOfWeakCalls);
104
  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
105
  CHECK_EQ(2,
106
           ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
107 108 109 110 111
}


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

  // Check initial capacity.
118
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
119 120 121

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

  // Check increased capacity.
133
  CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
134 135

  // Force a full GC.
136 137 138
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
  CHECK_EQ(
      0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
139
  CcTest::CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
140 141 142
  CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
  CHECK_EQ(
      32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
143 144

  // Check shrunk capacity.
145
  CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
146
}
147 148 149 150


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

  // Start second old-space page so that values land on evacuation candidate.
165
  Page* first_page = heap->old_space()->anchor()->next_page();
166
  heap::SimulateFullSpace(heap->old_space());
167 168 169

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

  // Force compacting garbage collection.
  CHECK(FLAG_always_compact);
182
  CcTest::CollectAllGarbage();
183
}
184 185 186 187 188


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

195
  LocalContext context;
196 197 198
  Isolate* isolate = GetIsolateFrom(&context);
  Factory* factory = isolate->factory();
  Heap* heap = isolate->heap();
199
  HandleScope scope(isolate);
200 201
  Handle<JSFunction> function =
      factory->NewFunctionForTest(factory->function_string());
202 203

  // Start second old-space page so that keys land on evacuation candidate.
204
  Page* first_page = heap->old_space()->anchor()->next_page();
205
  heap::SimulateFullSpace(heap->old_space());
206 207 208 209

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

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


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

247
}  // namespace test_weakmaps
248 249
}  // namespace internal
}  // namespace v8