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/heap/heap-inl.h"
33 : #include "src/isolate.h"
34 : #include "src/objects-inl.h"
35 : #include "src/objects/hash-table-inl.h"
36 : #include "src/objects/js-collection-inl.h"
37 : #include "test/cctest/cctest.h"
38 : #include "test/cctest/heap/heap-utils.h"
39 :
40 : namespace v8 {
41 : namespace internal {
42 : namespace test_weakmaps {
43 :
44 : static Isolate* GetIsolateFrom(LocalContext* context) {
45 25 : return reinterpret_cast<Isolate*>((*context)->GetIsolate());
46 : }
47 :
48 : static int NumberOfWeakCalls = 0;
49 5 : static void WeakPointerCallback(const v8::WeakCallbackInfo<void>& data) {
50 : std::pair<v8::Persistent<v8::Value>*, int>* p =
51 : reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
52 : data.GetParameter());
53 5 : CHECK_EQ(1234, p->second);
54 5 : NumberOfWeakCalls++;
55 5 : p->first->Reset();
56 5 : }
57 :
58 :
59 26644 : TEST(Weakness) {
60 5 : FLAG_incremental_marking = false;
61 5 : LocalContext context;
62 : Isolate* isolate = GetIsolateFrom(&context);
63 : Factory* factory = isolate->factory();
64 : HandleScope scope(isolate);
65 5 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
66 : GlobalHandles* global_handles = isolate->global_handles();
67 :
68 : // Keep global reference to the key.
69 : Handle<Object> key;
70 : {
71 : HandleScope scope(isolate);
72 5 : Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
73 5 : Handle<JSObject> object = factory->NewJSObjectFromMap(map);
74 5 : key = global_handles->Create(*object);
75 : }
76 5 : CHECK(!global_handles->IsWeak(key.location()));
77 :
78 : // Put two chained entries into weak map.
79 : {
80 : HandleScope scope(isolate);
81 5 : Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
82 5 : Handle<JSObject> object = factory->NewJSObjectFromMap(map);
83 : Handle<Smi> smi(Smi::FromInt(23), isolate);
84 10 : int32_t hash = key->GetOrCreateHash(isolate)->value();
85 5 : JSWeakCollection::Set(weakmap, key, object, hash);
86 10 : int32_t object_hash = object->GetOrCreateHash(isolate)->value();
87 5 : JSWeakCollection::Set(weakmap, object, smi, object_hash);
88 : }
89 5 : CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
90 :
91 : // Force a full GC.
92 5 : CcTest::PreciseCollectAllGarbage();
93 5 : CHECK_EQ(0, NumberOfWeakCalls);
94 5 : CHECK_EQ(2, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
95 5 : CHECK_EQ(
96 : 0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
97 :
98 : // Make the global reference to the key weak.
99 : std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
100 : GlobalHandles::MakeWeak(
101 : key.location(), reinterpret_cast<void*>(&handle_and_id),
102 5 : &WeakPointerCallback, v8::WeakCallbackType::kParameter);
103 5 : CHECK(global_handles->IsWeak(key.location()));
104 :
105 5 : CcTest::PreciseCollectAllGarbage();
106 5 : CHECK_EQ(1, NumberOfWeakCalls);
107 5 : CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
108 5 : CHECK_EQ(
109 : 2, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
110 5 : }
111 :
112 :
113 26639 : TEST(Shrinking) {
114 0 : LocalContext context;
115 : Isolate* isolate = GetIsolateFrom(&context);
116 : Factory* factory = isolate->factory();
117 : HandleScope scope(isolate);
118 0 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
119 :
120 : // Check initial capacity.
121 0 : CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
122 :
123 : // Fill up weak map to trigger capacity change.
124 : {
125 : HandleScope scope(isolate);
126 0 : Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
127 0 : for (int i = 0; i < 32; i++) {
128 0 : Handle<JSObject> object = factory->NewJSObjectFromMap(map);
129 : Handle<Smi> smi(Smi::FromInt(i), isolate);
130 0 : int32_t object_hash = object->GetOrCreateHash(isolate)->value();
131 0 : JSWeakCollection::Set(weakmap, object, smi, object_hash);
132 : }
133 : }
134 :
135 : // Check increased capacity.
136 0 : CHECK_EQ(128, EphemeronHashTable::cast(weakmap->table())->Capacity());
137 :
138 : // Force a full GC.
139 0 : CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
140 0 : CHECK_EQ(
141 : 0, EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
142 0 : CcTest::PreciseCollectAllGarbage();
143 0 : CHECK_EQ(0, EphemeronHashTable::cast(weakmap->table())->NumberOfElements());
144 0 : CHECK_EQ(
145 : 32,
146 : EphemeronHashTable::cast(weakmap->table())->NumberOfDeletedElements());
147 :
148 : // Check shrunk capacity.
149 0 : CHECK_EQ(32, EphemeronHashTable::cast(weakmap->table())->Capacity());
150 0 : }
151 :
152 : namespace {
153 30 : bool EphemeronHashTableContainsKey(EphemeronHashTable table, HeapObject key) {
154 210 : for (int i = 0; i < table.Capacity(); ++i) {
155 120 : if (table->KeyAt(i) == key) return true;
156 : }
157 : return false;
158 : }
159 : } // namespace
160 :
161 26644 : TEST(WeakMapPromotion) {
162 5 : LocalContext context;
163 : Isolate* isolate = GetIsolateFrom(&context);
164 : Factory* factory = isolate->factory();
165 : HandleScope scope(isolate);
166 5 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
167 :
168 5 : CcTest::CollectAllGarbage();
169 5 : CHECK(ObjectInYoungGeneration(weakmap->table()));
170 :
171 5 : Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
172 5 : Handle<JSObject> object = factory->NewJSObjectFromMap(map);
173 : Handle<Smi> smi(Smi::FromInt(1), isolate);
174 10 : int32_t object_hash = object->GetOrCreateHash(isolate)->value();
175 5 : JSWeakCollection::Set(weakmap, object, smi, object_hash);
176 :
177 10 : CHECK(EphemeronHashTableContainsKey(
178 : EphemeronHashTable::cast(weakmap->table()), *object));
179 5 : CcTest::CollectAllGarbage();
180 :
181 5 : CHECK(ObjectInYoungGeneration(*object));
182 5 : CHECK(!ObjectInYoungGeneration(weakmap->table()));
183 5 : CHECK(EphemeronHashTableContainsKey(
184 : EphemeronHashTable::cast(weakmap->table()), *object));
185 :
186 5 : CcTest::CollectAllGarbage();
187 5 : CHECK(!ObjectInYoungGeneration(*object));
188 5 : CHECK(!ObjectInYoungGeneration(weakmap->table()));
189 5 : CHECK(EphemeronHashTableContainsKey(
190 : EphemeronHashTable::cast(weakmap->table()), *object));
191 5 : }
192 :
193 26644 : TEST(WeakMapScavenge) {
194 5 : LocalContext context;
195 : Isolate* isolate = GetIsolateFrom(&context);
196 : Factory* factory = isolate->factory();
197 : HandleScope scope(isolate);
198 5 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
199 :
200 5 : CcTest::CollectAllGarbage();
201 5 : CHECK(ObjectInYoungGeneration(weakmap->table()));
202 :
203 5 : Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
204 5 : Handle<JSObject> object = factory->NewJSObjectFromMap(map);
205 : Handle<Smi> smi(Smi::FromInt(1), isolate);
206 10 : int32_t object_hash = object->GetOrCreateHash(isolate)->value();
207 5 : JSWeakCollection::Set(weakmap, object, smi, object_hash);
208 :
209 10 : CHECK(EphemeronHashTableContainsKey(
210 : EphemeronHashTable::cast(weakmap->table()), *object));
211 :
212 5 : heap::GcAndSweep(isolate->heap(), NEW_SPACE);
213 5 : CHECK(ObjectInYoungGeneration(*object));
214 5 : CHECK(!ObjectInYoungGeneration(weakmap->table()));
215 5 : CHECK(EphemeronHashTableContainsKey(
216 : EphemeronHashTable::cast(weakmap->table()), *object));
217 :
218 5 : heap::GcAndSweep(isolate->heap(), NEW_SPACE);
219 5 : CHECK(!ObjectInYoungGeneration(*object));
220 5 : CHECK(!ObjectInYoungGeneration(weakmap->table()));
221 5 : CHECK(EphemeronHashTableContainsKey(
222 : EphemeronHashTable::cast(weakmap->table()), *object));
223 5 : }
224 :
225 : // Test that weak map values on an evacuation candidate which are not reachable
226 : // by other paths are correctly recorded in the slots buffer.
227 26644 : TEST(Regress2060a) {
228 5 : if (i::FLAG_never_compact) return;
229 5 : FLAG_always_compact = true;
230 5 : LocalContext context;
231 : Isolate* isolate = GetIsolateFrom(&context);
232 : Factory* factory = isolate->factory();
233 : Heap* heap = isolate->heap();
234 : HandleScope scope(isolate);
235 : Handle<JSFunction> function =
236 5 : factory->NewFunctionForTest(factory->function_string());
237 5 : Handle<JSObject> key = factory->NewJSObject(function);
238 5 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
239 :
240 : // Start second old-space page so that values land on evacuation candidate.
241 : Page* first_page = heap->old_space()->first_page();
242 5 : heap::SimulateFullSpace(heap->old_space());
243 :
244 : // Fill up weak map with values on an evacuation candidate.
245 : {
246 : HandleScope scope(isolate);
247 325 : for (int i = 0; i < 32; i++) {
248 : Handle<JSObject> object =
249 160 : factory->NewJSObject(function, AllocationType::kOld);
250 160 : CHECK(!Heap::InYoungGeneration(*object));
251 160 : CHECK(!first_page->Contains(object->address()));
252 320 : int32_t hash = key->GetOrCreateHash(isolate)->value();
253 160 : JSWeakCollection::Set(weakmap, key, object, hash);
254 : }
255 : }
256 :
257 : // Force compacting garbage collection.
258 5 : CHECK(FLAG_always_compact);
259 5 : CcTest::CollectAllGarbage();
260 : }
261 :
262 :
263 : // Test that weak map keys on an evacuation candidate which are reachable by
264 : // other strong paths are correctly recorded in the slots buffer.
265 26644 : TEST(Regress2060b) {
266 5 : if (i::FLAG_never_compact) return;
267 5 : FLAG_always_compact = true;
268 : #ifdef VERIFY_HEAP
269 : FLAG_verify_heap = true;
270 : #endif
271 :
272 5 : LocalContext context;
273 : Isolate* isolate = GetIsolateFrom(&context);
274 : Factory* factory = isolate->factory();
275 : Heap* heap = isolate->heap();
276 : HandleScope scope(isolate);
277 : Handle<JSFunction> function =
278 5 : factory->NewFunctionForTest(factory->function_string());
279 :
280 : // Start second old-space page so that keys land on evacuation candidate.
281 : Page* first_page = heap->old_space()->first_page();
282 5 : heap::SimulateFullSpace(heap->old_space());
283 :
284 : // Fill up weak map with keys on an evacuation candidate.
285 325 : Handle<JSObject> keys[32];
286 325 : for (int i = 0; i < 32; i++) {
287 160 : keys[i] = factory->NewJSObject(function, AllocationType::kOld);
288 320 : CHECK(!Heap::InYoungGeneration(*keys[i]));
289 160 : CHECK(!first_page->Contains(keys[i]->address()));
290 : }
291 5 : Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
292 325 : for (int i = 0; i < 32; i++) {
293 : Handle<Smi> smi(Smi::FromInt(i), isolate);
294 480 : int32_t hash = keys[i]->GetOrCreateHash(isolate)->value();
295 160 : JSWeakCollection::Set(weakmap, keys[i], smi, hash);
296 : }
297 :
298 : // Force compacting garbage collection. The subsequent collections are used
299 : // to verify that key references were actually updated.
300 5 : CHECK(FLAG_always_compact);
301 5 : CcTest::CollectAllGarbage();
302 5 : CcTest::CollectAllGarbage();
303 5 : CcTest::CollectAllGarbage();
304 : }
305 :
306 :
307 26644 : TEST(Regress399527) {
308 5 : if (!FLAG_incremental_marking) return;
309 5 : CcTest::InitializeVM();
310 10 : v8::HandleScope scope(CcTest::isolate());
311 : Isolate* isolate = CcTest::i_isolate();
312 : Heap* heap = isolate->heap();
313 : {
314 : HandleScope scope(isolate);
315 5 : isolate->factory()->NewJSWeakMap();
316 5 : heap::SimulateIncrementalMarking(heap);
317 : }
318 : // The weak map is marked black here but leaving the handle scope will make
319 : // the object unreachable. Aborting incremental marking will clear all the
320 : // marking bits which makes the weak map garbage.
321 5 : CcTest::CollectAllGarbage();
322 : }
323 :
324 26644 : TEST(WeakMapsWithChainedEntries) {
325 : ManualGCScope manual_gc_scope;
326 5 : CcTest::InitializeVM();
327 5 : v8::Isolate* isolate = CcTest::isolate();
328 : i::Isolate* i_isolate = CcTest::i_isolate();
329 10 : v8::HandleScope scope(isolate);
330 :
331 : const int initial_gc_count = i_isolate->heap()->gc_count();
332 5 : Handle<JSWeakMap> weakmap1 = i_isolate->factory()->NewJSWeakMap();
333 5 : Handle<JSWeakMap> weakmap2 = i_isolate->factory()->NewJSWeakMap();
334 : v8::Global<v8::Object> g1;
335 : v8::Global<v8::Object> g2;
336 : {
337 10 : v8::HandleScope scope(isolate);
338 5 : v8::Local<v8::Object> o1 = v8::Object::New(isolate);
339 : g1.Reset(isolate, o1);
340 : g1.SetWeak();
341 5 : v8::Local<v8::Object> o2 = v8::Object::New(isolate);
342 : g2.Reset(isolate, o2);
343 : g2.SetWeak();
344 : Handle<Object> i_o1 = v8::Utils::OpenHandle(*o1);
345 : Handle<Object> i_o2 = v8::Utils::OpenHandle(*o2);
346 10 : int32_t hash1 = i_o1->GetOrCreateHash(i_isolate)->value();
347 10 : int32_t hash2 = i_o2->GetOrCreateHash(i_isolate)->value();
348 5 : JSWeakCollection::Set(weakmap1, i_o1, i_o2, hash1);
349 5 : JSWeakCollection::Set(weakmap2, i_o2, i_o1, hash2);
350 : }
351 5 : CcTest::CollectGarbage(OLD_SPACE);
352 5 : CHECK(g1.IsEmpty());
353 5 : CHECK(g2.IsEmpty());
354 5 : CHECK_EQ(1, i_isolate->heap()->gc_count() - initial_gc_count);
355 5 : }
356 :
357 : } // namespace test_weakmaps
358 : } // namespace internal
359 79917 : } // namespace v8
|