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 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
17 #include <windows.h> | 17 #include <windows.h> |
18 #endif | 18 #endif |
19 #if defined(OS_POSIX) | 19 #if defined(OS_POSIX) |
20 #include <errno.h> | 20 #include <errno.h> |
21 #endif | 21 #endif |
22 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
23 #include <malloc/malloc.h> | 23 #include <malloc/malloc.h> |
| 24 #include "base/mac/mac_util.h" |
24 #include "base/process/memory_unittest_mac.h" | 25 #include "base/process/memory_unittest_mac.h" |
25 #endif | 26 #endif |
26 #if defined(OS_LINUX) | 27 #if defined(OS_LINUX) |
27 #include <malloc.h> | 28 #include <malloc.h> |
28 #endif | 29 #endif |
29 | 30 |
30 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
31 // HeapQueryInformation function pointer. | 32 // HeapQueryInformation function pointer. |
32 typedef BOOL (WINAPI* HeapQueryFn) \ | 33 typedef BOOL (WINAPI* HeapQueryFn) \ |
33 (HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T); | 34 (HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 102 |
102 // For the following Mac tests: | 103 // For the following Mac tests: |
103 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | 104 // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
104 // test suite setup and does not need to be done again, else mach_override | 105 // test suite setup and does not need to be done again, else mach_override |
105 // will fail. | 106 // will fail. |
106 | 107 |
107 #if !defined(ADDRESS_SANITIZER) | 108 #if !defined(ADDRESS_SANITIZER) |
108 // The following code tests the system implementation of malloc() thus no need | 109 // The following code tests the system implementation of malloc() thus no need |
109 // to test it under AddressSanitizer. | 110 // to test it under AddressSanitizer. |
110 TEST(ProcessMemoryTest, MacMallocFailureDoesNotTerminate) { | 111 TEST(ProcessMemoryTest, MacMallocFailureDoesNotTerminate) { |
| 112 #if ARCH_CPU_32_BITS |
| 113 // The Mavericks malloc library changed in a way which breaks the tricks used |
| 114 // to implement EnableTerminationOnOutOfMemory() with UncheckedMalloc() under |
| 115 // 32-bit. Under 64-bit the oom_killer code handles this. |
| 116 if (base::mac::IsOSMavericksOrLater()) |
| 117 return; |
| 118 #endif |
| 119 |
111 // Test that ENOMEM doesn't crash via CrMallocErrorBreak two ways: the exit | 120 // Test that ENOMEM doesn't crash via CrMallocErrorBreak two ways: the exit |
112 // code and lack of the error string. The number of bytes is one less than | 121 // code and lack of the error string. The number of bytes is one less than |
113 // MALLOC_ABSOLUTE_MAX_SIZE, more than which the system early-returns NULL and | 122 // MALLOC_ABSOLUTE_MAX_SIZE, more than which the system early-returns NULL and |
114 // does not call through malloc_error_break(). See the comment at | 123 // does not call through malloc_error_break(). See the comment at |
115 // EnableTerminationOnOutOfMemory() for more information. | 124 // EnableTerminationOnOutOfMemory() for more information. |
116 void* buf = NULL; | 125 void* buf = NULL; |
117 ASSERT_EXIT( | 126 ASSERT_EXIT( |
118 { | 127 { |
119 base::EnableTerminationOnOutOfMemory(); | 128 base::EnableTerminationOnOutOfMemory(); |
120 | 129 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 base::EnableTerminationOnOutOfMemory(); | 399 base::EnableTerminationOnOutOfMemory(); |
391 } | 400 } |
392 }; | 401 }; |
393 | 402 |
394 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | 403 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
395 // on Windows as well. | 404 // on Windows as well. |
396 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() | 405 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() |
397 // under sanitizer tools. | 406 // under sanitizer tools. |
398 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 407 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
399 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { | 408 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
| 409 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS |
| 410 // The Mavericks malloc library changed in a way which breaks the tricks used |
| 411 // to implement EnableTerminationOnOutOfMemory() with UncheckedMalloc() under |
| 412 // 32-bit. The 64-bit malloc library works as desired without tricks. |
| 413 if (base::mac::IsOSMavericksOrLater()) |
| 414 return; |
| 415 #endif |
400 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); | 416 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
401 EXPECT_TRUE(value_ != NULL); | 417 EXPECT_TRUE(value_ != NULL); |
402 free(value_); | 418 free(value_); |
403 | 419 |
404 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | 420 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
405 EXPECT_TRUE(value_ == NULL); | 421 EXPECT_TRUE(value_ == NULL); |
406 } | 422 } |
407 | 423 |
408 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { | 424 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
| 425 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS |
| 426 // The Mavericks malloc library changed in a way which breaks the tricks used |
| 427 // to implement EnableTerminationOnOutOfMemory() with UncheckedCalloc() under |
| 428 // 32-bit. The 64-bit malloc library works as desired without tricks. |
| 429 if (base::mac::IsOSMavericksOrLater()) |
| 430 return; |
| 431 #endif |
409 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); | 432 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); |
410 EXPECT_TRUE(value_ != NULL); | 433 EXPECT_TRUE(value_ != NULL); |
411 const char* bytes = static_cast<const char*>(value_); | 434 const char* bytes = static_cast<const char*>(value_); |
412 for (size_t i = 0; i < kSafeMallocSize; ++i) | 435 for (size_t i = 0; i < kSafeMallocSize; ++i) |
413 EXPECT_EQ(0, bytes[i]); | 436 EXPECT_EQ(0, bytes[i]); |
414 free(value_); | 437 free(value_); |
415 | 438 |
416 EXPECT_TRUE( | 439 EXPECT_TRUE( |
417 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); | 440 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); |
418 EXPECT_TRUE(value_ != NULL); | 441 EXPECT_TRUE(value_ != NULL); |
419 bytes = static_cast<const char*>(value_); | 442 bytes = static_cast<const char*>(value_); |
420 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 443 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
421 EXPECT_EQ(0, bytes[i]); | 444 EXPECT_EQ(0, bytes[i]); |
422 free(value_); | 445 free(value_); |
423 | 446 |
424 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 447 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
425 EXPECT_TRUE(value_ == NULL); | 448 EXPECT_TRUE(value_ == NULL); |
426 } | 449 } |
427 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 450 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
428 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) | 451 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) |
OLD | NEW |