| 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" |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 for (int j = lengths[i]; j < 11; j++) | 387 for (int j = lengths[i]; j < 11; j++) |
| 388 CHECK_EQ(kNoChar, buffer[j]); | 388 CHECK_EQ(kNoChar, buffer[j]); |
| 389 } | 389 } |
| 390 } | 390 } |
| 391 | 391 |
| 392 | 392 |
| 393 class TwoByteResource: public v8::String::ExternalStringResource { | 393 class TwoByteResource: public v8::String::ExternalStringResource { |
| 394 public: | 394 public: |
| 395 TwoByteResource(const uint16_t* data, size_t length, bool* destructed) | 395 TwoByteResource(const uint16_t* data, size_t length, bool* destructed) |
| 396 : data_(data), length_(length), destructed_(destructed) { | 396 : data_(data), length_(length), destructed_(destructed) { |
| 397 if (destructed_ != NULL) { | 397 CHECK_NE(destructed, NULL); |
| 398 *destructed_ = false; | 398 *destructed_ = false; |
| 399 } | |
| 400 } | 399 } |
| 401 | 400 |
| 402 virtual ~TwoByteResource() { | 401 virtual ~TwoByteResource() { |
| 403 if (destructed_ != NULL) { | 402 CHECK_NE(destructed_, NULL); |
| 404 CHECK(!*destructed_); | 403 CHECK(!*destructed_); |
| 405 *destructed_ = true; | 404 *destructed_ = true; |
| 406 } | |
| 407 } | 405 } |
| 408 | 406 |
| 409 const uint16_t* data() const { return data_; } | 407 const uint16_t* data() const { return data_; } |
| 410 size_t length() const { return length_; } | 408 size_t length() const { return length_; } |
| 411 | 409 |
| 412 private: | 410 private: |
| 413 const uint16_t* data_; | 411 const uint16_t* data_; |
| 414 size_t length_; | 412 size_t length_; |
| 415 bool* destructed_; | 413 bool* destructed_; |
| 416 }; | 414 }; |
| 417 | 415 |
| 418 | 416 |
| 419 TEST(ExternalCrBug9746) { | |
| 420 InitializeVM(); | |
| 421 v8::HandleScope handle_scope; | |
| 422 | |
| 423 // This set of tests verifies that the workaround for Chromium bug 9746 | |
| 424 // works correctly. In certain situations the external resource of a symbol | |
| 425 // is collected while the symbol is still part of the symbol table. | |
| 426 static uint16_t two_byte_data[] = { | |
| 427 't', 'w', 'o', '-', 'b', 'y', 't', 'e', ' ', 'd', 'a', 't', 'a' | |
| 428 }; | |
| 429 static size_t two_byte_length = | |
| 430 sizeof(two_byte_data) / sizeof(two_byte_data[0]); | |
| 431 static const char* one_byte_data = "two-byte data"; | |
| 432 | |
| 433 // Allocate an external string resource and external string. | |
| 434 TwoByteResource* resource = new TwoByteResource(two_byte_data, | |
| 435 two_byte_length, | |
| 436 NULL); | |
| 437 Handle<String> string = Factory::NewExternalStringFromTwoByte(resource); | |
| 438 Vector<const char> one_byte_vec = CStrVector(one_byte_data); | |
| 439 Handle<String> compare = Factory::NewStringFromAscii(one_byte_vec); | |
| 440 | |
| 441 // Verify the correct behaviour before "collecting" the external resource. | |
| 442 CHECK(string->IsEqualTo(one_byte_vec)); | |
| 443 CHECK(string->Equals(*compare)); | |
| 444 | |
| 445 // "Collect" the external resource manually by setting the external resource | |
| 446 // pointer to NULL. Then redo the comparisons, they should not match AND | |
| 447 // not crash. | |
| 448 Handle<ExternalTwoByteString> external(ExternalTwoByteString::cast(*string)); | |
| 449 external->set_resource(NULL); | |
| 450 CHECK_EQ(false, string->IsEqualTo(one_byte_vec)); | |
| 451 #if !defined(DEBUG) | |
| 452 // These tests only work in non-debug as there are ASSERTs in the code that | |
| 453 // do prevent the ability to even get into the broken code when running the | |
| 454 // debug version of V8. | |
| 455 CHECK_EQ(false, string->Equals(*compare)); | |
| 456 CHECK_EQ(false, compare->Equals(*string)); | |
| 457 CHECK_EQ(false, string->Equals(Heap::empty_string())); | |
| 458 #endif // !defined(DEBUG) | |
| 459 } | |
| 460 | |
| 461 | |
| 462 // Regression test case for http://crbug.com/9746. The problem was | 417 // Regression test case for http://crbug.com/9746. The problem was |
| 463 // that when we marked objects reachable only through weak pointers, | 418 // that when we marked objects reachable only through weak pointers, |
| 464 // we ended up keeping a sliced symbol alive, even though we already | 419 // we ended up keeping a sliced symbol alive, even though we already |
| 465 // invoked the weak callback on the underlying external string thus | 420 // invoked the weak callback on the underlying external string thus |
| 466 // deleting its resource. | 421 // deleting its resource. |
| 467 TEST(Regress9746) { | 422 TEST(Regress9746) { |
| 468 InitializeVM(); | 423 InitializeVM(); |
| 469 | 424 |
| 470 // Setup lengths that guarantee we'll get slices instead of simple | 425 // Setup lengths that guarantee we'll get slices instead of simple |
| 471 // flat strings. | 426 // flat strings. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 // Force the slice into the symbol table. | 462 // Force the slice into the symbol table. |
| 508 slice = Factory::SymbolFromString(slice); | 463 slice = Factory::SymbolFromString(slice); |
| 509 CHECK(slice->IsSymbol()); | 464 CHECK(slice->IsSymbol()); |
| 510 CHECK(StringShape(*slice).IsSliced()); | 465 CHECK(StringShape(*slice).IsSliced()); |
| 511 | 466 |
| 512 Handle<String> buffer(Handle<SlicedString>::cast(slice)->buffer()); | 467 Handle<String> buffer(Handle<SlicedString>::cast(slice)->buffer()); |
| 513 CHECK(StringShape(*buffer).IsExternal()); | 468 CHECK(StringShape(*buffer).IsExternal()); |
| 514 CHECK(buffer->IsTwoByteRepresentation()); | 469 CHECK(buffer->IsTwoByteRepresentation()); |
| 515 | 470 |
| 516 // Finally, base a script on the slice of the external string and | 471 // Finally, base a script on the slice of the external string and |
| 517 // get its wrapper. This allocated yet another weak handle that | 472 // get its wrapper. This allocates yet another weak handle that |
| 518 // indirectly refers to the external string. | 473 // indirectly refers to the external string. |
| 519 Handle<Script> script = Factory::NewScript(slice); | 474 Handle<Script> script = Factory::NewScript(slice); |
| 520 Handle<JSObject> wrapper = GetScriptWrapper(script); | 475 Handle<JSObject> wrapper = GetScriptWrapper(script); |
| 521 } | 476 } |
| 522 | 477 |
| 523 // When we collect all garbage, we cannot get rid of the sliced | 478 // 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 | 479 // 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 | 480 // kept alive by the weak wrapper. Make sure we don't destruct the |
| 526 // external string. | 481 // external string. |
| 527 Heap::CollectAllGarbage(); | 482 Heap::CollectAllGarbage(); |
| 528 CHECK(!resource_destructed); | 483 CHECK(!resource_destructed); |
| 529 | 484 |
| 530 // Make sure the sliced symbol is still in the table. | 485 // Make sure the sliced symbol is still in the table. |
| 531 v8::HandleScope scope; | 486 v8::HandleScope scope; |
| 532 Vector<const char> vector(key, kSliceStringLength); | 487 Vector<const char> vector(key, kSliceStringLength); |
| 533 Handle<String> symbol = Factory::LookupSymbol(vector); | 488 Handle<String> symbol = Factory::LookupSymbol(vector); |
| 534 CHECK(StringShape(*symbol).IsSliced()); | 489 CHECK(StringShape(*symbol).IsSliced()); |
| 535 | 490 |
| 536 // Make sure the buffer is still a two-byte external string. | 491 // Make sure the buffer is still a two-byte external string. |
| 537 Handle<String> buffer(Handle<SlicedString>::cast(symbol)->buffer()); | 492 Handle<String> buffer(Handle<SlicedString>::cast(symbol)->buffer()); |
| 538 CHECK(StringShape(*buffer).IsExternal()); | 493 CHECK(StringShape(*buffer).IsExternal()); |
| 539 CHECK(buffer->IsTwoByteRepresentation()); | 494 CHECK(buffer->IsTwoByteRepresentation()); |
| 540 | 495 |
| 541 delete[] source; | 496 delete[] source; |
| 542 delete[] key; | 497 delete[] key; |
| 543 } | 498 } |
| OLD | NEW |