| 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_interception_mac.h" |
| 31 #include "base/allocator/allocator_shim.h" |
| 30 #include "base/process/memory_unittest_mac.h" | 32 #include "base/process/memory_unittest_mac.h" |
| 31 #endif | 33 #endif |
| 32 #if defined(OS_LINUX) | 34 #if defined(OS_LINUX) |
| 33 #include <malloc.h> | 35 #include <malloc.h> |
| 34 #include "base/test/malloc_wrapper.h" | 36 #include "base/test/malloc_wrapper.h" |
| 35 #endif | 37 #endif |
| 36 | 38 |
| 37 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
| 38 | 40 |
| 39 #if defined(_MSC_VER) | 41 #if defined(_MSC_VER) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 52 #endif // defined(OS_WIN) | 54 #endif // defined(OS_WIN) |
| 53 | 55 |
| 54 #if defined(OS_MACOSX) | 56 #if defined(OS_MACOSX) |
| 55 | 57 |
| 56 // For the following Mac tests: | 58 // For the following Mac tests: |
| 57 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | 59 // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
| 58 // test suite setup and does not need to be done again, else mach_override | 60 // test suite setup and does not need to be done again, else mach_override |
| 59 // will fail. | 61 // will fail. |
| 60 | 62 |
| 61 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { | 63 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
| 64 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 65 base::allocator::InitializeAllocatorShim(); |
| 66 #endif |
| 62 // Assert that freeing an unallocated pointer will crash the process. | 67 // Assert that freeing an unallocated pointer will crash the process. |
| 63 char buf[9]; | 68 char buf[9]; |
| 64 asm("" : "=r" (buf)); // Prevent clang from being too smart. | 69 asm("" : "=r" (buf)); // Prevent clang from being too smart. |
| 65 #if ARCH_CPU_64_BITS | 70 #if ARCH_CPU_64_BITS |
| 66 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption | 71 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption |
| 67 // but does not output anything. | 72 // but does not output anything. |
| 68 ASSERT_DEATH(free(buf), ""); | 73 ASSERT_DEATH(free(buf), ""); |
| 69 #elif defined(ADDRESS_SANITIZER) | 74 #elif defined(ADDRESS_SANITIZER) |
| 70 // AddressSanitizer replaces malloc() and prints a different error message on | 75 // AddressSanitizer replaces malloc() and prints a different error message on |
| 71 // heap corruption. | 76 // heap corruption. |
| 72 ASSERT_DEATH(free(buf), "attempting free on address which " | 77 ASSERT_DEATH(free(buf), "attempting free on address which " |
| 73 "was not malloc\\(\\)-ed"); | 78 "was not malloc\\(\\)-ed"); |
| 74 #else | 79 #else |
| 75 ADD_FAILURE() << "This test is not supported in this build configuration."; | 80 ADD_FAILURE() << "This test is not supported in this build configuration."; |
| 76 #endif | 81 #endif |
| 77 } | 82 } |
| 78 | 83 |
| 79 #endif // defined(OS_MACOSX) | 84 #endif // defined(OS_MACOSX) |
| 80 | 85 |
| 81 TEST(MemoryTest, AllocatorShimWorking) { | 86 TEST(MemoryTest, AllocatorShimWorking) { |
| 87 #if defined(OS_MACOSX) |
| 88 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 89 base::allocator::InitializeAllocatorShim(); |
| 90 #endif |
| 91 base::allocator::InterceptAllocationsMac(); |
| 92 #endif |
| 82 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); | 93 ASSERT_TRUE(base::allocator::IsAllocatorInitialized()); |
| 83 } | 94 } |
| 84 | 95 |
| 85 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan | 96 // OpenBSD does not support these tests. Don't test these on ASan/TSan/MSan |
| 86 // configurations: only test the real allocator. | 97 // configurations: only test the real allocator. |
| 87 // Windows only supports these tests with the allocator shim in place. | 98 // Windows only supports these tests with the allocator shim in place. |
| 88 #if !defined(OS_OPENBSD) && \ | 99 #if !defined(OS_OPENBSD) && \ |
| 89 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ | 100 BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && \ |
| 90 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 101 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 91 | 102 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 117 protected: | 128 protected: |
| 118 void* value_; | 129 void* value_; |
| 119 size_t test_size_; | 130 size_t test_size_; |
| 120 size_t insecure_test_size_; | 131 size_t insecure_test_size_; |
| 121 ssize_t signed_test_size_; | 132 ssize_t signed_test_size_; |
| 122 }; | 133 }; |
| 123 | 134 |
| 124 class OutOfMemoryDeathTest : public OutOfMemoryTest { | 135 class OutOfMemoryDeathTest : public OutOfMemoryTest { |
| 125 public: | 136 public: |
| 126 void SetUpInDeathAssert() { | 137 void SetUpInDeathAssert() { |
| 138 #if defined(OS_MACOSX) && BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 139 base::allocator::InitializeAllocatorShim(); |
| 140 #endif |
| 141 |
| 127 // Must call EnableTerminationOnOutOfMemory() because that is called from | 142 // Must call EnableTerminationOnOutOfMemory() because that is called from |
| 128 // chrome's main function and therefore hasn't been called yet. | 143 // chrome's main function and therefore hasn't been called yet. |
| 129 // Since this call may result in another thread being created and death | 144 // Since this call may result in another thread being created and death |
| 130 // tests shouldn't be started in a multithread environment, this call | 145 // tests shouldn't be started in a multithread environment, this call |
| 131 // should be done inside of the ASSERT_DEATH. | 146 // should be done inside of the ASSERT_DEATH. |
| 132 base::EnableTerminationOnOutOfMemory(); | 147 base::EnableTerminationOnOutOfMemory(); |
| 133 } | 148 } |
| 134 }; | 149 }; |
| 135 | 150 |
| 136 TEST_F(OutOfMemoryDeathTest, New) { | 151 TEST_F(OutOfMemoryDeathTest, New) { |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 bytes = static_cast<const char*>(value_); | 483 bytes = static_cast<const char*>(value_); |
| 469 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 484 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 470 EXPECT_EQ(0, bytes[i]); | 485 EXPECT_EQ(0, bytes[i]); |
| 471 free(value_); | 486 free(value_); |
| 472 | 487 |
| 473 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 488 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 474 EXPECT_TRUE(value_ == NULL); | 489 EXPECT_TRUE(value_ == NULL); |
| 475 } | 490 } |
| 476 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && | 491 #endif // !defined(OS_OPENBSD) && BUILDFLAG(ENABLE_WIN_ALLOCATOR_SHIM_TESTS) && |
| 477 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 492 // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| OLD | NEW |