1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "UtilityProcessImpl.h"
8 #include "mozilla/ipc/IOThreadChild.h"
9 #include "mozilla/GeckoArgs.h"
10 #include "mozilla/ProcInfo.h"
13 # include "nsExceptionHandler.h"
16 #if defined(XP_WIN) && defined(MOZ_SANDBOX)
17 # include "mozilla/sandboxTarget.h"
19 # include "WMFDecoderModule.h"
22 #if defined(XP_OPENBSD) && defined(MOZ_SANDBOX)
23 # include "mozilla/SandboxSettings.h"
26 #if defined(MOZ_WMF_CDM) && defined(MOZ_SANDBOX)
27 # include "mozilla/MFCDMParent.h"
30 namespace mozilla::ipc
{
32 UtilityProcessImpl::~UtilityProcessImpl() = default;
36 void UtilityProcessImpl::LoadLibraryOrCrash(LPCWSTR aLib
) {
37 // re-try a few times depending on the error we get ; inspired by both our
38 // results on content process allocations as well as msys2:
39 // https://github.com/git-for-windows/msys2-runtime/blob/b4fed42af089ab955286343835a97e287496b3f8/winsup/cygwin/autoload.cc#L323-L339
41 const int kMaxRetries
= 10;
44 for (int i
= 0; i
< kMaxRetries
; i
++) {
45 HMODULE module
= ::LoadLibraryW(aLib
);
50 err
= ::GetLastError();
52 if (err
!= ERROR_NOACCESS
&& err
!= ERROR_DLL_INIT_FAILED
) {
60 /* case ERROR_ACCESS_DENIED: */
61 /* case ERROR_BAD_EXE_FORMAT: */
62 /* case ERROR_SHARING_VIOLATION: */
63 case ERROR_MOD_NOT_FOUND
:
64 case ERROR_COMMITMENT_LIMIT
:
65 // We want to make it explicit in telemetry that this was in fact an
66 // OOM condition, even though we could not detect it on our own
67 CrashReporter::AnnotateOOMAllocationSize(1);
74 MOZ_CRASH_UNSAFE_PRINTF("Unable to preload module: 0x%lx", err
);
76 #endif // defined(XP_WIN)
78 bool UtilityProcessImpl::Init(int aArgc
, char* aArgv
[]) {
79 Maybe
<uint64_t> sandboxingKind
= geckoargs::sSandboxingKind
.Get(aArgc
, aArgv
);
80 if (sandboxingKind
.isNothing()) {
84 if (*sandboxingKind
>= SandboxingKind::COUNT
) {
88 #if defined(MOZ_SANDBOX) && defined(XP_WIN)
89 // We delay load winmm.dll so that its dependencies don't interfere with COM
90 // initialization when win32k is locked down. We need to load it before we
91 // lower the sandbox in processes where the policy will prevent loading.
92 LoadLibraryOrCrash(L
"winmm.dll");
94 // Call this once before enabling the sandbox, it will cache its result
95 // in a static variable.
98 if (*sandboxingKind
== SandboxingKind::GENERIC_UTILITY
) {
99 // Preload audio generic libraries required for ffmpeg only
100 UtilityAudioDecoderParent::GenericPreloadForSandbox();
103 if (*sandboxingKind
== SandboxingKind::UTILITY_AUDIO_DECODING_WMF
104 # ifdef MOZ_WMF_MEDIA_ENGINE
105 || *sandboxingKind
== SandboxingKind::MF_MEDIA_ENGINE_CDM
108 UtilityAudioDecoderParent::WMFPreloadForSandbox();
112 mozilla::SandboxTarget::Instance()->StartSandbox();
113 #elif defined(__OpenBSD__) && defined(MOZ_SANDBOX)
114 if (*sandboxingKind
!= SandboxingKind::GENERIC_UTILITY
) {
115 StartOpenBSDSandbox(GeckoProcessType_Utility
,
116 (SandboxingKind
)*sandboxingKind
);
120 #if defined(MOZ_WMF_CDM) && defined(MOZ_SANDBOX)
121 if (*sandboxingKind
== MF_MEDIA_ENGINE_CDM
) {
122 Maybe
<const char*> pluginPath
= geckoargs::sPluginPath
.Get(aArgc
, aArgv
);
124 MFCDMParent::SetWidevineL1Path(*pluginPath
);
126 NS_WARNING("No Widevine L1 plugin for the utility process!");
131 Maybe
<const char*> parentBuildID
=
132 geckoargs::sParentBuildID
.Get(aArgc
, aArgv
);
133 if (parentBuildID
.isNothing()) {
137 if (!ProcessChild::InitPrefs(aArgc
, aArgv
)) {
141 return mUtility
->Init(TakeInitialEndpoint(), nsCString(*parentBuildID
),
145 void UtilityProcessImpl::CleanUp() { NS_ShutdownXPCOM(nullptr); }
147 } // namespace mozilla::ipc