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
3 // found in the LICENSE file.
5 #define _CRT_SECURE_NO_WARNINGS
7 #include "base/process/memory.h"
11 #include "base/compiler_specific.h"
12 #include "base/debug/alias.h"
13 #include "base/strings/stringprintf.h"
14 #include "testing/gtest/include/gtest/gtest.h"
22 #if defined(OS_MACOSX)
23 #include <malloc/malloc.h>
24 #include "base/mac/mac_util.h"
25 #include "base/process/memory_unittest_mac.h"
29 #include "base/test/malloc_wrapper.h"
33 // HeapQueryInformation function pointer.
34 typedef BOOL (WINAPI
* HeapQueryFn
) \
35 (HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
37 const int kConstantInModule
= 42;
39 TEST(ProcessMemoryTest
, GetModuleFromAddress
) {
40 // Since the unit tests are their own EXE, this should be
41 // equivalent to the EXE's HINSTANCE.
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
)));
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
);
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
63 const char* no_debug_env
= getenv("_NO_DEBUG_HEAP");
64 if (!no_debug_env
|| strcmp(no_debug_env
, "1"))
67 HMODULE kernel32
= GetModuleHandle(L
"kernel32.dll");
68 ASSERT_TRUE(kernel32
!= NULL
);
69 HeapQueryFn heap_query
= reinterpret_cast<HeapQueryFn
>(GetProcAddress(
71 "HeapQueryInformation"));
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
76 if (heap_query
== NULL
)
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
) {
85 ASSERT_NE(0, heap_query(heaps
[i
],
86 HeapCompatibilityInformation
,
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.
95 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag.
100 #endif // defined(OS_WIN)
102 #if defined(OS_MACOSX)
104 // For the following Mac tests:
105 // Note that base::EnableTerminationOnHeapCorruption() is called as part of
106 // test suite setup and does not need to be done again, else mach_override
109 TEST(ProcessMemoryTest
, MacTerminateOnHeapCorruption
) {
110 // Assert that freeing an unallocated pointer will crash the process.
112 asm("" : "=r" (buf
)); // Prevent clang from being too smart.
114 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption
115 // but does not output anything.
116 ASSERT_DEATH(free(buf
), "");
117 #elif defined(ADDRESS_SANITIZER)
118 // AddressSanitizer replaces malloc() and prints a different error message on
120 ASSERT_DEATH(free(buf
), "attempting free on address which "
121 "was not malloc\\(\\)-ed");
123 ADD_FAILURE() << "This test is not supported in this build configuration.";
127 #endif // defined(OS_MACOSX)
129 // Android doesn't implement set_new_handler, so we can't use the
130 // OutOfMemoryTest cases. OpenBSD does not support these tests either.
131 // Don't test these on ASan/TSan/MSan configurations: only test the real
133 // TODO(vandebo) make this work on Windows too.
134 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) && \
135 !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
137 #if defined(USE_TCMALLOC)
139 int tc_set_new_mode(int mode
);
141 #endif // defined(USE_TCMALLOC)
144 const char *kOomRegex
= "Out of memory";
147 class OutOfMemoryTest
: public testing::Test
{
151 // Make test size as large as possible minus a few pages so
152 // that alignment or other rounding doesn't make it wrap.
153 test_size_(std::numeric_limits
<std::size_t>::max() - 12 * 1024),
154 signed_test_size_(std::numeric_limits
<ssize_t
>::max()) {
157 #if defined(USE_TCMALLOC)
158 void SetUp() override
{ tc_set_new_mode(1); }
160 void TearDown() override
{ tc_set_new_mode(0); }
161 #endif // defined(USE_TCMALLOC)
166 ssize_t signed_test_size_
;
169 class OutOfMemoryDeathTest
: public OutOfMemoryTest
{
171 void SetUpInDeathAssert() {
172 // Must call EnableTerminationOnOutOfMemory() because that is called from
173 // chrome's main function and therefore hasn't been called yet.
174 // Since this call may result in another thread being created and death
175 // tests shouldn't be started in a multithread environment, this call
176 // should be done inside of the ASSERT_DEATH.
177 base::EnableTerminationOnOutOfMemory();
181 TEST_F(OutOfMemoryDeathTest
, New
) {
183 SetUpInDeathAssert();
184 value_
= operator new(test_size_
);
188 TEST_F(OutOfMemoryDeathTest
, NewArray
) {
190 SetUpInDeathAssert();
191 value_
= new char[test_size_
];
195 TEST_F(OutOfMemoryDeathTest
, Malloc
) {
197 SetUpInDeathAssert();
198 value_
= malloc(test_size_
);
202 TEST_F(OutOfMemoryDeathTest
, Realloc
) {
204 SetUpInDeathAssert();
205 value_
= realloc(NULL
, test_size_
);
209 TEST_F(OutOfMemoryDeathTest
, Calloc
) {
211 SetUpInDeathAssert();
212 value_
= calloc(1024, test_size_
/ 1024L);
216 TEST_F(OutOfMemoryDeathTest
, Valloc
) {
218 SetUpInDeathAssert();
219 value_
= valloc(test_size_
);
223 #if defined(OS_LINUX)
225 #if PVALLOC_AVAILABLE == 1
226 TEST_F(OutOfMemoryDeathTest
, Pvalloc
) {
228 SetUpInDeathAssert();
229 value_
= pvalloc(test_size_
);
232 #endif // PVALLOC_AVAILABLE == 1
234 TEST_F(OutOfMemoryDeathTest
, Memalign
) {
236 SetUpInDeathAssert();
237 value_
= memalign(4, test_size_
);
241 TEST_F(OutOfMemoryDeathTest
, ViaSharedLibraries
) {
242 // This tests that the run-time symbol resolution is overriding malloc for
243 // shared libraries as well as for our code.
245 SetUpInDeathAssert();
246 value_
= MallocWrapper(test_size_
);
251 // Android doesn't implement posix_memalign().
252 #if defined(OS_POSIX) && !defined(OS_ANDROID)
253 TEST_F(OutOfMemoryDeathTest
, Posix_memalign
) {
254 // Grab the return value of posix_memalign to silence a compiler warning
255 // about unused return values. We don't actually care about the return
256 // value, since we're asserting death.
258 SetUpInDeathAssert();
259 EXPECT_EQ(ENOMEM
, posix_memalign(&value_
, 8, test_size_
));
262 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
264 #if defined(OS_MACOSX)
266 // Purgeable zone tests
268 TEST_F(OutOfMemoryDeathTest
, MallocPurgeable
) {
269 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
271 SetUpInDeathAssert();
272 value_
= malloc_zone_malloc(zone
, test_size_
);
276 TEST_F(OutOfMemoryDeathTest
, ReallocPurgeable
) {
277 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
279 SetUpInDeathAssert();
280 value_
= malloc_zone_realloc(zone
, NULL
, test_size_
);
284 TEST_F(OutOfMemoryDeathTest
, CallocPurgeable
) {
285 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
287 SetUpInDeathAssert();
288 value_
= malloc_zone_calloc(zone
, 1024, test_size_
/ 1024L);
292 TEST_F(OutOfMemoryDeathTest
, VallocPurgeable
) {
293 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
295 SetUpInDeathAssert();
296 value_
= malloc_zone_valloc(zone
, test_size_
);
300 TEST_F(OutOfMemoryDeathTest
, PosixMemalignPurgeable
) {
301 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
303 SetUpInDeathAssert();
304 value_
= malloc_zone_memalign(zone
, 8, test_size_
);
308 // Since these allocation functions take a signed size, it's possible that
309 // calling them just once won't be enough to exhaust memory. In the 32-bit
310 // environment, it's likely that these allocation attempts will fail because
311 // not enough contiguous address space is available. In the 64-bit environment,
312 // it's likely that they'll fail because they would require a preposterous
313 // amount of (virtual) memory.
315 TEST_F(OutOfMemoryDeathTest
, CFAllocatorSystemDefault
) {
317 SetUpInDeathAssert();
319 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_
))) {}
323 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMalloc
) {
325 SetUpInDeathAssert();
327 base::AllocateViaCFAllocatorMalloc(signed_test_size_
))) {}
331 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMallocZone
) {
333 SetUpInDeathAssert();
335 base::AllocateViaCFAllocatorMallocZone(signed_test_size_
))) {}
339 #if !defined(ARCH_CPU_64_BITS)
341 // See process_util_unittest_mac.mm for an explanation of why this test isn't
342 // run in the 64-bit environment.
344 TEST_F(OutOfMemoryDeathTest
, PsychoticallyBigObjCObject
) {
346 SetUpInDeathAssert();
347 while ((value_
= base::AllocatePsychoticallyBigObjCObject())) {}
351 #endif // !ARCH_CPU_64_BITS
354 class OutOfMemoryHandledTest
: public OutOfMemoryTest
{
356 static const size_t kSafeMallocSize
= 512;
357 static const size_t kSafeCallocSize
= 128;
358 static const size_t kSafeCallocItems
= 4;
360 void SetUp() override
{
361 OutOfMemoryTest::SetUp();
363 // We enable termination on OOM - just as Chrome does at early
364 // initialization - and test that UncheckedMalloc and UncheckedCalloc
365 // properly by-pass this in order to allow the caller to handle OOM.
366 base::EnableTerminationOnOutOfMemory();
370 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work
371 // on Windows as well.
372 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc()
373 // under sanitizer tools.
374 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
375 TEST_F(OutOfMemoryHandledTest
, UncheckedMalloc
) {
376 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS
377 // The Mavericks malloc library changed in a way which breaks the tricks used
378 // to implement EnableTerminationOnOutOfMemory() with UncheckedMalloc() under
379 // 32-bit. The 64-bit malloc library works as desired without tricks.
380 if (base::mac::IsOSMavericksOrLater())
383 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize
, &value_
));
384 EXPECT_TRUE(value_
!= NULL
);
387 EXPECT_FALSE(base::UncheckedMalloc(test_size_
, &value_
));
388 EXPECT_TRUE(value_
== NULL
);
391 TEST_F(OutOfMemoryHandledTest
, UncheckedCalloc
) {
392 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS
393 // The Mavericks malloc library changed in a way which breaks the tricks used
394 // to implement EnableTerminationOnOutOfMemory() with UncheckedCalloc() under
395 // 32-bit. The 64-bit malloc library works as desired without tricks.
396 if (base::mac::IsOSMavericksOrLater())
399 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize
, &value_
));
400 EXPECT_TRUE(value_
!= NULL
);
401 const char* bytes
= static_cast<const char*>(value_
);
402 for (size_t i
= 0; i
< kSafeMallocSize
; ++i
)
403 EXPECT_EQ(0, bytes
[i
]);
407 base::UncheckedCalloc(kSafeCallocItems
, kSafeCallocSize
, &value_
));
408 EXPECT_TRUE(value_
!= NULL
);
409 bytes
= static_cast<const char*>(value_
);
410 for (size_t i
= 0; i
< (kSafeCallocItems
* kSafeCallocSize
); ++i
)
411 EXPECT_EQ(0, bytes
[i
]);
414 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_
, &value_
));
415 EXPECT_TRUE(value_
== NULL
);
417 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
418 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN) &&
419 // !defined(ADDRESS_SANITIZER)