#include "base/process/process.h"
#include <memory>
#include <string>
#include <utility>
#include "base/at_exit.h"
#include "base/debug/invalid_access_win.h"
#include "base/process/kill.h"
#include "base/test/multiprocess_test.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_local.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/multiprocess_func_list.h"
#if BUILDFLAG(IS_CHROMEOS)
#include <unistd.h>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/win/base_win_buildflags.h"
#include <windows.h>
#endif
namespace {
#if BUILDFLAG(IS_WIN)
constexpr int kExpectedStillRunningExitCode = 0x102;
#else
constexpr int kExpectedStillRunningExitCode = 0;
#endif
constexpr int kDummyExitCode = 42;
#if BUILDFLAG(IS_APPLE)
class FakePortProvider : public base::PortProvider {
mach_port_t TaskForPid(base::ProcessHandle process) const override {
return mach_task_self();
}
};
#endif
#if BUILDFLAG(IS_CHROMEOS)
const char kForeground[] = "/chrome_renderers/foreground";
const char kCgroupRoot[] = "/sys/fs/cgroup/cpu";
const char kFullRendererCgroupRoot[] = "/sys/fs/cgroup/cpu/chrome_renderers";
const char kProcPath[] = "/proc/%d/cgroup";
std::string GetProcessCpuCgroup(const base::Process& process) {
std::string proc;
if (!base::ReadFileToString(
base::FilePath(base::StringPrintf(kProcPath, process.Pid())),
&proc)) {
return std::string();
}
std::vector<base::StringPiece> lines = SplitStringPiece(
proc, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
for (const auto& line : lines) {
std::vector<base::StringPiece> fields = SplitStringPiece(
line, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (fields.size() != 3U) {
continue;
}
if (fields[1] == "cpu") {
return static_cast<std::string>(fields[2]);
}
}
return std::string();
}
bool AddProcessToCpuCgroup(const base::Process& process,
const std::string& cgroup) {
base::FilePath path(cgroup);
path = path.Append("cgroup.procs");
return base::WriteFile(path, base::NumberToString(process.Pid()));
}
#endif
}
namespace base {
class ProcessTest : public MultiProcessTest {
};
TEST_F(ProcessTest, Create) {
Process process(SpawnChild("SimpleChildProcess"));
ASSERT_TRUE(process.IsValid());
ASSERT_FALSE(process.is_current());
EXPECT_NE(process.Pid(), kNullProcessId);
process.Close();
ASSERT_FALSE(process.IsValid());
}
TEST_F(ProcessTest, CreateCurrent) {
Process process = Process::Current();
ASSERT_TRUE(process.IsValid());
ASSERT_TRUE(process.is_current());
EXPECT_NE(process.Pid(), kNullProcessId);
process.Close();
ASSERT_FALSE(process.IsValid());
}
TEST_F(ProcessTest, Move) {
Process process1(SpawnChild("SimpleChildProcess"));
EXPECT_TRUE(process1.IsValid());
Process process2;
EXPECT_FALSE(process2.IsValid());
process2 = std::move(process1);
EXPECT_TRUE(process2.IsValid());
EXPECT_FALSE(process1.IsValid());
EXPECT_FALSE(process2.is_current());
Process process3 = Process::Current();
process2 = std::move(process3);
EXPECT_TRUE(process2.is_current());
EXPECT_TRUE(process2.IsValid());
EXPECT_FALSE(process3.IsValid());
}
TEST_F(ProcessTest, Duplicate) {
Process process1(SpawnChild("SimpleChildProcess"));
ASSERT_TRUE(process1.IsValid());
Process process2 = process1.Duplicate();
ASSERT_TRUE(process1.IsValid());
ASSERT_TRUE(process2.IsValid());
EXPECT_EQ(process1.Pid(), process2.Pid());
EXPECT_FALSE(process1.is_current());
EXPECT_FALSE(process2.is_current());
process1.Close();
ASSERT_TRUE(process2.IsValid());
}
TEST_F(ProcessTest, DuplicateCurrent) {
Process process1 = Process::Current();
ASSERT_TRUE(process1.IsValid());
Process process2 = process1.Duplicate();
ASSERT_TRUE(process1.IsValid());
ASSERT_TRUE(process2.IsValid());
EXPECT_EQ(process1.Pid(), process2.Pid());
EXPECT_TRUE(process1.is_current());
EXPECT_TRUE(process2.is_current());
process1.Close();
ASSERT_TRUE(process2.IsValid());
}
MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
PlatformThread::Sleep(TestTimeouts::action_max_timeout());
return 0;
}
TEST_F(ProcessTest, CreationTimeCurrentProcess) {
EXPECT_FALSE(Process::Current().CreationTime().is_null());
EXPECT_LE(Process::Current().CreationTime(), Time::Now());
}
#if !BUILDFLAG(IS_ANDROID) && !defined(OHOS_UNITTESTS)
TEST_F(ProcessTest, CreationTimeOtherProcess) {
constexpr base::TimeDelta kTolerance =
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
Milliseconds(1100);
#elif BUILDFLAG(IS_WIN)
Milliseconds(100);
#elif BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_FUCHSIA)
Milliseconds(0);
#else
#error Unsupported platform
#endif
const Time before_creation = Time::Now();
Process process(SpawnChild("SleepyChildProcess"));
const Time after_creation = Time::Now();
const Time creation = process.CreationTime();
EXPECT_LE(before_creation - kTolerance, creation);
EXPECT_LE(creation, after_creation + kTolerance);
EXPECT_TRUE(process.Terminate(kDummyExitCode, true));
}
#endif
TEST_F(ProcessTest, Terminate) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
int exit_code = kDummyExitCode;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
EXPECT_EQ(kExpectedStillRunningExitCode, exit_code);
exit_code = kDummyExitCode;
int kExpectedExitCode = 250;
process.Terminate(kExpectedExitCode, false);
process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code);
EXPECT_NE(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code));
#if BUILDFLAG(IS_WIN)
EXPECT_EQ(kExpectedExitCode, exit_code);
#endif
}
void AtExitHandler(void*) {
DCHECK(false);
}
class ThreadLocalObject {
public:
~ThreadLocalObject() {
DCHECK(false);
}
};
MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode0) {
base::ThreadLocalOwnedPointer<ThreadLocalObject> object;
object.Set(std::make_unique<ThreadLocalObject>());
base::AtExitManager::RegisterCallback(&AtExitHandler, nullptr);
Process::TerminateCurrentProcessImmediately(0);
}
TEST_F(ProcessTest, TerminateCurrentProcessImmediatelyWithZeroExitCode) {
Process process(SpawnChild("TerminateCurrentProcessImmediatelyWithCode0"));
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
EXPECT_EQ(0, exit_code);
}
MULTIPROCESS_TEST_MAIN(TerminateCurrentProcessImmediatelyWithCode250) {
Process::TerminateCurrentProcessImmediately(250);
}
TEST_F(ProcessTest, TerminateCurrentProcessImmediatelyWithNonZeroExitCode) {
Process process(SpawnChild("TerminateCurrentProcessImmediatelyWithCode250"));
ASSERT_TRUE(process.IsValid());
int exit_code = 42;
ASSERT_TRUE(process.WaitForExitWithTimeout(TestTimeouts::action_max_timeout(),
&exit_code));
EXPECT_EQ(250, exit_code);
}
MULTIPROCESS_TEST_MAIN(FastSleepyChildProcess) {
PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 10);
return 0;
}
TEST_F(ProcessTest, WaitForExit) {
Process process(SpawnChild("FastSleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
int exit_code = kDummyExitCode;
EXPECT_TRUE(process.WaitForExit(&exit_code));
EXPECT_EQ(0, exit_code);
}
TEST_F(ProcessTest, WaitForExitWithTimeout) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
int exit_code = kDummyExitCode;
TimeDelta timeout = TestTimeouts::tiny_timeout();
EXPECT_FALSE(process.WaitForExitWithTimeout(timeout, &exit_code));
EXPECT_EQ(kDummyExitCode, exit_code);
process.Terminate(kDummyExitCode, false);
}
#if BUILDFLAG(IS_WIN)
TEST_F(ProcessTest, WaitForExitOrEventWithProcessExit) {
Process process(SpawnChild("FastSleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
base::win::ScopedHandle stop_watching_handle(
CreateEvent(nullptr, TRUE, FALSE, nullptr));
int exit_code = kDummyExitCode;
EXPECT_EQ(process.WaitForExitOrEvent(stop_watching_handle, &exit_code),
base::Process::WaitExitStatus::PROCESS_EXITED);
EXPECT_EQ(0, exit_code);
}
TEST_F(ProcessTest, WaitForExitOrEventWithEventSet) {
Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid());
base::win::ScopedHandle stop_watching_handle(
CreateEvent(nullptr, TRUE, TRUE, nullptr));
int exit_code = kDummyExitCode;
EXPECT_EQ(process.WaitForExitOrEvent(stop_watching_handle, &exit_code),
base::Process::WaitExitStatus::STOP_EVENT_SIGNALED);
EXPECT_EQ(kDummyExitCode, exit_code);
process.Terminate(kDummyExitCode, false);
}
#endif
TEST_F(ProcessTest, SetProcessBackgrounded) {
if (!Process::CanBackgroundProcesses())
return;
Process process(SpawnChild("SimpleChildProcess"));
#if defined(OHOS_UNITTESTS)
process.SetProcessBackgrounded(false);
#endif
int old_priority = process.GetPriority();
#if BUILDFLAG(IS_APPLE)
FakePortProvider provider;
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, true));
EXPECT_TRUE(process.IsProcessBackgrounded(&provider));
EXPECT_TRUE(process.SetProcessBackgrounded(&provider, false));
EXPECT_FALSE(process.IsProcessBackgrounded(&provider));
#else
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
#endif
int new_priority = process.GetPriority();
EXPECT_EQ(old_priority, new_priority);
}
TEST_F(ProcessTest, CurrentProcessIsRunning) {
EXPECT_FALSE(Process::Current().WaitForExitWithTimeout(
base::TimeDelta(), nullptr));
}
#if BUILDFLAG(IS_APPLE)
TEST_F(ProcessTest, PredefinedProcessIsRunning) {
EXPECT_FALSE(Process::Open(1).WaitForExitWithTimeout(
base::TimeDelta(), nullptr));
}
#endif
#if BUILDFLAG(IS_WIN)
#if defined(ARCH_CPU_ARM64)
#define MAYBE_HeapCorruption DISABLED_HeapCorruption
#else
#define MAYBE_HeapCorruption HeapCorruption
#endif
TEST_F(ProcessTest, MAYBE_HeapCorruption) {
EXPECT_EXIT(base::debug::win::TerminateWithHeapCorruption(),
::testing::ExitedWithCode(STATUS_HEAP_CORRUPTION), "");
}
#if BUILDFLAG(WIN_ENABLE_CFG_GUARDS)
#define MAYBE_ControlFlowViolation ControlFlowViolation
#else
#define MAYBE_ControlFlowViolation DISABLED_ControlFlowViolation
#endif
TEST_F(ProcessTest, MAYBE_ControlFlowViolation) {
EXPECT_EXIT(base::debug::win::TerminateWithControlFlowViolation(),
::testing::ExitedWithCode(STATUS_STACK_BUFFER_OVERRUN), "");
}
#endif
TEST_F(ProcessTest, ChildProcessIsRunning) {
Process process(SpawnChild("SleepyChildProcess"));
EXPECT_FALSE(process.WaitForExitWithTimeout(
base::TimeDelta(), nullptr));
process.Terminate(0, true);
EXPECT_TRUE(process.WaitForExitWithTimeout(
base::TimeDelta(), nullptr));
}
#if BUILDFLAG(IS_CHROMEOS)
TEST_F(ProcessTest, TestIsProcessBackgroundedCGroup) {
const char kNotBackgrounded[] = "5:cpuacct,cpu,cpuset:/daemons\n";
const char kBackgrounded[] =
"2:freezer:/chrome_renderers/to_be_frozen\n"
"1:cpu:/chrome_renderers/background\n";
EXPECT_FALSE(IsProcessBackgroundedCGroup(kNotBackgrounded));
EXPECT_TRUE(IsProcessBackgroundedCGroup(kBackgrounded));
}
TEST_F(ProcessTest, InitializePriorityEmptyProcess) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
Process process;
process.InitializePriority();
const std::string unique_token = process.unique_token();
ASSERT_TRUE(unique_token.empty());
}
TEST_F(ProcessTest, SetProcessBackgroundedOneCgroupPerRender) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
base::test::TaskEnvironment task_env;
Process process(SpawnChild("SimpleChildProcess"));
process.InitializePriority();
const std::string unique_token = process.unique_token();
ASSERT_FALSE(unique_token.empty());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
std::string cgroup = GetProcessCpuCgroup(process);
EXPECT_FALSE(cgroup.empty());
EXPECT_NE(cgroup.find(unique_token), std::string::npos);
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.Terminate(0, false));
task_env.RunUntilIdle();
cgroup = std::string(kCgroupRoot) + cgroup;
EXPECT_FALSE(base::DirectoryExists(FilePath(cgroup)));
}
TEST_F(ProcessTest, CleanUpBusyProcess) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
base::test::TaskEnvironment task_env;
Process process(SpawnChild("SimpleChildProcess"));
process.InitializePriority();
const std::string unique_token = process.unique_token();
ASSERT_FALSE(unique_token.empty());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
std::string cgroup = GetProcessCpuCgroup(process);
EXPECT_FALSE(cgroup.empty());
EXPECT_NE(cgroup.find(unique_token), std::string::npos);
cgroup = std::string(kCgroupRoot) + cgroup;
Process process2(SpawnChild("SimpleChildProcess"));
EXPECT_TRUE(AddProcessToCpuCgroup(process2, cgroup));
EXPECT_TRUE(process.Terminate(0, false));
task_env.RunUntilIdle();
EXPECT_TRUE(base::DirectoryExists(FilePath(cgroup)));
std::string foreground_path =
std::string(kCgroupRoot) + std::string(kForeground);
EXPECT_TRUE(AddProcessToCpuCgroup(process2, foreground_path));
PlatformThread::Sleep(base::Milliseconds(1100));
task_env.RunUntilIdle();
EXPECT_FALSE(base::DirectoryExists(FilePath(cgroup)));
process2.Terminate(0, false);
}
TEST_F(ProcessTest, SetProcessBackgroundedEmptyToken) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
Process process(SpawnChild("SimpleChildProcess"));
const std::string unique_token = process.unique_token();
ASSERT_TRUE(unique_token.empty());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
std::string cgroup = GetProcessCpuCgroup(process);
EXPECT_FALSE(cgroup.empty());
EXPECT_EQ(cgroup, kForeground);
}
TEST_F(ProcessTest, CleansUpStaleGroups) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
base::test::TaskEnvironment task_env;
Process process(SpawnChild("SimpleChildProcess"));
process.InitializePriority();
const std::string unique_token = process.unique_token();
ASSERT_FALSE(unique_token.empty());
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
std::string root = kFullRendererCgroupRoot;
std::string cgroup = root + "/" + unique_token;
std::vector<std::string> tokens = base::SplitString(
cgroup, "-", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
tokens[1] = "fake";
std::string fake_cgroup = base::JoinString(tokens, "-");
EXPECT_TRUE(base::CreateDirectory(FilePath(fake_cgroup)));
Process::CleanUpStaleProcessStates();
EXPECT_FALSE(base::DirectoryExists(FilePath(fake_cgroup)));
EXPECT_TRUE(base::DirectoryExists(FilePath(cgroup)));
EXPECT_TRUE(base::DirectoryExists(FilePath(root + "/foreground")));
EXPECT_TRUE(base::DirectoryExists(FilePath(root + "/background")));
EXPECT_TRUE(process.Terminate(0, false));
task_env.RunUntilIdle();
EXPECT_FALSE(base::DirectoryExists(FilePath(cgroup)));
}
TEST_F(ProcessTest, OneCgroupDoesNotCleanUpGroupsWithWrongPrefix) {
if (!Process::OneGroupPerRendererEnabledForTesting())
return;
base::test::TaskEnvironment task_env;
Process process(SpawnChild("SimpleChildProcess"));
process.InitializePriority();
const std::string unique_token = process.unique_token();
ASSERT_FALSE(unique_token.empty());
EXPECT_TRUE(process.SetProcessBackgrounded(false));
EXPECT_FALSE(process.IsProcessBackgrounded());
std::string cgroup = GetProcessCpuCgroup(process);
EXPECT_FALSE(cgroup.empty());
EXPECT_NE(cgroup.find(unique_token), std::string::npos);
FilePath cgroup_path = FilePath(std::string(kCgroupRoot) + cgroup);
FilePath fake_cgroup = FilePath(kFullRendererCgroupRoot).AppendASCII("fake");
EXPECT_TRUE(base::CreateDirectory(fake_cgroup));
Process::CleanUpStaleProcessStates();
EXPECT_TRUE(base::DirectoryExists(fake_cgroup));
EXPECT_TRUE(base::DirectoryExists(cgroup_path));
EXPECT_TRUE(process.SetProcessBackgrounded(true));
EXPECT_TRUE(process.IsProcessBackgrounded());
EXPECT_TRUE(process.Terminate(0, false));
task_env.RunUntilIdle();
EXPECT_FALSE(base::DirectoryExists(cgroup_path));
base::DeleteFile(fake_cgroup);
}
#endif
}