| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/allocator/features.h" |
| 9 #include "base/process/memory.h" | 10 #include "base/process/memory.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 #if defined(OS_MACOSX) | 13 #if defined(OS_MACOSX) |
| 13 #include "base/allocator/allocator_interception_mac.h" | 14 #include "base/allocator/allocator_interception_mac.h" |
| 14 #endif | 15 #endif |
| 15 | 16 |
| 16 #if defined(ALLOCATOR_SHIM) | 17 #if BUILDFLAG(USE_ALLOCATOR_SHIM) |
| 17 // Test that the allocator shim is in-place so that base::UncheckedMalloc works. | 18 // Test that the allocator shim is in-place so that base::UncheckedMalloc works. |
| 18 TEST(OutOfMemoryHandledTest, UncheckedMalloc) { | 19 TEST(OutOfMemoryHandledTest, UncheckedMalloc) { |
| 19 // Enable termination on OOM - just as setup.exe does at early initialization | 20 // Enable termination on OOM - just as setup.exe does at early initialization |
| 20 // - and test that UncheckedMalloc properly by-passes this in order to allow | 21 // - and test that UncheckedMalloc properly by-passes this in order to allow |
| 21 // the caller to handle OOM. | 22 // the caller to handle OOM. |
| 22 base::EnableTerminationOnOutOfMemory(); | 23 base::EnableTerminationOnOutOfMemory(); |
| 23 | 24 |
| 24 const size_t kSafeMallocSize = 512; | 25 const size_t kSafeMallocSize = 512; |
| 25 | 26 |
| 26 void* value = nullptr; | 27 void* value = nullptr; |
| 27 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value)); | 28 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value)); |
| 28 EXPECT_NE(nullptr, value); | 29 EXPECT_NE(nullptr, value); |
| 29 free(value); | 30 free(value); |
| 30 | 31 |
| 31 // Make test size as large as possible minus a few pages so that alignment or | 32 // Make test size as large as possible minus a few pages so that alignment or |
| 32 // other rounding doesn't make it wrap. | 33 // other rounding doesn't make it wrap. |
| 33 const size_t kUnsafeMallocSize( | 34 const size_t kUnsafeMallocSize( |
| 34 std::numeric_limits<std::size_t>::max() - 12 * 1024); | 35 std::numeric_limits<std::size_t>::max() - 12 * 1024); |
| 35 | 36 |
| 36 EXPECT_FALSE(base::UncheckedMalloc(kUnsafeMallocSize, &value)); | 37 EXPECT_FALSE(base::UncheckedMalloc(kUnsafeMallocSize, &value)); |
| 37 EXPECT_EQ(nullptr, value); | 38 EXPECT_EQ(nullptr, value); |
| 38 | 39 |
| 39 #if defined(OS_MACOSX) | 40 #if defined(OS_MACOSX) |
| 40 base::allocator::UninterceptMallocZonesForTesting(); | 41 base::allocator::UninterceptMallocZonesForTesting(); |
| 41 #endif | 42 #endif |
| 42 } | 43 } |
| 43 #endif // ALLOCATOR_SHIM | 44 #endif // BUILDFLAG(USE_ALLOCATOR_SHIM) |
| OLD | NEW |