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 "test/cctest/cctest.h"
35 using namespace v8::internal;
38 static Isolate* GetIsolateFrom(LocalContext* context) {
39 return reinterpret_cast<Isolate*>((*context)->GetIsolate());
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.
47 HandleScope scope(isolate);
48 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
49 weakmap->set_table(*table);
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>*>(
60 DCHECK_EQ(1234, p->second);
67 FLAG_incremental_marking = false;
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();
76 // Keep global reference to the key.
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);
84 CHECK(!global_handles->IsWeak(key.location()));
86 // Put two chained entries into weak map.
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);
95 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
98 heap->CollectAllGarbage(false);
99 CHECK_EQ(0, NumberOfWeakCalls);
100 CHECK_EQ(2, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
102 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
104 // Make the global reference to the key weak.
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);
112 CHECK(global_handles->IsWeak(key.location()));
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());
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());
126 ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
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);
138 // Check initial capacity.
139 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
141 // Fill up weak map to trigger capacity change.
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);
152 // Check increased capacity.
153 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity());
156 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
158 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
159 heap->CollectAllGarbage(false);
160 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements());
162 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements());
164 // Check shrunk capacity.
165 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity());
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.
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);
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);
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 int dummy_array_size = Page::kMaxRegularHeapObjectSize - 92 * KB;
226 factory->NewFixedArray(dummy_array_size / kPointerSize, TENURED);
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()));
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);
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);
250 TEST(Regress399527) {
251 CcTest::InitializeVM();
252 v8::HandleScope scope(CcTest::isolate());
253 Isolate* isolate = CcTest::i_isolate();
254 Heap* heap = isolate->heap();
256 HandleScope scope(isolate);
257 AllocateJSWeakMap(isolate);
258 SimulateIncrementalMarking(heap);
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);