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