OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/allocator/partition_allocator/partition_alloc.h" | 5 #include "base/allocator/partition_allocator/partition_alloc.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bits.h" | 13 #include "base/bits.h" |
| 14 #include "base/sys_info.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
18 #include <sys/mman.h> | 19 #include <sys/mman.h> |
19 #include <sys/resource.h> | 20 #include <sys/resource.h> |
20 #include <sys/time.h> | 21 #include <sys/time.h> |
21 | 22 |
22 #ifndef MAP_ANONYMOUS | 23 #ifndef MAP_ANONYMOUS |
23 #define MAP_ANONYMOUS MAP_ANON | 24 #define MAP_ANONYMOUS MAP_ANON |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 178 } |
178 | 179 |
179 void CheckPageInCore(void* ptr, bool inCore) { | 180 void CheckPageInCore(void* ptr, bool inCore) { |
180 #if defined(OS_LINUX) | 181 #if defined(OS_LINUX) |
181 unsigned char ret; | 182 unsigned char ret; |
182 EXPECT_EQ(0, mincore(ptr, kSystemPageSize, &ret)); | 183 EXPECT_EQ(0, mincore(ptr, kSystemPageSize, &ret)); |
183 EXPECT_EQ(inCore, ret); | 184 EXPECT_EQ(inCore, ret); |
184 #endif | 185 #endif |
185 } | 186 } |
186 | 187 |
| 188 bool IsLargeMemoryDevice() { |
| 189 return base::SysInfo::AmountOfPhysicalMemory() >= 2LL * 1024 * 1024 * 1024; |
| 190 } |
| 191 |
187 class MockPartitionStatsDumper : public PartitionStatsDumper { | 192 class MockPartitionStatsDumper : public PartitionStatsDumper { |
188 public: | 193 public: |
189 MockPartitionStatsDumper() | 194 MockPartitionStatsDumper() |
190 : m_totalResidentBytes(0), | 195 : m_totalResidentBytes(0), |
191 m_totalActiveBytes(0), | 196 m_totalActiveBytes(0), |
192 m_totalDecommittableBytes(0), | 197 m_totalDecommittableBytes(0), |
193 m_totalDiscardableBytes(0) {} | 198 m_totalDiscardableBytes(0) {} |
194 | 199 |
195 void partitionDumpTotals(const char* partitionName, | 200 void partitionDumpTotals(const char* partitionName, |
196 const PartitionMemoryStats* memoryStats) override { | 201 const PartitionMemoryStats* memoryStats) override { |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 // enabled, the allocated area is overwritten with an "uninitialized" | 667 // enabled, the allocated area is overwritten with an "uninitialized" |
663 // byte pattern. | 668 // byte pattern. |
664 EXPECT_EQ(0, *(reinterpret_cast<char*>(newPtr) + (size - 1))); | 669 EXPECT_EQ(0, *(reinterpret_cast<char*>(newPtr) + (size - 1))); |
665 #endif | 670 #endif |
666 partitionFreeGeneric(genericAllocator.root(), newPtr); | 671 partitionFreeGeneric(genericAllocator.root(), newPtr); |
667 partitionFreeGeneric(genericAllocator.root(), ptr3); | 672 partitionFreeGeneric(genericAllocator.root(), ptr3); |
668 partitionFreeGeneric(genericAllocator.root(), ptr4); | 673 partitionFreeGeneric(genericAllocator.root(), ptr4); |
669 | 674 |
670 // Can we allocate a massive (512MB) size? | 675 // Can we allocate a massive (512MB) size? |
671 // Allocate 512MB, but +1, to test for cookie writing alignment issues. | 676 // Allocate 512MB, but +1, to test for cookie writing alignment issues. |
672 ptr = partitionAllocGeneric(genericAllocator.root(), 512 * 1024 * 1024 + 1, | 677 // Test this only if the device has enough memory or it might fail due |
673 typeName); | 678 // to OOM. |
674 partitionFreeGeneric(genericAllocator.root(), ptr); | 679 if (IsLargeMemoryDevice()) { |
| 680 ptr = partitionAllocGeneric(genericAllocator.root(), 512 * 1024 * 1024 + 1, |
| 681 typeName); |
| 682 partitionFreeGeneric(genericAllocator.root(), ptr); |
| 683 } |
675 | 684 |
676 // Check a more reasonable, but still direct mapped, size. | 685 // Check a more reasonable, but still direct mapped, size. |
677 // Chop a system page and a byte off to test for rounding errors. | 686 // Chop a system page and a byte off to test for rounding errors. |
678 size = 20 * 1024 * 1024; | 687 size = 20 * 1024 * 1024; |
679 size -= kSystemPageSize; | 688 size -= kSystemPageSize; |
680 size -= 1; | 689 size -= 1; |
681 ptr = partitionAllocGeneric(genericAllocator.root(), size, typeName); | 690 ptr = partitionAllocGeneric(genericAllocator.root(), size, typeName); |
682 char* charPtr = reinterpret_cast<char*>(ptr); | 691 char* charPtr = reinterpret_cast<char*>(ptr); |
683 *(charPtr + (size - 1)) = 'A'; | 692 *(charPtr + (size - 1)) = 'A'; |
684 partitionFreeGeneric(genericAllocator.root(), ptr); | 693 partitionFreeGeneric(genericAllocator.root(), ptr); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 EXPECT_TRUE(ptr); | 744 EXPECT_TRUE(ptr); |
736 actualSize = partitionAllocGetSize(ptr); | 745 actualSize = partitionAllocGetSize(ptr); |
737 EXPECT_EQ(predictedSize, actualSize); | 746 EXPECT_EQ(predictedSize, actualSize); |
738 EXPECT_EQ(requestedSize + kSystemPageSize, actualSize); | 747 EXPECT_EQ(requestedSize + kSystemPageSize, actualSize); |
739 // Check that we can write at the end of the reported size too. | 748 // Check that we can write at the end of the reported size too. |
740 char* charPtr = reinterpret_cast<char*>(ptr); | 749 char* charPtr = reinterpret_cast<char*>(ptr); |
741 *(charPtr + (actualSize - 1)) = 'A'; | 750 *(charPtr + (actualSize - 1)) = 'A'; |
742 partitionFreeGeneric(genericAllocator.root(), ptr); | 751 partitionFreeGeneric(genericAllocator.root(), ptr); |
743 | 752 |
744 // Allocate something very large, and uneven. | 753 // Allocate something very large, and uneven. |
745 requestedSize = 512 * 1024 * 1024 - 1; | 754 if (IsLargeMemoryDevice()) { |
746 predictedSize = | 755 requestedSize = 512 * 1024 * 1024 - 1; |
747 partitionAllocActualSize(genericAllocator.root(), requestedSize); | 756 predictedSize = |
748 ptr = partitionAllocGeneric(genericAllocator.root(), requestedSize, typeName); | 757 partitionAllocActualSize(genericAllocator.root(), requestedSize); |
749 EXPECT_TRUE(ptr); | 758 ptr = |
750 actualSize = partitionAllocGetSize(ptr); | 759 partitionAllocGeneric(genericAllocator.root(), requestedSize, typeName); |
751 EXPECT_EQ(predictedSize, actualSize); | 760 EXPECT_TRUE(ptr); |
752 EXPECT_LT(requestedSize, actualSize); | 761 actualSize = partitionAllocGetSize(ptr); |
753 partitionFreeGeneric(genericAllocator.root(), ptr); | 762 EXPECT_EQ(predictedSize, actualSize); |
| 763 EXPECT_LT(requestedSize, actualSize); |
| 764 partitionFreeGeneric(genericAllocator.root(), ptr); |
| 765 } |
754 | 766 |
755 // Too large allocation. | 767 // Too large allocation. |
756 requestedSize = INT_MAX; | 768 requestedSize = INT_MAX; |
757 predictedSize = | 769 predictedSize = |
758 partitionAllocActualSize(genericAllocator.root(), requestedSize); | 770 partitionAllocActualSize(genericAllocator.root(), requestedSize); |
759 EXPECT_EQ(requestedSize, predictedSize); | 771 EXPECT_EQ(requestedSize, predictedSize); |
760 | 772 |
761 TestShutdown(); | 773 TestShutdown(); |
762 } | 774 } |
763 | 775 |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1318 // | 1330 // |
1319 // Disable this test on Android because, due to its allocation-heavy behavior, | 1331 // Disable this test on Android because, due to its allocation-heavy behavior, |
1320 // it tends to get OOM-killed rather than pass. | 1332 // it tends to get OOM-killed rather than pass. |
1321 #if defined(OS_MACOSX) || defined(OS_ANDROID) | 1333 #if defined(OS_MACOSX) || defined(OS_ANDROID) |
1322 #define MAYBE_RepeatedReturnNullDirect DISABLED_RepeatedReturnNullDirect | 1334 #define MAYBE_RepeatedReturnNullDirect DISABLED_RepeatedReturnNullDirect |
1323 #else | 1335 #else |
1324 #define MAYBE_RepeatedReturnNullDirect RepeatedReturnNullDirect | 1336 #define MAYBE_RepeatedReturnNullDirect RepeatedReturnNullDirect |
1325 #endif | 1337 #endif |
1326 TEST(PartitionAllocTest, MAYBE_RepeatedReturnNullDirect) { | 1338 TEST(PartitionAllocTest, MAYBE_RepeatedReturnNullDirect) { |
1327 // A direct-mapped allocation size. | 1339 // A direct-mapped allocation size. |
1328 DoReturnNullTest(256 * 1024 * 1024); | 1340 DoReturnNullTest(32 * 1024 * 1024); |
1329 } | 1341 } |
1330 | 1342 |
1331 #endif // !defined(ARCH_CPU_64_BITS) || defined(OS_POSIX) | 1343 #endif // !defined(ARCH_CPU_64_BITS) || defined(OS_POSIX) |
1332 | 1344 |
1333 // Death tests misbehave on Android, http://crbug.com/643760. | 1345 // Death tests misbehave on Android, http://crbug.com/643760. |
1334 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) | 1346 #if defined(GTEST_HAS_DEATH_TEST) && !defined(OS_ANDROID) |
1335 | 1347 |
1336 // Make sure that malloc(-1) dies. | 1348 // Make sure that malloc(-1) dies. |
1337 // In the past, we had an integer overflow that would alias malloc(-1) to | 1349 // In the past, we had an integer overflow that would alias malloc(-1) to |
1338 // malloc(0), which is not good. | 1350 // malloc(0), which is not good. |
(...skipping 759 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2098 partitionFreeGeneric(genericAllocator.root(), ptr1); | 2110 partitionFreeGeneric(genericAllocator.root(), ptr1); |
2099 partitionFreeGeneric(genericAllocator.root(), ptr2); | 2111 partitionFreeGeneric(genericAllocator.root(), ptr2); |
2100 } | 2112 } |
2101 | 2113 |
2102 TestShutdown(); | 2114 TestShutdown(); |
2103 } | 2115 } |
2104 | 2116 |
2105 } // namespace base | 2117 } // namespace base |
2106 | 2118 |
2107 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 2119 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |