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<JSWeakSet> AllocateJSWeakSet(Isolate* isolate) {
45 Factory* factory = isolate->factory();
46 Handle<Map> map = factory->NewMap(JS_WEAK_SET_TYPE, JSWeakSet::kSize);
47 Handle<JSObject> weakset_obj = factory->NewJSObjectFromMap(map);
48 Handle<JSWeakSet> weakset(JSWeakSet::cast(*weakset_obj));
49 // Do not leak handles for the hash table, it would make entries strong.
51 HandleScope scope(isolate);
52 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 1);
53 weakset->set_table(*table);
58 static int NumberOfWeakCalls = 0;
59 static void WeakPointerCallback(
60 const v8::WeakCallbackData<v8::Value, void>& data) {
61 std::pair<v8::Persistent<v8::Value>*, int>* p =
62 reinterpret_cast<std::pair<v8::Persistent<v8::Value>*, int>*>(
64 DCHECK_EQ(1234, p->second);
70 TEST(WeakSet_Weakness) {
71 FLAG_incremental_marking = false;
73 Isolate* isolate = GetIsolateFrom(&context);
74 Factory* factory = isolate->factory();
75 Heap* heap = isolate->heap();
76 HandleScope scope(isolate);
77 Handle<JSWeakSet> weakset = AllocateJSWeakSet(isolate);
78 GlobalHandles* global_handles = isolate->global_handles();
80 // Keep global reference to the key.
83 HandleScope scope(isolate);
84 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
85 Handle<JSObject> object = factory->NewJSObjectFromMap(map);
86 key = global_handles->Create(*object);
88 CHECK(!global_handles->IsWeak(key.location()));
90 // Put entry into weak set.
92 HandleScope scope(isolate);
93 Handle<Smi> smi(Smi::FromInt(23), isolate);
94 Runtime::WeakCollectionSet(weakset, key, smi);
96 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements());
99 heap->CollectAllGarbage(false);
100 CHECK_EQ(0, NumberOfWeakCalls);
101 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements());
103 0, ObjectHashTable::cast(weakset->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 sets.
118 heap->CollectAllGarbage(false);
119 CHECK_EQ(1, NumberOfWeakCalls);
120 CHECK_EQ(1, ObjectHashTable::cast(weakset->table())->NumberOfElements());
122 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
123 heap->CollectAllGarbage(false);
124 CHECK_EQ(1, NumberOfWeakCalls);
125 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements());
127 1, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
131 TEST(WeakSet_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<JSWeakSet> weakset = AllocateJSWeakSet(isolate);
139 // Check initial capacity.
140 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->Capacity());
142 // Fill up weak set 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(weakset, object, smi);
153 // Check increased capacity.
154 CHECK_EQ(128, ObjectHashTable::cast(weakset->table())->Capacity());
157 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->NumberOfElements());
159 0, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
160 heap->CollectAllGarbage(false);
161 CHECK_EQ(0, ObjectHashTable::cast(weakset->table())->NumberOfElements());
163 32, ObjectHashTable::cast(weakset->table())->NumberOfDeletedElements());
165 // Check shrunk capacity.
166 CHECK_EQ(32, ObjectHashTable::cast(weakset->table())->Capacity());
170 // Test that weak set values on an evacuation candidate which are not reachable
171 // by other paths are correctly recorded in the slots buffer.
172 TEST(WeakSet_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<JSWeakSet> weakset = AllocateJSWeakSet(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 set 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(weakset, key, object);
200 // Force compacting garbage collection.
201 CHECK(FLAG_always_compact);
202 heap->CollectAllGarbage(Heap::kNoGCFlags);
206 // Test that weak set keys on an evacuation candidate which are reachable by
207 // other strong paths are correctly recorded in the slots buffer.
208 TEST(WeakSet_Regress2060b) {
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 set 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<JSWeakSet> weakset = AllocateJSWeakSet(isolate);
235 for (int i = 0; i < 32; i++) {
236 Handle<Smi> smi(Smi::FromInt(i), isolate);
237 Runtime::WeakCollectionSet(weakset, 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);