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 <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 ADD_FAILURE() << "This test is not supported in this build configuration."; | 75 ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 76 #endif | 76 #endif |
| 77 } | 77 } |
| 78 | 78 |
| 79 #endif // defined(OS_MACOSX) | 79 #endif // defined(OS_MACOSX) |
| 80 | 80 |
| 81 TEST(MemoryTest, AllocatorShimWorking) { | 81 TEST(MemoryTest, AllocatorShimWorking) { |
| 82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | 82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Android doesn't implement set_new_handler, so we can't use the | 85 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan |
| 86 // OutOfMemoryTest cases. OpenBSD does not support these tests either. | 86 // configurations: only test the real allocator. |
| 87 // Don't test these on ASan/TSan/MSan configurations: only test the real | |
| 88 // allocator. | |
| 89 // Windows only supports these tests with the allocator shim in place. | 87 // Windows only supports these tests with the allocator shim in place. |
| 90 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | 88 #if !defined(OS_OPENBSD) && \ |
| 91 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ | 89 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ |
| 92 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 90 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 93 | 91 |
| 94 namespace { | 92 namespace { |
| 95 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
| 96 // Windows raises an exception rather than using LOG(FATAL) in order to make the | 94 // Windows raises an exception rather than using LOG(FATAL) in order to make the |
| 97 // exit code unique to OOM. | 95 // exit code unique to OOM. |
| 98 const char* kOomRegex = ""; | 96 const char* kOomRegex = ""; |
| 99 const int kExitCode = base::win::kOomExceptionCode; | 97 const int kExitCode = base::win::kOomExceptionCode; |
| 100 #else | 98 #else |
| 101 const char* kOomRegex = "Out of memory"; | 99 const char* kOomRegex = "Out of memory"; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { | 180 TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { |
| 183 ASSERT_EXIT({ | 181 ASSERT_EXIT({ |
| 184 SetUpInDeathAssert(); | 182 SetUpInDeathAssert(); |
| 185 value_ = _aligned_realloc(NULL, test_size_, 8); | 183 value_ = _aligned_realloc(NULL, test_size_, 8); |
| 186 }, testing::ExitedWithCode(kExitCode), kOomRegex); | 184 }, testing::ExitedWithCode(kExitCode), kOomRegex); |
| 187 } | 185 } |
| 188 #endif // defined(OS_WIN) | 186 #endif // defined(OS_WIN) |
| 189 | 187 |
| 190 // OS X has no 2Gb allocation limit. | 188 // OS X has no 2Gb allocation limit. |
| 191 // See https://crbug.com/169327. | 189 // See https://crbug.com/169327. |
| 192 #if !defined(OS_MACOSX) | 190 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
|
Nico
2016/08/03 14:13:20
Why ifdef out these tests? Maybe update the commen
Primiano Tucci (use gerrit)
2016/08/03 15:04:48
So the thing is that, as things stands today, Andr
Nico
2016/08/03 15:06:52
Doesn't this test run on linux as well? Or am I mi
Primiano Tucci (use gerrit)
2016/08/03 15:21:45
Yes, it does. See logdog output for this CL in htt
| |
| 193 TEST_F(OutOfMemoryDeathTest, SecurityNew) { | 191 TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
| 194 ASSERT_EXIT({ | 192 ASSERT_EXIT({ |
| 195 SetUpInDeathAssert(); | 193 SetUpInDeathAssert(); |
| 196 value_ = operator new(insecure_test_size_); | 194 value_ = operator new(insecure_test_size_); |
| 197 }, testing::ExitedWithCode(kExitCode), kOomRegex); | 195 }, testing::ExitedWithCode(kExitCode), kOomRegex); |
| 198 } | 196 } |
| 199 | 197 |
| 200 TEST_F(OutOfMemoryDeathTest, SecurityNewArray) { | 198 TEST_F(OutOfMemoryDeathTest, SecurityNewArray) { |
| 201 ASSERT_EXIT({ | 199 ASSERT_EXIT({ |
| 202 SetUpInDeathAssert(); | 200 SetUpInDeathAssert(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 | 232 |
| 235 // POSIX does not define an aligned realloc function. | 233 // POSIX does not define an aligned realloc function. |
| 236 #if defined(OS_WIN) | 234 #if defined(OS_WIN) |
| 237 TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { | 235 TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { |
| 238 ASSERT_EXIT({ | 236 ASSERT_EXIT({ |
| 239 SetUpInDeathAssert(); | 237 SetUpInDeathAssert(); |
| 240 value_ = _aligned_realloc(NULL, insecure_test_size_, 8); | 238 value_ = _aligned_realloc(NULL, insecure_test_size_, 8); |
| 241 }, testing::ExitedWithCode(kExitCode), kOomRegex); | 239 }, testing::ExitedWithCode(kExitCode), kOomRegex); |
| 242 } | 240 } |
| 243 #endif // defined(OS_WIN) | 241 #endif // defined(OS_WIN) |
| 244 #endif // !defined(OS_MACOSX) | 242 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 245 | 243 |
| 246 #if defined(OS_LINUX) | 244 #if defined(OS_LINUX) |
| 247 | 245 |
| 248 TEST_F(OutOfMemoryDeathTest, Valloc) { | 246 TEST_F(OutOfMemoryDeathTest, Valloc) { |
| 249 ASSERT_DEATH({ | 247 ASSERT_DEATH({ |
| 250 SetUpInDeathAssert(); | 248 SetUpInDeathAssert(); |
| 251 value_ = valloc(test_size_); | 249 value_ = valloc(test_size_); |
| 252 }, kOomRegex); | 250 }, kOomRegex); |
| 253 } | 251 } |
| 254 | 252 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 | 404 |
| 407 // We enable termination on OOM - just as Chrome does at early | 405 // We enable termination on OOM - just as Chrome does at early |
| 408 // initialization - and test that UncheckedMalloc and UncheckedCalloc | 406 // initialization - and test that UncheckedMalloc and UncheckedCalloc |
| 409 // properly by-pass this in order to allow the caller to handle OOM. | 407 // properly by-pass this in order to allow the caller to handle OOM. |
| 410 base::EnableTerminationOnOutOfMemory(); | 408 base::EnableTerminationOnOutOfMemory(); |
| 411 } | 409 } |
| 412 }; | 410 }; |
| 413 | 411 |
| 414 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | 412 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
| 415 // on Windows as well. | 413 // on Windows as well. |
| 416 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() | |
| 417 // under sanitizer tools. | |
| 418 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | |
| 419 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { | 414 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
| 420 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); | 415 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
| 421 EXPECT_TRUE(value_ != NULL); | 416 EXPECT_TRUE(value_ != NULL); |
| 422 free(value_); | 417 free(value_); |
| 423 | 418 |
| 424 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | 419 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
| 425 EXPECT_TRUE(value_ == NULL); | 420 EXPECT_TRUE(value_ == NULL); |
| 426 } | 421 } |
| 427 | 422 |
| 428 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { | 423 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
| 429 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); | 424 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); |
| 430 EXPECT_TRUE(value_ != NULL); | 425 EXPECT_TRUE(value_ != NULL); |
| 431 const char* bytes = static_cast<const char*>(value_); | 426 const char* bytes = static_cast<const char*>(value_); |
| 432 for (size_t i = 0; i < kSafeMallocSize; ++i) | 427 for (size_t i = 0; i < kSafeMallocSize; ++i) |
| 433 EXPECT_EQ(0, bytes[i]); | 428 EXPECT_EQ(0, bytes[i]); |
| 434 free(value_); | 429 free(value_); |
| 435 | 430 |
| 436 EXPECT_TRUE( | 431 EXPECT_TRUE( |
| 437 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); | 432 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); |
| 438 EXPECT_TRUE(value_ != NULL); | 433 EXPECT_TRUE(value_ != NULL); |
| 439 bytes = static_cast<const char*>(value_); | 434 bytes = static_cast<const char*>(value_); |
| 440 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 435 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 441 EXPECT_EQ(0, bytes[i]); | 436 EXPECT_EQ(0, bytes[i]); |
| 442 free(value_); | 437 free(value_); |
| 443 | 438 |
| 444 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 439 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 445 EXPECT_TRUE(value_ == NULL); | 440 EXPECT_TRUE(value_ == NULL); |
| 446 } | 441 } |
| 447 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 442 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 448 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 443 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 449 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | |
| OLD | NEW |