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/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/debug/alias.h" | 15 #include "base/debug/alias.h" |
16 #include "base/memory/aligned_memory.h" | 16 #include "base/memory/aligned_memory.h" |
17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
22 #include <windows.h> | 22 #include <windows.h> |
23 #endif | 23 #endif |
24 #if defined(OS_POSIX) | 24 #if defined(OS_POSIX) |
25 #include <errno.h> | 25 #include <errno.h> |
26 #endif | 26 #endif |
27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
28 #include <malloc/malloc.h> | 28 #include <malloc/malloc.h> |
29 #include "base/mac/mac_util.h" | |
30 #include "base/process/memory_unittest_mac.h" | 29 #include "base/process/memory_unittest_mac.h" |
31 #endif | 30 #endif |
32 #if defined(OS_LINUX) | 31 #if defined(OS_LINUX) |
33 #include <malloc.h> | 32 #include <malloc.h> |
34 #include "base/test/malloc_wrapper.h" | 33 #include "base/test/malloc_wrapper.h" |
35 #endif | 34 #endif |
36 | 35 |
37 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
38 | 37 |
39 #if defined(_MSC_VER) | 38 #if defined(_MSC_VER) |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 base::EnableTerminationOnOutOfMemory(); | 401 base::EnableTerminationOnOutOfMemory(); |
403 } | 402 } |
404 }; | 403 }; |
405 | 404 |
406 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work | 405 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work |
407 // on Windows as well. | 406 // on Windows as well. |
408 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() | 407 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc() |
409 // under sanitizer tools. | 408 // under sanitizer tools. |
410 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 409 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
411 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { | 410 TEST_F(OutOfMemoryHandledTest, UncheckedMalloc) { |
412 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS | |
413 // The Mavericks malloc library changed in a way which breaks the tricks used | |
414 // to implement EnableTerminationOnOutOfMemory() with UncheckedMalloc() under | |
415 // 32-bit. The 64-bit malloc library works as desired without tricks. | |
416 if (base::mac::IsOSMavericksOrLater()) | |
417 return; | |
418 #endif | |
419 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); | 411 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_)); |
420 EXPECT_TRUE(value_ != NULL); | 412 EXPECT_TRUE(value_ != NULL); |
421 free(value_); | 413 free(value_); |
422 | 414 |
423 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); | 415 EXPECT_FALSE(base::UncheckedMalloc(test_size_, &value_)); |
424 EXPECT_TRUE(value_ == NULL); | 416 EXPECT_TRUE(value_ == NULL); |
425 } | 417 } |
426 | 418 |
427 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { | 419 TEST_F(OutOfMemoryHandledTest, UncheckedCalloc) { |
428 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS | |
429 // The Mavericks malloc library changed in a way which breaks the tricks used | |
430 // to implement EnableTerminationOnOutOfMemory() with UncheckedCalloc() under | |
431 // 32-bit. The 64-bit malloc library works as desired without tricks. | |
432 if (base::mac::IsOSMavericksOrLater()) | |
433 return; | |
434 #endif | |
435 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); | 420 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize, &value_)); |
436 EXPECT_TRUE(value_ != NULL); | 421 EXPECT_TRUE(value_ != NULL); |
437 const char* bytes = static_cast<const char*>(value_); | 422 const char* bytes = static_cast<const char*>(value_); |
438 for (size_t i = 0; i < kSafeMallocSize; ++i) | 423 for (size_t i = 0; i < kSafeMallocSize; ++i) |
439 EXPECT_EQ(0, bytes[i]); | 424 EXPECT_EQ(0, bytes[i]); |
440 free(value_); | 425 free(value_); |
441 | 426 |
442 EXPECT_TRUE( | 427 EXPECT_TRUE( |
443 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); | 428 base::UncheckedCalloc(kSafeCallocItems, kSafeCallocSize, &value_)); |
444 EXPECT_TRUE(value_ != NULL); | 429 EXPECT_TRUE(value_ != NULL); |
445 bytes = static_cast<const char*>(value_); | 430 bytes = static_cast<const char*>(value_); |
446 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 431 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
447 EXPECT_EQ(0, bytes[i]); | 432 EXPECT_EQ(0, bytes[i]); |
448 free(value_); | 433 free(value_); |
449 | 434 |
450 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 435 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
451 EXPECT_TRUE(value_ == NULL); | 436 EXPECT_TRUE(value_ == NULL); |
452 } | 437 } |
453 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 438 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
454 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && | 439 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !(defined(OS_WIN) && |
455 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 440 // !defined(ALLOCATOR_SHIM)) && !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
OLD | NEW |