Index: base/process/memory_unittest.cc |
diff --git a/base/process/memory_unittest.cc b/base/process/memory_unittest.cc |
index 5a1bcdfb1ba4d1bb195092b5d9889128dad06a1c..6ab7311227b4154cdc33a6f201612636aa5744be 100644 |
--- a/base/process/memory_unittest.cc |
+++ b/base/process/memory_unittest.cc |
@@ -149,34 +149,23 @@ TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
#endif // defined(OS_MACOSX) |
-// Android doesn't implement set_new_handler, so we can't use the |
-// OutOfMemoryTest cases. |
-// OpenBSD does not support these tests either. |
-// AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. |
-// functions so that they don't crash if the program is out of memory, so the |
-// OOM tests aren't supposed to work. |
-// TODO(vandebo) make this work on Windows too. |
-#if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
- !defined(OS_WIN) && \ |
- !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) |
- |
#if defined(USE_TCMALLOC) |
extern "C" { |
int tc_set_new_mode(int mode); |
} |
#endif // defined(USE_TCMALLOC) |
-class OutOfMemoryDeathTest : public testing::Test { |
+class MemoryTest : public testing::Test { |
public: |
- OutOfMemoryDeathTest() |
- : value_(NULL), |
+ MemoryTest() |
+ : value_(NULL), |
// Make test size as large as possible minus a few pages so |
// that alignment or other rounding doesn't make it wrap. |
- test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), |
- signed_test_size_(std::numeric_limits<ssize_t>::max()) { |
+ test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), |
+ signed_test_size_(std::numeric_limits<ssize_t>::max()) { |
} |
-#if defined(USE_TCMALLOC) |
+ #if defined(USE_TCMALLOC) |
Scott Hess - ex-Googler
2014/03/18 21:10:38
Indentation.
|
virtual void SetUp() OVERRIDE { |
tc_set_new_mode(1); |
} |
@@ -186,20 +175,35 @@ class OutOfMemoryDeathTest : public testing::Test { |
} |
#endif // defined(USE_TCMALLOC) |
- void SetUpInDeathAssert() { |
- // Must call EnableTerminationOnOutOfMemory() because that is called from |
- // chrome's main function and therefore hasn't been called yet. |
- // Since this call may result in another thread being created and death |
- // tests shouldn't be started in a multithread environment, this call |
- // should be done inside of the ASSERT_DEATH. |
- base::EnableTerminationOnOutOfMemory(); |
- } |
- |
+ protected: |
void* value_; |
size_t test_size_; |
ssize_t signed_test_size_; |
}; |
+class OutOfMemoryDeathTest : public MemoryTest { |
+ public: |
+ void SetUpInDeathAssert() { |
Scott Hess - ex-Googler
2014/03/18 21:10:38
I think the function is indented 2 spaces too much
|
+ // Must call EnableTerminationOnOutOfMemory() because that is called from |
+ // chrome's main function and therefore hasn't been called yet. |
+ // Since this call may result in another thread being created and death |
+ // tests shouldn't be started in a multithread environment, this call |
+ // should be done inside of the ASSERT_DEATH. |
+ base::EnableTerminationOnOutOfMemory(); |
+ } |
+}; |
+ |
+// Android doesn't implement set_new_handler, so we can't use the |
+// OutOfMemoryTest cases. |
+// OpenBSD does not support these tests either. |
+// AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. |
+// functions so that they don't crash if the program is out of memory, so the |
+// OOM tests aren't supposed to work. |
+// TODO(vandebo) make this work on Windows too. |
+#if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
+ !defined(OS_WIN) && \ |
+ !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) |
+ |
TEST_F(OutOfMemoryDeathTest, New) { |
ASSERT_DEATH({ |
SetUpInDeathAssert(); |
@@ -377,3 +381,50 @@ TEST_F(OutOfMemoryDeathTest, PsychoticallyBigObjCObject) { |
#endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
// !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
+ |
+// TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
+// well on Windows. |
+#if defined(USE_TCMALLOC) || defined(LIBC_GLIBC) |
+ |
+class OutOfMemoryHandledTest : public MemoryTest { |
+ public: |
+ static const size_t kSafeMallocSize = 512; |
+ static const size_t kSafeCallocSize = 128; |
+ static const size_t kSafeCallocItems = 4; |
+ |
+ virtual void SetUp() { |
+ MemoryTest::SetUp(); |
+ base::EnableTerminationOnOutOfMemory(); |
Scott Hess - ex-Googler
2014/03/18 21:10:38
Suggest a comment on this line about being importa
|
+ } |
+}; |
+ |
+TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
+ EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
+ EXPECT_TRUE(value_ != NULL); |
+ free(value_); |
+ |
+ EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
+ EXPECT_TRUE(value_ == NULL); |
+} |
+ |
+TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
+ EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); |
+ EXPECT_TRUE(value_ != NULL); |
+ const char* bytes = static_cast<const char*>(value_); |
+ for (size_t i = 0; i < kSafeMallocSize; ++i) |
+ EXPECT_EQ(bytes[i], 0); |
Scott Hess - ex-Googler
2014/03/18 21:10:38
EXPECT_EQ(expected, actual), so 0 on the left, byt
|
+ free(value_); |
+ |
+ EXPECT_TRUE( |
+ base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); |
+ EXPECT_TRUE(value_ != NULL); |
+ bytes = static_cast<const char*>(value_); |
+ for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
+ EXPECT_EQ(bytes[i], 0); |
Scott Hess - ex-Googler
2014/03/18 21:10:38
Also here.
|
+ free(value_); |
+ |
+ EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
+ EXPECT_TRUE(value_ == NULL); |
+} |
+ |
+#endif |