1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/browser/browser_child_process_observer.h"
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "content/browser/browser_child_process_host_impl.h"
#include "content/browser/child_process_host_impl.h"
#include "content/browser/service_host/utility_process_host.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/browser_child_process_host_delegate.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/process_type.h"
#include "content/public/common/sandboxed_process_launcher_delegate.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/test_service.mojom.h"
#include "sandbox/policy/sandbox_type.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace content {
namespace {
// An enum that represent the different type of notitifcations that exist in
// BrowserChildProcessObserver.
enum class Notification {
kLaunchedAndConnected,
kDisconnected,
kCrashed,
kKilled,
kLaunchFailed,
kExitedNormally,
};
// Nicer test output.
std::ostream& operator<<(std::ostream& os, Notification notification) {
switch (notification) {
case Notification::kLaunchedAndConnected:
os << "LaunchedAndConnected";
break;
case Notification::kDisconnected:
os << "Disconnected";
break;
case Notification::kCrashed:
os << "Crashed";
break;
case Notification::kKilled:
os << "Killed";
break;
case Notification::kLaunchFailed:
os << "LaunchFailed";
break;
case Notification::kExitedNormally:
os << "ExitedNormally";
break;
}
return os;
}
// Returns true if a child process whose ID is |child_id| is still alive.
bool IsHostAlive(int child_id) {
return BrowserChildProcessHost::FromID(child_id) != nullptr;
}
} // namespace
// A test BrowserChildProcessObserver that transforms every call to one of the
// observer's method to a call to the notification callback.
class BrowserChildProcessNotificationObserver
: public BrowserChildProcessObserver {
public:
using OnNotificationCallback =
base::RepeatingCallback<void(Notification notification)>;
BrowserChildProcessNotificationObserver(
int child_id,
OnNotificationCallback on_notification_callback)
: child_id_(child_id),
on_notification_callback_(std::move(on_notification_callback)) {
BrowserChildProcessObserver::Add(this);
}
~BrowserChildProcessNotificationObserver() override {
BrowserChildProcessObserver::Remove(this);
}
protected:
// BrowserChildProcessObserver:
void BrowserChildProcessLaunchedAndConnected(
const ChildProcessData& data) override {
OnNotification(data, Notification::kLaunchedAndConnected);
}
void BrowserChildProcessHostDisconnected(
const ChildProcessData& data) override {
OnNotification(data, Notification::kDisconnected);
}
void BrowserChildProcessCrashed(
const ChildProcessData& data,
const ChildProcessTerminationInfo& info) override {
OnNotification(data, Notification::kCrashed);
}
void BrowserChildProcessKilled(
const ChildProcessData& data,
const ChildProcessTerminationInfo& info) override {
OnNotification(data, Notification::kKilled);
}
void BrowserChildProcessLaunchFailed(
const ChildProcessData& data,
const ChildProcessTerminationInfo& info) override {
OnNotification(data, Notification::kLaunchFailed);
}
void BrowserChildProcessExitedNormally(
const ChildProcessData& data,
const ChildProcessTerminationInfo& info) override {
OnNotification(data, Notification::kExitedNormally);
}
void OnNotification(const ChildProcessData& data, Notification notification) {
if (data.id == child_id_)
on_notification_callback_.Run(notification);
}
private:
// Every notification coming for a child with a different ID will be ignored.
int child_id_;
// The callback to invoke every time a method of the observer is called.
OnNotificationCallback on_notification_callback_;
};
// A helper class that allows the user to wait until a specific |notification|
// is sent for a child process whose ID matches |child_id|.
class WaitForNotificationObserver {
public:
WaitForNotificationObserver(int child_id, Notification notification)
: inner_observer_(
child_id,
base::BindRepeating(&WaitForNotificationObserver::OnNotification,
base::Unretained(this))),
notification_(notification) {}
~WaitForNotificationObserver() = default;
// Waits until the notification is received. Returns immediately if it was
// already received.
void Wait() {
if (notification_received_)
return;
DCHECK(!run_loop_.running());
run_loop_.Run();
}
private:
void OnNotification(Notification notification) {
if (notification != notification_)
return;
notification_received_ = true;
if (run_loop_.running())
run_loop_.Quit();
}
BrowserChildProcessNotificationObserver inner_observer_;
Notification notification_;
base::RunLoop run_loop_;
bool notification_received_ = false;
};
class TestSandboxedProcessLauncherDelegate
: public SandboxedProcessLauncherDelegate {
public:
explicit TestSandboxedProcessLauncherDelegate(
sandbox::mojom::Sandbox sandbox_type)
: sandbox_type_(sandbox_type) {}
~TestSandboxedProcessLauncherDelegate() override = default;
// SandboxedProcessLauncherDelegate:
sandbox::mojom::Sandbox GetSandboxType() override { return sandbox_type_; }
private:
sandbox::mojom::Sandbox sandbox_type_;
};
// A test-specific type of process host. Self-owned.
class TestProcessHost : public BrowserChildProcessHostDelegate {
public:
static base::WeakPtr<TestProcessHost> Create() {
auto* instance = new TestProcessHost();
return instance->GetWeakPtr();
}
TestProcessHost()
: process_(BrowserChildProcessHost::Create(
PROCESS_TYPE_UTILITY,
this,
ChildProcessHost::IpcMode::kNormal)) {}
~TestProcessHost() override = default;
// Returns the ID of the child process.
int GetID() { return process_->GetData().id; }
// Binds to the test service on the child process and returns the bound
// remote.
mojo::Remote<mojom::TestService> BindTestService() {
mojo::Remote<mojom::TestService> test_service;
static_cast<ChildProcessHostImpl*>(process_->GetHost())
->child_process()
->BindServiceInterface(test_service.BindNewPipeAndPassReceiver());
return test_service;
}
// Returns the command line used to launch the child process.
std::unique_ptr<base::CommandLine> GetChildCommandLine() {
base::FilePath child_path =
ChildProcessHost::GetChildPath(ChildProcessHost::CHILD_NORMAL);
auto command_line = std::make_unique<base::CommandLine>(child_path);
command_line->AppendSwitchASCII(switches::kProcessType,
switches::kUtilityProcess);
command_line->AppendSwitchASCII(switches::kUtilitySubType,
"Test Utility Process");
sandbox::policy::SetCommandLineFlagsForSandboxType(command_line.get(),
sandbox_type_);
return command_line;
}
// Launches the child process using the default test launcher delegate.
void LaunchProcess() {
LaunchProcessWithDelegate(
std::make_unique<TestSandboxedProcessLauncherDelegate>(sandbox_type_));
}
// Launches the child process using a supplied sandbox delegate.
void LaunchProcessWithDelegate(
std::unique_ptr<SandboxedProcessLauncherDelegate>
sandboxed_process_launcher_delegate) {
process_->SetName(u"Test utility process");
auto command_line = GetChildCommandLine();
bool terminate_on_shutdown = true;
process_->Launch(std::move(sandboxed_process_launcher_delegate),
std::move(command_line), terminate_on_shutdown);
test_service_ = BindTestService();
}
// Requests the child process to shutdown.
void ForceShutdown() { process_->GetHost()->ForceShutdown(); }
// Disconnects the bound remote from the test service.
void Disconnect() { test_service_.reset(); }
// Sets the sandbox type to use for the child process.
void SetSandboxType(sandbox::mojom::Sandbox sandbox_type) {
sandbox_type_ = sandbox_type;
}
mojom::TestService* service() const { return test_service_.get(); }
base::WeakPtr<TestProcessHost> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
sandbox::mojom::Sandbox sandbox_type_ = sandbox::mojom::Sandbox::kUtility;
std::unique_ptr<BrowserChildProcessHost> process_;
mojo::Remote<mojom::TestService> test_service_;
base::WeakPtrFactory<TestProcessHost> weak_ptr_factory_{this};
};
// A helper class that exposes which notifications were sent for a specific
// child process.
class TestBrowserChildProcessObserver {
public:
explicit TestBrowserChildProcessObserver(int child_id)
: inner_observer_(child_id,
base::BindRepeating(
&TestBrowserChildProcessObserver::OnNotification,
base::Unretained(this))) {}
~TestBrowserChildProcessObserver() = default;
// Returns the notifications received for |child_id|.
const std::vector<Notification>& notifications() const {
return notifications_;
}
private:
void OnNotification(Notification notification) {
notifications_.push_back(notification);
}
BrowserChildProcessNotificationObserver inner_observer_;
std::vector<Notification> notifications_;
};
class BrowserChildProcessObserverBrowserTest : public ContentBrowserTest {};
// Tests that launching and then using ForceShutdown() results in a normal
// termination.
#if defined(ADDRESS_SANITIZER)
// TODO(crbug.com/40238612): Fix ASAN failures on trybot.
#define MAYBE_LaunchAndForceShutdown DISABLED_LaunchAndForceShutdown
#else
#define MAYBE_LaunchAndForceShutdown LaunchAndForceShutdown
#endif
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest,
MAYBE_LaunchAndForceShutdown) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id,
Notification::kLaunchedAndConnected);
host->LaunchProcess();
waiter.Wait();
}
{
WaitForNotificationObserver waiter(child_id, Notification::kDisconnected);
host->ForceShutdown();
waiter.Wait();
}
Notification kExitNotification =
#if BUILDFLAG(IS_ANDROID)
// TODO(pmonette): On Android, this currently causes a killed
// notification. Consider fixing.
Notification::kKilled;
#else
Notification::kExitedNormally;
#endif // BUILDFLAG(IS_ANDROID)
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(),
testing::ElementsAreArray({Notification::kLaunchedAndConnected,
kExitNotification,
Notification::kDisconnected}));
}
// Tests that launching and then deleting the host results in a normal
// termination.
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest,
LaunchAndDelete) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id,
Notification::kLaunchedAndConnected);
host->LaunchProcess();
waiter.Wait();
}
{
WaitForNotificationObserver waiter(child_id, Notification::kDisconnected);
delete host.get();
waiter.Wait();
}
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(),
testing::ElementsAreArray({Notification::kLaunchedAndConnected,
Notification::kExitedNormally,
Notification::kDisconnected}));
}
// Tests that launching and then disconnecting the service channel results in a
// normal termination.
// Note: This only works for services bound using BindServiceInterface(), not
// BindReceiver().
#if defined(ADDRESS_SANITIZER)
// TODO(crbug.com/40238612): Fix ASAN failures on trybot.
#define MAYBE_LaunchAndDisconnect DISABLED_LaunchAndDisconnect
#else
#define MAYBE_LaunchAndDisconnect LaunchAndDisconnect
#endif
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest,
MAYBE_LaunchAndDisconnect) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id,
Notification::kLaunchedAndConnected);
host->LaunchProcess();
waiter.Wait();
}
{
WaitForNotificationObserver waiter(child_id, Notification::kDisconnected);
host->Disconnect();
waiter.Wait();
}
Notification kExitNotification =
#if BUILDFLAG(IS_ANDROID)
// On Android, kKilled is always sent in the case of a crash.
Notification::kKilled;
#else
Notification::kExitedNormally;
#endif // BUILDFLAG(IS_ANDROID)
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(), testing::ElementsAreArray({
Notification::kLaunchedAndConnected,
kExitNotification,
Notification::kDisconnected,
}));
}
// Tests that launching and then causing a crash the host results in a crashed
// notification.
// TODO(crbug.com/40868150): Times out on Android tests.
#if BUILDFLAG(IS_ANDROID)
#define MAYBE_LaunchAndCrash DISABLED_LaunchAndCrash
#else
#define MAYBE_LaunchAndCrash LaunchAndCrash
#endif
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest,
MAYBE_LaunchAndCrash) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id,
Notification::kLaunchedAndConnected);
host->LaunchProcess();
waiter.Wait();
}
{
WaitForNotificationObserver waiter(child_id, Notification::kDisconnected);
host->service()->DoCrashImmediately(base::DoNothing());
waiter.Wait();
}
Notification kCrashedNotification =
#if BUILDFLAG(IS_ANDROID)
// On Android, kKilled is always sent in the case of a crash.
Notification::kKilled;
#else
Notification::kCrashed;
#endif // BUILDFLAG(IS_ANDROID)
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(),
testing::ElementsAreArray({Notification::kLaunchedAndConnected,
kCrashedNotification,
Notification::kDisconnected}));
}
// Tests that kLaunchFailed is correctly sent when the child process fails to
// launch.
//
// This test won't work as-is on POSIX platforms, where fork()+exec() is used to
// launch child processes, failure does not happen until exec(), therefore the
// test will see a valid child process followed by a
// TERMINATION_STATUS_ABNORMAL_TERMINATION of the forked process. However,
// posix_spawn() is used on macOS.
// See also ServiceProcessLauncherTest.FailToLaunchProcess.
#if !BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest, LaunchFailed) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
#if BUILDFLAG(IS_WIN)
// The Windows sandbox does not like the child process being a different
// process, so launch unsandboxed for the purpose of this test.
host->SetSandboxType(sandbox::mojom::Sandbox::kNoSandbox);
#endif
// Simulate a catastrophic launch failure for all child processes by
// making the path to the process non-existent.
base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
switches::kBrowserSubprocessPath,
base::FilePath(FILE_PATH_LITERAL("non_existent_path")));
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id, Notification::kLaunchFailed);
host->LaunchProcess();
waiter.Wait();
}
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(),
testing::ElementsAreArray({Notification::kLaunchFailed}));
}
#endif // !BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_WIN)
class TestPreSpawnTargetFailureSandboxedProcessLauncherDelegate
: public TestSandboxedProcessLauncherDelegate {
public:
using TestSandboxedProcessLauncherDelegate::
TestSandboxedProcessLauncherDelegate;
// SandboxedProcessLauncherDelegate:
bool PreSpawnTarget(sandbox::TargetPolicy* policy) override {
// Force a failure in PreSpawnTarget().
return false;
}
};
// Override the observer to verify the error occurred in PreSpawnTarget().
class TestPreSpawnTargetFailureBrowserChildProcessNotificationObserver
: public BrowserChildProcessNotificationObserver {
public:
using BrowserChildProcessNotificationObserver::
BrowserChildProcessNotificationObserver;
// BrowserChildProcessObserver:
void BrowserChildProcessLaunchFailed(
const ChildProcessData& data,
const ChildProcessTerminationInfo& info) override {
EXPECT_EQ(info.exit_code, sandbox::SBOX_ERROR_DELEGATE_PRE_SPAWN);
BrowserChildProcessNotificationObserver::OnNotification(
data, Notification::kLaunchFailed);
}
};
// Tests that a pre spawn failure results in a failed launch.
IN_PROC_BROWSER_TEST_F(BrowserChildProcessObserverBrowserTest,
LaunchPreSpawnFailed) {
base::WeakPtr<TestProcessHost> host = TestProcessHost::Create();
int child_id = host->GetID();
TestBrowserChildProcessObserver observer(child_id);
{
WaitForNotificationObserver waiter(child_id, Notification::kLaunchFailed);
host->LaunchProcessWithDelegate(
std::make_unique<
TestPreSpawnTargetFailureSandboxedProcessLauncherDelegate>(
sandbox::mojom::Sandbox::kUtility));
waiter.Wait();
}
// The host should be deleted now.
EXPECT_FALSE(host);
EXPECT_FALSE(IsHostAlive(child_id));
EXPECT_THAT(observer.notifications(),
testing::ElementsAreArray({Notification::kLaunchFailed}));
}
#endif // BUILDFLAG(IS_WIN)
} // namespace content
|