1 // Copyright 2012 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 4 // met: 5 // 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. 15 // 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. 27 28 #include <stdlib.h> 29 30 #ifdef __linux__ 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 #include <fcntl.h> 34 #include <unistd.h> 35 #include <errno.h> 36 #endif 37 38 39 #include "v8.h" 40 41 #include "global-handles.h" 42 #include "snapshot.h" 43 #include "cctest.h" 44 45 using namespace v8::internal; 46 47 48 TEST(MarkingDeque) { 49 CcTest::InitializeVM(); 50 int mem_size = 20 * kPointerSize; 51 byte* mem = NewArray<byte>(20*kPointerSize); 52 Address low = reinterpret_cast<Address>(mem); 53 Address high = low + mem_size; 54 MarkingDeque s; 55 s.Initialize(low, high); 56 57 Address original_address = reinterpret_cast<Address>(&s); 58 Address current_address = original_address; 59 while (!s.IsFull()) { 60 s.PushBlack(HeapObject::FromAddress(current_address)); 61 current_address += kPointerSize; 62 } 63 64 while (!s.IsEmpty()) { 65 Address value = s.Pop()->address(); 66 current_address -= kPointerSize; 67 CHECK_EQ(current_address, value); 68 } 69 70 CHECK_EQ(original_address, current_address); 71 DeleteArray(mem); 72 } 73 74 75 TEST(Promotion) { 76 // This test requires compaction. If compaction is turned off, we 77 // skip the entire test. 78 if (FLAG_never_compact) return; 79 80 // Ensure that we get a compacting collection so that objects are promoted 81 // from new space. 82 FLAG_gc_global = true; 83 FLAG_always_compact = true; 84 HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB); 85 86 CcTest::InitializeVM(); 87 88 v8::HandleScope sc(CcTest::isolate()); 89 90 // Allocate a fixed array in the new space. 91 int array_size = 92 (Page::kMaxNonCodeHeapObjectSize - FixedArray::kHeaderSize) / 93 (kPointerSize * 4); 94 Object* obj = HEAP->AllocateFixedArray(array_size)->ToObjectChecked(); 95 96 Handle<FixedArray> array(FixedArray::cast(obj)); 97 98 // Array should be in the new space. 99 CHECK(HEAP->InSpace(*array, NEW_SPACE)); 100 101 // Call the m-c collector, so array becomes an old object. 102 HEAP->CollectGarbage(OLD_POINTER_SPACE); 103 104 // Array now sits in the old space 105 CHECK(HEAP->InSpace(*array, OLD_POINTER_SPACE)); 106 } 107 108 109 TEST(NoPromotion) { 110 HEAP->ConfigureHeap(2*256*KB, 8*MB, 8*MB); 111 112 // Test the situation that some objects in new space are promoted to 113 // the old space 114 CcTest::InitializeVM(); 115 116 v8::HandleScope sc(CcTest::isolate()); 117 118 // Do a mark compact GC to shrink the heap. 119 HEAP->CollectGarbage(OLD_POINTER_SPACE); 120 121 // Allocate a big Fixed array in the new space. 122 int length = (Page::kMaxNonCodeHeapObjectSize - 123 FixedArray::kHeaderSize) / (2 * kPointerSize); 124 Object* obj = i::Isolate::Current()->heap()->AllocateFixedArray(length)-> 125 ToObjectChecked(); 126 127 Handle<FixedArray> array(FixedArray::cast(obj)); 128 129 // Array still stays in the new space. 130 CHECK(HEAP->InSpace(*array, NEW_SPACE)); 131 132 // Allocate objects in the old space until out of memory. 133 FixedArray* host = *array; 134 while (true) { 135 Object* obj; 136 { MaybeObject* maybe_obj = HEAP->AllocateFixedArray(100, TENURED); 137 if (!maybe_obj->ToObject(&obj)) break; 138 } 139 140 host->set(0, obj); 141 host = FixedArray::cast(obj); 142 } 143 144 // Call mark compact GC, and it should pass. 145 HEAP->CollectGarbage(OLD_POINTER_SPACE); 146 } 147 148 149 TEST(MarkCompactCollector) { 150 CcTest::InitializeVM(); 151 152 v8::HandleScope sc(CcTest::isolate()); 153 // call mark-compact when heap is empty 154 HEAP->CollectGarbage(OLD_POINTER_SPACE); 155 156 // keep allocating garbage in new space until it fails 157 const int ARRAY_SIZE = 100; 158 Object* array; 159 MaybeObject* maybe_array; 160 do { 161 maybe_array = HEAP->AllocateFixedArray(ARRAY_SIZE); 162 } while (maybe_array->ToObject(&array)); 163 HEAP->CollectGarbage(NEW_SPACE); 164 165 array = HEAP->AllocateFixedArray(ARRAY_SIZE)->ToObjectChecked(); 166 167 // keep allocating maps until it fails 168 Object* mapp; 169 MaybeObject* maybe_mapp; 170 do { 171 maybe_mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 172 } while (maybe_mapp->ToObject(&mapp)); 173 HEAP->CollectGarbage(MAP_SPACE); 174 mapp = HEAP->AllocateMap(JS_OBJECT_TYPE, 175 JSObject::kHeaderSize)->ToObjectChecked(); 176 177 // allocate a garbage 178 String* func_name = String::cast( 179 HEAP->InternalizeUtf8String("theFunction")->ToObjectChecked()); 180 SharedFunctionInfo* function_share = SharedFunctionInfo::cast( 181 HEAP->AllocateSharedFunctionInfo(func_name)->ToObjectChecked()); 182 JSFunction* function = JSFunction::cast( 183 HEAP->AllocateFunction(*Isolate::Current()->function_map(), 184 function_share, 185 HEAP->undefined_value())->ToObjectChecked()); 186 Map* initial_map = 187 Map::cast(HEAP->AllocateMap(JS_OBJECT_TYPE, 188 JSObject::kHeaderSize)->ToObjectChecked()); 189 function->set_initial_map(initial_map); 190 Isolate::Current()->context()->global_object()->SetProperty( 191 func_name, function, NONE, kNonStrictMode)->ToObjectChecked(); 192 193 JSObject* obj = JSObject::cast( 194 HEAP->AllocateJSObject(function)->ToObjectChecked()); 195 HEAP->CollectGarbage(OLD_POINTER_SPACE); 196 197 func_name = String::cast( 198 HEAP->InternalizeUtf8String("theFunction")->ToObjectChecked()); 199 CHECK(Isolate::Current()->context()->global_object()-> 200 HasLocalProperty(func_name)); 201 Object* func_value = Isolate::Current()->context()->global_object()-> 202 GetProperty(func_name)->ToObjectChecked(); 203 CHECK(func_value->IsJSFunction()); 204 function = JSFunction::cast(func_value); 205 206 obj = JSObject::cast(HEAP->AllocateJSObject(function)->ToObjectChecked()); 207 String* obj_name = 208 String::cast(HEAP->InternalizeUtf8String("theObject")->ToObjectChecked()); 209 Isolate::Current()->context()->global_object()->SetProperty( 210 obj_name, obj, NONE, kNonStrictMode)->ToObjectChecked(); 211 String* prop_name = 212 String::cast(HEAP->InternalizeUtf8String("theSlot")->ToObjectChecked()); 213 obj->SetProperty(prop_name, 214 Smi::FromInt(23), 215 NONE, 216 kNonStrictMode)->ToObjectChecked(); 217 218 HEAP->CollectGarbage(OLD_POINTER_SPACE); 219 220 obj_name = 221 String::cast(HEAP->InternalizeUtf8String("theObject")->ToObjectChecked()); 222 CHECK(Isolate::Current()->context()->global_object()-> 223 HasLocalProperty(obj_name)); 224 CHECK(Isolate::Current()->context()->global_object()-> 225 GetProperty(obj_name)->ToObjectChecked()->IsJSObject()); 226 obj = JSObject::cast(Isolate::Current()->context()->global_object()-> 227 GetProperty(obj_name)->ToObjectChecked()); 228 prop_name = 229 String::cast(HEAP->InternalizeUtf8String("theSlot")->ToObjectChecked()); 230 CHECK(obj->GetProperty(prop_name) == Smi::FromInt(23)); 231 } 232 233 234 // TODO(1600): compaction of map space is temporary removed from GC. 235 #if 0 236 static Handle<Map> CreateMap(Isolate* isolate) { 237 return isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 238 } 239 240 241 TEST(MapCompact) { 242 FLAG_max_map_space_pages = 16; 243 CcTest::InitializeVM(); 244 Isolate* isolate = Isolate::Current(); 245 Factory* factory = isolate->factory(); 246 247 { 248 v8::HandleScope sc; 249 // keep allocating maps while pointers are still encodable and thus 250 // mark compact is permitted. 251 Handle<JSObject> root = factory->NewJSObjectFromMap(CreateMap()); 252 do { 253 Handle<Map> map = CreateMap(); 254 map->set_prototype(*root); 255 root = factory->NewJSObjectFromMap(map); 256 } while (HEAP->map_space()->MapPointersEncodable()); 257 } 258 // Now, as we don't have any handles to just allocated maps, we should 259 // be able to trigger map compaction. 260 // To give an additional chance to fail, try to force compaction which 261 // should be impossible right now. 262 HEAP->CollectAllGarbage(Heap::kForceCompactionMask); 263 // And now map pointers should be encodable again. 264 CHECK(HEAP->map_space()->MapPointersEncodable()); 265 } 266 #endif 267 268 static int gc_starts = 0; 269 static int gc_ends = 0; 270 271 static void GCPrologueCallbackFunc() { 272 CHECK(gc_starts == gc_ends); 273 gc_starts++; 274 } 275 276 277 static void GCEpilogueCallbackFunc() { 278 CHECK(gc_starts == gc_ends + 1); 279 gc_ends++; 280 } 281 282 283 TEST(GCCallback) { 284 i::FLAG_stress_compaction = false; 285 CcTest::InitializeVM(); 286 287 HEAP->SetGlobalGCPrologueCallback(&GCPrologueCallbackFunc); 288 HEAP->SetGlobalGCEpilogueCallback(&GCEpilogueCallbackFunc); 289 290 // Scavenge does not call GC callback functions. 291 HEAP->PerformScavenge(); 292 293 CHECK_EQ(0, gc_starts); 294 CHECK_EQ(gc_ends, gc_starts); 295 296 HEAP->CollectGarbage(OLD_POINTER_SPACE); 297 CHECK_EQ(1, gc_starts); 298 CHECK_EQ(gc_ends, gc_starts); 299 } 300 301 302 static int NumberOfWeakCalls = 0; 303 static void WeakPointerCallback(v8::Isolate* isolate, 304 v8::Persistent<v8::Value>* handle, 305 void* id) { 306 ASSERT(id == reinterpret_cast<void*>(1234)); 307 NumberOfWeakCalls++; 308 handle->Dispose(isolate); 309 } 310 311 312 TEST(ObjectGroups) { 313 FLAG_incremental_marking = false; 314 CcTest::InitializeVM(); 315 GlobalHandles* global_handles = Isolate::Current()->global_handles(); 316 317 NumberOfWeakCalls = 0; 318 v8::HandleScope handle_scope(CcTest::isolate()); 319 320 Handle<Object> g1s1 = 321 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 322 Handle<Object> g1s2 = 323 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 324 Handle<Object> g1c1 = 325 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 326 global_handles->MakeWeak(g1s1.location(), 327 reinterpret_cast<void*>(1234), 328 &WeakPointerCallback); 329 global_handles->MakeWeak(g1s2.location(), 330 reinterpret_cast<void*>(1234), 331 &WeakPointerCallback); 332 global_handles->MakeWeak(g1c1.location(), 333 reinterpret_cast<void*>(1234), 334 &WeakPointerCallback); 335 336 Handle<Object> g2s1 = 337 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 338 Handle<Object> g2s2 = 339 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 340 Handle<Object> g2c1 = 341 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 342 global_handles->MakeWeak(g2s1.location(), 343 reinterpret_cast<void*>(1234), 344 &WeakPointerCallback); 345 global_handles->MakeWeak(g2s2.location(), 346 reinterpret_cast<void*>(1234), 347 &WeakPointerCallback); 348 global_handles->MakeWeak(g2c1.location(), 349 reinterpret_cast<void*>(1234), 350 &WeakPointerCallback); 351 352 Handle<Object> root = global_handles->Create(*g1s1); // make a root. 353 354 // Connect group 1 and 2, make a cycle. 355 Handle<FixedArray>::cast(g1s2)->set(0, *g2s2); 356 Handle<FixedArray>::cast(g2s1)->set(0, *g1s1); 357 358 { 359 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; 360 Object** g1_children[] = { g1c1.location() }; 361 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; 362 Object** g2_children[] = { g2c1.location() }; 363 global_handles->AddObjectGroup(g1_objects, 2, NULL); 364 global_handles->AddImplicitReferences( 365 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); 366 global_handles->AddObjectGroup(g2_objects, 2, NULL); 367 global_handles->AddImplicitReferences( 368 Handle<HeapObject>::cast(g2s1).location(), g2_children, 1); 369 } 370 // Do a full GC 371 HEAP->CollectGarbage(OLD_POINTER_SPACE); 372 373 // All object should be alive. 374 CHECK_EQ(0, NumberOfWeakCalls); 375 376 // Weaken the root. 377 global_handles->MakeWeak(root.location(), 378 reinterpret_cast<void*>(1234), 379 &WeakPointerCallback); 380 // But make children strong roots---all the objects (except for children) 381 // should be collectable now. 382 global_handles->ClearWeakness(g1c1.location()); 383 global_handles->ClearWeakness(g2c1.location()); 384 385 // Groups are deleted, rebuild groups. 386 { 387 Object** g1_objects[] = { g1s1.location(), g1s2.location() }; 388 Object** g1_children[] = { g1c1.location() }; 389 Object** g2_objects[] = { g2s1.location(), g2s2.location() }; 390 Object** g2_children[] = { g2c1.location() }; 391 global_handles->AddObjectGroup(g1_objects, 2, NULL); 392 global_handles->AddImplicitReferences( 393 Handle<HeapObject>::cast(g1s1).location(), g1_children, 1); 394 global_handles->AddObjectGroup(g2_objects, 2, NULL); 395 global_handles->AddImplicitReferences( 396 Handle<HeapObject>::cast(g2s1).location(), g2_children, 1); 397 } 398 399 HEAP->CollectGarbage(OLD_POINTER_SPACE); 400 401 // All objects should be gone. 5 global handles in total. 402 CHECK_EQ(5, NumberOfWeakCalls); 403 404 // And now make children weak again and collect them. 405 global_handles->MakeWeak(g1c1.location(), 406 reinterpret_cast<void*>(1234), 407 &WeakPointerCallback); 408 global_handles->MakeWeak(g2c1.location(), 409 reinterpret_cast<void*>(1234), 410 &WeakPointerCallback); 411 412 HEAP->CollectGarbage(OLD_POINTER_SPACE); 413 CHECK_EQ(7, NumberOfWeakCalls); 414 } 415 416 417 class TestRetainedObjectInfo : public v8::RetainedObjectInfo { 418 public: 419 TestRetainedObjectInfo() : has_been_disposed_(false) {} 420 421 bool has_been_disposed() { return has_been_disposed_; } 422 423 virtual void Dispose() { 424 ASSERT(!has_been_disposed_); 425 has_been_disposed_ = true; 426 } 427 428 virtual bool IsEquivalent(v8::RetainedObjectInfo* other) { 429 return other == this; 430 } 431 432 virtual intptr_t GetHash() { return 0; } 433 434 virtual const char* GetLabel() { return "whatever"; } 435 436 private: 437 bool has_been_disposed_; 438 }; 439 440 441 TEST(EmptyObjectGroups) { 442 CcTest::InitializeVM(); 443 GlobalHandles* global_handles = Isolate::Current()->global_handles(); 444 445 v8::HandleScope handle_scope(CcTest::isolate()); 446 447 Handle<Object> object = 448 global_handles->Create(HEAP->AllocateFixedArray(1)->ToObjectChecked()); 449 450 TestRetainedObjectInfo info; 451 global_handles->AddObjectGroup(NULL, 0, &info); 452 ASSERT(info.has_been_disposed()); 453 454 global_handles->AddImplicitReferences( 455 Handle<HeapObject>::cast(object).location(), NULL, 0); 456 } 457 458 459 #if defined(__has_feature) 460 #if __has_feature(address_sanitizer) 461 #define V8_WITH_ASAN 1 462 #endif 463 #endif 464 465 466 // Here is a memory use test that uses /proc, and is therefore Linux-only. We 467 // do not care how much memory the simulator uses, since it is only there for 468 // debugging purposes. Testing with ASAN doesn't make sense, either. 469 #if defined(__linux__) && !defined(USE_SIMULATOR) && !defined(V8_WITH_ASAN) 470 471 472 static uintptr_t ReadLong(char* buffer, intptr_t* position, int base) { 473 char* end_address = buffer + *position; 474 uintptr_t result = strtoul(buffer + *position, &end_address, base); 475 CHECK(result != ULONG_MAX || errno != ERANGE); 476 CHECK(end_address > buffer + *position); 477 *position = end_address - buffer; 478 return result; 479 } 480 481 482 // The memory use computed this way is not entirely accurate and depends on 483 // the way malloc allocates memory. That's why the memory use may seem to 484 // increase even though the sum of the allocated object sizes decreases. It 485 // also means that the memory use depends on the kernel and stdlib. 486 static intptr_t MemoryInUse() { 487 intptr_t memory_use = 0; 488 489 int fd = open("/proc/self/maps", O_RDONLY); 490 if (fd < 0) return -1; 491 492 const int kBufSize = 10000; 493 char buffer[kBufSize]; 494 int length = read(fd, buffer, kBufSize); 495 intptr_t line_start = 0; 496 CHECK_LT(length, kBufSize); // Make the buffer bigger. 497 CHECK_GT(length, 0); // We have to find some data in the file. 498 while (line_start < length) { 499 if (buffer[line_start] == '\n') { 500 line_start++; 501 continue; 502 } 503 intptr_t position = line_start; 504 uintptr_t start = ReadLong(buffer, &position, 16); 505 CHECK_EQ(buffer[position++], '-'); 506 uintptr_t end = ReadLong(buffer, &position, 16); 507 CHECK_EQ(buffer[position++], ' '); 508 CHECK(buffer[position] == '-' || buffer[position] == 'r'); 509 bool read_permission = (buffer[position++] == 'r'); 510 CHECK(buffer[position] == '-' || buffer[position] == 'w'); 511 bool write_permission = (buffer[position++] == 'w'); 512 CHECK(buffer[position] == '-' || buffer[position] == 'x'); 513 bool execute_permission = (buffer[position++] == 'x'); 514 CHECK(buffer[position] == '-' || buffer[position] == 'p'); 515 bool private_mapping = (buffer[position++] == 'p'); 516 CHECK_EQ(buffer[position++], ' '); 517 uintptr_t offset = ReadLong(buffer, &position, 16); 518 USE(offset); 519 CHECK_EQ(buffer[position++], ' '); 520 uintptr_t major = ReadLong(buffer, &position, 16); 521 USE(major); 522 CHECK_EQ(buffer[position++], ':'); 523 uintptr_t minor = ReadLong(buffer, &position, 16); 524 USE(minor); 525 CHECK_EQ(buffer[position++], ' '); 526 uintptr_t inode = ReadLong(buffer, &position, 10); 527 while (position < length && buffer[position] != '\n') position++; 528 if ((read_permission || write_permission || execute_permission) && 529 private_mapping && inode == 0) { 530 memory_use += (end - start); 531 } 532 533 line_start = position; 534 } 535 close(fd); 536 return memory_use; 537 } 538 539 540 TEST(BootUpMemoryUse) { 541 intptr_t initial_memory = MemoryInUse(); 542 // Avoid flakiness. 543 FLAG_crankshaft = false; 544 FLAG_parallel_recompilation = false; 545 546 // Only Linux has the proc filesystem and only if it is mapped. If it's not 547 // there we just skip the test. 548 if (initial_memory >= 0) { 549 CcTest::InitializeVM(); 550 intptr_t delta = MemoryInUse() - initial_memory; 551 printf("delta: %" V8_PTR_PREFIX "d kB\n", delta / 1024); 552 if (sizeof(initial_memory) == 8) { // 64-bit. 553 if (v8::internal::Snapshot::IsEnabled()) { 554 CHECK_LE(delta, 4000 * 1024); 555 } else { 556 CHECK_LE(delta, 4500 * 1024); 557 } 558 } else { // 32-bit. 559 if (v8::internal::Snapshot::IsEnabled()) { 560 CHECK_LE(delta, 3100 * 1024); 561 } else { 562 CHECK_LE(delta, 3450 * 1024); 563 } 564 } 565 } 566 } 567 568 569 intptr_t ShortLivingIsolate() { 570 v8::Isolate* isolate = v8::Isolate::New(); 571 { v8::Isolate::Scope isolate_scope(isolate); 572 v8::Locker lock(isolate); 573 v8::HandleScope handle_scope; 574 v8::Local<v8::Context> context = v8::Context::New(isolate); 575 CHECK(!context.IsEmpty()); 576 } 577 isolate->Dispose(); 578 return MemoryInUse(); 579 } 580 581 582 TEST(RegressJoinThreadsOnIsolateDeinit) { 583 intptr_t size_limit = ShortLivingIsolate() * 2; 584 for (int i = 0; i < 10; i++) { 585 CHECK_GT(size_limit, ShortLivingIsolate()); 586 } 587 } 588 589 #endif // __linux__ and !USE_SIMULATOR 590