From 15a964b8f86a36c35e99090ff2296906b9f35067 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Tue, 3 Oct 2023 18:40:30 +0000 Subject: [PATCH] Bug 1855138 - Add BackgroundParent::KillHardAsync method; r=nika,ipc-reviewers Differential Revision: https://phabricator.services.mozilla.com/D189482 --- ipc/glue/BackgroundImpl.cpp | 42 +++++++++++++++++++++++++++++++ ipc/glue/BackgroundParent.h | 3 +++ ipc/glue/BackgroundParentImpl.cpp | 5 ++++ 3 files changed, 50 insertions(+) diff --git a/ipc/glue/BackgroundImpl.cpp b/ipc/glue/BackgroundImpl.cpp index f12250a87f42..efe4fe3d7318 100644 --- a/ipc/glue/BackgroundImpl.cpp +++ b/ipc/glue/BackgroundImpl.cpp @@ -184,6 +184,10 @@ class ParentImpl final : public BackgroundParentImpl { // Forwarded from BackgroundParent. static uint64_t GetChildID(PBackgroundParent* aBackgroundActor); + // Forwarded from BackgroundParent. + static void KillHardAsync(PBackgroundParent* aBackgroundActor, + const char* aReason); + // Forwarded from BackgroundParent. static bool AllocStarter(ContentParent* aContent, Endpoint&& aEndpoint, @@ -653,6 +657,12 @@ uint64_t BackgroundParent::GetChildID(PBackgroundParent* aBackgroundActor) { return ParentImpl::GetChildID(aBackgroundActor); } +// static +void BackgroundParent::KillHardAsync(PBackgroundParent* aBackgroundActor, + const char* aReason) { + ParentImpl::KillHardAsync(aBackgroundActor, aReason); +} + // static bool BackgroundParent::AllocStarter( ContentParent* aContent, Endpoint&& aEndpoint) { @@ -756,6 +766,38 @@ uint64_t ParentImpl::GetChildID(PBackgroundParent* aBackgroundActor) { return 0; } +// static +void ParentImpl::KillHardAsync(PBackgroundParent* aBackgroundActor, + const char* aReason) { + AssertIsInMainProcess(); + AssertIsOnBackgroundThread(); + MOZ_ASSERT(aBackgroundActor); + MOZ_ASSERT(BackgroundParent::IsOtherProcessActor(aBackgroundActor)); + + RefPtr handle = + GetContentParentHandle(aBackgroundActor); + MOZ_ASSERT(handle); + + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread( + NS_NewRunnableFunction("ParentImpl::KillHardAsync", + [handle = std::move(handle), aReason]() { + mozilla::AssertIsOnMainThread(); + + if (RefPtr contentParent = + handle->GetContentParent()) { + contentParent->KillHard(aReason); + } + }), + NS_DISPATCH_NORMAL)); + + // After we've scheduled killing of the remote process, also ensure we induce + // a connection error in the IPC channel to immediately stop all IPC + // communication on this channel. + if (aBackgroundActor->CanSend()) { + aBackgroundActor->GetIPCChannel()->InduceConnectionError(); + } +} + // static bool ParentImpl::AllocStarter(ContentParent* aContent, Endpoint&& aEndpoint, diff --git a/ipc/glue/BackgroundParent.h b/ipc/glue/BackgroundParent.h index 80f3c0984a7c..8e5e6193ca9b 100644 --- a/ipc/glue/BackgroundParent.h +++ b/ipc/glue/BackgroundParent.h @@ -77,6 +77,9 @@ class BackgroundParent final { static uint64_t GetChildID(PBackgroundParent* aBackgroundActor); + static void KillHardAsync(PBackgroundParent* aBackgroundActor, + const char* aReason); + private: // Only called by ContentParent for cross-process actors. static bool AllocStarter(ContentParent* aContent, diff --git a/ipc/glue/BackgroundParentImpl.cpp b/ipc/glue/BackgroundParentImpl.cpp index aaf6c9cfe5d4..bbd45e981312 100644 --- a/ipc/glue/BackgroundParentImpl.cpp +++ b/ipc/glue/BackgroundParentImpl.cpp @@ -816,6 +816,11 @@ mozilla::ipc::IPCResult BackgroundParentImpl::RecvPBroadcastChannelConstructor( return IPC_OK(); } + // XXX It's now possible to call KillHardAsync on the PBackground thread, so + // the checks can be done directly here. + + // XXX Once BackgroundParent::ProcessingError calls KillHardAsync, we can + // just return IPC_FAIL() if a check fails. RefPtr runnable = new CheckPrincipalRunnable(parent.forget(), aPrincipalInfo, aOrigin); MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));