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/process/memory.h" | 9 #include "base/process/memory.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
| 12 #if defined(OS_MACOSX) |
| 13 #include "base/allocator/allocator_interception_mac.h" |
| 14 #endif |
| 15 |
12 #if defined(ALLOCATOR_SHIM) | 16 #if defined(ALLOCATOR_SHIM) |
13 // Test that the allocator shim is in-place so that base::UncheckedMalloc works. | 17 // Test that the allocator shim is in-place so that base::UncheckedMalloc works. |
14 TEST(OutOfMemoryHandledTest, UncheckedMalloc) { | 18 TEST(OutOfMemoryHandledTest, UncheckedMalloc) { |
15 // Enable termination on OOM - just as setup.exe does at early initialization | 19 // Enable termination on OOM - just as setup.exe does at early initialization |
16 // - and test that UncheckedMalloc properly by-passes this in order to allow | 20 // - and test that UncheckedMalloc properly by-passes this in order to allow |
17 // the caller to handle OOM. | 21 // the caller to handle OOM. |
18 base::EnableTerminationOnOutOfMemory(); | 22 base::EnableTerminationOnOutOfMemory(); |
19 | 23 |
20 const size_t kSafeMallocSize = 512; | 24 const size_t kSafeMallocSize = 512; |
21 | 25 |
22 void* value = nullptr; | 26 void* value = nullptr; |
23 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value)); | 27 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value)); |
24 EXPECT_NE(nullptr, value); | 28 EXPECT_NE(nullptr, value); |
25 free(value); | 29 free(value); |
26 | 30 |
27 // Make test size as large as possible minus a few pages so that alignment or | 31 // Make test size as large as possible minus a few pages so that alignment or |
28 // other rounding doesn't make it wrap. | 32 // other rounding doesn't make it wrap. |
29 const size_t kUnsafeMallocSize( | 33 const size_t kUnsafeMallocSize( |
30 std::numeric_limits<std::size_t>::max() - 12 * 1024); | 34 std::numeric_limits<std::size_t>::max() - 12 * 1024); |
31 | 35 |
32 EXPECT_FALSE(base::UncheckedMalloc(kUnsafeMallocSize, &value)); | 36 EXPECT_FALSE(base::UncheckedMalloc(kUnsafeMallocSize, &value)); |
33 EXPECT_EQ(nullptr, value); | 37 EXPECT_EQ(nullptr, value); |
| 38 |
| 39 #if defined(OS_MACOSX) |
| 40 base::allocator::UninterceptMallocZonesForTesting(); |
| 41 #endif |
34 } | 42 } |
35 #endif // ALLOCATOR_SHIM | 43 #endif // ALLOCATOR_SHIM |
OLD | NEW |