gecko-dev/ipc/glue/UtilityProcessParent.cpp
Gabriele Svelto aa43fa218e Bug 1831092 - Use the new pull-based API for all crash annotations and remove the global annotations table r=jgilbert,necko-reviewers,media-playback-reviewers,profiler-reviewers,win-reviewers,padenot,handyman,afranchuk,valentin,alwu,sotaro
This changes comes with several different refactorings all rolled into one,
unfotunately I couldn't find a way to pull them apart:
- First of all annotations now can either recorded (that is, we copy the value
  and have the crash reporting code own the copy) or registered. Several
  annotations are changed to use this functionality so that we don't need to
  update them as their value change.
- The code in the exception handler is modified to read the annotations from
  the mozannotation_client crate. This has the unfortunate side-effect that
  we need three different bits of code to serialize them: one for annotations
  read from a child process, one for reading annotations from the main process
  outside of the exception handler and one for reading annotations from the
  main process within the exception handler. As we move to fully
  out-of-process crash reporting the last two methods will go away.
- The mozannotation_client crate now doesn't record annotation types anymore.
  I realized as I was working on this that storing types at runtime has two
  issues: the first one is that buggy code might change the type of an
  annotation (that is record it under two different types at two different
  moments), the second issue is that types might become corrupt during a
  crash, so better enforce them at annotation-writing time. The end result is
  that the mozannotation_* crates now only store byte buffers, track the
  format the data is stored in (null-terminated string, fixed size buffer,
  etc...) but not the type of data each annotation is supposed to contain.
- Which brings us to the next change: concrete types for annotations are now
  enforced when they're written out. If an annotation doesn't match the
  expected type it's skipped. Storing an annotation with the wrong type will
  also trigger an assertion in debug builds.

Differential Revision: https://phabricator.services.mozilla.com/D195248
2024-03-04 10:24:43 +00:00

