Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #define _CRT_SECURE_NO_WARNINGS | 5 #define _CRT_SECURE_NO_WARNINGS |
| 6 | 6 |
| 7 #include "base/process/memory.h" | 7 #include "base/process/memory.h" |
| 8 | 8 |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "base/allocator/allocator_check.h" | 13 #include "base/allocator/allocator_check.h" |
| 14 #include "base/allocator/features.h" | 14 #include "base/allocator/features.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/debug/alias.h" | 16 #include "base/debug/alias.h" |
| 17 #include "base/memory/aligned_memory.h" | 17 #include "base/memory/aligned_memory.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 23 #include <windows.h> | 23 #include <windows.h> |
| 24 #endif | 24 #endif |
| 25 #if defined(OS_POSIX) | 25 #if defined(OS_POSIX) |
| 26 #include <errno.h> | 26 #include <errno.h> |
| 27 #endif | 27 #endif |
| 28 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
| 29 #include <malloc/malloc.h> | 29 #include <malloc/malloc.h> |
| 30 #include "base/allocator/allocator_shim.h" | |
| 30 #include "base/process/memory_unittest_mac.h" | 31 #include "base/process/memory_unittest_mac.h" |
| 31 #endif | 32 #endif |
| 32 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) |
| 33 #include <malloc.h> | 34 #include <malloc.h> |
| 34 #include "base/test/malloc_wrapper.h" | 35 #include "base/test/malloc_wrapper.h" |
| 35 #endif | 36 #endif |
| 36 | 37 |
| 37 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 38 | 39 |
| 39 #if defined(_MSC_VER) | 40 #if defined(_MSC_VER) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 52 #endif // defined(OS_WIN) | 53 #endif // defined(OS_WIN) |
| 53 | 54 |
| 54 #if defined(OS_MACOSX) | 55 #if defined(OS_MACOSX) |
| 55 | 56 |
| 56 // For the following Mac tests: | 57 // For the following Mac tests: |
| 57 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | 58 // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
| 58 // test suite setup and does not need to be done again, else mach_override | 59 // test suite setup and does not need to be done again, else mach_override |
| 59 // will fail. | 60 // will fail. |
| 60 | 61 |
| 61 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { | 62 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
| 63 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
| 64 base::allocator::InitializeAllocatorShim(); | |
| 65 #endif | |
| 62 // Assert that freeing an unallocated pointer will crash the process. | 66 // Assert that freeing an unallocated pointer will crash the process. |
| 63 char buf[9]; | 67 char buf[9]; |
| 64 asm("" : "=r" (buf)); // Prevent clang from being too smart. | 68 asm("" : "=r" (buf)); // Prevent clang from being too smart. |
| 65 #if ARCH_CPU_64_BITS | 69 #if ARCH_CPU_64_BITS |
| 66 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption | 70 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption |
| 67 // but does not output anything. | 71 // but does not output anything. |
| 68 ASSERT_DEATH(free(buf), ""); | 72 ASSERT_DEATH(free(buf), ""); |
| 69 #elif defined(ADDRESS_SANITIZER) | 73 #elif defined(ADDRESS_SANITIZER) |
| 70 // AddressSanitizer replaces malloc() and prints a different error message on | 74 // AddressSanitizer replaces malloc() and prints a different error message on |
| 71 // heap corruption. | 75 // heap corruption. |
| 72 ASSERT_DEATH(free(buf), "attempting free on address which " | 76 ASSERT_DEATH(free(buf), "attempting free on address which " |
| 73 "was not malloc\\(\\)-ed"); | 77 "was not malloc\\(\\)-ed"); |
| 74 #else | 78 #else |
| 75 ADD_FAILURE() << "This test is not supported in this build configuration."; | 79 ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 76 #endif | 80 #endif |
| 77 } | 81 } |
| 78 | 82 |
| 79 #endif // defined(OS_MACOSX) | 83 #endif // defined(OS_MACOSX) |
| 80 | 84 |
| 81 TEST(MemoryTest, AllocatorShimWorking) { | 85 TEST(MemoryTest, AllocatorShimWorking) { |
| 86 #if defined(OS_MACOSX) && BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
| 87 base::allocator::InitializeAllocatorShim(); | |
| 88 #endif | |
| 82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | 89 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
| 83 } | 90 } |
| 84 | 91 |
| 85 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan | 92 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan |
| 86 // configurations: only test the real allocator. | 93 // configurations: only test the real allocator. |
| 87 // Windows only supports these tests with the allocator shim in place. | 94 // Windows only supports these tests with the allocator shim in place. |
| 88 #if !defined(OS_OPENBSD) && \ | 95 #if !defined(OS_OPENBSD) && \ |
| 89 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ | 96 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ |
| 90 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 97 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 91 | 98 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 107 : value_(NULL), | 114 : value_(NULL), |
| 108 // Make test size as large as possible minus a few pages so | 115 // Make test size as large as possible minus a few pages so |
| 109 // that alignment or other rounding doesn't make it wrap. | 116 // that alignment or other rounding doesn't make it wrap. |
| 110 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), | 117 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), |
| 111 // A test size that is > 2Gb and will cause the allocators to reject | 118 // A test size that is > 2Gb and will cause the allocators to reject |
| 112 // the allocation due to security restrictions. See crbug.com/169327. | 119 // the allocation due to security restrictions. See crbug.com/169327. |
| 113 insecure_test_size_(std::numeric_limits<int>::max()), | 120 insecure_test_size_(std::numeric_limits<int>::max()), |
| 114 signed_test_size_(std::numeric_limits<ssize_t>::max()) { | 121 signed_test_size_(std::numeric_limits<ssize_t>::max()) { |
| 115 } | 122 } |
| 116 | 123 |
| 124 static void SetUpTestCase() { | |
| 125 #if defined(OS_MACOSX) && BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) | |
|
Primiano Tucci (use gerrit)
2017/02/07 11:58:08
I think you just need to move these 3 lines (#if..
erikchen
2017/02/09 23:49:04
Done.
| |
| 126 base::allocator::InitializeAllocatorShim(); | |
| 127 #endif | |
| 128 } | |
| 129 | |
| 117 protected: | 130 protected: |
| 118 void* value_; | 131 void* value_; |
| 119 size_t test_size_; | 132 size_t test_size_; |
| 120 size_t insecure_test_size_; | 133 size_t insecure_test_size_; |
| 121 ssize_t signed_test_size_; | 134 ssize_t signed_test_size_; |
| 122 }; | 135 }; |
| 123 | 136 |
| 124 class OutOfMemoryDeathTest : public OutOfMemoryTest { | 137 class OutOfMemoryDeathTest : public OutOfMemoryTest { |
| 125 public: | 138 public: |
| 126 void SetUpInDeathAssert() { | 139 void SetUpInDeathAssert() { |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 bytes = static_cast<const char*>(value_); | 481 bytes = static_cast<const char*>(value_); |
| 469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 482 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 470 EXPECT_EQ(0, bytes[i]); | 483 EXPECT_EQ(0, bytes[i]); |
| 471 free(value_); | 484 free(value_); |
| 472 | 485 |
| 473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 486 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 474 EXPECT_TRUE(value_ == NULL); | 487 EXPECT_TRUE(value_ == NULL); |
| 475 } | 488 } |
| 476 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && | 489 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 477 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 490 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |