blob: 136f8cbdcb0fb4807857bce70e3ac057d1fce56d [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]bac984102013-06-28 17:40:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
danakj51d26a42024-04-25 14:23:565#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]bac984102013-06-28 17:40:2410#define _CRT_SECURE_NO_WARNINGS
11
12#include "base/process/memory.h"
13
avibeced7c2015-12-24 06:47:5914#include <stddef.h>
15
[email protected]bac984102013-06-28 17:40:2416#include <limits>
Avi Drissman933398e2022-01-22 00:55:4217#include <tuple>
Benoît Lizé70f64a02020-01-15 00:33:1318#include <vector>
[email protected]bac984102013-06-28 17:40:2419
wfh08a96652016-04-01 22:36:3020#include "base/allocator/allocator_check.h"
[email protected]bac984102013-06-28 17:40:2421#include "base/compiler_specific.h"
22#include "base/debug/alias.h"
wfh08a96652016-04-01 22:36:3023#include "base/memory/aligned_memory.h"
Lei Zhang425893e2021-05-14 23:28:2524#include "base/memory/page_size.h"
avibeced7c2015-12-24 06:47:5925#include "build/build_config.h"
Arthur Sonzognifd39d612024-06-26 08:16:2326#include "partition_alloc/buildflags.h"
Yuki Shiino985ab91e2024-03-14 07:20:4627#include "partition_alloc/page_allocator.h"
[email protected]bac984102013-06-28 17:40:2428#include "testing/gtest/include/gtest/gtest.h"
29
Xiaohan Wang37e81612022-01-15 18:27:0030#if BUILDFLAG(IS_WIN)
[email protected]bac984102013-06-28 17:40:2431#include <windows.h>
32#endif
Xiaohan Wang37e81612022-01-15 18:27:0033#if BUILDFLAG(IS_POSIX)
[email protected]bac984102013-06-28 17:40:2434#include <errno.h>
35#endif
Xiaohan Wang37e81612022-01-15 18:27:0036#if BUILDFLAG(IS_MAC)
[email protected]bac984102013-06-28 17:40:2437#include <malloc/malloc.h>
Peter Kasting134ef9af2024-12-28 02:30:0938
Yuki Shiino8d478bd2023-06-08 09:43:5539#include "base/check_op.h"
[email protected]bac984102013-06-28 17:40:2440#include "base/process/memory_unittest_mac.h"
Yuki Shiino985ab91e2024-03-14 07:20:4641#include "partition_alloc/shim/allocator_interception_apple.h"
42#include "partition_alloc/shim/allocator_shim.h"
[email protected]bac984102013-06-28 17:40:2443#endif
Xiaohan Wang37e81612022-01-15 18:27:0044#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]bac984102013-06-28 17:40:2445#include <malloc.h>
Peter Kasting134ef9af2024-12-28 02:30:0946
icoolidge9a4f22d2015-04-07 19:28:0947#include "base/test/malloc_wrapper.h"
[email protected]bac984102013-06-28 17:40:2448#endif
Xiaohan Wang37e81612022-01-15 18:27:0049#if BUILDFLAG(IS_ANDROID)
Torne (Richard Coles)b8a9a662021-05-18 15:08:4750#include "base/android/build_info.h"
51#endif
[email protected]bac984102013-06-28 17:40:2452
Xiaohan Wang37e81612022-01-15 18:27:0053#if BUILDFLAG(IS_WIN)
wfh1bf93992015-09-21 02:21:1454
Thiago Farinaba7d2a422017-06-17 00:18:4955#if defined(COMPILER_MSVC)
wfh1bf93992015-09-21 02:21:1456// ssize_t needed for OutOfMemoryTest.
57#if defined(_WIN64)
58typedef __int64 ssize_t;
59#else
60typedef long ssize_t;
61#endif
62#endif
63
[email protected]bac984102013-06-28 17:40:2464// HeapQueryInformation function pointer.
Peter Kasting134ef9af2024-12-28 02:30:0965typedef BOOL(WINAPI* HeapQueryFn)(HANDLE,
66 HEAP_INFORMATION_CLASS,
67 PVOID,
68 SIZE_T,
69 PSIZE_T);
[email protected]bac984102013-06-28 17:40:2470
Xiaohan Wang37e81612022-01-15 18:27:0071#endif // BUILDFLAG(IS_WIN)
[email protected]bac984102013-06-28 17:40:2472
Xiaohan Wang37e81612022-01-15 18:27:0073#if BUILDFLAG(IS_MAC)
[email protected]bac984102013-06-28 17:40:2474
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 Wennborgc90800e2021-01-21 14:39:3480// Wrap free() in a function to thwart Clang's -Wfree-nonheap-object warning.
Peter Kasting134ef9af2024-12-28 02:30:0981static void callFree(void* ptr) {
Hans Wennborgc90800e2021-01-21 14:39:3482 free(ptr);
83}
84
[email protected]bac984102013-06-28 17:40:2485TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) {
Arthur Sonzogni62e877a2024-04-30 16:09:4386#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
Yuki Shiino2ff81312022-09-05 13:11:5587 allocator_shim::InitializeAllocatorShim();
erikchenbf11b55a2017-02-10 01:34:4888#endif
[email protected]bac984102013-06-28 17:40:2489 // Assert that freeing an unallocated pointer will crash the process.
[email protected]de8b1ad2013-07-22 21:46:5090 char buf[9];
Nico Weber57048a9c2019-08-29 18:24:4891 asm("" : "=m"(buf)); // Prevent clang from being too smart.
[email protected]bac984102013-06-28 17:40:2492#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 Wennborgc90800e2021-01-21 14:39:3495 ASSERT_DEATH(callFree(buf), "");
[email protected]bac984102013-06-28 17:40:2496#elif defined(ADDRESS_SANITIZER)
97 // AddressSanitizer replaces malloc() and prints a different error message on
98 // heap corruption.
Peter Kasting134ef9af2024-12-28 02:30:0999 ASSERT_DEATH(callFree(buf),
100 "attempting free on address which "
101 "was not malloc\\(\\)-ed");
[email protected]bac984102013-06-28 17:40:24102#else
rsesek931d092b2015-01-29 17:21:52103 ADD_FAILURE() << "This test is not supported in this build configuration.";
104#endif
erikchen2f9bf052017-03-28 01:11:26105
Arthur Sonzogni62e877a2024-04-30 16:09:43106#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
Yuki Shiino2ff81312022-09-05 13:11:55107 allocator_shim::UninterceptMallocZonesForTesting();
erikchen2f9bf052017-03-28 01:11:26108#endif
[email protected]bac984102013-06-28 17:40:24109}
110
Xiaohan Wang37e81612022-01-15 18:27:00111#endif // BUILDFLAG(IS_MAC)
[email protected]bac984102013-06-28 17:40:24112
Arthur Sonzogni62e877a2024-04-30 16:09:43113#if PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
wfh08a96652016-04-01 22:36:30114TEST(MemoryTest, AllocatorShimWorking) {
Xiaohan Wang37e81612022-01-15 18:27:00115#if BUILDFLAG(IS_MAC)
Yuki Shiino2ff81312022-09-05 13:11:55116 allocator_shim::InitializeAllocatorShim();
Yuki Shiino2ff81312022-09-05 13:11:55117 allocator_shim::InterceptAllocationsMac();
erikchenbf11b55a2017-02-10 01:34:48118#endif
wfh08a96652016-04-01 22:36:30119 ASSERT_TRUE(base::allocator::IsAllocatorInitialized());
erikchen2f9bf052017-03-28 01:11:26120
Xiaohan Wang37e81612022-01-15 18:27:00121#if BUILDFLAG(IS_MAC)
Yuki Shiino2ff81312022-09-05 13:11:55122 allocator_shim::UninterceptMallocZonesForTesting();
erikchen2f9bf052017-03-28 01:11:26123#endif
wfh08a96652016-04-01 22:36:30124}
Arthur Sonzogni62e877a2024-04-30 16:09:43125#endif // PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
wfh08a96652016-04-01 22:36:30126
primiano227dbd32016-08-03 16:31:03127// OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan
128// configurations: only test the real allocator.
Arthur Sonzogni62e877a2024-04-30 16:09:43129#if !BUILDFLAG(IS_OPENBSD) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM) && \
gliderf9857092015-04-21 12:58:25130 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
[email protected]bac984102013-06-28 17:40:24131
icoolidge85e13092015-04-20 18:47:12132namespace {
Bartek Nowierski96e5b4a22022-07-30 01:39:28133
Xiaohan Wang37e81612022-01-15 18:27:00134#if BUILDFLAG(IS_WIN)
Benoit Lize7a052052020-08-03 12:48:46135
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
wfh8ca194a2016-07-20 02:06:54141#else
Benoit Lize7a052052020-08-03 12:48:46142
143#define ASSERT_OOM_DEATH(statement) ASSERT_DEATH(statement, "")
144
Xiaohan Wang37e81612022-01-15 18:27:00145#endif // BUILDFLAG(IS_WIN)
Benoit Lize7a052052020-08-03 12:48:46146
icoolidge85e13092015-04-20 18:47:12147} // namespace
148
[email protected]29159eb2014-03-21 22:07:03149class OutOfMemoryTest : public testing::Test {
[email protected]bac984102013-06-28 17:40:24150 public:
[email protected]29159eb2014-03-21 22:07:03151 OutOfMemoryTest()
Bartek Nowierski5f04c3502024-01-30 06:12:48152 : // Make test size as large as possible minus a few pages so that
153 // alignment or other rounding doesn't make it wrap.
Avi Drissman5c8fd42b2020-08-26 17:09:25154 test_size_(std::numeric_limits<std::size_t>::max() -
155 3 * base::GetPageSize()),
Ivan Kotenkova16212a52017-11-08 12:37:33156 // 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]bac984102013-06-28 17:40:24160
[email protected]29159eb2014-03-21 22:07:03161 protected:
[email protected]29159eb2014-03-21 22:07:03162 size_t test_size_;
wfh1bf93992015-09-21 02:21:14163 size_t insecure_test_size_;
[email protected]29159eb2014-03-21 22:07:03164 ssize_t signed_test_size_;
165};
166
167class OutOfMemoryDeathTest : public OutOfMemoryTest {
168 public:
[email protected]bac984102013-06-28 17:40:24169 void SetUpInDeathAssert() {
Arthur Sonzogni62e877a2024-04-30 16:09:43170#if BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM)
Yuki Shiino2ff81312022-09-05 13:11:55171 allocator_shim::InitializeAllocatorShim();
erikchenbf11b55a2017-02-10 01:34:48172#endif
173
[email protected]bac984102013-06-28 17:40:24174 // 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 }
erikchen2f9bf052017-03-28 01:11:26181
Xiaohan Wang37e81612022-01-15 18:27:00182#if BUILDFLAG(IS_MAC)
erikchen2f9bf052017-03-28 01:11:26183 void TearDown() override {
Yuki Shiino2ff81312022-09-05 13:11:55184 allocator_shim::UninterceptMallocZonesForTesting();
erikchen2f9bf052017-03-28 01:11:26185 }
186#endif
Torne (Richard Coles)b8a9a662021-05-18 15:08:47187
188 // These tests don't work properly on old x86 Android; crbug.com/1181112
189 bool ShouldSkipTest() {
Xiaohan Wang37e81612022-01-15 18:27:00190#if BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_X86)
Torne (Richard Coles)b8a9a662021-05-18 15:08:47191 return base::android::BuildInfo::GetInstance()->sdk_int() <
192 base::android::SDK_VERSION_NOUGAT;
Peter Kotwicz2ebc2f22021-08-18 19:21:44193#else
Torne (Richard Coles)b8a9a662021-05-18 15:08:47194 return false;
Peter Kotwicz2ebc2f22021-08-18 19:21:44195#endif
Torne (Richard Coles)b8a9a662021-05-18 15:08:47196 }
[email protected]bac984102013-06-28 17:40:24197};
198
199TEST_F(OutOfMemoryDeathTest, New) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47200 if (ShouldSkipTest()) {
201 return;
202 }
Benoit Lize7a052052020-08-03 12:48:46203 ASSERT_OOM_DEATH({
204 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48205 [[maybe_unused]] void* volatile ptr = operator new(test_size_);
Benoit Lize7a052052020-08-03 12:48:46206 });
[email protected]bac984102013-06-28 17:40:24207}
208
209TEST_F(OutOfMemoryDeathTest, NewArray) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47210 if (ShouldSkipTest()) {
211 return;
212 }
Benoit Lize7a052052020-08-03 12:48:46213 ASSERT_OOM_DEATH({
214 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48215 [[maybe_unused]] void* volatile ptr = new char[test_size_];
Benoit Lize7a052052020-08-03 12:48:46216 });
[email protected]bac984102013-06-28 17:40:24217}
218
219TEST_F(OutOfMemoryDeathTest, Malloc) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47220 if (ShouldSkipTest()) {
221 return;
222 }
Benoit Lize7a052052020-08-03 12:48:46223 ASSERT_OOM_DEATH({
224 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48225 [[maybe_unused]] void* volatile ptr = malloc(test_size_);
Benoit Lize7a052052020-08-03 12:48:46226 });
[email protected]bac984102013-06-28 17:40:24227}
228
229TEST_F(OutOfMemoryDeathTest, Realloc) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47230 if (ShouldSkipTest()) {
231 return;
232 }
Benoit Lize7a052052020-08-03 12:48:46233 ASSERT_OOM_DEATH({
234 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48235 [[maybe_unused]] void* volatile ptr = realloc(nullptr, test_size_);
Benoit Lize7a052052020-08-03 12:48:46236 });
[email protected]bac984102013-06-28 17:40:24237}
238
239TEST_F(OutOfMemoryDeathTest, Calloc) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47240 if (ShouldSkipTest()) {
241 return;
242 }
Benoit Lize7a052052020-08-03 12:48:46243 ASSERT_OOM_DEATH({
244 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48245 [[maybe_unused]] void* volatile ptr = calloc(1024, test_size_ / 1024L);
Benoit Lize7a052052020-08-03 12:48:46246 });
[email protected]bac984102013-06-28 17:40:24247}
248
wfh08a96652016-04-01 22:36:30249TEST_F(OutOfMemoryDeathTest, AlignedAlloc) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47250 if (ShouldSkipTest()) {
251 return;
252 }
Benoit Lize7a052052020-08-03 12:48:46253 ASSERT_OOM_DEATH({
254 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48255 [[maybe_unused]] void* volatile ptr = base::AlignedAlloc(test_size_, 8);
Benoit Lize7a052052020-08-03 12:48:46256 });
wfh08a96652016-04-01 22:36:30257}
258
259// POSIX does not define an aligned realloc function.
Xiaohan Wang37e81612022-01-15 18:27:00260#if BUILDFLAG(IS_WIN)
wfh08a96652016-04-01 22:36:30261TEST_F(OutOfMemoryDeathTest, AlignedRealloc) {
Torne (Richard Coles)b8a9a662021-05-18 15:08:47262 if (ShouldSkipTest()) {
263 return;
264 }
Benoit Lize7a052052020-08-03 12:48:46265 ASSERT_OOM_DEATH({
266 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48267 [[maybe_unused]] void* volatile ptr =
268 _aligned_realloc(nullptr, test_size_, 8);
Benoit Lize7a052052020-08-03 12:48:46269 });
wfh08a96652016-04-01 22:36:30270}
Sigurdur Asgeirssoneef087df2017-07-27 15:52:13271
272namespace {
273
274constexpr 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.
279LONG WINAPI ExitingUnhandledExceptionFilter(EXCEPTION_POINTERS* ExceptionInfo) {
280 _exit(kUnhandledExceptionExitCode);
281}
282
283} // namespace
284
285TEST_F(OutOfMemoryDeathTest, NewHandlerGeneratesUnhandledException) {
286 ASSERT_EXIT(
287 {
288 SetUpInDeathAssert();
289 SetUnhandledExceptionFilter(&ExitingUnhandledExceptionFilter);
Bartek Nowierski5f04c3502024-01-30 06:12:48290 [[maybe_unused]] void* volatile ptr = new char[test_size_];
Sigurdur Asgeirssoneef087df2017-07-27 15:52:13291 },
Benoit Lize7a052052020-08-03 12:48:46292 testing::ExitedWithCode(kUnhandledExceptionExitCode), "");
Sigurdur Asgeirssoneef087df2017-07-27 15:52:13293}
Xiaohan Wang37e81612022-01-15 18:27:00294#endif // BUILDFLAG(IS_WIN)
wfh08a96652016-04-01 22:36:30295
Bartek Nowierski96e5b4a22022-07-30 01:39:28296// OS X has no 2Gb allocation limit.
wfh1bf93992015-09-21 02:21:14297// See https://crbug.com/169327.
Adrian Taylor95654d4b2023-03-08 12:54:18298// 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)
wfh1bf93992015-09-21 02:21:14302TEST_F(OutOfMemoryDeathTest, SecurityNew) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28303 if (ShouldSkipTest()) {
304 return;
305 }
Benoit Lize7a052052020-08-03 12:48:46306 ASSERT_OOM_DEATH({
307 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48308 [[maybe_unused]] void* volatile ptr = operator new(insecure_test_size_);
Benoit Lize7a052052020-08-03 12:48:46309 });
wfh1bf93992015-09-21 02:21:14310}
311
312TEST_F(OutOfMemoryDeathTest, SecurityNewArray) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28313 if (ShouldSkipTest()) {
314 return;
315 }
Benoit Lize7a052052020-08-03 12:48:46316 ASSERT_OOM_DEATH({
317 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48318 [[maybe_unused]] void* volatile ptr = new char[insecure_test_size_];
Benoit Lize7a052052020-08-03 12:48:46319 });
wfh1bf93992015-09-21 02:21:14320}
321
322TEST_F(OutOfMemoryDeathTest, SecurityMalloc) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28323 if (ShouldSkipTest()) {
324 return;
325 }
Benoit Lize7a052052020-08-03 12:48:46326 ASSERT_OOM_DEATH({
327 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48328 [[maybe_unused]] void* volatile ptr = malloc(insecure_test_size_);
Benoit Lize7a052052020-08-03 12:48:46329 });
wfh1bf93992015-09-21 02:21:14330}
331
332TEST_F(OutOfMemoryDeathTest, SecurityRealloc) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28333 if (ShouldSkipTest()) {
334 return;
335 }
Benoit Lize7a052052020-08-03 12:48:46336 ASSERT_OOM_DEATH({
337 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48338 [[maybe_unused]] void* volatile ptr = realloc(nullptr, insecure_test_size_);
Benoit Lize7a052052020-08-03 12:48:46339 });
wfh1bf93992015-09-21 02:21:14340}
341
342TEST_F(OutOfMemoryDeathTest, SecurityCalloc) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28343 if (ShouldSkipTest()) {
344 return;
345 }
Benoit Lize7a052052020-08-03 12:48:46346 ASSERT_OOM_DEATH({
347 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48348 [[maybe_unused]] void* volatile ptr =
349 calloc(1024, insecure_test_size_ / 1024L);
Benoit Lize7a052052020-08-03 12:48:46350 });
wfh1bf93992015-09-21 02:21:14351}
wfh08a96652016-04-01 22:36:30352
353TEST_F(OutOfMemoryDeathTest, SecurityAlignedAlloc) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28354 if (ShouldSkipTest()) {
355 return;
356 }
Benoit Lize7a052052020-08-03 12:48:46357 ASSERT_OOM_DEATH({
358 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48359 [[maybe_unused]] void* volatile ptr =
360 base::AlignedAlloc(insecure_test_size_, 8);
Benoit Lize7a052052020-08-03 12:48:46361 });
wfh08a96652016-04-01 22:36:30362}
363
364// POSIX does not define an aligned realloc function.
Xiaohan Wang37e81612022-01-15 18:27:00365#if BUILDFLAG(IS_WIN)
wfh08a96652016-04-01 22:36:30366TEST_F(OutOfMemoryDeathTest, SecurityAlignedRealloc) {
Bartek Nowierski96e5b4a22022-07-30 01:39:28367 if (ShouldSkipTest()) {
368 return;
369 }
Benoit Lize7a052052020-08-03 12:48:46370 ASSERT_OOM_DEATH({
371 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48372 [[maybe_unused]] void* volatile ptr =
373 _aligned_realloc(nullptr, insecure_test_size_, 8);
Benoit Lize7a052052020-08-03 12:48:46374 });
wfh08a96652016-04-01 22:36:30375}
Xiaohan Wang37e81612022-01-15 18:27:00376#endif // BUILDFLAG(IS_WIN)
Bartek Nowierski96e5b4a22022-07-30 01:39:28377#endif // !BUILDFLAG(IS_MAC)
wfh1bf93992015-09-21 02:21:14378
Xiaohan Wang37e81612022-01-15 18:27:00379#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
wfh1bf93992015-09-21 02:21:14380
[email protected]bac984102013-06-28 17:40:24381TEST_F(OutOfMemoryDeathTest, Valloc) {
Benoit Lize7a052052020-08-03 12:48:46382 ASSERT_OOM_DEATH({
383 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48384 [[maybe_unused]] void* volatile ptr = valloc(test_size_);
385 EXPECT_TRUE(ptr);
Benoit Lize7a052052020-08-03 12:48:46386 });
[email protected]bac984102013-06-28 17:40:24387}
388
wfh1bf93992015-09-21 02:21:14389TEST_F(OutOfMemoryDeathTest, SecurityValloc) {
Benoit Lize7a052052020-08-03 12:48:46390 ASSERT_OOM_DEATH({
391 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48392 [[maybe_unused]] void* volatile ptr = valloc(insecure_test_size_);
Benoit Lize7a052052020-08-03 12:48:46393 });
wfh1bf93992015-09-21 02:21:14394}
[email protected]6bad17e2014-03-04 04:54:26395
[email protected]bac984102013-06-28 17:40:24396TEST_F(OutOfMemoryDeathTest, Pvalloc) {
Benoit Lize7a052052020-08-03 12:48:46397 ASSERT_OOM_DEATH({
398 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48399 [[maybe_unused]] void* volatile ptr = pvalloc(test_size_);
Benoit Lize7a052052020-08-03 12:48:46400 });
[email protected]bac984102013-06-28 17:40:24401}
wfh1bf93992015-09-21 02:21:14402
403TEST_F(OutOfMemoryDeathTest, SecurityPvalloc) {
Benoit Lize7a052052020-08-03 12:48:46404 ASSERT_OOM_DEATH({
405 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48406 [[maybe_unused]] void* volatile ptr = pvalloc(insecure_test_size_);
Benoit Lize7a052052020-08-03 12:48:46407 });
wfh1bf93992015-09-21 02:21:14408}
[email protected]bac984102013-06-28 17:40:24409
410TEST_F(OutOfMemoryDeathTest, Memalign) {
Benoit Lize7a052052020-08-03 12:48:46411 ASSERT_OOM_DEATH({
412 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48413 [[maybe_unused]] void* volatile ptr = memalign(4, test_size_);
Benoit Lize7a052052020-08-03 12:48:46414 });
[email protected]bac984102013-06-28 17:40:24415}
416
417TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) {
[email protected]6ec70cc72013-11-20 05:33:46418 // This tests that the run-time symbol resolution is overriding malloc for
icoolidge9a4f22d2015-04-07 19:28:09419 // shared libraries as well as for our code.
Benoit Lize7a052052020-08-03 12:48:46420 ASSERT_OOM_DEATH({
icoolidge9a4f22d2015-04-07 19:28:09421 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48422 [[maybe_unused]] void* volatile ptr = MallocWrapper(test_size_);
Benoit Lize7a052052020-08-03 12:48:46423 });
[email protected]bac984102013-06-28 17:40:24424}
Xiaohan Wang37e81612022-01-15 18:27:00425#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]bac984102013-06-28 17:40:24426
427// Android doesn't implement posix_memalign().
Xiaohan Wang37e81612022-01-15 18:27:00428#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]bac984102013-06-28 17:40:24429TEST_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 Lize7a052052020-08-03 12:48:46433 ASSERT_OOM_DEATH({
434 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48435 void* ptr;
436 EXPECT_EQ(ENOMEM, posix_memalign(&ptr, 8, test_size_));
Benoit Lize7a052052020-08-03 12:48:46437 });
[email protected]bac984102013-06-28 17:40:24438}
Xiaohan Wang37e81612022-01-15 18:27:00439#endif // BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]bac984102013-06-28 17:40:24440
Xiaohan Wang37e81612022-01-15 18:27:00441#if BUILDFLAG(IS_MAC)
[email protected]bac984102013-06-28 17:40:24442
443// Purgeable zone tests
444
445TEST_F(OutOfMemoryDeathTest, MallocPurgeable) {
446 malloc_zone_t* zone = malloc_default_purgeable_zone();
Benoit Lize7a052052020-08-03 12:48:46447 ASSERT_OOM_DEATH({
448 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48449 [[maybe_unused]] void* volatile ptr = malloc_zone_malloc(zone, test_size_);
Benoit Lize7a052052020-08-03 12:48:46450 });
[email protected]bac984102013-06-28 17:40:24451}
452
453TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) {
454 malloc_zone_t* zone = malloc_default_purgeable_zone();
Benoit Lize7a052052020-08-03 12:48:46455 ASSERT_OOM_DEATH({
456 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48457 [[maybe_unused]] void* volatile ptr =
458 malloc_zone_realloc(zone, nullptr, test_size_);
Benoit Lize7a052052020-08-03 12:48:46459 });
[email protected]bac984102013-06-28 17:40:24460}
461
462TEST_F(OutOfMemoryDeathTest, CallocPurgeable) {
463 malloc_zone_t* zone = malloc_default_purgeable_zone();
Benoit Lize7a052052020-08-03 12:48:46464 ASSERT_OOM_DEATH({
465 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48466 [[maybe_unused]] void* volatile ptr =
467 malloc_zone_calloc(zone, 1024, test_size_ / 1024L);
Benoit Lize7a052052020-08-03 12:48:46468 });
[email protected]bac984102013-06-28 17:40:24469}
470
471TEST_F(OutOfMemoryDeathTest, VallocPurgeable) {
472 malloc_zone_t* zone = malloc_default_purgeable_zone();
Benoit Lize7a052052020-08-03 12:48:46473 ASSERT_OOM_DEATH({
474 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48475 [[maybe_unused]] void* volatile ptr = malloc_zone_valloc(zone, test_size_);
Benoit Lize7a052052020-08-03 12:48:46476 });
[email protected]bac984102013-06-28 17:40:24477}
478
479TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) {
480 malloc_zone_t* zone = malloc_default_purgeable_zone();
Benoit Lize7a052052020-08-03 12:48:46481 ASSERT_OOM_DEATH({
482 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48483 [[maybe_unused]] void* volatile ptr =
484 malloc_zone_memalign(zone, 8, test_size_);
Benoit Lize7a052052020-08-03 12:48:46485 });
[email protected]bac984102013-06-28 17:40:24486}
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 Shiino6bca5c6b2021-11-19 07:38:05495TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) {
496 ASSERT_OOM_DEATH({
497 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48498 [[maybe_unused]] void* ptr;
499 while ((ptr = base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {
Yuki Shiino6bca5c6b2021-11-19 07:38:05500 }
501 });
502}
503
Arthur Sonzogni62e877a2024-04-30 16:09:43504#if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
Yuki Shiino6bca5c6b2021-11-19 07:38:05505// PartitionAlloc-Everywhere does not intercept other malloc zones than the
Avi Drissman70d05a42025-05-29 15:19:23506// default (the top) malloc zone.
Yuki Shiino6bca5c6b2021-11-19 07:38:05507#define MAYBE_CFAllocatorSystemDefault DISABLED_CFAllocatorSystemDefault
508#else
509#define MAYBE_CFAllocatorSystemDefault CFAllocatorSystemDefault
510#endif
511TEST_F(OutOfMemoryDeathTest, MAYBE_CFAllocatorSystemDefault) {
Benoit Lize7a052052020-08-03 12:48:46512 ASSERT_OOM_DEATH({
513 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48514 [[maybe_unused]] void* ptr;
515 while (
516 (ptr = base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {
Benoit Lize7a052052020-08-03 12:48:46517 }
518 });
[email protected]bac984102013-06-28 17:40:24519}
520
Arthur Sonzogni62e877a2024-04-30 16:09:43521#if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
Yuki Shiino6bca5c6b2021-11-19 07:38:05522// PartitionAlloc-Everywhere does not intercept other malloc zones than the
Avi Drissman70d05a42025-05-29 15:19:23523// default (the top) malloc zone.
Yuki Shiino6bca5c6b2021-11-19 07:38:05524#define MAYBE_CFAllocatorMallocZone DISABLED_CFAllocatorMallocZone
525#else
526#define MAYBE_CFAllocatorMallocZone CFAllocatorMallocZone
527#endif
528TEST_F(OutOfMemoryDeathTest, MAYBE_CFAllocatorMallocZone) {
Benoit Lize7a052052020-08-03 12:48:46529 ASSERT_OOM_DEATH({
530 SetUpInDeathAssert();
Bartek Nowierski5f04c3502024-01-30 06:12:48531 [[maybe_unused]] void* ptr;
532 while ((ptr = base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {
Benoit Lize7a052052020-08-03 12:48:46533 }
534 });
[email protected]bac984102013-06-28 17:40:24535}
536
Xiaohan Wang37e81612022-01-15 18:27:00537#endif // BUILDFLAG(IS_MAC)
[email protected]bac984102013-06-28 17:40:24538
[email protected]29159eb2014-03-21 22:07:03539class 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
dcheng8aef37612014-12-23 02:56:47545 void SetUp() override {
[email protected]29159eb2014-03-21 22:07:03546 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 }
erikchen2f9bf052017-03-28 01:11:26553
554 void TearDown() override {
Xiaohan Wang37e81612022-01-15 18:27:00555#if BUILDFLAG(IS_MAC)
Yuki Shiino2ff81312022-09-05 13:11:55556 allocator_shim::UninterceptMallocZonesForTesting();
erikchen2f9bf052017-03-28 01:11:26557#endif
558 }
[email protected]29159eb2014-03-21 22:07:03559};
560
Xiaohan Wang37e81612022-01-15 18:27:00561#if BUILDFLAG(IS_WIN)
siggi7a7fbf7c52017-02-06 20:45:04562
563namespace {
564
565DWORD 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
577TEST_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 Wang37e81612022-01-15 18:27:00593#endif // BUILDFLAG(IS_WIN)
siggi7a7fbf7c52017-02-06 20:45:04594
Sean McAllister39b8d342020-08-25 09:08:32595#if defined(ARCH_CPU_32_BITS) && \
Xiaohan Wang37e81612022-01-15 18:27:00596 (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
Benoît Lizé70f64a02020-01-15 00:33:13597
598void TestAllocationsReleaseReservation(void* (*alloc_fn)(size_t),
599 void (*free_fn)(void*)) {
Kalvin Leecb9c00c22022-07-12 05:18:35600 partition_alloc::ReleaseReservation();
Benoît Lizé70f64a02020-01-15 00:33:13601 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 Leecb9c00c22022-07-12 05:18:35607 while (!partition_alloc::ReserveAddressSpace(reservation_size)) {
Benoît Lizé70f64a02020-01-15 00:33:13608 reservation_size -= 16 * kMiB;
609 }
Kalvin Leecb9c00c22022-07-12 05:18:35610 ASSERT_TRUE(partition_alloc::HasReservationForTesting());
Benoît Lizé70f64a02020-01-15 00:33:13611 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 Kasting134ef9af2024-12-28 02:30:09634 if (!partition_alloc::HasReservationForTesting()) {
Benoît Lizé70f64a02020-01-15 00:33:13635 break;
Peter Kasting134ef9af2024-12-28 02:30:09636 }
Benoît Lizé70f64a02020-01-15 00:33:13637 }
638
639 EXPECT_GE(areas.size(), 2u)
640 << "Should be able to allocate without releasing the reservation";
641
Peter Kasting134ef9af2024-12-28 02:30:09642 for (void* ptr : areas) {
Benoît Lizé70f64a02020-01-15 00:33:13643 free_fn(ptr);
Peter Kasting134ef9af2024-12-28 02:30:09644 }
Benoît Lizé70f64a02020-01-15 00:33:13645}
646
647TEST_F(OutOfMemoryHandledTest, MallocReleasesReservation) {
648 TestAllocationsReleaseReservation(malloc, free);
649}
650
651TEST_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 Wang37e81612022-01-15 18:27:00656#endif // defined(ARCH_CPU_32_BITS) && (BUILDFLAG(IS_WIN) ||
657 // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
Benoît Lizé70f64a02020-01-15 00:33:13658
Xiaohan Wang37e81612022-01-15 18:27:00659#if BUILDFLAG(IS_ANDROID)
Kevin Marshall7a835b42022-01-08 23:17:11660
661// Android's allocator does not allow overcommits, so very large
662// UncheckedMallocs will yield OOM errors.
Alison Gale47d1537d2024-04-19 21:31:46663// TODO(crbug.com/40143202): Fails on some Android bots.
Benoit Lizeced56e12020-08-06 09:14:48664#define MAYBE_UncheckedMallocDies DISABLED_UncheckedMallocDies
665#define MAYBE_UncheckedCallocDies DISABLED_UncheckedCallocDies
Benoit Lizeced56e12020-08-06 09:14:48666TEST_F(OutOfMemoryDeathTest, MAYBE_UncheckedMallocDies) {
Benoit Lize7a052052020-08-03 12:48:46667 ASSERT_OOM_DEATH({
668 SetUpInDeathAssert();
669 void* data;
Avi Drissman933398e2022-01-22 00:55:42670 std::ignore = base::UncheckedMalloc(test_size_, &data);
Kevin Marshall64a82a92021-09-08 20:49:45671 // Death expected here.
Benoit Lize7a052052020-08-03 12:48:46672 });
Benoit Lize2b7a7462020-08-03 09:20:53673}
674
Benoit Lizeced56e12020-08-06 09:14:48675TEST_F(OutOfMemoryDeathTest, MAYBE_UncheckedCallocDies) {
Benoit Lize7a052052020-08-03 12:48:46676 ASSERT_OOM_DEATH({
677 SetUpInDeathAssert();
678 void* data;
Avi Drissman933398e2022-01-22 00:55:42679 std::ignore = base::UncheckedCalloc(1, test_size_, &data);
Kevin Marshall64a82a92021-09-08 20:49:45680 // Death expected here.
Benoit Lize7a052052020-08-03 12:48:46681 });
Benoit Lize2b7a7462020-08-03 09:20:53682}
683
684#else
685
[email protected]29159eb2014-03-21 22:07:03686TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) {
Bartek Nowierski5f04c3502024-01-30 06:12:48687 void* ptr;
688 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &ptr));
689 EXPECT_TRUE(ptr != nullptr);
690 base::UncheckedFree(ptr);
[email protected]29159eb2014-03-21 22:07:03691
Bartek Nowierski5f04c3502024-01-30 06:12:48692 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &ptr));
693 EXPECT_TRUE(ptr == nullptr);
[email protected]29159eb2014-03-21 22:07:03694}
695
696TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) {
Bartek Nowierski5f04c3502024-01-30 06:12:48697 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 Kasting134ef9af2024-12-28 02:30:09701 for (size_t i = 0; i < kSafeMallocSize; ++i) {
[email protected]29159eb2014-03-21 22:07:03702 EXPECT_EQ(0, bytes[i]);
Peter Kasting134ef9af2024-12-28 02:30:09703 }
Bartek Nowierski5f04c3502024-01-30 06:12:48704 base::UncheckedFree(ptr);
[email protected]29159eb2014-03-21 22:07:03705
Bartek Nowierski5f04c3502024-01-30 06:12:48706 EXPECT_TRUE(base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &ptr));
707 EXPECT_TRUE(ptr != nullptr);
708 bytes = static_cast<const char*>(ptr);
Peter Kasting134ef9af2024-12-28 02:30:09709 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) {
[email protected]29159eb2014-03-21 22:07:03710 EXPECT_EQ(0, bytes[i]);
Peter Kasting134ef9af2024-12-28 02:30:09711 }
Bartek Nowierski5f04c3502024-01-30 06:12:48712 base::UncheckedFree(ptr);
[email protected]29159eb2014-03-21 22:07:03713
Bartek Nowierski5f04c3502024-01-30 06:12:48714 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &ptr));
715 EXPECT_TRUE(ptr == nullptr);
[email protected]29159eb2014-03-21 22:07:03716}
Benoit Lize2b7a7462020-08-03 09:20:53717
Xiaohan Wang37e81612022-01-15 18:27:00718#endif // BUILDFLAG(IS_ANDROID)
Arthur Sonzogni62e877a2024-04-30 16:09:43719#endif // !BUILDFLAG(IS_OPENBSD) && PA_BUILDFLAG(USE_ALLOCATOR_SHIM) &&
primiano227dbd32016-08-03 16:31:03720 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
Benoit Lizee21b5902022-11-08 17:36:55721
Arthur Sonzogni62e877a2024-04-30 16:09:43722#if BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
Benoit Lizee21b5902022-11-08 17:36:55723
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 Jianuad4cc6232024-10-08 19:06:01728size_t need_a_static_initializer = [] {
Benoit Lizee21b5902022-11-08 17:36:55729 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 Sonzogni62e877a2024-04-30 16:09:43742#endif // BUILDFLAG(IS_MAC) && PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)