| 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 <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include <sys/resource.h> | 37 #include <sys/resource.h> |
| 38 #include <sys/socket.h> | 38 #include <sys/socket.h> |
| 39 #include <sys/wait.h> | 39 #include <sys/wait.h> |
| 40 #endif | 40 #endif |
| 41 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
| 42 #include <windows.h> | 42 #include <windows.h> |
| 43 #endif | 43 #endif |
| 44 #if defined(OS_MACOSX) | 44 #if defined(OS_MACOSX) |
| 45 #include <mach/vm_param.h> | 45 #include <mach/vm_param.h> |
| 46 #include <malloc/malloc.h> | 46 #include <malloc/malloc.h> |
| 47 #include "base/process_util_unittest_mac.h" | |
| 48 #endif | 47 #endif |
| 49 | 48 |
| 50 using base::FilePath; | 49 using base::FilePath; |
| 51 | 50 |
| 52 namespace { | 51 namespace { |
| 53 | 52 |
| 54 #if defined(OS_WIN) | 53 #if defined(OS_WIN) |
| 55 const wchar_t kProcessName[] = L"base_unittests.exe"; | 54 const wchar_t kProcessName[] = L"base_unittests.exe"; |
| 56 #else | 55 #else |
| 57 const wchar_t kProcessName[] = L"base_unittests"; | 56 const wchar_t kProcessName[] = L"base_unittests"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 69 const char kSignalFileCrash[] = "CrashingChildProcess.die"; | 68 const char kSignalFileCrash[] = "CrashingChildProcess.die"; |
| 70 const char kSignalFileKill[] = "KilledChildProcess.die"; | 69 const char kSignalFileKill[] = "KilledChildProcess.die"; |
| 71 | 70 |
| 72 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
| 73 const int kExpectedStillRunningExitCode = 0x102; | 72 const int kExpectedStillRunningExitCode = 0x102; |
| 74 const int kExpectedKilledExitCode = 1; | 73 const int kExpectedKilledExitCode = 1; |
| 75 #else | 74 #else |
| 76 const int kExpectedStillRunningExitCode = 0; | 75 const int kExpectedStillRunningExitCode = 0; |
| 77 #endif | 76 #endif |
| 78 | 77 |
| 79 #if defined(OS_WIN) | |
| 80 // HeapQueryInformation function pointer. | |
| 81 typedef BOOL (WINAPI* HeapQueryFn) \ | |
| 82 (HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T, PSIZE_T); | |
| 83 #endif | |
| 84 | |
| 85 // Sleeps until file filename is created. | 78 // Sleeps until file filename is created. |
| 86 void WaitToDie(const char* filename) { | 79 void WaitToDie(const char* filename) { |
| 87 FILE* fp; | 80 FILE* fp; |
| 88 do { | 81 do { |
| 89 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 82 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
| 90 fp = fopen(filename, "r"); | 83 fp = fopen(filename, "r"); |
| 91 } while (!fp); | 84 } while (!fp); |
| 92 fclose(fp); | 85 fclose(fp); |
| 93 } | 86 } |
| 94 | 87 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 TEST_F(ProcessUtilTest, GetProcId) { | 199 TEST_F(ProcessUtilTest, GetProcId) { |
| 207 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); | 200 base::ProcessId id1 = base::GetProcId(GetCurrentProcess()); |
| 208 EXPECT_NE(0ul, id1); | 201 EXPECT_NE(0ul, id1); |
| 209 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); | 202 base::ProcessHandle handle = this->SpawnChild("SimpleChildProcess", false); |
| 210 ASSERT_NE(base::kNullProcessHandle, handle); | 203 ASSERT_NE(base::kNullProcessHandle, handle); |
| 211 base::ProcessId id2 = base::GetProcId(handle); | 204 base::ProcessId id2 = base::GetProcId(handle); |
| 212 EXPECT_NE(0ul, id2); | 205 EXPECT_NE(0ul, id2); |
| 213 EXPECT_NE(id1, id2); | 206 EXPECT_NE(id1, id2); |
| 214 base::CloseProcessHandle(handle); | 207 base::CloseProcessHandle(handle); |
| 215 } | 208 } |
| 216 | |
| 217 TEST_F(ProcessUtilTest, GetModuleFromAddress) { | |
| 218 // Since the unit tests are their own EXE, this should be | |
| 219 // equivalent to the EXE's HINSTANCE. | |
| 220 // | |
| 221 // kExpectedKilledExitCode is a constant in this file and | |
| 222 // therefore within the unit test EXE. | |
| 223 EXPECT_EQ(::GetModuleHandle(NULL), | |
| 224 base::GetModuleFromAddress( | |
| 225 const_cast<int*>(&kExpectedKilledExitCode))); | |
| 226 | |
| 227 // Any address within the kernel32 module should return | |
| 228 // kernel32's HMODULE. Our only assumption here is that | |
| 229 // kernel32 is larger than 4 bytes. | |
| 230 HMODULE kernel32 = ::GetModuleHandle(L"kernel32.dll"); | |
| 231 HMODULE kernel32_from_address = | |
| 232 base::GetModuleFromAddress(reinterpret_cast<DWORD*>(kernel32) + 1); | |
| 233 EXPECT_EQ(kernel32, kernel32_from_address); | |
| 234 } | |
| 235 #endif | 209 #endif |
| 236 | 210 |
| 237 #if !defined(OS_MACOSX) | 211 #if !defined(OS_MACOSX) |
| 238 // This test is disabled on Mac, since it's flaky due to ReportCrash | 212 // This test is disabled on Mac, since it's flaky due to ReportCrash |
| 239 // taking a variable amount of time to parse and load the debug and | 213 // taking a variable amount of time to parse and load the debug and |
| 240 // symbol data for this unit test's executable before firing the | 214 // symbol data for this unit test's executable before firing the |
| 241 // signal handler. | 215 // signal handler. |
| 242 // | 216 // |
| 243 // TODO(gspencer): turn this test process into a very small program | 217 // TODO(gspencer): turn this test process into a very small program |
| 244 // with no symbols (instead of using the multiprocess testing | 218 // with no symbols (instead of using the multiprocess testing |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 EXPECT_GT(info.shmem, 0); | 385 EXPECT_GT(info.shmem, 0); |
| 412 EXPECT_LT(info.shmem, info.total); | 386 EXPECT_LT(info.shmem, info.total); |
| 413 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects | 387 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects |
| 414 // and gem_size cannot be tested here. | 388 // and gem_size cannot be tested here. |
| 415 #endif | 389 #endif |
| 416 } | 390 } |
| 417 #endif // defined(OS_LINUX) || defined(OS_ANDROID) | 391 #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
| 418 | 392 |
| 419 // TODO(estade): if possible, port these 2 tests. | 393 // TODO(estade): if possible, port these 2 tests. |
| 420 #if defined(OS_WIN) | 394 #if defined(OS_WIN) |
| 421 TEST_F(ProcessUtilTest, EnableLFH) { | |
| 422 ASSERT_TRUE(base::EnableLowFragmentationHeap()); | |
| 423 if (IsDebuggerPresent()) { | |
| 424 // Under these conditions, LFH can't be enabled. There's no point to test | |
| 425 // anything. | |
| 426 const char* no_debug_env = getenv("_NO_DEBUG_HEAP"); | |
| 427 if (!no_debug_env || strcmp(no_debug_env, "1")) | |
| 428 return; | |
| 429 } | |
| 430 HMODULE kernel32 = GetModuleHandle(L"kernel32.dll"); | |
| 431 ASSERT_TRUE(kernel32 != NULL); | |
| 432 HeapQueryFn heap_query = reinterpret_cast<HeapQueryFn>(GetProcAddress( | |
| 433 kernel32, | |
| 434 "HeapQueryInformation")); | |
| 435 | |
| 436 // On Windows 2000, the function is not exported. This is not a reason to | |
| 437 // fail but we won't be able to retrieves information about the heap, so we | |
| 438 // should stop here. | |
| 439 if (heap_query == NULL) | |
| 440 return; | |
| 441 | |
| 442 HANDLE heaps[1024] = { 0 }; | |
| 443 unsigned number_heaps = GetProcessHeaps(1024, heaps); | |
| 444 EXPECT_GT(number_heaps, 0u); | |
| 445 for (unsigned i = 0; i < number_heaps; ++i) { | |
| 446 ULONG flag = 0; | |
| 447 SIZE_T length; | |
| 448 ASSERT_NE(0, heap_query(heaps[i], | |
| 449 HeapCompatibilityInformation, | |
| 450 &flag, | |
| 451 sizeof(flag), | |
| 452 &length)); | |
| 453 // If flag is 0, the heap is a standard heap that does not support | |
| 454 // look-asides. If flag is 1, the heap supports look-asides. If flag is 2, | |
| 455 // the heap is a low-fragmentation heap (LFH). Note that look-asides are not | |
| 456 // supported on the LFH. | |
| 457 | |
| 458 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag. | |
| 459 EXPECT_LE(flag, 2u); | |
| 460 EXPECT_NE(flag, 1u); | |
| 461 } | |
| 462 } | |
| 463 | |
| 464 TEST_F(ProcessUtilTest, CalcFreeMemory) { | 395 TEST_F(ProcessUtilTest, CalcFreeMemory) { |
| 465 scoped_ptr<base::ProcessMetrics> metrics( | 396 scoped_ptr<base::ProcessMetrics> metrics( |
| 466 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); | 397 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess())); |
| 467 ASSERT_TRUE(NULL != metrics.get()); | 398 ASSERT_TRUE(NULL != metrics.get()); |
| 468 | 399 |
| 469 bool using_tcmalloc = false; | 400 bool using_tcmalloc = false; |
| 470 | 401 |
| 471 // Detect if we are using tcmalloc | 402 // Detect if we are using tcmalloc |
| 472 #if !defined(NO_TCMALLOC) | 403 #if !defined(NO_TCMALLOC) |
| 473 const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); | 404 const char* chrome_allocator = getenv("CHROME_ALLOCATOR"); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); | 469 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &token)); |
| 539 std::wstring cmdline = | 470 std::wstring cmdline = |
| 540 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); | 471 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString(); |
| 541 base::LaunchOptions options; | 472 base::LaunchOptions options; |
| 542 options.as_user = token; | 473 options.as_user = token; |
| 543 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); | 474 EXPECT_TRUE(base::LaunchProcess(cmdline, options, NULL)); |
| 544 } | 475 } |
| 545 | 476 |
| 546 #endif // defined(OS_WIN) | 477 #endif // defined(OS_WIN) |
| 547 | 478 |
| 548 #if defined(OS_MACOSX) | |
| 549 | |
| 550 // For the following Mac tests: | |
| 551 // Note that base::EnableTerminationOnHeapCorruption() is called as part of | |
| 552 // test suite setup and does not need to be done again, else mach_override | |
| 553 // will fail. | |
| 554 | |
| 555 #if !defined(ADDRESS_SANITIZER) | |
| 556 // The following code tests the system implementation of malloc() thus no need | |
| 557 // to test it under AddressSanitizer. | |
| 558 TEST_F(ProcessUtilTest, MacMallocFailureDoesNotTerminate) { | |
| 559 // Test that ENOMEM doesn't crash via CrMallocErrorBreak two ways: the exit | |
| 560 // code and lack of the error string. The number of bytes is one less than | |
| 561 // MALLOC_ABSOLUTE_MAX_SIZE, more than which the system early-returns NULL and | |
| 562 // does not call through malloc_error_break(). See the comment at | |
| 563 // EnableTerminationOnOutOfMemory() for more information. | |
| 564 void* buf = NULL; | |
| 565 ASSERT_EXIT( | |
| 566 { | |
| 567 base::EnableTerminationOnOutOfMemory(); | |
| 568 | |
| 569 buf = malloc(std::numeric_limits<size_t>::max() - (2 * PAGE_SIZE) - 1); | |
| 570 }, | |
| 571 testing::KilledBySignal(SIGTRAP), | |
| 572 "\\*\\*\\* error: can't allocate region.*" | |
| 573 "(Terminating process due to a potential for future heap " | |
| 574 "corruption){0}"); | |
| 575 | |
| 576 base::debug::Alias(buf); | |
| 577 } | |
| 578 #endif // !defined(ADDRESS_SANITIZER) | |
| 579 | |
| 580 TEST_F(ProcessUtilTest, MacTerminateOnHeapCorruption) { | |
| 581 // Assert that freeing an unallocated pointer will crash the process. | |
| 582 char buf[3]; | |
| 583 asm("" : "=r" (buf)); // Prevent clang from being too smart. | |
| 584 #if ARCH_CPU_64_BITS | |
| 585 // On 64 bit Macs, the malloc system automatically abort()s on heap corruption | |
| 586 // but does not output anything. | |
| 587 ASSERT_DEATH(free(buf), ""); | |
| 588 #elif defined(ADDRESS_SANITIZER) | |
| 589 // AddressSanitizer replaces malloc() and prints a different error message on | |
| 590 // heap corruption. | |
| 591 ASSERT_DEATH(free(buf), "attempting free on address which " | |
| 592 "was not malloc\\(\\)-ed"); | |
| 593 #else | |
| 594 ASSERT_DEATH(free(buf), "being freed.*" | |
| 595 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*" | |
| 596 "Terminating process due to a potential for future heap corruption"); | |
| 597 #endif // ARCH_CPU_64_BITS || defined(ADDRESS_SANITIZER) | |
| 598 } | |
| 599 | |
| 600 #endif // defined(OS_MACOSX) | |
| 601 | |
| 602 #if defined(OS_POSIX) | 479 #if defined(OS_POSIX) |
| 603 | 480 |
| 604 namespace { | 481 namespace { |
| 605 | 482 |
| 606 // Returns the maximum number of files that a process can have open. | 483 // Returns the maximum number of files that a process can have open. |
| 607 // Returns 0 on error. | 484 // Returns 0 on error. |
| 608 int GetMaxFilesOpenInProcess() { | 485 int GetMaxFilesOpenInProcess() { |
| 609 struct rlimit rlim; | 486 struct rlimit rlim; |
| 610 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { | 487 if (getrlimit(RLIMIT_NOFILE, &rlim) != 0) { |
| 611 return 0; | 488 return 0; |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 // Check that process was really killed. | 948 // Check that process was really killed. |
| 1072 EXPECT_TRUE(IsProcessDead(child_process)); | 949 EXPECT_TRUE(IsProcessDead(child_process)); |
| 1073 base::CloseProcessHandle(child_process); | 950 base::CloseProcessHandle(child_process); |
| 1074 } | 951 } |
| 1075 | 952 |
| 1076 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { | 953 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately) { |
| 1077 return 0; | 954 return 0; |
| 1078 } | 955 } |
| 1079 | 956 |
| 1080 #endif // defined(OS_POSIX) | 957 #endif // defined(OS_POSIX) |
| 1081 | |
| 1082 // Android doesn't implement set_new_handler, so we can't use the | |
| 1083 // OutOfMemoryTest cases. | |
| 1084 // OpenBSD does not support these tests either. | |
| 1085 // AddressSanitizer and ThreadSanitizer define the malloc()/free()/etc. | |
| 1086 // functions so that they don't crash if the program is out of memory, so the | |
| 1087 // OOM tests aren't supposed to work. | |
| 1088 // TODO(vandebo) make this work on Windows too. | |
| 1089 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \ | |
| 1090 !defined(OS_WIN) && \ | |
| 1091 !defined(ADDRESS_SANITIZER) && !defined(THREAD_SANITIZER) | |
| 1092 | |
| 1093 #if defined(USE_TCMALLOC) | |
| 1094 extern "C" { | |
| 1095 int tc_set_new_mode(int mode); | |
| 1096 } | |
| 1097 #endif // defined(USE_TCMALLOC) | |
| 1098 | |
| 1099 class OutOfMemoryDeathTest : public testing::Test { | |
| 1100 public: | |
| 1101 OutOfMemoryDeathTest() | |
| 1102 : value_(NULL), | |
| 1103 // Make test size as large as possible minus a few pages so | |
| 1104 // that alignment or other rounding doesn't make it wrap. | |
| 1105 test_size_(std::numeric_limits<std::size_t>::max() - 12 * 1024), | |
| 1106 signed_test_size_(std::numeric_limits<ssize_t>::max()) { | |
| 1107 } | |
| 1108 | |
| 1109 #if defined(USE_TCMALLOC) | |
| 1110 virtual void SetUp() OVERRIDE { | |
| 1111 tc_set_new_mode(1); | |
| 1112 } | |
| 1113 | |
| 1114 virtual void TearDown() OVERRIDE { | |
| 1115 tc_set_new_mode(0); | |
| 1116 } | |
| 1117 #endif // defined(USE_TCMALLOC) | |
| 1118 | |
| 1119 void SetUpInDeathAssert() { | |
| 1120 // Must call EnableTerminationOnOutOfMemory() because that is called from | |
| 1121 // chrome's main function and therefore hasn't been called yet. | |
| 1122 // Since this call may result in another thread being created and death | |
| 1123 // tests shouldn't be started in a multithread environment, this call | |
| 1124 // should be done inside of the ASSERT_DEATH. | |
| 1125 base::EnableTerminationOnOutOfMemory(); | |
| 1126 } | |
| 1127 | |
| 1128 void* value_; | |
| 1129 size_t test_size_; | |
| 1130 ssize_t signed_test_size_; | |
| 1131 }; | |
| 1132 | |
| 1133 TEST_F(OutOfMemoryDeathTest, New) { | |
| 1134 ASSERT_DEATH({ | |
| 1135 SetUpInDeathAssert(); | |
| 1136 value_ = operator new(test_size_); | |
| 1137 }, ""); | |
| 1138 } | |
| 1139 | |
| 1140 TEST_F(OutOfMemoryDeathTest, NewArray) { | |
| 1141 ASSERT_DEATH({ | |
| 1142 SetUpInDeathAssert(); | |
| 1143 value_ = new char[test_size_]; | |
| 1144 }, ""); | |
| 1145 } | |
| 1146 | |
| 1147 TEST_F(OutOfMemoryDeathTest, Malloc) { | |
| 1148 ASSERT_DEATH({ | |
| 1149 SetUpInDeathAssert(); | |
| 1150 value_ = malloc(test_size_); | |
| 1151 }, ""); | |
| 1152 } | |
| 1153 | |
| 1154 TEST_F(OutOfMemoryDeathTest, Realloc) { | |
| 1155 ASSERT_DEATH({ | |
| 1156 SetUpInDeathAssert(); | |
| 1157 value_ = realloc(NULL, test_size_); | |
| 1158 }, ""); | |
| 1159 } | |
| 1160 | |
| 1161 TEST_F(OutOfMemoryDeathTest, Calloc) { | |
| 1162 ASSERT_DEATH({ | |
| 1163 SetUpInDeathAssert(); | |
| 1164 value_ = calloc(1024, test_size_ / 1024L); | |
| 1165 }, ""); | |
| 1166 } | |
| 1167 | |
| 1168 TEST_F(OutOfMemoryDeathTest, Valloc) { | |
| 1169 ASSERT_DEATH({ | |
| 1170 SetUpInDeathAssert(); | |
| 1171 value_ = valloc(test_size_); | |
| 1172 }, ""); | |
| 1173 } | |
| 1174 | |
| 1175 #if defined(OS_LINUX) | |
| 1176 TEST_F(OutOfMemoryDeathTest, Pvalloc) { | |
| 1177 ASSERT_DEATH({ | |
| 1178 SetUpInDeathAssert(); | |
| 1179 value_ = pvalloc(test_size_); | |
| 1180 }, ""); | |
| 1181 } | |
| 1182 | |
| 1183 TEST_F(OutOfMemoryDeathTest, Memalign) { | |
| 1184 ASSERT_DEATH({ | |
| 1185 SetUpInDeathAssert(); | |
| 1186 value_ = memalign(4, test_size_); | |
| 1187 }, ""); | |
| 1188 } | |
| 1189 | |
| 1190 TEST_F(OutOfMemoryDeathTest, ViaSharedLibraries) { | |
| 1191 // g_try_malloc is documented to return NULL on failure. (g_malloc is the | |
| 1192 // 'safe' default that crashes if allocation fails). However, since we have | |
| 1193 // hopefully overridden malloc, even g_try_malloc should fail. This tests | |
| 1194 // that the run-time symbol resolution is overriding malloc for shared | |
| 1195 // libraries as well as for our code. | |
| 1196 ASSERT_DEATH({ | |
| 1197 SetUpInDeathAssert(); | |
| 1198 value_ = g_try_malloc(test_size_); | |
| 1199 }, ""); | |
| 1200 } | |
| 1201 #endif // OS_LINUX | |
| 1202 | |
| 1203 // Android doesn't implement posix_memalign(). | |
| 1204 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 1205 TEST_F(OutOfMemoryDeathTest, Posix_memalign) { | |
| 1206 // Grab the return value of posix_memalign to silence a compiler warning | |
| 1207 // about unused return values. We don't actually care about the return | |
| 1208 // value, since we're asserting death. | |
| 1209 ASSERT_DEATH({ | |
| 1210 SetUpInDeathAssert(); | |
| 1211 EXPECT_EQ(ENOMEM, posix_memalign(&value_, 8, test_size_)); | |
| 1212 }, ""); | |
| 1213 } | |
| 1214 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | |
| 1215 | |
| 1216 #if defined(OS_MACOSX) | |
| 1217 | |
| 1218 // Purgeable zone tests | |
| 1219 | |
| 1220 TEST_F(OutOfMemoryDeathTest, MallocPurgeable) { | |
| 1221 malloc_zone_t* zone = malloc_default_purgeable_zone(); | |
| 1222 ASSERT_DEATH({ | |
| 1223 SetUpInDeathAssert(); | |
| 1224 value_ = malloc_zone_malloc(zone, test_size_); | |
| 1225 }, ""); | |
| 1226 } | |
| 1227 | |
| 1228 TEST_F(OutOfMemoryDeathTest, ReallocPurgeable) { | |
| 1229 malloc_zone_t* zone = malloc_default_purgeable_zone(); | |
| 1230 ASSERT_DEATH({ | |
| 1231 SetUpInDeathAssert(); | |
| 1232 value_ = malloc_zone_realloc(zone, NULL, test_size_); | |
| 1233 }, ""); | |
| 1234 } | |
| 1235 | |
| 1236 TEST_F(OutOfMemoryDeathTest, CallocPurgeable) { | |
| 1237 malloc_zone_t* zone = malloc_default_purgeable_zone(); | |
| 1238 ASSERT_DEATH({ | |
| 1239 SetUpInDeathAssert(); | |
| 1240 value_ = malloc_zone_calloc(zone, 1024, test_size_ / 1024L); | |
| 1241 }, ""); | |
| 1242 } | |
| 1243 | |
| 1244 TEST_F(OutOfMemoryDeathTest, VallocPurgeable) { | |
| 1245 malloc_zone_t* zone = malloc_default_purgeable_zone(); | |
| 1246 ASSERT_DEATH({ | |
| 1247 SetUpInDeathAssert(); | |
| 1248 value_ = malloc_zone_valloc(zone, test_size_); | |
| 1249 }, ""); | |
| 1250 } | |
| 1251 | |
| 1252 TEST_F(OutOfMemoryDeathTest, PosixMemalignPurgeable) { | |
| 1253 malloc_zone_t* zone = malloc_default_purgeable_zone(); | |
| 1254 ASSERT_DEATH({ | |
| 1255 SetUpInDeathAssert(); | |
| 1256 value_ = malloc_zone_memalign(zone, 8, test_size_); | |
| 1257 }, ""); | |
| 1258 } | |
| 1259 | |
| 1260 // Since these allocation functions take a signed size, it's possible that | |
| 1261 // calling them just once won't be enough to exhaust memory. In the 32-bit | |
| 1262 // environment, it's likely that these allocation attempts will fail because | |
| 1263 // not enough contiguous address space is available. In the 64-bit environment, | |
| 1264 // it's likely that they'll fail because they would require a preposterous | |
| 1265 // amount of (virtual) memory. | |
| 1266 | |
| 1267 TEST_F(OutOfMemoryDeathTest, CFAllocatorSystemDefault) { | |
| 1268 ASSERT_DEATH({ | |
| 1269 SetUpInDeathAssert(); | |
| 1270 while ((value_ = | |
| 1271 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_))) {} | |
| 1272 }, ""); | |
| 1273 } | |
| 1274 | |
| 1275 TEST_F(OutOfMemoryDeathTest, CFAllocatorMalloc) { | |
| 1276 ASSERT_DEATH({ | |
| 1277 SetUpInDeathAssert(); | |
| 1278 while ((value_ = | |
| 1279 base::AllocateViaCFAllocatorMalloc(signed_test_size_))) {} | |
| 1280 }, ""); | |
| 1281 } | |
| 1282 | |
| 1283 TEST_F(OutOfMemoryDeathTest, CFAllocatorMallocZone) { | |
| 1284 ASSERT_DEATH({ | |
| 1285 SetUpInDeathAssert(); | |
| 1286 while ((value_ = | |
| 1287 base::AllocateViaCFAllocatorMallocZone(signed_test_size_))) {} | |
| 1288 }, ""); | |
| 1289 } | |
| 1290 | |
| 1291 #if !defined(ARCH_CPU_64_BITS) | |
| 1292 | |
| 1293 // See process_util_unittest_mac.mm for an explanation of why this test isn't | |
| 1294 // run in the 64-bit environment. | |
| 1295 | |
| 1296 TEST_F(OutOfMemoryDeathTest, PsychoticallyBigObjCObject) { | |
| 1297 ASSERT_DEATH({ | |
| 1298 SetUpInDeathAssert(); | |
| 1299 while ((value_ = base::AllocatePsychoticallyBigObjCObject())) {} | |
| 1300 }, ""); | |
| 1301 } | |
| 1302 | |
| 1303 #endif // !ARCH_CPU_64_BITS | |
| 1304 #endif // OS_MACOSX | |
| 1305 | |
| 1306 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) && | |
| 1307 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER) | |
| OLD | NEW |