Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/process/memory.h" | 7 #include "base/process/memory.h" |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 "was not malloc\\(\\)-ed"); | 142 "was not malloc\\(\\)-ed"); |
| 143 #else | 143 #else |
| 144 ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*" | 144 ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*" |
| 145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" | 145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" |
| 146 "Terminating process due to a potential for future heap corruption"); | 146 "Terminating process due to a potential for future heap corruption"); |
| 147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) | 147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) |
| 148 } | 148 } |
| 149 | 149 |
| 150 #endif // defined(OS_MACOSX) | 150 #endif // defined(OS_MACOSX) |
| 151 | 151 |
| 152 // Android doesn't implement set_new_handler, so we can't use the | |
| 153 // OutOfMemoryTest cases. | |
| 154 // OpenBSD does not support these tests either. | |
| 155 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. | |
| 156 // functions so that they don't crash if the program is out of memory, so the | |
| 157 // OOM tests aren't supposed to work. | |
| 158 // TODO(vandebo) make this work on Windows too. | |
| 159 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | |
| 160 !defined(OS_WIN) && \ | |
| 161 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) | |
| 162 | |
| 163 #if defined(USE_TCMALLOC) | 152 #if defined(USE_TCMALLOC) |
| 164 extern "C" { | 153 extern "C" { |
| 165 int tc_set_new_mode(int mode); | 154 int tc_set_new_mode(int mode); |
| 166 } | 155 } |
| 167 #endif // defined(USE_TCMALLOC) | 156 #endif // defined(USE_TCMALLOC) |
| 168 | 157 |
| 169 class OutOfMemoryDeathTest : public testing::Test { | 158 class OutOfMemoryDeathTest : public testing::Test { |
| 170 public: | 159 public: |
| 171 OutOfMemoryDeathTest() | 160 OutOfMemoryDeathTest() |
| 172 : value_(NULL), | 161 : value_(NULL), |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 193 // tests shouldn't be started in a multithread environment, this call | 182 // tests shouldn't be started in a multithread environment, this call |
| 194 // should be done inside of the ASSERT_DEATH. | 183 // should be done inside of the ASSERT_DEATH. |
| 195 base::EnableTerminationOnOutOfMemory(); | 184 base::EnableTerminationOnOutOfMemory(); |
| 196 } | 185 } |
| 197 | 186 |
| 198 void* value_; | 187 void* value_; |
| 199 size_t test_size_; | 188 size_t test_size_; |
| 200 ssize_t signed_test_size_; | 189 ssize_t signed_test_size_; |
| 201 }; | 190 }; |
| 202 | 191 |
| 192 // Android doesn't implement set_new_handler, so we can't use the | |
| 193 // OutOfMemoryTest cases. | |
| 194 // OpenBSD does not support these tests either. | |
| 195 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. | |
| 196 // functions so that they don't crash if the program is out of memory, so the | |
| 197 // OOM tests aren't supposed to work. | |
| 198 // TODO(vandebo) make this work on Windows too. | |
| 199 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | |
| 200 !defined(OS_WIN) && \ | |
| 201 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) | |
| 202 | |
| 203 TEST_F(OutOfMemoryDeathTest, New) { | 203 TEST_F(OutOfMemoryDeathTest, New) { |
| 204 ASSERT_DEATH({ | 204 ASSERT_DEATH({ |
| 205 SetUpInDeathAssert(); | 205 SetUpInDeathAssert(); |
| 206 value_ = operator new(test_size_); | 206 value_ = operator new(test_size_); |
| 207 }, ""); | 207 }, ""); |
| 208 } | 208 } |
| 209 | 209 |
| 210 TEST_F(OutOfMemoryDeathTest, NewArray) { | 210 TEST_F(OutOfMemoryDeathTest, NewArray) { |
| 211 ASSERT_DEATH({ | 211 ASSERT_DEATH({ |
| 212 SetUpInDeathAssert(); | 212 SetUpInDeathAssert(); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 SetUpInDeathAssert(); | 370 SetUpInDeathAssert(); |
| 371 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | 371 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} |
| 372 }, ""); | 372 }, ""); |
| 373 } | 373 } |
| 374 | 374 |
| 375 #endif // !ARCH_CPU_64_BITS | 375 #endif // !ARCH_CPU_64_BITS |
| 376 #endif // OS_MACOSX | 376 #endif // OS_MACOSX |
| 377 | 377 |
| 378 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | 378 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
| 379 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | 379 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
| 380 | |
| 381 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | |
| 382 // well on Windows. | |
| 383 #if defined(USE_TCMALLOC) || defined(LIBC_GLIBC) | |
| 384 | |
| 385 TEST_F(OutOfMemoryDeathTest, UncheckedMalloc) { | |
| 386 SetUpInDeathAssert(); | |
|
Scott Hess - ex-Googler
2014/03/13 22:30:33
This shouldn't be a death test, right?
kbalazs
2014/03/13 23:07:00
No I just tried to reuse the class because I need
Scott Hess - ex-Googler
2014/03/13 23:18:45
Yeah, I think "death test" has meaning that you do
kbalazs
2014/03/18 20:03:54
I factored the common code into a base class.
| |
| 387 EXPECT_TRUE(base::UncheckedMalloc(512, &value_)); | |
| 388 EXPECT_TRUE(value_ != NULL); | |
|
Scott Hess - ex-Googler
2014/03/13 22:30:33
When these succeed, you'll need to free value_.
kbalazs
2014/03/18 20:03:54
Done.
| |
| 389 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | |
| 390 EXPECT_TRUE(value_ == NULL); | |
| 391 } | |
| 392 | |
| 393 TEST_F(OutOfMemoryDeathTest, UncheckedCalloc) { | |
| 394 SetUpInDeathAssert(); | |
|
Scott Hess - ex-Googler
2014/03/13 22:30:33
Also not a death test?
| |
| 395 EXPECT_TRUE(base::UncheckedCalloc(1, 512, &value_)); | |
| 396 EXPECT_TRUE(value_ != NULL); | |
| 397 char zero[512]; | |
| 398 memset(zero, 0, 512); | |
| 399 EXPECT_EQ(memcmp(value_, zero, 512), 0); | |
|
Scott Hess - ex-Googler
2014/03/13 22:30:33
I think prefer a kConstant to copies of 512.
I th
kbalazs
2014/03/18 20:03:54
Done.
| |
| 400 | |
| 401 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | |
| 402 EXPECT_TRUE(value_ == NULL); | |
| 403 } | |
| 404 | |
| 405 #endif | |
| OLD | NEW |