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.
131 // OpenBSD does not support these tests either.
132 // TODO(vandebo) make this work on Windows too.
133 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
136 #if defined(USE_TCMALLOC)
138 int tc_set_new_mode(int mode
);
140 #endif // defined(USE_TCMALLOC)
142 class OutOfMemoryTest
: public testing::Test
{
146 // Make test size as large as possible minus a few pages so
147 // that alignment or other rounding doesn't make it wrap.
148 test_size_(std::numeric_limits
<std::size_t>::max() - 12 * 1024),
149 signed_test_size_(std::numeric_limits
<ssize_t
>::max()) {
152 #if defined(USE_TCMALLOC)
153 void SetUp() override
{ tc_set_new_mode(1); }
155 void TearDown() override
{ tc_set_new_mode(0); }
156 #endif // defined(USE_TCMALLOC)
161 ssize_t signed_test_size_
;
164 class OutOfMemoryDeathTest
: public OutOfMemoryTest
{
166 void SetUpInDeathAssert() {
167 // Must call EnableTerminationOnOutOfMemory() because that is called from
168 // chrome's main function and therefore hasn't been called yet.
169 // Since this call may result in another thread being created and death
170 // tests shouldn't be started in a multithread environment, this call
171 // should be done inside of the ASSERT_DEATH.
172 base::EnableTerminationOnOutOfMemory();
176 TEST_F(OutOfMemoryDeathTest
, New
) {
178 SetUpInDeathAssert();
179 value_
= operator new(test_size_
);
183 TEST_F(OutOfMemoryDeathTest
, NewArray
) {
185 SetUpInDeathAssert();
186 value_
= new char[test_size_
];
190 TEST_F(OutOfMemoryDeathTest
, Malloc
) {
192 SetUpInDeathAssert();
193 value_
= malloc(test_size_
);
197 TEST_F(OutOfMemoryDeathTest
, Realloc
) {
199 SetUpInDeathAssert();
200 value_
= realloc(NULL
, test_size_
);
204 TEST_F(OutOfMemoryDeathTest
, Calloc
) {
206 SetUpInDeathAssert();
207 value_
= calloc(1024, test_size_
/ 1024L);
211 TEST_F(OutOfMemoryDeathTest
, Valloc
) {
213 SetUpInDeathAssert();
214 value_
= valloc(test_size_
);
218 #if defined(OS_LINUX)
220 #if PVALLOC_AVAILABLE == 1
221 TEST_F(OutOfMemoryDeathTest
, Pvalloc
) {
223 SetUpInDeathAssert();
224 value_
= pvalloc(test_size_
);
227 #endif // PVALLOC_AVAILABLE == 1
229 TEST_F(OutOfMemoryDeathTest
, Memalign
) {
231 SetUpInDeathAssert();
232 value_
= memalign(4, test_size_
);
236 TEST_F(OutOfMemoryDeathTest
, ViaSharedLibraries
) {
237 // This tests that the run-time symbol resolution is overriding malloc for
238 // shared libraries as well as for our code.
240 SetUpInDeathAssert();
241 value_
= MallocWrapper(test_size_
);
246 // Android doesn't implement posix_memalign().
247 #if defined(OS_POSIX) && !defined(OS_ANDROID)
248 TEST_F(OutOfMemoryDeathTest
, Posix_memalign
) {
249 // Grab the return value of posix_memalign to silence a compiler warning
250 // about unused return values. We don't actually care about the return
251 // value, since we're asserting death.
253 SetUpInDeathAssert();
254 EXPECT_EQ(ENOMEM
, posix_memalign(&value_
, 8, test_size_
));
257 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
259 #if defined(OS_MACOSX)
261 // Purgeable zone tests
263 TEST_F(OutOfMemoryDeathTest
, MallocPurgeable
) {
264 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
266 SetUpInDeathAssert();
267 value_
= malloc_zone_malloc(zone
, test_size_
);
271 TEST_F(OutOfMemoryDeathTest
, ReallocPurgeable
) {
272 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
274 SetUpInDeathAssert();
275 value_
= malloc_zone_realloc(zone
, NULL
, test_size_
);
279 TEST_F(OutOfMemoryDeathTest
, CallocPurgeable
) {
280 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
282 SetUpInDeathAssert();
283 value_
= malloc_zone_calloc(zone
, 1024, test_size_
/ 1024L);
287 TEST_F(OutOfMemoryDeathTest
, VallocPurgeable
) {
288 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
290 SetUpInDeathAssert();
291 value_
= malloc_zone_valloc(zone
, test_size_
);
295 TEST_F(OutOfMemoryDeathTest
, PosixMemalignPurgeable
) {
296 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
298 SetUpInDeathAssert();
299 value_
= malloc_zone_memalign(zone
, 8, test_size_
);
303 // Since these allocation functions take a signed size, it's possible that
304 // calling them just once won't be enough to exhaust memory. In the 32-bit
305 // environment, it's likely that these allocation attempts will fail because
306 // not enough contiguous address space is available. In the 64-bit environment,
307 // it's likely that they'll fail because they would require a preposterous
308 // amount of (virtual) memory.
310 TEST_F(OutOfMemoryDeathTest
, CFAllocatorSystemDefault
) {
312 SetUpInDeathAssert();
314 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_
))) {}
318 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMalloc
) {
320 SetUpInDeathAssert();
322 base::AllocateViaCFAllocatorMalloc(signed_test_size_
))) {}
326 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMallocZone
) {
328 SetUpInDeathAssert();
330 base::AllocateViaCFAllocatorMallocZone(signed_test_size_
))) {}
334 #if !defined(ARCH_CPU_64_BITS)
336 // See process_util_unittest_mac.mm for an explanation of why this test isn't
337 // run in the 64-bit environment.
339 TEST_F(OutOfMemoryDeathTest
, PsychoticallyBigObjCObject
) {
341 SetUpInDeathAssert();
342 while ((value_
= base::AllocatePsychoticallyBigObjCObject())) {}
346 #endif // !ARCH_CPU_64_BITS
349 class OutOfMemoryHandledTest
: public OutOfMemoryTest
{
351 static const size_t kSafeMallocSize
= 512;
352 static const size_t kSafeCallocSize
= 128;
353 static const size_t kSafeCallocItems
= 4;
355 void SetUp() override
{
356 OutOfMemoryTest::SetUp();
358 // We enable termination on OOM - just as Chrome does at early
359 // initialization - and test that UncheckedMalloc and UncheckedCalloc
360 // properly by-pass this in order to allow the caller to handle OOM.
361 base::EnableTerminationOnOutOfMemory();
365 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work
366 // on Windows as well.
367 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc()
368 // under sanitizer tools.
369 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
370 TEST_F(OutOfMemoryHandledTest
, UncheckedMalloc
) {
371 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS
372 // The Mavericks malloc library changed in a way which breaks the tricks used
373 // to implement EnableTerminationOnOutOfMemory() with UncheckedMalloc() under
374 // 32-bit. The 64-bit malloc library works as desired without tricks.
375 if (base::mac::IsOSMavericksOrLater())
378 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize
, &value_
));
379 EXPECT_TRUE(value_
!= NULL
);
382 EXPECT_FALSE(base::UncheckedMalloc(test_size_
, &value_
));
383 EXPECT_TRUE(value_
== NULL
);
386 TEST_F(OutOfMemoryHandledTest
, UncheckedCalloc
) {
387 #if defined(OS_MACOSX) && ARCH_CPU_32_BITS
388 // The Mavericks malloc library changed in a way which breaks the tricks used
389 // to implement EnableTerminationOnOutOfMemory() with UncheckedCalloc() under
390 // 32-bit. The 64-bit malloc library works as desired without tricks.
391 if (base::mac::IsOSMavericksOrLater())
394 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize
, &value_
));
395 EXPECT_TRUE(value_
!= NULL
);
396 const char* bytes
= static_cast<const char*>(value_
);
397 for (size_t i
= 0; i
< kSafeMallocSize
; ++i
)
398 EXPECT_EQ(0, bytes
[i
]);
402 base::UncheckedCalloc(kSafeCallocItems
, kSafeCallocSize
, &value_
));
403 EXPECT_TRUE(value_
!= NULL
);
404 bytes
= static_cast<const char*>(value_
);
405 for (size_t i
= 0; i
< (kSafeCallocItems
* kSafeCallocSize
); ++i
)
406 EXPECT_EQ(0, bytes
[i
]);
409 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_
, &value_
));
410 EXPECT_TRUE(value_
== NULL
);
412 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
413 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN)