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
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.
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.
32 #include "src/global-handles.h"
33 #include "src/snapshot.h"
34 #include "test/cctest/cctest.h"
36 using namespace v8::internal;
39 static Isolate* GetIsolateFrom(LocalContext* context) {
40 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
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.
48 HandleScope scope(isolate);
49 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
50 weakmap->set_table(*table);
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>*>(
61 DCHECK_EQ(1234, p->second);
68 FLAG_incremental_marking = false;
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();
77 // Keep global reference to the key.
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);
85 CHECK(!global_handles->IsWeak(key.location()));
87 // Put two chained entries into weak map.
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);
96 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
99 heap->CollectAllGarbage(false);
100 CHECK_EQ(0, NumberOfWeakCalls);
101 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
103 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
105 // Make the global reference to the key weak.
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);
113 CHECK(global_handles->IsWeak(key.location()));
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());
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());
127 ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
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);
139 // Check initial capacity.
140 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
142 // Fill up weak map to trigger capacity change.
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);
153 // Check increased capacity.
154 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
157 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
159 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
160 heap->CollectAllGarbage(false);
161 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
163 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
165 // Check shrunk capacity.
166 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
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.
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);
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);
189 // Fill up weak map with values on an evacuation candidate.
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);
200 // Force compacting garbage collection.
201 CHECK(FLAG_always_compact);
202 heap->CollectAllGarbage(Heap::kNoGCFlags);
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.
209 if (i::FLAG_never_compact) return;
210 FLAG_always_compact = true;
212 FLAG_verify_heap = true;
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());
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);
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()));
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);
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);
249 TEST(Regress399527) {
250 CcTest::InitializeVM();
251 v8::HandleScope scope(CcTest::isolate());
252 Isolate* isolate = CcTest::i_isolate();
253 Heap* heap = isolate->heap();
255 HandleScope scope(isolate);
256 AllocateJSWeakMap(isolate);
257 SimulateIncrementalMarking(heap);
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);