Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | 2 |
| 3 // Check that we can traverse very deep stacks of ConsStrings using | 3 // Check that we can traverse very deep stacks of ConsStrings using |
| 4 // StringInputBuffer. Check that Get(int) works on very deep stacks | 4 // StringInputBuffer. Check that Get(int) works on very deep stacks |
| 5 // of ConsStrings. These operations may not be very fast, but they | 5 // of ConsStrings. These operations may not be very fast, but they |
| 6 // should be possible without getting errors due to too deep recursion. | 6 // should be possible without getting errors due to too deep recursion. |
| 7 | 7 |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 | 9 |
| 10 #include "v8.h" | 10 #include "v8.h" |
| 11 | 11 |
| 12 #include "api.h" | |
| 12 #include "factory.h" | 13 #include "factory.h" |
| 13 #include "cctest.h" | 14 #include "cctest.h" |
| 14 #include "zone-inl.h" | 15 #include "zone-inl.h" |
| 15 | 16 |
| 16 unsigned int seed = 123; | 17 unsigned int seed = 123; |
| 17 | 18 |
| 18 static uint32_t gen() { | 19 static uint32_t gen() { |
| 19 uint64_t z; | 20 uint64_t z; |
| 20 z = seed; | 21 z = seed; |
| 21 z *= 279470273; | 22 z *= 279470273; |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j])); | 385 CHECK_EQ(as_utf8[j], static_cast<unsigned char>(buffer[j])); |
| 385 // Check that the rest of the buffer hasn't been touched | 386 // Check that the rest of the buffer hasn't been touched |
| 386 for (int j = lengths[i]; j < 11; j++) | 387 for (int j = lengths[i]; j < 11; j++) |
| 387 CHECK_EQ(kNoChar, buffer[j]); | 388 CHECK_EQ(kNoChar, buffer[j]); |
| 388 } | 389 } |
| 389 } | 390 } |
| 390 | 391 |
| 391 | 392 |
| 392 class TwoByteResource: public v8::String::ExternalStringResource { | 393 class TwoByteResource: public v8::String::ExternalStringResource { |
| 393 public: | 394 public: |
| 394 explicit TwoByteResource(const uint16_t* data, size_t length) | 395 TwoByteResource(const uint16_t* data, size_t length, bool* destructed) |
| 395 : data_(data), length_(length) { } | 396 : data_(data), length_(length), destructed_(destructed) { |
| 396 virtual ~TwoByteResource() { } | 397 if (destructed_ != NULL) { |
| 398 *destructed_ = false; | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 virtual ~TwoByteResource() { | |
| 403 if (destructed_ != NULL) { | |
| 404 CHECK(!*destructed_); | |
| 405 *destructed_ = true; | |
| 406 } | |
| 407 } | |
| 397 | 408 |
| 398 const uint16_t* data() const { return data_; } | 409 const uint16_t* data() const { return data_; } |
| 399 size_t length() const { return length_; } | 410 size_t length() const { return length_; } |
| 400 | 411 |
| 401 private: | 412 private: |
| 402 const uint16_t* data_; | 413 const uint16_t* data_; |
| 403 size_t length_; | 414 size_t length_; |
| 415 bool* destructed_; | |
| 404 }; | 416 }; |
| 405 | 417 |
| 406 | 418 |
| 407 TEST(ExternalCrBug9746) { | 419 TEST(ExternalCrBug9746) { |
| 408 InitializeVM(); | 420 InitializeVM(); |
| 409 v8::HandleScope handle_scope; | 421 v8::HandleScope handle_scope; |
| 410 | 422 |
| 411 // This set of tests verifies that the workaround for Chromium bug 9746 | 423 // This set of tests verifies that the workaround for Chromium bug 9746 |
| 412 // works correctly. In certain situations the external resource of a symbol | 424 // works correctly. In certain situations the external resource of a symbol |
| 413 // is collected while the symbol is still part of the symbol table. | 425 // is collected while the symbol is still part of the symbol table. |
| 414 static uint16_t two_byte_data[] = { | 426 static uint16_t two_byte_data[] = { |
| 415 't', 'w', 'o', '-', 'b', 'y', 't', 'e', ' ', 'd', 'a', 't', 'a' | 427 't', 'w', 'o', '-', 'b', 'y', 't', 'e', ' ', 'd', 'a', 't', 'a' |
| 416 }; | 428 }; |
| 417 static size_t two_byte_length = | 429 static size_t two_byte_length = |
| 418 sizeof(two_byte_data) / sizeof(two_byte_data[0]); | 430 sizeof(two_byte_data) / sizeof(two_byte_data[0]); |
| 419 static const char* one_byte_data = "two-byte data"; | 431 static const char* one_byte_data = "two-byte data"; |
| 420 | 432 |
| 421 // Allocate an external string resource and external string. | 433 // Allocate an external string resource and external string. |
| 422 TwoByteResource* resource = new TwoByteResource(two_byte_data, | 434 TwoByteResource* resource = new TwoByteResource(two_byte_data, |
| 423 two_byte_length); | 435 two_byte_length, |
| 436 NULL); | |
| 424 Handle<String> string = Factory::NewExternalStringFromTwoByte(resource); | 437 Handle<String> string = Factory::NewExternalStringFromTwoByte(resource); |
| 425 Vector<const char> one_byte_vec = CStrVector(one_byte_data); | 438 Vector<const char> one_byte_vec = CStrVector(one_byte_data); |
| 426 Handle<String> compare = Factory::NewStringFromAscii(one_byte_vec); | 439 Handle<String> compare = Factory::NewStringFromAscii(one_byte_vec); |
| 427 | 440 |
| 428 // Verify the correct behaviour before "collecting" the external resource. | 441 // Verify the correct behaviour before "collecting" the external resource. |
| 429 CHECK(string->IsEqualTo(one_byte_vec)); | 442 CHECK(string->IsEqualTo(one_byte_vec)); |
| 430 CHECK(string->Equals(*compare)); | 443 CHECK(string->Equals(*compare)); |
| 431 | 444 |
| 432 // "Collect" the external resource manually by setting the external resource | 445 // "Collect" the external resource manually by setting the external resource |
| 433 // pointer to NULL. Then redo the comparisons, they should not match AND | 446 // pointer to NULL. Then redo the comparisons, they should not match AND |
| 434 // not crash. | 447 // not crash. |
| 435 Handle<ExternalTwoByteString> external(ExternalTwoByteString::cast(*string)); | 448 Handle<ExternalTwoByteString> external(ExternalTwoByteString::cast(*string)); |
| 436 external->set_resource(NULL); | 449 external->set_resource(NULL); |
| 437 CHECK_EQ(false, string->IsEqualTo(one_byte_vec)); | 450 CHECK_EQ(false, string->IsEqualTo(one_byte_vec)); |
| 438 #if !defined(DEBUG) | 451 #if !defined(DEBUG) |
| 439 // These tests only work in non-debug as there are ASSERTs in the code that | 452 // These tests only work in non-debug as there are ASSERTs in the code that |
| 440 // do prevent the ability to even get into the broken code when running the | 453 // do prevent the ability to even get into the broken code when running the |
| 441 // debug version of V8. | 454 // debug version of V8. |
| 442 CHECK_EQ(false, string->Equals(*compare)); | 455 CHECK_EQ(false, string->Equals(*compare)); |
| 443 CHECK_EQ(false, compare->Equals(*string)); | 456 CHECK_EQ(false, compare->Equals(*string)); |
| 444 CHECK_EQ(false, string->Equals(Heap::empty_string())); | 457 CHECK_EQ(false, string->Equals(Heap::empty_string())); |
| 445 #endif // !defined(DEBUG) | 458 #endif // !defined(DEBUG) |
| 446 } | 459 } |
| 460 | |
| 461 | |
| 462 // Regression test case for http://crbug.com/9746. The problem was | |
| 463 // that when we marked objects reachable only through weak pointers, | |
| 464 // we ended up keeping a sliced symbol alive, even though we already | |
| 465 // invoked the weak callback on the underlying external string thus | |
| 466 // deleting its resource. | |
| 467 TEST(Regress9746) { | |
| 468 InitializeVM(); | |
| 469 | |
| 470 // Setup lengths that guarantee we'll get slices instead of simple | |
| 471 // flat strings. | |
| 472 static const int kFullStringLength = String::kMinNonFlatLength * 2; | |
| 473 static const int kSliceStringLength = String::kMinNonFlatLength + 1; | |
| 474 | |
| 475 uint16_t* source = new uint16_t[kFullStringLength]; | |
| 476 for (int i = 0; i < kFullStringLength; i++) source[i] = '1'; | |
| 477 char* key = new char[kSliceStringLength]; | |
| 478 for (int i = 0; i < kSliceStringLength; i++) key[i] = '1'; | |
| 479 | |
| 480 // Allocate an external string resource that keeps track of when it | |
| 481 // is destructed. | |
| 482 bool resource_destructed = false; | |
| 483 TwoByteResource* resource = | |
| 484 new TwoByteResource(source, kFullStringLength, &resource_destructed); | |
| 485 | |
| 486 { | |
| 487 v8::HandleScope scope; | |
| 488 | |
| 489 // Allocate an external string resource and external string. We | |
| 490 // have to go through the API to get the weak handle and the | |
| 491 // automatic destruction going. | |
| 492 Handle<String> string = | |
| 493 v8::Utils::OpenHandle(*v8::String::NewExternal(resource)); | |
| 494 | |
| 495 // Create a slice of the external string. | |
| 496 Handle<String> slice = | |
| 497 Factory::NewStringSlice(string, 0, kSliceStringLength); | |
| 498 CHECK_EQ(kSliceStringLength, slice->length()); | |
| 499 CHECK(StringShape(*slice).IsSliced()); | |
| 500 | |
| 501 // Make sure the slice ends up in old space so we can morph it | |
| 502 // into a symbol. | |
| 503 while (Heap::InNewSpace(*slice)) { | |
|
Kevin Millikin (Chromium)
2009/05/05 08:13:00
This will probably terminate :)
| |
| 504 Heap::PerformScavenge(); | |
| 505 } | |
| 506 | |
| 507 // Force the slice into the symbol table. | |
| 508 slice = Factory::SymbolFromString(slice); | |
| 509 CHECK(slice->IsSymbol()); | |
| 510 CHECK(StringShape(*slice).IsSliced()); | |
| 511 | |
| 512 Handle<String> buffer(Handle<SlicedString>::cast(slice)->buffer()); | |
| 513 CHECK(StringShape(*buffer).IsExternal()); | |
| 514 CHECK(buffer->IsTwoByteRepresentation()); | |
| 515 | |
| 516 // Finally, base a script on the slice of the external string and | |
| 517 // get its wrapper. This allocated yet another weak handle that | |
|
Kevin Millikin (Chromium)
2009/05/05 08:13:00
"allocated" => "allocates"
| |
| 518 // indirectly refers to the external string. | |
| 519 Handle<Script> script = Factory::NewScript(slice); | |
| 520 Handle<JSObject> wrapper = GetScriptWrapper(script); | |
| 521 } | |
| 522 | |
| 523 // When we collect all garbage, we cannot get rid of the sliced | |
| 524 // symbol entry in the symbol table because it is used by the script | |
| 525 // kept alive by the weak wrapper. Make sure we don't destruct the | |
| 526 // external string. | |
| 527 Heap::CollectAllGarbage(); | |
| 528 CHECK(!resource_destructed); | |
| 529 | |
| 530 // Make sure the sliced symbol is still in the table. | |
| 531 v8::HandleScope scope; | |
| 532 Vector<const char> vector(key, kSliceStringLength); | |
| 533 Handle<String> symbol = Factory::LookupSymbol(vector); | |
| 534 CHECK(StringShape(*symbol).IsSliced()); | |
| 535 | |
| 536 // Make sure the buffer is still a two-byte external string. | |
| 537 Handle<String> buffer(Handle<SlicedString>::cast(symbol)->buffer()); | |
| 538 CHECK(StringShape(*buffer).IsExternal()); | |
| 539 CHECK(buffer->IsTwoByteRepresentation()); | |
| 540 | |
| 541 delete[] source; | |
| 542 delete[] key; | |
| 543 } | |
| OLD | NEW |