204 lines
6.6 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/UtilityProcessParent.h"
#include "mozilla/ipc/UtilityProcessManager.h"
#if defined(XP_WIN)
# include <dwrite.h>
# include <process.h>
# include "mozilla/WinDllServices.h"
#endif
#include "mozilla/ipc/ProcessChild.h"
#include "mozilla/FOGIPC.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TelemetryIPC.h"
#include "nsHashPropertyBag.h"
#include "mozilla/Services.h"
#include "nsIObserverService.h"
namespace mozilla::ipc {
UtilityProcessParent::UtilityProcessParent(UtilityProcessHost* aHost)
: mHost(aHost) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mHost);
}
UtilityProcessParent::~UtilityProcessParent() = default;
bool UtilityProcessParent::SendRequestMemoryReport(
const uint32_t& aGeneration, const bool& aAnonymize,
const bool& aMinimizeMemoryUsage, const Maybe<FileDescriptor>& aDMDFile) {
mMemoryReportRequest = MakeUnique<MemoryReportRequestHost>(aGeneration);
PUtilityProcessParent::SendRequestMemoryReport(
aGeneration, aAnonymize, aMinimizeMemoryUsage, aDMDFile,
[self = RefPtr{this}](const uint32_t& aGeneration2) {
if (self->mMemoryReportRequest) {
self->mMemoryReportRequest->Finish(aGeneration2);
self->mMemoryReportRequest = nullptr;
}
},
[self = RefPtr{this}](mozilla::ipc::ResponseRejectReason) {
self->mMemoryReportRequest = nullptr;
});
return true;
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvAddMemoryReport(
const MemoryReport& aReport) {
if (mMemoryReportRequest) {
mMemoryReportRequest->RecvReport(aReport);
}
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvFOGData(ByteBuf&& aBuf) {
glean::FOGData(std::move(aBuf));
return IPC_OK();
}
#if defined(XP_WIN)
mozilla::ipc::IPCResult UtilityProcessParent::RecvGetModulesTrust(
ModulePaths&& aModPaths, bool aRunAtNormalPriority,
GetModulesTrustResolver&& aResolver) {
RefPtr<DllServices> dllSvc(DllServices::Get());
dllSvc->GetModulesTrust(std::move(aModPaths), aRunAtNormalPriority)
->Then(
GetMainThreadSerialEventTarget(), __func__,
[aResolver](ModulesMapResult&& aResult) {
aResolver(Some(ModulesMapResult(std::move(aResult))));
},
[aResolver](nsresult aRv) { aResolver(Nothing()); });
return IPC_OK();
}
#endif // defined(XP_WIN)
mozilla::ipc::IPCResult UtilityProcessParent::RecvAccumulateChildHistograms(
nsTArray<HistogramAccumulation>&& aAccumulations) {
TelemetryIPC::AccumulateChildHistograms(Telemetry::ProcessID::Utility,
aAccumulations);
return IPC_OK();
}
mozilla::ipc::IPCResult
UtilityProcessParent::RecvAccumulateChildKeyedHistograms(
nsTArray<KeyedHistogramAccumulation>&& aAccumulations) {
TelemetryIPC::AccumulateChildKeyedHistograms(Telemetry::ProcessID::Utility,
aAccumulations);
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvUpdateChildScalars(
nsTArray<ScalarAction>&& aScalarActions) {
TelemetryIPC::UpdateChildScalars(Telemetry::ProcessID::Utility,
aScalarActions);
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvUpdateChildKeyedScalars(
nsTArray<KeyedScalarAction>&& aScalarActions) {
TelemetryIPC::UpdateChildKeyedScalars(Telemetry::ProcessID::Utility,
aScalarActions);
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvRecordChildEvents(
nsTArray<mozilla::Telemetry::ChildEventData>&& aEvents) {
TelemetryIPC::RecordChildEvents(Telemetry::ProcessID::Utility, aEvents);
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvRecordDiscardedData(
const mozilla::Telemetry::DiscardedData& aDiscardedData) {
TelemetryIPC::RecordDiscardedData(Telemetry::ProcessID::Utility,
aDiscardedData);
return IPC_OK();
}
mozilla::ipc::IPCResult UtilityProcessParent::RecvInitCompleted() {
MOZ_ASSERT(mHost);
mHost->ResolvePromise();
return IPC_OK();
}
void UtilityProcessParent::ActorDestroy(ActorDestroyReason aWhy) {
RefPtr<nsHashPropertyBag> props = new nsHashPropertyBag();
if (aWhy == AbnormalShutdown) {
nsAutoString dumpID;
if (mCrashReporter) {
#if defined(MOZ_SANDBOX)
RefPtr<mozilla::ipc::UtilityProcessManager> upm =
mozilla::ipc::UtilityProcessManager::GetSingleton();
if (upm) {
Span<const UtilityActorName> actors = upm->GetActors(this);
nsAutoCString actorsName;
if (!actors.IsEmpty()) {
actorsName += GetUtilityActorName(actors.First<1>()[0]);
for (const auto& actor : actors.From(1)) {
actorsName += ", "_ns + GetUtilityActorName(actor);
}
}
mCrashReporter->AddAnnotationNSCString(
CrashReporter::Annotation::UtilityActorsName, actorsName);
}
#endif
}
GenerateCrashReport(OtherPid(), &dumpID);
// It's okay for dumpID to be empty if there was no minidump generated
// tests like ipc/glue/test/browser/browser_utility_crashReporter.js are
// there to verify this
if (!dumpID.IsEmpty()) {
props->SetPropertyAsAString(u"dumpID"_ns, dumpID);
}
MaybeTerminateProcess();
}
nsAutoString pid;
pid.AppendInt(static_cast<uint64_t>(OtherPid()));
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers((nsIPropertyBag2*)props, "ipc:utility-shutdown",
pid.get());
} else {
NS_WARNING("Could not get a nsIObserverService, ipc:utility-shutdown skip");
}
mHost->OnChannelClosed();
}
// To ensure that IPDL is finished before UtilityParent gets deleted.
class DeferredDeleteUtilityProcessParent : public Runnable {
public:
explicit DeferredDeleteUtilityProcessParent(
RefPtr<UtilityProcessParent> aParent)
: Runnable("ipc::glue::DeferredDeleteUtilityProcessParent"),
mParent(std::move(aParent)) {}
NS_IMETHODIMP Run() override { return NS_OK; }
private:
RefPtr<UtilityProcessParent> mParent;
};
/* static */
void UtilityProcessParent::Destroy(RefPtr<UtilityProcessParent> aParent) {
NS_DispatchToMainThread(
new DeferredDeleteUtilityProcessParent(std::move(aParent)));
}
} // namespace mozilla::ipc