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