Files
sousa-gecko/ipc/glue/ProcessChild.cpp
T
Yannis Juglaret 94a7941255 Bug 1888528 - Add test coverage for uniform child shutdown-hang crash reports. r=whimboo,ipc-reviewers,nika
Stop skipping the shutdown-hang crash path under MOZ_TEST_CHILD_EXIT_HANG,
since the hang delay used by browser_child_hang.js is low enough to avoid
the crash path anyway.

Add a Marionette test for NS_FREE_PERMANENT_DATA builds, that forces a
child process hang at shutdown by using MOZ_TEST_CHILD_EXIT_HANG, then
verifies that this results in a crash report that carries the expected
uniform crash signature override annotation.

Mark MOZ_TEST_CHILD_EXIT_HANG crashes as intentional by using the new
IntentionalCrashForTesting annotation, to avoid flagging the test runs
that produce these crash reports as failures.

Differential Revision: https://phabricator.services.mozilla.com/D311491
2026-08-04 12:32:28 +00:00

133 lines
4.3 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ipc/ProcessChild.h"
#include "Endpoint.h"
#include "nsDebug.h"
#ifdef XP_WIN
# include <stdlib.h> // for _exit()
# include <synchapi.h>
#else
# include <unistd.h> // for _exit()
# include <time.h>
# include "base/eintr_wrapper.h"
# include "prenv.h"
#endif
#include "nsAppRunner.h"
#include "nsExceptionHandler.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/ipc/IOThread.h"
#include "mozilla/ipc/ProcessUtils.h"
#include "mozilla/GeckoArgs.h"
namespace mozilla {
namespace ipc {
ProcessChild* ProcessChild::gProcessChild;
StaticMutex ProcessChild::gIPCShutdownStateLock;
constinit nsCString ProcessChild::gIPCShutdownStateAnnotation;
ProcessChild::ProcessChild(IPC::Channel::ChannelHandle aClientChannel,
ProcessId aParentPid, const nsID& aMessageChannelId)
: mUILoop(MessageLoop::current()),
mParentPid(aParentPid),
mMessageChannelId(aMessageChannelId),
mChildThread(
MakeUnique<IOThreadChild>(std::move(aClientChannel), aParentPid)) {
MOZ_ASSERT(mUILoop, "UILoop should be created by now");
MOZ_ASSERT(!gProcessChild, "should only be one ProcessChild");
CrashReporter::RegisterAnnotationNSCString(
CrashReporter::Annotation::IPCShutdownState,
&gIPCShutdownStateAnnotation);
gProcessChild = this;
}
/* static */
void ProcessChild::AddPlatformBuildID(geckoargs::ChildProcessArgs& aExtraArgs) {
nsCString parentBuildID(mozilla::PlatformBuildID());
geckoargs::sParentBuildID.Put(parentBuildID.get(), aExtraArgs);
}
/* static */
bool ProcessChild::InitPrefs(int aArgc, char* aArgv[]) {
Maybe<ReadOnlySharedMemoryHandle> prefsHandle =
geckoargs::sPrefsHandle.Get(aArgc, aArgv);
Maybe<ReadOnlySharedMemoryHandle> prefMapHandle =
geckoargs::sPrefMapHandle.Get(aArgc, aArgv);
if (prefsHandle.isNothing() || prefMapHandle.isNothing()) {
return false;
}
SharedPreferenceDeserializer deserializer;
return deserializer.DeserializeFromSharedMemory(std::move(*prefsHandle),
std::move(*prefMapHandle));
}
#ifdef ENABLE_TESTS
// Allow tests to cause a synthetic delay/"hang" during child process
// shutdown by setting environment variables.
# ifdef XP_UNIX
static void ReallySleep(int aSeconds) {
struct ::timespec snooze = {aSeconds, 0};
HANDLE_EINTR(nanosleep(&snooze, &snooze));
}
# else
static void ReallySleep(int aSeconds) { ::Sleep(aSeconds * 1000); }
# endif // Unix/Win
static void SleepIfEnv(const char* aName) {
if (auto* value = PR_GetEnv(aName)) {
CrashReporter::RecordAnnotationBool(
CrashReporter::Annotation::IntentionalCrashForTesting, true);
ReallySleep(atoi(value));
CrashReporter::UnrecordAnnotation(
CrashReporter::Annotation::IntentionalCrashForTesting);
}
}
#else // not tests
static void SleepIfEnv(const char* aName) {}
#endif
ProcessChild::~ProcessChild() {
#ifdef NS_FREE_PERMANENT_DATA
// debug/*san/ccov: content processes reach ~ProcessChild on normal shutdown.
// This sleep is late enough that it won't block parent process shutdown, so
// we'll get into late IPC shutdown with processes still running.
SleepIfEnv("MOZ_TEST_CHILD_EXIT_HANG");
#endif
gIPCShutdownStateAnnotation = ""_ns;
gProcessChild = nullptr;
}
/* static */
void ProcessChild::NotifiedImpendingShutdown() {
AppShutdown::SetImpendingShutdown();
ProcessChild::AppendToIPCShutdownStateAnnotation(
"NotifiedImpendingShutdown"_ns);
}
/* static */
void ProcessChild::QuickExit() {
#ifndef NS_FREE_PERMANENT_DATA
// opt: content processes exit here without reaching ~ProcessChild on normal
// shutdown (and the parent is terminated before ProcessWatcher's shutdown
// hook). Blocking here lets us exercise ProcessWatcher's kill timer.
SleepIfEnv("MOZ_TEST_CHILD_EXIT_HANG");
#endif
AppShutdown::DoImmediateExit();
}
UntypedEndpoint ProcessChild::TakeInitialEndpoint() {
return UntypedEndpoint{PrivateIPDLInterface{},
mChildThread->TakeInitialPort(), mMessageChannelId,
EndpointProcInfo::Current(),
EndpointProcInfo{.mPid = mParentPid, .mChildID = 0}};
}
} // namespace ipc
} // namespace mozilla