| OLD | NEW |
| 1 // Copyright 2007-2010 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2010 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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 // will clear the pending fixups array, which would otherwise contain GC roots | 243 // will clear the pending fixups array, which would otherwise contain GC roots |
| 244 // that would confuse the serialization/deserialization process. | 244 // that would confuse the serialization/deserialization process. |
| 245 v8::Persistent<v8::Context> env = v8::Context::New(); | 245 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 246 env.Dispose(); | 246 env.Dispose(); |
| 247 WriteToFile(FLAG_testing_serialization_file); | 247 WriteToFile(FLAG_testing_serialization_file); |
| 248 } | 248 } |
| 249 | 249 |
| 250 | 250 |
| 251 // Test that the whole heap can be serialized. | 251 // Test that the whole heap can be serialized. |
| 252 TEST(Serialize) { | 252 TEST(Serialize) { |
| 253 Serializer::Enable(); | 253 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 254 v8::V8::Initialize(); | 254 Serializer::Enable(); |
| 255 Serialize(); | 255 v8::V8::Initialize(); |
| 256 Serialize(); |
| 257 } |
| 256 } | 258 } |
| 257 | 259 |
| 258 | 260 |
| 259 // Test that heap serialization is non-destructive. | 261 // Test that heap serialization is non-destructive. |
| 260 TEST(SerializeTwice) { | 262 TEST(SerializeTwice) { |
| 261 Serializer::Enable(); | 263 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 262 v8::V8::Initialize(); | 264 Serializer::Enable(); |
| 263 Serialize(); | 265 v8::V8::Initialize(); |
| 264 Serialize(); | 266 Serialize(); |
| 267 Serialize(); |
| 268 } |
| 265 } | 269 } |
| 266 | 270 |
| 267 | 271 |
| 268 //---------------------------------------------------------------------------- | 272 //---------------------------------------------------------------------------- |
| 269 // Tests that the heap can be deserialized. | 273 // Tests that the heap can be deserialized. |
| 270 | 274 |
| 271 static void Deserialize() { | 275 static void Deserialize() { |
| 272 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file)); | 276 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file)); |
| 273 } | 277 } |
| 274 | 278 |
| 275 | 279 |
| 276 static void SanityCheck() { | 280 static void SanityCheck() { |
| 277 v8::HandleScope scope; | 281 v8::HandleScope scope; |
| 278 #ifdef DEBUG | 282 #ifdef DEBUG |
| 279 HEAP->Verify(); | 283 HEAP->Verify(); |
| 280 #endif | 284 #endif |
| 281 CHECK(Isolate::Current()->global()->IsJSObject()); | 285 CHECK(Isolate::Current()->global()->IsJSObject()); |
| 282 CHECK(Isolate::Current()->global_context()->IsContext()); | 286 CHECK(Isolate::Current()->global_context()->IsContext()); |
| 283 CHECK(HEAP->symbol_table()->IsSymbolTable()); | 287 CHECK(HEAP->symbol_table()->IsSymbolTable()); |
| 284 CHECK(!FACTORY->LookupAsciiSymbol("Empty")->IsFailure()); | 288 CHECK(!FACTORY->LookupAsciiSymbol("Empty")->IsFailure()); |
| 285 } | 289 } |
| 286 | 290 |
| 287 | 291 |
| 288 DEPENDENT_TEST(Deserialize, Serialize) { | 292 DEPENDENT_TEST(Deserialize, Serialize) { |
| 289 // The serialize-deserialize tests only work if the VM is built without | 293 // The serialize-deserialize tests only work if the VM is built without |
| 290 // serialization. That doesn't matter. We don't need to be able to | 294 // serialization. That doesn't matter. We don't need to be able to |
| 291 // serialize a snapshot in a VM that is booted from a snapshot. | 295 // serialize a snapshot in a VM that is booted from a snapshot. |
| 292 if (!Snapshot::IsEnabled()) { | 296 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 293 v8::HandleScope scope; | 297 v8::HandleScope scope; |
| 294 Deserialize(); | 298 Deserialize(); |
| 295 | 299 |
| 296 v8::Persistent<v8::Context> env = v8::Context::New(); | 300 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 297 env->Enter(); | 301 env->Enter(); |
| 298 | 302 |
| 299 SanityCheck(); | 303 SanityCheck(); |
| 300 } | 304 } |
| 301 } | 305 } |
| 302 | 306 |
| 303 | 307 |
| 304 DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) { | 308 DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) { |
| 305 if (!Snapshot::IsEnabled()) { | 309 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 306 v8::HandleScope scope; | 310 v8::HandleScope scope; |
| 307 Deserialize(); | 311 Deserialize(); |
| 308 | 312 |
| 309 v8::Persistent<v8::Context> env = v8::Context::New(); | 313 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 310 env->Enter(); | 314 env->Enter(); |
| 311 | 315 |
| 312 SanityCheck(); | 316 SanityCheck(); |
| 313 } | 317 } |
| 314 } | 318 } |
| 315 | 319 |
| 316 | 320 |
| 317 DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) { | 321 DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) { |
| 318 if (!Snapshot::IsEnabled()) { | 322 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 319 v8::HandleScope scope; | 323 v8::HandleScope scope; |
| 320 Deserialize(); | 324 Deserialize(); |
| 321 | 325 |
| 322 v8::Persistent<v8::Context> env = v8::Context::New(); | 326 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 323 env->Enter(); | 327 env->Enter(); |
| 324 | 328 |
| 325 const char* c_source = "\"1234\".length"; | 329 const char* c_source = "\"1234\".length"; |
| 326 v8::Local<v8::String> source = v8::String::New(c_source); | 330 v8::Local<v8::String> source = v8::String::New(c_source); |
| 327 v8::Local<v8::Script> script = v8::Script::Compile(source); | 331 v8::Local<v8::Script> script = v8::Script::Compile(source); |
| 328 CHECK_EQ(4, script->Run()->Int32Value()); | 332 CHECK_EQ(4, script->Run()->Int32Value()); |
| 329 } | 333 } |
| 330 } | 334 } |
| 331 | 335 |
| 332 | 336 |
| 333 DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2, | 337 DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2, |
| 334 SerializeTwice) { | 338 SerializeTwice) { |
| 335 if (!Snapshot::IsEnabled()) { | 339 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 336 v8::HandleScope scope; | 340 v8::HandleScope scope; |
| 337 Deserialize(); | 341 Deserialize(); |
| 338 | 342 |
| 339 v8::Persistent<v8::Context> env = v8::Context::New(); | 343 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 340 env->Enter(); | 344 env->Enter(); |
| 341 | 345 |
| 342 const char* c_source = "\"1234\".length"; | 346 const char* c_source = "\"1234\".length"; |
| 343 v8::Local<v8::String> source = v8::String::New(c_source); | 347 v8::Local<v8::String> source = v8::String::New(c_source); |
| 344 v8::Local<v8::Script> script = v8::Script::Compile(source); | 348 v8::Local<v8::Script> script = v8::Script::Compile(source); |
| 345 CHECK_EQ(4, script->Run()->Int32Value()); | 349 CHECK_EQ(4, script->Run()->Int32Value()); |
| 346 } | 350 } |
| 347 } | 351 } |
| 348 | 352 |
| 349 | 353 |
| 350 TEST(PartialSerialization) { | 354 TEST(PartialSerialization) { |
| 351 Serializer::Enable(); | 355 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 352 v8::V8::Initialize(); | 356 Serializer::Enable(); |
| 357 v8::V8::Initialize(); |
| 353 | 358 |
| 354 v8::Persistent<v8::Context> env = v8::Context::New(); | 359 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 355 ASSERT(!env.IsEmpty()); | 360 ASSERT(!env.IsEmpty()); |
| 356 env->Enter(); | 361 env->Enter(); |
| 357 // Make sure all builtin scripts are cached. | 362 // Make sure all builtin scripts are cached. |
| 358 { HandleScope scope; | 363 { HandleScope scope; |
| 359 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { | 364 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
| 360 Isolate::Current()->bootstrapper()->NativesSourceLookup(i); | 365 Isolate::Current()->bootstrapper()->NativesSourceLookup(i); |
| 366 } |
| 361 } | 367 } |
| 368 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 369 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 370 |
| 371 Object* raw_foo; |
| 372 { |
| 373 v8::HandleScope handle_scope; |
| 374 v8::Local<v8::String> foo = v8::String::New("foo"); |
| 375 ASSERT(!foo.IsEmpty()); |
| 376 raw_foo = *(v8::Utils::OpenHandle(*foo)); |
| 377 } |
| 378 |
| 379 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
| 380 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 381 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 382 |
| 383 env->Exit(); |
| 384 env.Dispose(); |
| 385 |
| 386 FileByteSink startup_sink(startup_name.start()); |
| 387 startup_name.Dispose(); |
| 388 StartupSerializer startup_serializer(&startup_sink); |
| 389 startup_serializer.SerializeStrongReferences(); |
| 390 |
| 391 FileByteSink partial_sink(FLAG_testing_serialization_file); |
| 392 PartialSerializer p_ser(&startup_serializer, &partial_sink); |
| 393 p_ser.Serialize(&raw_foo); |
| 394 startup_serializer.SerializeWeakReferences(); |
| 395 partial_sink.WriteSpaceUsed( |
| 396 p_ser.CurrentAllocationAddress(NEW_SPACE), |
| 397 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), |
| 398 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), |
| 399 p_ser.CurrentAllocationAddress(CODE_SPACE), |
| 400 p_ser.CurrentAllocationAddress(MAP_SPACE), |
| 401 p_ser.CurrentAllocationAddress(CELL_SPACE), |
| 402 p_ser.CurrentAllocationAddress(LO_SPACE)); |
| 362 } | 403 } |
| 363 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | |
| 364 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | |
| 365 | |
| 366 Object* raw_foo; | |
| 367 { | |
| 368 v8::HandleScope handle_scope; | |
| 369 v8::Local<v8::String> foo = v8::String::New("foo"); | |
| 370 ASSERT(!foo.IsEmpty()); | |
| 371 raw_foo = *(v8::Utils::OpenHandle(*foo)); | |
| 372 } | |
| 373 | |
| 374 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; | |
| 375 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); | |
| 376 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); | |
| 377 | |
| 378 env->Exit(); | |
| 379 env.Dispose(); | |
| 380 | |
| 381 FileByteSink startup_sink(startup_name.start()); | |
| 382 startup_name.Dispose(); | |
| 383 StartupSerializer startup_serializer(&startup_sink); | |
| 384 startup_serializer.SerializeStrongReferences(); | |
| 385 | |
| 386 FileByteSink partial_sink(FLAG_testing_serialization_file); | |
| 387 PartialSerializer p_ser(&startup_serializer, &partial_sink); | |
| 388 p_ser.Serialize(&raw_foo); | |
| 389 startup_serializer.SerializeWeakReferences(); | |
| 390 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE), | |
| 391 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), | |
| 392 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), | |
| 393 p_ser.CurrentAllocationAddress(CODE_SPACE), | |
| 394 p_ser.CurrentAllocationAddress(MAP_SPACE), | |
| 395 p_ser.CurrentAllocationAddress(CELL_SPACE), | |
| 396 p_ser.CurrentAllocationAddress(LO_SPACE)); | |
| 397 } | 404 } |
| 398 | 405 |
| 399 | 406 |
| 400 static void ReserveSpaceForPartialSnapshot(const char* file_name) { | 407 static void ReserveSpaceForPartialSnapshot(const char* file_name) { |
| 401 int file_name_length = StrLength(file_name) + 10; | 408 int file_name_length = StrLength(file_name) + 10; |
| 402 Vector<char> name = Vector<char>::New(file_name_length + 1); | 409 Vector<char> name = Vector<char>::New(file_name_length + 1); |
| 403 OS::SNPrintF(name, "%s.size", file_name); | 410 OS::SNPrintF(name, "%s.size", file_name); |
| 404 FILE* fp = OS::FOpen(name.start(), "r"); | 411 FILE* fp = OS::FOpen(name.start(), "r"); |
| 405 name.Dispose(); | 412 name.Dispose(); |
| 406 int new_size, pointer_size, data_size, code_size, map_size, cell_size; | 413 int new_size, pointer_size, data_size, code_size, map_size, cell_size; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 464 Deserializer deserializer(&source); | 471 Deserializer deserializer(&source); |
| 465 deserializer.DeserializePartial(&root2); | 472 deserializer.DeserializePartial(&root2); |
| 466 CHECK(root2->IsString()); | 473 CHECK(root2->IsString()); |
| 467 CHECK(*root_handle == root2); | 474 CHECK(*root_handle == root2); |
| 468 } | 475 } |
| 469 } | 476 } |
| 470 } | 477 } |
| 471 | 478 |
| 472 | 479 |
| 473 TEST(ContextSerialization) { | 480 TEST(ContextSerialization) { |
| 474 Serializer::Enable(); | 481 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 475 v8::V8::Initialize(); | 482 Serializer::Enable(); |
| 483 v8::V8::Initialize(); |
| 476 | 484 |
| 477 v8::Persistent<v8::Context> env = v8::Context::New(); | 485 v8::Persistent<v8::Context> env = v8::Context::New(); |
| 478 ASSERT(!env.IsEmpty()); | 486 ASSERT(!env.IsEmpty()); |
| 479 env->Enter(); | 487 env->Enter(); |
| 480 // Make sure all builtin scripts are cached. | 488 // Make sure all builtin scripts are cached. |
| 481 { HandleScope scope; | 489 { HandleScope scope; |
| 482 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { | 490 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
| 483 Isolate::Current()->bootstrapper()->NativesSourceLookup(i); | 491 Isolate::Current()->bootstrapper()->NativesSourceLookup(i); |
| 492 } |
| 484 } | 493 } |
| 494 // If we don't do this then we end up with a stray root pointing at the |
| 495 // context even after we have disposed of env. |
| 496 HEAP->CollectAllGarbage(Heap::kNoGCFlags); |
| 497 |
| 498 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
| 499 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 500 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 501 |
| 502 env->Exit(); |
| 503 |
| 504 Object* raw_context = *(v8::Utils::OpenHandle(*env)); |
| 505 |
| 506 env.Dispose(); |
| 507 |
| 508 FileByteSink startup_sink(startup_name.start()); |
| 509 startup_name.Dispose(); |
| 510 StartupSerializer startup_serializer(&startup_sink); |
| 511 startup_serializer.SerializeStrongReferences(); |
| 512 |
| 513 FileByteSink partial_sink(FLAG_testing_serialization_file); |
| 514 PartialSerializer p_ser(&startup_serializer, &partial_sink); |
| 515 p_ser.Serialize(&raw_context); |
| 516 startup_serializer.SerializeWeakReferences(); |
| 517 partial_sink.WriteSpaceUsed( |
| 518 p_ser.CurrentAllocationAddress(NEW_SPACE), |
| 519 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), |
| 520 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), |
| 521 p_ser.CurrentAllocationAddress(CODE_SPACE), |
| 522 p_ser.CurrentAllocationAddress(MAP_SPACE), |
| 523 p_ser.CurrentAllocationAddress(CELL_SPACE), |
| 524 p_ser.CurrentAllocationAddress(LO_SPACE)); |
| 485 } | 525 } |
| 486 // If we don't do this then we end up with a stray root pointing at the | |
| 487 // context even after we have disposed of env. | |
| 488 HEAP->CollectAllGarbage(Heap::kNoGCFlags); | |
| 489 | |
| 490 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; | |
| 491 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); | |
| 492 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); | |
| 493 | |
| 494 env->Exit(); | |
| 495 | |
| 496 Object* raw_context = *(v8::Utils::OpenHandle(*env)); | |
| 497 | |
| 498 env.Dispose(); | |
| 499 | |
| 500 FileByteSink startup_sink(startup_name.start()); | |
| 501 startup_name.Dispose(); | |
| 502 StartupSerializer startup_serializer(&startup_sink); | |
| 503 startup_serializer.SerializeStrongReferences(); | |
| 504 | |
| 505 FileByteSink partial_sink(FLAG_testing_serialization_file); | |
| 506 PartialSerializer p_ser(&startup_serializer, &partial_sink); | |
| 507 p_ser.Serialize(&raw_context); | |
| 508 startup_serializer.SerializeWeakReferences(); | |
| 509 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE), | |
| 510 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), | |
| 511 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), | |
| 512 p_ser.CurrentAllocationAddress(CODE_SPACE), | |
| 513 p_ser.CurrentAllocationAddress(MAP_SPACE), | |
| 514 p_ser.CurrentAllocationAddress(CELL_SPACE), | |
| 515 p_ser.CurrentAllocationAddress(LO_SPACE)); | |
| 516 } | 526 } |
| 517 | 527 |
| 518 | 528 |
| 519 DEPENDENT_TEST(ContextDeserialization, ContextSerialization) { | 529 DEPENDENT_TEST(ContextDeserialization, ContextSerialization) { |
| 520 if (!Snapshot::IsEnabled()) { | 530 if (!Snapshot::HaveASnapshotToStartFrom()) { |
| 521 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; | 531 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
| 522 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); | 532 Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 523 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); | 533 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 524 | 534 |
| 525 CHECK(Snapshot::Initialize(startup_name.start())); | 535 CHECK(Snapshot::Initialize(startup_name.start())); |
| 526 startup_name.Dispose(); | 536 startup_name.Dispose(); |
| 527 | 537 |
| 528 const char* file_name = FLAG_testing_serialization_file; | 538 const char* file_name = FLAG_testing_serialization_file; |
| 529 ReserveSpaceForPartialSnapshot(file_name); | 539 ReserveSpaceForPartialSnapshot(file_name); |
| 530 | 540 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 TEST(TestThatAlwaysFails) { | 685 TEST(TestThatAlwaysFails) { |
| 676 bool ArtificialFailure = false; | 686 bool ArtificialFailure = false; |
| 677 CHECK(ArtificialFailure); | 687 CHECK(ArtificialFailure); |
| 678 } | 688 } |
| 679 | 689 |
| 680 | 690 |
| 681 DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) { | 691 DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) { |
| 682 bool ArtificialFailure2 = false; | 692 bool ArtificialFailure2 = false; |
| 683 CHECK(ArtificialFailure2); | 693 CHECK(ArtificialFailure2); |
| 684 } | 694 } |
| OLD | NEW |