| 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 <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // does not call through malloc_error_break(). See the comment at | 114 // does not call through malloc_error_break(). See the comment at |
| 115 // EnableTerminationOnOutOfMemory() for more information. | 115 // EnableTerminationOnOutOfMemory() for more information. |
| 116 void* buf = NULL; | 116 void* buf = NULL; |
| 117 ASSERT_EXIT( | 117 ASSERT_EXIT( |
| 118 { | 118 { |
| 119 base::EnableTerminationOnOutOfMemory(); | 119 base::EnableTerminationOnOutOfMemory(); |
| 120 | 120 |
| 121 buf = malloc(std::numeric_limits<size_t>::max() - (2 * PAGE_SIZE) - 1); | 121 buf = malloc(std::numeric_limits<size_t>::max() - (2 * PAGE_SIZE) - 1); |
| 122 }, | 122 }, |
| 123 testing::KilledBySignal(SIGTRAP), | 123 testing::KilledBySignal(SIGTRAP), |
| 124 "\\*\\*\\* error: can't allocate region.*" | 124 "\\*\\*\\* error: can't allocate region.*\\n?.*"); |
| 125 "(Terminating process due to a potential for future heap " | |
| 126 "corruption){0}"); | |
| 127 | 125 |
| 128 base::debug::Alias(buf); | 126 base::debug::Alias(buf); |
| 129 } | 127 } |
| 130 #endif // !defined(ADDRESS_SANITIZER) | 128 #endif // !defined(ADDRESS_SANITIZER) |
| 131 | 129 |
| 132 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { | 130 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
| 133 // Assert that freeing an unallocated pointer will crash the process. | 131 // Assert that freeing an unallocated pointer will crash the process. |
| 134 char buf[9]; | 132 char buf[9]; |
| 135 asm("" : "=r" (buf)); // Prevent clang from being too smart. | 133 asm("" : "=r" (buf)); // Prevent clang from being too smart. |
| 136 #if ARCH_CPU_64_BITS | 134 #if ARCH_CPU_64_BITS |
| 137 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption | 135 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption |
| 138 // but does not output anything. | 136 // but does not output anything. |
| 139 ASSERT_DEATH(free(buf), ""); | 137 ASSERT_DEATH(free(buf), ""); |
| 140 #elif defined(ADDRESS_SANITIZER) | 138 #elif defined(ADDRESS_SANITIZER) |
| 141 // AddressSanitizer replaces malloc() and prints a different error message on | 139 // AddressSanitizer replaces malloc() and prints a different error message on |
| 142 // heap corruption. | 140 // heap corruption. |
| 143 ASSERT_DEATH(free(buf), "attempting free on address which " | 141 ASSERT_DEATH(free(buf), "attempting free on address which " |
| 144 "was not malloc\\(\\)-ed"); | 142 "was not malloc\\(\\)-ed"); |
| 145 #else | 143 #else |
| 146 ASSERT_DEATH(free(buf), "being freed.*" | 144 ASSERT_DEATH(free(buf), "being freed.*\\n?\\.*" |
| 147 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*" | 145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*" |
| 148 "Terminating process due to a potential for future heap corruption"); | 146 "Terminating process due to a potential for future heap corruption"); |
| 149 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) | 147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) |
| 150 } | 148 } |
| 151 | 149 |
| 152 #endif // defined(OS_MACOSX) | 150 #endif // defined(OS_MACOSX) |
| 153 | 151 |
| 154 // Android doesn't implement set_new_handler, so we can't use the | 152 // Android doesn't implement set_new_handler, so we can't use the |
| 155 // OutOfMemoryTest cases. | 153 // OutOfMemoryTest cases. |
| 156 // OpenBSD does not support these tests either. | 154 // OpenBSD does not support these tests either. |
| 157 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. | 155 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 SetUpInDeathAssert(); | 367 SetUpInDeathAssert(); |
| 370 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | 368 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} |
| 371 }, ""); | 369 }, ""); |
| 372 } | 370 } |
| 373 | 371 |
| 374 #endif // !ARCH_CPU_64_BITS | 372 #endif // !ARCH_CPU_64_BITS |
| 375 #endif // OS_MACOSX | 373 #endif // OS_MACOSX |
| 376 | 374 |
| 377 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | 375 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
| 378 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | 376 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) |
| OLD | NEW |