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
9 #include "base/command_line.h"
10 #include "base/debug/alias.h"
11 #include "base/debug/stack_trace.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/process_util.h"
18 #include "base/test/multiprocess_test.h"
19 #include "base/test/test_timeouts.h"
20 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
21 #include "base/threading/platform_thread.h"
22 #include "base/threading/thread.h"
23 #include "base/utf_string_conversions.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/multiprocess_func_list.h"
37 #include <sys/resource.h>
38 #include <sys/socket.h>
44 #if defined(OS_MACOSX)
45 #include <mach/vm_param.h>
46 #include <malloc/malloc.h>
47 #include "base/process_util_unittest_mac.h"
55 const wchar_t kProcessName
[] = L
"base_unittests.exe";
57 const wchar_t kProcessName
[] = L
"base_unittests";
58 #endif // defined(OS_WIN)
60 #if defined(OS_ANDROID)
61 const char kShellPath
[] = "/system/bin/sh";
62 const char kPosixShell
[] = "sh";
64 const char kShellPath
[] = "/bin/sh";
65 const char kPosixShell
[] = "bash";
68 const char kSignalFileSlow
[] = "SlowChildProcess.die";
69 const char kSignalFileCrash
[] = "CrashingChildProcess.die";
70 const char kSignalFileKill
[] = "KilledChildProcess.die";
73 const int kExpectedStillRunningExitCode
= 0x102;
74 const int kExpectedKilledExitCode
= 1;
76 const int kExpectedStillRunningExitCode
= 0;
80 // HeapQueryInformation function pointer.
81 typedef BOOL (WINAPI
* HeapQueryFn
) \
82 (HANDLE
, HEAP_INFORMATION_CLASS
, PVOID
, SIZE_T
, PSIZE_T
);
85 // Sleeps until file filename is created.
86 void WaitToDie(const char* filename
) {
89 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
90 fp
= fopen(filename
, "r");
95 // Signals children they should die now.
96 void SignalChildren(const char* filename
) {
97 FILE* fp
= fopen(filename
, "w");
101 // Using a pipe to the child to wait for an event was considered, but
102 // there were cases in the past where pipes caused problems (other
103 // libraries closing the fds, child deadlocking). This is a simple
104 // case, so it's not worth the risk. Using wait loops is discouraged
105 // in most instances.
106 base::TerminationStatus
WaitForChildTermination(base::ProcessHandle handle
,
108 // Now we wait until the result is something other than STILL_RUNNING.
109 base::TerminationStatus status
= base::TERMINATION_STATUS_STILL_RUNNING
;
110 const base::TimeDelta kInterval
= base::TimeDelta::FromMilliseconds(20);
111 base::TimeDelta waited
;
113 status
= base::GetTerminationStatus(handle
, exit_code
);
114 base::PlatformThread::Sleep(kInterval
);
116 } while (status
== base::TERMINATION_STATUS_STILL_RUNNING
&&
117 // Waiting for more time for process termination on android devices.
118 #if defined(OS_ANDROID)
119 waited
< TestTimeouts::large_test_timeout());
121 waited
< TestTimeouts::action_max_timeout());
129 class ProcessUtilTest
: public base::MultiProcessTest
{
131 #if defined(OS_POSIX)
132 // Spawn a child process that counts how many file descriptors are open.
133 int CountOpenFDsInChild();
135 // Converts the filename to a platform specific filepath.
136 // On Android files can not be created in arbitrary directories.
137 static std::string
GetSignalFilePath(const char* filename
);
140 std::string
ProcessUtilTest::GetSignalFilePath(const char* filename
) {
141 #if !defined(OS_ANDROID)
145 PathService::Get(base::DIR_CACHE
, &tmp_dir
);
146 tmp_dir
= tmp_dir
.Append(filename
);
147 return tmp_dir
.value();
151 MULTIPROCESS_TEST_MAIN(SimpleChildProcess
) {
155 TEST_F(ProcessUtilTest
, SpawnChild
) {
156 base::ProcessHandle handle
= this->SpawnChild("SimpleChildProcess", false);
157 ASSERT_NE(base::kNullProcessHandle
, handle
);
158 EXPECT_TRUE(base::WaitForSingleProcess(
159 handle
, TestTimeouts::action_max_timeout()));
160 base::CloseProcessHandle(handle
);
163 MULTIPROCESS_TEST_MAIN(SlowChildProcess
) {
164 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileSlow
).c_str());
168 TEST_F(ProcessUtilTest
, KillSlowChild
) {
169 const std::string signal_file
=
170 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow
);
171 remove(signal_file
.c_str());
172 base::ProcessHandle handle
= this->SpawnChild("SlowChildProcess", false);
173 ASSERT_NE(base::kNullProcessHandle
, handle
);
174 SignalChildren(signal_file
.c_str());
175 EXPECT_TRUE(base::WaitForSingleProcess(
176 handle
, TestTimeouts::action_max_timeout()));
177 base::CloseProcessHandle(handle
);
178 remove(signal_file
.c_str());
181 // Times out on Linux and Win, flakes on other platforms, http://crbug.com/95058
182 TEST_F(ProcessUtilTest
, DISABLED_GetTerminationStatusExit
) {
183 const std::string signal_file
=
184 ProcessUtilTest::GetSignalFilePath(kSignalFileSlow
);
185 remove(signal_file
.c_str());
186 base::ProcessHandle handle
= this->SpawnChild("SlowChildProcess", false);
187 ASSERT_NE(base::kNullProcessHandle
, handle
);
190 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING
,
191 base::GetTerminationStatus(handle
, &exit_code
));
192 EXPECT_EQ(kExpectedStillRunningExitCode
, exit_code
);
194 SignalChildren(signal_file
.c_str());
196 base::TerminationStatus status
=
197 WaitForChildTermination(handle
, &exit_code
);
198 EXPECT_EQ(base::TERMINATION_STATUS_NORMAL_TERMINATION
, status
);
199 EXPECT_EQ(0, exit_code
);
200 base::CloseProcessHandle(handle
);
201 remove(signal_file
.c_str());
205 // TODO(cpu): figure out how to test this in other platforms.
206 TEST_F(ProcessUtilTest
, GetProcId
) {
207 base::ProcessId id1
= base::GetProcId(GetCurrentProcess());
209 base::ProcessHandle handle
= this->SpawnChild("SimpleChildProcess", false);
210 ASSERT_NE(base::kNullProcessHandle
, handle
);
211 base::ProcessId id2
= base::GetProcId(handle
);
214 base::CloseProcessHandle(handle
);
217 TEST_F(ProcessUtilTest
, GetModuleFromAddress
) {
218 // Since the unit tests are their own EXE, this should be
219 // equivalent to the EXE's HINSTANCE.
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
)));
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
);
237 #if !defined(OS_MACOSX)
238 // 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
240 // symbol data for this unit test's executable before firing the
243 // TODO(gspencer): turn this test process into a very small program
244 // with no symbols (instead of using the multiprocess testing
245 // framework) to reduce the ReportCrash overhead.
247 MULTIPROCESS_TEST_MAIN(CrashingChildProcess
) {
248 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileCrash
).c_str());
249 #if defined(OS_POSIX)
250 // Have to disable to signal handler for segv so we can get a crash
251 // instead of an abnormal termination through the crash dump handler.
252 ::signal(SIGSEGV
, SIG_DFL
);
254 // Make this process have a segmentation fault.
255 volatile int* oops
= NULL
;
260 // This test intentionally crashes, so we don't need to run it under
262 // TODO(jschuh): crbug.com/175753 Fix this in Win64 bots.
263 #if defined(ADDRESS_SANITIZER) || (defined(OS_WIN) && defined(ARCH_CPU_X86_64))
264 #define MAYBE_GetTerminationStatusCrash DISABLED_GetTerminationStatusCrash
266 #define MAYBE_GetTerminationStatusCrash GetTerminationStatusCrash
268 TEST_F(ProcessUtilTest
, MAYBE_GetTerminationStatusCrash
) {
269 const std::string signal_file
=
270 ProcessUtilTest::GetSignalFilePath(kSignalFileCrash
);
271 remove(signal_file
.c_str());
272 base::ProcessHandle handle
= this->SpawnChild("CrashingChildProcess",
274 ASSERT_NE(base::kNullProcessHandle
, handle
);
277 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING
,
278 base::GetTerminationStatus(handle
, &exit_code
));
279 EXPECT_EQ(kExpectedStillRunningExitCode
, exit_code
);
281 SignalChildren(signal_file
.c_str());
283 base::TerminationStatus status
=
284 WaitForChildTermination(handle
, &exit_code
);
285 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_CRASHED
, status
);
288 EXPECT_EQ(0xc0000005, exit_code
);
289 #elif defined(OS_POSIX)
290 int signaled
= WIFSIGNALED(exit_code
);
291 EXPECT_NE(0, signaled
);
292 int signal
= WTERMSIG(exit_code
);
293 EXPECT_EQ(SIGSEGV
, signal
);
295 base::CloseProcessHandle(handle
);
297 // Reset signal handlers back to "normal".
298 base::debug::EnableInProcessStackDumping();
299 remove(signal_file
.c_str());
301 #endif // !defined(OS_MACOSX)
303 MULTIPROCESS_TEST_MAIN(KilledChildProcess
) {
304 WaitToDie(ProcessUtilTest::GetSignalFilePath(kSignalFileKill
).c_str());
307 HANDLE handle
= ::OpenProcess(PROCESS_ALL_ACCESS
, 0, ::GetCurrentProcessId());
308 ::TerminateProcess(handle
, kExpectedKilledExitCode
);
309 #elif defined(OS_POSIX)
310 // Send a SIGKILL to this process, just like the OOM killer would.
311 ::kill(getpid(), SIGKILL
);
316 TEST_F(ProcessUtilTest
, GetTerminationStatusKill
) {
317 const std::string signal_file
=
318 ProcessUtilTest::GetSignalFilePath(kSignalFileKill
);
319 remove(signal_file
.c_str());
320 base::ProcessHandle handle
= this->SpawnChild("KilledChildProcess",
322 ASSERT_NE(base::kNullProcessHandle
, handle
);
325 EXPECT_EQ(base::TERMINATION_STATUS_STILL_RUNNING
,
326 base::GetTerminationStatus(handle
, &exit_code
));
327 EXPECT_EQ(kExpectedStillRunningExitCode
, exit_code
);
329 SignalChildren(signal_file
.c_str());
331 base::TerminationStatus status
=
332 WaitForChildTermination(handle
, &exit_code
);
333 EXPECT_EQ(base::TERMINATION_STATUS_PROCESS_WAS_KILLED
, status
);
335 EXPECT_EQ(kExpectedKilledExitCode
, exit_code
);
336 #elif defined(OS_POSIX)
337 int signaled
= WIFSIGNALED(exit_code
);
338 EXPECT_NE(0, signaled
);
339 int signal
= WTERMSIG(exit_code
);
340 EXPECT_EQ(SIGKILL
, signal
);
342 base::CloseProcessHandle(handle
);
343 remove(signal_file
.c_str());
346 // Ensure that the priority of a process is restored correctly after
347 // backgrounding and restoring.
348 // Note: a platform may not be willing or able to lower the priority of
349 // a process. The calls to SetProcessBackground should be noops then.
350 TEST_F(ProcessUtilTest
, SetProcessBackgrounded
) {
351 base::ProcessHandle handle
= this->SpawnChild("SimpleChildProcess", false);
352 base::Process
process(handle
);
353 int old_priority
= process
.GetPriority();
355 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
356 EXPECT_TRUE(process
.IsProcessBackgrounded());
357 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
358 EXPECT_FALSE(process
.IsProcessBackgrounded());
360 process
.SetProcessBackgrounded(true);
361 process
.SetProcessBackgrounded(false);
363 int new_priority
= process
.GetPriority();
364 EXPECT_EQ(old_priority
, new_priority
);
367 // Same as SetProcessBackgrounded but to this very process. It uses
368 // a different code path at least for Windows.
369 TEST_F(ProcessUtilTest
, SetProcessBackgroundedSelf
) {
370 base::Process
process(base::Process::Current().handle());
371 int old_priority
= process
.GetPriority();
373 EXPECT_TRUE(process
.SetProcessBackgrounded(true));
374 EXPECT_TRUE(process
.IsProcessBackgrounded());
375 EXPECT_TRUE(process
.SetProcessBackgrounded(false));
376 EXPECT_FALSE(process
.IsProcessBackgrounded());
378 process
.SetProcessBackgrounded(true);
379 process
.SetProcessBackgrounded(false);
381 int new_priority
= process
.GetPriority();
382 EXPECT_EQ(old_priority
, new_priority
);
385 #if defined(OS_LINUX) || defined(OS_ANDROID)
386 TEST_F(ProcessUtilTest
, GetSystemMemoryInfo
) {
387 base::SystemMemoryInfoKB info
;
388 EXPECT_TRUE(base::GetSystemMemoryInfo(&info
));
390 // Ensure each field received a value.
391 EXPECT_GT(info
.total
, 0);
392 EXPECT_GT(info
.free
, 0);
393 EXPECT_GT(info
.buffers
, 0);
394 EXPECT_GT(info
.cached
, 0);
395 EXPECT_GT(info
.active_anon
, 0);
396 EXPECT_GT(info
.inactive_anon
, 0);
397 EXPECT_GT(info
.active_file
, 0);
398 EXPECT_GT(info
.inactive_file
, 0);
400 // All the values should be less than the total amount of memory.
401 EXPECT_LT(info
.free
, info
.total
);
402 EXPECT_LT(info
.buffers
, info
.total
);
403 EXPECT_LT(info
.cached
, info
.total
);
404 EXPECT_LT(info
.active_anon
, info
.total
);
405 EXPECT_LT(info
.inactive_anon
, info
.total
);
406 EXPECT_LT(info
.active_file
, info
.total
);
407 EXPECT_LT(info
.inactive_file
, info
.total
);
409 #if defined(OS_CHROMEOS)
410 // Chrome OS exposes shmem.
411 EXPECT_GT(info
.shmem
, 0);
412 EXPECT_LT(info
.shmem
, info
.total
);
413 // Chrome unit tests are not run on actual Chrome OS hardware, so gem_objects
414 // and gem_size cannot be tested here.
417 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
419 // TODO(estade): if possible, port these 2 tests.
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
426 const char* no_debug_env
= getenv("_NO_DEBUG_HEAP");
427 if (!no_debug_env
|| strcmp(no_debug_env
, "1"))
430 HMODULE kernel32
= GetModuleHandle(L
"kernel32.dll");
431 ASSERT_TRUE(kernel32
!= NULL
);
432 HeapQueryFn heap_query
= reinterpret_cast<HeapQueryFn
>(GetProcAddress(
434 "HeapQueryInformation"));
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
439 if (heap_query
== NULL
)
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
) {
448 ASSERT_NE(0, heap_query(heaps
[i
],
449 HeapCompatibilityInformation
,
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.
458 // We don't have any documented way of querying the HEAP_NO_SERIALIZE flag.
464 TEST_F(ProcessUtilTest
, CalcFreeMemory
) {
465 scoped_ptr
<base::ProcessMetrics
> metrics(
466 base::ProcessMetrics::CreateProcessMetrics(::GetCurrentProcess()));
467 ASSERT_TRUE(NULL
!= metrics
.get());
469 // Typical values here is ~1900 for total and ~1000 for largest. Obviously
470 // it depends in what other tests have done to this process.
471 base::FreeMBytes free_mem1
= {0};
472 EXPECT_TRUE(metrics
->CalculateFreeMemory(&free_mem1
));
473 EXPECT_LT(10u, free_mem1
.total
);
474 EXPECT_LT(10u, free_mem1
.largest
);
475 EXPECT_GT(2048u, free_mem1
.total
);
476 EXPECT_GT(2048u, free_mem1
.largest
);
477 EXPECT_GE(free_mem1
.total
, free_mem1
.largest
);
478 EXPECT_TRUE(NULL
!= free_mem1
.largest_ptr
);
480 // Allocate 20M and check again. It should have gone down.
481 const int kAllocMB
= 20;
482 scoped_ptr
<char[]> alloc(new char[kAllocMB
* 1024 * 1024]);
483 size_t expected_total
= free_mem1
.total
- kAllocMB
;
484 size_t expected_largest
= free_mem1
.largest
;
486 base::FreeMBytes free_mem2
= {0};
487 EXPECT_TRUE(metrics
->CalculateFreeMemory(&free_mem2
));
488 EXPECT_GE(free_mem2
.total
, free_mem2
.largest
);
489 EXPECT_GE(expected_total
, free_mem2
.total
);
490 EXPECT_GE(expected_largest
, free_mem2
.largest
);
491 EXPECT_TRUE(NULL
!= free_mem2
.largest_ptr
);
494 TEST_F(ProcessUtilTest
, GetAppOutput
) {
495 // Let's create a decently long message.
497 for (int i
= 0; i
< 1025; i
++) { // 1025 so it does not end on a kilo-byte
501 // cmd.exe's echo always adds a \r\n to its output.
502 std::string
expected(message
);
505 FilePath
cmd(L
"cmd.exe");
506 CommandLine
cmd_line(cmd
);
507 cmd_line
.AppendArg("/c");
508 cmd_line
.AppendArg("echo " + message
+ "");
510 ASSERT_TRUE(base::GetAppOutput(cmd_line
, &output
));
511 EXPECT_EQ(expected
, output
);
513 // Let's make sure stderr is ignored.
514 CommandLine
other_cmd_line(cmd
);
515 other_cmd_line
.AppendArg("/c");
516 // http://msdn.microsoft.com/library/cc772622.aspx
517 cmd_line
.AppendArg("echo " + message
+ " >&2");
519 ASSERT_TRUE(base::GetAppOutput(other_cmd_line
, &output
));
520 EXPECT_EQ("", output
);
523 TEST_F(ProcessUtilTest
, LaunchAsUser
) {
524 base::UserTokenHandle token
;
525 ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS
, &token
));
526 std::wstring cmdline
=
527 this->MakeCmdLine("SimpleChildProcess", false).GetCommandLineString();
528 base::LaunchOptions options
;
529 options
.as_user
= token
;
530 EXPECT_TRUE(base::LaunchProcess(cmdline
, options
, NULL
));
533 #endif // defined(OS_WIN)
535 #if defined(OS_MACOSX)
537 // For the following Mac tests:
538 // Note that base::EnableTerminationOnHeapCorruption() is called as part of
539 // test suite setup and does not need to be done again, else mach_override
542 #if !defined(ADDRESS_SANITIZER)
543 // The following code tests the system implementation of malloc() thus no need
544 // to test it under AddressSanitizer.
545 TEST_F(ProcessUtilTest
, MacMallocFailureDoesNotTerminate
) {
546 // Install the OOM killer.
547 base::EnableTerminationOnOutOfMemory();
549 // Test that ENOMEM doesn't crash via CrMallocErrorBreak two ways: the exit
550 // code and lack of the error string. The number of bytes is one less than
551 // MALLOC_ABSOLUTE_MAX_SIZE, more than which the system early-returns NULL and
552 // does not call through malloc_error_break(). See the comment at
553 // EnableTerminationOnOutOfMemory() for more information.
556 buf
= malloc(std::numeric_limits
<size_t>::max() - (2 * PAGE_SIZE
) - 1),
557 testing::KilledBySignal(SIGTRAP
),
558 "\\*\\*\\* error: can't allocate region.*"
559 "(Terminating process due to a potential for future heap "
562 base::debug::Alias(buf
);
564 #endif // !defined(ADDRESS_SANITIZER)
566 TEST_F(ProcessUtilTest
, MacTerminateOnHeapCorruption
) {
567 // Assert that freeing an unallocated pointer will crash the process.
569 asm("" : "=r" (buf
)); // Prevent clang from being too smart.
570 #if !defined(ADDRESS_SANITIZER)
571 ASSERT_DEATH(free(buf
), "being freed.*"
572 "\\*\\*\\* set a breakpoint in malloc_error_break to debug.*"
573 "Terminating process due to a potential for future heap corruption");
575 // AddressSanitizer replaces malloc() and prints a different error message on
577 ASSERT_DEATH(free(buf
), "attempting free on address which "
578 "was not malloc\\(\\)-ed");
579 #endif // !defined(ADDRESS_SANITIZER)
582 #endif // defined(OS_MACOSX)
584 #if defined(OS_POSIX)
588 // Returns the maximum number of files that a process can have open.
589 // Returns 0 on error.
590 int GetMaxFilesOpenInProcess() {
592 if (getrlimit(RLIMIT_NOFILE
, &rlim
) != 0) {
596 // rlim_t is a uint64 - clip to maxint. We do this since FD #s are ints
597 // which are all 32 bits on the supported platforms.
598 rlim_t max_int
= static_cast<rlim_t
>(std::numeric_limits
<int32
>::max());
599 if (rlim
.rlim_cur
> max_int
) {
603 return rlim
.rlim_cur
;
606 const int kChildPipe
= 20; // FD # for write end of pipe in child process.
610 MULTIPROCESS_TEST_MAIN(ProcessUtilsLeakFDChildProcess
) {
611 // This child process counts the number of open FDs, it then writes that
612 // number out to a pipe connected to the parent.
613 int num_open_files
= 0;
614 int write_pipe
= kChildPipe
;
615 int max_files
= GetMaxFilesOpenInProcess();
616 for (int i
= STDERR_FILENO
+ 1; i
< max_files
; i
++) {
617 if (i
!= kChildPipe
) {
619 if ((fd
= HANDLE_EINTR(dup(i
))) != -1) {
626 int written
= HANDLE_EINTR(write(write_pipe
, &num_open_files
,
627 sizeof(num_open_files
)));
628 DCHECK_EQ(static_cast<size_t>(written
), sizeof(num_open_files
));
629 int ret
= HANDLE_EINTR(close(write_pipe
));
635 int ProcessUtilTest::CountOpenFDsInChild() {
640 base::FileHandleMappingVector fd_mapping_vec
;
641 fd_mapping_vec
.push_back(std::pair
<int, int>(fds
[1], kChildPipe
));
642 base::ProcessHandle handle
= this->SpawnChild(
643 "ProcessUtilsLeakFDChildProcess", fd_mapping_vec
, false);
645 int ret
= HANDLE_EINTR(close(fds
[1]));
648 // Read number of open files in client process from pipe;
649 int num_open_files
= -1;
651 HANDLE_EINTR(read(fds
[0], &num_open_files
, sizeof(num_open_files
)));
652 CHECK_EQ(bytes_read
, static_cast<ssize_t
>(sizeof(num_open_files
)));
654 #if defined(THREAD_SANITIZER) || defined(USE_HEAPCHECKER)
655 // Compiler-based ThreadSanitizer makes this test slow.
656 CHECK(base::WaitForSingleProcess(handle
, base::TimeDelta::FromSeconds(3)));
658 CHECK(base::WaitForSingleProcess(handle
, base::TimeDelta::FromSeconds(1)));
660 base::CloseProcessHandle(handle
);
661 ret
= HANDLE_EINTR(close(fds
[0]));
664 return num_open_files
;
667 TEST_F(ProcessUtilTest
, FDRemapping
) {
668 int fds_before
= CountOpenFDsInChild();
670 // open some dummy fds to make sure they don't propagate over to the
672 int dev_null
= open("/dev/null", O_RDONLY
);
674 socketpair(AF_UNIX
, SOCK_STREAM
, 0, sockets
);
676 int fds_after
= CountOpenFDsInChild();
678 ASSERT_EQ(fds_after
, fds_before
);
681 ret
= HANDLE_EINTR(close(sockets
[0]));
683 ret
= HANDLE_EINTR(close(sockets
[1]));
685 ret
= HANDLE_EINTR(close(dev_null
));
691 std::string
TestLaunchProcess(const base::EnvironmentVector
& env_changes
,
692 const int clone_flags
) {
693 std::vector
<std::string
> args
;
694 base::FileHandleMappingVector fds_to_remap
;
696 args
.push_back(kPosixShell
);
697 args
.push_back("-c");
698 args
.push_back("echo $BASE_TEST");
701 PCHECK(pipe(fds
) == 0);
703 fds_to_remap
.push_back(std::make_pair(fds
[1], 1));
704 base::LaunchOptions options
;
706 options
.environ
= &env_changes
;
707 options
.fds_to_remap
= &fds_to_remap
;
708 #if defined(OS_LINUX)
709 options
.clone_flags
= clone_flags
;
711 CHECK_EQ(0, clone_flags
);
713 EXPECT_TRUE(base::LaunchProcess(args
, options
, NULL
));
714 PCHECK(HANDLE_EINTR(close(fds
[1])) == 0);
717 const ssize_t n
= HANDLE_EINTR(read(fds
[0], buf
, sizeof(buf
)));
720 PCHECK(HANDLE_EINTR(close(fds
[0])) == 0);
722 return std::string(buf
, n
);
725 const char kLargeString
[] =
726 "0123456789012345678901234567890123456789012345678901234567890123456789"
727 "0123456789012345678901234567890123456789012345678901234567890123456789"
728 "0123456789012345678901234567890123456789012345678901234567890123456789"
729 "0123456789012345678901234567890123456789012345678901234567890123456789"
730 "0123456789012345678901234567890123456789012345678901234567890123456789"
731 "0123456789012345678901234567890123456789012345678901234567890123456789"
732 "0123456789012345678901234567890123456789012345678901234567890123456789";
736 TEST_F(ProcessUtilTest
, LaunchProcess
) {
737 base::EnvironmentVector env_changes
;
738 const int no_clone_flags
= 0;
740 env_changes
.push_back(std::make_pair(std::string("BASE_TEST"),
741 std::string("bar")));
742 EXPECT_EQ("bar\n", TestLaunchProcess(env_changes
, no_clone_flags
));
745 EXPECT_EQ(0, setenv("BASE_TEST", "testing", 1 /* override */));
746 EXPECT_EQ("testing\n", TestLaunchProcess(env_changes
, no_clone_flags
));
748 env_changes
.push_back(std::make_pair(std::string("BASE_TEST"),
750 EXPECT_EQ("\n", TestLaunchProcess(env_changes
, no_clone_flags
));
752 env_changes
[0].second
= "foo";
753 EXPECT_EQ("foo\n", TestLaunchProcess(env_changes
, no_clone_flags
));
756 EXPECT_EQ(0, setenv("BASE_TEST", kLargeString
, 1 /* override */));
757 EXPECT_EQ(std::string(kLargeString
) + "\n",
758 TestLaunchProcess(env_changes
, no_clone_flags
));
760 env_changes
.push_back(std::make_pair(std::string("BASE_TEST"),
761 std::string("wibble")));
762 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes
, no_clone_flags
));
764 #if defined(OS_LINUX)
765 // Test a non-trival value for clone_flags.
766 // Don't test on Valgrind as it has limited support for clone().
767 if (!RunningOnValgrind()) {
768 EXPECT_EQ("wibble\n", TestLaunchProcess(env_changes
, CLONE_FS
| SIGCHLD
));
773 TEST_F(ProcessUtilTest
, AlterEnvironment
) {
774 const char* const empty
[] = { NULL
};
775 const char* const a2
[] = { "A=2", NULL
};
776 base::EnvironmentVector changes
;
779 e
= base::AlterEnvironment(changes
, empty
);
780 EXPECT_TRUE(e
[0] == NULL
);
783 changes
.push_back(std::make_pair(std::string("A"), std::string("1")));
784 e
= base::AlterEnvironment(changes
, empty
);
785 EXPECT_EQ(std::string("A=1"), e
[0]);
786 EXPECT_TRUE(e
[1] == NULL
);
790 changes
.push_back(std::make_pair(std::string("A"), std::string("")));
791 e
= base::AlterEnvironment(changes
, empty
);
792 EXPECT_TRUE(e
[0] == NULL
);
796 e
= base::AlterEnvironment(changes
, a2
);
797 EXPECT_EQ(std::string("A=2"), e
[0]);
798 EXPECT_TRUE(e
[1] == NULL
);
802 changes
.push_back(std::make_pair(std::string("A"), std::string("1")));
803 e
= base::AlterEnvironment(changes
, a2
);
804 EXPECT_EQ(std::string("A=1"), e
[0]);
805 EXPECT_TRUE(e
[1] == NULL
);
809 changes
.push_back(std::make_pair(std::string("A"), std::string("")));
810 e
= base::AlterEnvironment(changes
, a2
);
811 EXPECT_TRUE(e
[0] == NULL
);
815 TEST_F(ProcessUtilTest
, GetAppOutput
) {
818 #if defined(OS_ANDROID)
819 std::vector
<std::string
> argv
;
820 argv
.push_back("sh"); // Instead of /bin/sh, force path search to find it.
821 argv
.push_back("-c");
823 argv
.push_back("exit 0");
824 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv
), &output
));
825 EXPECT_STREQ("", output
.c_str());
828 EXPECT_FALSE(base::GetAppOutput(CommandLine(argv
), &output
));
829 EXPECT_STREQ("", output
.c_str());
831 argv
[2] = "echo foobar42";
832 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv
), &output
));
833 EXPECT_STREQ("foobar42\n", output
.c_str());
835 EXPECT_TRUE(base::GetAppOutput(CommandLine(FilePath("true")), &output
));
836 EXPECT_STREQ("", output
.c_str());
838 EXPECT_FALSE(base::GetAppOutput(CommandLine(FilePath("false")), &output
));
840 std::vector
<std::string
> argv
;
841 argv
.push_back("/bin/echo");
842 argv
.push_back("-n");
843 argv
.push_back("foobar42");
844 EXPECT_TRUE(base::GetAppOutput(CommandLine(argv
), &output
));
845 EXPECT_STREQ("foobar42", output
.c_str());
846 #endif // defined(OS_ANDROID)
849 TEST_F(ProcessUtilTest
, GetAppOutputRestricted
) {
850 // Unfortunately, since we can't rely on the path, we need to know where
851 // everything is. So let's use /bin/sh, which is on every POSIX system, and
853 std::vector
<std::string
> argv
;
854 argv
.push_back(std::string(kShellPath
)); // argv[0]
855 argv
.push_back("-c"); // argv[1]
857 // On success, should set |output|. We use |/bin/sh -c 'exit 0'| instead of
858 // |true| since the location of the latter may be |/bin| or |/usr/bin| (and we
859 // need absolute paths).
860 argv
.push_back("exit 0"); // argv[2]; equivalent to "true"
861 std::string output
= "abc";
862 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 100));
863 EXPECT_STREQ("", output
.c_str());
865 argv
[2] = "exit 1"; // equivalent to "false"
867 EXPECT_FALSE(base::GetAppOutputRestricted(CommandLine(argv
),
869 EXPECT_STREQ("", output
.c_str());
871 // Amount of output exactly equal to space allowed.
872 argv
[2] = "echo 123456789"; // (the sh built-in doesn't take "-n")
874 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 10));
875 EXPECT_STREQ("123456789\n", output
.c_str());
877 // Amount of output greater than space allowed.
879 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 5));
880 EXPECT_STREQ("12345", output
.c_str());
882 // Amount of output less than space allowed.
884 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 15));
885 EXPECT_STREQ("123456789\n", output
.c_str());
887 // Zero space allowed.
889 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 0));
890 EXPECT_STREQ("", output
.c_str());
893 #if !defined(OS_MACOSX) && !defined(OS_OPENBSD)
894 // TODO(benwells): GetAppOutputRestricted should terminate applications
895 // with SIGPIPE when we have enough output. http://crbug.com/88502
896 TEST_F(ProcessUtilTest
, GetAppOutputRestrictedSIGPIPE
) {
897 std::vector
<std::string
> argv
;
900 argv
.push_back(std::string(kShellPath
)); // argv[0]
901 argv
.push_back("-c");
902 #if defined(OS_ANDROID)
903 argv
.push_back("while echo 12345678901234567890; do :; done");
904 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 10));
905 EXPECT_STREQ("1234567890", output
.c_str());
907 argv
.push_back("yes");
908 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 10));
909 EXPECT_STREQ("y\ny\ny\ny\ny\n", output
.c_str());
914 TEST_F(ProcessUtilTest
, GetAppOutputRestrictedNoZombies
) {
915 std::vector
<std::string
> argv
;
917 argv
.push_back(std::string(kShellPath
)); // argv[0]
918 argv
.push_back("-c"); // argv[1]
919 argv
.push_back("echo 123456789012345678901234567890"); // argv[2]
921 // Run |GetAppOutputRestricted()| 300 (> default per-user processes on Mac OS
922 // 10.5) times with an output buffer big enough to capture all output.
923 for (int i
= 0; i
< 300; i
++) {
925 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 100));
926 EXPECT_STREQ("123456789012345678901234567890\n", output
.c_str());
929 // Ditto, but with an output buffer too small to capture all output.
930 for (int i
= 0; i
< 300; i
++) {
932 EXPECT_TRUE(base::GetAppOutputRestricted(CommandLine(argv
), &output
, 10));
933 EXPECT_STREQ("1234567890", output
.c_str());
937 TEST_F(ProcessUtilTest
, GetAppOutputWithExitCode
) {
938 // Test getting output from a successful application.
939 std::vector
<std::string
> argv
;
942 argv
.push_back(std::string(kShellPath
)); // argv[0]
943 argv
.push_back("-c"); // argv[1]
944 argv
.push_back("echo foo"); // argv[2];
945 EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv
), &output
,
947 EXPECT_STREQ("foo\n", output
.c_str());
948 EXPECT_EQ(exit_code
, 0);
950 // Test getting output from an application which fails with a specific exit
953 argv
[2] = "echo foo; exit 2";
954 EXPECT_TRUE(base::GetAppOutputWithExitCode(CommandLine(argv
), &output
,
956 EXPECT_STREQ("foo\n", output
.c_str());
957 EXPECT_EQ(exit_code
, 2);
960 TEST_F(ProcessUtilTest
, GetParentProcessId
) {
961 base::ProcessId ppid
= base::GetParentProcessId(base::GetCurrentProcId());
962 EXPECT_EQ(ppid
, getppid());
965 #if defined(OS_LINUX) || defined(OS_ANDROID)
966 TEST_F(ProcessUtilTest
, ParseProcStatCPU
) {
967 // /proc/self/stat for a process running "top".
968 const char kTopStat
[] = "960 (top) S 16230 960 16230 34818 960 "
970 "12 16 0 0 " // <- These are the goods.
971 "20 0 1 0 121946157 15077376 314 18446744073709551615 4194304 "
972 "4246868 140733983044336 18446744073709551615 140244213071219 "
973 "0 0 0 138047495 0 0 0 17 1 0 0 0 0 0";
974 EXPECT_EQ(12 + 16, base::ParseProcStatCPU(kTopStat
));
976 // cat /proc/self/stat on a random other machine I have.
977 const char kSelfStat
[] = "5364 (cat) R 5354 5364 5354 34819 5364 "
979 "0 0 0 0 " // <- No CPU, apparently.
980 "16 0 1 0 1676099790 2957312 114 4294967295 134512640 134528148 "
981 "3221224832 3221224344 3086339742 0 0 0 0 0 0 0 17 0 0 0";
983 EXPECT_EQ(0, base::ParseProcStatCPU(kSelfStat
));
986 // Disable on Android because base_unittests runs inside a Dalvik VM that
987 // starts and stop threads (crbug.com/175563).
988 #if !defined(OS_ANDROID)
989 TEST_F(ProcessUtilTest
, GetNumberOfThreads
) {
990 const base::ProcessHandle current
= base::GetCurrentProcessHandle();
991 const int initial_threads
= base::GetNumberOfThreads(current
);
992 ASSERT_GT(initial_threads
, 0);
993 const int kNumAdditionalThreads
= 10;
995 scoped_ptr
<base::Thread
> my_threads
[kNumAdditionalThreads
];
996 for (int i
= 0; i
< kNumAdditionalThreads
; ++i
) {
997 my_threads
[i
].reset(new base::Thread("GetNumberOfThreadsTest"));
998 my_threads
[i
]->Start();
999 ASSERT_EQ(base::GetNumberOfThreads(current
), initial_threads
+ 1 + i
);
1002 // The Thread destructor will stop them.
1003 ASSERT_EQ(initial_threads
, base::GetNumberOfThreads(current
));
1005 #endif // !defined(OS_ANDROID)
1007 #endif // defined(OS_LINUX) || defined(OS_ANDROID)
1009 // TODO(port): port those unit tests.
1010 bool IsProcessDead(base::ProcessHandle child
) {
1011 // waitpid() will actually reap the process which is exactly NOT what we
1012 // want to test for. The good thing is that if it can't find the process
1013 // we'll get a nice value for errno which we can test for.
1014 const pid_t result
= HANDLE_EINTR(waitpid(child
, NULL
, WNOHANG
));
1015 return result
== -1 && errno
== ECHILD
;
1018 TEST_F(ProcessUtilTest
, DelayedTermination
) {
1019 base::ProcessHandle child_process
=
1020 SpawnChild("process_util_test_never_die", false);
1021 ASSERT_TRUE(child_process
);
1022 base::EnsureProcessTerminated(child_process
);
1023 base::WaitForSingleProcess(child_process
, base::TimeDelta::FromSeconds(5));
1025 // Check that process was really killed.
1026 EXPECT_TRUE(IsProcessDead(child_process
));
1027 base::CloseProcessHandle(child_process
);
1030 MULTIPROCESS_TEST_MAIN(process_util_test_never_die
) {
1037 TEST_F(ProcessUtilTest
, ImmediateTermination
) {
1038 base::ProcessHandle child_process
=
1039 SpawnChild("process_util_test_die_immediately", false);
1040 ASSERT_TRUE(child_process
);
1041 // Give it time to die.
1043 base::EnsureProcessTerminated(child_process
);
1045 // Check that process was really killed.
1046 EXPECT_TRUE(IsProcessDead(child_process
));
1047 base::CloseProcessHandle(child_process
);
1050 MULTIPROCESS_TEST_MAIN(process_util_test_die_immediately
) {
1054 #endif // defined(OS_POSIX)
1056 // Android doesn't implement set_new_handler, so we can't use the
1057 // OutOfMemoryTest cases.
1058 // OpenBSD does not support these tests either.
1059 // AddressSanitizer defines the malloc()/free()/etc. functions so that they
1060 // don't crash if the program is out of memory, so the OOM tests aren't supposed
1062 // TODO(vandebo) make this work on Windows too.
1063 #if !defined(OS_ANDROID) && !defined(OS_OPENBSD) && \
1064 !defined(OS_WIN) && !defined(ADDRESS_SANITIZER)
1066 #if defined(USE_TCMALLOC)
1068 int tc_set_new_mode(int mode
);
1070 #endif // defined(USE_TCMALLOC)
1072 class OutOfMemoryDeathTest
: public testing::Test
{
1074 OutOfMemoryDeathTest()
1076 // Make test size as large as possible minus a few pages so
1077 // that alignment or other rounding doesn't make it wrap.
1078 test_size_(std::numeric_limits
<std::size_t>::max() - 12 * 1024),
1079 signed_test_size_(std::numeric_limits
<ssize_t
>::max()) {
1082 #if defined(USE_TCMALLOC)
1083 virtual void SetUp() OVERRIDE
{
1087 virtual void TearDown() OVERRIDE
{
1090 #endif // defined(USE_TCMALLOC)
1092 void SetUpInDeathAssert() {
1093 // Must call EnableTerminationOnOutOfMemory() because that is called from
1094 // chrome's main function and therefore hasn't been called yet.
1095 // Since this call may result in another thread being created and death
1096 // tests shouldn't be started in a multithread environment, this call
1097 // should be done inside of the ASSERT_DEATH.
1098 base::EnableTerminationOnOutOfMemory();
1103 ssize_t signed_test_size_
;
1106 TEST_F(OutOfMemoryDeathTest
, New
) {
1108 SetUpInDeathAssert();
1109 value_
= operator new(test_size_
);
1113 TEST_F(OutOfMemoryDeathTest
, NewArray
) {
1115 SetUpInDeathAssert();
1116 value_
= new char[test_size_
];
1120 TEST_F(OutOfMemoryDeathTest
, Malloc
) {
1122 SetUpInDeathAssert();
1123 value_
= malloc(test_size_
);
1127 TEST_F(OutOfMemoryDeathTest
, Realloc
) {
1129 SetUpInDeathAssert();
1130 value_
= realloc(NULL
, test_size_
);
1134 TEST_F(OutOfMemoryDeathTest
, Calloc
) {
1136 SetUpInDeathAssert();
1137 value_
= calloc(1024, test_size_
/ 1024L);
1141 TEST_F(OutOfMemoryDeathTest
, Valloc
) {
1143 SetUpInDeathAssert();
1144 value_
= valloc(test_size_
);
1148 #if defined(OS_LINUX)
1149 TEST_F(OutOfMemoryDeathTest
, Pvalloc
) {
1151 SetUpInDeathAssert();
1152 value_
= pvalloc(test_size_
);
1156 TEST_F(OutOfMemoryDeathTest
, Memalign
) {
1158 SetUpInDeathAssert();
1159 value_
= memalign(4, test_size_
);
1163 TEST_F(OutOfMemoryDeathTest
, ViaSharedLibraries
) {
1164 // g_try_malloc is documented to return NULL on failure. (g_malloc is the
1165 // 'safe' default that crashes if allocation fails). However, since we have
1166 // hopefully overridden malloc, even g_try_malloc should fail. This tests
1167 // that the run-time symbol resolution is overriding malloc for shared
1168 // libraries as well as for our code.
1170 SetUpInDeathAssert();
1171 value_
= g_try_malloc(test_size_
);
1176 // Android doesn't implement posix_memalign().
1177 #if defined(OS_POSIX) && !defined(OS_ANDROID)
1178 TEST_F(OutOfMemoryDeathTest
, Posix_memalign
) {
1179 // Grab the return value of posix_memalign to silence a compiler warning
1180 // about unused return values. We don't actually care about the return
1181 // value, since we're asserting death.
1183 SetUpInDeathAssert();
1184 EXPECT_EQ(ENOMEM
, posix_memalign(&value_
, 8, test_size_
));
1187 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
1189 #if defined(OS_MACOSX)
1191 // Purgeable zone tests
1193 TEST_F(OutOfMemoryDeathTest
, MallocPurgeable
) {
1194 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
1196 SetUpInDeathAssert();
1197 value_
= malloc_zone_malloc(zone
, test_size_
);
1201 TEST_F(OutOfMemoryDeathTest
, ReallocPurgeable
) {
1202 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
1204 SetUpInDeathAssert();
1205 value_
= malloc_zone_realloc(zone
, NULL
, test_size_
);
1209 TEST_F(OutOfMemoryDeathTest
, CallocPurgeable
) {
1210 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
1212 SetUpInDeathAssert();
1213 value_
= malloc_zone_calloc(zone
, 1024, test_size_
/ 1024L);
1217 TEST_F(OutOfMemoryDeathTest
, VallocPurgeable
) {
1218 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
1220 SetUpInDeathAssert();
1221 value_
= malloc_zone_valloc(zone
, test_size_
);
1225 TEST_F(OutOfMemoryDeathTest
, PosixMemalignPurgeable
) {
1226 malloc_zone_t
* zone
= malloc_default_purgeable_zone();
1228 SetUpInDeathAssert();
1229 value_
= malloc_zone_memalign(zone
, 8, test_size_
);
1233 // Since these allocation functions take a signed size, it's possible that
1234 // calling them just once won't be enough to exhaust memory. In the 32-bit
1235 // environment, it's likely that these allocation attempts will fail because
1236 // not enough contiguous address space is available. In the 64-bit environment,
1237 // it's likely that they'll fail because they would require a preposterous
1238 // amount of (virtual) memory.
1240 TEST_F(OutOfMemoryDeathTest
, CFAllocatorSystemDefault
) {
1242 SetUpInDeathAssert();
1244 base::AllocateViaCFAllocatorSystemDefault(signed_test_size_
))) {}
1248 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMalloc
) {
1250 SetUpInDeathAssert();
1252 base::AllocateViaCFAllocatorMalloc(signed_test_size_
))) {}
1256 TEST_F(OutOfMemoryDeathTest
, CFAllocatorMallocZone
) {
1258 SetUpInDeathAssert();
1260 base::AllocateViaCFAllocatorMallocZone(signed_test_size_
))) {}
1264 #if !defined(ARCH_CPU_64_BITS)
1266 // See process_util_unittest_mac.mm for an explanation of why this test isn't
1267 // run in the 64-bit environment.
1269 TEST_F(OutOfMemoryDeathTest
, PsychoticallyBigObjCObject
) {
1271 SetUpInDeathAssert();
1272 while ((value_
= base::AllocatePsychoticallyBigObjCObject())) {}
1276 #endif // !ARCH_CPU_64_BITS
1279 #endif // !defined(OS_ANDROID) && !defined(OS_OPENBSD) &&
1280 // !defined(OS_WIN) && !defined(ADDRESS_SANITIZER)