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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 namespace { | 92 namespace { |
| 93 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
| 94 // 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 |
| 95 // exit code unique to OOM. | 95 // exit code unique to OOM. |
| 96 const char* kOomRegex = ""; | 96 const char* kOomRegex = ""; |
| 97 const int kExitCode = base::win::kOomExceptionCode; | 97 const int kExitCode = base::win::kOomExceptionCode; |
| 98 #else | 98 #else |
| 99 const char* kOomRegex = "Out of memory"; | 99 const char* kOomRegex = "Out of memory"; |
| 100 const int kExitCode = 1; | 100 const int kExitCode = 1; |
| 101 #endif | 101 #endif |
| 102 | |
| 103 // See corresponding function in content/renderer/render_frame_impl.cc | |
| 104 NOINLINE void ExhaustMemory() { | |
| 105 volatile void* ptr = nullptr; | |
| 106 do { | |
| 107 ptr = malloc(0x10000000); | |
| 108 base::debug::Alias(&ptr); | |
| 109 } while (ptr); | |
| 110 } | |
| 111 | |
| 102 } // namespace | 112 } // namespace |
| 103 | 113 |
| 104 class OutOfMemoryTest : public testing::Test { | 114 class OutOfMemoryTest : public testing::Test { |
| 105 public: | 115 public: |
| 106 OutOfMemoryTest() | 116 OutOfMemoryTest() |
| 107 : value_(NULL), | 117 : value_(NULL), |
| 108 // Make test size as large as possible minus a few pages so | 118 // Make test size as large as possible minus a few pages so |
| 109 // that alignment or other rounding doesn't make it wrap. | 119 // that alignment or other rounding doesn't make it wrap. |
| 110 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), | 120 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), |
| 111 // A test size that is > 2Gb and will cause the allocators to reject | 121 // A test size that is > 2Gb and will cause the allocators to reject |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 126 void SetUpInDeathAssert() { | 136 void SetUpInDeathAssert() { |
| 127 // Must call EnableTerminationOnOutOfMemory() because that is called from | 137 // Must call EnableTerminationOnOutOfMemory() because that is called from |
| 128 // chrome's main function and therefore hasn't been called yet. | 138 // chrome's main function and therefore hasn't been called yet. |
| 129 // Since this call may result in another thread being created and death | 139 // Since this call may result in another thread being created and death |
| 130 // tests shouldn't be started in a multithread environment, this call | 140 // tests shouldn't be started in a multithread environment, this call |
| 131 // should be done inside of the ASSERT_DEATH. | 141 // should be done inside of the ASSERT_DEATH. |
| 132 base::EnableTerminationOnOutOfMemory(); | 142 base::EnableTerminationOnOutOfMemory(); |
| 133 } | 143 } |
| 134 }; | 144 }; |
| 135 | 145 |
| 146 TEST_F(OutOfMemoryDeathTest, Exhaust) { | |
| 147 ASSERT_EXIT({ | |
| 148 SetUpInDeathAssert(); | |
| 149 ExhaustMemory(); | |
| 150 }, testing::ExitedWithCode(kExitCode), kOomRegex); | |
| 151 } | |
|
Nico
2017/01/26 17:56:53
do we really need this test? it duplicates the fun
Will Harris
2017/01/26 18:12:44
I'm not sure I totally understand what you mean by
| |
| 152 | |
| 136 TEST_F(OutOfMemoryDeathTest, New) { | 153 TEST_F(OutOfMemoryDeathTest, New) { |
| 137 ASSERT_EXIT({ | 154 ASSERT_EXIT({ |
| 138 SetUpInDeathAssert(); | 155 SetUpInDeathAssert(); |
| 139 value_ = operator new(test_size_); | 156 value_ = operator new(test_size_); |
| 140 }, testing::ExitedWithCode(kExitCode), kOomRegex); | 157 }, testing::ExitedWithCode(kExitCode), kOomRegex); |
| 141 } | 158 } |
| 142 | 159 |
| 143 TEST_F(OutOfMemoryDeathTest, NewArray) { | 160 TEST_F(OutOfMemoryDeathTest, NewArray) { |
| 144 ASSERT_EXIT({ | 161 ASSERT_EXIT({ |
| 145 SetUpInDeathAssert(); | 162 SetUpInDeathAssert(); |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 bytes = static_cast<const char*>(value_); | 451 bytes = static_cast<const char*>(value_); |
| 435 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 452 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 436 EXPECT_EQ(0, bytes[i]); | 453 EXPECT_EQ(0, bytes[i]); |
| 437 free(value_); | 454 free(value_); |
| 438 | 455 |
| 439 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 456 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 440 EXPECT_TRUE(value_ == NULL); | 457 EXPECT_TRUE(value_ == NULL); |
| 441 } | 458 } |
| 442 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && | 459 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 443 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 460 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |