Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(930)

Unified Diff: base/process/memory_unittest.cc

Issue 55333002: Make possible to check memory allocations inside chromium (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improve testing code Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/process/memory_mac.mm ('k') | base/process/memory_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « base/process/memory_mac.mm ('k') | base/process/memory_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698