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/process/memory_unittest_mac.h"
31 // HeapQueryInformation function pointer.
32 typedef BOOL (WINAPI
* HeapQueryFn
) \
33 (HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
35 const int kConstantInModule
= 42;
37 TEST(ProcessMemoryTest
, GetModuleFromAddress
) {
38 // Since the unit tests are their own EXE, this should be
39 // equivalent to the EXE's HINSTANCE.
41 // kConstantInModule is a constant in this file and
42 // therefore within the unit test EXE.
43 EXPECT_EQ(::GetModuleHandle(NULL
),
44 base::GetModuleFromAddress(
45 const_cast<int*>(&kConstantInModule
)));
47 // Any address within the kernel32 module should return
48 // kernel32's HMODULE. Our only assumption here is that
49 // kernel32 is larger than 4 bytes.
50 HMODULE kernel32
= ::GetModuleHandle(L
"kernel32.dll");
51 HMODULE kernel32_from_address
=
52 base::GetModuleFromAddress(reinterpret_cast<DWORD
*>(kernel32
) + 1);
53 EXPECT_EQ(kernel32
, kernel32_from_address
);
56 TEST(ProcessMemoryTest
, EnableLFH
) {
57 ASSERT_TRUE(base::EnableLowFragmentationHeap());
58 if (IsDebuggerPresent()) {
59 // Under these conditions, LFH can't be enabled. There's no point to test
61 const char* no_debug_env
= getenv("_NO_DEBUG_HEAP");
62 if (!no_debug_env
|| strcmp(no_debug_env
, "1"))
65 HMODULE kernel32
= GetModuleHandle(L
"kernel32.dll");
66 ASSERT_TRUE(kernel32
!= NULL
);
67 HeapQueryFn heap_query
= reinterpret_cast<HeapQueryFn
>(GetProcAddress(
69 "HeapQueryInformation"));
71 // On Windows 2000, the function is not exported. This is not a reason to
72 // fail but we won't be able to retrieves information about the heap, so we
74 if (heap_query
== NULL
)
77 HANDLE heaps
[1024] = { 0 };
78 unsigned number_heaps
= GetProcessHeaps(1024, heaps
);
79 EXPECT_GT(number_heaps
, 0u);
80 for (unsigned i
= 0; i
< number_heaps
; ++i
) {
83 ASSERT_NE(0, heap_query(heaps
[i
],
84 HeapCompatibilityInformation
,
88 // If flag is 0, the heap is a standard heap that does not support
89 // look-asides. If flag is 1, the heap supports look-asides. If flag is 2,
90 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not
91 // supported on the LFH.
93 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag.
98 #endif // defined(OS_WIN)
100 #if defined(OS_MACOSX)
102 // For the following Mac tests:
103 // Note that base::EnableTerminationOnHeapCorruption() is called as part of
104 // test suite setup and does not need to be done again, else mach_override
107 #if !defined(ADDRESS_SANITIZER)
108 // The following code tests the system implementation of malloc() thus no need
109 // to test it under AddressSanitizer.
110 TEST(ProcessMemoryTest
, MacMallocFailureDoesNotTerminate
) {
111 // 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
113 // 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
115 // EnableTerminationOnOutOfMemory() for more information.
119 base::EnableTerminationOnOutOfMemory();
121 buf
= malloc(std::numeric_limits
<size_t>::max() - (2 * PAGE_SIZE
) - 1);
123 testing::KilledBySignal(SIGTRAP
),
124 "\\*\\*\\* error: can't allocate region.*\\n?.*");
126 base::debug::Alias(buf
);
128 #endif // !defined(ADDRESS_SANITIZER)
130 TEST(ProcessMemoryTest
, MacTerminateOnHeapCorruption
) {
131 // Assert that freeing an unallocated pointer will crash the process.
133 asm("" : "=r" (buf
)); // Prevent clang from being too smart.
135 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption
136 // but does not output anything.
137 ASSERT_DEATH(free(buf
), "");
138 #elif defined(ADDRESS_SANITIZER)
139 // AddressSanitizer replaces malloc() and prints a different error message on
141 ASSERT_DEATH(free(buf
), "attempting free on address which "
142 "was not malloc\\(\\)-ed");
144 ASSERT_DEATH(free(buf
), "being freed.*\\n?\\.*"
145 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*\\n?.*"
146 "Terminating process due to a potential for future heap corruption");
147 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER)
150 #endif // defined(OS_MACOSX)
152 // Android doesn't implement set_new_handler, so we can't use the
153 // OutOfMemoryTest cases.
154 // OpenBSD does not support these tests either.
155 // TODO(vandebo) make this work on Windows too.
156 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
159 #if defined(USE_TCMALLOC)
161 int tc_set_new_mode(int mode
);
163 #endif // defined(USE_TCMALLOC)
165 class OutOfMemoryTest
: public testing::Test
{
169 // Make test size as large as possible minus a few pages so
170 // that alignment or other rounding doesn't make it wrap.
171 test_size_(std::numeric_limits
<std::size_t>::max() - 12 * 1024),
172 signed_test_size_(std::numeric_limits
<ssize_t
>::max()) {
175 #if defined(USE_TCMALLOC)
176 virtual void SetUp() OVERRIDE
{
180 virtual void TearDown() OVERRIDE
{
183 #endif // defined(USE_TCMALLOC)
188 ssize_t signed_test_size_
;
191 class OutOfMemoryDeathTest
: public OutOfMemoryTest
{
193 void SetUpInDeathAssert() {
194 // Must call EnableTerminationOnOutOfMemory() because that is called from
195 // chrome's main function and therefore hasn't been called yet.
196 // Since this call may result in another thread being created and death
197 // tests shouldn't be started in a multithread environment, this call
198 // should be done inside of the ASSERT_DEATH.
199 base::EnableTerminationOnOutOfMemory();
203 TEST_F(OutOfMemoryDeathTest
, New
) {
205 SetUpInDeathAssert();
206 value_
= operator new(test_size_
);
210 TEST_F(OutOfMemoryDeathTest
, NewArray
) {
212 SetUpInDeathAssert();
213 value_
= new char[test_size_
];
217 TEST_F(OutOfMemoryDeathTest
, Malloc
) {
219 SetUpInDeathAssert();
220 value_
= malloc(test_size_
);
224 TEST_F(OutOfMemoryDeathTest
, Realloc
) {
226 SetUpInDeathAssert();
227 value_
= realloc(NULL
, test_size_
);
231 TEST_F(OutOfMemoryDeathTest
, Calloc
) {
233 SetUpInDeathAssert();
234 value_
= calloc(1024, test_size_
/ 1024L);
238 TEST_F(OutOfMemoryDeathTest
, Valloc
) {
240 SetUpInDeathAssert();
241 value_
= valloc(test_size_
);
245 #if defined(OS_LINUX)
247 #if PVALLOC_AVAILABLE == 1
248 TEST_F(OutOfMemoryDeathTest
, Pvalloc
) {
250 SetUpInDeathAssert();
251 value_
= pvalloc(test_size_
);
254 #endif // PVALLOC_AVAILABLE == 1
256 TEST_F(OutOfMemoryDeathTest
, Memalign
) {
258 SetUpInDeathAssert();
259 value_
= memalign(4, test_size_
);
263 TEST_F(OutOfMemoryDeathTest
, ViaSharedLibraries
) {
264 // This tests that the run-time symbol resolution is overriding malloc for
265 // shared libraries (including libc itself) as well as for our code.
266 std::string format
= base::StringPrintf("%%%zud", test_size_
);
269 SetUpInDeathAssert();
270 EXPECT_EQ(-1, asprintf(&value
, format
.c_str(), 0));
275 // Android doesn't implement posix_memalign().
276 #if defined(OS_POSIX) && !defined(OS_ANDROID)
277 TEST_F(OutOfMemoryDeathTest
, Posix_memalign
) {
278 // Grab the return value of posix_memalign to silence a compiler warning
279 // about unused return values. We don't actually care about the return
280 // value, since we're asserting death.
282 SetUpInDeathAssert();
283 EXPECT_EQ(ENOMEM
, posix_memalign(&value_
, 8, test_size_
));
286 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
288 #if defined(OS_MACOSX)
290 // Purgeable zone tests
292 TEST_F(OutOfMemoryDeathTest
, MallocPurgeable
) {
293 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
295 SetUpInDeathAssert();
296 value_
= malloc_zone_malloc(zone
, test_size_
);
300 TEST_F(OutOfMemoryDeathTest
, ReallocPurgeable
) {
301 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
303 SetUpInDeathAssert();
304 value_
= malloc_zone_realloc(zone
, NULL
, test_size_
);
308 TEST_F(OutOfMemoryDeathTest
, CallocPurgeable
) {
309 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
311 SetUpInDeathAssert();
312 value_
= malloc_zone_calloc(zone
, 1024, test_size_
/ 1024L);
316 TEST_F(OutOfMemoryDeathTest
, VallocPurgeable
) {
317 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
319 SetUpInDeathAssert();
320 value_
= malloc_zone_valloc(zone
, test_size_
);
324 TEST_F(OutOfMemoryDeathTest
, PosixMemalignPurgeable
) {
325 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
327 SetUpInDeathAssert();
328 value_
= malloc_zone_memalign(zone
, 8, test_size_
);
332 // Since these allocation functions take a signed size, it's possible that
333 // calling them just once won't be enough to exhaust memory. In the 32-bit
334 // environment, it's likely that these allocation attempts will fail because
335 // not enough contiguous address space is available. In the 64-bit environment,
336 // it's likely that they'll fail because they would require a preposterous
337 // amount of (virtual) memory.
339 TEST_F(OutOfMemoryDeathTest
, CFAllocatorSystemDefault
) {
341 SetUpInDeathAssert();
343 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_
))) {}
347 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMalloc
) {
349 SetUpInDeathAssert();
351 base::AllocateViaCFAllocatorMalloc(signed_test_size_
))) {}
355 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMallocZone
) {
357 SetUpInDeathAssert();
359 base::AllocateViaCFAllocatorMallocZone(signed_test_size_
))) {}
363 #if !defined(ARCH_CPU_64_BITS)
365 // See process_util_unittest_mac.mm for an explanation of why this test isn't
366 // run in the 64-bit environment.
368 TEST_F(OutOfMemoryDeathTest
, PsychoticallyBigObjCObject
) {
370 SetUpInDeathAssert();
371 while ((value_
= base::AllocatePsychoticallyBigObjCObject())) {}
375 #endif // !ARCH_CPU_64_BITS
378 class OutOfMemoryHandledTest
: public OutOfMemoryTest
{
380 static const size_t kSafeMallocSize
= 512;
381 static const size_t kSafeCallocSize
= 128;
382 static const size_t kSafeCallocItems
= 4;
384 virtual void SetUp() {
385 OutOfMemoryTest::SetUp();
387 // We enable termination on OOM - just as Chrome does at early
388 // initialization - and test that UncheckedMalloc and UncheckedCalloc
389 // properly by-pass this in order to allow the caller to handle OOM.
390 base::EnableTerminationOnOutOfMemory();
394 // TODO(b.kelemen): make UncheckedMalloc and UncheckedCalloc work
395 // on Windows as well.
396 // UncheckedMalloc() and UncheckedCalloc() work as regular malloc()/calloc()
397 // under sanitizer tools.
398 #if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
399 TEST_F(OutOfMemoryHandledTest
, UncheckedMalloc
) {
400 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize
, &value_
));
401 EXPECT_TRUE(value_
!= NULL
);
404 EXPECT_FALSE(base::UncheckedMalloc(test_size_
, &value_
));
405 EXPECT_TRUE(value_
== NULL
);
408 TEST_F(OutOfMemoryHandledTest
, UncheckedCalloc
) {
409 EXPECT_TRUE(base::UncheckedCalloc(1, kSafeMallocSize
, &value_
));
410 EXPECT_TRUE(value_
!= NULL
);
411 const char* bytes
= static_cast<const char*>(value_
);
412 for (size_t i
= 0; i
< kSafeMallocSize
; ++i
)
413 EXPECT_EQ(0, bytes
[i
]);
417 base::UncheckedCalloc(kSafeCallocItems
, kSafeCallocSize
, &value_
));
418 EXPECT_TRUE(value_
!= NULL
);
419 bytes
= static_cast<const char*>(value_
);
420 for (size_t i
= 0; i
< (kSafeCallocItems
* kSafeCallocSize
); ++i
)
421 EXPECT_EQ(0, bytes
[i
]);
424 EXPECT_FALSE(base::UncheckedCalloc(1, test_size_
, &value_
));
425 EXPECT_TRUE(value_
== NULL
);
427 #endif // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)
428 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && !defined(OS_WIN)