| 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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // but does not output anything. | 72 // but does not output anything. |
| 73 ASSERT_DEATH(free(buf), ""); | 73 ASSERT_DEATH(free(buf), ""); |
| 74 #elif defined(ADDRESS_SANITIZER) | 74 #elif defined(ADDRESS_SANITIZER) |
| 75 // AddressSanitizer replaces malloc() and prints a different error message on | 75 // AddressSanitizer replaces malloc() and prints a different error message on |
| 76 // heap corruption. | 76 // heap corruption. |
| 77 ASSERT_DEATH(free(buf), "attempting free on address which " | 77 ASSERT_DEATH(free(buf), "attempting free on address which " |
| 78 "was not malloc\\(\\)-ed"); | 78 "was not malloc\\(\\)-ed"); |
| 79 #else | 79 #else |
| 80 ADD_FAILURE() << "This test is not supported in this build configuration."; | 80 ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 81 #endif | 81 #endif |
| 82 |
| 83 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 84 base::allocator::UninterceptMallocZonesForTesting(); |
| 85 #endif |
| 82 } | 86 } |
| 83 | 87 |
| 84 #endif // defined(OS_MACOSX) | 88 #endif // defined(OS_MACOSX) |
| 85 | 89 |
| 86 TEST(MemoryTest, AllocatorShimWorking) { | 90 TEST(MemoryTest, AllocatorShimWorking) { |
| 87 #if defined(OS_MACOSX) | 91 #if defined(OS_MACOSX) |
| 88 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | 92 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 89 base::allocator::InitializeAllocatorShim(); | 93 base::allocator::InitializeAllocatorShim(); |
| 90 #endif | 94 #endif |
| 91 base::allocator::InterceptAllocationsMac(); | 95 base::allocator::InterceptAllocationsMac(); |
| 92 #endif | 96 #endif |
| 93 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | 97 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
| 98 |
| 99 #if defined(OS_MACOSX) |
| 100 base::allocator::UninterceptMallocZonesForTesting(); |
| 101 #endif |
| 94 } | 102 } |
| 95 | 103 |
| 96 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan | 104 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan |
| 97 // configurations: only test the real allocator. | 105 // configurations: only test the real allocator. |
| 98 // Windows only supports these tests with the allocator shim in place. | 106 // Windows only supports these tests with the allocator shim in place. |
| 99 #if !defined(OS_OPENBSD) && \ | 107 #if !defined(OS_OPENBSD) && \ |
| 100 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ | 108 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ |
| 101 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 109 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 102 | 110 |
| 103 namespace { | 111 namespace { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 base::allocator::InitializeAllocatorShim(); | 147 base::allocator::InitializeAllocatorShim(); |
| 140 #endif | 148 #endif |
| 141 | 149 |
| 142 // Must call EnableTerminationOnOutOfMemory() because that is called from | 150 // Must call EnableTerminationOnOutOfMemory() because that is called from |
| 143 // chrome's main function and therefore hasn't been called yet. | 151 // chrome's main function and therefore hasn't been called yet. |
| 144 // Since this call may result in another thread being created and death | 152 // Since this call may result in another thread being created and death |
| 145 // tests shouldn't be started in a multithread environment, this call | 153 // tests shouldn't be started in a multithread environment, this call |
| 146 // should be done inside of the ASSERT_DEATH. | 154 // should be done inside of the ASSERT_DEATH. |
| 147 base::EnableTerminationOnOutOfMemory(); | 155 base::EnableTerminationOnOutOfMemory(); |
| 148 } | 156 } |
| 157 |
| 158 #if defined(OS_MACOSX) |
| 159 void TearDown() override { |
| 160 base::allocator::UninterceptMallocZonesForTesting(); |
| 161 } |
| 162 #endif |
| 149 }; | 163 }; |
| 150 | 164 |
| 151 TEST_F(OutOfMemoryDeathTest, New) { | 165 TEST_F(OutOfMemoryDeathTest, New) { |
| 152 ASSERT_EXIT({ | 166 ASSERT_EXIT({ |
| 153 SetUpInDeathAssert(); | 167 SetUpInDeathAssert(); |
| 154 value_ = operator new(test_size_); | 168 value_ = operator new(test_size_); |
| 155 }, testing::ExitedWithCode(kExitCode), kOomRegex); | 169 }, testing::ExitedWithCode(kExitCode), kOomRegex); |
| 156 } | 170 } |
| 157 | 171 |
| 158 TEST_F(OutOfMemoryDeathTest, NewArray) { | 172 TEST_F(OutOfMemoryDeathTest, NewArray) { |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 static const size_t kSafeCallocItems = 4; | 429 static const size_t kSafeCallocItems = 4; |
| 416 | 430 |
| 417 void SetUp() override { | 431 void SetUp() override { |
| 418 OutOfMemoryTest::SetUp(); | 432 OutOfMemoryTest::SetUp(); |
| 419 | 433 |
| 420 // We enable termination on OOM - just as Chrome does at early | 434 // We enable termination on OOM - just as Chrome does at early |
| 421 // initialization - and test that UncheckedMalloc and UncheckedCalloc | 435 // initialization - and test that UncheckedMalloc and UncheckedCalloc |
| 422 // properly by-pass this in order to allow the caller to handle OOM. | 436 // properly by-pass this in order to allow the caller to handle OOM. |
| 423 base::EnableTerminationOnOutOfMemory(); | 437 base::EnableTerminationOnOutOfMemory(); |
| 424 } | 438 } |
| 439 |
| 440 void TearDown() override { |
| 441 #if defined(OS_MACOSX) |
| 442 base::allocator::UninterceptMallocZonesForTesting(); |
| 443 #endif |
| 444 } |
| 425 }; | 445 }; |
| 426 | 446 |
| 427 #if defined(OS_WIN) | 447 #if defined(OS_WIN) |
| 428 | 448 |
| 429 namespace { | 449 namespace { |
| 430 | 450 |
| 431 DWORD HandleOutOfMemoryException(EXCEPTION_POINTERS* exception_ptrs, | 451 DWORD HandleOutOfMemoryException(EXCEPTION_POINTERS* exception_ptrs, |
| 432 size_t expected_size) { | 452 size_t expected_size) { |
| 433 EXPECT_EQ(base::win::kOomExceptionCode, | 453 EXPECT_EQ(base::win::kOomExceptionCode, |
| 434 exception_ptrs->ExceptionRecord->ExceptionCode); | 454 exception_ptrs->ExceptionRecord->ExceptionCode); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 bytes = static_cast<const char*>(value_); | 503 bytes = static_cast<const char*>(value_); |
| 484 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 504 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 485 EXPECT_EQ(0, bytes[i]); | 505 EXPECT_EQ(0, bytes[i]); |
| 486 free(value_); | 506 free(value_); |
| 487 | 507 |
| 488 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 508 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 489 EXPECT_TRUE(value_ == NULL); | 509 EXPECT_TRUE(value_ == NULL); |
| 490 } | 510 } |
| 491 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && | 511 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 492 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 512 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |