Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a4 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 10 | #define _CRT_SECURE_NO_WARNINGS |
| 11 | |
| 12 | #include "base/process/memory.h" |
| 13 | |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 16 | #include <limits> |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 17 | #include <tuple> |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 18 | #include <vector> |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 19 | |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 20 | #include "base/allocator/allocator_check.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 21 | #include "base/compiler_specific.h" |
| 22 | #include "base/debug/alias.h" |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 23 | #include "base/memory/aligned_memory.h" |
Lei Zhang | 425893e | 2021-05-14 23:28:25 | [diff] [blame] | 24 | #include "base/memory/page_size.h" |
avi | beced7c | 2015-12-24 06:47:59 | [diff] [blame] | 25 | #include "build/build_config.h" |
Arthur Sonzogni | fd39d61 | 2024-06-26 08:16:23 | [diff] [blame] | 26 | #include "partition_alloc/buildflags.h" |
Yuki Shiino | 985ab91e | 2024-03-14 07:20:46 | [diff] [blame] | 27 | #include "partition_alloc/page_allocator.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 28 | #include "testing/gtest/include/gtest/gtest.h" |
| 29 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 30 | #if BUILDFLAG(IS_WIN) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 31 | #include <windows.h> |
| 32 | #endif |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 33 | #if BUILDFLAG(IS_POSIX) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #endif |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 36 | #if BUILDFLAG(IS_MAC) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 37 | #include <malloc/malloc.h> |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 38 | |
Yuki Shiino | 8d478bd | 2023-06-08 09:43:55 | [diff] [blame] | 39 | #include "base/check_op.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 40 | #include "base/process/memory_unittest_mac.h" |
Yuki Shiino | 985ab91e | 2024-03-14 07:20:46 | [diff] [blame] | 41 | #include "partition_alloc/shim/allocator_interception_apple.h" |
| 42 | #include "partition_alloc/shim/allocator_shim.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 43 | #endif |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 44 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 45 | #include <malloc.h> |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 46 | |
icoolidge | 9a4f22d | 2015-04-07 19:28:09 | [diff] [blame] | 47 | #include "base/test/malloc_wrapper.h" |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 48 | #endif |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 49 | #if BUILDFLAG(IS_ANDROID) |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 50 | #include "base/android/build_info.h" |
| 51 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 52 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 53 | #if BUILDFLAG(IS_WIN) |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 54 | |
Thiago Farina | ba7d2a42 | 2017-06-17 00:18:49 | [diff] [blame] | 55 | #if defined(COMPILER_MSVC) |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 56 | // ssize_t needed for OutOfMemoryTest. |
| 57 | #if defined(_WIN64) |
| 58 | typedef __int64 ssize_t; |
| 59 | #else |
| 60 | typedef long ssize_t; |
| 61 | #endif |
| 62 | #endif |
| 63 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 64 | // HeapQueryInformation function pointer. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 65 | typedef BOOL(WINAPI* HeapQueryFn)(HANDLE, |
| 66 | HEAP_INFORMATION_CLASS, |
| 67 | PVOID, |
| 68 | SIZE_T, |
| 69 | PSIZE_T); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 70 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 71 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 72 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 73 | #if BUILDFLAG(IS_MAC) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 74 | |
| 75 | // For the following Mac tests: |
| 76 | // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
| 77 | // test suite setup and does not need to be done again, else mach_override |
| 78 | // will fail. |
| 79 | |
Hans Wennborg | c90800e | 2021-01-21 14:39:34 | [diff] [blame] | 80 | // Wrap free() in a function to thwart Clang's -Wfree-nonheap-object warning. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 81 | static void callFree(void* ptr) { |
Hans Wennborg | c90800e | 2021-01-21 14:39:34 | [diff] [blame] | 82 | free(ptr); |
| 83 | } |
| 84 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 85 | TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 86 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 87 | allocator_shim::InitializeAllocatorShim(); |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 88 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 89 | // Assert that freeing an unallocated pointer will crash the process. |
[email protected] | de8b1ad | 2013-07-22 21:46:50 | [diff] [blame] | 90 | char buf[9]; |
Nico Weber | 57048a9c | 2019-08-29 18:24:48 | [diff] [blame] | 91 | asm("" : "=m"(buf)); // Prevent clang from being too smart. |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 92 | #if ARCH_CPU_64_BITS |
| 93 | // On 64 bit Macs, the malloc system automatically abort()s on heap corruption |
| 94 | // but does not output anything. |
Hans Wennborg | c90800e | 2021-01-21 14:39:34 | [diff] [blame] | 95 | ASSERT_DEATH(callFree(buf), ""); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 96 | #elif defined(ADDRESS_SANITIZER) |
| 97 | // AddressSanitizer replaces malloc() and prints a different error message on |
| 98 | // heap corruption. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 99 | ASSERT_DEATH(callFree(buf), |
| 100 | "attempting free on address which " |
| 101 | "was not malloc\\(\\)-ed"); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 102 | #else |
rsesek | 931d092b | 2015-01-29 17:21:52 | [diff] [blame] | 103 | ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 104 | #endif |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 105 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 106 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 107 | allocator_shim::UninterceptMallocZonesForTesting(); |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 108 | #endif |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 109 | } |
| 110 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 111 | #endif // BUILDFLAG(IS_MAC) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 112 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 113 | #if PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 114 | TEST(MemoryTest, AllocatorShimWorking) { |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 115 | #if BUILDFLAG(IS_MAC) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 116 | allocator_shim::InitializeAllocatorShim(); |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 117 | allocator_shim::InterceptAllocationsMac(); |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 118 | #endif |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 119 | ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 120 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 121 | #if BUILDFLAG(IS_MAC) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 122 | allocator_shim::UninterceptMallocZonesForTesting(); |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 123 | #endif |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 124 | } |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 125 | #endif // PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 126 | |
primiano | 227dbd3 | 2016-08-03 16:31:03 | [diff] [blame] | 127 | // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan |
| 128 | // configurations: only test the real allocator. |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 129 | #if !BUILDFLAG(IS_OPENBSD) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM) && \ |
glider | f985709 | 2015-04-21 12:58:25 | [diff] [blame] | 130 | !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 131 | |
icoolidge | 85e1309 | 2015-04-20 18:47:12 | [diff] [blame] | 132 | namespace { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 133 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 134 | #if BUILDFLAG(IS_WIN) |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 135 | |
| 136 | // Windows raises an exception in order to make the exit code unique to OOM. |
| 137 | #define ASSERT_OOM_DEATH(statement) \ |
| 138 | ASSERT_EXIT(statement, \ |
| 139 | testing::ExitedWithCode(base::win::kOomExceptionCode), "") |
| 140 | |
wfh | 8ca194a | 2016-07-20 02:06:54 | [diff] [blame] | 141 | #else |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 142 | |
| 143 | #define ASSERT_OOM_DEATH(statement) ASSERT_DEATH(statement, "") |
| 144 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 145 | #endif // BUILDFLAG(IS_WIN) |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 146 | |
icoolidge | 85e1309 | 2015-04-20 18:47:12 | [diff] [blame] | 147 | } // namespace |
| 148 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 149 | class OutOfMemoryTest : public testing::Test { |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 150 | public: |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 151 | OutOfMemoryTest() |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 152 | : // Make test size as large as possible minus a few pages so that |
| 153 | // alignment or other rounding doesn't make it wrap. |
Avi Drissman | 5c8fd42b | 2020-08-26 17:09:25 | [diff] [blame] | 154 | test_size_(std::numeric_limits<std::size_t>::max() - |
| 155 | 3 * base::GetPageSize()), |
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 156 | // A test size that is > 2Gb and will cause the allocators to reject |
| 157 | // the allocation due to security restrictions. See crbug.com/169327. |
| 158 | insecure_test_size_(std::numeric_limits<int>::max()), |
| 159 | signed_test_size_(std::numeric_limits<ssize_t>::max()) {} |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 160 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 161 | protected: |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 162 | size_t test_size_; |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 163 | size_t insecure_test_size_; |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 164 | ssize_t signed_test_size_; |
| 165 | }; |
| 166 | |
| 167 | class OutOfMemoryDeathTest : public OutOfMemoryTest { |
| 168 | public: |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 169 | void SetUpInDeathAssert() { |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 170 | #if BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 171 | allocator_shim::InitializeAllocatorShim(); |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 172 | #endif |
| 173 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 174 | // Must call EnableTerminationOnOutOfMemory() because that is called from |
| 175 | // chrome's main function and therefore hasn't been called yet. |
| 176 | // Since this call may result in another thread being created and death |
| 177 | // tests shouldn't be started in a multithread environment, this call |
| 178 | // should be done inside of the ASSERT_DEATH. |
| 179 | base::EnableTerminationOnOutOfMemory(); |
| 180 | } |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 181 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 182 | #if BUILDFLAG(IS_MAC) |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 183 | void TearDown() override { |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 184 | allocator_shim::UninterceptMallocZonesForTesting(); |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 185 | } |
| 186 | #endif |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 187 | |
| 188 | // These tests don't work properly on old x86 Android; crbug.com/1181112 |
| 189 | bool ShouldSkipTest() { |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 190 | #if BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_X86) |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 191 | return base::android::BuildInfo::GetInstance()->sdk_int() < |
| 192 | base::android::SDK_VERSION_NOUGAT; |
Peter Kotwicz | 2ebc2f2 | 2021-08-18 19:21:44 | [diff] [blame] | 193 | #else |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 194 | return false; |
Peter Kotwicz | 2ebc2f2 | 2021-08-18 19:21:44 | [diff] [blame] | 195 | #endif |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 196 | } |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | TEST_F(OutOfMemoryDeathTest, New) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 200 | if (ShouldSkipTest()) { |
| 201 | return; |
| 202 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 203 | ASSERT_OOM_DEATH({ |
| 204 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 205 | [[maybe_unused]] void* volatile ptr = operator new(test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 206 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | TEST_F(OutOfMemoryDeathTest, NewArray) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 210 | if (ShouldSkipTest()) { |
| 211 | return; |
| 212 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 213 | ASSERT_OOM_DEATH({ |
| 214 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 215 | [[maybe_unused]] void* volatile ptr = new char[test_size_]; |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 216 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | TEST_F(OutOfMemoryDeathTest, Malloc) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 220 | if (ShouldSkipTest()) { |
| 221 | return; |
| 222 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 223 | ASSERT_OOM_DEATH({ |
| 224 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 225 | [[maybe_unused]] void* volatile ptr = malloc(test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 226 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | TEST_F(OutOfMemoryDeathTest, Realloc) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 230 | if (ShouldSkipTest()) { |
| 231 | return; |
| 232 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 233 | ASSERT_OOM_DEATH({ |
| 234 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 235 | [[maybe_unused]] void* volatile ptr = realloc(nullptr, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 236 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST_F(OutOfMemoryDeathTest, Calloc) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 240 | if (ShouldSkipTest()) { |
| 241 | return; |
| 242 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 243 | ASSERT_OOM_DEATH({ |
| 244 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 245 | [[maybe_unused]] void* volatile ptr = calloc(1024, test_size_ / 1024L); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 246 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 247 | } |
| 248 | |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 249 | TEST_F(OutOfMemoryDeathTest, AlignedAlloc) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 250 | if (ShouldSkipTest()) { |
| 251 | return; |
| 252 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 253 | ASSERT_OOM_DEATH({ |
| 254 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 255 | [[maybe_unused]] void* volatile ptr = base::AlignedAlloc(test_size_, 8); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 256 | }); |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // POSIX does not define an aligned realloc function. |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 260 | #if BUILDFLAG(IS_WIN) |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 261 | TEST_F(OutOfMemoryDeathTest, AlignedRealloc) { |
Torne (Richard Coles) | b8a9a66 | 2021-05-18 15:08:47 | [diff] [blame] | 262 | if (ShouldSkipTest()) { |
| 263 | return; |
| 264 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 265 | ASSERT_OOM_DEATH({ |
| 266 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 267 | [[maybe_unused]] void* volatile ptr = |
| 268 | _aligned_realloc(nullptr, test_size_, 8); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 269 | }); |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 270 | } |
Sigurdur Asgeirsson | eef087df | 2017-07-27 15:52:13 | [diff] [blame] | 271 | |
| 272 | namespace { |
| 273 | |
| 274 | constexpr uint32_t kUnhandledExceptionExitCode = 0xBADA55; |
| 275 | |
| 276 | // This unhandled exception filter exits the process with an exit code distinct |
| 277 | // from the exception code. This is to verify that the out of memory new handler |
| 278 | // causes an unhandled exception. |
| 279 | LONG WINAPI ExitingUnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo) { |
| 280 | _exit(kUnhandledExceptionExitCode); |
| 281 | } |
| 282 | |
| 283 | } // namespace |
| 284 | |
| 285 | TEST_F(OutOfMemoryDeathTest, NewHandlerGeneratesUnhandledException) { |
| 286 | ASSERT_EXIT( |
| 287 | { |
| 288 | SetUpInDeathAssert(); |
| 289 | SetUnhandledExceptionFilter(&ExitingUnhandledExceptionFilter); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 290 | [[maybe_unused]] void* volatile ptr = new char[test_size_]; |
Sigurdur Asgeirsson | eef087df | 2017-07-27 15:52:13 | [diff] [blame] | 291 | }, |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 292 | testing::ExitedWithCode(kUnhandledExceptionExitCode), ""); |
Sigurdur Asgeirsson | eef087df | 2017-07-27 15:52:13 | [diff] [blame] | 293 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 294 | #endif // BUILDFLAG(IS_WIN) |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 295 | |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 296 | // OS X has no 2Gb allocation limit. |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 297 | // See https://crbug.com/169327. |
Adrian Taylor | 95654d4b | 2023-03-08 12:54:18 | [diff] [blame] | 298 | // PartitionAlloc is not active in component builds, so cannot enforce |
| 299 | // this limit. (//BUILD.gn asserts that we cannot have an official component |
| 300 | // build.) |
| 301 | #if !BUILDFLAG(IS_MAC) && !defined(COMPONENT_BUILD) |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 302 | TEST_F(OutOfMemoryDeathTest, SecurityNew) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 303 | if (ShouldSkipTest()) { |
| 304 | return; |
| 305 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 306 | ASSERT_OOM_DEATH({ |
| 307 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 308 | [[maybe_unused]] void* volatile ptr = operator new(insecure_test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 309 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | TEST_F(OutOfMemoryDeathTest, SecurityNewArray) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 313 | if (ShouldSkipTest()) { |
| 314 | return; |
| 315 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 316 | ASSERT_OOM_DEATH({ |
| 317 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 318 | [[maybe_unused]] void* volatile ptr = new char[insecure_test_size_]; |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 319 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | TEST_F(OutOfMemoryDeathTest, SecurityMalloc) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 323 | if (ShouldSkipTest()) { |
| 324 | return; |
| 325 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 326 | ASSERT_OOM_DEATH({ |
| 327 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 328 | [[maybe_unused]] void* volatile ptr = malloc(insecure_test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 329 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST_F(OutOfMemoryDeathTest, SecurityRealloc) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 333 | if (ShouldSkipTest()) { |
| 334 | return; |
| 335 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 336 | ASSERT_OOM_DEATH({ |
| 337 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 338 | [[maybe_unused]] void* volatile ptr = realloc(nullptr, insecure_test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 339 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST_F(OutOfMemoryDeathTest, SecurityCalloc) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 343 | if (ShouldSkipTest()) { |
| 344 | return; |
| 345 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 346 | ASSERT_OOM_DEATH({ |
| 347 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 348 | [[maybe_unused]] void* volatile ptr = |
| 349 | calloc(1024, insecure_test_size_ / 1024L); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 350 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 351 | } |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 352 | |
| 353 | TEST_F(OutOfMemoryDeathTest, SecurityAlignedAlloc) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 354 | if (ShouldSkipTest()) { |
| 355 | return; |
| 356 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 357 | ASSERT_OOM_DEATH({ |
| 358 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 359 | [[maybe_unused]] void* volatile ptr = |
| 360 | base::AlignedAlloc(insecure_test_size_, 8); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 361 | }); |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // POSIX does not define an aligned realloc function. |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 365 | #if BUILDFLAG(IS_WIN) |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 366 | TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) { |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 367 | if (ShouldSkipTest()) { |
| 368 | return; |
| 369 | } |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 370 | ASSERT_OOM_DEATH({ |
| 371 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 372 | [[maybe_unused]] void* volatile ptr = |
| 373 | _aligned_realloc(nullptr, insecure_test_size_, 8); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 374 | }); |
wfh | 08a9665 | 2016-04-01 22:36:30 | [diff] [blame] | 375 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 376 | #endif // BUILDFLAG(IS_WIN) |
Bartek Nowierski | 96e5b4a2 | 2022-07-30 01:39:28 | [diff] [blame] | 377 | #endif // !BUILDFLAG(IS_MAC) |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 378 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 379 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 380 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 381 | TEST_F(OutOfMemoryDeathTest, Valloc) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 382 | ASSERT_OOM_DEATH({ |
| 383 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 384 | [[maybe_unused]] void* volatile ptr = valloc(test_size_); |
| 385 | EXPECT_TRUE(ptr); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 386 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 387 | } |
| 388 | |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 389 | TEST_F(OutOfMemoryDeathTest, SecurityValloc) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 390 | ASSERT_OOM_DEATH({ |
| 391 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 392 | [[maybe_unused]] void* volatile ptr = valloc(insecure_test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 393 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 394 | } |
[email protected] | 6bad17e | 2014-03-04 04:54:26 | [diff] [blame] | 395 | |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 396 | TEST_F(OutOfMemoryDeathTest, Pvalloc) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 397 | ASSERT_OOM_DEATH({ |
| 398 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 399 | [[maybe_unused]] void* volatile ptr = pvalloc(test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 400 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 401 | } |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 402 | |
| 403 | TEST_F(OutOfMemoryDeathTest, SecurityPvalloc) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 404 | ASSERT_OOM_DEATH({ |
| 405 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 406 | [[maybe_unused]] void* volatile ptr = pvalloc(insecure_test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 407 | }); |
wfh | 1bf9399 | 2015-09-21 02:21:14 | [diff] [blame] | 408 | } |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 409 | |
| 410 | TEST_F(OutOfMemoryDeathTest, Memalign) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 411 | ASSERT_OOM_DEATH({ |
| 412 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 413 | [[maybe_unused]] void* volatile ptr = memalign(4, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 414 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) { |
[email protected] | 6ec70cc7 | 2013-11-20 05:33:46 | [diff] [blame] | 418 | // This tests that the run-time symbol resolution is overriding malloc for |
icoolidge | 9a4f22d | 2015-04-07 19:28:09 | [diff] [blame] | 419 | // shared libraries as well as for our code. |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 420 | ASSERT_OOM_DEATH({ |
icoolidge | 9a4f22d | 2015-04-07 19:28:09 | [diff] [blame] | 421 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 422 | [[maybe_unused]] void* volatile ptr = MallocWrapper(test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 423 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 424 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 425 | #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 426 | |
| 427 | // Android doesn't implement posix_memalign(). |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 428 | #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 429 | TEST_F(OutOfMemoryDeathTest, Posix_memalign) { |
| 430 | // Grab the return value of posix_memalign to silence a compiler warning |
| 431 | // about unused return values. We don't actually care about the return |
| 432 | // value, since we're asserting death. |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 433 | ASSERT_OOM_DEATH({ |
| 434 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 435 | void* ptr; |
| 436 | EXPECT_EQ(ENOMEM, posix_memalign(&ptr, 8, test_size_)); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 437 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 438 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 439 | #endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 440 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 441 | #if BUILDFLAG(IS_MAC) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 442 | |
| 443 | // Purgeable zone tests |
| 444 | |
| 445 | TEST_F(OutOfMemoryDeathTest, MallocPurgeable) { |
| 446 | malloc_zone_t* zone = malloc_default_purgeable_zone(); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 447 | ASSERT_OOM_DEATH({ |
| 448 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 449 | [[maybe_unused]] void* volatile ptr = malloc_zone_malloc(zone, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 450 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) { |
| 454 | malloc_zone_t* zone = malloc_default_purgeable_zone(); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 455 | ASSERT_OOM_DEATH({ |
| 456 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 457 | [[maybe_unused]] void* volatile ptr = |
| 458 | malloc_zone_realloc(zone, nullptr, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 459 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST_F(OutOfMemoryDeathTest, CallocPurgeable) { |
| 463 | malloc_zone_t* zone = malloc_default_purgeable_zone(); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 464 | ASSERT_OOM_DEATH({ |
| 465 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 466 | [[maybe_unused]] void* volatile ptr = |
| 467 | malloc_zone_calloc(zone, 1024, test_size_ / 1024L); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 468 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | TEST_F(OutOfMemoryDeathTest, VallocPurgeable) { |
| 472 | malloc_zone_t* zone = malloc_default_purgeable_zone(); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 473 | ASSERT_OOM_DEATH({ |
| 474 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 475 | [[maybe_unused]] void* volatile ptr = malloc_zone_valloc(zone, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 476 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) { |
| 480 | malloc_zone_t* zone = malloc_default_purgeable_zone(); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 481 | ASSERT_OOM_DEATH({ |
| 482 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 483 | [[maybe_unused]] void* volatile ptr = |
| 484 | malloc_zone_memalign(zone, 8, test_size_); |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 485 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Since these allocation functions take a signed size, it's possible that |
| 489 | // calling them just once won't be enough to exhaust memory. In the 32-bit |
| 490 | // environment, it's likely that these allocation attempts will fail because |
| 491 | // not enough contiguous address space is available. In the 64-bit environment, |
| 492 | // it's likely that they'll fail because they would require a preposterous |
| 493 | // amount of (virtual) memory. |
| 494 | |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 495 | TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) { |
| 496 | ASSERT_OOM_DEATH({ |
| 497 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 498 | [[maybe_unused]] void* ptr; |
| 499 | while ((ptr = base::AllocateViaCFAllocatorMalloc(signed_test_size_))) { |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 500 | } |
| 501 | }); |
| 502 | } |
| 503 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 504 | #if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 505 | // PartitionAlloc-Everywhere does not intercept other malloc zones than the |
Avi Drissman | 70d05a4 | 2025-05-29 15:19:23 | [diff] [blame] | 506 | // default (the top) malloc zone. |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 507 | #define MAYBE_CFAllocatorSystemDefault DISABLED_CFAllocatorSystemDefault |
| 508 | #else |
| 509 | #define MAYBE_CFAllocatorSystemDefault CFAllocatorSystemDefault |
| 510 | #endif |
| 511 | TEST_F(OutOfMemoryDeathTest, MAYBE_CFAllocatorSystemDefault) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 512 | ASSERT_OOM_DEATH({ |
| 513 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 514 | [[maybe_unused]] void* ptr; |
| 515 | while ( |
| 516 | (ptr = base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 517 | } |
| 518 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 519 | } |
| 520 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 521 | #if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 522 | // PartitionAlloc-Everywhere does not intercept other malloc zones than the |
Avi Drissman | 70d05a4 | 2025-05-29 15:19:23 | [diff] [blame] | 523 | // default (the top) malloc zone. |
Yuki Shiino | 6bca5c6b | 2021-11-19 07:38:05 | [diff] [blame] | 524 | #define MAYBE_CFAllocatorMallocZone DISABLED_CFAllocatorMallocZone |
| 525 | #else |
| 526 | #define MAYBE_CFAllocatorMallocZone CFAllocatorMallocZone |
| 527 | #endif |
| 528 | TEST_F(OutOfMemoryDeathTest, MAYBE_CFAllocatorMallocZone) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 529 | ASSERT_OOM_DEATH({ |
| 530 | SetUpInDeathAssert(); |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 531 | [[maybe_unused]] void* ptr; |
| 532 | while ((ptr = base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 533 | } |
| 534 | }); |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 535 | } |
| 536 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 537 | #endif // BUILDFLAG(IS_MAC) |
[email protected] | bac98410 | 2013-06-28 17:40:24 | [diff] [blame] | 538 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 539 | class OutOfMemoryHandledTest : public OutOfMemoryTest { |
| 540 | public: |
| 541 | static const size_t kSafeMallocSize = 512; |
| 542 | static const size_t kSafeCallocSize = 128; |
| 543 | static const size_t kSafeCallocItems = 4; |
| 544 | |
dcheng | 8aef3761 | 2014-12-23 02:56:47 | [diff] [blame] | 545 | void SetUp() override { |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 546 | OutOfMemoryTest::SetUp(); |
| 547 | |
| 548 | // We enable termination on OOM - just as Chrome does at early |
| 549 | // initialization - and test that UncheckedMalloc and UncheckedCalloc |
| 550 | // properly by-pass this in order to allow the caller to handle OOM. |
| 551 | base::EnableTerminationOnOutOfMemory(); |
| 552 | } |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 553 | |
| 554 | void TearDown() override { |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 555 | #if BUILDFLAG(IS_MAC) |
Yuki Shiino | 2ff8131 | 2022-09-05 13:11:55 | [diff] [blame] | 556 | allocator_shim::UninterceptMallocZonesForTesting(); |
erikchen | 2f9bf05 | 2017-03-28 01:11:26 | [diff] [blame] | 557 | #endif |
| 558 | } |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 559 | }; |
| 560 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 561 | #if BUILDFLAG(IS_WIN) |
siggi | 7a7fbf7c5 | 2017-02-06 20:45:04 | [diff] [blame] | 562 | |
| 563 | namespace { |
| 564 | |
| 565 | DWORD HandleOutOfMemoryException(EXCEPTION_POINTERS* exception_ptrs, |
| 566 | size_t expected_size) { |
| 567 | EXPECT_EQ(base::win::kOomExceptionCode, |
| 568 | exception_ptrs->ExceptionRecord->ExceptionCode); |
| 569 | EXPECT_LE(1U, exception_ptrs->ExceptionRecord->NumberParameters); |
| 570 | EXPECT_EQ(expected_size, |
| 571 | exception_ptrs->ExceptionRecord->ExceptionInformation[0]); |
| 572 | return EXCEPTION_EXECUTE_HANDLER; |
| 573 | } |
| 574 | |
| 575 | } // namespace |
| 576 | |
| 577 | TEST_F(OutOfMemoryTest, TerminateBecauseOutOfMemoryReportsAllocSize) { |
| 578 | // On Windows, TerminateBecauseOutOfMemory reports the attempted allocation |
| 579 | // size in the exception raised. |
| 580 | #if defined(ARCH_CPU_64_BITS) |
| 581 | // Test with a size larger than 32 bits on 64 bit machines. |
| 582 | const size_t kAttemptedAllocationSize = 0xBADA55F00DULL; |
| 583 | #else |
| 584 | const size_t kAttemptedAllocationSize = 0xBADA55; |
| 585 | #endif |
| 586 | |
| 587 | __try { |
| 588 | base::TerminateBecauseOutOfMemory(kAttemptedAllocationSize); |
| 589 | } __except (HandleOutOfMemoryException(GetExceptionInformation(), |
| 590 | kAttemptedAllocationSize)) { |
| 591 | } |
| 592 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 593 | #endif // BUILDFLAG(IS_WIN) |
siggi | 7a7fbf7c5 | 2017-02-06 20:45:04 | [diff] [blame] | 594 | |
Sean McAllister | 39b8d34 | 2020-08-25 09:08:32 | [diff] [blame] | 595 | #if defined(ARCH_CPU_32_BITS) && \ |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 596 | (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 597 | |
| 598 | void TestAllocationsReleaseReservation(void* (*alloc_fn)(size_t), |
| 599 | void (*free_fn)(void*)) { |
Kalvin Lee | cb9c00c2 | 2022-07-12 05:18:35 | [diff] [blame] | 600 | partition_alloc::ReleaseReservation(); |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 601 | base::EnableTerminationOnOutOfMemory(); |
| 602 | |
| 603 | constexpr size_t kMiB = 1 << 20; |
| 604 | constexpr size_t kReservationSize = 512 * kMiB; // MiB. |
| 605 | |
| 606 | size_t reservation_size = kReservationSize; |
Kalvin Lee | cb9c00c2 | 2022-07-12 05:18:35 | [diff] [blame] | 607 | while (!partition_alloc::ReserveAddressSpace(reservation_size)) { |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 608 | reservation_size -= 16 * kMiB; |
| 609 | } |
Kalvin Lee | cb9c00c2 | 2022-07-12 05:18:35 | [diff] [blame] | 610 | ASSERT_TRUE(partition_alloc::HasReservationForTesting()); |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 611 | ASSERT_GT(reservation_size, 0u); |
| 612 | |
| 613 | // Allocate a large area at a time to bump into address space exhaustion |
| 614 | // before other limits. It is important not to do a larger allocation, to |
| 615 | // verify that we can allocate without removing the reservation. On the other |
| 616 | // hand, must be large enough to make the underlying implementation call |
| 617 | // mmap()/VirtualAlloc(). |
| 618 | size_t allocation_size = reservation_size / 2; |
| 619 | |
| 620 | std::vector<void*> areas; |
| 621 | // Pre-reserve the vector to make sure that we don't hit the address space |
| 622 | // limit while resizing the array. |
| 623 | areas.reserve(((2 * 4096 * kMiB) / allocation_size) + 1); |
| 624 | |
| 625 | while (true) { |
| 626 | void* area = alloc_fn(allocation_size / 2); |
| 627 | ASSERT_TRUE(area); |
| 628 | areas.push_back(area); |
| 629 | |
| 630 | // Working as intended, the allocation was successful, and the reservation |
| 631 | // was dropped instead of crashing. |
| 632 | // |
| 633 | // Meaning that the test is either successful, or crashes. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 634 | if (!partition_alloc::HasReservationForTesting()) { |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 635 | break; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 636 | } |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | EXPECT_GE(areas.size(), 2u) |
| 640 | << "Should be able to allocate without releasing the reservation"; |
| 641 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 642 | for (void* ptr : areas) { |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 643 | free_fn(ptr); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 644 | } |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | TEST_F(OutOfMemoryHandledTest, MallocReleasesReservation) { |
| 648 | TestAllocationsReleaseReservation(malloc, free); |
| 649 | } |
| 650 | |
| 651 | TEST_F(OutOfMemoryHandledTest, NewReleasesReservation) { |
| 652 | TestAllocationsReleaseReservation( |
| 653 | [](size_t size) { return static_cast<void*>(new char[size]); }, |
| 654 | [](void* ptr) { delete[] static_cast<char*>(ptr); }); |
| 655 | } |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 656 | #endif // defined(ARCH_CPU_32_BITS) && (BUILDFLAG(IS_WIN) || |
| 657 | // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) |
Benoît Lizé | 70f64a0 | 2020-01-15 00:33:13 | [diff] [blame] | 658 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 659 | #if BUILDFLAG(IS_ANDROID) |
Kevin Marshall | 7a835b4 | 2022-01-08 23:17:11 | [diff] [blame] | 660 | |
| 661 | // Android's allocator does not allow overcommits, so very large |
| 662 | // UncheckedMallocs will yield OOM errors. |
Alison Gale | 47d1537d | 2024-04-19 21:31:46 | [diff] [blame] | 663 | // TODO(crbug.com/40143202): Fails on some Android bots. |
Benoit Lize | ced56e1 | 2020-08-06 09:14:48 | [diff] [blame] | 664 | #define MAYBE_UncheckedMallocDies DISABLED_UncheckedMallocDies |
| 665 | #define MAYBE_UncheckedCallocDies DISABLED_UncheckedCallocDies |
Benoit Lize | ced56e1 | 2020-08-06 09:14:48 | [diff] [blame] | 666 | TEST_F(OutOfMemoryDeathTest, MAYBE_UncheckedMallocDies) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 667 | ASSERT_OOM_DEATH({ |
| 668 | SetUpInDeathAssert(); |
| 669 | void* data; |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 670 | std::ignore = base::UncheckedMalloc(test_size_, &data); |
Kevin Marshall | 64a82a9 | 2021-09-08 20:49:45 | [diff] [blame] | 671 | // Death expected here. |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 672 | }); |
Benoit Lize | 2b7a746 | 2020-08-03 09:20:53 | [diff] [blame] | 673 | } |
| 674 | |
Benoit Lize | ced56e1 | 2020-08-06 09:14:48 | [diff] [blame] | 675 | TEST_F(OutOfMemoryDeathTest, MAYBE_UncheckedCallocDies) { |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 676 | ASSERT_OOM_DEATH({ |
| 677 | SetUpInDeathAssert(); |
| 678 | void* data; |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 679 | std::ignore = base::UncheckedCalloc(1, test_size_, &data); |
Kevin Marshall | 64a82a9 | 2021-09-08 20:49:45 | [diff] [blame] | 680 | // Death expected here. |
Benoit Lize | 7a05205 | 2020-08-03 12:48:46 | [diff] [blame] | 681 | }); |
Benoit Lize | 2b7a746 | 2020-08-03 09:20:53 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | #else |
| 685 | |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 686 | TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 687 | void* ptr; |
| 688 | EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &ptr)); |
| 689 | EXPECT_TRUE(ptr != nullptr); |
| 690 | base::UncheckedFree(ptr); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 691 | |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 692 | EXPECT_FALSE(base::UncheckedMalloc(test_size_, &ptr)); |
| 693 | EXPECT_TRUE(ptr == nullptr); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 697 | void* ptr; |
| 698 | EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &ptr)); |
| 699 | EXPECT_TRUE(ptr != nullptr); |
| 700 | const char* bytes = static_cast<const char*>(ptr); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 701 | for (size_t i = 0; i < kSafeMallocSize; ++i) { |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 702 | EXPECT_EQ(0, bytes[i]); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 703 | } |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 704 | base::UncheckedFree(ptr); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 705 | |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 706 | EXPECT_TRUE(base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &ptr)); |
| 707 | EXPECT_TRUE(ptr != nullptr); |
| 708 | bytes = static_cast<const char*>(ptr); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 709 | for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) { |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 710 | EXPECT_EQ(0, bytes[i]); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 711 | } |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 712 | base::UncheckedFree(ptr); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 713 | |
Bartek Nowierski | 5f04c350 | 2024-01-30 06:12:48 | [diff] [blame] | 714 | EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &ptr)); |
| 715 | EXPECT_TRUE(ptr == nullptr); |
[email protected] | 29159eb | 2014-03-21 22:07:03 | [diff] [blame] | 716 | } |
Benoit Lize | 2b7a746 | 2020-08-03 09:20:53 | [diff] [blame] | 717 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 718 | #endif // BUILDFLAG(IS_ANDROID) |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 719 | #endif // !BUILDFLAG(IS_OPENBSD) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM) && |
primiano | 227dbd3 | 2016-08-03 16:31:03 | [diff] [blame] | 720 | // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
Benoit Lize | e21b590 | 2022-11-08 17:36:55 | [diff] [blame] | 721 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 722 | #if BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
Benoit Lize | e21b590 | 2022-11-08 17:36:55 | [diff] [blame] | 723 | |
| 724 | // Not a proper test because it needs to be in a static initializer, see the |
| 725 | // comment in UncheckedMalloc() in memory_mac.mm. |
| 726 | // |
| 727 | // The "test" passes if the binary doesn't crash. |
Sorin Jianu | ad4cc623 | 2024-10-08 19:06:01 | [diff] [blame] | 728 | size_t need_a_static_initializer = [] { |
Benoit Lize | e21b590 | 2022-11-08 17:36:55 | [diff] [blame] | 729 | void* ptr; |
| 730 | constexpr size_t kRequestedSize = 1000u; |
| 731 | bool ok = base::UncheckedMalloc(kRequestedSize, &ptr); |
| 732 | CHECK(ok); |
| 733 | size_t actual_size = malloc_size(ptr); |
| 734 | // If no known zone owns the pointer, dispatching code in libmalloc returns 0. |
| 735 | CHECK_GE(actual_size, kRequestedSize); |
| 736 | // If no zone owns the pointer, libmalloc aborts here. |
| 737 | free(ptr); |
| 738 | |
| 739 | return actual_size; |
| 740 | }(); |
| 741 | |
Arthur Sonzogni | 62e877a | 2024-04-30 16:09:43 | [diff] [blame] | 742 | #endif // BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |