04f41b9aee979161b05a6ebb2c74e5b6792c7459
[platform/upstream/nodejs.git] / deps / v8 / test / cctest / test-weakmaps.cc
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/v8.h"
31
32 #include "src/global-handles.h"
33 #include "src/snapshot.h"
34 #include "test/cctest/cctest.h"
35
36 using namespace v8::internal;
37
38
39 static Isolate* GetIsolateFrom(LocalContext* context) {
40   return reinterpret_cast<Isolate*>((*context)->GetIsolate());
41 }
42
43
44 static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) {
45   Handle<JSWeakMap> weakmap = isolate->factory()->NewJSWeakMap();
46   // Do not leak handles for the hash table, it would make entries strong.
47   {
48     HandleScope scope(isolate);
49     Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
50     weakmap->set_table(*table);
51   }
52   return weakmap;
53 }
54
55 static int NumberOfWeakCalls = 0;
56 static void WeakPointerCallback(
57     const v8::WeakCallbackData<v8::Value, void>& data) {
58   std::pair<v8::Persistent<v8::Value>*, int>* p =
59       reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
60           data.GetParameter());
61   DCHECK_EQ(1234, p->second);
62   NumberOfWeakCalls++;
63   p->first->Reset();
64 }
65
66
67 TEST(Weakness) {
68   FLAG_incremental_marking = false;
69   LocalContext context;
70   Isolate* isolate = GetIsolateFrom(&context);
71   Factory* factory = isolate->factory();
72   Heap* heap = isolate->heap();
73   HandleScope scope(isolate);
74   Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
75   GlobalHandles* global_handles = isolate->global_handles();
76
77   // Keep global reference to the key.
78   Handle<Object> key;
79   {
80     HandleScope scope(isolate);
81     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
82     Handle<JSObject> object = factory->NewJSObjectFromMap(map);
83     key = global_handles->Create(*object);
84   }
85   CHECK(!global_handles->IsWeak(key.location()));
86
87   // Put two chained entries into weak map.
88   {
89     HandleScope scope(isolate);
90     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
91     Handle<JSObject> object = factory->NewJSObjectFromMap(map);
92     Handle<Smi> smi(Smi::FromInt(23), isolate);
93     Runtime::WeakCollectionSet(weakmap, key, object);
94     Runtime::WeakCollectionSet(weakmap, object, smi);
95   }
96   CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
97
98   // Force a full GC.
99   heap->CollectAllGarbage(false);
100   CHECK_EQ(0, NumberOfWeakCalls);
101   CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
102   CHECK_EQ(
103       0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
104
105   // Make the global reference to the key weak.
106   {
107     HandleScope scope(isolate);
108     std::pair<Handle<Object>*, int> handle_and_id(&key, 1234);
109     GlobalHandles::MakeWeak(key.location(),
110                             reinterpret_cast<void*>(&handle_and_id),
111                             &WeakPointerCallback);
112   }
113   CHECK(global_handles->IsWeak(key.location()));
114
115   // Force a full GC.
116   // Perform two consecutive GCs because the first one will only clear
117   // weak references whereas the second one will also clear weak maps.
118   heap->CollectAllGarbage(false);
119   CHECK_EQ(1, NumberOfWeakCalls);
120   CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
121   CHECK_EQ(
122       0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
123   heap->CollectAllGarbage(false);
124   CHECK_EQ(1, NumberOfWeakCalls);
125   CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
126   CHECK_EQ(2,
127            ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
128 }
129
130
131 TEST(Shrinking) {
132   LocalContext context;
133   Isolate* isolate = GetIsolateFrom(&context);
134   Factory* factory = isolate->factory();
135   Heap* heap = isolate->heap();
136   HandleScope scope(isolate);
137   Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
138
139   // Check initial capacity.
140   CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
141
142   // Fill up weak map to trigger capacity change.
143   {
144     HandleScope scope(isolate);
145     Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
146     for (int i = 0; i < 32; i++) {
147       Handle<JSObject> object = factory->NewJSObjectFromMap(map);
148       Handle<Smi> smi(Smi::FromInt(i), isolate);
149       Runtime::WeakCollectionSet(weakmap, object, smi);
150     }
151   }
152
153   // Check increased capacity.
154   CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
155
156   // Force a full GC.
157   CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
158   CHECK_EQ(
159       0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
160   heap->CollectAllGarbage(false);
161   CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
162   CHECK_EQ(
163       32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
164
165   // Check shrunk capacity.
166   CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
167 }
168
169
170 // Test that weak map values on an evacuation candidate which are not reachable
171 // by other paths are correctly recorded in the slots buffer.
172 TEST(Regress2060a) {
173   if (i::FLAG_never_compact) return;
174   FLAG_always_compact = true;
175   LocalContext context;
176   Isolate* isolate = GetIsolateFrom(&context);
177   Factory* factory = isolate->factory();
178   Heap* heap = isolate->heap();
179   HandleScope scope(isolate);
180   Handle<JSFunction> function = factory->NewFunction(
181       factory->function_string());
182   Handle<JSObject> key = factory->NewJSObject(function);
183   Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
184
185   // Start second old-space page so that values land on evacuation candidate.
186   Page* first_page = heap->old_pointer_space()->anchor()->next_page();
187   factory->NewFixedArray(900 * KB / kPointerSize, TENURED);
188
189   // Fill up weak map with values on an evacuation candidate.
190   {
191     HandleScope scope(isolate);
192     for (int i = 0; i < 32; i++) {
193       Handle<JSObject> object = factory->NewJSObject(function, TENURED);
194       CHECK(!heap->InNewSpace(object->address()));
195       CHECK(!first_page->Contains(object->address()));
196       Runtime::WeakCollectionSet(weakmap, key, object);
197     }
198   }
199
200   // Force compacting garbage collection.
201   CHECK(FLAG_always_compact);
202   heap->CollectAllGarbage(Heap::kNoGCFlags);
203 }
204
205
206 // Test that weak map keys on an evacuation candidate which are reachable by
207 // other strong paths are correctly recorded in the slots buffer.
208 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   Isolate* isolate = GetIsolateFrom(&context);
217   Factory* factory = isolate->factory();
218   Heap* heap = isolate->heap();
219   HandleScope scope(isolate);
220   Handle<JSFunction> function = factory->NewFunction(
221       factory->function_string());
222
223   // Start second old-space page so that keys land on evacuation candidate.
224   Page* first_page = heap->old_pointer_space()->anchor()->next_page();
225   factory->NewFixedArray(900 * KB / kPointerSize, TENURED);
226
227   // Fill up weak map with keys on an evacuation candidate.
228   Handle<JSObject> keys[32];
229   for (int i = 0; i < 32; i++) {
230     keys[i] = factory->NewJSObject(function, TENURED);
231     CHECK(!heap->InNewSpace(keys[i]->address()));
232     CHECK(!first_page->Contains(keys[i]->address()));
233   }
234   Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate);
235   for (int i = 0; i < 32; i++) {
236     Handle<Smi> smi(Smi::FromInt(i), isolate);
237     Runtime::WeakCollectionSet(weakmap, keys[i], smi);
238   }
239
240   // Force compacting garbage collection. The subsequent collections are used
241   // to verify that key references were actually updated.
242   CHECK(FLAG_always_compact);
243   heap->CollectAllGarbage(Heap::kNoGCFlags);
244   heap->CollectAllGarbage(Heap::kNoGCFlags);
245   heap->CollectAllGarbage(Heap::kNoGCFlags);
246 }
247
248
249 TEST(Regress399527) {
250   CcTest::InitializeVM();
251   v8::HandleScope scope(CcTest::isolate());
252   Isolate* isolate = CcTest::i_isolate();
253   Heap* heap = isolate->heap();
254   {
255     HandleScope scope(isolate);
256     AllocateJSWeakMap(isolate);
257     SimulateIncrementalMarking(heap);
258   }
259   // The weak map is marked black here but leaving the handle scope will make
260   // the object unreachable. Aborting incremental marking will clear all the
261   // marking bits which makes the weak map garbage.
262   heap->CollectAllGarbage(Heap::kAbortIncrementalMarkingMask);
263 }