| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "cctest.h" | 32 #include "cctest.h" |
| 33 | 33 |
| 34 using namespace v8::internal; | 34 using namespace v8::internal; |
| 35 | 35 |
| 36 | 36 |
| 37 static Isolate* GetIsolateFrom(LocalContext* context) { | 37 static Isolate* GetIsolateFrom(LocalContext* context) { |
| 38 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); | 38 return reinterpret_cast<Isolate*>((*context)->GetIsolate()); |
| 39 } | 39 } |
| 40 | 40 |
| 41 | 41 |
| 42 static Handle<JSWeakMap> AllocateJSWeakMap() { | 42 static Handle<JSWeakMap> AllocateJSWeakMap(Isolate* isolate) { |
| 43 Handle<Map> map = FACTORY->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); | 43 Factory* factory = isolate->factory(); |
| 44 Handle<JSObject> weakmap_obj = FACTORY->NewJSObjectFromMap(map); | 44 Heap* heap = isolate->heap(); |
| 45 Handle<Map> map = factory->NewMap(JS_WEAK_MAP_TYPE, JSWeakMap::kSize); |
| 46 Handle<JSObject> weakmap_obj = factory->NewJSObjectFromMap(map); |
| 45 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); | 47 Handle<JSWeakMap> weakmap(JSWeakMap::cast(*weakmap_obj)); |
| 46 // Do not use handles for the hash table, it would make entries strong. | 48 // Do not use handles for the hash table, it would make entries strong. |
| 47 Object* table_obj = ObjectHashTable::Allocate(1)->ToObjectChecked(); | 49 Object* table_obj = ObjectHashTable::Allocate(heap, 1)->ToObjectChecked(); |
| 48 ObjectHashTable* table = ObjectHashTable::cast(table_obj); | 50 ObjectHashTable* table = ObjectHashTable::cast(table_obj); |
| 49 weakmap->set_table(table); | 51 weakmap->set_table(table); |
| 50 weakmap->set_next(Smi::FromInt(0)); | 52 weakmap->set_next(Smi::FromInt(0)); |
| 51 return weakmap; | 53 return weakmap; |
| 52 } | 54 } |
| 53 | 55 |
| 54 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, | 56 static void PutIntoWeakMap(Handle<JSWeakMap> weakmap, |
| 55 Handle<JSObject> key, | 57 Handle<JSObject> key, |
| 56 Handle<Object> value) { | 58 Handle<Object> value) { |
| 57 Handle<ObjectHashTable> table = PutIntoObjectHashTable( | 59 Handle<ObjectHashTable> table = PutIntoObjectHashTable( |
| 58 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), | 60 Handle<ObjectHashTable>(ObjectHashTable::cast(weakmap->table())), |
| 59 Handle<JSObject>(JSObject::cast(*key)), | 61 Handle<JSObject>(JSObject::cast(*key)), |
| 60 value); | 62 value); |
| 61 weakmap->set_table(*table); | 63 weakmap->set_table(*table); |
| 62 } | 64 } |
| 63 | 65 |
| 64 static int NumberOfWeakCalls = 0; | 66 static int NumberOfWeakCalls = 0; |
| 65 static void WeakPointerCallback(v8::Isolate* isolate, | 67 static void WeakPointerCallback(v8::Isolate* isolate, |
| 66 v8::Persistent<v8::Value> handle, | 68 v8::Persistent<v8::Value> handle, |
| 67 void* id) { | 69 void* id) { |
| 68 ASSERT(id == reinterpret_cast<void*>(1234)); | 70 ASSERT(id == reinterpret_cast<void*>(1234)); |
| 69 NumberOfWeakCalls++; | 71 NumberOfWeakCalls++; |
| 70 handle.Dispose(isolate); | 72 handle.Dispose(isolate); |
| 71 } | 73 } |
| 72 | 74 |
| 73 | 75 |
| 74 TEST(Weakness) { | 76 TEST(Weakness) { |
| 75 FLAG_incremental_marking = false; | 77 FLAG_incremental_marking = false; |
| 76 LocalContext context; | 78 LocalContext context; |
| 79 Isolate* isolate = GetIsolateFrom(&context); |
| 80 Factory* factory = isolate->factory(); |
| 81 Heap* heap = isolate->heap(); |
| 77 v8::HandleScope scope; | 82 v8::HandleScope scope; |
| 78 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 83 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 79 GlobalHandles* global_handles = GetIsolateFrom(&context)->global_handles(); | 84 GlobalHandles* global_handles = isolate->global_handles(); |
| 80 | 85 |
| 81 // Keep global reference to the key. | 86 // Keep global reference to the key. |
| 82 Handle<Object> key; | 87 Handle<Object> key; |
| 83 { | 88 { |
| 84 v8::HandleScope scope; | 89 v8::HandleScope scope; |
| 85 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 90 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 86 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 91 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 87 key = global_handles->Create(*object); | 92 key = global_handles->Create(*object); |
| 88 } | 93 } |
| 89 CHECK(!global_handles->IsWeak(key.location())); | 94 CHECK(!global_handles->IsWeak(key.location())); |
| 90 | 95 |
| 91 // Put entry into weak map. | 96 // Put entry into weak map. |
| 92 { | 97 { |
| 93 v8::HandleScope scope; | 98 v8::HandleScope scope; |
| 94 PutIntoWeakMap(weakmap, | 99 PutIntoWeakMap(weakmap, |
| 95 Handle<JSObject>(JSObject::cast(*key)), | 100 Handle<JSObject>(JSObject::cast(*key)), |
| 96 Handle<Smi>(Smi::FromInt(23), GetIsolateFrom(&context))); | 101 Handle<Smi>(Smi::FromInt(23), isolate)); |
| 97 } | 102 } |
| 98 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 103 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 99 | 104 |
| 100 // Force a full GC. | 105 // Force a full GC. |
| 101 HEAP->CollectAllGarbage(false); | 106 heap->CollectAllGarbage(false); |
| 102 CHECK_EQ(0, NumberOfWeakCalls); | 107 CHECK_EQ(0, NumberOfWeakCalls); |
| 103 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 108 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 104 CHECK_EQ( | 109 CHECK_EQ( |
| 105 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 110 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 106 | 111 |
| 107 // Make the global reference to the key weak. | 112 // Make the global reference to the key weak. |
| 108 { | 113 { |
| 109 v8::HandleScope scope; | 114 v8::HandleScope scope; |
| 110 global_handles->MakeWeak(key.location(), | 115 global_handles->MakeWeak(key.location(), |
| 111 reinterpret_cast<void*>(1234), | 116 reinterpret_cast<void*>(1234), |
| 112 NULL, | 117 NULL, |
| 113 &WeakPointerCallback); | 118 &WeakPointerCallback); |
| 114 } | 119 } |
| 115 CHECK(global_handles->IsWeak(key.location())); | 120 CHECK(global_handles->IsWeak(key.location())); |
| 116 | 121 |
| 117 // Force a full GC. | 122 // Force a full GC. |
| 118 // Perform two consecutive GCs because the first one will only clear | 123 // Perform two consecutive GCs because the first one will only clear |
| 119 // weak references whereas the second one will also clear weak maps. | 124 // weak references whereas the second one will also clear weak maps. |
| 120 HEAP->CollectAllGarbage(false); | 125 heap->CollectAllGarbage(false); |
| 121 CHECK_EQ(1, NumberOfWeakCalls); | 126 CHECK_EQ(1, NumberOfWeakCalls); |
| 122 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 127 CHECK_EQ(1, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 123 CHECK_EQ( | 128 CHECK_EQ( |
| 124 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 129 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 125 HEAP->CollectAllGarbage(false); | 130 heap->CollectAllGarbage(false); |
| 126 CHECK_EQ(1, NumberOfWeakCalls); | 131 CHECK_EQ(1, NumberOfWeakCalls); |
| 127 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 132 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 128 CHECK_EQ( | 133 CHECK_EQ( |
| 129 1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 134 1, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 130 } | 135 } |
| 131 | 136 |
| 132 | 137 |
| 133 TEST(Shrinking) { | 138 TEST(Shrinking) { |
| 134 LocalContext context; | 139 LocalContext context; |
| 140 Isolate* isolate = GetIsolateFrom(&context); |
| 141 Factory* factory = isolate->factory(); |
| 142 Heap* heap = isolate->heap(); |
| 135 v8::HandleScope scope; | 143 v8::HandleScope scope; |
| 136 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 144 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 137 | 145 |
| 138 // Check initial capacity. | 146 // Check initial capacity. |
| 139 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 147 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 140 | 148 |
| 141 // Fill up weak map to trigger capacity change. | 149 // Fill up weak map to trigger capacity change. |
| 142 { | 150 { |
| 143 v8::HandleScope scope; | 151 v8::HandleScope scope; |
| 144 Handle<Map> map = FACTORY->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); | 152 Handle<Map> map = factory->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); |
| 145 for (int i = 0; i < 32; i++) { | 153 for (int i = 0; i < 32; i++) { |
| 146 Handle<JSObject> object = FACTORY->NewJSObjectFromMap(map); | 154 Handle<JSObject> object = factory->NewJSObjectFromMap(map); |
| 147 PutIntoWeakMap(weakmap, object, | 155 PutIntoWeakMap(weakmap, object, Handle<Smi>(Smi::FromInt(i), isolate)); |
| 148 Handle<Smi>(Smi::FromInt(i), GetIsolateFrom(&context))); | |
| 149 } | 156 } |
| 150 } | 157 } |
| 151 | 158 |
| 152 // Check increased capacity. | 159 // Check increased capacity. |
| 153 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); | 160 CHECK_EQ(128, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 154 | 161 |
| 155 // Force a full GC. | 162 // Force a full GC. |
| 156 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 163 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 157 CHECK_EQ( | 164 CHECK_EQ( |
| 158 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 165 0, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 159 HEAP->CollectAllGarbage(false); | 166 heap->CollectAllGarbage(false); |
| 160 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); | 167 CHECK_EQ(0, ObjectHashTable::cast(weakmap->table())->NumberOfElements()); |
| 161 CHECK_EQ( | 168 CHECK_EQ( |
| 162 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); | 169 32, ObjectHashTable::cast(weakmap->table())->NumberOfDeletedElements()); |
| 163 | 170 |
| 164 // Check shrunk capacity. | 171 // Check shrunk capacity. |
| 165 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); | 172 CHECK_EQ(32, ObjectHashTable::cast(weakmap->table())->Capacity()); |
| 166 } | 173 } |
| 167 | 174 |
| 168 | 175 |
| 169 // Test that weak map values on an evacuation candidate which are not reachable | 176 // Test that weak map values on an evacuation candidate which are not reachable |
| 170 // by other paths are correctly recorded in the slots buffer. | 177 // by other paths are correctly recorded in the slots buffer. |
| 171 TEST(Regress2060a) { | 178 TEST(Regress2060a) { |
| 172 FLAG_always_compact = true; | 179 FLAG_always_compact = true; |
| 173 LocalContext context; | 180 LocalContext context; |
| 181 Isolate* isolate = GetIsolateFrom(&context); |
| 182 Factory* factory = isolate->factory(); |
| 183 Heap* heap = isolate->heap(); |
| 174 v8::HandleScope scope; | 184 v8::HandleScope scope; |
| 175 Handle<JSFunction> function = | 185 Handle<JSFunction> function = |
| 176 FACTORY->NewFunction(FACTORY->function_string(), FACTORY->null_value()); | 186 factory->NewFunction(factory->function_string(), factory->null_value()); |
| 177 Handle<JSObject> key = FACTORY->NewJSObject(function); | 187 Handle<JSObject> key = factory->NewJSObject(function); |
| 178 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 188 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 179 | 189 |
| 180 // Start second old-space page so that values land on evacuation candidate. | 190 // Start second old-space page so that values land on evacuation candidate. |
| 181 Page* first_page = HEAP->old_pointer_space()->anchor()->next_page(); | 191 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
| 182 FACTORY->NewFixedArray(900 * KB / kPointerSize, TENURED); | 192 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 183 | 193 |
| 184 // Fill up weak map with values on an evacuation candidate. | 194 // Fill up weak map with values on an evacuation candidate. |
| 185 { | 195 { |
| 186 v8::HandleScope scope; | 196 v8::HandleScope scope; |
| 187 for (int i = 0; i < 32; i++) { | 197 for (int i = 0; i < 32; i++) { |
| 188 Handle<JSObject> object = FACTORY->NewJSObject(function, TENURED); | 198 Handle<JSObject> object = factory->NewJSObject(function, TENURED); |
| 189 CHECK(!HEAP->InNewSpace(object->address())); | 199 CHECK(!heap->InNewSpace(object->address())); |
| 190 CHECK(!first_page->Contains(object->address())); | 200 CHECK(!first_page->Contains(object->address())); |
| 191 PutIntoWeakMap(weakmap, key, object); | 201 PutIntoWeakMap(weakmap, key, object); |
| 192 } | 202 } |
| 193 } | 203 } |
| 194 | 204 |
| 195 // Force compacting garbage collection. | 205 // Force compacting garbage collection. |
| 196 CHECK(FLAG_always_compact); | 206 CHECK(FLAG_always_compact); |
| 197 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 207 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 198 } | 208 } |
| 199 | 209 |
| 200 | 210 |
| 201 // Test that weak map keys on an evacuation candidate which are reachable by | 211 // Test that weak map keys on an evacuation candidate which are reachable by |
| 202 // other strong paths are correctly recorded in the slots buffer. | 212 // other strong paths are correctly recorded in the slots buffer. |
| 203 TEST(Regress2060b) { | 213 TEST(Regress2060b) { |
| 204 FLAG_always_compact = true; | 214 FLAG_always_compact = true; |
| 205 #ifdef VERIFY_HEAP | 215 #ifdef VERIFY_HEAP |
| 206 FLAG_verify_heap = true; | 216 FLAG_verify_heap = true; |
| 207 #endif | 217 #endif |
| 208 | 218 |
| 209 LocalContext context; | 219 LocalContext context; |
| 220 Isolate* isolate = GetIsolateFrom(&context); |
| 221 Factory* factory = isolate->factory(); |
| 222 Heap* heap = isolate->heap(); |
| 210 v8::HandleScope scope; | 223 v8::HandleScope scope; |
| 211 Handle<JSFunction> function = | 224 Handle<JSFunction> function = |
| 212 FACTORY->NewFunction(FACTORY->function_string(), FACTORY->null_value()); | 225 factory->NewFunction(factory->function_string(), factory->null_value()); |
| 213 | 226 |
| 214 // Start second old-space page so that keys land on evacuation candidate. | 227 // Start second old-space page so that keys land on evacuation candidate. |
| 215 Page* first_page = HEAP->old_pointer_space()->anchor()->next_page(); | 228 Page* first_page = heap->old_pointer_space()->anchor()->next_page(); |
| 216 FACTORY->NewFixedArray(900 * KB / kPointerSize, TENURED); | 229 factory->NewFixedArray(900 * KB / kPointerSize, TENURED); |
| 217 | 230 |
| 218 // Fill up weak map with keys on an evacuation candidate. | 231 // Fill up weak map with keys on an evacuation candidate. |
| 219 Handle<JSObject> keys[32]; | 232 Handle<JSObject> keys[32]; |
| 220 for (int i = 0; i < 32; i++) { | 233 for (int i = 0; i < 32; i++) { |
| 221 keys[i] = FACTORY->NewJSObject(function, TENURED); | 234 keys[i] = factory->NewJSObject(function, TENURED); |
| 222 CHECK(!HEAP->InNewSpace(keys[i]->address())); | 235 CHECK(!heap->InNewSpace(keys[i]->address())); |
| 223 CHECK(!first_page->Contains(keys[i]->address())); | 236 CHECK(!first_page->Contains(keys[i]->address())); |
| 224 } | 237 } |
| 225 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(); | 238 Handle<JSWeakMap> weakmap = AllocateJSWeakMap(isolate); |
| 226 for (int i = 0; i < 32; i++) { | 239 for (int i = 0; i < 32; i++) { |
| 227 PutIntoWeakMap(weakmap, | 240 PutIntoWeakMap(weakmap, |
| 228 keys[i], | 241 keys[i], |
| 229 Handle<Smi>(Smi::FromInt(i), GetIsolateFrom(&context))); | 242 Handle<Smi>(Smi::FromInt(i), isolate)); |
| 230 } | 243 } |
| 231 | 244 |
| 232 // Force compacting garbage collection. The subsequent collections are used | 245 // Force compacting garbage collection. The subsequent collections are used |
| 233 // to verify that key references were actually updated. | 246 // to verify that key references were actually updated. |
| 234 CHECK(FLAG_always_compact); | 247 CHECK(FLAG_always_compact); |
| 235 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 248 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 236 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 249 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 237 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | 250 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 238 } | 251 } |
| OLD | NEW |