| 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) | |
| 17 #include <windows.h> | |
| 18 #endif | |
| 19 #if defined(OS_POSIX) | 16 #if defined(OS_POSIX) |
| 20 #include <errno.h> | 17 #include <errno.h> |
| 21 #endif | 18 #endif |
| 22 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
| 23 #include <malloc/malloc.h> | 20 #include <malloc/malloc.h> |
| 24 #include "base/mac/mac_util.h" | 21 #include "base/mac/mac_util.h" |
| 25 #include "base/process/memory_unittest_mac.h" | 22 #include "base/process/memory_unittest_mac.h" |
| 26 #endif | 23 #endif |
| 27 #if defined(OS_LINUX) | 24 #if defined(OS_LINUX) |
| 28 #include <malloc.h> | 25 #include <malloc.h> |
| 29 #include "base/test/malloc_wrapper.h" | 26 #include "base/test/malloc_wrapper.h" |
| 30 #endif | 27 #endif |
| 31 | 28 |
| 32 #if defined(OS_WIN) | |
| 33 // HeapQueryInformation function pointer. | |
| 34 typedef BOOL (WINAPI* HeapQueryFn) \ | |
| 35 (HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T); | |
| 36 | |
| 37 const int kConstantInModule = 42; | |
| 38 | |
| 39 TEST(ProcessMemoryTest, GetModuleFromAddress) { | |
| 40 // Since the unit tests are their own EXE, this should be | |
| 41 // equivalent to the EXE's HINSTANCE. | |
| 42 // | |
| 43 // kConstantInModule is a constant in this file and | |
| 44 // therefore within the unit test EXE. | |
| 45 EXPECT_EQ(::GetModuleHandle(NULL), | |
| 46 base::GetModuleFromAddress( | |
| 47 const_cast<int*>(&kConstantInModule))); | |
| 48 | |
| 49 // Any address within the kernel32 module should return | |
| 50 // kernel32's HMODULE. Our only assumption here is that | |
| 51 // kernel32 is larger than 4 bytes. | |
| 52 HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll"); | |
| 53 HMODULE kernel32_from_address = | |
| 54 base::GetModuleFromAddress(reinterpret_cast<DWORD*>(kernel32) + 1); | |
| 55 EXPECT_EQ(kernel32, kernel32_from_address); | |
| 56 } | |
| 57 | |
| 58 TEST(ProcessMemoryTest, EnableLFH) { | |
| 59 ASSERT_TRUE(base::EnableLowFragmentationHeap()); | |
| 60 if (IsDebuggerPresent()) { | |
| 61 // Under these conditions, LFH can't be enabled. There's no point to test | |
| 62 // anything. | |
| 63 const char* no_debug_env = getenv("_NO_DEBUG_HEAP"); | |
| 64 if (!no_debug_env || strcmp(no_debug_env, "1")) | |
| 65 return; | |
| 66 } | |
| 67 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll"); | |
| 68 ASSERT_TRUE(kernel32 != NULL); | |
| 69 HeapQueryFn heap_query = reinterpret_cast<HeapQueryFn>(GetProcAddress( | |
| 70 kernel32, | |
| 71 "HeapQueryInformation")); | |
| 72 | |
| 73 // On Windows 2000, the function is not exported. This is not a reason to | |
| 74 // fail but we won't be able to retrieves information about the heap, so we | |
| 75 // should stop here. | |
| 76 if (heap_query == NULL) | |
| 77 return; | |
| 78 | |
| 79 HANDLE heaps[1024] = { 0 }; | |
| 80 unsigned number_heaps = GetProcessHeaps(1024, heaps); | |
| 81 EXPECT_GT(number_heaps, 0u); | |
| 82 for (unsigned i = 0; i < number_heaps; ++i) { | |
| 83 ULONG flag = 0; | |
| 84 SIZE_T length; | |
| 85 ASSERT_NE(0, heap_query(heaps[i], | |
| 86 HeapCompatibilityInformation, | |
| 87 &flag, | |
| 88 sizeof(flag), | |
| 89 &length)); | |
| 90 // If flag is 0, the heap is a standard heap that does not support | |
| 91 // look-asides. If flag is 1, the heap supports look-asides. If flag is 2, | |
| 92 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not | |
| 93 // supported on the LFH. | |
| 94 | |
| 95 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag. | |
| 96 EXPECT_LE(flag, 2u); | |
| 97 EXPECT_NE(flag, 1u); | |
| 98 } | |
| 99 } | |
| 100 #endif // defined(OS_WIN) | |
| 101 | |
| 102 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 103 | 30 |
| 104 // For the following Mac tests: | 31 // For the following Mac tests: |
| 105 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | 32 // Note that base::EnableTerminationOnHeapCorruption() is called as part of |
| 106 // test suite setup and does not need to be done again, else mach_override | 33 // test suite setup and does not need to be done again, else mach_override |
| 107 // will fail. | 34 // will fail. |
| 108 | 35 |
| 109 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { | 36 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) { |
| 110 // Assert that freeing an unallocated pointer will crash the process. | 37 // Assert that freeing an unallocated pointer will crash the process. |
| 111 char buf[9]; | 38 char buf[9]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 124 #endif | 51 #endif |
| 125 } | 52 } |
| 126 | 53 |
| 127 #endif // defined(OS_MACOSX) | 54 #endif // defined(OS_MACOSX) |
| 128 | 55 |
| 129 // Android doesn't implement set_new_handler, so we can't use the | 56 // Android doesn't implement set_new_handler, so we can't use the |
| 130 // OutOfMemoryTest cases. OpenBSD does not support these tests either. | 57 // OutOfMemoryTest cases. OpenBSD does not support these tests either. |
| 131 // Don't test these on ASan/TSan/MSan configurations: only test the real | 58 // Don't test these on ASan/TSan/MSan configurations: only test the real |
| 132 // allocator. | 59 // allocator. |
| 133 // TODO(vandebo) make this work on Windows too. | 60 // TODO(vandebo) make this work on Windows too. |
| 134 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) && \ | 61 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ |
| 135 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 62 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 136 | 63 |
| 137 #if defined(USE_TCMALLOC) | 64 #if defined(USE_TCMALLOC) |
| 138 extern "C" { | 65 extern "C" { |
| 139 int tc_set_new_mode(int mode); | 66 int tc_set_new_mode(int mode); |
| 140 } | 67 } |
| 141 #endif // defined(USE_TCMALLOC) | 68 #endif // defined(USE_TCMALLOC) |
| 142 | 69 |
| 143 namespace { | 70 namespace { |
| 144 const char *kOomRegex = "Out of memory"; | 71 const char *kOomRegex = "Out of memory"; |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 EXPECT_TRUE(value_ != NULL); | 335 EXPECT_TRUE(value_ != NULL); |
| 409 bytes = static_cast<const char*>(value_); | 336 bytes = static_cast<const char*>(value_); |
| 410 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) | 337 for (size_t i = 0; i < (kSafeCallocItems * kSafeCallocSize); ++i) |
| 411 EXPECT_EQ(0, bytes[i]); | 338 EXPECT_EQ(0, bytes[i]); |
| 412 free(value_); | 339 free(value_); |
| 413 | 340 |
| 414 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); | 341 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_, &value_)); |
| 415 EXPECT_TRUE(value_ == NULL); | 342 EXPECT_TRUE(value_ == NULL); |
| 416 } | 343 } |
| 417 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) | 344 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR) |
| 418 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) && | 345 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && |
| 419 // !defined(ADDRESS_SANITIZER) | 346 // !defined(ADDRESS_SANITIZER) |
| OLD | NEW |