Enterprise policy: Ignore the deprecated ForceSafeSearch if ForceGoogleSafeSearch...
[chromium-blink-merge.git] / base / process / memory_unittest.cc
blob2e64fd7476602419181d3d0259cdbe90b1a1688a
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"
9 #include <limits>
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"
16 #if defined(OS_WIN)
17 #include <windows.h>
18 #endif
19 #if defined(OS_POSIX)
20 #include <errno.h>
21 #endif
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"
26 #endif
27 #if defined(OS_LINUX)
28 #include <malloc.h>
29 #include "base/test/malloc_wrapper.h"
30 #endif
32 #if defined(OS_WIN)
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
62 // anything.
63 const char* no_debug_env = getenv("_NO_DEBUG_HEAP");
64 if (!no_debug_env || strcmp(no_debug_env, "1"))
65 return;
67 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll");
68 ASSERT_TRUE(kernel32 != NULL);
69 HeapQueryFn heap_query = reinterpret_cast<HeapQueryFn>(GetProcAddress(
70 kernel32,
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
75 // should stop here.
76 if (heap_query == NULL)
77 return;
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.
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);
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
107 // will fail.
109 TEST(ProcessMemoryTest, MacTerminateOnHeapCorruption) {
110 // Assert that freeing an unallocated pointer will crash the process.
111 char buf[9];
112 asm("" : "=r" (buf)); // Prevent clang from being too smart.
113 #if ARCH_CPU_64_BITS
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
119 // heap corruption.
120 ASSERT_DEATH(free(buf), "attempting free on address which "
121 "was not malloc\\(\\)-ed");
122 #else
123 ADD_FAILURE() << "This test is not supported in this build configuration.";
124 #endif
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) && \
134 !defined(OS_WIN)
136 #if defined(USE_TCMALLOC)
137 extern "C" {
138 int tc_set_new_mode(int mode);
140 #endif // defined(USE_TCMALLOC)
142 class OutOfMemoryTest : public testing::Test {
143 public:
144 OutOfMemoryTest()
145 : value_(NULL),
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)
158 protected:
159 void* value_;
160 size_t test_size_;
161 ssize_t signed_test_size_;
164 class OutOfMemoryDeathTest : public OutOfMemoryTest {
165 public:
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) {
177 ASSERT_DEATH({
178 SetUpInDeathAssert();
179 value_ = operator new(test_size_);
180 }, "");
183 TEST_F(OutOfMemoryDeathTest, NewArray) {
184 ASSERT_DEATH({
185 SetUpInDeathAssert();
186 value_ = new char[test_size_];
187 }, "");
190 TEST_F(OutOfMemoryDeathTest, Malloc) {
191 ASSERT_DEATH({
192 SetUpInDeathAssert();
193 value_ = malloc(test_size_);
194 }, "");
197 TEST_F(OutOfMemoryDeathTest, Realloc) {
198 ASSERT_DEATH({
199 SetUpInDeathAssert();
200 value_ = realloc(NULL, test_size_);
201 }, "");
204 TEST_F(OutOfMemoryDeathTest, Calloc) {
205 ASSERT_DEATH({
206 SetUpInDeathAssert();
207 value_ = calloc(1024, test_size_ / 1024L);
208 }, "");
211 TEST_F(OutOfMemoryDeathTest, Valloc) {
212 ASSERT_DEATH({
213 SetUpInDeathAssert();
214 value_ = valloc(test_size_);
215 }, "");
218 #if defined(OS_LINUX)
220 #if PVALLOC_AVAILABLE == 1
221 TEST_F(OutOfMemoryDeathTest, Pvalloc) {
222 ASSERT_DEATH({
223 SetUpInDeathAssert();
224 value_ = pvalloc(test_size_);
225 }, "");
227 #endif // PVALLOC_AVAILABLE == 1
229 TEST_F(OutOfMemoryDeathTest, Memalign) {
230 ASSERT_DEATH({
231 SetUpInDeathAssert();
232 value_ = memalign(4, test_size_);
233 }, "");
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.
239 ASSERT_DEATH({
240 SetUpInDeathAssert();
241 value_ = MallocWrapper(test_size_);
242 }, "");
244 #endif // OS_LINUX
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.
252 ASSERT_DEATH({
253 SetUpInDeathAssert();
254 EXPECT_EQ(ENOMEM, posix_memalign(&value_, 8, test_size_));
255 }, "");
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();
265 ASSERT_DEATH({
266 SetUpInDeathAssert();
267 value_ = malloc_zone_malloc(zone, test_size_);
268 }, "");
271 TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) {
272 malloc_zone_t* zone = malloc_default_purgeable_zone();
273 ASSERT_DEATH({
274 SetUpInDeathAssert();
275 value_ = malloc_zone_realloc(zone, NULL, test_size_);
276 }, "");
279 TEST_F(OutOfMemoryDeathTest, CallocPurgeable) {
280 malloc_zone_t* zone = malloc_default_purgeable_zone();
281 ASSERT_DEATH({
282 SetUpInDeathAssert();
283 value_ = malloc_zone_calloc(zone, 1024, test_size_ / 1024L);
284 }, "");
287 TEST_F(OutOfMemoryDeathTest, VallocPurgeable) {
288 malloc_zone_t* zone = malloc_default_purgeable_zone();
289 ASSERT_DEATH({
290 SetUpInDeathAssert();
291 value_ = malloc_zone_valloc(zone, test_size_);
292 }, "");
295 TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) {
296 malloc_zone_t* zone = malloc_default_purgeable_zone();
297 ASSERT_DEATH({
298 SetUpInDeathAssert();
299 value_ = malloc_zone_memalign(zone, 8, test_size_);
300 }, "");
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) {
311 ASSERT_DEATH({
312 SetUpInDeathAssert();
313 while ((value_ =
314 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {}
315 }, "");
318 TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) {
319 ASSERT_DEATH({
320 SetUpInDeathAssert();
321 while ((value_ =
322 base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {}
323 }, "");
326 TEST_F(OutOfMemoryDeathTest, CFAllocatorMallocZone) {
327 ASSERT_DEATH({
328 SetUpInDeathAssert();
329 while ((value_ =
330 base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {}
331 }, "");
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) {
340 ASSERT_DEATH({
341 SetUpInDeathAssert();
342 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {}
343 }, "");
346 #endif // !ARCH_CPU_64_BITS
347 #endif // OS_MACOSX
349 class OutOfMemoryHandledTest : public OutOfMemoryTest {
350 public:
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())
376 return;
377 #endif
378 EXPECT_TRUE(base::UncheckedMalloc(kSafeMallocSize, &value_));
379 EXPECT_TRUE(value_ != NULL);
380 free(value_);
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())
392 return;
393 #endif
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]);
399 free(value_);
401 EXPECT_TRUE(
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]);
407 free(value_);
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)