OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/eintr_wrapper.h" | 10 #include "base/eintr_wrapper.h" |
(...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
548 | 548 |
549 // TODO(vandebo) make this work on Windows too. | 549 // TODO(vandebo) make this work on Windows too. |
550 #if !defined(OS_WIN) | 550 #if !defined(OS_WIN) |
551 | 551 |
552 #if defined(USE_TCMALLOC) | 552 #if defined(USE_TCMALLOC) |
553 extern "C" { | 553 extern "C" { |
554 int tc_set_new_mode(int mode); | 554 int tc_set_new_mode(int mode); |
555 } | 555 } |
556 #endif // defined(USE_TCMALLOC) | 556 #endif // defined(USE_TCMALLOC) |
557 | 557 |
558 class OutOfMemoryTest : public testing::Test { | 558 class OutOfMemoryDeathTest : public testing::Test { |
559 public: | 559 public: |
560 OutOfMemoryTest() | 560 OutOfMemoryDeathTest() |
561 : value_(NULL), | 561 : value_(NULL), |
562 // Make test size as large as possible minus a few pages so | 562 // Make test size as large as possible minus a few pages so |
563 // that alignment or other rounding doesn't make it wrap. | 563 // that alignment or other rounding doesn't make it wrap. |
564 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), | 564 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), |
565 signed_test_size_(std::numeric_limits<ssize_t>::max()) { | 565 signed_test_size_(std::numeric_limits<ssize_t>::max()) { |
566 } | 566 } |
567 | 567 |
568 virtual void SetUp() { | 568 virtual void SetUp() { |
569 // Must call EnableTerminationOnOutOfMemory() because that is called from | |
570 // chrome's main function and therefore hasn't been called yet. | |
571 base::EnableTerminationOnOutOfMemory(); | |
572 #if defined(USE_TCMALLOC) | 569 #if defined(USE_TCMALLOC) |
573 tc_set_new_mode(1); | 570 tc_set_new_mode(1); |
574 } | 571 } |
575 | 572 |
576 virtual void TearDown() { | 573 virtual void TearDown() { |
577 tc_set_new_mode(0); | 574 tc_set_new_mode(0); |
578 #endif // defined(USE_TCMALLOC) | 575 #endif // defined(USE_TCMALLOC) |
579 } | 576 } |
580 | 577 |
578 void SetUpInDeathAssert() { | |
579 // Must call EnableTerminationOnOutOfMemory() because that is called from | |
580 // chrome's main function and therefore hasn't been called yet. | |
581 // Since this call may result in another thread being created and death | |
582 // tests shouldn't be started in a multithread environment, this call | |
583 // should be done inside of the ASSERT_DEATH. | |
584 base::EnableTerminationOnOutOfMemory(); | |
585 } | |
586 | |
581 void* value_; | 587 void* value_; |
582 size_t test_size_; | 588 size_t test_size_; |
583 ssize_t signed_test_size_; | 589 ssize_t signed_test_size_; |
584 }; | 590 }; |
585 | 591 |
586 TEST_F(OutOfMemoryTest, New) { | 592 TEST_F(OutOfMemoryDeathTest, New) { |
587 ASSERT_DEATH(value_ = operator new(test_size_), ""); | 593 ASSERT_DEATH({ |
594 SetUpInDeathAssert(); | |
vandebo (ex-Chrome)
2010/08/23 21:52:14
Is there a reason to put these inside the macro ca
| |
595 value_ = operator new(test_size_); | |
596 }, ""); | |
588 } | 597 } |
589 | 598 |
590 TEST_F(OutOfMemoryTest, NewArray) { | 599 TEST_F(OutOfMemoryDeathTest, NewArray) { |
591 ASSERT_DEATH(value_ = new char[test_size_], ""); | 600 ASSERT_DEATH({ |
601 SetUpInDeathAssert(); | |
602 value_ = new char[test_size_]; | |
603 }, ""); | |
592 } | 604 } |
593 | 605 |
594 TEST_F(OutOfMemoryTest, Malloc) { | 606 TEST_F(OutOfMemoryDeathTest, Malloc) { |
595 ASSERT_DEATH(value_ = malloc(test_size_), ""); | 607 ASSERT_DEATH({ |
608 SetUpInDeathAssert(); | |
609 value_ = malloc(test_size_); | |
610 }, ""); | |
596 } | 611 } |
597 | 612 |
598 TEST_F(OutOfMemoryTest, Realloc) { | 613 TEST_F(OutOfMemoryDeathTest, Realloc) { |
599 ASSERT_DEATH(value_ = realloc(NULL, test_size_), ""); | 614 ASSERT_DEATH({ |
615 SetUpInDeathAssert(); | |
616 value_ = realloc(NULL, test_size_); | |
617 }, ""); | |
600 } | 618 } |
601 | 619 |
602 TEST_F(OutOfMemoryTest, Calloc) { | 620 TEST_F(OutOfMemoryDeathTest, Calloc) { |
603 ASSERT_DEATH(value_ = calloc(1024, test_size_ / 1024L), ""); | 621 ASSERT_DEATH({ |
622 SetUpInDeathAssert(); | |
623 value_ = calloc(1024, test_size_ / 1024L); | |
624 }, ""); | |
604 } | 625 } |
605 | 626 |
606 TEST_F(OutOfMemoryTest, Valloc) { | 627 TEST_F(OutOfMemoryDeathTest, Valloc) { |
607 ASSERT_DEATH(value_ = valloc(test_size_), ""); | 628 ASSERT_DEATH({ |
629 SetUpInDeathAssert(); | |
630 value_ = valloc(test_size_); | |
631 }, ""); | |
608 } | 632 } |
609 | 633 |
610 #if defined(OS_LINUX) | 634 #if defined(OS_LINUX) |
611 TEST_F(OutOfMemoryTest, Pvalloc) { | 635 TEST_F(OutOfMemoryDeathTest, Pvalloc) { |
612 ASSERT_DEATH(value_ = pvalloc(test_size_), ""); | 636 ASSERT_DEATH({ |
637 SetUpInDeathAssert(); | |
638 value_ = pvalloc(test_size_); | |
639 }, ""); | |
613 } | 640 } |
614 | 641 |
615 TEST_F(OutOfMemoryTest, Memalign) { | 642 TEST_F(OutOfMemoryDeathTest, Memalign) { |
616 ASSERT_DEATH(value_ = memalign(4, test_size_), ""); | 643 ASSERT_DEATH({ |
644 SetUpInDeathAssert(); | |
645 value_ = memalign(4, test_size_); | |
646 }, ""); | |
617 } | 647 } |
618 | 648 |
619 TEST_F(OutOfMemoryTest, ViaSharedLibraries) { | 649 TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) { |
620 // g_try_malloc is documented to return NULL on failure. (g_malloc is the | 650 // g_try_malloc is documented to return NULL on failure. (g_malloc is the |
621 // 'safe' default that crashes if allocation fails). However, since we have | 651 // 'safe' default that crashes if allocation fails). However, since we have |
622 // hopefully overridden malloc, even g_try_malloc should fail. This tests | 652 // hopefully overridden malloc, even g_try_malloc should fail. This tests |
623 // that the run-time symbol resolution is overriding malloc for shared | 653 // that the run-time symbol resolution is overriding malloc for shared |
624 // libraries as well as for our code. | 654 // libraries as well as for our code. |
625 ASSERT_DEATH(value_ = g_try_malloc(test_size_), ""); | 655 ASSERT_DEATH({ |
656 SetUpInDeathAssert(); | |
657 value_ = g_try_malloc(test_size_); | |
658 }, ""); | |
626 } | 659 } |
627 #endif // OS_LINUX | 660 #endif // OS_LINUX |
628 | 661 |
629 #if defined(OS_POSIX) | 662 #if defined(OS_POSIX) |
630 TEST_F(OutOfMemoryTest, Posix_memalign) { | 663 TEST_F(OutOfMemoryDeathTest, Posix_memalign) { |
631 typedef int (*memalign_t)(void **, size_t, size_t); | 664 typedef int (*memalign_t)(void **, size_t, size_t); |
632 #if defined(OS_MACOSX) | 665 #if defined(OS_MACOSX) |
633 // posix_memalign only exists on >= 10.6. Use dlsym to grab it at runtime | 666 // posix_memalign only exists on >= 10.6. Use dlsym to grab it at runtime |
634 // because it may not be present in the SDK used for compilation. | 667 // because it may not be present in the SDK used for compilation. |
635 memalign_t memalign = | 668 memalign_t memalign = |
636 reinterpret_cast<memalign_t>(dlsym(RTLD_DEFAULT, "posix_memalign")); | 669 reinterpret_cast<memalign_t>(dlsym(RTLD_DEFAULT, "posix_memalign")); |
637 #else | 670 #else |
638 memalign_t memalign = posix_memalign; | 671 memalign_t memalign = posix_memalign; |
639 #endif // OS_* | 672 #endif // OS_* |
640 if (memalign) { | 673 if (memalign) { |
641 // Grab the return value of posix_memalign to silence a compiler warning | 674 // Grab the return value of posix_memalign to silence a compiler warning |
642 // about unused return values. We don't actually care about the return | 675 // about unused return values. We don't actually care about the return |
643 // value, since we're asserting death. | 676 // value, since we're asserting death. |
644 ASSERT_DEATH(EXPECT_EQ(ENOMEM, memalign(&value_, 8, test_size_)), ""); | 677 ASSERT_DEATH({ |
678 SetUpInDeathAssert(); | |
679 EXPECT_EQ(ENOMEM, memalign(&value_, 8, test_size_)); | |
680 }, ""); | |
645 } | 681 } |
646 } | 682 } |
647 #endif // OS_POSIX | 683 #endif // OS_POSIX |
648 | 684 |
649 #if defined(OS_MACOSX) | 685 #if defined(OS_MACOSX) |
650 | 686 |
651 // Purgeable zone tests (if it exists) | 687 // Purgeable zone tests (if it exists) |
652 | 688 |
653 TEST_F(OutOfMemoryTest, MallocPurgeable) { | 689 TEST_F(OutOfMemoryDeathTest, MallocPurgeable) { |
654 malloc_zone_t* zone = base::GetPurgeableZone(); | 690 malloc_zone_t* zone = base::GetPurgeableZone(); |
655 if (zone) | 691 if (zone) |
656 ASSERT_DEATH(value_ = malloc_zone_malloc(zone, test_size_), ""); | 692 ASSERT_DEATH({ |
693 SetUpInDeathAssert(); | |
694 value_ = malloc_zone_malloc(zone, test_size_); | |
695 }, ""); | |
657 } | 696 } |
658 | 697 |
659 TEST_F(OutOfMemoryTest, ReallocPurgeable) { | 698 TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) { |
660 malloc_zone_t* zone = base::GetPurgeableZone(); | 699 malloc_zone_t* zone = base::GetPurgeableZone(); |
661 if (zone) | 700 if (zone) |
662 ASSERT_DEATH(value_ = malloc_zone_realloc(zone, NULL, test_size_), ""); | 701 ASSERT_DEATH({ |
702 SetUpInDeathAssert(); | |
703 value_ = malloc_zone_realloc(zone, NULL, test_size_); | |
704 }, ""); | |
663 } | 705 } |
664 | 706 |
665 TEST_F(OutOfMemoryTest, CallocPurgeable) { | 707 TEST_F(OutOfMemoryDeathTest, CallocPurgeable) { |
666 malloc_zone_t* zone = base::GetPurgeableZone(); | 708 malloc_zone_t* zone = base::GetPurgeableZone(); |
667 if (zone) | 709 if (zone) |
668 ASSERT_DEATH(value_ = malloc_zone_calloc(zone, 1024, test_size_ / 1024L), | 710 ASSERT_DEATH({ |
669 ""); | 711 SetUpInDeathAssert(); |
712 value_ = malloc_zone_calloc(zone, 1024, test_size_ / 1024L); | |
713 }, ""); | |
670 } | 714 } |
671 | 715 |
672 TEST_F(OutOfMemoryTest, VallocPurgeable) { | 716 TEST_F(OutOfMemoryDeathTest, VallocPurgeable) { |
673 malloc_zone_t* zone = base::GetPurgeableZone(); | 717 malloc_zone_t* zone = base::GetPurgeableZone(); |
674 if (zone) | 718 if (zone) |
675 ASSERT_DEATH(value_ = malloc_zone_valloc(zone, test_size_), ""); | 719 ASSERT_DEATH({ |
720 SetUpInDeathAssert(); | |
721 value_ = malloc_zone_valloc(zone, test_size_); | |
722 }, ""); | |
676 } | 723 } |
677 | 724 |
678 TEST_F(OutOfMemoryTest, PosixMemalignPurgeable) { | 725 TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) { |
679 malloc_zone_t* zone = base::GetPurgeableZone(); | 726 malloc_zone_t* zone = base::GetPurgeableZone(); |
680 | 727 |
681 typedef void* (*zone_memalign_t)(malloc_zone_t*, size_t, size_t); | 728 typedef void* (*zone_memalign_t)(malloc_zone_t*, size_t, size_t); |
682 // malloc_zone_memalign only exists on >= 10.6. Use dlsym to grab it at | 729 // malloc_zone_memalign only exists on >= 10.6. Use dlsym to grab it at |
683 // runtime because it may not be present in the SDK used for compilation. | 730 // runtime because it may not be present in the SDK used for compilation. |
684 zone_memalign_t zone_memalign = | 731 zone_memalign_t zone_memalign = |
685 reinterpret_cast<zone_memalign_t>( | 732 reinterpret_cast<zone_memalign_t>( |
686 dlsym(RTLD_DEFAULT, "malloc_zone_memalign")); | 733 dlsym(RTLD_DEFAULT, "malloc_zone_memalign")); |
687 | 734 |
688 if (zone && zone_memalign) { | 735 if (zone && zone_memalign) { |
689 ASSERT_DEATH(value_ = zone_memalign(zone, 8, test_size_), ""); | 736 ASSERT_DEATH({ |
737 SetUpInDeathAssert(); | |
738 value_ = zone_memalign(zone, 8, test_size_); | |
739 }, ""); | |
690 } | 740 } |
691 } | 741 } |
692 | 742 |
693 // Since these allocation functions take a signed size, it's possible that | 743 // Since these allocation functions take a signed size, it's possible that |
694 // calling them just once won't be enough to exhaust memory. In the 32-bit | 744 // calling them just once won't be enough to exhaust memory. In the 32-bit |
695 // environment, it's likely that these allocation attempts will fail because | 745 // environment, it's likely that these allocation attempts will fail because |
696 // not enough contiguous address space is availble. In the 64-bit environment, | 746 // not enough contiguous address space is availble. In the 64-bit environment, |
697 // it's likely that they'll fail because they would require a preposterous | 747 // it's likely that they'll fail because they would require a preposterous |
698 // amount of (virtual) memory. | 748 // amount of (virtual) memory. |
699 | 749 |
700 TEST_F(OutOfMemoryTest, CFAllocatorSystemDefault) { | 750 TEST_F(OutOfMemoryDeathTest, CFAllocatorSystemDefault) { |
701 ASSERT_DEATH(while ((value_ = | 751 ASSERT_DEATH({ |
702 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}, ""); | 752 SetUpInDeathAssert(); |
753 while ((value_ = | |
754 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {} | |
755 }, ""); | |
703 } | 756 } |
704 | 757 |
705 TEST_F(OutOfMemoryTest, CFAllocatorMalloc) { | 758 TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) { |
706 ASSERT_DEATH(while ((value_ = | 759 ASSERT_DEATH({ |
707 base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {}, ""); | 760 SetUpInDeathAssert(); |
761 while ((value_ = | |
762 base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {} | |
763 }, ""); | |
708 } | 764 } |
709 | 765 |
710 TEST_F(OutOfMemoryTest, CFAllocatorMallocZone) { | 766 TEST_F(OutOfMemoryDeathTest, CFAllocatorMallocZone) { |
711 ASSERT_DEATH(while ((value_ = | 767 ASSERT_DEATH({ |
712 base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}, ""); | 768 SetUpInDeathAssert(); |
769 while ((value_ = | |
770 base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {} | |
771 }, ""); | |
713 } | 772 } |
714 | 773 |
715 #if !defined(ARCH_CPU_64_BITS) | 774 #if !defined(ARCH_CPU_64_BITS) |
716 | 775 |
717 // See process_util_unittest_mac.mm for an explanation of why this test isn't | 776 // See process_util_unittest_mac.mm for an explanation of why this test isn't |
718 // run in the 64-bit environment. | 777 // run in the 64-bit environment. |
719 | 778 |
720 TEST_F(OutOfMemoryTest, PsychoticallyBigObjCObject) { | 779 TEST_F(OutOfMemoryDeathTest, PsychoticallyBigObjCObject) { |
721 ASSERT_DEATH(while ((value_ = | 780 ASSERT_DEATH({ |
722 base::AllocatePsychoticallyBigObjCObject())) {}, ""); | 781 SetUpInDeathAssert(); |
782 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | |
783 }, ""); | |
723 } | 784 } |
724 | 785 |
725 #endif // !ARCH_CPU_64_BITS | 786 #endif // !ARCH_CPU_64_BITS |
726 #endif // OS_MACOSX | 787 #endif // OS_MACOSX |
727 | 788 |
728 #endif // !defined(OS_WIN) | 789 #endif // !defined(OS_WIN) |
OLD | NEW |