| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | 2 |
| 3 // Test constant pool array code. | 3 // Test constant pool array code. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/factory.h" | 7 #include "src/factory.h" |
| 8 #include "src/objects.h" | 8 #include "src/objects.h" |
| 9 #include "test/cctest/cctest.h" | 9 #include "test/cctest/cctest.h" |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( | 24 i::Handle<i::JSFunction> fun = v8::Utils::OpenHandle( |
| 25 *v8::Local<v8::Function>::Cast( | 25 *v8::Local<v8::Function>::Cast( |
| 26 (*context)->Global()->Get(v8_str("foo")))); | 26 (*context)->Global()->Get(v8_str("foo")))); |
| 27 return fun->code(); | 27 return fun->code(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 | 30 |
| 31 TEST(ConstantPoolSmall) { | 31 TEST(ConstantPoolSmall) { |
| 32 LocalContext context; | 32 LocalContext context; |
| 33 Isolate* isolate = CcTest::i_isolate(); | 33 Isolate* isolate = CcTest::i_isolate(); |
| 34 Heap* heap = isolate->heap(); | |
| 35 Factory* factory = isolate->factory(); | 34 Factory* factory = isolate->factory(); |
| 36 v8::HandleScope scope(context->GetIsolate()); | 35 v8::HandleScope scope(context->GetIsolate()); |
| 37 | 36 |
| 38 // Check construction. | 37 // Check construction. |
| 39 ConstantPoolArray::NumberOfEntries small(3, 1, 2, 1); | 38 ConstantPoolArray::NumberOfEntries small(3, 1, 2, 1); |
| 40 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); | 39 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
| 41 | 40 |
| 42 int expected_counts[] = { 3, 1, 2, 1 }; | 41 int expected_counts[] = { 3, 1, 2, 1 }; |
| 43 int expected_first_idx[] = { 0, 3, 4, 6 }; | 42 int expected_first_idx[] = { 0, 3, 4, 6 }; |
| 44 int expected_last_idx[] = { 2, 3, 5, 6 }; | 43 int expected_last_idx[] = { 2, 3, 5, 6 }; |
| 45 for (int i = 0; i < 4; i++) { | 44 for (int i = 0; i < 4; i++) { |
| 46 CHECK_EQ(expected_counts[i], array->number_of_entries(kTypes[i], kSmall)); | 45 CHECK_EQ(expected_counts[i], array->number_of_entries(kTypes[i], kSmall)); |
| 47 CHECK_EQ(expected_first_idx[i], array->first_index(kTypes[i], kSmall)); | 46 CHECK_EQ(expected_first_idx[i], array->first_index(kTypes[i], kSmall)); |
| 48 CHECK_EQ(expected_last_idx[i], array->last_index(kTypes[i], kSmall)); | 47 CHECK_EQ(expected_last_idx[i], array->last_index(kTypes[i], kSmall)); |
| 49 } | 48 } |
| 50 CHECK(!array->is_extended_layout()); | 49 CHECK(!array->is_extended_layout()); |
| 51 | 50 |
| 52 // Check getters and setters. | 51 // Check getters and setters. |
| 53 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); | 52 int64_t big_number = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
| 54 Handle<Object> object = factory->NewHeapNumber(4.0); | 53 Handle<Object> object = factory->NewHeapNumber(4.0, IMMUTABLE, TENURED); |
| 55 Code* code = DummyCode(&context); | 54 Code* code = DummyCode(&context); |
| 56 array->set(0, big_number); | 55 array->set(0, big_number); |
| 57 array->set(1, 0.5); | 56 array->set(1, 0.5); |
| 58 array->set(2, 3e-24); | 57 array->set(2, 3e-24); |
| 59 array->set(3, code->entry()); | 58 array->set(3, code->entry()); |
| 60 array->set(4, code); | 59 array->set(4, code); |
| 61 array->set(5, *object); | 60 array->set(5, *object); |
| 62 array->set(6, 50); | 61 array->set(6, 50); |
| 63 CHECK_EQ(big_number, array->get_int64_entry(0)); | 62 CHECK_EQ(big_number, array->get_int64_entry(0)); |
| 64 CHECK_EQ(0.5, array->get_int64_entry_as_double(1)); | 63 CHECK_EQ(0.5, array->get_int64_entry_as_double(1)); |
| 65 CHECK_EQ(3e-24, array->get_int64_entry_as_double(2)); | 64 CHECK_EQ(3e-24, array->get_int64_entry_as_double(2)); |
| 66 CHECK_EQ(code->entry(), array->get_code_ptr_entry(3)); | 65 CHECK_EQ(code->entry(), array->get_code_ptr_entry(3)); |
| 67 CHECK_EQ(code, array->get_heap_ptr_entry(4)); | 66 CHECK_EQ(code, array->get_heap_ptr_entry(4)); |
| 68 CHECK_EQ(*object, array->get_heap_ptr_entry(5)); | 67 CHECK_EQ(*object, array->get_heap_ptr_entry(5)); |
| 69 CHECK_EQ(50, array->get_int32_entry(6)); | 68 CHECK_EQ(50, array->get_int32_entry(6)); |
| 70 | |
| 71 // Check pointers are updated on GC. | |
| 72 Object* old_ptr = array->get_heap_ptr_entry(5); | |
| 73 CHECK_EQ(*object, old_ptr); | |
| 74 heap->CollectGarbage(NEW_SPACE); | |
| 75 Object* new_ptr = array->get_heap_ptr_entry(5); | |
| 76 CHECK_NE(*object, old_ptr); | |
| 77 CHECK_EQ(*object, new_ptr); | |
| 78 } | 69 } |
| 79 | 70 |
| 80 | 71 |
| 81 TEST(ConstantPoolExtended) { | 72 TEST(ConstantPoolExtended) { |
| 82 LocalContext context; | 73 LocalContext context; |
| 83 Isolate* isolate = CcTest::i_isolate(); | 74 Isolate* isolate = CcTest::i_isolate(); |
| 84 Heap* heap = isolate->heap(); | |
| 85 Factory* factory = isolate->factory(); | 75 Factory* factory = isolate->factory(); |
| 86 v8::HandleScope scope(context->GetIsolate()); | 76 v8::HandleScope scope(context->GetIsolate()); |
| 87 | 77 |
| 88 // Check construction. | 78 // Check construction. |
| 89 ConstantPoolArray::NumberOfEntries small(1, 2, 3, 4); | 79 ConstantPoolArray::NumberOfEntries small(1, 2, 3, 4); |
| 90 ConstantPoolArray::NumberOfEntries extended(5, 6, 7, 8); | 80 ConstantPoolArray::NumberOfEntries extended(5, 6, 7, 8); |
| 91 Handle<ConstantPoolArray> array = | 81 Handle<ConstantPoolArray> array = |
| 92 factory->NewExtendedConstantPoolArray(small, extended); | 82 factory->NewExtendedConstantPoolArray(small, extended); |
| 93 | 83 |
| 94 // Check small section. | 84 // Check small section. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 for (int i = 0; i < 4; i++) { | 99 for (int i = 0; i < 4; i++) { |
| 110 CHECK_EQ(extended_counts[i], | 100 CHECK_EQ(extended_counts[i], |
| 111 array->number_of_entries(kTypes[i], kExtended)); | 101 array->number_of_entries(kTypes[i], kExtended)); |
| 112 CHECK_EQ(extended_first_idx[i], array->first_index(kTypes[i], kExtended)); | 102 CHECK_EQ(extended_first_idx[i], array->first_index(kTypes[i], kExtended)); |
| 113 CHECK_EQ(extended_last_idx[i], array->last_index(kTypes[i], kExtended)); | 103 CHECK_EQ(extended_last_idx[i], array->last_index(kTypes[i], kExtended)); |
| 114 } | 104 } |
| 115 | 105 |
| 116 // Check small and large section's don't overlap. | 106 // Check small and large section's don't overlap. |
| 117 int64_t small_section_int64 = V8_2PART_UINT64_C(0x56781234, DEF09ABC); | 107 int64_t small_section_int64 = V8_2PART_UINT64_C(0x56781234, DEF09ABC); |
| 118 Code* small_section_code_ptr = DummyCode(&context); | 108 Code* small_section_code_ptr = DummyCode(&context); |
| 119 Handle<Object> small_section_heap_ptr = factory->NewHeapNumber(4.0); | 109 Handle<Object> small_section_heap_ptr = |
| 110 factory->NewHeapNumber(4.0, IMMUTABLE, TENURED); |
| 120 int32_t small_section_int32 = 0xab12cd45; | 111 int32_t small_section_int32 = 0xab12cd45; |
| 121 | 112 |
| 122 int64_t extended_section_int64 = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); | 113 int64_t extended_section_int64 = V8_2PART_UINT64_C(0x12345678, 9ABCDEF0); |
| 123 Code* extended_section_code_ptr = DummyCode(&context); | 114 Code* extended_section_code_ptr = DummyCode(&context); |
| 124 Handle<Object> extended_section_heap_ptr = factory->NewHeapNumber(4.0); | 115 Handle<Object> extended_section_heap_ptr = |
| 116 factory->NewHeapNumber(5.0, IMMUTABLE, TENURED); |
| 125 int32_t extended_section_int32 = 0xef67ab89; | 117 int32_t extended_section_int32 = 0xef67ab89; |
| 126 | 118 |
| 127 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); | 119 for (int i = array->first_index(ConstantPoolArray::INT64, kSmall); |
| 128 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { | 120 i <= array->last_index(ConstantPoolArray::INT32, kSmall); i++) { |
| 129 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { | 121 if (i <= array->last_index(ConstantPoolArray::INT64, kSmall)) { |
| 130 array->set(i, small_section_int64); | 122 array->set(i, small_section_int64); |
| 131 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { | 123 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kSmall)) { |
| 132 array->set(i, small_section_code_ptr->entry()); | 124 array->set(i, small_section_code_ptr->entry()); |
| 133 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { | 125 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kSmall)) { |
| 134 array->set(i, *small_section_heap_ptr); | 126 array->set(i, *small_section_heap_ptr); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { | 163 } else if (i <= array->last_index(ConstantPoolArray::CODE_PTR, kExtended)) { |
| 172 CHECK_EQ(extended_section_code_ptr->entry(), | 164 CHECK_EQ(extended_section_code_ptr->entry(), |
| 173 array->get_code_ptr_entry(i)); | 165 array->get_code_ptr_entry(i)); |
| 174 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { | 166 } else if (i <= array->last_index(ConstantPoolArray::HEAP_PTR, kExtended)) { |
| 175 CHECK_EQ(*extended_section_heap_ptr, array->get_heap_ptr_entry(i)); | 167 CHECK_EQ(*extended_section_heap_ptr, array->get_heap_ptr_entry(i)); |
| 176 } else { | 168 } else { |
| 177 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); | 169 CHECK(i <= array->last_index(ConstantPoolArray::INT32, kExtended)); |
| 178 CHECK_EQ(extended_section_int32, array->get_int32_entry(i)); | 170 CHECK_EQ(extended_section_int32, array->get_int32_entry(i)); |
| 179 } | 171 } |
| 180 } | 172 } |
| 181 // Check pointers are updated on GC in extended section. | |
| 182 int index = array->first_index(ConstantPoolArray::HEAP_PTR, kExtended); | |
| 183 Object* old_ptr = array->get_heap_ptr_entry(index); | |
| 184 CHECK_EQ(*extended_section_heap_ptr, old_ptr); | |
| 185 heap->CollectGarbage(NEW_SPACE); | |
| 186 Object* new_ptr = array->get_heap_ptr_entry(index); | |
| 187 CHECK_NE(*extended_section_heap_ptr, old_ptr); | |
| 188 CHECK_EQ(*extended_section_heap_ptr, new_ptr); | |
| 189 } | 173 } |
| 190 | 174 |
| 191 | 175 |
| 192 static void CheckIterator(Handle<ConstantPoolArray> array, | 176 static void CheckIterator(Handle<ConstantPoolArray> array, |
| 193 ConstantPoolArray::Type type, | 177 ConstantPoolArray::Type type, |
| 194 int expected_indexes[], | 178 int expected_indexes[], |
| 195 int count) { | 179 int count) { |
| 196 int i = 0; | 180 int i = 0; |
| 197 ConstantPoolArray::Iterator iter(*array, type); | 181 ConstantPoolArray::Iterator iter(*array, type); |
| 198 while (!iter.is_finished()) { | 182 while (!iter.is_finished()) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 219 |
| 236 int expected_int64_indexs[] = { 0, 5, 6, 7, 8, 9 }; | 220 int expected_int64_indexs[] = { 0, 5, 6, 7, 8, 9 }; |
| 237 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 6); | 221 CheckIterator(array, ConstantPoolArray::INT64, expected_int64_indexs, 6); |
| 238 int expected_code_indexs[1]; | 222 int expected_code_indexs[1]; |
| 239 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 0); | 223 CheckIterator(array, ConstantPoolArray::CODE_PTR, expected_code_indexs, 0); |
| 240 int expected_heap_indexs[] = { 10, 11, 12 }; | 224 int expected_heap_indexs[] = { 10, 11, 12 }; |
| 241 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 3); | 225 CheckIterator(array, ConstantPoolArray::HEAP_PTR, expected_heap_indexs, 3); |
| 242 int expected_int32_indexs[] = { 1, 2, 3, 4 }; | 226 int expected_int32_indexs[] = { 1, 2, 3, 4 }; |
| 243 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 4); | 227 CheckIterator(array, ConstantPoolArray::INT32, expected_int32_indexs, 4); |
| 244 } | 228 } |
| 229 |
| 230 |
| 231 TEST(ConstantPoolPreciseGC) { |
| 232 LocalContext context; |
| 233 Isolate* isolate = CcTest::i_isolate(); |
| 234 Heap* heap = isolate->heap(); |
| 235 Factory* factory = isolate->factory(); |
| 236 v8::HandleScope scope(context->GetIsolate()); |
| 237 |
| 238 ConstantPoolArray::NumberOfEntries small(1, 0, 0, 1); |
| 239 Handle<ConstantPoolArray> array = factory->NewConstantPoolArray(small); |
| 240 |
| 241 // Check that the store buffer knows which entries are pointers and which are |
| 242 // not. To do this, make non-pointer entries which look like new space |
| 243 // pointers but are actually invalid and ensure the GC doesn't try to move |
| 244 // them. |
| 245 Handle<HeapObject> object = factory->NewHeapNumber(4.0); |
| 246 Object* raw_ptr = *object; |
| 247 // If interpreted as a pointer, this should be right inside the heap number |
| 248 // which will cause a crash when trying to lookup the 'map' pointer. |
| 249 intptr_t invalid_ptr = reinterpret_cast<intptr_t>(raw_ptr) + kInt32Size; |
| 250 int32_t invalid_ptr_int32 = static_cast<int32_t>(invalid_ptr); |
| 251 int64_t invalid_ptr_int64 = static_cast<int64_t>(invalid_ptr); |
| 252 array->set(0, invalid_ptr_int64); |
| 253 array->set(1, invalid_ptr_int32); |
| 254 |
| 255 // Ensure we perform a scan on scavenge for the constant pool's page. |
| 256 MemoryChunk::FromAddress(array->address())->set_scan_on_scavenge(true); |
| 257 heap->CollectGarbage(NEW_SPACE); |
| 258 |
| 259 // Check the object was moved by GC. |
| 260 CHECK_NE(*object, raw_ptr); |
| 261 |
| 262 // Check the non-pointer entries weren't changed. |
| 263 CHECK_EQ(invalid_ptr_int64, array->get_int64_entry(0)); |
| 264 CHECK_EQ(invalid_ptr_int32, array->get_int32_entry(1)); |
| 265 } |
| 266 |
| 267 |
| 268 TEST(ConstantPoolCompacting) { |
| 269 if (i::FLAG_never_compact) return; |
| 270 i::FLAG_always_compact = true; |
| 271 LocalContext context; |
| 272 Isolate* isolate = CcTest::i_isolate(); |
| 273 Heap* heap = isolate->heap(); |
| 274 Factory* factory = isolate->factory(); |
| 275 v8::HandleScope scope(context->GetIsolate()); |
| 276 |
| 277 ConstantPoolArray::NumberOfEntries small(0, 0, 1, 0); |
| 278 ConstantPoolArray::NumberOfEntries extended(0, 0, 1, 0); |
| 279 Handle<ConstantPoolArray> array = |
| 280 factory->NewExtendedConstantPoolArray(small, extended); |
| 281 |
| 282 // Start a second old-space page so that the heap pointer added to the |
| 283 // constant pool array ends up on the an evacuation candidate page. |
| 284 Page* first_page = heap->old_data_space()->anchor()->next_page(); |
| 285 { |
| 286 HandleScope scope(isolate); |
| 287 Handle<HeapObject> temp = |
| 288 factory->NewFixedDoubleArray(900 * KB / kDoubleSize, TENURED); |
| 289 CHECK(heap->InOldDataSpace(temp->address())); |
| 290 Handle<HeapObject> heap_ptr = |
| 291 factory->NewHeapNumber(5.0, IMMUTABLE, TENURED); |
| 292 CHECK(heap->InOldDataSpace(heap_ptr->address())); |
| 293 CHECK(!first_page->Contains(heap_ptr->address())); |
| 294 array->set(0, *heap_ptr); |
| 295 array->set(1, *heap_ptr); |
| 296 } |
| 297 |
| 298 // Check heap pointers are correctly updated on GC. |
| 299 Object* old_ptr = array->get_heap_ptr_entry(0); |
| 300 Handle<Object> object(old_ptr, isolate); |
| 301 CHECK_EQ(old_ptr, *object); |
| 302 CHECK_EQ(old_ptr, array->get_heap_ptr_entry(1)); |
| 303 |
| 304 // Force compacting garbage collection. |
| 305 CHECK(FLAG_always_compact); |
| 306 heap->CollectAllGarbage(Heap::kNoGCFlags); |
| 307 |
| 308 CHECK_NE(old_ptr, *object); |
| 309 CHECK_EQ(*object, array->get_heap_ptr_entry(0)); |
| 310 CHECK_EQ(*object, array->get_heap_ptr_entry(1)); |
| 311 } |
| OLD | NEW |