Bug Summary

File:var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/CmdLineAndEnvUtils.h
Warning:line 710, column 1
Potential leak of memory pointed to by 'expr'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name nsAppRunner.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/xre -fcoverage-compilation-dir=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/xre -resource-dir /usr/lib/llvm-18/lib/clang/18 -include /var/lib/jenkins/workspace/firefox-scan-build/config/gcc_hidden.h -include /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D TELEMETRY_PING_FORMAT_VERSION=4 -D PROXY_PRINTING=1 -D USE_GLX_TEST -D MOZ_APP_NAME="firefox" -D MOZ_APP_BASENAME="Firefox" -D MOZ_APP_DISPLAYNAME="Nightly" -D MOZ_APP_VENDOR="Mozilla" -D MOZ_APP_VERSION="130.0a1" -D OS_TARGET="Linux" -D MOZ_WIDGET_TOOLKIT="gtk" -D MOZ_UPDATER -D MOZ_DISTRIBUTION_ID="org.mozilla" -D TARGET_OS_ABI="Linux_x86_64-gcc3" -D GRE_MILESTONE=130.0a1 -D MOZ_APP_VERSION_DISPLAY=130.0a1 -D APP_VERSION=130.0a1 -D APP_ID={ec8030f7-c20a-464f-9b0e-13a3a9e97384} -D MOZ_BUILD_APP_IS_BROWSER -D MAR_NSS -D TOPOBJDIR=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/xre -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/remote -I /var/lib/jenkins/workspace/firefox-scan-build/widget -I /var/lib/jenkins/workspace/firefox-scan-build/widget/gtk -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/printingui -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/ipc/ipdl/_ipdlheaders -I /var/lib/jenkins/workspace/firefox-scan-build/ipc/chromium/src -I /var/lib/jenkins/workspace/firefox-scan-build/other-licenses/nsis/Contrib/CityHash/cityhash -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/find -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/printingui/ipc -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/windowwatcher -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/mozapps/update/common -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/profile -I /var/lib/jenkins/workspace/firefox-scan-build/config -I /var/lib/jenkins/workspace/firefox-scan-build/dom/base -I /var/lib/jenkins/workspace/firefox-scan-build/dom/commandhandler -I /var/lib/jenkins/workspace/firefox-scan-build/dom/ipc -I /var/lib/jenkins/workspace/firefox-scan-build/dom/webbrowserpersist -I /var/lib/jenkins/workspace/firefox-scan-build/js/xpconnect/loader -I /var/lib/jenkins/workspace/firefox-scan-build/testing/gtest/mozilla -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/crashreporter -I /var/lib/jenkins/workspace/firefox-scan-build/xpcom/build -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -I /usr/include/gtk-3.0 -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/gio-unix-2.0 -I /usr/include/cloudproviders -I /usr/include/atk-1.0 -I /usr/include/at-spi2-atk/2.0 -I /usr/include/at-spi-2.0 -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -I /usr/include/gtk-3.0/unix-print -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-18/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-deprecated-this-capture -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -fdeprecated-macro -ferror-limit 19 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-07-27-022226-2793976-1 -x c++ /var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp

/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp

1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6#include "mozilla/dom/ContentParent.h"
7#include "mozilla/dom/ContentChild.h"
8#include "mozilla/ipc/GeckoChildProcessHost.h"
9
10#include "mozilla/AppShutdown.h"
11#include "mozilla/ArrayUtils.h"
12#include "mozilla/Assertions.h"
13#include "mozilla/Attributes.h"
14#include "mozilla/Components.h"
15#include "mozilla/FilePreferences.h"
16#include "mozilla/ChaosMode.h"
17#include "mozilla/CmdLineAndEnvUtils.h"
18#include "mozilla/IOInterposer.h"
19#include "mozilla/ipc/UtilityProcessChild.h"
20#include "mozilla/Likely.h"
21#include "mozilla/MemoryChecking.h"
22#include "mozilla/Poison.h"
23#include "mozilla/Preferences.h"
24#include "mozilla/PreferenceSheet.h"
25#include "mozilla/Printf.h"
26#include "mozilla/ProcessType.h"
27#include "mozilla/ResultExtensions.h"
28#include "mozilla/RuntimeExceptionModule.h"
29#include "mozilla/ScopeExit.h"
30#include "mozilla/StaticPrefs_browser.h"
31#include "mozilla/StaticPrefs_fission.h"
32#include "mozilla/StaticPrefs_webgl.h"
33#include "mozilla/StaticPrefs_widget.h"
34#include "mozilla/Telemetry.h"
35#include "mozilla/Utf8.h"
36#include "mozilla/intl/LocaleService.h"
37#include "mozilla/JSONWriter.h"
38#include "mozilla/gfx/gfxVars.h"
39#include "mozilla/glean/GleanMetrics.h"
40#include "mozilla/glean/GleanPings.h"
41#include "mozilla/widget/TextRecognition.h"
42#include "BaseProfiler.h"
43#include "mozJSModuleLoader.h"
44
45#include "nsAppRunner.h"
46#include "mozilla/XREAppData.h"
47#include "mozilla/Bootstrap.h"
48#if defined(MOZ_UPDATER1) && !defined(MOZ_WIDGET_ANDROID)
49# include "nsUpdateDriver.h"
50# include "nsUpdateSyncManager.h"
51#endif
52#include "ProfileReset.h"
53
54#ifdef MOZ_INSTRUMENT_EVENT_LOOP1
55# include "EventTracer.h"
56#endif
57
58#ifdef XP_MACOSX
59# include "nsVersionComparator.h"
60# include "MacLaunchHelper.h"
61# include "MacApplicationDelegate.h"
62# include "MacAutoreleasePool.h"
63# include "MacRunFromDmgUtils.h"
64// these are needed for sysctl
65# include <sys/types.h>
66# include <sys/sysctl.h>
67#endif
68
69#include "prnetdb.h"
70#include "prprf.h"
71#include "prproces.h"
72#include "prenv.h"
73#include "prtime.h"
74
75#include "nsIAppStartup.h"
76#include "nsCategoryManagerUtils.h"
77#include "nsIMutableArray.h"
78#include "nsCommandLine.h"
79#include "nsIComponentRegistrar.h"
80#include "nsIDialogParamBlock.h"
81#include "mozilla/ModuleUtils.h"
82#include "nsIIOService.h"
83#include "nsIObserverService.h"
84#include "nsINativeAppSupport.h"
85#include "nsIPlatformInfo.h"
86#include "nsIProcess.h"
87#include "nsIProfileUnlocker.h"
88#include "nsIPromptService.h"
89#include "nsIPropertyBag2.h"
90#include "nsIServiceManager.h"
91#include "nsIStringBundle.h"
92#include "nsISupportsPrimitives.h"
93#include "nsIToolkitProfile.h"
94#include "nsToolkitProfileService.h"
95#include "nsIURI.h"
96#include "nsIURL.h"
97#include "nsIWindowCreator.h"
98#include "nsIWindowWatcher.h"
99#include "nsIXULAppInfo.h"
100#include "nsIXULRuntime.h"
101#include "nsPIDOMWindow.h"
102#include "nsIWidget.h"
103#include "nsAppShellCID.h"
104#include "mozilla/dom/quota/QuotaManager.h"
105#include "mozilla/scache/StartupCache.h"
106#include "gfxPlatform.h"
107#include "PDMFactory.h"
108#ifdef XP_MACOSX
109# include "gfxPlatformMac.h"
110#endif
111
112#include "mozilla/Unused.h"
113
114#ifdef XP_WIN
115# include "nsIWinAppHelper.h"
116# include <windows.h>
117# include <intrin.h>
118# include <math.h>
119# include "cairo/cairo-features.h"
120# include "detect_win32k_conflicts.h"
121# include "mozilla/PreXULSkeletonUI.h"
122# include "mozilla/DllPrefetchExperimentRegistryInfo.h"
123# include "mozilla/WindowsBCryptInitialization.h"
124# include "mozilla/WindowsDllBlocklist.h"
125# include "mozilla/WindowsMsctfInitialization.h"
126# include "mozilla/WindowsProcessMitigations.h"
127# include "mozilla/WindowsVersion.h"
128# include "mozilla/WinHeaderOnlyUtils.h"
129# include "mozilla/mscom/ProcessRuntime.h"
130# include "mozilla/mscom/ProfilerMarkers.h"
131# include "WinTokenUtils.h"
132
133# if defined(MOZ_LAUNCHER_PROCESS)
134# include "mozilla/LauncherRegistryInfo.h"
135# endif
136
137# if defined(MOZ_DEFAULT_BROWSER_AGENT)
138# include "nsIWindowsRegKey.h"
139# endif
140
141# ifndef PROCESS_DEP_ENABLE
142# define PROCESS_DEP_ENABLE 0x1
143# endif
144#endif
145
146#if defined(MOZ_SANDBOX1)
147# include "mozilla/SandboxSettings.h"
148#endif
149
150#ifdef ACCESSIBILITY1
151# include "nsAccessibilityService.h"
152# if defined(XP_WIN)
153# include "mozilla/a11y/Compatibility.h"
154# include "mozilla/a11y/Platform.h"
155# endif
156#endif
157
158#include "nsCRT.h"
159#include "nsCOMPtr.h"
160#include "nsDirectoryServiceDefs.h"
161#include "nsDirectoryServiceUtils.h"
162#include "nsEmbedCID.h"
163#include "nsIDUtils.h"
164#include "nsNetUtil.h"
165#include "nsReadableUtils.h"
166#include "nsXPCOM.h"
167#include "nsXPCOMCIDInternal.h"
168#include "nsString.h"
169#include "nsPrintfCString.h"
170#include "nsVersionComparator.h"
171
172#include "nsAppDirectoryServiceDefs.h"
173#include "nsXULAppAPI.h"
174#include "nsXREDirProvider.h"
175
176#include "nsINIParser.h"
177#include "mozilla/Omnijar.h"
178#include "mozilla/StartupTimeline.h"
179#include "mozilla/LateWriteChecks.h"
180
181#include <stdlib.h>
182#include <locale.h>
183
184#ifdef XP_UNIX1
185# include <errno(*__errno_location ()).h>
186# include <pwd.h>
187# include <string.h>
188# include <sys/resource.h>
189# include <sys/stat.h>
190# include <unistd.h>
191#endif
192
193#ifdef XP_WIN
194# include <process.h>
195# include <shlobj.h>
196# include "mozilla/WinDllServices.h"
197# include "nsThreadUtils.h"
198# include "WinUtils.h"
199#endif
200
201#ifdef XP_MACOSX
202# include "nsILocalFileMac.h"
203# include "nsCommandLineServiceMac.h"
204#endif
205
206// for X remote support
207#if defined(MOZ_HAS_REMOTE1)
208# include "nsRemoteService.h"
209#endif
210
211#if defined(DEBUG1) && defined(XP_WIN)
212# include <malloc.h>
213#endif
214
215#if defined(XP_MACOSX)
216# include <Carbon/Carbon.h>
217#endif
218
219#ifdef DEBUG1
220# include "mozilla/Logging.h"
221#endif
222
223#ifdef MOZ_JPROF
224# include "jprof.h"
225#endif
226
227#include "nsExceptionHandler.h"
228#include "nsICrashReporter.h"
229#include "nsIPrefService.h"
230#include "nsIMemoryInfoDumper.h"
231#if defined(XP_LINUX1) && !defined(ANDROID)
232# include "mozilla/widget/LSBUtils.h"
233#endif
234
235#include "base/command_line.h"
236#include "GTestRunner.h"
237
238#ifdef MOZ_WIDGET_ANDROID
239# include "mozilla/java/GeckoAppShellWrappers.h"
240#endif
241
242#if defined(MOZ_SANDBOX1)
243# if defined(XP_LINUX1) && !defined(ANDROID)
244# include "mozilla/SandboxInfo.h"
245# elif defined(XP_WIN)
246# include "sandboxBroker.h"
247# endif
248#endif
249
250#ifdef MOZ_CODE_COVERAGE
251# include "mozilla/CodeCoverageHandler.h"
252#endif
253
254#include "GMPProcessChild.h"
255#include "SafeMode.h"
256
257#ifdef MOZ_BACKGROUNDTASKS1
258# include "mozilla/BackgroundTasks.h"
259# include "nsIPowerManagerService.h"
260# include "nsIStringBundle.h"
261#endif
262
263#ifdef USE_GLX_TEST1
264# include "mozilla/GUniquePtr.h"
265# include "mozilla/GfxInfo.h"
266#endif
267
268#ifdef MOZ_WIDGET_GTK1
269# include "nsAppShell.h"
270#endif
271#ifdef MOZ_ENABLE_DBUS1
272# include "DBusService.h"
273#endif
274
275extern uint32_t gRestartMode;
276extern void InstallSignalHandlers(const char* ProgramName);
277
278#define FILE_COMPATIBILITY_INFO"compatibility.ini"_ns "compatibility.ini"_ns
279#define FILE_INVALIDATE_CACHES".purgecaches"_ns ".purgecaches"_ns
280#define FILE_STARTUP_INCOMPLETEu".startup-incomplete"_ns u".startup-incomplete"_ns
281
282#if defined(MOZ_BLOCK_PROFILE_DOWNGRADE1) || defined(MOZ_LAUNCHER_PROCESS) || \
283 defined(MOZ_DEFAULT_BROWSER_AGENT)
284static const char kPrefHealthReportUploadEnabled[] =
285 "datareporting.healthreport.uploadEnabled";
286#endif // defined(MOZ_BLOCK_PROFILE_DOWNGRADE) || defined(MOZ_LAUNCHER_PROCESS)
287 // || defined(MOZ_DEFAULT_BROWSER_AGENT)
288#if defined(MOZ_DEFAULT_BROWSER_AGENT)
289static const char kPrefDefaultAgentEnabled[] = "default-browser-agent.enabled";
290
291static const char kPrefServicesSettingsServer[] = "services.settings.server";
292static const char kPrefSetDefaultBrowserUserChoicePref[] =
293 "browser.shell.setDefaultBrowserUserChoice";
294#endif // defined(MOZ_DEFAULT_BROWSER_AGENT)
295
296#if defined(XP_WIN)
297static const char kPrefThemeId[] = "extensions.activeThemeID";
298static const char kPrefBrowserStartupBlankWindow[] =
299 "browser.startup.blankWindow";
300static const char kPrefPreXulSkeletonUI[] = "browser.startup.preXulSkeletonUI";
301#endif // defined(XP_WIN)
302
303#if defined(MOZ_WIDGET_GTK1)
304constexpr nsLiteralCString kStartupTokenNames[] = {
305 "XDG_ACTIVATION_TOKEN"_ns,
306 "DESKTOP_STARTUP_ID"_ns,
307};
308#endif
309
310int gArgc;
311char** gArgv;
312
313static const char gToolkitVersion[] = MOZ_STRINGIFY(GRE_MILESTONE)"130.0a1";
314// The gToolkitBuildID global is defined to MOZ_BUILDID via gen_buildid.py
315// in toolkit/library. See related comment in toolkit/library/moz.build.
316extern const char gToolkitBuildID[];
317
318static nsIProfileLock* gProfileLock;
319#if defined(MOZ_HAS_REMOTE1)
320static nsRemoteService* gRemoteService;
321bool gRestartWithoutRemote = false;
322#endif
323
324int gRestartArgc;
325char** gRestartArgv;
326
327// If gRestartedByOS is set, we were automatically restarted by the OS.
328bool gRestartedByOS = false;
329
330bool gIsGtest = false;
331
332bool gKioskMode = false;
333int gKioskMonitor = -1;
334
335bool gAllowContentAnalysisArgPresent = false;
336
337nsString gAbsoluteArgv0Path;
338
339#if defined(XP_WIN)
340nsString gProcessStartupShortcut;
341#endif
342
343#if defined(MOZ_WIDGET_GTK1)
344# include <glib.h>
345# include "mozilla/WidgetUtilsGtk.h"
346# include <gtk/gtk.h>
347# ifdef MOZ_WAYLAND1
348# include <gdk/gdkwayland.h>
349# include "mozilla/widget/nsWaylandDisplay.h"
350# include "wayland-proxy.h"
351# endif
352# ifdef MOZ_X111
353# include <gdk/gdkx.h>
354# endif /* MOZ_X11 */
355#endif
356
357#if defined(MOZ_WAYLAND1)
358std::unique_ptr<WaylandProxy> gWaylandProxy;
359#endif
360
361#include "BinaryPath.h"
362
363#ifdef MOZ_LOGGING1
364# include "mozilla/Logging.h"
365extern mozilla::LazyLogModule gWidgetWaylandLog;
366#endif /* MOZ_LOGGING */
367
368#ifdef FUZZING
369# include "FuzzerRunner.h"
370
371namespace mozilla {
372FuzzerRunner* fuzzerRunner = 0;
373} // namespace mozilla
374
375# ifdef LIBFUZZER
376void XRE_LibFuzzerSetDriver(LibFuzzerDriver aDriver) {
377 mozilla::fuzzerRunner->setParams(aDriver);
378}
379# endif
380#endif // FUZZING
381
382// Undo X11/X.h's definition of None
383#undef None
384
385namespace mozilla {
386int (*RunGTest)(int*, char**) = 0;
387
388bool RunningGTest() { return RunGTest; }
389} // namespace mozilla
390
391using namespace mozilla;
392using namespace mozilla::widget;
393using namespace mozilla::startup;
394using mozilla::Unused;
395using mozilla::dom::ContentChild;
396using mozilla::dom::ContentParent;
397using mozilla::dom::quota::QuotaManager;
398using mozilla::intl::LocaleService;
399using mozilla::scache::StartupCache;
400
401// Save the given word to the specified environment variable.
402static void MOZ_NEVER_INLINE__attribute__((noinline)) SaveWordToEnv(const char* name,
403 const nsACString& word) {
404 char* expr =
405 Smprintf("%s=%s", name, PromiseFlatCStringTPromiseFlatString<char>(word).get()).release();
406 if (expr) PR_SetEnv(expr);
407 // We intentionally leak |expr| here since it is required by PR_SetEnv.
408}
409
410// Save the path of the given file to the specified environment variable.
411static void SaveFileToEnv(const char* name, nsIFile* file) {
412#ifdef XP_WIN
413 nsAutoString path;
414 file->GetPath(path);
415 SetEnvironmentVariableW(NS_ConvertASCIItoUTF16(name).get(), path.get());
416#else
417 nsAutoCString path;
418 file->GetNativePath(path);
419 SaveWordToEnv(name, path);
420#endif
421}
422
423static bool gIsExpectedExit = false;
424
425void MozExpectedExit() { gIsExpectedExit = true; }
426
427/**
428 * Runs atexit() to catch unexpected exit from 3rd party libraries like the
429 * Intel graphics driver calling exit in an error condition. When they
430 * call exit() to report an error we won't shutdown correctly and wont catch
431 * the issue with our crash reporter.
432 */
433static void UnexpectedExit() {
434 if (!gIsExpectedExit) {
435 gIsExpectedExit = true; // Don't risk re-entrency issues when crashing.
436 MOZ_CRASH("Exit called by third party code.")do { do { } while (false); MOZ_ReportCrash("" "Exit called by third party code."
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 436); AnnotateMozCrashReason("MOZ_CRASH(" "Exit called by third party code."
")"); do { *((volatile int*)__null) = 436; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
437 }
438}
439
440#if defined(MOZ_WAYLAND1)
441bool IsWaylandEnabled() {
442 static bool isWaylandEnabled = []() {
443 const char* waylandDisplay = PR_GetEnv("WAYLAND_DISPLAY");
444 if (!waylandDisplay) {
445 return false;
446 }
447 if (!PR_GetEnv("DISPLAY")) {
448 // No X11 display, so try to run wayland.
449 return true;
450 }
451 // MOZ_ENABLE_WAYLAND is our primary Wayland on/off switch.
452 if (const char* waylandPref = PR_GetEnv("MOZ_ENABLE_WAYLAND")) {
453 return *waylandPref == '1';
454 }
455 if (const char* backendPref = PR_GetEnv("GDK_BACKEND")) {
456 if (!strncmp(backendPref, "wayland", 7)) {
457 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, "Wayland backend should be enabled by MOZ_ENABLE_WAYLAND=1."
"GDK_BACKEND is a Gtk3 debug variable and may cause issues."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 459)
458 "Wayland backend should be enabled by MOZ_ENABLE_WAYLAND=1."NS_DebugBreak(NS_DEBUG_WARNING, "Wayland backend should be enabled by MOZ_ENABLE_WAYLAND=1."
"GDK_BACKEND is a Gtk3 debug variable and may cause issues."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 459)
459 "GDK_BACKEND is a Gtk3 debug variable and may cause issues.")NS_DebugBreak(NS_DEBUG_WARNING, "Wayland backend should be enabled by MOZ_ENABLE_WAYLAND=1."
"GDK_BACKEND is a Gtk3 debug variable and may cause issues."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 459)
;
460 return true;
461 }
462 }
463 // Enable by default when we're running on a recent enough GTK version. We'd
464 // like to check further details like compositor version and so on ideally
465 // to make sure we don't enable it on old Mutter or what not, but we can't,
466 // so let's assume that if the user is running on a Wayland session by
467 // default we're ok, since either the distro has enabled Wayland by default,
468 // or the user has gone out of their way to use Wayland.
469 return !gtk_check_version(3, 24, 30);
470 }();
471 return isWaylandEnabled;
472}
473#else
474bool IsWaylandEnabled() { return false; }
475#endif
476
477/**
478 * Output a string to the user. This method is really only meant to be used to
479 * output last-ditch error messages designed for developers NOT END USERS.
480 *
481 * @param isError
482 * Pass true to indicate severe errors.
483 * @param fmt
484 * printf-style format string followed by arguments.
485 */
486static MOZ_FORMAT_PRINTF(2, 3)__attribute__((format(printf, 2, 3))) void Output(bool isError, const char* fmt, ...) {
487 va_list ap;
488 va_start(ap, fmt)__builtin_va_start(ap, fmt);
489
490#if defined(XP_WIN) && !MOZ_WINCONSOLE
491 SmprintfPointer msg = mozilla::Vsmprintf(fmt, ap);
492 if (msg) {
493 UINT flags = MB_OK;
494 if (isError)
495 flags |= MB_ICONERROR;
496 else
497 flags |= MB_ICONINFORMATION;
498
499 wchar_t wide_msg[1024];
500 MultiByteToWideChar(CP_ACP, 0, msg.get(), -1, wide_msg,
501 sizeof(wide_msg) / sizeof(wchar_t));
502
503 wchar_t wide_caption[128];
504 MultiByteToWideChar(CP_ACP, 0, gAppData ? gAppData->name : "XULRunner", -1,
505 wide_caption, sizeof(wide_caption) / sizeof(wchar_t));
506 MessageBoxW(nullptr, wide_msg, wide_caption, flags);
507 }
508#elif defined(MOZ_WIDGET_ANDROID)
509 SmprintfPointer msg = mozilla::Vsmprintf(fmt, ap);
510 if (msg) {
511 __android_log_print(isError ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO,
512 "GeckoRuntime", "%s", msg.get());
513 }
514#else
515 vfprintf(stderrstderr, fmt, ap);
516#endif
517
518 va_end(ap)__builtin_va_end(ap);
519}
520
521/**
522 * Check for a commandline flag. If the flag takes a parameter, the
523 * parameter is returned in aParam. Flags may be in the form -arg or
524 * --arg (or /arg on win32).
525 *
526 * @param aArg the parameter to check. Must be lowercase.
527 * @param aParam if non-null, the -arg <data> will be stored in this pointer.
528 * This is *not* allocated, but rather a pointer to the argv data.
529 * @param aFlags flags @see CheckArgFlag
530 */
531static ArgResult CheckArg(const char* aArg, const char** aParam = nullptr,
532 CheckArgFlag aFlags = CheckArgFlag::RemoveArg) {
533 MOZ_ASSERT(gArgv, "gArgv must be initialized before CheckArg()")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(gArgv)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(gArgv))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("gArgv" " (" "gArgv must be initialized before CheckArg()"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 533); AnnotateMozCrashReason("MOZ_ASSERT" "(" "gArgv" ") ("
"gArgv must be initialized before CheckArg()" ")"); do { *((
volatile int*)__null) = 533; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false)
;
534 return CheckArg(gArgc, gArgv, aArg, aParam, aFlags);
535}
536
537/**
538 * Check for a commandline flag. Ignore data that's passed in with the flag.
539 * Flags may be in the form -arg or --arg (or /arg on win32).
540 * Will not remove flag if found.
541 *
542 * @param aArg the parameter to check. Must be lowercase.
543 */
544static ArgResult CheckArgExists(const char* aArg) {
545 return CheckArg(aArg, nullptr, CheckArgFlag::None);
546}
547
548bool gSafeMode = false;
549bool gFxREmbedded = false;
550
551enum E10sStatus {
552 kE10sEnabledByDefault,
553 kE10sForceDisabled,
554};
555
556static bool gBrowserTabsRemoteAutostart = false;
557static E10sStatus gBrowserTabsRemoteStatus;
558static bool gBrowserTabsRemoteAutostartInitialized = false;
559
560namespace mozilla {
561
562bool BrowserTabsRemoteAutostart() {
563 if (gBrowserTabsRemoteAutostartInitialized) {
564 return gBrowserTabsRemoteAutostart;
565 }
566 gBrowserTabsRemoteAutostartInitialized = true;
567
568 // If we're not in the parent process, we are running E10s.
569 if (!XRE_IsParentProcess()) {
570 gBrowserTabsRemoteAutostart = true;
571 return gBrowserTabsRemoteAutostart;
572 }
573
574 gBrowserTabsRemoteAutostart = true;
575 E10sStatus status = kE10sEnabledByDefault;
576
577 // We use "are non-local connections disabled" as a proxy for
578 // "are we running some kind of automated test". It would be nicer to use
579 // xpc::IsInAutomation(), but that depends on some prefs being set, which
580 // they are not in (at least) gtests (where we can't) and xpcshell.
581 // Long-term, hopefully we can make all tests e10s-friendly,
582 // then we could remove this automation-only env variable.
583 if (gBrowserTabsRemoteAutostart && xpc::AreNonLocalConnectionsDisabled()) {
584 const char* forceDisable = PR_GetEnv("MOZ_FORCE_DISABLE_E10S");
585 if (forceDisable && !strcmp(forceDisable, "1")) {
586 gBrowserTabsRemoteAutostart = false;
587 status = kE10sForceDisabled;
588 }
589 }
590
591 gBrowserTabsRemoteStatus = status;
592
593 return gBrowserTabsRemoteAutostart;
594}
595
596} // namespace mozilla
597
598// Win32k Infrastructure ==============================================
599
600// This bool tells us if we have initialized the following two statics
601// upon startup.
602
603static bool gWin32kInitialized = false;
604
605// Win32k Lockdown for the current session is determined once, at startup,
606// and then remains the same for the duration of the session.
607
608static nsIXULRuntime::ContentWin32kLockdownState gWin32kStatus;
609
610// The status of the win32k experiment. It is determined once, at startup,
611// and then remains the same for the duration of the session.
612
613static nsIXULRuntime::ExperimentStatus gWin32kExperimentStatus =
614 nsIXULRuntime::eExperimentStatusUnenrolled;
615
616#ifdef XP_WIN
617// The states for win32k lockdown can be generalized to:
618// - User doesn't meet requirements (bad OS, webrender, remotegl) aka
619// PersistentRequirement
620// - User has Safe Mode enabled, Win32k Disabled by Env Var, or E10S disabled
621// by Env Var aka TemporaryRequirement
622// - User has set the win32k pref to something non-default aka NonDefaultPref
623// - User has been enrolled in the experiment and does what it says aka
624// Enrolled
625// - User does the default aka Default
626//
627// We expect the below behvaior. In the code, there may be references to these
628// behaviors (e.g. [A]) but not always.
629//
630// [A] Becoming enrolled in the experiment while NonDefaultPref disqualifies
631// you upon next browser start
632// [B] Becoming enrolled in the experiment while PersistentRequirement
633// disqualifies you upon next browser start
634// [C] Becoming enrolled in the experiment while TemporaryRequirement does not
635// disqualify you
636// [D] Becoming enrolled in the experiment will only take effect after restart
637// [E] While enrolled, becoming PersistentRequirement will not disqualify you
638// until browser restart, but if the state is present at browser start,
639// will disqualify you. Additionally, it will not affect the session value
640// of gWin32kStatus.
641// XXX(bobowen): I hope both webrender and wbgl.out-of-process require a
642// restart to take effect!
643// [F] While enrolled, becoming NonDefaultPref will disqualify you upon next
644// browser start
645// [G] While enrolled, becoming TemporaryRequirement will _not_ disqualify you,
646// but the session status will prevent win32k lockdown from taking effect.
647
648// The win32k pref
649static const char kPrefWin32k[] = "security.sandbox.content.win32k-disable";
650
651// The current enrollment status as controlled by Normandy. This value is only
652// stored in the default preference branch, and is not persisted across
653// sessions by the preference service. It therefore isn't available early
654// enough at startup, and needs to be synced to a preference in the user
655// branch which is persisted across sessions.
656static const char kPrefWin32kExperimentEnrollmentStatus[] =
657 "security.sandbox.content.win32k-experiment.enrollmentStatus";
658
659// The enrollment status to be used at browser startup. This automatically
660// synced from the above enrollmentStatus preference whenever the latter is
661// changed. We reused the Fission experiment enum - it can have any of the
662// values defined in the `nsIXULRuntime_ExperimentStatus` enum _except_ rollout.
663// Meanings are documented in the declaration of
664// `nsIXULRuntime.fissionExperimentStatus`
665static const char kPrefWin32kExperimentStartupEnrollmentStatus[] =
666 "security.sandbox.content.win32k-experiment.startupEnrollmentStatus";
667
668namespace mozilla {
669
670bool Win32kExperimentEnrolled() {
671 MOZ_ASSERT(XRE_IsParentProcess())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(XRE_IsParentProcess())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(XRE_IsParentProcess()))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("XRE_IsParentProcess()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 671); AnnotateMozCrashReason("MOZ_ASSERT" "(" "XRE_IsParentProcess()"
")"); do { *((volatile int*)__null) = 671; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
672 return gWin32kExperimentStatus == nsIXULRuntime::eExperimentStatusControl ||
673 gWin32kExperimentStatus == nsIXULRuntime::eExperimentStatusTreatment;
674}
675
676} // namespace mozilla
677
678static bool Win32kRequirementsUnsatisfied(
679 nsIXULRuntime::ContentWin32kLockdownState aStatus) {
680 return aStatus == nsIXULRuntime::ContentWin32kLockdownState::
681 OperatingSystemNotSupported ||
682 aStatus ==
683 nsIXULRuntime::ContentWin32kLockdownState::MissingWebRender ||
684 aStatus ==
685 nsIXULRuntime::ContentWin32kLockdownState::MissingRemoteWebGL ||
686 aStatus ==
687 nsIXULRuntime::ContentWin32kLockdownState::DecodersArentRemote;
688}
689
690static void Win32kExperimentDisqualify() {
691 MOZ_ASSERT(XRE_IsParentProcess())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(XRE_IsParentProcess())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(XRE_IsParentProcess()))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("XRE_IsParentProcess()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 691); AnnotateMozCrashReason("MOZ_ASSERT" "(" "XRE_IsParentProcess()"
")"); do { *((volatile int*)__null) = 691; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
692 Preferences::SetUint(kPrefWin32kExperimentEnrollmentStatus,
693 nsIXULRuntime::eExperimentStatusDisqualified);
694}
695
696static void OnWin32kEnrollmentStatusChanged(const char* aPref, void* aData) {
697 auto newStatusInt =
698 Preferences::GetUint(kPrefWin32kExperimentEnrollmentStatus,
699 nsIXULRuntime::eExperimentStatusUnenrolled);
700 auto newStatus = newStatusInt < nsIXULRuntime::eExperimentStatusCount
701 ? nsIXULRuntime::ExperimentStatus(newStatusInt)
702 : nsIXULRuntime::eExperimentStatusDisqualified;
703
704 // Set the startup pref for next browser start [D]
705 Preferences::SetUint(kPrefWin32kExperimentStartupEnrollmentStatus, newStatus);
706}
707
708namespace {
709// This observer is notified during `profile-before-change`, and ensures that
710// the experiment enrollment status is synced over before the browser shuts
711// down, even if it was not modified since win32k was initialized.
712class Win32kEnrollmentStatusShutdownObserver final : public nsIObserver {
713 public:
714 NS_DECL_ISUPPORTSpublic: virtual nsresult QueryInterface(const nsIID& aIID
, void** aInstancePtr) override; virtual MozExternalRefCountType
AddRef(void) override; virtual MozExternalRefCountType Release
(void) override; using HasThreadSafeRefCnt = std::false_type;
protected: nsAutoRefCnt mRefCnt; nsAutoOwningThread _mOwningThread
; public:
715
716 NS_IMETHODvirtual nsresult Observe(nsISupports* aSubject, const char* aTopic,
717 const char16_t* aData) override {
718 MOZ_ASSERT(!strcmp("profile-before-change", aTopic))do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!strcmp("profile-before-change", aTopic))>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(!strcmp("profile-before-change", aTopic)))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("!strcmp(\"profile-before-change\", aTopic)"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 718); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!strcmp(\"profile-before-change\", aTopic)"
")"); do { *((volatile int*)__null) = 718; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
719 OnWin32kEnrollmentStatusChanged(kPrefWin32kExperimentEnrollmentStatus,
720 nullptr);
721 return NS_OK;
722 }
723
724 private:
725 ~Win32kEnrollmentStatusShutdownObserver() = default;
726};
727NS_IMPL_ISUPPORTS(Win32kEnrollmentStatusShutdownObserver, nsIObserver)MozExternalRefCountType Win32kEnrollmentStatusShutdownObserver
::AddRef(void) { static_assert(!std::is_destructible_v<Win32kEnrollmentStatusShutdownObserver
>, "Reference-counted class " "Win32kEnrollmentStatusShutdownObserver"
" should not have a public destructor. " "Make this class's destructor non-public"
); do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 727); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { *((volatile int*)__null) =
727; __attribute__((nomerge)) ::abort(); } while (false); } }
while (false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("Win32kEnrollmentStatusShutdownObserver" != nullptr
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!("Win32kEnrollmentStatusShutdownObserver" != nullptr)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("\"Win32kEnrollmentStatusShutdownObserver\" != nullptr"
" (" "Must specify a name" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 727); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"Win32kEnrollmentStatusShutdownObserver\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 727; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("Win32kEnrollmentStatusShutdownObserver" " not thread-safe"
); nsrefcnt count = ++mRefCnt; NS_LogAddRef((this), (count), (
"Win32kEnrollmentStatusShutdownObserver"), (uint32_t)(sizeof(
*this))); return count; } MozExternalRefCountType Win32kEnrollmentStatusShutdownObserver
::Release(void) { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(int32_t(mRefCnt) > 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) > 0))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0"
" (" "dup release" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 727); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0"
") (" "dup release" ")"); do { *((volatile int*)__null) = 727
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("Win32kEnrollmentStatusShutdownObserver" != nullptr
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!("Win32kEnrollmentStatusShutdownObserver" != nullptr)
)), 0))) { do { } while (false); MOZ_ReportAssertionFailure("\"Win32kEnrollmentStatusShutdownObserver\" != nullptr"
" (" "Must specify a name" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 727); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"Win32kEnrollmentStatusShutdownObserver\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 727; __attribute__((nomerge)) ::abort(); } while (false);
} } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("Win32kEnrollmentStatusShutdownObserver" " not thread-safe"
); const char* const nametmp = "Win32kEnrollmentStatusShutdownObserver"
; nsrefcnt count = --mRefCnt; NS_LogRelease((this), (count), (
nametmp)); if (count == 0) { mRefCnt = 1; delete (this); return
0; } return count; } nsresult Win32kEnrollmentStatusShutdownObserver
::QueryInterface(const nsIID& aIID, void** aInstancePtr) {
do { if (!(aInstancePtr)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "QueryInterface requires a non-NULL destination!", "aInstancePtr"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 727); MOZ_PretendNoReturn(); } } while (0); nsresult rv = NS_ERROR_FAILURE
; static_assert(1 > 0, "Need more arguments to NS_INTERFACE_TABLE"
); static const QITableEntry table[] = { {&mozilla::detail
::kImplementedIID<Win32kEnrollmentStatusShutdownObserver, nsIObserver
>, int32_t( reinterpret_cast<char*>(static_cast<nsIObserver
*>((Win32kEnrollmentStatusShutdownObserver*)0x1000)) - reinterpret_cast
<char*>((Win32kEnrollmentStatusShutdownObserver*)0x1000
))}, {&mozilla::detail::kImplementedIID<Win32kEnrollmentStatusShutdownObserver
, nsISupports>, int32_t(reinterpret_cast<char*>(static_cast
<nsISupports*>( static_cast<nsIObserver*>((Win32kEnrollmentStatusShutdownObserver
*)0x1000))) - reinterpret_cast<char*>((Win32kEnrollmentStatusShutdownObserver
*)0x1000))}, { nullptr, 0 } } ; static_assert((sizeof(table) /
sizeof(table[0])) > 1, "need at least 1 interface"); rv =
NS_TableDrivenQI(static_cast<void*>(this), aIID, aInstancePtr
, table); return rv; }
728} // namespace
729
730static void OnWin32kChanged(const char* aPref, void* aData) {
731 // If we're actively enrolled in the Win32k experiment, disqualify the user
732 // from the experiment if the Win32k pref is modified. [F]
733 if (Win32kExperimentEnrolled() && Preferences::HasUserValue(kPrefWin32k)) {
734 Win32kExperimentDisqualify();
735 }
736}
737
738#endif // XP_WIN
739
740namespace mozilla {
741void EnsureWin32kInitialized();
742}
743
744nsIXULRuntime::ContentWin32kLockdownState GetLiveWin32kLockdownState() {
745#ifdef XP_WIN
746
747 // HasUserValue The Pref functions can only be called on main thread
748 MOZ_ASSERT(NS_IsMainThread())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(NS_IsMainThread())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(NS_IsMainThread()))), 0))) {
do { } while (false); MOZ_ReportAssertionFailure("NS_IsMainThread()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 748); AnnotateMozCrashReason("MOZ_ASSERT" "(" "NS_IsMainThread()"
")"); do { *((volatile int*)__null) = 748; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
749
750# ifdef MOZ_BACKGROUNDTASKS1
751 if (BackgroundTasks::IsBackgroundTaskMode()) {
752 // Let's bail out before loading all the graphics libs.
753 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByDefault;
754 }
755# endif
756
757 mozilla::EnsureWin32kInitialized();
758 gfxPlatform::GetPlatform();
759
760 if (gSafeMode) {
761 return nsIXULRuntime::ContentWin32kLockdownState::DisabledBySafeMode;
762 }
763
764 if (EnvHasValue("MOZ_ENABLE_WIN32K")) {
765 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByEnvVar;
766 }
767
768 if (!mozilla::BrowserTabsRemoteAutostart()) {
769 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByE10S;
770 }
771
772 // Win32k lockdown is available on Win8+, but we are initially limiting it to
773 // Windows 10 v1709 (build 16299) or later. Before this COM initialization
774 // currently fails if user32.dll has loaded before it is called.
775 if (!IsWin10FallCreatorsUpdateOrLater()) {
776 return nsIXULRuntime::ContentWin32kLockdownState::
777 OperatingSystemNotSupported;
778 }
779
780 {
781 ConflictingMitigationStatus conflictingMitigationStatus = {};
782 if (!detect_win32k_conflicting_mitigations(&conflictingMitigationStatus)) {
783 return nsIXULRuntime::ContentWin32kLockdownState::
784 IncompatibleMitigationPolicy;
785 }
786 if (conflictingMitigationStatus.caller_check ||
787 conflictingMitigationStatus.sim_exec ||
788 conflictingMitigationStatus.stack_pivot) {
789 return nsIXULRuntime::ContentWin32kLockdownState::
790 IncompatibleMitigationPolicy;
791 }
792 }
793
794 // Win32k Lockdown requires Remote WebGL, but it may be disabled on
795 // certain hardware or virtual machines.
796 if (!gfx::gfxVars::AllowWebglOop() || !StaticPrefs::webgl_out_of_process()) {
797 return nsIXULRuntime::ContentWin32kLockdownState::MissingRemoteWebGL;
798 }
799
800 // Some (not sure exactly which) decoders are not compatible
801 if (!PDMFactory::AllDecodersAreRemote()) {
802 return nsIXULRuntime::ContentWin32kLockdownState::DecodersArentRemote;
803 }
804
805 bool prefSetByUser =
806 Preferences::HasUserValue("security.sandbox.content.win32k-disable");
807 bool prefValue = Preferences::GetBool(
808 "security.sandbox.content.win32k-disable", false, PrefValueKind::User);
809 bool defaultValue = Preferences::GetBool(
810 "security.sandbox.content.win32k-disable", false, PrefValueKind::Default);
811
812 if (prefSetByUser) {
813 if (prefValue) {
814 return nsIXULRuntime::ContentWin32kLockdownState::EnabledByUserPref;
815 } else {
816 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByUserPref;
817 }
818 }
819
820 if (gWin32kExperimentStatus ==
821 nsIXULRuntime::ExperimentStatus::eExperimentStatusControl) {
822 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByControlGroup;
823 } else if (gWin32kExperimentStatus ==
824 nsIXULRuntime::ExperimentStatus::eExperimentStatusTreatment) {
825 return nsIXULRuntime::ContentWin32kLockdownState::EnabledByTreatmentGroup;
826 }
827
828 if (defaultValue) {
829 return nsIXULRuntime::ContentWin32kLockdownState::EnabledByDefault;
830 } else {
831 return nsIXULRuntime::ContentWin32kLockdownState::DisabledByDefault;
832 }
833
834#else
835
836 return nsIXULRuntime::ContentWin32kLockdownState::OperatingSystemNotSupported;
837
838#endif
839}
840
841namespace mozilla {
842
843void EnsureWin32kInitialized() {
844 if (gWin32kInitialized) {
845 return;
846 }
847 gWin32kInitialized = true;
848
849#ifdef XP_WIN
850
851 // Initialize the Win32k experiment, configuring win32k's
852 // default, before checking other overrides. This allows opting-out of a
853 // Win32k experiment through about:preferences or about:config from a
854 // safemode session.
855 uint32_t experimentRaw =
856 Preferences::GetUint(kPrefWin32kExperimentStartupEnrollmentStatus,
857 nsIXULRuntime::eExperimentStatusUnenrolled);
858 gWin32kExperimentStatus =
859 experimentRaw < nsIXULRuntime::eExperimentStatusCount
860 ? nsIXULRuntime::ExperimentStatus(experimentRaw)
861 : nsIXULRuntime::eExperimentStatusDisqualified;
862
863 // Watch the experiment enrollment status pref to detect experiment
864 // disqualification, and ensure it is propagated for the next restart.
865 Preferences::RegisterCallback(&OnWin32kEnrollmentStatusChanged,
866 kPrefWin32kExperimentEnrollmentStatus);
867 if (nsCOMPtr<nsIObserverService> observerService =
868 mozilla::services::GetObserverService()) {
869 nsCOMPtr<nsIObserver> shutdownObserver =
870 new Win32kEnrollmentStatusShutdownObserver();
871 observerService->AddObserver(shutdownObserver, "profile-before-change",
872 false);
873 }
874
875 // If the user no longer qualifies because they edited a required pref, check
876 // that. [B] [E]
877 auto tmpStatus = GetLiveWin32kLockdownState();
878 if (Win32kExperimentEnrolled() && Win32kRequirementsUnsatisfied(tmpStatus)) {
879 Win32kExperimentDisqualify();
880 gWin32kExperimentStatus = nsIXULRuntime::eExperimentStatusDisqualified;
881 }
882
883 // If the user has overridden an active experiment by setting a user value for
884 // "security.sandbox.content.win32k-disable", disqualify the user from the
885 // experiment. [A] [F]
886 if (Preferences::HasUserValue(kPrefWin32k) && Win32kExperimentEnrolled()) {
887 Win32kExperimentDisqualify();
888 gWin32kExperimentStatus = nsIXULRuntime::eExperimentStatusDisqualified;
889 }
890
891 // Unlike Fission, we do not configure the default branch based on experiment
892 // enrollment status. Instead we check the startupEnrollmentPref in
893 // GetContentWin32kLockdownState()
894
895 Preferences::RegisterCallback(&OnWin32kChanged, kPrefWin32k);
896
897 // Set the state
898 gWin32kStatus = GetLiveWin32kLockdownState();
899
900#else
901 gWin32kStatus =
902 nsIXULRuntime::ContentWin32kLockdownState::OperatingSystemNotSupported;
903 gWin32kExperimentStatus = nsIXULRuntime::eExperimentStatusUnenrolled;
904
905#endif // XP_WIN
906}
907
908nsIXULRuntime::ContentWin32kLockdownState GetWin32kLockdownState() {
909#ifdef XP_WIN
910
911 mozilla::EnsureWin32kInitialized();
912 return gWin32kStatus;
913
914#else
915
916 return nsIXULRuntime::ContentWin32kLockdownState::OperatingSystemNotSupported;
917
918#endif
919}
920
921} // namespace mozilla
922
923// End Win32k Infrastructure ==========================================
924
925// Fission Infrastructure =============================================
926
927// Fission enablement for the current session is determined once, at startup,
928// and then remains the same for the duration of the session.
929//
930// The following factors determine whether or not Fission is enabled for a
931// session, in order of precedence:
932//
933// - Safe mode: In safe mode, Fission is never enabled.
934//
935// - The MOZ_FORCE_ENABLE_FISSION environment variable: If set to any value,
936// Fission will be enabled.
937//
938// - The 'fission.autostart' preference, if it has been configured by the user.
939static const char kPrefFissionAutostart[] = "fission.autostart";
940
941// The computed FissionAutostart value for the session, read by content
942// processes to initialize gFissionAutostart.
943//
944// This pref is locked, and only configured on the default branch, so should
945// never be persisted in a profile.
946static const char kPrefFissionAutostartSession[] = "fission.autostart.session";
947
948//
949// The computed FissionAutostart value for the session, read by content
950// processes to initialize gFissionAutostart.
951
952static bool gFissionAutostart = false;
953static bool gFissionAutostartInitialized = false;
954static nsIXULRuntime::FissionDecisionStatus gFissionDecisionStatus;
955static void EnsureFissionAutostartInitialized() {
956 if (gFissionAutostartInitialized) {
957 return;
958 }
959 gFissionAutostartInitialized = true;
960
961 if (!XRE_IsParentProcess()) {
962 // This pref is configured for the current session by the parent process.
963 gFissionAutostart = Preferences::GetBool(kPrefFissionAutostartSession,
964 false, PrefValueKind::Default);
965 return;
966 }
967
968 if (!BrowserTabsRemoteAutostart()) {
969 gFissionAutostart = false;
970 if (gBrowserTabsRemoteStatus == kE10sForceDisabled) {
971 gFissionDecisionStatus = nsIXULRuntime::eFissionDisabledByE10sEnv;
972 } else {
973 gFissionDecisionStatus = nsIXULRuntime::eFissionDisabledByE10sOther;
974 }
975 } else if (EnvHasValue("MOZ_FORCE_ENABLE_FISSION")) {
976 gFissionAutostart = true;
977 gFissionDecisionStatus = nsIXULRuntime::eFissionEnabledByEnv;
978 } else if (EnvHasValue("MOZ_FORCE_DISABLE_FISSION")) {
979 gFissionAutostart = false;
980 gFissionDecisionStatus = nsIXULRuntime::eFissionDisabledByEnv;
981 } else {
982 // NOTE: This will take into account changes to the default due to
983 // `InitializeFissionExperimentStatus`.
984 gFissionAutostart = Preferences::GetBool(kPrefFissionAutostart, false);
985 if (Preferences::HasUserValue(kPrefFissionAutostart)) {
986 gFissionDecisionStatus = gFissionAutostart
987 ? nsIXULRuntime::eFissionEnabledByUserPref
988 : nsIXULRuntime::eFissionDisabledByUserPref;
989 } else {
990 gFissionDecisionStatus = gFissionAutostart
991 ? nsIXULRuntime::eFissionEnabledByDefault
992 : nsIXULRuntime::eFissionDisabledByDefault;
993 }
994 }
995
996 // Content processes cannot run the same logic as we're running in the parent
997 // process, as the current value of various preferences may have changed
998 // between launches. Instead, the content process will read the default branch
999 // of the locked `fission.autostart.session` preference to determine the value
1000 // determined by this method.
1001 Preferences::Unlock(kPrefFissionAutostartSession);
1002 Preferences::ClearUser(kPrefFissionAutostartSession);
1003 Preferences::SetBool(kPrefFissionAutostartSession, gFissionAutostart,
1004 PrefValueKind::Default);
1005 Preferences::Lock(kPrefFissionAutostartSession);
1006}
1007
1008namespace mozilla {
1009
1010bool FissionAutostart() {
1011 EnsureFissionAutostartInitialized();
1012 return gFissionAutostart;
1013}
1014
1015} // namespace mozilla
1016
1017// End Fission Infrastructure =========================================
1018
1019namespace mozilla {
1020
1021bool SessionHistoryInParent() {
1022 return FissionAutostart() ||
1023 !StaticPrefs::
1024 fission_disableSessionHistoryInParent_AtStartup_DoNotUseDirectly();
1025}
1026
1027bool SessionStorePlatformCollection() {
1028 return SessionHistoryInParent() &&
1029 !StaticPrefs::
1030 browser_sessionstore_disable_platform_collection_AtStartup_DoNotUseDirectly();
1031}
1032
1033bool BFCacheInParent() {
1034 return SessionHistoryInParent() &&
1035 StaticPrefs::fission_bfcacheInParent_DoNotUseDirectly();
1036}
1037
1038} // namespace mozilla
1039
1040/**
1041 * The nsXULAppInfo object implements nsIFactory so that it can be its own
1042 * singleton.
1043 */
1044class nsXULAppInfo : public nsIXULAppInfo,
1045#ifdef XP_WIN
1046 public nsIWinAppHelper,
1047#endif
1048 public nsICrashReporter,
1049 public nsIFinishDumpingCallback,
1050 public nsIXULRuntime
1051
1052{
1053 public:
1054 constexpr nsXULAppInfo() = default;
1055 NS_DECL_ISUPPORTS_INHERITEDpublic: virtual nsresult QueryInterface(const nsIID& aIID
, void** aInstancePtr) override; virtual MozExternalRefCountType
AddRef(void) override; virtual MozExternalRefCountType Release
(void) override;
1056 NS_DECL_NSIPLATFORMINFOvirtual nsresult GetPlatformVersion(nsACString& aPlatformVersion
) override; virtual nsresult GetPlatformBuildID(nsACString&
aPlatformBuildID) override;
1057 NS_DECL_NSIXULAPPINFOvirtual nsresult GetVendor(nsACString& aVendor) override;
virtual nsresult GetName(nsACString& aName) override; virtual
nsresult GetID(nsACString& aID) override; virtual nsresult
GetVersion(nsACString& aVersion) override; virtual nsresult
GetAppBuildID(nsACString& aAppBuildID) override; virtual
nsresult GetUAName(nsACString& aUAName) override; virtual
nsresult GetSourceURL(nsACString& aSourceURL) override; virtual
nsresult GetUpdateURL(nsACString& aUpdateURL) override;
1058 NS_DECL_NSIXULRUNTIMEvirtual nsresult GetInSafeMode(bool *aInSafeMode) override; virtual
nsresult GetWin32kExperimentStatus(nsIXULRuntime::ExperimentStatus
*aWin32kExperimentStatus) override; virtual nsresult GetWin32kLiveStatusTestingOnly
(nsIXULRuntime::ContentWin32kLockdownState *aWin32kLiveStatusTestingOnly
) override; virtual nsresult GetWin32kSessionStatus(nsIXULRuntime
::ContentWin32kLockdownState *aWin32kSessionStatus) override;
virtual nsresult GetFissionAutostart(bool *aFissionAutostart
) override; virtual nsresult GetFissionDecisionStatus(nsIXULRuntime
::FissionDecisionStatus *aFissionDecisionStatus) override; virtual
nsresult GetFissionDecisionStatusString(nsACString& aFissionDecisionStatusString
) override; virtual nsresult GetSessionHistoryInParent(bool *
aSessionHistoryInParent) override; virtual nsresult GetSessionStorePlatformCollection
(bool *aSessionStorePlatformCollection) override; virtual nsresult
GetLogConsoleErrors(bool *aLogConsoleErrors) override; virtual
nsresult SetLogConsoleErrors(bool aLogConsoleErrors) override
; virtual nsresult GetOS(nsACString& aOS) override; virtual
nsresult GetXPCOMABI(nsACString& aXPCOMABI) override; virtual
nsresult GetWidgetToolkit(nsACString& aWidgetToolkit) override
; virtual nsresult GetProcessType(uint32_t *aProcessType) override
; virtual nsresult GetProcessID(uint32_t *aProcessID) override
; virtual nsresult GetUniqueProcessID(uint64_t *aUniqueProcessID
) override; virtual nsresult GetRemoteType(nsACString& aRemoteType
) override; virtual nsresult GetBrowserTabsRemoteAutostart(bool
*aBrowserTabsRemoteAutostart) override; virtual nsresult GetMaxWebProcessCount
(uint32_t *aMaxWebProcessCount) override; virtual nsresult GetAccessibilityEnabled
(bool *aAccessibilityEnabled) override; virtual nsresult GetAccessibilityInstantiator
(nsAString& aAccessibilityInstantiator) override; virtual
nsresult GetIs64Bit(bool *aIs64Bit) override; virtual nsresult
GetIsTextRecognitionSupported(bool *aIsTextRecognitionSupported
) override; virtual nsresult InvalidateCachesOnRestart(void) override
; virtual nsresult GetReplacedLockTime(PRTime *aReplacedLockTime
) override; virtual nsresult GetDefaultUpdateChannel(nsACString
& aDefaultUpdateChannel) override; virtual nsresult GetDistributionID
(nsACString& aDistributionID) override; virtual nsresult GetWindowsDLLBlocklistStatus
(bool *aWindowsDLLBlocklistStatus) override; virtual nsresult
GetRestartedByOS(bool *aRestartedByOS) override; virtual nsresult
GetChromeColorSchemeIsDark(bool *aChromeColorSchemeIsDark) override
; virtual nsresult GetContentThemeDerivedColorSchemeIsDark(bool
*aContentThemeDerivedColorSchemeIsDark) override; virtual nsresult
GetPrefersReducedMotion(bool *aPrefersReducedMotion) override
; virtual nsresult GetDrawInTitlebar(bool *aDrawInTitlebar) override
; virtual nsresult GetDesktopEnvironment(nsACString& aDesktopEnvironment
) override; virtual nsresult GetIsWayland(bool *aIsWayland) override
; virtual nsresult GetProcessStartupShortcut(nsAString& aProcessStartupShortcut
) override; virtual nsresult GetLauncherProcessState(uint32_t
*aLauncherProcessState) override; virtual nsresult GetLastAppVersion
(nsACString& aLastAppVersion) override; virtual nsresult GetLastAppBuildID
(nsACString& aLastAppBuildID) override;
1059 NS_DECL_NSICRASHREPORTERvirtual nsresult GetCrashReporterEnabled(bool *aCrashReporterEnabled
) override; virtual nsresult SetEnabled(bool enabled) override
; virtual nsresult GetServerURL(nsIURL **aServerURL) override
; virtual nsresult SetServerURL(nsIURL *aServerURL) override;
virtual nsresult GetMinidumpPath(nsIFile **aMinidumpPath) override
; virtual nsresult SetMinidumpPath(nsIFile *aMinidumpPath) override
; virtual nsresult GetMinidumpForID(const nsAString& id, nsIFile
**_retval) override; virtual nsresult GetExtraFileForID(const
nsAString& id, nsIFile **_retval) override; virtual nsresult
AnnotateCrashReport(const nsACString& key, const nsACString
& data) override; virtual nsresult RemoveCrashReportAnnotation
(const nsACString& key) override; virtual nsresult IsAnnotationAllowedForPing
(const nsACString& value, bool *_retval) override; virtual
nsresult AppendAppNotesToCrashReport(const nsACString& data
) override; virtual nsresult RegisterAppMemory(uint64_t ptr, uint64_t
size) override; virtual nsresult WriteMinidumpForException(void
* aExceptionInfo) override; virtual nsresult AppendObjCExceptionInfoToAppNotes
(void * aException) override; virtual nsresult GetSubmitReports
(bool *aSubmitReports) override; virtual nsresult SetSubmitReports
(bool aSubmitReports) override; virtual nsresult UpdateCrashEventsDir
(void) override; virtual nsresult SaveMemoryReport(void) override
;
1060 NS_DECL_NSIFINISHDUMPINGCALLBACKvirtual nsresult Callback(nsISupports *data) override;
1061#ifdef XP_WIN
1062 NS_DECL_NSIWINAPPHELPER
1063#endif
1064};
1065
1066NS_INTERFACE_MAP_BEGIN(nsXULAppInfo)nsresult nsXULAppInfo::QueryInterface(const nsIID& aIID, void
** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak(
NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1066); MOZ_PretendNoReturn(); } } while (0); nsISupports* foundInterface
;
1067 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXULRuntime)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsISupports>)) foundInterface = static_cast
<nsISupports*>(static_cast<nsIXULRuntime*>(this))
; else
1068 NS_INTERFACE_MAP_ENTRY(nsIXULRuntime)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIXULRuntime>)) foundInterface =
static_cast<nsIXULRuntime*>(this); else
1069#ifdef XP_WIN
1070 NS_INTERFACE_MAP_ENTRY(nsIWinAppHelper)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIWinAppHelper>)) foundInterface
= static_cast<nsIWinAppHelper*>(this); else
1071#endif
1072 NS_INTERFACE_MAP_ENTRY(nsICrashReporter)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsICrashReporter>)) foundInterface
= static_cast<nsICrashReporter*>(this); else
1073 NS_INTERFACE_MAP_ENTRY(nsIFinishDumpingCallback)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIFinishDumpingCallback>)) foundInterface
= static_cast<nsIFinishDumpingCallback*>(this); else
1074 NS_INTERFACE_MAP_ENTRY(nsIPlatformInfo)if (aIID.Equals(mozilla::detail::kImplementedIID<std::remove_reference_t
<decltype(*this)>, nsIPlatformInfo>)) foundInterface
= static_cast<nsIPlatformInfo*>(this); else
1075 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIXULAppInfo,if ((gAppData || XRE_IsContentProcess()) && aIID.Equals
(mozilla::detail::kImplementedIID<std::remove_reference_t<
decltype(*this)>, nsIXULAppInfo>)) foundInterface = static_cast
<nsIXULAppInfo*>(this); else
1076 gAppData || XRE_IsContentProcess())if ((gAppData || XRE_IsContentProcess()) && aIID.Equals
(mozilla::detail::kImplementedIID<std::remove_reference_t<
decltype(*this)>, nsIXULAppInfo>)) foundInterface = static_cast
<nsIXULAppInfo*>(this); else
1077NS_INTERFACE_MAP_ENDfoundInterface = 0; nsresult status; if (!foundInterface) { do
{ static_assert( mozilla::detail::AssertionConditionType<
decltype(!aIID.Equals((nsISupports::COMTypeInfo<nsISupports
, void>::kIID)))>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(!aIID.Equals((nsISupports::COMTypeInfo
<nsISupports, void>::kIID))))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1077); AnnotateMozCrashReason("MOZ_ASSERT" "(" "!aIID.Equals((nsISupports::COMTypeInfo<nsISupports, void>::kIID))"
")"); do { *((volatile int*)__null) = 1077; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); status = NS_NOINTERFACE
; } else { (foundInterface)->AddRef(); status = NS_OK; } *
aInstancePtr = foundInterface; return status; }
1078
1079NS_IMETHODIMP_(MozExternalRefCountType)MozExternalRefCountType
1080nsXULAppInfo::AddRef() { return 1; }
1081
1082NS_IMETHODIMP_(MozExternalRefCountType)MozExternalRefCountType
1083nsXULAppInfo::Release() { return 1; }
1084
1085NS_IMETHODIMPnsresult
1086nsXULAppInfo::GetVendor(nsACString& aResult) {
1087 if (XRE_IsContentProcess()) {
1088 ContentChild* cc = ContentChild::GetSingleton();
1089 aResult = cc->GetAppInfo().vendor;
1090 return NS_OK;
1091 }
1092 aResult.Assign(gAppData->vendor);
1093
1094 return NS_OK;
1095}
1096
1097NS_IMETHODIMPnsresult
1098nsXULAppInfo::GetName(nsACString& aResult) {
1099 if (XRE_IsContentProcess()) {
1100 ContentChild* cc = ContentChild::GetSingleton();
1101 aResult = cc->GetAppInfo().name;
1102 return NS_OK;
1103 }
1104
1105#ifdef MOZ_WIDGET_ANDROID
1106 nsCString name = java::GeckoAppShell::GetAppName()->ToCString();
1107 aResult.Assign(std::move(name));
1108#else
1109 aResult.Assign(gAppData->name);
1110#endif
1111
1112 return NS_OK;
1113}
1114
1115NS_IMETHODIMPnsresult
1116nsXULAppInfo::GetID(nsACString& aResult) {
1117 if (XRE_IsContentProcess()) {
1118 ContentChild* cc = ContentChild::GetSingleton();
1119 aResult = cc->GetAppInfo().ID;
1120 return NS_OK;
1121 }
1122 aResult.Assign(gAppData->ID);
1123
1124 return NS_OK;
1125}
1126
1127NS_IMETHODIMPnsresult
1128nsXULAppInfo::GetVersion(nsACString& aResult) {
1129 if (XRE_IsContentProcess()) {
1130 ContentChild* cc = ContentChild::GetSingleton();
1131 aResult = cc->GetAppInfo().version;
1132 return NS_OK;
1133 }
1134 aResult.Assign(gAppData->version);
1135
1136 return NS_OK;
1137}
1138
1139NS_IMETHODIMPnsresult
1140nsXULAppInfo::GetPlatformVersion(nsACString& aResult) {
1141 aResult.Assign(gToolkitVersion);
1142
1143 return NS_OK;
1144}
1145
1146NS_IMETHODIMPnsresult
1147nsXULAppInfo::GetAppBuildID(nsACString& aResult) {
1148 if (XRE_IsContentProcess()) {
1149 ContentChild* cc = ContentChild::GetSingleton();
1150 aResult = cc->GetAppInfo().buildID;
1151 return NS_OK;
1152 }
1153 aResult.Assign(gAppData->buildID);
1154
1155 return NS_OK;
1156}
1157
1158NS_IMETHODIMPnsresult
1159nsXULAppInfo::GetPlatformBuildID(nsACString& aResult) {
1160 aResult.Assign(gToolkitBuildID);
1161
1162 return NS_OK;
1163}
1164
1165NS_IMETHODIMPnsresult
1166nsXULAppInfo::GetUAName(nsACString& aResult) {
1167 if (XRE_IsContentProcess()) {
1168 ContentChild* cc = ContentChild::GetSingleton();
1169 aResult = cc->GetAppInfo().UAName;
1170 return NS_OK;
1171 }
1172 aResult.Assign(gAppData->UAName);
1173
1174 return NS_OK;
1175}
1176
1177NS_IMETHODIMPnsresult
1178nsXULAppInfo::GetSourceURL(nsACString& aResult) {
1179 if (XRE_IsContentProcess()) {
1180 ContentChild* cc = ContentChild::GetSingleton();
1181 aResult = cc->GetAppInfo().sourceURL;
1182 return NS_OK;
1183 }
1184 aResult.Assign(gAppData->sourceURL);
1185
1186 return NS_OK;
1187}
1188
1189NS_IMETHODIMPnsresult
1190nsXULAppInfo::GetUpdateURL(nsACString& aResult) {
1191 if (XRE_IsContentProcess()) {
1192 ContentChild* cc = ContentChild::GetSingleton();
1193 aResult = cc->GetAppInfo().updateURL;
1194 return NS_OK;
1195 }
1196 aResult.Assign(gAppData->updateURL);
1197
1198 return NS_OK;
1199}
1200
1201NS_IMETHODIMPnsresult
1202nsXULAppInfo::GetLogConsoleErrors(bool* aResult) {
1203 *aResult = gLogConsoleErrors;
1204 return NS_OK;
1205}
1206
1207NS_IMETHODIMPnsresult
1208nsXULAppInfo::SetLogConsoleErrors(bool aValue) {
1209 gLogConsoleErrors = aValue;
1210 return NS_OK;
1211}
1212
1213NS_IMETHODIMPnsresult
1214nsXULAppInfo::GetInSafeMode(bool* aResult) {
1215 *aResult = gSafeMode;
1216 return NS_OK;
1217}
1218
1219NS_IMETHODIMPnsresult
1220nsXULAppInfo::GetOS(nsACString& aResult) {
1221 aResult.AssignLiteral(OS_TARGET"Linux");
1222 return NS_OK;
1223}
1224
1225NS_IMETHODIMPnsresult
1226nsXULAppInfo::GetXPCOMABI(nsACString& aResult) {
1227#ifdef TARGET_XPCOM_ABI"x86_64-gcc3"
1228 aResult.AssignLiteral(TARGET_XPCOM_ABI"x86_64-gcc3");
1229 return NS_OK;
1230#else
1231 return NS_ERROR_NOT_AVAILABLE;
1232#endif
1233}
1234
1235NS_IMETHODIMPnsresult
1236nsXULAppInfo::GetWidgetToolkit(nsACString& aResult) {
1237 aResult.AssignLiteral(MOZ_WIDGET_TOOLKIT"gtk");
1238 return NS_OK;
1239}
1240
1241// Ensure that the GeckoProcessType enum, defined in xpcom/build/nsXULAppAPI.h,
1242// is synchronized with the const unsigned longs defined in
1243// xpcom/system/nsIXULRuntime.idl.
1244#define GECKO_PROCESS_TYPE(enum_value, enum_name, string_name, proc_typename, \
1245 process_bin_type, procinfo_typename, \
1246 webidl_typename, allcaps_name) \
1247 static_assert(nsIXULRuntime::PROCESS_TYPE_##allcaps_name == \
1248 static_cast<int>(GeckoProcessType_##enum_name), \
1249 "GeckoProcessType in nsXULAppAPI.h not synchronized with " \
1250 "nsIXULRuntime.idl");
1251#include "mozilla/GeckoProcessTypes.h"
1252#undef GECKO_PROCESS_TYPE
1253
1254// .. and ensure that that is all of them:
1255static_assert(GeckoProcessType_Utility + 1 == GeckoProcessType_End,
1256 "Did not find the final GeckoProcessType");
1257
1258NS_IMETHODIMPnsresult
1259nsXULAppInfo::GetProcessType(uint32_t* aResult) {
1260 NS_ENSURE_ARG_POINTER(aResult)do { if ((__builtin_expect(!!(!(aResult)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "aResult" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1260); return NS_ERROR_INVALID_POINTER; } } while (false)
;
1261 *aResult = XRE_GetProcessType();
1262 return NS_OK;
1263}
1264
1265NS_IMETHODIMPnsresult
1266nsXULAppInfo::GetProcessID(uint32_t* aResult) {
1267#ifdef XP_WIN
1268 *aResult = GetCurrentProcessId();
1269#else
1270 *aResult = getpid();
1271#endif
1272 return NS_OK;
1273}
1274
1275NS_IMETHODIMPnsresult
1276nsXULAppInfo::GetUniqueProcessID(uint64_t* aResult) {
1277 if (XRE_IsContentProcess()) {
1278 ContentChild* cc = ContentChild::GetSingleton();
1279 *aResult = cc->GetID();
1280 } else {
1281 *aResult = 0;
1282 }
1283 return NS_OK;
1284}
1285
1286NS_IMETHODIMPnsresult
1287nsXULAppInfo::GetRemoteType(nsACString& aRemoteType) {
1288 if (XRE_IsContentProcess()) {
1289 aRemoteType = ContentChild::GetSingleton()->GetRemoteType();
1290 } else {
1291 aRemoteType = NOT_REMOTE_TYPEVoidCString();
1292 }
1293
1294 return NS_OK;
1295}
1296
1297static nsCString gLastAppVersion;
1298static nsCString gLastAppBuildID;
1299
1300NS_IMETHODIMPnsresult
1301nsXULAppInfo::GetLastAppVersion(nsACString& aResult) {
1302 if (XRE_IsContentProcess()) {
1303 return NS_ERROR_NOT_AVAILABLE;
1304 }
1305
1306 if (!gLastAppVersion.IsVoid() && gLastAppVersion.IsEmpty()) {
1307 NS_WARNING("Attempt to retrieve lastAppVersion before it has been set.")NS_DebugBreak(NS_DEBUG_WARNING, "Attempt to retrieve lastAppVersion before it has been set."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1307)
;
1308 return NS_ERROR_NOT_AVAILABLE;
1309 }
1310
1311 aResult.Assign(gLastAppVersion);
1312 return NS_OK;
1313}
1314
1315NS_IMETHODIMPnsresult
1316nsXULAppInfo::GetLastAppBuildID(nsACString& aResult) {
1317 if (XRE_IsContentProcess()) {
1318 return NS_ERROR_NOT_AVAILABLE;
1319 }
1320
1321 if (!gLastAppBuildID.IsVoid() && gLastAppBuildID.IsEmpty()) {
1322 NS_WARNING("Attempt to retrieve lastAppBuildID before it has been set.")NS_DebugBreak(NS_DEBUG_WARNING, "Attempt to retrieve lastAppBuildID before it has been set."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1322)
;
1323 return NS_ERROR_NOT_AVAILABLE;
1324 }
1325
1326 aResult.Assign(gLastAppBuildID);
1327 return NS_OK;
1328}
1329
1330NS_IMETHODIMPnsresult
1331nsXULAppInfo::GetFissionAutostart(bool* aResult) {
1332 *aResult = FissionAutostart();
1333 return NS_OK;
1334}
1335
1336NS_IMETHODIMPnsresult
1337nsXULAppInfo::GetWin32kExperimentStatus(ExperimentStatus* aResult) {
1338 if (!XRE_IsParentProcess()) {
1339 return NS_ERROR_NOT_AVAILABLE;
1340 }
1341
1342 EnsureWin32kInitialized();
1343 *aResult = gWin32kExperimentStatus;
1344 return NS_OK;
1345}
1346
1347NS_IMETHODIMPnsresult
1348nsXULAppInfo::GetWin32kLiveStatusTestingOnly(
1349 nsIXULRuntime::ContentWin32kLockdownState* aResult) {
1350 if (!XRE_IsParentProcess()) {
1351 return NS_ERROR_NOT_AVAILABLE;
1352 }
1353
1354 EnsureWin32kInitialized();
1355 *aResult = GetLiveWin32kLockdownState();
1356 return NS_OK;
1357}
1358
1359NS_IMETHODIMPnsresult
1360nsXULAppInfo::GetWin32kSessionStatus(
1361 nsIXULRuntime::ContentWin32kLockdownState* aResult) {
1362 if (!XRE_IsParentProcess()) {
1363 return NS_ERROR_NOT_AVAILABLE;
1364 }
1365
1366 EnsureWin32kInitialized();
1367 *aResult = gWin32kStatus;
1368 return NS_OK;
1369}
1370
1371NS_IMETHODIMPnsresult
1372nsXULAppInfo::GetFissionDecisionStatus(FissionDecisionStatus* aResult) {
1373 if (!XRE_IsParentProcess()) {
1374 return NS_ERROR_NOT_AVAILABLE;
1375 }
1376
1377 EnsureFissionAutostartInitialized();
1378
1379 MOZ_ASSERT(gFissionDecisionStatus != eFissionStatusUnknown)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(gFissionDecisionStatus != eFissionStatusUnknown)>
::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(gFissionDecisionStatus != eFissionStatusUnknown))), 0
))) { do { } while (false); MOZ_ReportAssertionFailure("gFissionDecisionStatus != eFissionStatusUnknown"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1379); AnnotateMozCrashReason("MOZ_ASSERT" "(" "gFissionDecisionStatus != eFissionStatusUnknown"
")"); do { *((volatile int*)__null) = 1379; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1380 *aResult = gFissionDecisionStatus;
1381 return NS_OK;
1382}
1383
1384NS_IMETHODIMPnsresult
1385nsXULAppInfo::GetFissionDecisionStatusString(nsACString& aResult) {
1386 if (!XRE_IsParentProcess()) {
1387 return NS_ERROR_NOT_AVAILABLE;
1388 }
1389
1390 EnsureFissionAutostartInitialized();
1391 switch (gFissionDecisionStatus) {
1392 case eFissionExperimentControl:
1393 aResult = "experimentControl";
1394 break;
1395 case eFissionExperimentTreatment:
1396 aResult = "experimentTreatment";
1397 break;
1398 case eFissionDisabledByE10sEnv:
1399 aResult = "disabledByE10sEnv";
1400 break;
1401 case eFissionEnabledByEnv:
1402 aResult = "enabledByEnv";
1403 break;
1404 case eFissionDisabledByEnv:
1405 aResult = "disabledByEnv";
1406 break;
1407 case eFissionEnabledByDefault:
1408 aResult = "enabledByDefault";
1409 break;
1410 case eFissionDisabledByDefault:
1411 aResult = "disabledByDefault";
1412 break;
1413 case eFissionEnabledByUserPref:
1414 aResult = "enabledByUserPref";
1415 break;
1416 case eFissionDisabledByUserPref:
1417 aResult = "disabledByUserPref";
1418 break;
1419 case eFissionDisabledByE10sOther:
1420 aResult = "disabledByE10sOther";
1421 break;
1422 case eFissionEnabledByRollout:
1423 aResult = "enabledByRollout";
1424 break;
1425 default:
1426 MOZ_ASSERT_UNREACHABLE("Unexpected enum value")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(false)>::isValid, "invalid assertion condition");
if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while (
false); MOZ_ReportAssertionFailure("false" " (" "MOZ_ASSERT_UNREACHABLE: "
"Unexpected enum value" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1426); AnnotateMozCrashReason("MOZ_ASSERT" "(" "false" ") ("
"MOZ_ASSERT_UNREACHABLE: " "Unexpected enum value" ")"); do {
*((volatile int*)__null) = 1426; __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
1427 }
1428 return NS_OK;
1429}
1430
1431NS_IMETHODIMPnsresult
1432nsXULAppInfo::GetSessionHistoryInParent(bool* aResult) {
1433 *aResult = SessionHistoryInParent();
1434 return NS_OK;
1435}
1436
1437NS_IMETHODIMPnsresult
1438nsXULAppInfo::GetSessionStorePlatformCollection(bool* aResult) {
1439 *aResult = SessionStorePlatformCollection();
1440 return NS_OK;
1441}
1442
1443NS_IMETHODIMPnsresult
1444nsXULAppInfo::GetBrowserTabsRemoteAutostart(bool* aResult) {
1445 *aResult = BrowserTabsRemoteAutostart();
1446 return NS_OK;
1447}
1448
1449NS_IMETHODIMPnsresult
1450nsXULAppInfo::GetMaxWebProcessCount(uint32_t* aResult) {
1451 *aResult = mozilla::GetMaxWebProcessCount();
1452 return NS_OK;
1453}
1454
1455NS_IMETHODIMPnsresult
1456nsXULAppInfo::GetAccessibilityEnabled(bool* aResult) {
1457#ifdef ACCESSIBILITY1
1458 *aResult = GetAccService() != nullptr;
1459#else
1460 *aResult = false;
1461#endif
1462 return NS_OK;
1463}
1464
1465NS_IMETHODIMPnsresult
1466nsXULAppInfo::GetAccessibilityInstantiator(nsAString& aInstantiator) {
1467#if defined(ACCESSIBILITY1) && defined(XP_WIN)
1468 if (!GetAccService()) {
1469 aInstantiator.Truncate();
1470 return NS_OK;
1471 }
1472 nsAutoString ipClientInfo;
1473 a11y::Compatibility::GetHumanReadableConsumersStr(ipClientInfo);
1474 aInstantiator.Append(ipClientInfo);
1475 aInstantiator.AppendLiteral("|");
1476
1477 nsCOMPtr<nsIFile> oopClientExe;
1478 if (a11y::GetInstantiator(getter_AddRefs(oopClientExe))) {
1479 nsAutoString oopClientInfo;
1480 if (NS_SUCCEEDED(oopClientExe->GetPath(oopClientInfo))((bool)(__builtin_expect(!!(!NS_FAILED_impl(oopClientExe->
GetPath(oopClientInfo))), 1)))
) {
1481 aInstantiator.Append(oopClientInfo);
1482 }
1483 }
1484#else
1485 aInstantiator.Truncate();
1486#endif
1487 return NS_OK;
1488}
1489
1490NS_IMETHODIMPnsresult
1491nsXULAppInfo::GetIs64Bit(bool* aResult) {
1492#ifdef HAVE_64BIT_BUILD1
1493 *aResult = true;
1494#else
1495 *aResult = false;
1496#endif
1497 return NS_OK;
1498}
1499
1500NS_IMETHODIMPnsresult
1501nsXULAppInfo::GetIsTextRecognitionSupported(bool* aResult) {
1502 *aResult = widget::TextRecognition::IsSupported();
1503 return NS_OK;
1504}
1505
1506NS_IMETHODIMPnsresult
1507nsXULAppInfo::InvalidateCachesOnRestart() {
1508 nsCOMPtr<nsIFile> file;
1509 nsresult rv =
1510 NS_GetSpecialDirectory(NS_APP_PROFILE_DIR_STARTUP"ProfDS", getter_AddRefs(file));
1511 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
1512 if (!file) return NS_ERROR_NOT_AVAILABLE;
1513
1514 file->AppendNative(FILE_COMPATIBILITY_INFO"compatibility.ini"_ns);
1515
1516 nsINIParsernsINIParser_internal parser;
1517 rv = parser.Init(file);
1518 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1519 // This fails if compatibility.ini is not there, so we'll
1520 // flush the caches on the next restart anyways.
1521 return NS_OK;
1522 }
1523
1524 nsAutoCString buf;
1525 rv = parser.GetString("Compatibility", "InvalidateCaches", buf);
1526
1527 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1528 PRFileDesc* fd;
1529 rv = file->OpenNSPRFileDesc(PR_RDWR0x04 | PR_APPEND0x10, 0600, &fd);
1530 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1531 NS_ERROR("could not create output stream")do { NS_DebugBreak(NS_DEBUG_ASSERTION, "could not create output stream"
, "Error", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1531); MOZ_PretendNoReturn(); } while (0)
;
1532 return NS_ERROR_NOT_AVAILABLE;
1533 }
1534 static const char kInvalidationHeader[] =
1535 NS_LINEBREAK"\012" "InvalidateCaches=1" NS_LINEBREAK"\012";
1536 PR_Write(fd, kInvalidationHeader, sizeof(kInvalidationHeader) - 1);
1537 PR_Close(fd);
1538 }
1539 return NS_OK;
1540}
1541
1542NS_IMETHODIMPnsresult
1543nsXULAppInfo::GetReplacedLockTime(PRTime* aReplacedLockTime) {
1544 if (!gProfileLock) return NS_ERROR_NOT_AVAILABLE;
1545 gProfileLock->GetReplacedLockTime(aReplacedLockTime);
1546 return NS_OK;
1547}
1548
1549NS_IMETHODIMPnsresult
1550nsXULAppInfo::GetDefaultUpdateChannel(nsACString& aResult) {
1551 aResult.AssignLiteral(MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL)"default");
1552 return NS_OK;
1553}
1554
1555NS_IMETHODIMPnsresult
1556nsXULAppInfo::GetDistributionID(nsACString& aResult) {
1557 aResult.AssignLiteral(MOZ_DISTRIBUTION_ID"org.mozilla");
1558 return NS_OK;
1559}
1560
1561NS_IMETHODIMPnsresult
1562nsXULAppInfo::GetWindowsDLLBlocklistStatus(bool* aResult) {
1563#if defined(HAS_DLL_BLOCKLIST)
1564 *aResult = DllBlocklist_CheckStatus();
1565#else
1566 *aResult = false;
1567#endif
1568 return NS_OK;
1569}
1570
1571NS_IMETHODIMPnsresult
1572nsXULAppInfo::GetRestartedByOS(bool* aResult) {
1573 *aResult = gRestartedByOS;
1574 return NS_OK;
1575}
1576
1577NS_IMETHODIMPnsresult
1578nsXULAppInfo::GetChromeColorSchemeIsDark(bool* aResult) {
1579 PreferenceSheet::EnsureInitialized();
1580 *aResult = PreferenceSheet::ColorSchemeForChrome() == ColorScheme::Dark;
1581 return NS_OK;
1582}
1583
1584NS_IMETHODIMPnsresult
1585nsXULAppInfo::GetContentThemeDerivedColorSchemeIsDark(bool* aResult) {
1586 *aResult =
1587 PreferenceSheet::ThemeDerivedColorSchemeForContent() == ColorScheme::Dark;
1588 return NS_OK;
1589}
1590
1591NS_IMETHODIMPnsresult
1592nsXULAppInfo::GetPrefersReducedMotion(bool* aResult) {
1593 *aResult =
1594 LookAndFeel::GetInt(LookAndFeel::IntID::PrefersReducedMotion, 0) == 1;
1595 return NS_OK;
1596}
1597
1598NS_IMETHODIMPnsresult
1599nsXULAppInfo::GetDrawInTitlebar(bool* aResult) {
1600 *aResult = LookAndFeel::DrawInTitlebar();
1601 return NS_OK;
1602}
1603
1604NS_IMETHODIMPnsresult
1605nsXULAppInfo::GetDesktopEnvironment(nsACString& aDesktopEnvironment) {
1606#ifdef MOZ_WIDGET_GTK1
1607 aDesktopEnvironment.Assign(GetDesktopEnvironmentIdentifier());
1608#endif
1609 return NS_OK;
1610}
1611
1612NS_IMETHODIMPnsresult
1613nsXULAppInfo::GetIsWayland(bool* aResult) {
1614#ifdef MOZ_WIDGET_GTK1
1615 *aResult = GdkIsWaylandDisplay();
1616#else
1617 *aResult = false;
1618#endif
1619 return NS_OK;
1620}
1621
1622NS_IMETHODIMPnsresult
1623nsXULAppInfo::GetProcessStartupShortcut(nsAString& aShortcut) {
1624#if defined(XP_WIN)
1625 if (XRE_IsParentProcess()) {
1626 aShortcut.Assign(gProcessStartupShortcut);
1627 return NS_OK;
1628 }
1629#endif
1630 return NS_ERROR_NOT_AVAILABLE;
1631}
1632
1633#if defined(XP_WIN) && defined(MOZ_LAUNCHER_PROCESS)
1634// Forward declaration
1635void SetupLauncherProcessPref();
1636
1637static Maybe<LauncherRegistryInfo::EnabledState> gLauncherProcessState;
1638#endif // defined(XP_WIN) && defined(MOZ_LAUNCHER_PROCESS)
1639
1640NS_IMETHODIMPnsresult
1641nsXULAppInfo::GetLauncherProcessState(uint32_t* aResult) {
1642#if defined(XP_WIN) && defined(MOZ_LAUNCHER_PROCESS)
1643 SetupLauncherProcessPref();
1644
1645 if (!gLauncherProcessState) {
1646 return NS_ERROR_UNEXPECTED;
1647 }
1648
1649 *aResult = static_cast<uint32_t>(gLauncherProcessState.value());
1650 return NS_OK;
1651#else
1652 return NS_ERROR_NOT_AVAILABLE;
1653#endif
1654}
1655
1656#ifdef XP_WIN
1657NS_IMETHODIMPnsresult
1658nsXULAppInfo::GetUserCanElevate(bool* aUserCanElevate) {
1659 HANDLE rawToken;
1660 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &rawToken)) {
1661 *aUserCanElevate = false;
1662 return NS_OK;
1663 }
1664
1665 nsAutoHandle token(rawToken);
1666 LauncherResult<TOKEN_ELEVATION_TYPE> elevationType = GetElevationType(token);
1667 if (elevationType.isErr()) {
1668 *aUserCanElevate = false;
1669 return NS_OK;
1670 }
1671
1672 // The possible values returned for elevationType and their meanings are:
1673 // TokenElevationTypeDefault: The token does not have a linked token
1674 // (e.g. UAC disabled or a standard user, so they can't be elevated)
1675 // TokenElevationTypeFull: The token is linked to an elevated token
1676 // (e.g. UAC is enabled and the user is already elevated so they can't
1677 // be elevated again)
1678 // TokenElevationTypeLimited: The token is linked to a limited token
1679 // (e.g. UAC is enabled and the user is not elevated, so they can be
1680 // elevated)
1681 *aUserCanElevate = (elevationType.inspect() == TokenElevationTypeLimited);
1682 return NS_OK;
1683}
1684#endif
1685
1686NS_IMETHODIMPnsresult
1687nsXULAppInfo::GetCrashReporterEnabled(bool* aEnabled) {
1688 *aEnabled = CrashReporter::GetEnabled();
1689 return NS_OK;
1690}
1691
1692NS_IMETHODIMPnsresult
1693nsXULAppInfo::SetEnabled(bool aEnabled) {
1694 if (aEnabled) {
1695 if (CrashReporter::GetEnabled()) {
1696 // no point in erroring for double-enabling
1697 return NS_OK;
1698 }
1699
1700 nsCOMPtr<nsIFile> greBinDir;
1701 NS_GetSpecialDirectory(NS_GRE_BIN_DIR"GreBinD", getter_AddRefs(greBinDir));
1702 if (!greBinDir) {
1703 return NS_ERROR_FAILURE;
1704 }
1705
1706 nsCOMPtr<nsIFile> xreBinDirectory = greBinDir;
1707 if (!xreBinDirectory) {
1708 return NS_ERROR_FAILURE;
1709 }
1710
1711 return CrashReporter::SetExceptionHandler(xreBinDirectory, true);
1712 }
1713
1714 if (!CrashReporter::GetEnabled()) {
1715 // no point in erroring for double-disabling
1716 return NS_OK;
1717 }
1718
1719 return CrashReporter::UnsetExceptionHandler();
1720}
1721
1722NS_IMETHODIMPnsresult
1723nsXULAppInfo::GetServerURL(nsIURL** aServerURL) {
1724 NS_ENSURE_ARG_POINTER(aServerURL)do { if ((__builtin_expect(!!(!(aServerURL)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "aServerURL" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1724); return NS_ERROR_INVALID_POINTER; } } while (false)
;
1725 if (!CrashReporter::GetEnabled()) return NS_ERROR_NOT_INITIALIZED;
1726
1727 nsAutoCString data;
1728 if (!CrashReporter::GetServerURL(data)) {
1729 return NS_ERROR_FAILURE;
1730 }
1731 nsCOMPtr<nsIURI> uri;
1732 NS_NewURI(getter_AddRefs(uri), data);
1733 if (!uri) return NS_ERROR_FAILURE;
1734
1735 nsCOMPtr<nsIURL> url;
1736 url = do_QueryInterface(uri);
1737 NS_ADDREF(*aServerURL = url)(*aServerURL = url)->AddRef();
1738
1739 return NS_OK;
1740}
1741
1742NS_IMETHODIMPnsresult
1743nsXULAppInfo::SetServerURL(nsIURL* aServerURL) {
1744 // Only allow https or http URLs
1745 if (!aServerURL->SchemeIs("http") && !aServerURL->SchemeIs("https")) {
1746 return NS_ERROR_INVALID_ARG;
1747 }
1748
1749 nsAutoCString spec;
1750 nsresult rv = aServerURL->GetSpec(spec);
1751 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1751); return rv; } } while (false)
;
1752
1753 return CrashReporter::SetServerURL(spec);
1754}
1755
1756NS_IMETHODIMPnsresult
1757nsXULAppInfo::GetMinidumpPath(nsIFile** aMinidumpPath) {
1758 if (!CrashReporter::GetEnabled()) return NS_ERROR_NOT_INITIALIZED;
1759
1760 nsAutoString path;
1761 if (!CrashReporter::GetMinidumpPath(path)) return NS_ERROR_FAILURE;
1762
1763 nsresult rv = NS_NewLocalFile(path, false, aMinidumpPath);
1764 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1764); return rv; } } while (false)
;
1765 return NS_OK;
1766}
1767
1768NS_IMETHODIMPnsresult
1769nsXULAppInfo::SetMinidumpPath(nsIFile* aMinidumpPath) {
1770 nsAutoString path;
1771 nsresult rv = aMinidumpPath->GetPath(path);
1772 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1772); return rv; } } while (false)
;
1773 return CrashReporter::SetMinidumpPath(path);
1774}
1775
1776NS_IMETHODIMPnsresult
1777nsXULAppInfo::GetMinidumpForID(const nsAString& aId, nsIFile** aMinidump) {
1778 if (!CrashReporter::GetMinidumpForID(aId, aMinidump)) {
1779 return NS_ERROR_FILE_NOT_FOUND;
1780 }
1781
1782 return NS_OK;
1783}
1784
1785NS_IMETHODIMPnsresult
1786nsXULAppInfo::GetExtraFileForID(const nsAString& aId, nsIFile** aExtraFile) {
1787 if (!CrashReporter::GetExtraFileForID(aId, aExtraFile)) {
1788 return NS_ERROR_FILE_NOT_FOUND;
1789 }
1790
1791 return NS_OK;
1792}
1793
1794NS_IMETHODIMPnsresult
1795nsXULAppInfo::AnnotateCrashReport(const nsACString& key,
1796 const nsACString& data) {
1797 auto annotation = CrashReporter::AnnotationFromString(key);
1798 NS_ENSURE_TRUE(annotation.isSome(), NS_ERROR_INVALID_ARG)do { if ((__builtin_expect(!!(!(annotation.isSome())), 0))) {
NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "annotation.isSome()"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1798); return NS_ERROR_INVALID_ARG; } } while (false)
;
1799 CrashReporter::RecordAnnotationNSCString(*annotation, data);
1800 return NS_OK;
1801}
1802
1803NS_IMETHODIMPnsresult
1804nsXULAppInfo::RemoveCrashReportAnnotation(const nsACString& key) {
1805 auto annotation = CrashReporter::AnnotationFromString(key);
1806 NS_ENSURE_TRUE(annotation.isSome(), NS_ERROR_INVALID_ARG)do { if ((__builtin_expect(!!(!(annotation.isSome())), 0))) {
NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "annotation.isSome()"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1806); return NS_ERROR_INVALID_ARG; } } while (false)
;
1807 CrashReporter::UnrecordAnnotation(*annotation);
1808 return NS_OK;
1809}
1810
1811NS_IMETHODIMPnsresult
1812nsXULAppInfo::IsAnnotationAllowedForPing(const nsACString& aValue,
1813 bool* aIsAllowed) {
1814 auto annotation = CrashReporter::AnnotationFromString(aValue);
1815 NS_ENSURE_TRUE(annotation.isSome(), NS_ERROR_INVALID_ARG)do { if ((__builtin_expect(!!(!(annotation.isSome())), 0))) {
NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "annotation.isSome()"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1815); return NS_ERROR_INVALID_ARG; } } while (false)
;
1816 *aIsAllowed = CrashReporter::IsAnnotationAllowedForPing(*annotation);
1817
1818 return NS_OK;
1819}
1820
1821NS_IMETHODIMPnsresult
1822nsXULAppInfo::AppendAppNotesToCrashReport(const nsACString& data) {
1823 return CrashReporter::AppendAppNotesToCrashReport(data);
1824}
1825
1826NS_IMETHODIMPnsresult
1827nsXULAppInfo::RegisterAppMemory(uint64_t pointer, uint64_t len) {
1828 return CrashReporter::RegisterAppMemory((void*)pointer, len);
1829}
1830
1831NS_IMETHODIMPnsresult
1832nsXULAppInfo::WriteMinidumpForException(void* aExceptionInfo) {
1833#ifdef XP_WIN
1834 return CrashReporter::WriteMinidumpForException(
1835 static_cast<EXCEPTION_POINTERS*>(aExceptionInfo));
1836#else
1837 return NS_ERROR_NOT_IMPLEMENTED;
1838#endif
1839}
1840
1841NS_IMETHODIMPnsresult
1842nsXULAppInfo::AppendObjCExceptionInfoToAppNotes(void* aException) {
1843#ifdef XP_MACOSX
1844 return CrashReporter::AppendObjCExceptionInfoToAppNotes(aException);
1845#else
1846 return NS_ERROR_NOT_IMPLEMENTED;
1847#endif
1848}
1849
1850NS_IMETHODIMPnsresult
1851nsXULAppInfo::GetSubmitReports(bool* aEnabled) {
1852 return CrashReporter::GetSubmitReports(aEnabled);
1853}
1854
1855NS_IMETHODIMPnsresult
1856nsXULAppInfo::SetSubmitReports(bool aEnabled) {
1857 return CrashReporter::SetSubmitReports(aEnabled);
1858}
1859
1860NS_IMETHODIMPnsresult
1861nsXULAppInfo::UpdateCrashEventsDir() {
1862 CrashReporter::UpdateCrashEventsDir();
1863 return NS_OK;
1864}
1865
1866NS_IMETHODIMPnsresult
1867nsXULAppInfo::SaveMemoryReport() {
1868 if (!CrashReporter::GetEnabled()) {
1869 return NS_ERROR_NOT_INITIALIZED;
1870 }
1871 nsCOMPtr<nsIFile> file;
1872 nsresult rv = CrashReporter::GetDefaultMemoryReportFile(getter_AddRefs(file));
1873 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1873)
) {
1874 return rv;
1875 }
1876
1877 nsString path;
1878 file->GetPath(path);
1879
1880 nsCOMPtr<nsIMemoryInfoDumper> dumper =
1881 do_GetService("@mozilla.org/memory-info-dumper;1");
1882 if (NS_WARN_IF(!dumper)NS_warn_if_impl(!dumper, "!dumper", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1882)
) {
1883 return NS_ERROR_UNEXPECTED;
1884 }
1885
1886 rv = dumper->DumpMemoryReportsToNamedFile(
1887 path, this, file, true /* anonymize */, false /* minimizeMemoryUsage */);
1888 if (NS_WARN_IF(NS_FAILED(rv))NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))), "NS_FAILED(rv)", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1888)
) {
1889 return rv;
1890 }
1891 return NS_OK;
1892}
1893
1894// This method is from nsIFInishDumpingCallback.
1895NS_IMETHODIMPnsresult
1896nsXULAppInfo::Callback(nsISupports* aData) {
1897 nsCOMPtr<nsIFile> file = do_QueryInterface(aData);
1898 MOZ_ASSERT(file)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(file)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(file))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("file", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1898); AnnotateMozCrashReason("MOZ_ASSERT" "(" "file" ")");
do { *((volatile int*)__null) = 1898; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
1899
1900 CrashReporter::SetMemoryReportFile(file);
1901 return NS_OK;
1902}
1903
1904static const nsXULAppInfo kAppInfo;
1905namespace mozilla {
1906nsresult AppInfoConstructor(REFNSIIDconst nsIID& aIID, void** aResult) {
1907 return const_cast<nsXULAppInfo*>(&kAppInfo)->QueryInterface(aIID, aResult);
1908}
1909} // namespace mozilla
1910
1911bool gLogConsoleErrors = false;
1912
1913#define NS_ENSURE_TRUE_LOG(x, ret)do { if ((__builtin_expect(!!(!(x)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "x" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1913); gLogConsoleErrors = true; return ret; } } while (0)
\
1914 PR_BEGIN_MACROdo { \
1915 if (MOZ_UNLIKELY(!(x))(__builtin_expect(!!(!(x)), 0))) { \
1916 NS_WARNING("NS_ENSURE_TRUE(" #x ") failed")NS_DebugBreak(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" #x ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1916)
; \
1917 gLogConsoleErrors = true; \
1918 return ret; \
1919 } \
1920 PR_END_MACRO} while (0)
1921
1922#define NS_ENSURE_SUCCESS_LOG(res, ret)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(res)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(res)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1922); gLogConsoleErrors = true; return ret; } } while (0)
\
1923 NS_ENSURE_TRUE_LOG(NS_SUCCEEDED(res), ret)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(res)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(res)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1923); gLogConsoleErrors = true; return ret; } } while (0)
1924
1925/**
1926 * Because we're starting/stopping XPCOM several times in different scenarios,
1927 * this class is a stack-based critter that makes sure that XPCOM is shut down
1928 * during early returns.
1929 */
1930
1931class ScopedXPCOMStartup {
1932 public:
1933 ScopedXPCOMStartup() : mServiceManager(nullptr) {}
1934 ~ScopedXPCOMStartup();
1935
1936 nsresult Initialize(bool aInitJSContext = true);
1937 nsresult SetWindowCreator(nsINativeAppSupport* native);
1938
1939 private:
1940 nsIServiceManager* mServiceManager;
1941 static nsINativeAppSupport* gNativeAppSupport;
1942
1943 friend already_AddRefed<nsINativeAppSupport> NS_GetNativeAppSupport();
1944};
1945
1946ScopedXPCOMStartup::~ScopedXPCOMStartup() {
1947 NS_IF_RELEASE(gNativeAppSupport)do { if (gNativeAppSupport) { (gNativeAppSupport)->Release
(); (gNativeAppSupport) = 0; } } while (0)
;
1948
1949 if (mServiceManager) {
1950#ifdef XP_MACOSX
1951 // On OS X, we need a pool to catch cocoa objects that are autoreleased
1952 // during teardown.
1953 mozilla::MacAutoreleasePool pool;
1954#endif
1955
1956 nsCOMPtr<nsIAppStartup> appStartup(components::AppStartup::Service());
1957 if (appStartup) appStartup->DestroyHiddenWindow();
1958
1959 gDirServiceProvider->DoShutdown();
1960 PROFILER_MARKER_UNTYPED("Shutdown early", OTHER)do { ; do { if (profiler_is_collecting_markers()) { ::profiler_add_marker_impl
("Shutdown early", ::geckoprofiler::category::OTHER); } } while
(false); } while (false)
;
1961
1962 WriteConsoleLog();
1963
1964 NS_ShutdownXPCOM(mServiceManager);
1965 mServiceManager = nullptr;
1966 }
1967}
1968
1969nsresult ScopedXPCOMStartup::Initialize(bool aInitJSContext) {
1970 NS_ASSERTION(gDirServiceProvider, "Should not get here!")do { if (!(gDirServiceProvider)) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Should not get here!", "gDirServiceProvider", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1970); MOZ_PretendNoReturn(); } } while (0)
;
1971
1972 nsresult rv;
1973
1974 rv = NS_InitXPCOM(&mServiceManager, gDirServiceProvider->GetAppDir(),
1975 gDirServiceProvider, aInitJSContext);
1976 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
1977 NS_ERROR("Couldn't start xpcom!")do { NS_DebugBreak(NS_DEBUG_ASSERTION, "Couldn't start xpcom!"
, "Error", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1977); MOZ_PretendNoReturn(); } while (0)
;
1978 mServiceManager = nullptr;
1979 } else {
1980#ifdef DEBUG1
1981 nsCOMPtr<nsIComponentRegistrar> reg = do_QueryInterface(mServiceManager);
1982 NS_ASSERTION(reg, "Service Manager doesn't QI to Registrar.")do { if (!(reg)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Service Manager doesn't QI to Registrar."
, "reg", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 1982); MOZ_PretendNoReturn(); } } while (0)
;
1983#endif
1984 }
1985
1986 return rv;
1987}
1988
1989/**
1990 * This is a little factory class that serves as a singleton-service-factory
1991 * for the nativeappsupport object.
1992 */
1993class nsSingletonFactory final : public nsIFactory {
1994 public:
1995 NS_DECL_ISUPPORTSpublic: virtual nsresult QueryInterface(const nsIID& aIID
, void** aInstancePtr) override; virtual MozExternalRefCountType
AddRef(void) override; virtual MozExternalRefCountType Release
(void) override; using HasThreadSafeRefCnt = std::false_type;
protected: nsAutoRefCnt mRefCnt; nsAutoOwningThread _mOwningThread
; public:
1996 NS_DECL_NSIFACTORYvirtual nsresult CreateInstance(const nsIID & iid, void *
* result) override;
1997
1998 explicit nsSingletonFactory(nsISupports* aSingleton);
1999
2000 private:
2001 ~nsSingletonFactory() = default;
2002 nsCOMPtr<nsISupports> mSingleton;
2003};
2004
2005nsSingletonFactory::nsSingletonFactory(nsISupports* aSingleton)
2006 : mSingleton(aSingleton) {
2007 NS_ASSERTION(mSingleton, "Singleton was null!")do { if (!(mSingleton)) { NS_DebugBreak(NS_DEBUG_ASSERTION, "Singleton was null!"
, "mSingleton", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2007); MOZ_PretendNoReturn(); } } while (0)
;
2008}
2009
2010NS_IMPL_ISUPPORTS(nsSingletonFactory, nsIFactory)MozExternalRefCountType nsSingletonFactory::AddRef(void) { static_assert
(!std::is_destructible_v<nsSingletonFactory>, "Reference-counted class "
"nsSingletonFactory" " should not have a public destructor. "
"Make this class's destructor non-public"); do { static_assert
( mozilla::detail::AssertionConditionType<decltype(int32_t
(mRefCnt) >= 0)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(int32_t(mRefCnt) >= 0))),
0))) { do { } while (false); MOZ_ReportAssertionFailure("int32_t(mRefCnt) >= 0"
" (" "illegal refcnt" ")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2010); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) >= 0"
") (" "illegal refcnt" ")"); do { *((volatile int*)__null) =
2010; __attribute__((nomerge)) ::abort(); } while (false); }
} while (false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("nsSingletonFactory" != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!("nsSingletonFactory" != nullptr
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"\"nsSingletonFactory\" != nullptr" " (" "Must specify a name"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2010); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"nsSingletonFactory\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 2010; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("nsSingletonFactory" " not thread-safe"); nsrefcnt
count = ++mRefCnt; NS_LogAddRef((this), (count), ("nsSingletonFactory"
), (uint32_t)(sizeof(*this))); return count; } MozExternalRefCountType
nsSingletonFactory::Release(void) { do { static_assert( mozilla
::detail::AssertionConditionType<decltype(int32_t(mRefCnt)
> 0)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(int32_t(mRefCnt) > 0))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("int32_t(mRefCnt) > 0" " (" "dup release"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2010); AnnotateMozCrashReason("MOZ_ASSERT" "(" "int32_t(mRefCnt) > 0"
") (" "dup release" ")"); do { *((volatile int*)__null) = 2010
; __attribute__((nomerge)) ::abort(); } while (false); } } while
(false); do { static_assert( mozilla::detail::AssertionConditionType
<decltype("nsSingletonFactory" != nullptr)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!("nsSingletonFactory" != nullptr
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"\"nsSingletonFactory\" != nullptr" " (" "Must specify a name"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2010); AnnotateMozCrashReason("MOZ_ASSERT" "(" "\"nsSingletonFactory\" != nullptr"
") (" "Must specify a name" ")"); do { *((volatile int*)__null
) = 2010; __attribute__((nomerge)) ::abort(); } while (false)
; } } while (false); if (!mRefCnt.isThreadSafe) _mOwningThread
.AssertOwnership("nsSingletonFactory" " not thread-safe"); const
char* const nametmp = "nsSingletonFactory"; nsrefcnt count =
--mRefCnt; NS_LogRelease((this), (count), (nametmp)); if (count
== 0) { mRefCnt = 1; delete (this); return 0; } return count
; } nsresult nsSingletonFactory::QueryInterface(const nsIID&
aIID, void** aInstancePtr) { do { if (!(aInstancePtr)) { NS_DebugBreak
(NS_DEBUG_ASSERTION, "QueryInterface requires a non-NULL destination!"
, "aInstancePtr", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2010); MOZ_PretendNoReturn(); } } while (0); nsresult rv = NS_ERROR_FAILURE
; static_assert(1 > 0, "Need more arguments to NS_INTERFACE_TABLE"
); static const QITableEntry table[] = { {&mozilla::detail
::kImplementedIID<nsSingletonFactory, nsIFactory>, int32_t
( reinterpret_cast<char*>(static_cast<nsIFactory*>
((nsSingletonFactory*)0x1000)) - reinterpret_cast<char*>
((nsSingletonFactory*)0x1000))}, {&mozilla::detail::kImplementedIID
<nsSingletonFactory, nsISupports>, int32_t(reinterpret_cast
<char*>(static_cast<nsISupports*>( static_cast<
nsIFactory*>((nsSingletonFactory*)0x1000))) - reinterpret_cast
<char*>((nsSingletonFactory*)0x1000))}, { nullptr, 0 } }
; static_assert((sizeof(table) / sizeof(table[0])) > 1, "need at least 1 interface"
); rv = NS_TableDrivenQI(static_cast<void*>(this), aIID
, aInstancePtr, table); return rv; }
2011
2012NS_IMETHODIMPnsresult
2013nsSingletonFactory::CreateInstance(const nsIID& aIID, void** aResult) {
2014 return mSingleton->QueryInterface(aIID, aResult);
2015}
2016
2017/**
2018 * Set our windowcreator on the WindowWatcher service.
2019 */
2020nsresult ScopedXPCOMStartup::SetWindowCreator(nsINativeAppSupport* native) {
2021 nsresult rv;
2022
2023 NS_IF_ADDREF(gNativeAppSupport = native)ns_if_addref(gNativeAppSupport = native);
2024
2025 nsCOMPtr<nsIWindowCreator> creator(components::AppStartup::Service());
2026 if (!creator) return NS_ERROR_UNEXPECTED;
2027
2028 nsCOMPtr<nsIWindowWatcher> wwatch(
2029 do_GetService(NS_WINDOWWATCHER_CONTRACTID"@mozilla.org/embedcomp/window-watcher;1", &rv));
2030 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2030); return rv; } } while (false)
;
2031
2032 return wwatch->SetWindowCreator(creator);
2033}
2034
2035/* static */ already_AddRefed<nsINativeAppSupport> NS_GetNativeAppSupport() {
2036 if (!ScopedXPCOMStartup::gNativeAppSupport) {
2037 return nullptr;
2038 }
2039
2040 return do_AddRef(ScopedXPCOMStartup::gNativeAppSupport);
2041}
2042
2043nsINativeAppSupport* ScopedXPCOMStartup::gNativeAppSupport;
2044
2045static void DumpArbitraryHelp() {
2046 nsresult rv;
2047
2048 ScopedLogging log;
2049
2050 {
2051 ScopedXPCOMStartup xpcom;
2052 xpcom.Initialize();
2053
2054 nsCOMPtr<nsICommandLineRunner> cmdline(new nsCommandLine());
2055
2056 nsCString text;
2057 rv = cmdline->GetHelpText(text);
2058 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) printf("%s", text.get());
2059 }
2060}
2061
2062// English text needs to go into a dtd file.
2063// But when this is called we have no components etc. These strings must either
2064// be here, or in a native resource file.
2065static void DumpHelp() {
2066 printf(
2067 "Usage: %s [ options ... ] [URL]\n"
2068 " where options include:\n\n",
2069 gArgv[0]);
2070
2071#ifdef MOZ_X111
2072 printf(
2073 "X11 options\n"
2074 " --display=DISPLAY X display to use\n"
2075 " --sync Make X calls synchronous\n");
2076#endif
2077#ifdef XP_UNIX1
2078 printf(
2079 " --g-fatal-warnings Make all warnings fatal\n"
2080 "\n%s options\n",
2081 (const char*)gAppData->name);
2082#endif
2083
2084 printf(
2085 " -h or --help Print this message.\n"
2086 " -v or --version Print %s version.\n"
2087 " --full-version Print %s version, build and platform build ids.\n"
2088 " -P <profile> Start with <profile>.\n"
2089 " --profile <path> Start with profile at <path>.\n"
2090 " --migration Start with migration wizard.\n"
2091 " --ProfileManager Start with ProfileManager.\n"
2092#ifdef MOZ_HAS_REMOTE1
2093 " --no-remote Do not accept or send remote commands; implies\n"
2094 " --new-instance.\n"
2095 " --new-instance Open new instance, not a new window in running "
2096 "instance.\n"
2097#endif
2098 " --safe-mode Disables extensions and themes for this session.\n"
2099#ifdef MOZ_BLOCK_PROFILE_DOWNGRADE1
2100 " --allow-downgrade Allows downgrading a profile.\n"
2101#endif
2102 " --MOZ_LOG=<modules> Treated as MOZ_LOG=<modules> environment "
2103 "variable,\n"
2104 " overrides it.\n"
2105 " --MOZ_LOG_FILE=<file> Treated as MOZ_LOG_FILE=<file> environment "
2106 "variable,\n"
2107 " overrides it. If MOZ_LOG_FILE is not specified as "
2108 "an\n"
2109 " argument or as an environment variable, logging "
2110 "will be\n"
2111 " written to stdout.\n",
2112 (const char*)gAppData->name, (const char*)gAppData->name);
2113
2114#if defined(XP_WIN)
2115 printf(" --console Start %s with a debugging console.\n",
2116 (const char*)gAppData->name);
2117#endif
2118
2119#if defined(XP_WIN) || defined(MOZ_WIDGET_GTK1) || defined(XP_MACOSX)
2120 printf(" --headless Run without a GUI.\n");
2121#endif
2122
2123#if defined(MOZ_ENABLE_DBUS1)
2124 printf(
2125 " --dbus-service <launcher> Run as DBus service for "
2126 "org.freedesktop.Application and\n"
2127 " set a launcher (usually /usr/bin/appname "
2128 "script) for it.");
2129#endif
2130
2131 // this works, but only after the components have registered. so if you drop
2132 // in a new command line handler, --help won't not until the second run. out
2133 // of the bug, because we ship a component.reg file, it works correctly.
2134 DumpArbitraryHelp();
2135}
2136
2137static inline void DumpVersion() {
2138 if (gAppData->vendor && *gAppData->vendor) {
2139 printf("%s ", (const char*)gAppData->vendor);
2140 }
2141 printf("%s ", (const char*)gAppData->name);
2142
2143 // Use the displayed version
2144 // For example, for beta, we would display 42.0b2 instead of 42.0
2145 printf("%s", MOZ_STRINGIFY(MOZ_APP_VERSION_DISPLAY)"130.0a1");
2146
2147 if (gAppData->copyright && *gAppData->copyright) {
2148 printf(", %s", (const char*)gAppData->copyright);
2149 }
2150 printf("\n");
2151}
2152
2153static inline void DumpFullVersion() {
2154 if (gAppData->vendor && *gAppData->vendor) {
2155 printf("%s ", (const char*)gAppData->vendor);
2156 }
2157 printf("%s ", (const char*)gAppData->name);
2158
2159 // Use the displayed version
2160 // For example, for beta, we would display 42.0b2 instead of 42.0
2161 printf("%s ", MOZ_STRINGIFY(MOZ_APP_VERSION_DISPLAY)"130.0a1");
2162
2163 printf("%s ", (const char*)gAppData->buildID);
2164 printf("%s ", (const char*)PlatformBuildID());
2165 if (gAppData->copyright && *gAppData->copyright) {
2166 printf(", %s", (const char*)gAppData->copyright);
2167 }
2168 printf("\n");
2169}
2170
2171void XRE_InitOmnijar(nsIFile* greOmni, nsIFile* appOmni) {
2172 mozilla::Omnijar::Init(greOmni, appOmni);
2173}
2174
2175nsresult XRE_GetBinaryPath(nsIFile** aResult) {
2176 return mozilla::BinaryPath::GetFile(aResult);
2177}
2178
2179#ifdef XP_WIN
2180# include "nsWindowsRestart.cpp"
2181# include <shellapi.h>
2182
2183typedef BOOL(WINAPI* SetProcessDEPPolicyFunc)(DWORD dwFlags);
2184
2185static void RegisterApplicationRestartChanged(const char* aPref, void* aData) {
2186 DWORD cchCmdLine = 0;
2187 HRESULT rc = ::GetApplicationRestartSettings(::GetCurrentProcess(), nullptr,
2188 &cchCmdLine, nullptr);
2189 bool wasRegistered = false;
2190 if (rc == S_OK) {
2191 wasRegistered = true;
2192 }
2193
2194 if (Preferences::GetBool(PREF_WIN_REGISTER_APPLICATION_RESTART, false) &&
2195 !wasRegistered) {
2196 // Make the command line to use when restarting.
2197 // Excludes argv[0] because RegisterApplicationRestart adds the
2198 // executable name, replace that temporarily with -os-restarted
2199 char* exeName = gRestartArgv[0];
2200 gRestartArgv[0] = const_cast<char*>("-os-restarted");
2201 wchar_t** restartArgvConverted =
2202 AllocConvertUTF8toUTF16Strings(gRestartArgc, gRestartArgv);
2203 gRestartArgv[0] = exeName;
2204
2205 mozilla::UniquePtr<wchar_t[]> restartCommandLine;
2206 if (restartArgvConverted) {
2207 restartCommandLine =
2208 mozilla::MakeCommandLine(gRestartArgc, restartArgvConverted);
2209 FreeAllocStrings(gRestartArgc, restartArgvConverted);
2210 }
2211
2212 if (restartCommandLine) {
2213 // Flags RESTART_NO_PATCH and RESTART_NO_REBOOT are not set, so we
2214 // should be restarted if terminated by an update or restart.
2215 ::RegisterApplicationRestart(restartCommandLine.get(),
2216 RESTART_NO_CRASH | RESTART_NO_HANG);
2217 }
2218 } else if (wasRegistered) {
2219 ::UnregisterApplicationRestart();
2220 }
2221}
2222
2223static void OnAlteredPrefetchPrefChanged(const char* aPref, void* aData) {
2224 int32_t prefVal = Preferences::GetInt(PREF_WIN_ALTERED_DLL_PREFETCH, 0);
2225
2226 mozilla::DllPrefetchExperimentRegistryInfo prefetchRegInfo;
2227 mozilla::DebugOnly<mozilla::Result<Ok, nsresult>> reflectResult =
2228 prefetchRegInfo.ReflectPrefToRegistry(prefVal);
2229
2230 MOZ_ASSERT(reflectResult.value.isOk())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(reflectResult.value.isOk())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(reflectResult.value.isOk()))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("reflectResult.value.isOk()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2230); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reflectResult.value.isOk()"
")"); do { *((volatile int*)__null) = 2230; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2231}
2232
2233static void SetupAlteredPrefetchPref() {
2234 mozilla::DllPrefetchExperimentRegistryInfo prefetchRegInfo;
2235
2236 mozilla::DebugOnly<mozilla::Result<Ok, nsresult>> reflectResult =
2237 prefetchRegInfo.ReflectPrefToRegistry(
2238 Preferences::GetInt(PREF_WIN_ALTERED_DLL_PREFETCH, 0));
2239 MOZ_ASSERT(reflectResult.value.isOk())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(reflectResult.value.isOk())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(reflectResult.value.isOk()))
), 0))) { do { } while (false); MOZ_ReportAssertionFailure("reflectResult.value.isOk()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2239); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reflectResult.value.isOk()"
")"); do { *((volatile int*)__null) = 2239; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2240
2241 Preferences::RegisterCallback(&OnAlteredPrefetchPrefChanged,
2242 PREF_WIN_ALTERED_DLL_PREFETCH);
2243}
2244
2245static void ReflectSkeletonUIPrefToRegistry(const char* aPref, void* aData) {
2246 Unused << aPref;
2247 Unused << aData;
2248
2249 bool shouldBeEnabled =
2250 Preferences::GetBool(kPrefPreXulSkeletonUI, false) &&
2251 Preferences::GetBool(kPrefBrowserStartupBlankWindow, false) &&
2252 LookAndFeel::DrawInTitlebar();
2253 if (shouldBeEnabled && Preferences::HasUserValue(kPrefThemeId)) {
2254 nsCString themeId;
2255 Preferences::GetCString(kPrefThemeId, themeId);
2256 if (themeId.EqualsLiteral("default-theme@mozilla.org")) {
2257 Unused << SetPreXULSkeletonUIThemeId(ThemeMode::Default);
2258 } else if (themeId.EqualsLiteral("firefox-compact-dark@mozilla.org")) {
2259 Unused << SetPreXULSkeletonUIThemeId(ThemeMode::Dark);
2260 } else if (themeId.EqualsLiteral("firefox-compact-light@mozilla.org")) {
2261 Unused << SetPreXULSkeletonUIThemeId(ThemeMode::Light);
2262 } else {
2263 shouldBeEnabled = false;
2264 }
2265 } else if (shouldBeEnabled) {
2266 Unused << SetPreXULSkeletonUIThemeId(ThemeMode::Default);
2267 }
2268
2269 if (GetPreXULSkeletonUIEnabled() != shouldBeEnabled) {
2270 Unused << SetPreXULSkeletonUIEnabledIfAllowed(shouldBeEnabled);
2271 }
2272}
2273
2274static void SetupSkeletonUIPrefs() {
2275 ReflectSkeletonUIPrefToRegistry(nullptr, nullptr);
2276 Preferences::RegisterCallback(&ReflectSkeletonUIPrefToRegistry,
2277 kPrefPreXulSkeletonUI);
2278 Preferences::RegisterCallback(&ReflectSkeletonUIPrefToRegistry,
2279 kPrefBrowserStartupBlankWindow);
2280 Preferences::RegisterCallback(&ReflectSkeletonUIPrefToRegistry, kPrefThemeId);
2281 Preferences::RegisterCallback(
2282 &ReflectSkeletonUIPrefToRegistry,
2283 nsDependentCString(StaticPrefs::GetPrefName_browser_tabs_inTitlebar()));
2284}
2285
2286# if defined(MOZ_LAUNCHER_PROCESS)
2287
2288static void OnLauncherPrefChanged(const char* aPref, void* aData) {
2289 bool prefVal = Preferences::GetBool(PREF_WIN_LAUNCHER_PROCESS_ENABLED, true);
2290
2291 mozilla::LauncherRegistryInfo launcherRegInfo;
2292 mozilla::DebugOnly<mozilla::LauncherVoidResult> reflectResult =
2293 launcherRegInfo.ReflectPrefToRegistry(prefVal);
2294 MOZ_ASSERT(reflectResult.inspect().isOk())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(reflectResult.inspect().isOk())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(reflectResult.inspect().isOk
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("reflectResult.inspect().isOk()", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2294); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reflectResult.inspect().isOk()"
")"); do { *((volatile int*)__null) = 2294; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2295}
2296
2297static void OnLauncherTelemetryPrefChanged(const char* aPref, void* aData) {
2298 bool prefVal = Preferences::GetBool(kPrefHealthReportUploadEnabled, true);
2299
2300 mozilla::LauncherRegistryInfo launcherRegInfo;
2301 mozilla::DebugOnly<mozilla::LauncherVoidResult> reflectResult =
2302 launcherRegInfo.ReflectTelemetryPrefToRegistry(prefVal);
2303 MOZ_ASSERT(reflectResult.inspect().isOk())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(reflectResult.inspect().isOk())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(reflectResult.inspect().isOk
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("reflectResult.inspect().isOk()", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2303); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reflectResult.inspect().isOk()"
")"); do { *((volatile int*)__null) = 2303; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2304}
2305
2306static void SetupLauncherProcessPref() {
2307 if (gLauncherProcessState) {
2308 // We've already successfully run
2309 return;
2310 }
2311
2312 mozilla::LauncherRegistryInfo launcherRegInfo;
2313
2314 mozilla::LauncherResult<mozilla::LauncherRegistryInfo::EnabledState>
2315 enabledState = launcherRegInfo.IsEnabled();
2316
2317 if (enabledState.isOk()) {
2318 gLauncherProcessState = Some(enabledState.unwrap());
2319
2320 CrashReporter::RecordAnnotationU32(
2321 CrashReporter::Annotation::LauncherProcessState,
2322 static_cast<uint32_t>(enabledState.unwrap()));
2323
2324 // Reflect the launcher process registry state into user prefs
2325 Preferences::SetBool(
2326 PREF_WIN_LAUNCHER_PROCESS_ENABLED,
2327 enabledState.unwrap() !=
2328 mozilla::LauncherRegistryInfo::EnabledState::ForceDisabled);
2329 }
2330
2331 mozilla::DebugOnly<mozilla::LauncherVoidResult> reflectResult =
2332 launcherRegInfo.ReflectTelemetryPrefToRegistry(
2333 Preferences::GetBool(kPrefHealthReportUploadEnabled, true));
2334 MOZ_ASSERT(reflectResult.inspect().isOk())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(reflectResult.inspect().isOk())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(reflectResult.inspect().isOk
()))), 0))) { do { } while (false); MOZ_ReportAssertionFailure
("reflectResult.inspect().isOk()", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2334); AnnotateMozCrashReason("MOZ_ASSERT" "(" "reflectResult.inspect().isOk()"
")"); do { *((volatile int*)__null) = 2334; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2335
2336 Preferences::RegisterCallback(&OnLauncherPrefChanged,
2337 PREF_WIN_LAUNCHER_PROCESS_ENABLED);
2338 Preferences::RegisterCallback(&OnLauncherTelemetryPrefChanged,
2339 kPrefHealthReportUploadEnabled);
2340}
2341
2342# endif // defined(MOZ_LAUNCHER_PROCESS)
2343
2344# if defined(MOZ_DEFAULT_BROWSER_AGENT)
2345
2346# define DEFAULT_BROWSER_AGENT_KEY_NAME \
2347 "SOFTWARE\\" MOZ_APP_VENDOR"Mozilla" "\\" MOZ_APP_NAME"firefox" "\\Default Browser Agent"
2348
2349static nsresult PrependRegistryValueName(nsAutoString& aValueName) {
2350 nsresult rv;
2351
2352 nsCOMPtr<nsIFile> binaryPath;
2353 rv = XRE_GetBinaryPath(getter_AddRefs(binaryPath));
2354 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2354); return rv; } } while (false)
;
2355
2356 nsCOMPtr<nsIFile> binaryDir;
2357 rv = binaryPath->GetParent(getter_AddRefs(binaryDir));
2358 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2358); return rv; } } while (false)
;
2359
2360 nsAutoString prefix;
2361 rv = binaryDir->GetPath(prefix);
2362 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2362); return rv; } } while (false)
;
2363
2364 prefix.AppendLiteral("|");
2365 aValueName.Insert(prefix, 0);
2366
2367 return NS_OK;
2368}
2369
2370static void OnDefaultAgentTelemetryPrefChanged(const char* aPref, void* aData) {
2371 nsresult rv;
2372 nsAutoString valueName;
2373 if (strcmp(aPref, kPrefHealthReportUploadEnabled) == 0) {
2374 valueName.AssignLiteral("DisableTelemetry");
2375 } else if (strcmp(aPref, kPrefDefaultAgentEnabled) == 0) {
2376 valueName.AssignLiteral("DisableDefaultBrowserAgent");
2377 } else {
2378 return;
2379 }
2380 rv = PrependRegistryValueName(valueName);
2381 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2381); return; } } while (false)
;
2382
2383 nsCOMPtr<nsIWindowsRegKey> regKey =
2384 do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv);
2385 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2385); return; } } while (false)
;
2386
2387 nsAutoString keyName;
2388 keyName.AppendLiteral(DEFAULT_BROWSER_AGENT_KEY_NAME);
2389 rv = regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, keyName,
2390 nsIWindowsRegKey::ACCESS_WRITE);
2391
2392 bool prefVal = Preferences::GetBool(aPref, true);
2393
2394 // We're recording whether the pref is *disabled*, so invert the value.
2395 rv = regKey->WriteIntValue(valueName, prefVal ? 0 : 1);
2396 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2396); return; } } while (false)
;
2397}
2398
2399static void OnSetDefaultBrowserUserChoicePrefChanged(const char* aPref,
2400 void* aData) {
2401 nsresult rv;
2402 if (strcmp(aPref, kPrefSetDefaultBrowserUserChoicePref) != 0) {
2403 return;
2404 }
2405 nsAutoString valueName;
2406 valueName.AssignLiteral("SetDefaultBrowserUserChoice");
2407 rv = PrependRegistryValueName(valueName);
2408 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2408); return; } } while (false)
;
2409
2410 nsCOMPtr<nsIWindowsRegKey> regKey =
2411 do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv);
2412 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2412); return; } } while (false)
;
2413
2414 nsAutoString keyName;
2415 keyName.AppendLiteral(DEFAULT_BROWSER_AGENT_KEY_NAME);
2416 rv = regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, keyName,
2417 nsIWindowsRegKey::ACCESS_WRITE);
2418
2419 bool prefVal = Preferences::GetBool(aPref, true);
2420
2421 rv = regKey->WriteIntValue(valueName, prefVal);
2422 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2422); return; } } while (false)
;
2423}
2424
2425static void OnDefaultAgentRemoteSettingsPrefChanged(const char* aPref,
2426 void* aData) {
2427 nsresult rv;
2428 nsAutoString valueName;
2429 if (strcmp(aPref, kPrefServicesSettingsServer) == 0) {
2430 valueName.AssignLiteral("ServicesSettingsServer");
2431 } else {
2432 return;
2433 }
2434 rv = PrependRegistryValueName(valueName);
2435 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2435); return; } } while (false)
;
2436
2437 nsCOMPtr<nsIWindowsRegKey> regKey =
2438 do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv);
2439 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2439); return; } } while (false)
;
2440
2441 nsAutoString keyName;
2442 keyName.AppendLiteral(DEFAULT_BROWSER_AGENT_KEY_NAME);
2443 rv = regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, keyName,
2444 nsIWindowsRegKey::ACCESS_WRITE);
2445
2446 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2446); return; } } while (false)
;
2447
2448 nsAutoString prefVal;
2449 rv = Preferences::GetString(aPref, prefVal);
2450 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2451 return;
2452 }
2453
2454 if (prefVal.IsEmpty()) {
2455 rv = regKey->RemoveValue(valueName);
2456 } else {
2457 rv = regKey->WriteStringValue(valueName, prefVal);
2458 }
2459 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2459); return; } } while (false)
;
2460}
2461
2462static void SetDefaultAgentLastRunTime() {
2463 nsresult rv;
2464 nsAutoString valueName;
2465 valueName.AppendLiteral("AppLastRunTime");
2466 rv = PrependRegistryValueName(valueName);
2467 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2467); return; } } while (false)
;
2468
2469 nsCOMPtr<nsIWindowsRegKey> regKey =
2470 do_CreateInstance("@mozilla.org/windows-registry-key;1", &rv);
2471 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2471); return; } } while (false)
;
2472
2473 nsAutoString keyName;
2474 keyName.AppendLiteral(DEFAULT_BROWSER_AGENT_KEY_NAME);
2475 rv = regKey->Create(nsIWindowsRegKey::ROOT_KEY_CURRENT_USER, keyName,
2476 nsIWindowsRegKey::ACCESS_WRITE);
2477 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2477); return; } } while (false)
;
2478
2479 FILETIME fileTime;
2480 GetSystemTimeAsFileTime(&fileTime);
2481
2482 ULARGE_INTEGER integerTime;
2483 integerTime.u.LowPart = fileTime.dwLowDateTime;
2484 integerTime.u.HighPart = fileTime.dwHighDateTime;
2485
2486 rv = regKey->WriteInt64Value(valueName, integerTime.QuadPart);
2487 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2487); return; } } while (false)
;
2488}
2489
2490# endif // defined(MOZ_DEFAULT_BROWSER_AGENT)
2491
2492#endif // XP_WIN
2493
2494void UnlockProfile() {
2495 if (gProfileLock) {
2496 gProfileLock->Unlock();
2497 }
2498}
2499
2500nsresult LaunchChild(bool aBlankCommandLine, bool aTryExec) {
2501 // Restart this process by exec'ing it into the current process
2502 // if supported by the platform. Otherwise, use NSPR.
2503
2504#ifdef MOZ_JPROF
2505 // make sure JPROF doesn't think we're E10s
2506 unsetenv("JPROF_ISCHILD");
2507#endif
2508
2509 if (aBlankCommandLine) {
1
Assuming 'aBlankCommandLine' is false
2
Taking false branch
2510 gRestartArgc = 1;
2511 gRestartArgv[gRestartArgc] = nullptr;
2512 }
2513
2514#if defined(MOZ_HAS_REMOTE1)
2515 if (gRestartWithoutRemote) {
3
Assuming 'gRestartWithoutRemote' is false
4
Taking false branch
2516 SaveToEnv("MOZ_NO_REMOTE=1");
2517 }
2518#endif
2519
2520 SaveToEnv("MOZ_LAUNCHED_CHILD=1");
5
Calling 'SaveToEnv'
2521#if defined(MOZ_LAUNCHER_PROCESS)
2522 SaveToEnv("MOZ_LAUNCHER_PROCESS=1");
2523#endif // defined(MOZ_LAUNCHER_PROCESS)
2524
2525#if !defined(MOZ_WIDGET_ANDROID) // Android has separate restart code.
2526# if defined(XP_MACOSX)
2527 InitializeMacApp();
2528 CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv, true);
2529 LaunchChildMac(gRestartArgc, gRestartArgv);
2530# else
2531 nsCOMPtr<nsIFile> lf;
2532 nsresult rv = XRE_GetBinaryPath(getter_AddRefs(lf));
2533 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
2534
2535# if defined(XP_WIN)
2536 nsAutoString exePath;
2537 rv = lf->GetPath(exePath);
2538 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
2539
2540 HANDLE hProcess;
2541 if (!WinLaunchChild(exePath.get(), gRestartArgc, gRestartArgv, nullptr,
2542 &hProcess))
2543 return NS_ERROR_FAILURE;
2544 // Keep the current process around until the restarted process has created
2545 // its message queue, to avoid the launched process's windows being forced
2546 // into the background.
2547 mozilla::WaitForInputIdle(hProcess);
2548 ::CloseHandle(hProcess);
2549
2550# else
2551 nsAutoCString exePath;
2552 rv = lf->GetNativePath(exePath);
2553 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return rv;
2554
2555# if defined(XP_UNIX1)
2556 if (aTryExec) {
2557 execv(exePath.get(), gRestartArgv);
2558
2559 // If execv returns we know it's because it failed.
2560 return NS_ERROR_FAILURE;
2561 }
2562# endif
2563 if (PR_CreateProcessDetached(exePath.get(), gRestartArgv, nullptr, nullptr) ==
2564 PR_FAILURE) {
2565 return NS_ERROR_FAILURE;
2566 }
2567
2568 // Note that we don't know if the child process starts okay, if it
2569 // immediately returns non-zero then we may mask that by returning a zero
2570 // exit status.
2571
2572# endif // WP_WIN
2573# endif // WP_MACOSX
2574#endif // MOZ_WIDGET_ANDROID
2575
2576 return NS_ERROR_LAUNCHED_CHILD_PROCESS;
2577}
2578
2579static const char kProfileProperties[] =
2580 "chrome://mozapps/locale/profile/profileSelection.properties";
2581
2582namespace {
2583
2584/**
2585 * This class, instead of a raw nsresult, should be the return type of any
2586 * function called by SelectProfile that initializes XPCOM.
2587 */
2588class ReturnAbortOnError {
2589 public:
2590 MOZ_IMPLICIT ReturnAbortOnError(nsresult aRv) { mRv = ConvertRv(aRv); }
2591
2592 operator nsresult() { return mRv; }
2593
2594 private:
2595 inline nsresult ConvertRv(nsresult aRv) {
2596 if (NS_SUCCEEDED(aRv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(aRv)), 1))) || aRv == NS_ERROR_LAUNCHED_CHILD_PROCESS) {
2597 return aRv;
2598 }
2599#ifdef MOZ_BACKGROUNDTASKS1
2600 // A background task that fails to lock its profile will return
2601 // NS_ERROR_UNEXPECTED and this will allow the task to exit with a
2602 // non-zero exit code.
2603 if (aRv == NS_ERROR_UNEXPECTED && BackgroundTasks::IsBackgroundTaskMode()) {
2604 return aRv;
2605 }
2606#endif
2607 return NS_ERROR_ABORT;
2608 }
2609
2610 nsresult mRv;
2611};
2612
2613} // namespace
2614
2615static nsresult ProfileMissingDialog(nsINativeAppSupport* aNative) {
2616#ifdef MOZ_WIDGET_ANDROID
2617 // We cannot really do anything this early during initialization, so we just
2618 // return as this is likely the effect of misconfiguration on the test side.
2619 // Non-test code paths cannot set the profile folder, which is always the
2620 // default one.
2621 Output(true, "Could not find profile folder.\n");
2622 return NS_ERROR_ABORT;
2623#else
2624# ifdef MOZ_BACKGROUNDTASKS1
2625 if (BackgroundTasks::IsBackgroundTaskMode()) {
2626 // We should never get to this point in background task mode.
2627 printf_stderr(
2628 "Could not determine any profile running in backgroundtask mode!\n");
2629 return NS_ERROR_ABORT;
2630 }
2631# endif // MOZ_BACKGROUNDTASKS
2632
2633 nsresult rv;
2634
2635 ScopedXPCOMStartup xpcom;
2636 rv = xpcom.Initialize();
2637 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2637); return rv; } } while (false)
;
2638
2639 rv = xpcom.SetWindowCreator(aNative);
2640 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2640); return NS_ERROR_FAILURE; } } while (false)
;
2641
2642 { // extra scoping is needed so we release these components before xpcom
2643 // shutdown
2644 nsCOMPtr<nsIStringBundleService> sbs =
2645 mozilla::components::StringBundle::Service();
2646 NS_ENSURE_TRUE(sbs, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(sbs)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "sbs" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2646); return NS_ERROR_FAILURE; } } while (false)
;
2647
2648 nsCOMPtr<nsIStringBundle> sb;
2649 sbs->CreateBundle(kProfileProperties, getter_AddRefs(sb));
2650 NS_ENSURE_TRUE_LOG(sbs, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(sbs)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "sbs" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2650); gLogConsoleErrors = true; return NS_ERROR_FAILURE; }
} while (0)
;
2651
2652 NS_ConvertUTF8toUTF16 appName(gAppData->name);
2653 AutoTArray<nsString, 2> params = {appName, appName};
2654
2655 // profileMissing
2656 nsAutoString missingMessage;
2657 rv = sb->FormatStringFromName("profileMissing", params, missingMessage);
2658 NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_ABORT", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2658); return NS_ERROR_ABORT; } } while (false)
;
2659
2660 nsAutoString missingTitle;
2661 params.SetLength(1);
2662 rv = sb->FormatStringFromName("profileMissingTitle", params, missingTitle);
2663 NS_ENSURE_SUCCESS(rv, NS_ERROR_ABORT)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_ABORT", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2663); return NS_ERROR_ABORT; } } while (false)
;
2664
2665 nsCOMPtr<nsIPromptService> ps(do_GetService(NS_PROMPTSERVICE_CONTRACTID"@mozilla.org/prompter;1"));
2666 NS_ENSURE_TRUE(ps, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(ps)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "ps" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2666); return NS_ERROR_FAILURE; } } while (false)
;
2667
2668 ps->Alert(nullptr, missingTitle.get(), missingMessage.get());
2669
2670 return NS_ERROR_ABORT;
2671 }
2672#endif // MOZ_WIDGET_ANDROID
2673}
2674
2675static ReturnAbortOnError ProfileLockedDialog(nsIFile* aProfileDir,
2676 nsIFile* aProfileLocalDir,
2677 nsIProfileUnlocker* aUnlocker,
2678 nsINativeAppSupport* aNative,
2679 nsIProfileLock** aResult) {
2680 nsresult rv;
2681
2682 bool exists;
2683 aProfileDir->Exists(&exists);
2684 if (!exists) {
2685 return ProfileMissingDialog(aNative);
2686 }
2687
2688 ScopedXPCOMStartup xpcom;
2689 rv = xpcom.Initialize();
2690 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2690); return rv; } } while (false)
;
2691
2692#if defined(MOZ_TELEMETRY_REPORTING)
2693 // We cannot check if telemetry has been disabled by the user, yet.
2694 // So, rely on the build time settings, instead.
2695 mozilla::Telemetry::WriteFailedProfileLock(aProfileDir);
2696#endif
2697
2698 rv = xpcom.SetWindowCreator(aNative);
2699 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2699); return NS_ERROR_FAILURE; } } while (false)
;
2700
2701 { // extra scoping is needed so we release these components before xpcom
2702 // shutdown
2703 nsCOMPtr<nsIStringBundleService> sbs =
2704 mozilla::components::StringBundle::Service();
2705 NS_ENSURE_TRUE(sbs, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(sbs)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "sbs" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2705); return NS_ERROR_FAILURE; } } while (false)
;
2706
2707 nsCOMPtr<nsIStringBundle> sb;
2708 sbs->CreateBundle(kProfileProperties, getter_AddRefs(sb));
2709 NS_ENSURE_TRUE_LOG(sbs, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(sbs)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "sbs" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2709); gLogConsoleErrors = true; return NS_ERROR_FAILURE; }
} while (0)
;
2710
2711 NS_ConvertUTF8toUTF16 appName(gAppData->name);
2712 AutoTArray<nsString, 3> params = {appName, appName, appName};
2713
2714 nsAutoString killMessage;
2715#ifndef XP_MACOSX
2716 rv = sb->FormatStringFromName(
2717 aUnlocker ? "restartMessageUnlocker" : "restartMessageNoUnlocker2",
2718 params, killMessage);
2719#else
2720 rv = sb->FormatStringFromName(
2721 aUnlocker ? "restartMessageUnlockerMac" : "restartMessageNoUnlockerMac",
2722 params, killMessage);
2723#endif
2724 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2724); return NS_ERROR_FAILURE; } } while (false)
;
2725
2726 params.SetLength(1);
2727 nsAutoString killTitle;
2728 rv = sb->FormatStringFromName("restartTitle", params, killTitle);
2729 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2729); return NS_ERROR_FAILURE; } } while (false)
;
2730
2731#ifdef MOZ_BACKGROUNDTASKS1
2732 if (BackgroundTasks::IsBackgroundTaskMode()) {
2733 // This error is handled specially to exit with a non-zero exit code.
2734 printf_stderr("%s\n", NS_LossyConvertUTF16toASCII(killMessage).get());
2735 return NS_ERROR_UNEXPECTED;
2736 }
2737#endif
2738
2739 if (gfxPlatform::IsHeadless()) {
2740 // TODO: make a way to turn off all dialogs when headless.
2741 Output(true, "%s\n", NS_LossyConvertUTF16toASCII(killMessage).get());
2742 return NS_ERROR_FAILURE;
2743 }
2744
2745 nsCOMPtr<nsIPromptService> ps(do_GetService(NS_PROMPTSERVICE_CONTRACTID"@mozilla.org/prompter;1"));
2746 NS_ENSURE_TRUE(ps, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(ps)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "ps" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2746); return NS_ERROR_FAILURE; } } while (false)
;
2747
2748 if (aUnlocker) {
2749 int32_t button;
2750#ifdef MOZ_WIDGET_ANDROID
2751 // On Android we always kill the process if the lock is still being held
2752 button = 0;
2753#else
2754 const uint32_t flags = (nsIPromptService::BUTTON_TITLE_IS_STRING *
2755 nsIPromptService::BUTTON_POS_0) +
2756 (nsIPromptService::BUTTON_TITLE_CANCEL *
2757 nsIPromptService::BUTTON_POS_1);
2758
2759 bool checkState = false;
2760 rv = ps->ConfirmEx(nullptr, killTitle.get(), killMessage.get(), flags,
2761 killTitle.get(), nullptr, nullptr, nullptr,
2762 &checkState, &button);
2763 NS_ENSURE_SUCCESS_LOG(rv, rv)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2763); gLogConsoleErrors = true; return rv; } } while (0)
;
2764#endif
2765
2766 if (button == 0) {
2767 rv = aUnlocker->Unlock(nsIProfileUnlocker::FORCE_QUIT);
2768 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
2769 return rv;
2770 }
2771
2772 SaveFileToEnv("XRE_PROFILE_PATH", aProfileDir);
2773 SaveFileToEnv("XRE_PROFILE_LOCAL_PATH", aProfileLocalDir);
2774
2775#if defined(MOZ_HAS_REMOTE1)
2776 if (gRemoteService) {
2777 gRemoteService->UnlockStartup();
2778 gRemoteService = nullptr;
2779 }
2780#endif
2781 return LaunchChild(false, true);
2782 }
2783 } else {
2784 rv = ps->Alert(nullptr, killTitle.get(), killMessage.get());
2785 NS_ENSURE_SUCCESS_LOG(rv, rv)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2785); gLogConsoleErrors = true; return rv; } } while (0)
;
2786 }
2787
2788 return NS_ERROR_ABORT;
2789 }
2790}
2791
2792static const char kProfileManagerURL[] =
2793 "chrome://mozapps/content/profile/profileSelection.xhtml";
2794
2795static ReturnAbortOnError ShowProfileManager(
2796 nsIToolkitProfileService* aProfileSvc, nsINativeAppSupport* aNative) {
2797 nsresult rv;
2798
2799 nsCOMPtr<nsIFile> profD, profLD;
2800 bool offline = false;
2801 int32_t dialogReturn;
2802
2803 {
2804 ScopedXPCOMStartup xpcom;
2805 rv = xpcom.Initialize();
2806 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2806); return rv; } } while (false)
;
2807
2808 rv = xpcom.SetWindowCreator(aNative);
2809 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2809); return NS_ERROR_FAILURE; } } while (false)
;
2810
2811#ifdef XP_MACOSX
2812 InitializeMacApp();
2813 CommandLineServiceMac::SetupMacCommandLine(gRestartArgc, gRestartArgv,
2814 true);
2815#endif
2816
2817 { // extra scoping is needed so we release these components before xpcom
2818 // shutdown
2819 nsCOMPtr<nsIWindowWatcher> windowWatcher(
2820 do_GetService(NS_WINDOWWATCHER_CONTRACTID"@mozilla.org/embedcomp/window-watcher;1"));
2821 nsCOMPtr<nsIDialogParamBlock> ioParamBlock(
2822 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID"@mozilla.org/embedcomp/dialogparam;1"));
2823 nsCOMPtr<nsIMutableArray> dlgArray(
2824 do_CreateInstance(NS_ARRAY_CONTRACTID"@mozilla.org/array;1"));
2825 NS_ENSURE_TRUE(windowWatcher && ioParamBlock && dlgArray,do { if ((__builtin_expect(!!(!(windowWatcher && ioParamBlock
&& dlgArray)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "windowWatcher && ioParamBlock && dlgArray"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2826); return NS_ERROR_FAILURE; } } while (false)
2826 NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(windowWatcher && ioParamBlock
&& dlgArray)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "windowWatcher && ioParamBlock && dlgArray"
") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2826); return NS_ERROR_FAILURE; } } while (false)
;
2827
2828 ioParamBlock->SetObjects(dlgArray);
2829
2830 nsCOMPtr<nsIAppStartup> appStartup(components::AppStartup::Service());
2831 NS_ENSURE_TRUE(appStartup, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(appStartup)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "appStartup" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2831); return NS_ERROR_FAILURE; } } while (false)
;
2832
2833 nsAutoCString features("centerscreen,chrome,modal,titlebar");
2834 // If we're launching a private browsing window make sure to set that
2835 // feature for the Profile Manager window as well, so it groups correctly
2836 // on the Windows taskbar.
2837 if (CheckArgExists("private-window") == ARG_FOUND) {
2838 features.AppendLiteral(",private");
2839 }
2840 nsCOMPtr<mozIDOMWindowProxy> newWindow;
2841 rv = windowWatcher->OpenWindow(
2842 nullptr, nsDependentCString(kProfileManagerURL), "_blank"_ns,
2843 features, ioParamBlock, getter_AddRefs(newWindow));
2844
2845 NS_ENSURE_SUCCESS_LOG(rv, rv)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2845); gLogConsoleErrors = true; return rv; } } while (0)
;
2846
2847 rv = ioParamBlock->GetInt(0, &dialogReturn);
2848 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || dialogReturn == nsIToolkitProfileService::exit) {
2849 return NS_ERROR_ABORT;
2850 }
2851
2852 int32_t startOffline;
2853 rv = ioParamBlock->GetInt(1, &startOffline);
2854 offline = NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && startOffline == 1;
2855
2856 rv = dlgArray->QueryElementAt(0, NS_GET_IID(nsIFile)(nsIFile::COMTypeInfo<nsIFile, void>::kIID),
2857 getter_AddRefs(profD));
2858 NS_ENSURE_SUCCESS_LOG(rv, rv)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2858); gLogConsoleErrors = true; return rv; } } while (0)
;
2859
2860 rv = dlgArray->QueryElementAt(1, NS_GET_IID(nsIFile)(nsIFile::COMTypeInfo<nsIFile, void>::kIID),
2861 getter_AddRefs(profLD));
2862 NS_ENSURE_SUCCESS_LOG(rv, rv)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2862); gLogConsoleErrors = true; return rv; } } while (0)
;
2863 }
2864 }
2865
2866 if (offline) {
2867 SaveToEnv("XRE_START_OFFLINE=1");
2868 }
2869
2870 // User requested that we restart back into the profile manager.
2871 if (dialogReturn == nsIToolkitProfileService::restart) {
2872 SaveToEnv("XRE_RESTART_TO_PROFILE_MANAGER=1");
2873 SaveToEnv("XRE_RESTARTED_BY_PROFILE_MANAGER=1");
2874 } else {
2875 MOZ_ASSERT(dialogReturn == nsIToolkitProfileService::launchWithProfile)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(dialogReturn == nsIToolkitProfileService::launchWithProfile
)>::isValid, "invalid assertion condition"); if ((__builtin_expect
(!!(!(!!(dialogReturn == nsIToolkitProfileService::launchWithProfile
))), 0))) { do { } while (false); MOZ_ReportAssertionFailure(
"dialogReturn == nsIToolkitProfileService::launchWithProfile"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2875); AnnotateMozCrashReason("MOZ_ASSERT" "(" "dialogReturn == nsIToolkitProfileService::launchWithProfile"
")"); do { *((volatile int*)__null) = 2875; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
2876 SaveFileToEnv("XRE_PROFILE_PATH", profD);
2877 SaveFileToEnv("XRE_PROFILE_LOCAL_PATH", profLD);
2878 SaveToEnv("XRE_RESTARTED_BY_PROFILE_MANAGER=1");
2879 }
2880
2881 if (gRestartedByOS) {
2882 // Re-add this argument when actually starting the application.
2883 char** newArgv =
2884 (char**)realloc(gRestartArgv, sizeof(char*) * (gRestartArgc + 2));
2885 NS_ENSURE_TRUE(newArgv, NS_ERROR_OUT_OF_MEMORY)do { if ((__builtin_expect(!!(!(newArgv)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "newArgv" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2885); return NS_ERROR_OUT_OF_MEMORY; } } while (false)
;
2886 gRestartArgv = newArgv;
2887 gRestartArgv[gRestartArgc++] = const_cast<char*>("-os-restarted");
2888 gRestartArgv[gRestartArgc] = nullptr;
2889 }
2890#if defined(MOZ_HAS_REMOTE1)
2891 if (gRemoteService) {
2892 gRemoteService->UnlockStartup();
2893 gRemoteService = nullptr;
2894 }
2895#endif
2896 return LaunchChild(false, true);
2897}
2898
2899static bool gDoMigration = false;
2900static bool gDoProfileReset = false;
2901static nsCOMPtr<nsIToolkitProfile> gResetOldProfile;
2902
2903static nsresult LockProfile(nsINativeAppSupport* aNative, nsIFile* aRootDir,
2904 nsIFile* aLocalDir, nsIToolkitProfile* aProfile,
2905 nsIProfileLock** aResult) {
2906 // If you close Firefox and very quickly reopen it, the old Firefox may
2907 // still be closing down. Rather than immediately showing the
2908 // "Firefox is running but is not responding" message, we spend a few
2909 // seconds retrying first.
2910
2911 static const int kLockRetrySeconds = 5;
2912 static const int kLockRetrySleepMS = 100;
2913
2914 nsresult rv;
2915 nsCOMPtr<nsIProfileUnlocker> unlocker;
2916 const TimeStamp start = TimeStamp::Now();
2917 do {
2918 if (aProfile) {
2919 rv = aProfile->Lock(getter_AddRefs(unlocker), aResult);
2920 } else {
2921 rv = NS_LockProfilePath(aRootDir, aLocalDir, getter_AddRefs(unlocker),
2922 aResult);
2923 }
2924 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
2925 StartupTimeline::Record(StartupTimeline::AFTER_PROFILE_LOCKED);
2926 return NS_OK;
2927 }
2928 PR_Sleep(kLockRetrySleepMS);
2929 } while (TimeStamp::Now() - start <
2930 TimeDuration::FromSeconds(kLockRetrySeconds));
2931
2932 return ProfileLockedDialog(aRootDir, aLocalDir, unlocker, aNative, aResult);
2933}
2934
2935// Pick a profile. We need to end up with a profile root dir, local dir and
2936// potentially an nsIToolkitProfile instance.
2937//
2938// 1) check for --profile <path>
2939// 2) check for -P <name>
2940// 3) check for --ProfileManager
2941// 4) use the default profile, if there is one
2942// 5) if there are *no* profiles, set up profile-migration
2943// 6) display the profile-manager UI
2944static nsresult SelectProfile(nsToolkitProfileService* aProfileSvc,
2945 nsINativeAppSupport* aNative, nsIFile** aRootDir,
2946 nsIFile** aLocalDir, nsIToolkitProfile** aProfile,
2947 bool* aWasDefaultSelection) {
2948 StartupTimeline::Record(StartupTimeline::SELECT_PROFILE);
2949
2950 nsresult rv;
2951
2952 if (EnvHasValue("MOZ_RESET_PROFILE_RESTART")) {
2953 gDoProfileReset = true;
2954 gDoMigration = true;
2955 }
2956
2957 // reset-profile and migration args need to be checked before any profiles are
2958 // chosen below.
2959 ArgResult ar = CheckArg("reset-profile");
2960 if (ar == ARG_FOUND) {
2961 gDoProfileReset = true;
2962 }
2963
2964 ar = CheckArg("migration");
2965 if (ar == ARG_FOUND) {
2966 gDoMigration = true;
2967 }
2968
2969#if defined(XP_WIN)
2970 // This arg is only used to indicate to telemetry that a profile refresh
2971 // (reset+migration) was requested from the uninstaller, pass this along
2972 // via an environment variable for simplicity.
2973 ar = CheckArg("uninstaller-profile-refresh");
2974 if (ar == ARG_FOUND) {
2975 SaveToEnv("MOZ_UNINSTALLER_PROFILE_REFRESH=1");
2976 }
2977#endif
2978
2979 if (EnvHasValue("XRE_RESTART_TO_PROFILE_MANAGER")) {
2980 return ShowProfileManager(aProfileSvc, aNative);
2981 }
2982
2983 // Ask the profile manager to select the profile directories to use.
2984 bool didCreate = false;
2985 rv = aProfileSvc->SelectStartupProfile(&gArgc, gArgv, gDoProfileReset,
2986 aRootDir, aLocalDir, aProfile,
2987 &didCreate, aWasDefaultSelection);
2988
2989 if (rv == NS_ERROR_SHOW_PROFILE_MANAGER) {
2990 return ShowProfileManager(aProfileSvc, aNative);
2991 }
2992
2993 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 2993); return rv; } } while (false)
;
2994
2995 if (didCreate) {
2996 // For a fresh install, we would like to let users decide
2997 // to do profile migration on their own later after using.
2998 gDoProfileReset = false;
2999 gDoMigration = false;
3000 }
3001
3002 if (gDoProfileReset && !*aProfile) {
3003 NS_WARNING("Profile reset is only supported for named profiles.")NS_DebugBreak(NS_DEBUG_WARNING, "Profile reset is only supported for named profiles."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3003)
;
3004 return NS_ERROR_ABORT;
3005 }
3006
3007 // No profile could be found. This generally shouldn't happen, a new profile
3008 // should be created in all cases except for profile reset which is covered
3009 // above, but just in case...
3010 if (!*aRootDir) {
3011 NS_WARNING("Failed to select or create profile.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to select or create profile."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3011)
;
3012 return NS_ERROR_ABORT;
3013 }
3014
3015 return NS_OK;
3016}
3017
3018#ifdef MOZ_BLOCK_PROFILE_DOWNGRADE1
3019struct FileWriteFunc final : public JSONWriteFunc {
3020 FILE* mFile;
3021 explicit FileWriteFunc(FILE* aFile) : mFile(aFile) {}
3022
3023 void Write(const Span<const char>& aStr) final {
3024 fprintf(mFile, "%.*s", int(aStr.size()), aStr.data());
3025 }
3026};
3027
3028static void SubmitDowngradeTelemetry(const nsCString& aLastVersion,
3029 bool aHasSync, int32_t aButton) {
3030 nsCOMPtr<nsIPrefService> prefSvc =
3031 do_GetService("@mozilla.org/preferences-service;1");
3032 NS_ENSURE_TRUE_VOID(prefSvc)do { if ((__builtin_expect(!!(!(prefSvc)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "prefSvc" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3032); return; } } while (false)
;
3033
3034 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(prefSvc);
3035 NS_ENSURE_TRUE_VOID(prefBranch)do { if ((__builtin_expect(!!(!(prefBranch)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "prefBranch" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3035); return; } } while (false)
;
3036
3037 bool enabled;
3038 nsresult rv =
3039 prefBranch->GetBoolPref(kPrefHealthReportUploadEnabled, &enabled);
3040 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3040); return; } } while (false)
;
3041 if (!enabled) {
3042 return;
3043 }
3044
3045 nsCString server;
3046 rv = prefBranch->GetCharPref("toolkit.telemetry.server", server);
3047 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3047); return; } } while (false)
;
3048
3049 nsCString clientId;
3050 rv = prefBranch->GetCharPref("toolkit.telemetry.cachedClientID", clientId);
3051 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3051); return; } } while (false)
;
3052
3053 rv = prefSvc->GetDefaultBranch(nullptr, getter_AddRefs(prefBranch));
3054 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3054); return; } } while (false)
;
3055
3056 nsCString channel("default");
3057 rv = prefBranch->GetCharPref("app.update.channel", channel);
3058 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3058); return; } } while (false)
;
3059
3060 nsID uuid;
3061 rv = nsID::GenerateUUIDInPlace(uuid);
3062 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3062); return; } } while (false)
;
3063
3064 nsCString arch("null");
3065 nsCOMPtr<nsIPropertyBag2> sysInfo =
3066 do_GetService("@mozilla.org/system-info;1");
3067 NS_ENSURE_TRUE_VOID(sysInfo)do { if ((__builtin_expect(!!(!(sysInfo)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "sysInfo" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3067); return; } } while (false)
;
3068 sysInfo->GetPropertyAsACString(u"arch"_ns, arch);
3069
3070 time_t now;
3071 time(&now);
3072 char date[sizeof "YYYY-MM-DDThh:mm:ss.000Z"];
3073 strftime(date, sizeof date, "%FT%T.000Z", gmtime(&now));
3074
3075 NSID_TrimBracketsASCII pingId(uuid);
3076 constexpr auto pingType = "downgrade"_ns;
3077
3078 int32_t pos = aLastVersion.Find("_");
3079 if (pos == kNotFound) {
3080 return;
3081 }
3082
3083 const nsDependentCSubstring lastVersion = Substring(aLastVersion, 0, pos);
3084 const nsDependentCSubstring lastBuildId =
3085 Substring(aLastVersion, pos + 1, 14);
3086
3087 nsPrintfCString url("%s/submit/telemetry/%s/%s/%s/%s/%s/%s?v=%d",
3088 server.get(), PromiseFlatCStringTPromiseFlatString<char>(pingId).get(),
3089 pingType.get(), (const char*)gAppData->name,
3090 (const char*)gAppData->version, channel.get(),
3091 (const char*)gAppData->buildID,
3092 TELEMETRY_PING_FORMAT_VERSION4);
3093
3094 nsCOMPtr<nsIFile> pingFile;
3095 rv = NS_GetSpecialDirectory(XRE_USER_APP_DATA_DIR"UAppData", getter_AddRefs(pingFile));
3096 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3096); return; } } while (false)
;
3097 rv = pingFile->Append(u"Pending Pings"_ns);
3098 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3098); return; } } while (false)
;
3099 rv = pingFile->Create(nsIFile::DIRECTORY_TYPE, 0755);
3100 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) && rv != NS_ERROR_FILE_ALREADY_EXISTS) {
3101 return;
3102 }
3103 rv = pingFile->Append(NS_ConvertUTF8toUTF16(pingId));
3104 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3104); return; } } while (false)
;
3105
3106 nsCOMPtr<nsIFile> pingSender;
3107 rv = NS_GetSpecialDirectory(NS_GRE_BIN_DIR"GreBinD", getter_AddRefs(pingSender));
3108 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3108); return; } } while (false)
;
3109# ifdef XP_WIN
3110 pingSender->Append(u"pingsender.exe"_ns);
3111# else
3112 pingSender->Append(u"pingsender"_ns);
3113# endif
3114
3115 bool exists;
3116 rv = pingSender->Exists(&exists);
3117 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3117); return; } } while (false)
;
3118 if (!exists) {
3119 return;
3120 }
3121
3122 FILE* file;
3123 rv = pingFile->OpenANSIFileDesc("w", &file);
3124 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3124); return; } } while (false)
;
3125
3126 JSONWriter w(MakeUnique<FileWriteFunc>(file));
3127 w.Start();
3128 {
3129 w.StringProperty("type",
3130 Span<const char>(pingType.Data(), pingType.Length()));
3131 w.StringProperty("id", PromiseFlatCStringTPromiseFlatString<char>(pingId));
3132 w.StringProperty("creationDate", MakeStringSpan(date));
3133 w.IntProperty("version", TELEMETRY_PING_FORMAT_VERSION4);
3134 w.StringProperty("clientId", clientId);
3135 w.StartObjectProperty("application");
3136 {
3137 w.StringProperty("architecture", arch);
3138 w.StringProperty(
3139 "buildId",
3140 MakeStringSpan(static_cast<const char*>(gAppData->buildID)));
3141 w.StringProperty(
3142 "name", MakeStringSpan(static_cast<const char*>(gAppData->name)));
3143 w.StringProperty(
3144 "version",
3145 MakeStringSpan(static_cast<const char*>(gAppData->version)));
3146 w.StringProperty("displayVersion",
3147 MOZ_STRINGIFY(MOZ_APP_VERSION_DISPLAY)"130.0a1");
3148 w.StringProperty(
3149 "vendor", MakeStringSpan(static_cast<const char*>(gAppData->vendor)));
3150 w.StringProperty("platformVersion", gToolkitVersion);
3151# ifdef TARGET_XPCOM_ABI"x86_64-gcc3"
3152 w.StringProperty("xpcomAbi", TARGET_XPCOM_ABI"x86_64-gcc3");
3153# else
3154 w.StringProperty("xpcomAbi", "unknown");
3155# endif
3156 w.StringProperty("channel", channel);
3157 }
3158 w.EndObject();
3159 w.StartObjectProperty("payload");
3160 {
3161 w.StringProperty("lastVersion", PromiseFlatCStringTPromiseFlatString<char>(lastVersion));
3162 w.StringProperty("lastBuildId", PromiseFlatCStringTPromiseFlatString<char>(lastBuildId));
3163 w.BoolProperty("hasSync", aHasSync);
3164 w.IntProperty("button", aButton);
3165 }
3166 w.EndObject();
3167 }
3168 w.End();
3169
3170 fclose(file);
3171
3172 PathString filePath = pingFile->NativePath();
3173 const filesystem::Path::value_type* args[2];
3174# ifdef XP_WIN
3175 nsString urlw = NS_ConvertUTF8toUTF16(url);
3176 args[0] = urlw.get();
3177# else
3178 args[0] = url.get();
3179# endif
3180 args[1] = filePath.get();
3181
3182 nsCOMPtr<nsIProcess> process =
3183 do_CreateInstance("@mozilla.org/process/util;1");
3184 NS_ENSURE_TRUE_VOID(process)do { if ((__builtin_expect(!!(!(process)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "process" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3184); return; } } while (false)
;
3185 process->Init(pingSender);
3186 process->SetStartHidden(true);
3187 process->SetNoShell(true);
3188
3189# ifdef XP_WIN
3190 process->Runw(false, args, 2);
3191# else
3192 process->Run(false, args, 2);
3193# endif
3194}
3195
3196static const char kProfileDowngradeURL[] =
3197 "chrome://mozapps/content/profile/profileDowngrade.xhtml";
3198
3199static ReturnAbortOnError CheckDowngrade(nsIFile* aProfileDir,
3200 nsINativeAppSupport* aNative,
3201 nsIToolkitProfileService* aProfileSvc,
3202 const nsCString& aLastVersion) {
3203 int32_t result = 0;
3204 nsresult rv;
3205
3206 {
3207 if (gfxPlatform::IsHeadless()) {
3208 // TODO: make a way to turn off all dialogs when headless.
3209 Output(true,
3210 "This profile was last used with a newer version of this "
3211 "application. Please create a new profile.\n");
3212 return NS_ERROR_ABORT;
3213 }
3214
3215 ScopedXPCOMStartup xpcom;
3216 rv = xpcom.Initialize();
3217 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3217); return rv; } } while (false)
;
3218
3219 rv = xpcom.SetWindowCreator(aNative);
3220 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3220); return rv; } } while (false)
;
3221
3222 { // extra scoping is needed so we release these components before xpcom
3223 // shutdown
3224 bool hasSync = false;
3225 nsCOMPtr<nsIPrefService> prefSvc =
3226 do_GetService("@mozilla.org/preferences-service;1");
3227 NS_ENSURE_TRUE(prefSvc, rv)do { if ((__builtin_expect(!!(!(prefSvc)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "prefSvc" ") failed", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3227); return rv; } } while (false)
;
3228
3229 nsCOMPtr<nsIFile> prefsFile;
3230 rv = aProfileDir->Clone(getter_AddRefs(prefsFile));
3231 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3231); return rv; } } while (false)
;
3232
3233 rv = prefsFile->Append(u"prefs.js"_ns);
3234 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3234); return rv; } } while (false)
;
3235
3236 rv = prefSvc->ReadUserPrefsFromFile(prefsFile);
3237 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
3238 nsCOMPtr<nsIPrefBranch> prefBranch = do_QueryInterface(prefSvc);
3239
3240 rv = prefBranch->PrefHasUserValue("services.sync.username", &hasSync);
3241 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3241); return rv; } } while (false)
;
3242 }
3243
3244 nsCOMPtr<nsIWindowWatcher> windowWatcher =
3245 do_GetService(NS_WINDOWWATCHER_CONTRACTID"@mozilla.org/embedcomp/window-watcher;1");
3246 NS_ENSURE_TRUE(windowWatcher, NS_ERROR_ABORT)do { if ((__builtin_expect(!!(!(windowWatcher)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "windowWatcher" ") failed"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3246); return NS_ERROR_ABORT; } } while (false)
;
3247
3248 nsCOMPtr<nsIAppStartup> appStartup(components::AppStartup::Service());
3249 NS_ENSURE_TRUE(appStartup, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(appStartup)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "appStartup" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3249); return NS_ERROR_FAILURE; } } while (false)
;
3250
3251 nsCOMPtr<nsIDialogParamBlock> paramBlock =
3252 do_CreateInstance(NS_DIALOGPARAMBLOCK_CONTRACTID"@mozilla.org/embedcomp/dialogparam;1");
3253 NS_ENSURE_TRUE(paramBlock, NS_ERROR_ABORT)do { if ((__builtin_expect(!!(!(paramBlock)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "paramBlock" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3253); return NS_ERROR_ABORT; } } while (false)
;
3254
3255 uint8_t flags = 0;
3256 if (hasSync) {
3257 flags |= nsIToolkitProfileService::hasSync;
3258 }
3259
3260 paramBlock->SetInt(0, flags);
3261
3262 nsAutoCString features("centerscreen,chrome,modal,titlebar");
3263 // If we're launching a private browsing window make sure to set that
3264 // feature for the Profile Manager window as well, so it groups correctly
3265 // on the Windows taskbar.
3266 if (CheckArgExists("private-window") == ARG_FOUND) {
3267 features.AppendLiteral(",private");
3268 }
3269 nsCOMPtr<mozIDOMWindowProxy> newWindow;
3270 rv = windowWatcher->OpenWindow(
3271 nullptr, nsDependentCString(kProfileDowngradeURL), "_blank"_ns,
3272 features, paramBlock, getter_AddRefs(newWindow));
3273 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3273); return rv; } } while (false)
;
3274
3275 paramBlock->GetInt(1, &result);
3276
3277 SubmitDowngradeTelemetry(aLastVersion, hasSync, result);
3278 }
3279 }
3280
3281 if (result == nsIToolkitProfileService::createNewProfile) {
3282 // Create a new profile and start it.
3283 nsCString profileName;
3284 profileName.AssignLiteral("default");
3285# ifdef MOZ_DEDICATED_PROFILES1
3286 profileName.Append("-" MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL)"default");
3287# endif
3288 nsCOMPtr<nsIToolkitProfile> newProfile;
3289 rv = aProfileSvc->CreateUniqueProfile(nullptr, profileName,
3290 getter_AddRefs(newProfile));
3291 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3291); return rv; } } while (false)
;
3292 rv = aProfileSvc->SetDefaultProfile(newProfile);
3293 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3293); return rv; } } while (false)
;
3294 rv = aProfileSvc->Flush();
3295 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3295); return rv; } } while (false)
;
3296
3297 nsCOMPtr<nsIFile> profD, profLD;
3298 rv = newProfile->GetRootDir(getter_AddRefs(profD));
3299 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3299); return rv; } } while (false)
;
3300 rv = newProfile->GetLocalDir(getter_AddRefs(profLD));
3301 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3301); return rv; } } while (false)
;
3302
3303 SaveFileToEnv("XRE_PROFILE_PATH", profD);
3304 SaveFileToEnv("XRE_PROFILE_LOCAL_PATH", profLD);
3305
3306 return LaunchChild(false, true);
3307 }
3308
3309 // Cancel
3310 return NS_ERROR_ABORT;
3311}
3312#endif
3313
3314/**
3315 * Extracts the various parts of a compatibility version string.
3316 *
3317 * Compatibility versions are of the form
3318 * "<appversion>_<appbuildid>/<platformbuildid>". The toolkit version comparator
3319 * can only handle 32-bit numbers and in the normal case build IDs are larger
3320 * than this. So if the build ID is numeric we split it into two version parts.
3321 */
3322static void ExtractCompatVersionInfo(const nsACString& aCompatVersion,
3323 nsACString& aAppVersion,
3324 nsACString& aAppBuildID) {
3325 int32_t underscorePos = aCompatVersion.FindChar('_');
3326 int32_t slashPos = aCompatVersion.FindChar('/');
3327
3328 if (underscorePos == kNotFound || slashPos == kNotFound ||
3329 slashPos < underscorePos) {
3330 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, "compatibility.ini Version string does not match the expected format."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3331)
3331 "compatibility.ini Version string does not match the expected format.")NS_DebugBreak(NS_DEBUG_WARNING, "compatibility.ini Version string does not match the expected format."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3331)
;
3332
3333 // Fall back to just using the entire string as the version.
3334 aAppVersion = aCompatVersion;
3335 aAppBuildID.Truncate(0);
3336 return;
3337 }
3338
3339 aAppVersion = Substring(aCompatVersion, 0, underscorePos);
3340 aAppBuildID = Substring(aCompatVersion, underscorePos + 1,
3341 slashPos - (underscorePos + 1));
3342}
3343
3344/**
3345 * Compares the provided compatibility versions. Returns 0 if they match,
3346 * < 0 if the new version is considered an upgrade from the old version and
3347 * > 0 if the new version is considered a downgrade from the old version.
3348 */
3349int32_t CompareCompatVersions(const nsACString& aOldCompatVersion,
3350 const nsACString& aNewCompatVersion) {
3351 // Hardcode the case where the last run was in safe mode (Bug 1556612). We
3352 // cannot tell if this is a downgrade or not so just assume it isn't and let
3353 // the user proceed.
3354 if (aOldCompatVersion.EqualsLiteral("Safe Mode")) {
3355 return -1;
3356 }
3357
3358 // Extract the major version part from the version string and only use that
3359 // for version comparison.
3360 int32_t index = aOldCompatVersion.FindChar('.');
3361 const nsACString& oldMajorVersion = Substring(
3362 aOldCompatVersion, 0, index < 0 ? aOldCompatVersion.Length() : index);
3363 index = aNewCompatVersion.FindChar('.');
3364 const nsACString& newMajorVersion = Substring(
3365 aNewCompatVersion, 0, index < 0 ? aNewCompatVersion.Length() : index);
3366
3367 return CompareVersions(PromiseFlatCStringTPromiseFlatString<char>(oldMajorVersion).get(),
3368 PromiseFlatCStringTPromiseFlatString<char>(newMajorVersion).get());
3369}
3370
3371/**
3372 * Checks the compatibility.ini file to see if we have updated our application
3373 * or otherwise invalidated our caches. If the application has been updated,
3374 * we return false; otherwise, we return true.
3375 *
3376 * We also write the status of the caches (valid/invalid) into the return param
3377 * aCachesOK. The aCachesOK is always invalid if the application has been
3378 * updated.
3379 *
3380 * Finally, aIsDowngrade is set to true if the current application is older
3381 * than that previously used by the profile.
3382 */
3383static bool CheckCompatibility(nsIFile* aProfileDir, const nsCString& aVersion,
3384 const nsCString& aOSABI, nsIFile* aXULRunnerDir,
3385 nsIFile* aAppDir, nsIFile* aFlagFile,
3386 bool* aCachesOK, bool* aIsDowngrade,
3387 nsCString& aLastVersion) {
3388 *aCachesOK = false;
3389 *aIsDowngrade = false;
3390 gLastAppVersion.SetIsVoid(true);
3391 gLastAppBuildID.SetIsVoid(true);
3392
3393 nsCOMPtr<nsIFile> file;
3394 aProfileDir->Clone(getter_AddRefs(file));
3395 if (!file) return false;
3396 file->AppendNative(FILE_COMPATIBILITY_INFO"compatibility.ini"_ns);
3397
3398 nsINIParsernsINIParser_internal parser;
3399 nsresult rv = parser.Init(file);
3400 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3401
3402 rv = parser.GetString("Compatibility", "LastVersion", aLastVersion);
3403 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
3404 return false;
3405 }
3406
3407 if (!aLastVersion.Equals(aVersion)) {
3408 // The version is not the same. Whether it's a downgrade depends on an
3409 // actual comparison:
3410 *aIsDowngrade = 0 < CompareCompatVersions(aLastVersion, aVersion);
3411 ExtractCompatVersionInfo(aLastVersion, gLastAppVersion, gLastAppBuildID);
3412 return false;
3413 }
3414
3415 // If we get here, the version matched, but there may still be other
3416 // differences between us and the build that the profile last ran under.
3417
3418 gLastAppVersion.Assign(gAppData->version);
3419 gLastAppBuildID.Assign(gAppData->buildID);
3420
3421 nsAutoCString buf;
3422 rv = parser.GetString("Compatibility", "LastOSABI", buf);
3423 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !aOSABI.Equals(buf)) return false;
3424
3425 rv = parser.GetString("Compatibility", "LastPlatformDir", buf);
3426 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3427
3428 nsCOMPtr<nsIFile> lf;
3429 rv = NS_NewNativeLocalFile(""_ns, false, getter_AddRefs(lf));
3430 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3431
3432 rv = lf->SetPersistentDescriptor(buf);
3433 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3434
3435 bool eq;
3436 rv = lf->Equals(aXULRunnerDir, &eq);
3437 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !eq) return false;
3438
3439 if (aAppDir) {
3440 rv = parser.GetString("Compatibility", "LastAppDir", buf);
3441 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3442
3443 rv = NS_NewNativeLocalFile(""_ns, false, getter_AddRefs(lf));
3444 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3445
3446 rv = lf->SetPersistentDescriptor(buf);
3447 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return false;
3448
3449 rv = lf->Equals(aAppDir, &eq);
3450 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !eq) return false;
3451 }
3452
3453 // If we see this flag, caches are invalid.
3454 rv = parser.GetString("Compatibility", "InvalidateCaches", buf);
3455 *aCachesOK = (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !buf.EqualsLiteral("1"));
3456
3457 bool purgeCaches = false;
3458 if (aFlagFile && NS_SUCCEEDED(aFlagFile->Exists(&purgeCaches))((bool)(__builtin_expect(!!(!NS_FAILED_impl(aFlagFile->Exists
(&purgeCaches))), 1)))
&&
3459 purgeCaches) {
3460 *aCachesOK = false;
3461 }
3462
3463 return true;
3464}
3465
3466void BuildCompatVersion(const char* aAppVersion, const char* aAppBuildID,
3467 const char* aToolkitBuildID, nsACString& aBuf) {
3468 aBuf.Assign(aAppVersion);
3469 aBuf.Append('_');
3470 aBuf.Append(aAppBuildID);
3471 aBuf.Append('/');
3472 aBuf.Append(aToolkitBuildID);
3473}
3474
3475static void BuildVersion(nsCString& aBuf) {
3476 BuildCompatVersion(gAppData->version, gAppData->buildID, gToolkitBuildID,
3477 aBuf);
3478}
3479
3480static void WriteVersion(nsIFile* aProfileDir, const nsCString& aVersion,
3481 const nsCString& aOSABI, nsIFile* aXULRunnerDir,
3482 nsIFile* aAppDir, bool invalidateCache) {
3483 nsCOMPtr<nsIFile> file;
3484 aProfileDir->Clone(getter_AddRefs(file));
3485 if (!file) return;
3486 file->AppendNative(FILE_COMPATIBILITY_INFO"compatibility.ini"_ns);
3487
3488 nsAutoCString platformDir;
3489 Unused << aXULRunnerDir->GetPersistentDescriptor(platformDir);
3490
3491 nsAutoCString appDir;
3492 if (aAppDir) Unused << aAppDir->GetPersistentDescriptor(appDir);
3493
3494 PRFileDesc* fd;
3495 nsresult rv = file->OpenNSPRFileDesc(PR_WRONLY0x02 | PR_CREATE_FILE0x08 | PR_TRUNCATE0x20,
3496 0600, &fd);
3497 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
3498 NS_ERROR("could not create output stream")do { NS_DebugBreak(NS_DEBUG_ASSERTION, "could not create output stream"
, "Error", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3498); MOZ_PretendNoReturn(); } while (0)
;
3499 return;
3500 }
3501
3502 static const char kHeader[] = "[Compatibility]" NS_LINEBREAK"\012" "LastVersion=";
3503
3504 PR_Write(fd, kHeader, sizeof(kHeader) - 1);
3505 PR_Write(fd, aVersion.get(), aVersion.Length());
3506
3507 static const char kOSABIHeader[] = NS_LINEBREAK"\012" "LastOSABI=";
3508 PR_Write(fd, kOSABIHeader, sizeof(kOSABIHeader) - 1);
3509 PR_Write(fd, aOSABI.get(), aOSABI.Length());
3510
3511 static const char kPlatformDirHeader[] = NS_LINEBREAK"\012" "LastPlatformDir=";
3512
3513 PR_Write(fd, kPlatformDirHeader, sizeof(kPlatformDirHeader) - 1);
3514 PR_Write(fd, platformDir.get(), platformDir.Length());
3515
3516 static const char kAppDirHeader[] = NS_LINEBREAK"\012" "LastAppDir=";
3517 if (aAppDir) {
3518 PR_Write(fd, kAppDirHeader, sizeof(kAppDirHeader) - 1);
3519 PR_Write(fd, appDir.get(), appDir.Length());
3520 }
3521
3522 static const char kInvalidationHeader[] = NS_LINEBREAK"\012" "InvalidateCaches=1";
3523 if (invalidateCache)
3524 PR_Write(fd, kInvalidationHeader, sizeof(kInvalidationHeader) - 1);
3525
3526 static const char kNL[] = NS_LINEBREAK"\012";
3527 PR_Write(fd, kNL, sizeof(kNL) - 1);
3528
3529 PR_Close(fd);
3530}
3531
3532/**
3533 * Returns true if the startup cache file was successfully removed.
3534 * Returns false if file->Clone fails at any point (OOM) or if unable
3535 * to remove the startup cache file. Note in particular the return value
3536 * is unaffected by a failure to remove extensions.ini
3537 */
3538static bool RemoveComponentRegistries(nsIFile* aProfileDir,
3539 nsIFile* aLocalProfileDir,
3540 bool aRemoveEMFiles) {
3541 nsCOMPtr<nsIFile> file;
3542 aProfileDir->Clone(getter_AddRefs(file));
3543 if (!file) return false;
3544
3545 if (aRemoveEMFiles) {
3546 file->SetNativeLeafName("extensions.ini"_ns);
3547 file->Remove(false);
3548 }
3549
3550 aLocalProfileDir->Clone(getter_AddRefs(file));
3551 if (!file) return false;
3552
3553 file->AppendNative("startupCache"_ns);
3554 nsresult rv = file->Remove(true);
3555 return NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) || rv == NS_ERROR_FILE_NOT_FOUND;
3556}
3557
3558// When we first initialize the crash reporter we don't have a profile,
3559// so we set the minidump path to $TEMP. Once we have a profile,
3560// we set it to $PROFILE/minidumps, creating the directory
3561// if needed.
3562static void MakeOrSetMinidumpPath(nsIFile* profD) {
3563 nsCOMPtr<nsIFile> dumpD;
3564 profD->Clone(getter_AddRefs(dumpD));
3565
3566 if (dumpD) {
3567 bool fileExists;
3568 // XXX: do some more error checking here
3569 dumpD->Append(u"minidumps"_ns);
3570 dumpD->Exists(&fileExists);
3571 if (!fileExists) {
3572 nsresult rv = dumpD->Create(nsIFile::DIRECTORY_TYPE, 0700);
3573 NS_ENSURE_SUCCESS_VOID(rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS_VOID(%s) failed with "
"result 0x%" "X" "%s%s%s", "rv", static_cast<uint32_t>
(__rv), name ? " (" : "", name ? name : "", name ? ")" : "");
NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3573); return; } } while (false)
;
3574 }
3575
3576 nsAutoString pathStr;
3577 if (NS_SUCCEEDED(dumpD->GetPath(pathStr))((bool)(__builtin_expect(!!(!NS_FAILED_impl(dumpD->GetPath
(pathStr))), 1)))
)
3578 CrashReporter::SetMinidumpPath(pathStr);
3579 }
3580}
3581
3582const XREAppData* gAppData = nullptr;
3583
3584/**
3585 * NSPR will search for the "nspr_use_zone_allocator" symbol throughout
3586 * the process and use it to determine whether the application defines its own
3587 * memory allocator or not.
3588 *
3589 * Since most applications (e.g. Firefox and Thunderbird) don't use any special
3590 * allocators and therefore don't define this symbol, NSPR must search the
3591 * entire process, which reduces startup performance.
3592 *
3593 * By defining the symbol here, we can avoid the wasted lookup and hopefully
3594 * improve startup performance.
3595 */
3596NS_VISIBILITY_DEFAULT__attribute__((visibility("default"))) PRBool nspr_use_zone_allocator = PR_FALSE0;
3597
3598#ifdef CAIRO_HAS_DWRITE_FONT
3599
3600# include <dwrite.h>
3601# include "nsWindowsHelpers.h"
3602
3603# ifdef DEBUG_DWRITE_STARTUP
3604
3605# define LOGREGISTRY(msg) LogRegistryEvent(msg)
3606
3607// for use when monitoring process
3608static void LogRegistryEvent(const wchar_t* msg) {
3609 HKEY dummyKey;
3610 HRESULT hr;
3611 wchar_t buf[512];
3612
3613 wsprintf(buf, L" log %s", msg);
3614 hr = RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_READ, &dummyKey);
3615 if (SUCCEEDED(hr)) {
3616 RegCloseKey(dummyKey);
3617 }
3618}
3619# else
3620
3621# define LOGREGISTRY(msg)
3622
3623# endif
3624
3625static DWORD WINAPI InitDwriteBG(LPVOID lpdwThreadParam) {
3626 SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
3627 LOGREGISTRY(L"loading dwrite.dll");
3628 HMODULE dwdll = LoadLibrarySystem32(L"dwrite.dll");
3629 if (dwdll) {
3630 decltype(DWriteCreateFactory)* createDWriteFactory =
3631 (decltype(DWriteCreateFactory)*)GetProcAddress(dwdll,
3632 "DWriteCreateFactory");
3633 if (createDWriteFactory) {
3634 LOGREGISTRY(L"creating dwrite factory");
3635 IDWriteFactory* factory;
3636 HRESULT hr = createDWriteFactory(DWRITE_FACTORY_TYPE_SHARED,
3637 __uuidof(IDWriteFactory),
3638 reinterpret_cast<IUnknown**>(&factory));
3639 if (SUCCEEDED(hr)) {
3640 LOGREGISTRY(L"dwrite factory done");
3641 factory->Release();
3642 LOGREGISTRY(L"freed factory");
3643 } else {
3644 LOGREGISTRY(L"failed to create factory");
3645 }
3646 }
3647 }
3648 SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
3649 return 0;
3650}
3651#endif
3652
3653#include "GeckoProfiler.h"
3654#include "ProfilerControl.h"
3655
3656// Encapsulates startup and shutdown state for XRE_main
3657class XREMain {
3658 public:
3659 XREMain() = default;
3660
3661 ~XREMain() {
3662 mScopedXPCOM = nullptr;
3663 mAppData = nullptr;
3664 }
3665
3666 int XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig);
3667 int XRE_mainInit(bool* aExitFlag);
3668 int XRE_mainStartup(bool* aExitFlag);
3669 nsresult XRE_mainRun();
3670
3671 bool CheckLastStartupWasCrash();
3672
3673 nsCOMPtr<nsINativeAppSupport> mNativeApp;
3674 RefPtr<nsToolkitProfileService> mProfileSvc;
3675 nsCOMPtr<nsIFile> mProfD;
3676 nsCOMPtr<nsIFile> mProfLD;
3677 nsCOMPtr<nsIProfileLock> mProfileLock;
3678#if defined(MOZ_HAS_REMOTE1)
3679 RefPtr<nsRemoteService> mRemoteService;
3680#endif
3681
3682 UniquePtr<ScopedXPCOMStartup> mScopedXPCOM;
3683 UniquePtr<XREAppData> mAppData;
3684
3685 nsXREDirProvider mDirProvider;
3686
3687#ifdef MOZ_WIDGET_GTK1
3688 nsAutoCString mXDGActivationToken;
3689 nsAutoCString mDesktopStartupID;
3690#endif
3691
3692 bool mStartOffline = false;
3693#if defined(MOZ_HAS_REMOTE1)
3694 bool mDisableRemoteClient = false;
3695 bool mDisableRemoteServer = false;
3696#endif
3697};
3698
3699#if defined(XP_UNIX1) && !defined(ANDROID)
3700static SmprintfPointer FormatUid(uid_t aId) {
3701 if (const auto pw = getpwuid(aId)) {
3702 return mozilla::Smprintf("%s", pw->pw_name);
3703 }
3704 return mozilla::Smprintf("uid %d", static_cast<int>(aId));
3705}
3706
3707// Bug 1323302: refuse to run under sudo or similar.
3708static bool CheckForUserMismatch() {
3709 static char const* const kVars[] = {
3710 "HOME",
3711# ifdef MOZ_WIDGET_GTK1
3712 "XDG_RUNTIME_DIR",
3713# endif
3714# ifdef MOZ_X111
3715 "XAUTHORITY",
3716# endif
3717 };
3718
3719 const uid_t euid = geteuid();
3720 if (euid != 0) {
3721 // On Linux it's possible to have superuser capabilities with a
3722 // nonzero uid, but anyone who knows enough to make that happen
3723 // probably knows enough to debug the resulting problems.
3724 // Otherwise, a non-root user can't cause the problems we're
3725 // concerned about.
3726 return false;
3727 }
3728
3729 for (const auto var : kVars) {
3730 if (const auto path = PR_GetEnv(var)) {
3731 struct stat st;
3732 if (stat(path, &st) == 0) {
3733 if (st.st_uid != euid) {
3734 const auto owner = FormatUid(st.st_uid);
3735 Output(true,
3736 "Running " MOZ_APP_DISPLAYNAME"Nightly"
3737 " as root in a regular"
3738 " user's session is not supported. ($%s is %s which is"
3739 " owned by %s.)\n",
3740 var, path, owner.get());
3741 return true;
3742 }
3743 }
3744 }
3745 }
3746 return false;
3747}
3748#else // !XP_UNIX || ANDROID
3749static bool CheckForUserMismatch() { return false; }
3750#endif
3751
3752void mozilla::startup::IncreaseDescriptorLimits() {
3753#ifdef XP_UNIX1
3754 // Increase the fd limit to accomodate IPC resources like shared memory.
3755# ifdef XP_DARWIN
3756 // We use Mach IPC for shared memory, so a lower limit suffices.
3757 // See also the Darwin case in config/external/nspr/pr/moz.build
3758 static constexpr rlim_t kFDs = 4096;
3759# else // Unix but not Darwin
3760 // This can be increased if needed, but also be aware that Linux
3761 // distributions may impose hard limits less than this.
3762 static constexpr rlim_t kFDs = 65536;
3763# endif // Darwin or not
3764 struct rlimit rlim;
3765
3766 if (getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &rlim) != 0) {
3767 Output(false, "getrlimit: %s\n", strerror(errno(*__errno_location ())));
3768 return;
3769 }
3770 // Don't decrease the limit if it's already high enough, but don't
3771 // try to go over the hard limit. (RLIM_INFINITY isn't required to
3772 // be the numerically largest rlim_t, so don't assume that.)
3773 if (rlim.rlim_cur != RLIM_INFINITY((__rlim_t) -1) && rlim.rlim_cur < kFDs &&
3774 rlim.rlim_cur < rlim.rlim_max) {
3775 if (rlim.rlim_max != RLIM_INFINITY((__rlim_t) -1) && rlim.rlim_max < kFDs) {
3776 rlim.rlim_cur = rlim.rlim_max;
3777 } else {
3778 rlim.rlim_cur = kFDs;
3779 }
3780 if (setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &rlim) != 0) {
3781 Output(false, "setrlimit: %s\n", strerror(errno(*__errno_location ())));
3782 }
3783 }
3784#endif
3785}
3786
3787#ifdef XP_WIN
3788
3789static uint32_t GetMicrocodeVersionByVendor(HKEY key, DWORD upper,
3790 DWORD lower) {
3791 WCHAR data[13]; // The CPUID vendor string is 12 characters long plus null
3792 DWORD len = sizeof(data);
3793 DWORD vtype;
3794
3795 if (RegQueryValueExW(key, L"VendorIdentifier", nullptr, &vtype,
3796 reinterpret_cast<LPBYTE>(data), &len) == ERROR_SUCCESS) {
3797 if (wcscmp(L"GenuineIntel", data) == 0) {
3798 // Intel reports the microcode version in the upper 32 bits of the MSR
3799 return upper;
3800 }
3801
3802 if (wcscmp(L"AuthenticAMD", data) == 0) {
3803 // AMD reports the microcode version in the lower 32 bits of the MSR
3804 return lower;
3805 }
3806
3807 // Unknown CPU vendor, return whatever half is non-zero
3808 return lower ? lower : upper;
3809 }
3810
3811 return 0; // No clue
3812}
3813
3814#endif // XP_WIN
3815
3816static void MaybeAddCPUMicrocodeCrashAnnotation() {
3817#ifdef XP_WIN
3818 // Add CPU microcode version to the crash report as "CPUMicrocodeVersion".
3819 // It feels like this code may belong in nsSystemInfo instead.
3820 uint32_t cpuUpdateRevision = 0;
3821 HKEY key;
3822 static const WCHAR keyName[] =
3823 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0";
3824
3825 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_QUERY_VALUE, &key) ==
3826 ERROR_SUCCESS) {
3827 DWORD updateRevision[2];
3828 DWORD len = sizeof(updateRevision);
3829 DWORD vtype;
3830
3831 // Windows 7 uses "Update Signature", 8 uses "Update Revision".
3832 // For AMD CPUs, "CurrentPatchLevel" is sometimes used.
3833 // Take the first one we find.
3834 LPCWSTR choices[] = {L"Update Signature", L"Update Revision",
3835 L"CurrentPatchLevel"};
3836 for (const auto& oneChoice : choices) {
3837 if (RegQueryValueExW(key, oneChoice, nullptr, &vtype,
3838 reinterpret_cast<LPBYTE>(updateRevision),
3839 &len) == ERROR_SUCCESS) {
3840 if (vtype == REG_BINARY && len == sizeof(updateRevision)) {
3841 cpuUpdateRevision = GetMicrocodeVersionByVendor(
3842 key, updateRevision[1], updateRevision[0]);
3843 break;
3844 }
3845
3846 if (vtype == REG_DWORD && len == sizeof(updateRevision[0])) {
3847 cpuUpdateRevision = static_cast<int>(updateRevision[0]);
3848 break;
3849 }
3850 }
3851 }
3852 }
3853
3854 if (cpuUpdateRevision > 0) {
3855 CrashReporter::RecordAnnotationNSCString(
3856 CrashReporter::Annotation::CPUMicrocodeVersion,
3857 nsPrintfCString("0x%" PRIx32"x", cpuUpdateRevision));
3858 }
3859#endif
3860}
3861
3862#if defined(MOZ_BACKGROUNDTASKS1)
3863static void SetupConsoleForBackgroundTask(
3864 const nsCString& aBackgroundTaskName) {
3865 // We do not suppress output on Windows because:
3866 // 1. Background task subprocesses launched via LaunchApp() does not attach to
3867 // the console.
3868 // 2. Suppressing output intermittently causes failures on when running
3869 // multiple tasks (see bug 1831631)
3870# ifndef XP_WIN
3871 if (BackgroundTasks::IsNoOutputTaskName(aBackgroundTaskName) &&
3872 !CheckArg("attach-console") &&
3873 !EnvHasValue("MOZ_BACKGROUNDTASKS_IGNORE_NO_OUTPUT")) {
3874 // Suppress output, somewhat crudely. We need to suppress stderr as well
3875 // as stdout because assertions, of which there are many, write to stderr.
3876 Unused << freopen("/dev/null", "w", stdoutstdout);
3877 Unused << freopen("/dev/null", "w", stderrstderr);
3878 return;
3879 }
3880# endif
3881 printf_stderr("*** You are running in background task mode. ***\n");
3882}
3883#endif
3884
3885/*
3886 * XRE_mainInit - Initial setup and command line parameter processing.
3887 * Main() will exit early if either return value != 0 or if aExitFlag is
3888 * true.
3889 */
3890int XREMain::XRE_mainInit(bool* aExitFlag) {
3891 if (!aExitFlag) return 1;
3892 *aExitFlag = false;
3893
3894 atexit(UnexpectedExit);
3895 auto expectedShutdown = mozilla::MakeScopeExit([&] { MozExpectedExit(); });
3896
3897 StartupTimeline::Record(StartupTimeline::MAIN);
3898
3899 if (CheckForUserMismatch()) {
3900 return 1;
3901 }
3902
3903#ifdef XP_MACOSX
3904 mozilla::MacAutoreleasePool pool;
3905
3906 DisableAppNap();
3907#endif
3908
3909#ifdef MOZ_BACKGROUNDTASKS1
3910 Maybe<nsCString> backgroundTask = Nothing();
3911 const char* backgroundTaskName = nullptr;
3912 if (ARG_FOUND ==
3913 CheckArg("backgroundtask", &backgroundTaskName, CheckArgFlag::None)) {
3914 backgroundTask = Some(backgroundTaskName);
3915
3916 SetupConsoleForBackgroundTask(backgroundTask.ref());
3917 }
3918
3919 BackgroundTasks::Init(backgroundTask);
3920#endif
3921
3922#ifndef ANDROID
3923 if (PR_GetEnv("MOZ_RUN_GTEST")
3924# ifdef FUZZING
3925 || PR_GetEnv("FUZZER")
3926# endif
3927# ifdef MOZ_BACKGROUNDTASKS1
3928 || BackgroundTasks::IsBackgroundTaskMode()
3929# endif
3930 ) {
3931 // Enable headless mode and assert that it worked, since gfxPlatform
3932 // uses a static bool set after the first call to `IsHeadless`.
3933 // Note: Android gtests seem to require an Activity and fail to start
3934 // with headless mode enabled.
3935 PR_SetEnv("MOZ_HEADLESS=1");
3936 MOZ_ASSERT(gfxPlatform::IsHeadless())do { static_assert( mozilla::detail::AssertionConditionType<
decltype(gfxPlatform::IsHeadless())>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(gfxPlatform::IsHeadless())))
, 0))) { do { } while (false); MOZ_ReportAssertionFailure("gfxPlatform::IsHeadless()"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3936); AnnotateMozCrashReason("MOZ_ASSERT" "(" "gfxPlatform::IsHeadless()"
")"); do { *((volatile int*)__null) = 3936; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
3937 }
3938#endif // ANDROID
3939
3940 if (PR_GetEnv("MOZ_CHAOSMODE")) {
3941 ChaosFeature feature = ChaosFeature::Any;
3942 long featureInt = strtol(PR_GetEnv("MOZ_CHAOSMODE"), nullptr, 16);
3943 if (featureInt) {
3944 // NOTE: MOZ_CHAOSMODE=0 or a non-hex value maps to Any feature.
3945 feature = static_cast<ChaosFeature>(featureInt);
3946 }
3947 ChaosMode::SetChaosFeature(feature);
3948 }
3949
3950 if (CheckArgExists("fxr")) {
3951 gFxREmbedded = true;
3952 }
3953
3954 if (ChaosMode::isActive(ChaosFeature::Any)) {
3955 printf_stderr(
3956 "*** You are running in chaos test mode. See ChaosMode.h. ***\n");
3957 }
3958
3959 if (CheckArg("headless") || CheckArgExists("screenshot")) {
3960 PR_SetEnv("MOZ_HEADLESS=1");
3961 }
3962
3963 if (gfxPlatform::IsHeadless()) {
3964#if defined(XP_WIN) || defined(MOZ_WIDGET_GTK1) || defined(XP_MACOSX)
3965 printf_stderr("*** You are running in headless mode.\n");
3966#else
3967 Output(
3968 true,
3969 "Error: headless mode is not currently supported on this platform.\n");
3970 return 1;
3971#endif
3972
3973#ifdef XP_MACOSX
3974 // To avoid taking focus when running in headless mode immediately
3975 // transition Firefox to a background application.
3976 ProcessSerialNumber psn = {0, kCurrentProcess};
3977 OSStatus transformStatus =
3978 TransformProcessType(&psn, kProcessTransformToBackgroundApplication);
3979 if (transformStatus != noErr) {
3980 NS_ERROR("Failed to make process a background application.")do { NS_DebugBreak(NS_DEBUG_ASSERTION, "Failed to make process a background application."
, "Error", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 3980); MOZ_PretendNoReturn(); } while (0)
;
3981 return 1;
3982 }
3983#endif
3984 }
3985
3986 gKioskMode = CheckArg("kiosk", nullptr, CheckArgFlag::None);
3987 const char* kioskMonitorNumber = nullptr;
3988 if (CheckArg("kiosk-monitor", &kioskMonitorNumber, CheckArgFlag::None)) {
3989 gKioskMode = true;
3990 gKioskMonitor = atoi(kioskMonitorNumber);
3991 }
3992
3993 if (XRE_IsParentProcess()) {
3994 gAllowContentAnalysisArgPresent =
3995 CheckArg("allow-content-analysis", nullptr, CheckArgFlag::None) ==
3996 ARG_FOUND;
3997 }
3998
3999 nsresult rv;
4000 ArgResult ar;
4001
4002#ifdef DEBUG1
4003 if (PR_GetEnv("XRE_MAIN_BREAK")) NS_BREAK()do { NS_DebugBreak(NS_DEBUG_BREAK, nullptr, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4003); MOZ_PretendNoReturn(); } while (0)
;
4004#endif
4005
4006 mozilla::startup::IncreaseDescriptorLimits();
4007
4008 SetupErrorHandling(gArgv[0]);
4009
4010#ifdef CAIRO_HAS_DWRITE_FONT
4011 {
4012 // Bug 602792 - when DWriteCreateFactory is called the dwrite client dll
4013 // starts the FntCache service if it isn't already running (it's set
4014 // to manual startup by default in Windows 7 RTM). Subsequent DirectWrite
4015 // calls cause the IDWriteFactory object to communicate with the FntCache
4016 // service with a timeout; if there's no response after the timeout, the
4017 // DirectWrite client library will assume the service isn't around and do
4018 // manual font file I/O on _all_ system fonts. To avoid this, load the
4019 // dwrite library and create a factory as early as possible so that the
4020 // FntCache service is ready by the time it's needed.
4021
4022 CreateThread(nullptr, 0, &InitDwriteBG, nullptr, 0, nullptr);
4023 }
4024#endif
4025
4026#ifdef XP_UNIX1
4027 const char* home = PR_GetEnv("HOME");
4028 if (!home || !*home) {
4029 struct passwd* pw = getpwuid(geteuid());
4030 if (!pw || !pw->pw_dir) {
4031 Output(true, "Could not determine HOME directory");
4032 return 1;
4033 }
4034 SaveWordToEnv("HOME", nsDependentCString(pw->pw_dir));
4035 }
4036#endif
4037
4038#ifdef MOZ_ACCESSIBILITY_ATK1
4039 // Suppress atk-bridge init at startup, until mozilla accessibility is
4040 // initialized. This works after gnome 2.24.2.
4041 SaveToEnv("NO_AT_BRIDGE=1");
4042#endif
4043
4044 // Check for application.ini overrides
4045 const char* override = nullptr;
4046 ar = CheckArg("override", &override);
4047 if (ar == ARG_BAD) {
4048 Output(true, "Incorrect number of arguments passed to --override");
4049 return 1;
4050 }
4051 if (ar == ARG_FOUND) {
4052 nsCOMPtr<nsIFile> overrideLF;
4053 rv = XRE_GetFileFromPath(override, getter_AddRefs(overrideLF));
4054 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
4055 Output(true, "Error: unrecognized override.ini path.\n");
4056 return 1;
4057 }
4058
4059 rv = XRE_ParseAppData(overrideLF, *mAppData);
4060 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
4061 Output(true, "Couldn't read override.ini");
4062 return 1;
4063 }
4064 }
4065
4066 // Check sanity and correctness of app data.
4067
4068 if (!mAppData->name) {
4069 Output(true, "Error: App:Name not specified in application.ini\n");
4070 return 1;
4071 }
4072 if (!mAppData->buildID) {
4073 Output(true, "Error: App:BuildID not specified in application.ini\n");
4074 return 1;
4075 }
4076
4077 // XXX Originally ScopedLogging was here? Now it's in XRE_main above
4078 // XRE_mainInit.
4079
4080 if (!mAppData->minVersion) {
4081 Output(true, "Error: Gecko:MinVersion not specified in application.ini\n");
4082 return 1;
4083 }
4084
4085 if (!mAppData->maxVersion) {
4086 // If no maxVersion is specified, we assume the app is only compatible
4087 // with the initial preview release. Do not increment this number ever!
4088 mAppData->maxVersion = "1.*";
4089 }
4090
4091 if (mozilla::Version(mAppData->minVersion) > gToolkitVersion ||
4092 mozilla::Version(mAppData->maxVersion) < gToolkitVersion) {
4093 Output(true,
4094 "Error: Platform version '%s' is not compatible with\n"
4095 "minVersion >= %s\nmaxVersion <= %s\n",
4096 (const char*)gToolkitVersion, (const char*)mAppData->minVersion,
4097 (const char*)mAppData->maxVersion);
4098 return 1;
4099 }
4100
4101 rv = mDirProvider.Initialize(mAppData->directory, mAppData->xreDirectory);
4102 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return 1;
4103
4104 if (EnvHasValue("MOZ_CRASHREPORTER")) {
4105 mAppData->flags |= NS_XRE_ENABLE_CRASH_REPORTER(1 << 3);
4106 }
4107
4108 nsCOMPtr<nsIFile> xreBinDirectory;
4109 xreBinDirectory = mDirProvider.GetGREBinDir();
4110
4111 if ((mAppData->flags & NS_XRE_ENABLE_CRASH_REPORTER(1 << 3)) &&
4112 NS_SUCCEEDED(CrashReporter::SetExceptionHandler(xreBinDirectory))((bool)(__builtin_expect(!!(!NS_FAILED_impl(CrashReporter::SetExceptionHandler
(xreBinDirectory))), 1)))
) {
4113 nsCOMPtr<nsIFile> file;
4114 rv = nsXREDirProvider::GetUserAppDataDirectory(getter_AddRefs(file));
4115 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
4116 CrashReporter::SetUserAppDataDirectory(file);
4117 }
4118 if (mAppData->crashReporterURL) {
4119 CrashReporter::SetServerURL(
4120 nsDependentCString(mAppData->crashReporterURL));
4121 }
4122
4123 // We overwrite this once we finish starting up.
4124 CrashReporter::RecordAnnotationBool(CrashReporter::Annotation::StartupCrash,
4125 true);
4126
4127 // pass some basic info from the app data
4128 if (mAppData->vendor) {
4129 CrashReporter::RecordAnnotationCString(CrashReporter::Annotation::Vendor,
4130 mAppData->vendor);
4131 }
4132 if (mAppData->name) {
4133 CrashReporter::RecordAnnotationCString(
4134 CrashReporter::Annotation::ProductName, mAppData->name);
4135 }
4136 if (mAppData->ID) {
4137 CrashReporter::RecordAnnotationCString(
4138 CrashReporter::Annotation::ProductID, mAppData->ID);
4139 }
4140 if (mAppData->version) {
4141 CrashReporter::RecordAnnotationCString(CrashReporter::Annotation::Version,
4142 mAppData->version);
4143 }
4144 if (mAppData->buildID) {
4145 CrashReporter::RecordAnnotationCString(CrashReporter::Annotation::BuildID,
4146 mAppData->buildID);
4147 }
4148
4149 nsDependentCString releaseChannel(MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL)"default");
4150 CrashReporter::RecordAnnotationNSCString(
4151 CrashReporter::Annotation::ReleaseChannel, releaseChannel);
4152
4153#ifdef XP_WIN
4154 nsAutoString appInitDLLs;
4155 if (widget::WinUtils::GetAppInitDLLs(appInitDLLs)) {
4156 CrashReporter::RecordAnnotationNSString(
4157 CrashReporter::Annotation::AppInitDLLs, appInitDLLs);
4158 }
4159
4160 nsString packageFamilyName = widget::WinUtils::GetPackageFamilyName();
4161 if (StringBeginsWith(packageFamilyName, u"Mozilla."_ns) ||
4162 StringBeginsWith(packageFamilyName, u"MozillaCorporation."_ns)) {
4163 CrashReporter::RecordAnnotationNSCString(
4164 CrashReporter::Annotation::WindowsPackageFamilyName,
4165 NS_ConvertUTF16toUTF8(packageFamilyName));
4166 }
4167#endif
4168
4169 bool isBackgroundTaskMode = false;
4170#ifdef MOZ_BACKGROUNDTASKS1
4171 Maybe<nsCString> backgroundTasks = BackgroundTasks::GetBackgroundTasks();
4172 if (backgroundTasks.isSome()) {
4173 isBackgroundTaskMode = true;
4174 CrashReporter::RecordAnnotationNSCString(
4175 CrashReporter::Annotation::BackgroundTaskName, backgroundTasks.ref());
4176 }
4177#endif
4178 CrashReporter::RecordAnnotationBool(
4179 CrashReporter::Annotation::BackgroundTaskMode, isBackgroundTaskMode);
4180
4181 CrashReporter::RecordAnnotationBool(CrashReporter::Annotation::HeadlessMode,
4182 gfxPlatform::IsHeadless());
4183
4184 CrashReporter::SetRestartArgs(gArgc, gArgv);
4185
4186 // annotate other data (user id etc)
4187 nsCOMPtr<nsIFile> userAppDataDir;
4188 if (NS_SUCCEEDED(mDirProvider.GetUserAppDataDirectory(((bool)(__builtin_expect(!!(!NS_FAILED_impl(mDirProvider.GetUserAppDataDirectory
( getter_AddRefs(userAppDataDir)))), 1)))
4189 getter_AddRefs(userAppDataDir)))((bool)(__builtin_expect(!!(!NS_FAILED_impl(mDirProvider.GetUserAppDataDirectory
( getter_AddRefs(userAppDataDir)))), 1)))
) {
4190 CrashReporter::SetupExtraData(userAppDataDir,
4191 nsDependentCString(mAppData->buildID));
4192
4193 // see if we have a crashreporter-override.ini in the application
4194 // directory
4195 nsCOMPtr<nsIFile> overrideini;
4196 if (NS_SUCCEEDED(((bool)(__builtin_expect(!!(!NS_FAILED_impl(mDirProvider.GetAppDir
()->Clone(getter_AddRefs(overrideini)))), 1)))
4197 mDirProvider.GetAppDir()->Clone(getter_AddRefs(overrideini)))((bool)(__builtin_expect(!!(!NS_FAILED_impl(mDirProvider.GetAppDir
()->Clone(getter_AddRefs(overrideini)))), 1)))
&&
4198 NS_SUCCEEDED(((bool)(__builtin_expect(!!(!NS_FAILED_impl(overrideini->AppendNative
("crashreporter-override.ini"_ns))), 1)))
4199 overrideini->AppendNative("crashreporter-override.ini"_ns))((bool)(__builtin_expect(!!(!NS_FAILED_impl(overrideini->AppendNative
("crashreporter-override.ini"_ns))), 1)))
) {
4200#ifdef XP_WIN
4201 nsAutoString overridePathW;
4202 overrideini->GetPath(overridePathW);
4203 NS_ConvertUTF16toUTF8 overridePath(overridePathW);
4204#else
4205 nsAutoCString overridePath;
4206 overrideini->GetNativePath(overridePath);
4207#endif
4208
4209 SaveWordToEnv("MOZ_CRASHREPORTER_STRINGS_OVERRIDE", overridePath);
4210 }
4211 }
4212 } else {
4213 // We might have registered a runtime exception module very early in process
4214 // startup to catch early crashes. This is before we have access to ini file
4215 // data, so unregister here if it turns out the crash reporter is disabled.
4216 CrashReporter::UnregisterRuntimeExceptionModule();
4217 }
4218
4219#if defined(MOZ_SANDBOX1) && defined(XP_WIN)
4220 if (mAppData->sandboxBrokerServices) {
4221 nsAutoString binDirPath;
4222 MOZ_ALWAYS_SUCCEEDS(xreBinDirectory->GetPath(binDirPath))do { if ((__builtin_expect(!!(((bool)(__builtin_expect(!!(!NS_FAILED_impl
(xreBinDirectory->GetPath(binDirPath))), 1)))), 1))) { } else
{ do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "NS_SUCCEEDED(xreBinDirectory->GetPath(binDirPath))"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4222); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "NS_SUCCEEDED(xreBinDirectory->GetPath(binDirPath))"
")"); do { *((volatile int*)__null) = 4222; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false); } } while (
false)
;
4223 SandboxBroker::Initialize(mAppData->sandboxBrokerServices, binDirPath);
4224 } else {
4225# if defined(MOZ_SANDBOX1)
4226 // If we're sandboxing content and we fail to initialize, then crashing here
4227 // seems like the sensible option.
4228 if (BrowserTabsRemoteAutostart()) {
4229 MOZ_CRASH("Failed to initialize broker services, can't continue.")do { do { } while (false); MOZ_ReportCrash("" "Failed to initialize broker services, can't continue."
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4229); AnnotateMozCrashReason("MOZ_CRASH(" "Failed to initialize broker services, can't continue."
")"); do { *((volatile int*)__null) = 4229; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
4230 }
4231# endif
4232 // Otherwise just warn for the moment, as most things will work.
4233 NS_WARNING(NS_DebugBreak(NS_DEBUG_WARNING, "Failed to initialize broker services, sandboxed processes will "
"fail to start.", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4235)
4234 "Failed to initialize broker services, sandboxed processes will "NS_DebugBreak(NS_DEBUG_WARNING, "Failed to initialize broker services, sandboxed processes will "
"fail to start.", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4235)
4235 "fail to start.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to initialize broker services, sandboxed processes will "
"fail to start.", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4235)
;
4236 }
4237#endif
4238
4239#ifdef XP_MACOSX
4240 // Set up ability to respond to system (Apple) events. This must occur before
4241 // ProcessUpdates to ensure that links clicked in external applications aren't
4242 // lost when updates are pending.
4243 SetupMacApplicationDelegate(&gRestartedByOS);
4244
4245 if (EnvHasValue("MOZ_LAUNCHED_CHILD")) {
4246 // This is needed, on relaunch, to force the OS to use the "Cocoa Dock
4247 // API". Otherwise the call to ReceiveNextEvent() below will make it
4248 // use the "Carbon Dock API". For more info see bmo bug 377166.
4249 EnsureUseCocoaDockAPI();
4250
4251 // When the app relaunches, the original process exits. This causes
4252 // the dock tile to stop bouncing, lose the "running" triangle, and
4253 // if the tile does not permanently reside in the Dock, even disappear.
4254 // This can be confusing to the user, who is expecting the app to launch.
4255 // Calling ReceiveNextEvent without requesting any event is enough to
4256 // cause a dock tile for the child process to appear.
4257 const EventTypeSpec kFakeEventList[] = {{INT_MAX2147483647, INT_MAX2147483647}};
4258 EventRef event;
4259 ::ReceiveNextEvent(GetEventTypeCount(kFakeEventList), kFakeEventList,
4260 kEventDurationNoWait, false, &event);
4261 }
4262
4263 if (CheckArg("foreground")) {
4264 // The original process communicates that it was in the foreground by
4265 // adding this argument. This new process, which is taking over for
4266 // the old one, should make itself the active application.
4267 ProcessSerialNumber psn;
4268 if (::GetCurrentProcess(&psn) == noErr) ::SetFrontProcess(&psn);
4269 }
4270#endif
4271
4272 SaveToEnv("MOZ_LAUNCHED_CHILD=");
4273
4274 // On Windows, the -os-restarted command line switch lets us know when we are
4275 // restarted via RegisterApplicationRestart. May be used for other OSes later.
4276 if (CheckArg("os-restarted", nullptr, CheckArgFlag::RemoveArg) == ARG_FOUND) {
4277 gRestartedByOS = true;
4278 }
4279
4280 gRestartArgc = gArgc;
4281 gRestartArgv =
4282 (char**)malloc(sizeof(char*) * (gArgc + 1 + (override ? 2 : 0)));
4283 if (!gRestartArgv) {
4284 return 1;
4285 }
4286
4287 int i;
4288 for (i = 0; i < gArgc; ++i) {
4289 gRestartArgv[i] = gArgv[i];
4290 }
4291
4292 // Add the -override argument back (it is removed automatically be CheckArg)
4293 // if there is one
4294 if (override) {
4295 gRestartArgv[gRestartArgc++] = const_cast<char*>("-override");
4296 gRestartArgv[gRestartArgc++] = const_cast<char*>(override);
4297 }
4298
4299 gRestartArgv[gRestartArgc] = nullptr;
4300
4301 Maybe<bool> safeModeRequested = IsSafeModeRequested(gArgc, gArgv);
4302 if (!safeModeRequested) {
4303 return 1;
4304 }
4305#ifdef MOZ_BACKGROUNDTASKS1
4306 if (BackgroundTasks::IsBackgroundTaskMode()) {
4307 safeModeRequested = Some(false);
4308
4309 // Remove the --backgroundtask arg now that it has been saved in
4310 // gRestartArgv.
4311 const char* tmpBackgroundTaskName = nullptr;
4312 Unused << CheckArg("backgroundtask", &tmpBackgroundTaskName,
4313 CheckArgFlag::RemoveArg);
4314 }
4315#endif
4316
4317 gSafeMode = safeModeRequested.value();
4318
4319 MaybeAddCPUMicrocodeCrashAnnotation();
4320 CrashReporter::RegisterAnnotationBool(CrashReporter::Annotation::SafeMode,
4321 &gSafeMode);
4322
4323#if defined(MOZ_HAS_REMOTE1)
4324 // Handle --no-remote and --new-instance command line arguments. Setup
4325 // the environment to better accommodate other components and various
4326 // restart scenarios.
4327 ar = CheckArg("no-remote");
4328 if (ar == ARG_FOUND || EnvHasValue("MOZ_NO_REMOTE")) {
4329 mDisableRemoteClient = true;
4330 mDisableRemoteServer = true;
4331 gRestartWithoutRemote = true;
4332 // We don't want to propagate MOZ_NO_REMOTE to potential child
4333 // process.
4334 SaveToEnv("MOZ_NO_REMOTE=");
4335 }
4336
4337 ar = CheckArg("new-instance");
4338 if (ar == ARG_FOUND || EnvHasValue("MOZ_NEW_INSTANCE")) {
4339 mDisableRemoteClient = true;
4340 }
4341#else
4342 // These arguments do nothing in platforms with no remoting support but we
4343 // should remove them from the command line anyway.
4344 CheckArg("no-remote");
4345 CheckArg("new-instance");
4346#endif
4347
4348 ar = CheckArg("offline");
4349 if (ar || EnvHasValue("XRE_START_OFFLINE")) {
4350 mStartOffline = true;
4351 }
4352
4353 // On Windows, to get working console arrangements so help/version/etc
4354 // print something, we need to initialize the native app support.
4355 rv = NS_CreateNativeAppSupport(getter_AddRefs(mNativeApp));
4356 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return 1;
4357
4358 // Handle --help, --full-version and --version command line arguments.
4359 // They should return quickly, so we deal with them here.
4360 if (CheckArg("h") || CheckArg("help") || CheckArg("?")) {
4361 DumpHelp();
4362 *aExitFlag = true;
4363 return 0;
4364 }
4365
4366 if (CheckArg("v") || CheckArg("version")) {
4367 DumpVersion();
4368 *aExitFlag = true;
4369 return 0;
4370 }
4371
4372 if (CheckArg("full-version")) {
4373 DumpFullVersion();
4374 *aExitFlag = true;
4375 return 0;
4376 }
4377
4378#ifdef MOZ_ENABLE_DBUS1
4379 const char* dbusServiceLauncher = nullptr;
4380 ar = CheckArg("dbus-service", &dbusServiceLauncher, CheckArgFlag::None);
4381 if (ar == ARG_BAD) {
4382 Output(true, "Missing launcher param for --dbus-service\n");
4383 return 1;
4384 }
4385 if (ar == ARG_FOUND) {
4386 UniquePtr<DBusService> dbusService =
4387 MakeUnique<DBusService>(dbusServiceLauncher);
4388 if (dbusService->Init()) {
4389 dbusService->Run();
4390 }
4391 *aExitFlag = true;
4392 return 0;
4393 }
4394#endif
4395
4396 rv = XRE_InitCommandLine(gArgc, gArgv);
4397 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4397); return 1; } } while (false)
;
4398
4399 return 0;
4400}
4401
4402#if defined(XP_LINUX1) && !defined(ANDROID)
4403
4404static void AnnotateLSBRelease(void*) {
4405 nsCString dist, desc, release, codename;
4406 if (widget::lsb::GetLSBRelease(dist, desc, release, codename)) {
4407 CrashReporter::AppendAppNotesToCrashReport(desc);
4408 }
4409}
4410
4411#endif // defined(XP_LINUX) && !defined(ANDROID)
4412
4413#ifdef XP_WIN
4414static void ReadAheadSystemDll(const wchar_t* dllName) {
4415 wchar_t dllPath[MAX_PATH];
4416 if (ConstructSystem32Path(dllName, dllPath, MAX_PATH)) {
4417 ReadAheadLib(dllPath);
4418 }
4419}
4420
4421static void ReadAheadPackagedDll(const wchar_t* dllName,
4422 const wchar_t* aGREDir) {
4423 wchar_t dllPath[MAX_PATH];
4424 swprintf(dllPath, MAX_PATH, L"%s\\%s", aGREDir, dllName);
4425 ReadAheadLib(dllPath);
4426}
4427
4428static void PR_CALLBACK ReadAheadDlls_ThreadStart(void* arg) {
4429 UniquePtr<wchar_t[]> greDir(static_cast<wchar_t*>(arg));
4430
4431 // In Bug 1628903, we investigated which DLLs we should prefetch in
4432 // order to reduce disk I/O and improve startup on Windows machines.
4433 // Our ultimate goal is to measure the impact of these improvements on
4434 // retention (see Bug 1640087). Before we place this within a pref,
4435 // we should ensure this feature only ships to the nightly channel
4436 // and monitor results from that subset.
4437 if (greDir) {
4438 // Prefetch the DLLs shipped with firefox
4439 ReadAheadPackagedDll(L"libegl.dll", greDir.get());
4440 ReadAheadPackagedDll(L"libGLESv2.dll", greDir.get());
4441 ReadAheadPackagedDll(L"nssckbi.dll", greDir.get());
4442 ReadAheadPackagedDll(L"freebl3.dll", greDir.get());
4443 ReadAheadPackagedDll(L"softokn3.dll", greDir.get());
4444
4445 // Prefetch the system DLLs
4446 ReadAheadSystemDll(L"DWrite.dll");
4447 ReadAheadSystemDll(L"D3DCompiler_47.dll");
4448 } else {
4449 // Load DataExchange.dll and twinapi.appcore.dll for
4450 // nsWindow::EnableDragDrop
4451 ReadAheadSystemDll(L"DataExchange.dll");
4452 ReadAheadSystemDll(L"twinapi.appcore.dll");
4453
4454 // Load twinapi.dll for WindowsUIUtils::UpdateTabletModeState
4455 ReadAheadSystemDll(L"twinapi.dll");
4456
4457 // Load explorerframe.dll for WinTaskbar::Initialize
4458 ReadAheadSystemDll(L"ExplorerFrame.dll");
4459
4460 // Load WinTypes.dll for nsOSHelperAppService::GetApplicationDescription
4461 ReadAheadSystemDll(L"WinTypes.dll");
4462 }
4463}
4464#endif
4465
4466#if defined(MOZ_UPDATER1) && !defined(MOZ_WIDGET_ANDROID)
4467enum struct ShouldNotProcessUpdatesReason {
4468 DevToolsLaunching,
4469 NotAnUpdatingTask,
4470 OtherInstanceRunning,
4471 FirstStartup
4472};
4473
4474const char* ShouldNotProcessUpdatesReasonAsString(
4475 ShouldNotProcessUpdatesReason aReason) {
4476 switch (aReason) {
4477 case ShouldNotProcessUpdatesReason::DevToolsLaunching:
4478 return "DevToolsLaunching";
4479 case ShouldNotProcessUpdatesReason::NotAnUpdatingTask:
4480 return "NotAnUpdatingTask";
4481 case ShouldNotProcessUpdatesReason::OtherInstanceRunning:
4482 return "OtherInstanceRunning";
4483 default:
4484 MOZ_CRASH("impossible value for ShouldNotProcessUpdatesReason")do { do { } while (false); MOZ_ReportCrash("" "impossible value for ShouldNotProcessUpdatesReason"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4484); AnnotateMozCrashReason("MOZ_CRASH(" "impossible value for ShouldNotProcessUpdatesReason"
")"); do { *((volatile int*)__null) = 4484; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
4485 }
4486}
4487
4488Maybe<ShouldNotProcessUpdatesReason> ShouldNotProcessUpdates(
4489 nsXREDirProvider& aDirProvider) {
4490 // Don't process updates when launched from the installer.
4491 // It's possible for a stale update to be present in the case of a paveover;
4492 // ignore it and leave the update service to discard it.
4493 if (ARG_FOUND == CheckArgExists("first-startup")) {
4494 NS_WARNING("ShouldNotProcessUpdates(): FirstStartup")NS_DebugBreak(NS_DEBUG_WARNING, "ShouldNotProcessUpdates(): FirstStartup"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4494)
;
4495 return Some(ShouldNotProcessUpdatesReason::FirstStartup);
4496 }
4497
4498 // Do not process updates if we're launching devtools, as evidenced by
4499 // "--chrome ..." with the browser toolbox chrome document URL.
4500
4501 // Keep this synchronized with the value of the same name in
4502 // devtools/client/framework/browser-toolbox/Launcher.sys.mjs. Or, for bonus
4503 // points, lift this value to nsIXulRuntime or similar, so that it can be
4504 // accessed in both locations. (The prefs service isn't available at this
4505 // point so the simplest manner of sharing the value is not available to us.)
4506 const char* BROWSER_TOOLBOX_WINDOW_URL =
4507 "chrome://devtools/content/framework/browser-toolbox/window.html";
4508
4509 const char* chromeParam = nullptr;
4510 if (ARG_FOUND == CheckArg("chrome", &chromeParam, CheckArgFlag::None)) {
4511 if (!chromeParam || !strcmp(BROWSER_TOOLBOX_WINDOW_URL, chromeParam)) {
4512 NS_WARNING("ShouldNotProcessUpdates(): DevToolsLaunching")NS_DebugBreak(NS_DEBUG_WARNING, "ShouldNotProcessUpdates(): DevToolsLaunching"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4512)
;
4513 return Some(ShouldNotProcessUpdatesReason::DevToolsLaunching);
4514 }
4515 }
4516
4517# ifdef MOZ_BACKGROUNDTASKS1
4518 // Do not process updates if we're running a background task mode and another
4519 // instance is already running. This avoids periodic maintenance updating
4520 // underneath a browsing session.
4521 Maybe<nsCString> backgroundTasks = BackgroundTasks::GetBackgroundTasks();
4522 if (backgroundTasks.isSome()) {
4523 // Only process updates for specific tasks: at this time, the
4524 // `backgroundupdate` task and the test-only `shouldprocessupdates` task.
4525 //
4526 // Background tasks can be sparked by Firefox instances that are shutting
4527 // down, which can cause races between the task startup trying to update and
4528 // Firefox trying to invoke the updater. This happened when converting
4529 // `pingsender` to a background task, since it is launched to send pings at
4530 // shutdown: Bug 1736373.
4531 //
4532 // We'd prefer to have this be a property of the task definition sibling to
4533 // `backgroundTaskTimeoutSec`, but when we reach this code we're well before
4534 // we can load the task JSM.
4535 if (!BackgroundTasks::IsUpdatingTaskName(backgroundTasks.ref())) {
4536 NS_WARNING("ShouldNotProcessUpdates(): NotAnUpdatingTask")NS_DebugBreak(NS_DEBUG_WARNING, "ShouldNotProcessUpdates(): NotAnUpdatingTask"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4536)
;
4537 return Some(ShouldNotProcessUpdatesReason::NotAnUpdatingTask);
4538 }
4539
4540 // At this point we have a dir provider but no XPCOM directory service. We
4541 // launch the update sync manager using that information so that it doesn't
4542 // need to ask for (and fail to find) the directory service.
4543 nsCOMPtr<nsIFile> anAppFile;
4544 bool persistent;
4545 nsresult rv = aDirProvider.GetFile(XRE_EXECUTABLE_FILE"XREExeF", &persistent,
4546 getter_AddRefs(anAppFile));
4547 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !anAppFile) {
4548 // Strange, but not a reason to skip processing updates.
4549 return Nothing();
4550 }
4551
4552 auto updateSyncManager = new nsUpdateSyncManager(anAppFile);
4553
4554 bool otherInstance = false;
4555 updateSyncManager->IsOtherInstanceRunning(&otherInstance);
4556 if (otherInstance) {
4557 NS_WARNING("ShouldNotProcessUpdates(): OtherInstanceRunning")NS_DebugBreak(NS_DEBUG_WARNING, "ShouldNotProcessUpdates(): OtherInstanceRunning"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4557)
;
4558 return Some(ShouldNotProcessUpdatesReason::OtherInstanceRunning);
4559 }
4560 }
4561# endif
4562
4563 return Nothing();
4564}
4565#endif
4566
4567namespace mozilla::startup {
4568Result<nsCOMPtr<nsIFile>, nsresult> GetIncompleteStartupFile(nsIFile* aProfLD) {
4569 nsCOMPtr<nsIFile> crashFile;
4570 MOZ_TRY(aProfLD->Clone(getter_AddRefs(crashFile)))do { auto mozTryTempResult_ = ::mozilla::ToResult(aProfLD->
Clone(getter_AddRefs(crashFile))); if ((__builtin_expect(!!(mozTryTempResult_
.isErr()), 0))) { return mozTryTempResult_.propagateErr(); } }
while (0)
;
4571 MOZ_TRY(crashFile->Append(FILE_STARTUP_INCOMPLETE))do { auto mozTryTempResult_ = ::mozilla::ToResult(crashFile->
Append(u".startup-incomplete"_ns)); if ((__builtin_expect(!!(
mozTryTempResult_.isErr()), 0))) { return mozTryTempResult_.propagateErr
(); } } while (0)
;
4572 return std::move(crashFile);
4573}
4574} // namespace mozilla::startup
4575
4576// Check whether the last startup attempt resulted in a crash or hang.
4577// This is distinct from the definition of a startup crash from
4578// nsAppStartup::TrackStartupCrashBegin.
4579bool XREMain::CheckLastStartupWasCrash() {
4580 Result<nsCOMPtr<nsIFile>, nsresult> crashFile =
4581 GetIncompleteStartupFile(mProfLD);
4582 if (crashFile.isErr()) {
4583 return true;
4584 }
4585
4586 // Attempt to create the incomplete startup canary file. If the file already
4587 // exists, this fails, and we know the last startup was a crash. If it
4588 // doesn't already exist, it is created, and will be removed at the end of
4589 // the startup crash detection window.
4590 AutoFDClose fd;
4591 Unused << crashFile.inspect()->OpenNSPRFileDesc(
4592 PR_WRONLY0x02 | PR_CREATE_FILE0x08 | PR_EXCL0x80, 0666, getter_Transfers(fd));
4593 return !fd;
4594}
4595
4596/*
4597 * XRE_mainStartup - Initializes the profile and various other services.
4598 * Main() will exit early if either return value != 0 or if aExitFlag is
4599 * true.
4600 */
4601int XREMain::XRE_mainStartup(bool* aExitFlag) {
4602 nsresult rv;
4603
4604 if (!aExitFlag) return 1;
4605 *aExitFlag = false;
4606
4607#ifdef XP_MACOSX
4608 mozilla::MacAutoreleasePool pool;
4609#endif
4610
4611 // Enable Telemetry IO Reporting on DEBUG, nightly and local builds,
4612 // but disable it on FUZZING builds and for ANDROID.
4613#ifndef FUZZING
4614# ifndef ANDROID
4615# ifdef DEBUG1
4616 mozilla::Telemetry::InitIOReporting(gAppData->xreDirectory);
4617# else
4618 {
4619 const char* releaseChannel = MOZ_STRINGIFY(MOZ_UPDATE_CHANNEL)"default";
4620 if (strcmp(releaseChannel, "nightly") == 0 ||
4621 strcmp(releaseChannel, "default") == 0) {
4622 mozilla::Telemetry::InitIOReporting(gAppData->xreDirectory);
4623 }
4624 }
4625# endif /* DEBUG */
4626# endif /* ANDROID */
4627#endif /* FUZZING */
4628
4629#if defined(XP_WIN)
4630 // Enable the HeapEnableTerminationOnCorruption exploit mitigation. We ignore
4631 // the return code because it always returns success, although it has no
4632 // effect on Windows older than XP SP3.
4633 HeapSetInformation(NULL__null, HeapEnableTerminationOnCorruption, NULL__null, 0);
4634#endif /* XP_WIN */
4635
4636#ifdef MOZ_WIDGET_GTK1
4637 // Stash startup token in owned memory because gtk_init will clear
4638 // DESKTOP_STARTUP_ID it.
4639 if (const char* v = PR_GetEnv("DESKTOP_STARTUP_ID")) {
4640 mDesktopStartupID.Assign(v);
4641 }
4642 if (const char* v = PR_GetEnv("XDG_ACTIVATION_TOKEN")) {
4643 mXDGActivationToken.Assign(v);
4644 }
4645#endif
4646
4647#if defined(XP_WIN)
4648 {
4649 // Save the shortcut path before lpTitle is replaced by an AUMID,
4650 // such as by WinTaskbar
4651 STARTUPINFOW si;
4652 GetStartupInfoW(&si);
4653 if (si.dwFlags & STARTF_TITLEISAPPID) {
4654 NS_WARNING("AUMID was already set, shortcut may have been lost.")NS_DebugBreak(NS_DEBUG_WARNING, "AUMID was already set, shortcut may have been lost."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4654)
;
4655 } else if ((si.dwFlags & STARTF_TITLEISLINKNAME) && si.lpTitle) {
4656 gProcessStartupShortcut.Assign(si.lpTitle);
4657 }
4658 }
4659#endif /* XP_WIN */
4660
4661#if defined(MOZ_WIDGET_GTK1)
4662 // setup for private colormap. Ideally we'd like to do this
4663 // in nsAppShell::Create, but we need to get in before gtk
4664 // has been initialized to make sure everything is running
4665 // consistently.
4666
4667 // Set program name to the one defined in application.ini.
4668 g_set_prgname(gAppData->remotingName);
4669
4670 // Initialize GTK here for splash.
4671
4672# if defined(MOZ_WIDGET_GTK1) && defined(MOZ_X111)
4673 // Disable XInput2 multidevice support due to focus bugginess.
4674 // See bugs 1182700, 1170342.
4675 // gdk_disable_multidevice() affects Gdk X11 backend only,
4676 // the multidevice support is always enabled on Wayland backend.
4677 const char* useXI2 = PR_GetEnv("MOZ_USE_XINPUT2");
4678 if (!useXI2 || (*useXI2 == '0')) gdk_disable_multidevice();
4679# endif
4680
4681 // Open the display ourselves instead of using gtk_init, so that we can
4682 // close it without fear that one day gtk might clean up the display it
4683 // opens.
4684 if (!gtk_parse_args(&gArgc, &gArgv)) return 1;
4685#endif /* MOZ_WIDGET_GTK */
4686
4687#ifdef FUZZING
4688 if (PR_GetEnv("FUZZER")) {
4689 *aExitFlag = true;
4690 return mozilla::fuzzerRunner->Run(&gArgc, &gArgv);
4691 }
4692#endif
4693
4694 if (PR_GetEnv("MOZ_RUN_GTEST")) {
4695 int result;
4696#ifdef XP_WIN
4697 UseParentConsole();
4698#endif
4699 // RunGTest will only be set if we're in xul-unit
4700 if (mozilla::RunGTest) {
4701 gIsGtest = true;
4702 result = mozilla::RunGTest(&gArgc, gArgv);
4703 gIsGtest = false;
4704 } else {
4705 result = 1;
4706 printf("TEST-UNEXPECTED-FAIL | gtest | Not compiled with enable-tests\n");
4707 }
4708 *aExitFlag = true;
4709 return result;
4710 }
4711
4712 bool isBackgroundTaskMode = false;
4713#ifdef MOZ_BACKGROUNDTASKS1
4714 isBackgroundTaskMode = BackgroundTasks::IsBackgroundTaskMode();
4715#endif
4716
4717#ifdef MOZ_HAS_REMOTE1
4718 if (gfxPlatform::IsHeadless()) {
4719 mDisableRemoteClient = true;
4720 mDisableRemoteServer = true;
4721 }
4722#endif
4723
4724#ifdef MOZ_X111
4725 // Init X11 in thread-safe mode. Must be called prior to the first call to
4726 // XOpenDisplay (called inside gdk_display_open). This is a requirement for
4727 // off main tread compositing.
4728 if (!isBackgroundTaskMode && !gfxPlatform::IsHeadless()) {
4729 XInitThreads();
4730 }
4731#endif
4732#if defined(MOZ_WIDGET_GTK1)
4733 if (!isBackgroundTaskMode && !gfxPlatform::IsHeadless()) {
4734 const char* display_name = nullptr;
4735 bool saveDisplayArg = false;
4736
4737 // display_name is owned by gdk.
4738 display_name = gdk_get_display_arg_name();
4739 bool waylandEnabled = IsWaylandEnabled();
4740# ifdef MOZ_WAYLAND1
4741 if (!display_name) {
4742 auto* proxyEnv = getenv("MOZ_DISABLE_WAYLAND_PROXY");
4743 bool disableWaylandProxy = proxyEnv && *proxyEnv;
4744 if (!disableWaylandProxy && XRE_IsParentProcess() && waylandEnabled) {
4745# ifdef MOZ_LOGGING1
4746 if (MOZ_LOG_TEST(gWidgetWaylandLog, mozilla::LogLevel::Debug)(__builtin_expect(!!(mozilla::detail::log_test(gWidgetWaylandLog
, mozilla::LogLevel::Debug)), 0))
) {
4747 WaylandProxy::SetVerbose(true);
4748 }
4749# endif
4750 gWaylandProxy = WaylandProxy::Create();
4751 if (gWaylandProxy) {
4752 gWaylandProxy->RunThread();
4753 }
4754 }
4755 }
4756# endif
4757
4758 // if --display argument is given make sure it's
4759 // also passed to ContentChild::Init() by MOZ_GDK_DISPLAY.
4760 if (display_name) {
4761 SaveWordToEnv("MOZ_GDK_DISPLAY", nsDependentCString(display_name));
4762 saveDisplayArg = true;
4763 }
4764
4765 // On Wayland disabled builds read X11 DISPLAY env exclusively
4766 // and don't care about different displays.
4767 if (!waylandEnabled && !display_name) {
4768 display_name = PR_GetEnv("DISPLAY");
4769 if (!display_name) {
4770 PR_fprintf(PR_STDERRPR_GetSpecialFD(PR_StandardError),
4771 "Error: no DISPLAY environment variable specified\n");
4772 return 1;
4773 }
4774 }
4775
4776 if (display_name) {
4777 GdkDisplay* disp = gdk_display_open(display_name);
4778 if (!disp) {
4779 PR_fprintf(PR_STDERRPR_GetSpecialFD(PR_StandardError), "Error: cannot open display: %s\n", display_name);
4780 return 1;
4781 }
4782 if (saveDisplayArg) {
4783 if (GdkIsX11Display(disp)) {
4784 SaveWordToEnv("DISPLAY", nsDependentCString(display_name));
4785 } else if (GdkIsWaylandDisplay(disp)) {
4786 SaveWordToEnv("WAYLAND_DISPLAY", nsDependentCString(display_name));
4787 }
4788 }
4789 } else {
4790 gdk_display_manager_open_display(gdk_display_manager_get(), nullptr);
4791 }
4792# if defined(MOZ_WAYLAND1)
4793 // We want to use proxy for main connection only so
4794 // restore original Wayland display for next potential Wayland connections
4795 // from gfx probe code and so on.
4796 if (gWaylandProxy) {
4797 gWaylandProxy->RestoreWaylandDisplay();
4798 }
4799 if (waylandEnabled && PR_GetEnv("WAYLAND_DISPLAY") && GdkIsX11Display()) {
4800 // Gtk somehow switched to X11 display but we want Wayland.
4801 // It may happen if compositor response is missig or it's slow
4802 // or WAYLAND_DISPLAY is wrong. In such case throw warning but
4803 // run with X11.
4804 Output(true,
4805 "Error: Failed to open Wayland display, fallback to X11. "
4806 "WAYLAND_DISPLAY='%s' DISPLAY='%s'\n",
4807 PR_GetEnv("WAYLAND_DISPLAY"), PR_GetEnv("DISPLAY"));
4808
4809 // We need to unset WAYLAND_DISPLAY. Gfx probe code doesn't have fallback
4810 // to X11 and we'll end with Browser running SW rendering only then.
4811 g_unsetenv("WAYLAND_DISPLAY");
4812 gWaylandProxy = nullptr;
4813 }
4814# endif
4815 if (!gdk_display_get_default()) {
4816 Output(true,
4817 "Error: we don't have any display, WAYLAND_DISPLAY='%s' "
4818 "DISPLAY='%s'\n",
4819 PR_GetEnv("WAYLAND_DISPLAY"), PR_GetEnv("DISPLAY"));
4820 return 1;
4821 }
4822 // Check that Wayland only and X11 only builds
4823 // use appropriate displays.
4824# if defined(MOZ_WAYLAND1) && !defined(MOZ_X111)
4825 if (!GdkIsWaylandDisplay()) {
4826 Output(true, "Wayland only build is missig Wayland display!\n");
4827 return 1;
4828 }
4829# endif
4830# if !defined(MOZ_WAYLAND1) && defined(MOZ_X111)
4831 if (!GdkIsX11Display()) {
4832 Output(true, "X11 only build is missig X11 display!\n");
4833 return 1;
4834 }
4835# endif
4836 }
4837#endif
4838#if defined(MOZ_HAS_REMOTE1)
4839 // handle --remote now that xpcom is fired up
4840 mRemoteService = new nsRemoteService(gAppData->remotingName);
4841 if (mRemoteService && !mDisableRemoteServer) {
4842 mRemoteService->LockStartup();
4843 gRemoteService = mRemoteService;
4844 }
4845#endif
4846#if defined(MOZ_WIDGET_GTK1)
4847 g_set_application_name(mAppData->name);
4848
4849#endif /* defined(MOZ_WIDGET_GTK) */
4850#ifdef MOZ_X111
4851 // Do this after initializing GDK, or GDK will install its own handler.
4852 XRE_InstallX11ErrorHandler();
4853#endif
4854
4855 // Call the code to install our handler
4856#ifdef MOZ_JPROF
4857 setupProfilingStuff();
4858#endif
4859
4860 bool canRun = false;
4861 rv = mNativeApp->Start(&canRun);
4862 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || !canRun) {
4863 return 1;
4864 }
4865
4866#ifdef MOZ_WIDGET_GTK1
4867 // startup token might be cleared now, we recover it in case we need a
4868 // restart.
4869 if (!mDesktopStartupID.IsEmpty()) {
4870 // Leak it with extreme prejudice!
4871 PR_SetEnv(ToNewCString("DESKTOP_STARTUP_ID="_ns + mDesktopStartupID));
4872 }
4873
4874 if (!mXDGActivationToken.IsEmpty()) {
4875 // Leak it with extreme prejudice!
4876 PR_SetEnv(ToNewCString("XDG_ACTIVATION_TOKEN="_ns + mXDGActivationToken));
4877 }
4878#endif
4879
4880 // Support exiting early for testing startup sequence. Bug 1360493
4881 if (CheckArg("test-launch-without-hang")) {
4882 *aExitFlag = true;
4883 return 0;
4884 }
4885
4886 mProfileSvc = NS_GetToolkitProfileService();
4887 if (!mProfileSvc) {
4888 // We failed to choose or create profile - notify user and quit
4889 ProfileMissingDialog(mNativeApp);
4890 return 1;
4891 }
4892
4893 bool wasDefaultSelection;
4894 nsCOMPtr<nsIToolkitProfile> profile;
4895 rv = SelectProfile(mProfileSvc, mNativeApp, getter_AddRefs(mProfD),
4896 getter_AddRefs(mProfLD), getter_AddRefs(profile),
4897 &wasDefaultSelection);
4898 if (rv == NS_ERROR_LAUNCHED_CHILD_PROCESS || rv == NS_ERROR_ABORT) {
4899 *aExitFlag = true;
4900 return 0;
4901 }
4902
4903 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
4904 // We failed to choose or create profile - notify user and quit
4905 ProfileMissingDialog(mNativeApp);
4906 return 1;
4907 }
4908
4909#if defined(MOZ_HAS_REMOTE1)
4910 if (mRemoteService) {
4911 // We want a unique profile name to identify the remote instance.
4912 nsCString profileName;
4913 if (profile) {
4914 rv = profile->GetName(profileName);
4915 }
4916 if (!profile || NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0))) || profileName.IsEmpty()) {
4917 // Couldn't get a name from the profile. Use the directory name?
4918 nsString leafName;
4919 rv = mProfD->GetLeafName(leafName);
4920 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
4921 CopyUTF16toUTF8(leafName, profileName);
4922 }
4923 }
4924
4925 mRemoteService->SetProfile(profileName);
4926
4927 if (!mDisableRemoteClient) {
4928 // Try to remote the entire command line. If this fails, start up
4929 // normally.
4930# ifdef MOZ_WIDGET_GTK1
4931 const auto& startupToken =
4932 GdkIsWaylandDisplay() ? mXDGActivationToken : mDesktopStartupID;
4933# else
4934 const nsCString startupToken;
4935# endif
4936 RemoteResult rr = mRemoteService->StartClient(
4937 startupToken.IsEmpty() ? nullptr : startupToken.get());
4938 if (rr == REMOTE_FOUND) {
4939 *aExitFlag = true;
4940 mRemoteService->UnlockStartup();
4941 return 0;
4942 }
4943 if (rr == REMOTE_ARG_BAD) {
4944 mRemoteService->UnlockStartup();
4945 return 1;
4946 }
4947 }
4948 }
4949#endif
4950
4951#if defined(MOZ_UPDATER1) && !defined(MOZ_WIDGET_ANDROID)
4952# ifdef XP_WIN
4953 {
4954 // When automatically restarting for a staged background update
4955 // we want the child process to wait here so that the updater
4956 // does not register two instances running and use that as a
4957 // reason to not process updates. This function requires having
4958 // -restart-pid <param> in the command line to function properly.
4959 // Ensure we keep -restart-pid if we are running tests
4960 if (ARG_FOUND == CheckArgExists("restart-pid") &&
4961 !CheckArg("test-only-automatic-restart-no-wait")) {
4962 // We're not testing and can safely remove it now and read the pid.
4963 const char* restartPidString = nullptr;
4964 CheckArg("restart-pid", &restartPidString, CheckArgFlag::RemoveArg);
4965 // pid should be first parameter following -restart-pid.
4966 uint32_t pid = nsDependentCString(restartPidString).ToInteger(&rv, 10U);
4967 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1))) && pid > 0) {
4968 printf_stderr(
4969 "*** MaybeWaitForProcessExit: launched pidDWORD = %u ***\n", pid);
4970 RefPtr<nsUpdateProcessor> updater = new nsUpdateProcessor();
4971 if (NS_FAILED(((bool)(__builtin_expect(!!(NS_FAILED_impl(updater->WaitForProcessExit
(pid, MAYBE_WAIT_TIMEOUT_MS))), 0)))
4972 updater->WaitForProcessExit(pid, MAYBE_WAIT_TIMEOUT_MS))((bool)(__builtin_expect(!!(NS_FAILED_impl(updater->WaitForProcessExit
(pid, MAYBE_WAIT_TIMEOUT_MS))), 0)))
) {
4973 NS_WARNING("Failed to MaybeWaitForProcessExit.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to MaybeWaitForProcessExit."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4973)
;
4974 }
4975 } else {
4976 NS_WARNING("Failed to parse pid from -restart-pid.")NS_DebugBreak(NS_DEBUG_WARNING, "Failed to parse pid from -restart-pid."
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 4976)
;
4977 }
4978 }
4979 }
4980# endif
4981 Maybe<ShouldNotProcessUpdatesReason> shouldNotProcessUpdatesReason =
4982 ShouldNotProcessUpdates(mDirProvider);
4983 if (shouldNotProcessUpdatesReason.isNothing()) {
4984 // Check for and process any available updates
4985 nsCOMPtr<nsIFile> updRoot;
4986 bool persistent;
4987 rv = mDirProvider.GetFile(XRE_UPDATE_ROOT_DIR"UpdRootD", &persistent,
4988 getter_AddRefs(updRoot));
4989 // XRE_UPDATE_ROOT_DIR may fail. Fallback to appDir if failed
4990 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
4991 updRoot = mDirProvider.GetAppDir();
4992 }
4993
4994 // If the MOZ_TEST_PROCESS_UPDATES environment variable already exists, then
4995 // we are being called from the callback application.
4996 if (EnvHasValue("MOZ_TEST_PROCESS_UPDATES")) {
4997 // If the caller has asked us to log our arguments, do so. This is used
4998 // to make sure that the maintenance service successfully launches the
4999 // callback application.
5000 const char* logFile = nullptr;
5001 if (ARG_FOUND == CheckArg("dump-args", &logFile)) {
5002 FILE* logFP = fopen(logFile, "wb");
5003 if (logFP) {
5004 for (int i = 1; i < gRestartArgc; ++i) {
5005 fprintf(logFP, "%s\n", gRestartArgv[i]);
5006 }
5007 fclose(logFP);
5008 }
5009 }
5010 *aExitFlag = true;
5011 return 0;
5012 }
5013
5014 // Support for processing an update and exiting. The
5015 // MOZ_TEST_PROCESS_UPDATES environment variable will be part of the
5016 // updater's environment and the application that is relaunched by the
5017 // updater. When the application is relaunched by the updater it will be
5018 // removed below and the application will exit.
5019 if (CheckArg("test-process-updates")) {
5020 SaveToEnv("MOZ_TEST_PROCESS_UPDATES=1");
5021 }
5022 nsCOMPtr<nsIFile> exeFile, exeDir;
5023 rv = mDirProvider.GetFile(XRE_EXECUTABLE_FILE"XREExeF", &persistent,
5024 getter_AddRefs(exeFile));
5025 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5025); return 1; } } while (false)
;
5026 rv = exeFile->GetParent(getter_AddRefs(exeDir));
5027 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5027); return 1; } } while (false)
;
5028 ProcessUpdates(mDirProvider.GetGREDir(), exeDir, updRoot, gRestartArgc,
5029 gRestartArgv, mAppData->version);
5030 if (EnvHasValue("MOZ_TEST_PROCESS_UPDATES")) {
5031 SaveToEnv("MOZ_TEST_PROCESS_UPDATES=");
5032 *aExitFlag = true;
5033 return 0;
5034 }
5035 } else {
5036 if (CheckArg("test-process-updates") ||
5037 EnvHasValue("MOZ_TEST_PROCESS_UPDATES")) {
5038 // Support for testing *not* processing an update. The launched process
5039 // can witness this environment variable and conclude that its runtime
5040 // environment resulted in not processing updates.
5041
5042 SaveToEnv(nsPrintfCString(
5043 "MOZ_TEST_PROCESS_UPDATES=ShouldNotProcessUpdates(): %s",
5044 ShouldNotProcessUpdatesReasonAsString(
5045 shouldNotProcessUpdatesReason.value()))
5046 .get());
5047 }
5048 }
5049#endif
5050
5051 // We now know there is no existing instance using the selected profile. If
5052 // the profile wasn't selected by specific command line arguments and the
5053 // user has chosen to show the profile manager on startup then do that.
5054 if (wasDefaultSelection) {
5055 bool useSelectedProfile;
5056 rv = mProfileSvc->GetStartWithLastProfile(&useSelectedProfile);
5057 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5057); return 1; } } while (false)
;
5058
5059 if (!useSelectedProfile) {
5060 rv = ShowProfileManager(mProfileSvc, mNativeApp);
5061 if (rv == NS_ERROR_LAUNCHED_CHILD_PROCESS || rv == NS_ERROR_ABORT) {
5062 *aExitFlag = true;
5063 return 0;
5064 }
5065 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5066 return 1;
5067 }
5068 }
5069 }
5070
5071 // We always want to lock the profile even if we're actually going to reset
5072 // it later.
5073 rv = LockProfile(mNativeApp, mProfD, mProfLD, profile,
5074 getter_AddRefs(mProfileLock));
5075 if (rv == NS_ERROR_LAUNCHED_CHILD_PROCESS || rv == NS_ERROR_ABORT) {
5076 *aExitFlag = true;
5077 return 0;
5078 } else if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5079 return 1;
5080 }
5081
5082 if (gDoProfileReset) {
5083 if (EnvHasValue("MOZ_RESET_PROFILE_RESTART")) {
5084 SaveToEnv("MOZ_RESET_PROFILE_RESTART=");
5085 // We only want to restore the previous session if the profile refresh was
5086 // triggered by user. And if it was a user-triggered profile refresh
5087 // through, say, the safeMode dialog or the troubleshooting page, the
5088 // MOZ_RESET_PROFILE_RESTART env variable would be set. Hence we set
5089 // MOZ_RESET_PROFILE_MIGRATE_SESSION here so that Firefox profile migrator
5090 // would migrate old session data later.
5091 SaveToEnv("MOZ_RESET_PROFILE_MIGRATE_SESSION=1");
5092 }
5093 // Unlock the source profile.
5094 mProfileLock->Unlock();
5095
5096 // If we're resetting a profile, create a new one and use it to startup.
5097 gResetOldProfile = profile;
5098 rv = mProfileSvc->CreateResetProfile(getter_AddRefs(profile));
5099 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5100 rv = profile->GetRootDir(getter_AddRefs(mProfD));
5101 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5101); return 1; } } while (false)
;
5102 SaveFileToEnv("XRE_PROFILE_PATH", mProfD);
5103
5104 rv = profile->GetLocalDir(getter_AddRefs(mProfLD));
5105 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5105); return 1; } } while (false)
;
5106 SaveFileToEnv("XRE_PROFILE_LOCAL_PATH", mProfLD);
5107
5108 // Lock the new profile
5109 rv = LockProfile(mNativeApp, mProfD, mProfLD, profile,
5110 getter_AddRefs(mProfileLock));
5111 if (rv == NS_ERROR_LAUNCHED_CHILD_PROCESS || rv == NS_ERROR_ABORT) {
5112 *aExitFlag = true;
5113 return 0;
5114 } else if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5115 return 1;
5116 }
5117 } else {
5118 NS_WARNING("Profile reset failed.")NS_DebugBreak(NS_DEBUG_WARNING, "Profile reset failed.", nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5118)
;
5119 return 1;
5120 }
5121 }
5122
5123 gProfileLock = mProfileLock;
5124
5125 nsAutoCString version;
5126 BuildVersion(version);
5127
5128#ifdef TARGET_OS_ABI"Linux_x86_64-gcc3"
5129 constexpr auto osABI = nsLiteralCString{TARGET_OS_ABI"Linux_x86_64-gcc3"};
5130#else
5131 // No TARGET_XPCOM_ABI, but at least the OS is known
5132 constexpr auto osABI = nsLiteralCString{OS_TARGET"Linux" "_UNKNOWN"};
5133#endif
5134
5135 // Check for version compatibility with the last version of the app this
5136 // profile was started with. The format of the version stamp is defined
5137 // by the BuildVersion function.
5138 // Also check to see if something has happened to invalidate our
5139 // fastload caches, like an app upgrade.
5140
5141 // If we see .purgecaches, that means someone did a make.
5142 // Re-register components to catch potential changes.
5143 nsCOMPtr<nsIFile> flagFile;
5144 if (mAppData->directory) {
5145 Unused << mAppData->directory->Clone(getter_AddRefs(flagFile));
5146 }
5147 if (flagFile) {
5148 flagFile->AppendNative(FILE_INVALIDATE_CACHES".purgecaches"_ns);
5149 }
5150
5151 bool cachesOK;
5152 bool isDowngrade;
5153 nsCString lastVersion;
5154 bool versionOK = CheckCompatibility(
5155 mProfD, version, osABI, mDirProvider.GetGREDir(), mAppData->directory,
5156 flagFile, &cachesOK, &isDowngrade, lastVersion);
5157
5158 MOZ_RELEASE_ASSERT(!cachesOK || lastVersion.Equals(version),do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!cachesOK || lastVersion.Equals(version))>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(!cachesOK || lastVersion.Equals(version)))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("!cachesOK || lastVersion.Equals(version)"
" (" "Caches cannot be good if the version has changed." ")"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5159); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "!cachesOK || lastVersion.Equals(version)"
") (" "Caches cannot be good if the version has changed." ")"
); do { *((volatile int*)__null) = 5159; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
5159 "Caches cannot be good if the version has changed.")do { static_assert( mozilla::detail::AssertionConditionType<
decltype(!cachesOK || lastVersion.Equals(version))>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(!cachesOK || lastVersion.Equals(version)))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("!cachesOK || lastVersion.Equals(version)"
" (" "Caches cannot be good if the version has changed." ")"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5159); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "!cachesOK || lastVersion.Equals(version)"
") (" "Caches cannot be good if the version has changed." ")"
); do { *((volatile int*)__null) = 5159; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
5160
5161#ifdef MOZ_BLOCK_PROFILE_DOWNGRADE1
5162 // The argument check must come first so the argument is always removed from
5163 // the command line regardless of whether this is a downgrade or not.
5164 if (!CheckArg("allow-downgrade") && isDowngrade &&
5165 !EnvHasValue("MOZ_ALLOW_DOWNGRADE")) {
5166# ifdef XP_MACOSX
5167 InitializeMacApp();
5168# endif
5169 rv = CheckDowngrade(mProfD, mNativeApp, mProfileSvc, lastVersion);
5170 if (rv == NS_ERROR_LAUNCHED_CHILD_PROCESS || rv == NS_ERROR_ABORT) {
5171 *aExitFlag = true;
5172 return 0;
5173 }
5174 }
5175#endif
5176
5177 rv = mDirProvider.SetProfile(mProfD, mProfLD);
5178 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5178); return 1; } } while (false)
;
5179
5180 //////////////////////// NOW WE HAVE A PROFILE ////////////////////////
5181
5182 mozilla::Telemetry::SetProfileDir(mProfD);
5183
5184 if (mAppData->flags & NS_XRE_ENABLE_CRASH_REPORTER(1 << 3)) {
5185 MakeOrSetMinidumpPath(mProfD);
5186 }
5187
5188 CrashReporter::SetProfileDirectory(mProfD);
5189
5190#ifdef MOZ_ASAN_REPORTER
5191 // In ASan reporter builds, we need to set ASan's log_path as early as
5192 // possible, so it dumps its errors into files there instead of using
5193 // the default stderr location. Since this is crucial for ASan reporter
5194 // to work at all (and we don't want people to use a non-functional
5195 // ASan reporter build), all failures while setting log_path are fatal.
5196 setASanReporterPath(mProfD);
5197
5198 // Export to env for child processes
5199 SaveFileToEnv("ASAN_REPORTER_PATH", mProfD);
5200#endif
5201
5202 bool lastStartupWasCrash = CheckLastStartupWasCrash();
5203
5204 CrashReporter::RecordAnnotationBool(
5205 CrashReporter::Annotation::LastStartupWasCrash, lastStartupWasCrash);
5206
5207 if (CheckArg("purgecaches") || PR_GetEnv("MOZ_PURGE_CACHES") ||
5208 lastStartupWasCrash || gSafeMode) {
5209 cachesOK = false;
5210 }
5211
5212 CrashReporter::RecordAnnotationBool(
5213 CrashReporter::Annotation::StartupCacheValid, cachesOK && versionOK);
5214
5215 // Every time a profile is loaded by a build with a different version,
5216 // it updates the compatibility.ini file saying what version last wrote
5217 // the fastload caches. On subsequent launches if the version matches,
5218 // there is no need for re-registration. If the user loads the same
5219 // profile in different builds the component registry must be
5220 // re-generated to prevent mysterious component loading failures.
5221 //
5222 bool startupCacheValid = true;
5223
5224 if (!cachesOK || !versionOK) {
5225 QuotaManager::InvalidateQuotaCache();
5226
5227 startupCacheValid = RemoveComponentRegistries(mProfD, mProfLD, false);
5228
5229 // Rewrite compatibility.ini to match the current build. The next run
5230 // should attempt to invalidate the caches if either this run is safe mode
5231 // or the attempt to invalidate the caches this time failed.
5232 WriteVersion(mProfD, version, osABI, mDirProvider.GetGREDir(),
5233 mAppData->directory, gSafeMode || !startupCacheValid);
5234 }
5235
5236 if (!startupCacheValid) StartupCache::IgnoreDiskCache();
5237
5238 if (flagFile) {
5239 flagFile->Remove(true);
5240 }
5241
5242 // Flush any pending page load events.
5243 mozilla::glean_pings::Pageload.Submit("startup"_ns);
5244
5245 if (!isBackgroundTaskMode) {
5246#ifdef USE_GLX_TEST1
5247 GfxInfo::FireGLXTestProcess();
5248#endif
5249#ifdef MOZ_WAYLAND1
5250 // Make sure we have wayland connection for main thread.
5251 // It's used as template to create display connections
5252 // for different threads.
5253 if (IsWaylandEnabled()) {
5254 MOZ_UNUSED(WaylandDisplayGet())do { if (WaylandDisplayGet()) { (void)0; } } while (0);
5255 }
5256#endif
5257#ifdef MOZ_WIDGET_GTK1
5258 nsAppShell::InstallTermSignalHandler();
5259#endif
5260 }
5261
5262 return 0;
5263}
5264
5265#if defined(MOZ_SANDBOX1)
5266void AddSandboxAnnotations() {
5267 CrashReporter::RecordAnnotationU32(
5268 CrashReporter::Annotation::ContentSandboxLevel,
5269 GetEffectiveContentSandboxLevel());
5270 CrashReporter::RecordAnnotationU32(CrashReporter::Annotation::GpuSandboxLevel,
5271 GetEffectiveGpuSandboxLevel());
5272
5273 // Include whether or not this instance is capable of content sandboxing
5274 bool sSandboxCapable = false;
5275
5276# if defined(XP_WIN)
5277 // All supported Windows versions support some level of content sandboxing
5278 sSandboxCapable = true;
5279# elif defined(XP_MACOSX)
5280 // All supported OS X versions are capable
5281 sSandboxCapable = true;
5282# elif defined(XP_LINUX1)
5283 sSandboxCapable = SandboxInfo::Get().CanSandboxContent();
5284# elif defined(__OpenBSD__)
5285 sSandboxCapable = true;
5286 StartOpenBSDSandbox(GeckoProcessType_Default);
5287# endif
5288
5289 CrashReporter::RecordAnnotationBool(
5290 CrashReporter::Annotation::ContentSandboxCapable, sSandboxCapable);
5291}
5292#endif /* MOZ_SANDBOX */
5293
5294/*
5295 * XRE_mainRun - Command line startup, profile migration, and
5296 * the calling of appStartup->Run().
5297 */
5298nsresult XREMain::XRE_mainRun() {
5299 nsresult rv = NS_OK;
5300 NS_ASSERTION(mScopedXPCOM, "Scoped xpcom not initialized.")do { if (!(mScopedXPCOM)) { NS_DebugBreak(NS_DEBUG_ASSERTION,
"Scoped xpcom not initialized.", "mScopedXPCOM", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5300); MOZ_PretendNoReturn(); } } while (0)
;
5301
5302#if defined(XP_WIN)
5303 RefPtr<mozilla::DllServices> dllServices(mozilla::DllServices::Get());
5304 dllServices->StartUntrustedModulesProcessor(false);
5305 auto dllServicesDisable =
5306 MakeScopeExit([&dllServices]() { dllServices->DisableFull(); });
5307
5308 mozilla::mscom::InitProfilerMarkers();
5309#endif // defined(XP_WIN)
5310
5311 // We need the appStartup pointer to span multiple scopes, so we declare
5312 // it here.
5313 nsCOMPtr<nsIAppStartup> appStartup;
5314 // Ditto with the command line.
5315 nsCOMPtr<nsICommandLineRunner> cmdLine;
5316
5317 {
5318#ifdef XP_MACOSX
5319 // In this scope, create an autorelease pool that will leave scope with
5320 // it just before entering our event loop.
5321 mozilla::MacAutoreleasePool pool;
5322#endif
5323
5324 rv = mScopedXPCOM->SetWindowCreator(mNativeApp);
5325 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5325); return NS_ERROR_FAILURE; } } while (false)
;
5326
5327 // tell the crash reporter to also send the release channel
5328 nsCOMPtr<nsIPrefService> prefs =
5329 do_GetService("@mozilla.org/preferences-service;1", &rv);
5330 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5331 nsCOMPtr<nsIPrefBranch> defaultPrefBranch;
5332 rv = prefs->GetDefaultBranch(nullptr, getter_AddRefs(defaultPrefBranch));
5333
5334 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5335 nsAutoCString sval;
5336 rv = defaultPrefBranch->GetCharPref("app.update.channel", sval);
5337 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5338 CrashReporter::RecordAnnotationNSCString(
5339 CrashReporter::Annotation::ReleaseChannel, sval);
5340 }
5341 }
5342 }
5343 // Needs to be set after xpcom initialization.
5344 bool includeContextHeap = Preferences::GetBool(
5345 "toolkit.crashreporter.include_context_heap", false);
5346 CrashReporter::SetIncludeContextHeap(includeContextHeap);
5347
5348#if defined(XP_LINUX1) && !defined(ANDROID)
5349 PR_CreateThread(PR_USER_THREAD, AnnotateLSBRelease, 0, PR_PRIORITY_LOW,
5350 PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
5351#endif
5352
5353 if (mStartOffline) {
5354 nsCOMPtr<nsIIOService> io(
5355 do_GetService("@mozilla.org/network/io-service;1"));
5356 NS_ENSURE_TRUE(io, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(io)), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "io" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5356); return NS_ERROR_FAILURE; } } while (false)
;
5357 io->SetManageOfflineStatus(false);
5358 io->SetOffline(true);
5359 }
5360
5361#ifdef XP_WIN
5362 mozilla::DllPrefetchExperimentRegistryInfo prefetchRegInfo;
5363 mozilla::AlteredDllPrefetchMode dllPrefetchMode =
5364 prefetchRegInfo.GetAlteredDllPrefetchMode();
5365
5366 if (!PR_GetEnv("XRE_NO_DLL_READAHEAD") &&
5367 dllPrefetchMode != mozilla::AlteredDllPrefetchMode::NoPrefetch) {
5368 nsCOMPtr<nsIFile> greDir = mDirProvider.GetGREDir();
5369 nsAutoString path;
5370 rv = greDir->GetPath(path);
5371 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5372 PRThread* readAheadThread;
5373 wchar_t* pathRaw;
5374
5375 // We use the presence of a path argument inside the thread to determine
5376 // which list of Dlls to use. The old list does not need access to the
5377 // GRE dir, so the path argument is set to a null pointer.
5378 if (dllPrefetchMode ==
5379 mozilla::AlteredDllPrefetchMode::OptimizedPrefetch) {
5380 pathRaw = new wchar_t[MAX_PATH];
5381 wcscpy_s(pathRaw, MAX_PATH, path.get());
5382 } else {
5383 pathRaw = nullptr;
5384 }
5385 readAheadThread = PR_CreateThread(
5386 PR_USER_THREAD, ReadAheadDlls_ThreadStart, (void*)pathRaw,
5387 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
5388 if (readAheadThread == NULL__null) {
5389 delete[] pathRaw;
5390 }
5391 }
5392 }
5393#endif
5394
5395 if (gDoMigration) {
5396 nsCOMPtr<nsIFile> file;
5397 mDirProvider.GetAppDir()->Clone(getter_AddRefs(file));
5398 file->AppendNative("override.ini"_ns);
5399 nsINIParsernsINIParser_internal parser;
5400 nsresult rv = parser.Init(file);
5401 // if override.ini doesn't exist, also check for distribution.ini
5402 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5403 bool persistent;
5404 mDirProvider.GetFile(XRE_APP_DISTRIBUTION_DIR"XREAppDist", &persistent,
5405 getter_AddRefs(file));
5406 file->AppendNative("distribution.ini"_ns);
5407 rv = parser.Init(file);
5408 }
5409 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5410 nsAutoCString buf;
5411 rv = parser.GetString("XRE", "EnableProfileMigrator", buf);
5412 if (NS_SUCCEEDED(rv)((bool)(__builtin_expect(!!(!NS_FAILED_impl(rv)), 1)))) {
5413 if (buf[0] == '0' || buf[0] == 'f' || buf[0] == 'F') {
5414 gDoMigration = false;
5415 }
5416 }
5417 }
5418 }
5419
5420 // We'd like to initialize the JSContext *after* reading the user prefs.
5421 // Unfortunately that's not possible if we have to do profile migration
5422 // because that requires us to execute JS before reading user prefs.
5423 // Restarting the browser after profile migration would fix this. See
5424 // bug 1592523.
5425 bool initializedJSContext = false;
5426
5427 {
5428 // Profile Migration
5429 if (mAppData->flags & NS_XRE_ENABLE_PROFILE_MIGRATOR(1 << 1) && gDoMigration) {
5430 gDoMigration = false;
5431
5432 xpc::InitializeJSContext();
5433 initializedJSContext = true;
5434
5435 nsCOMPtr<nsIProfileMigrator> pm(
5436 do_CreateInstance(NS_PROFILEMIGRATOR_CONTRACTID"@mozilla.org/toolkit/profile-migrator;1"));
5437 if (pm) {
5438 nsAutoCString aKey;
5439 nsAutoCString aName;
5440 if (gDoProfileReset) {
5441 // Automatically migrate from the current application if we just
5442 // reset the profile.
5443 aKey = MOZ_APP_NAME"firefox";
5444 gResetOldProfile->GetName(aName);
5445 }
5446#ifdef XP_MACOSX
5447 // Necessary for migration wizard to be accessible.
5448 InitializeMacApp();
5449#endif
5450 pm->Migrate(&mDirProvider, aKey, aName);
5451 }
5452 }
5453
5454 if (gDoProfileReset) {
5455 if (!initializedJSContext) {
5456 xpc::InitializeJSContext();
5457 initializedJSContext = true;
5458 }
5459
5460 nsresult backupCreated =
5461 ProfileResetCleanup(mProfileSvc, gResetOldProfile);
5462 if (NS_FAILED(backupCreated)((bool)(__builtin_expect(!!(NS_FAILED_impl(backupCreated)), 0
)))
) {
5463 NS_WARNING("Could not cleanup the profile that was reset")NS_DebugBreak(NS_DEBUG_WARNING, "Could not cleanup the profile that was reset"
, nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5463)
;
5464 }
5465 }
5466 }
5467
5468 // Initialize user preferences before notifying startup observers so they're
5469 // ready in time for early consumers, such as the component loader.
5470 mDirProvider.InitializeUserPrefs();
5471
5472 // Now that all (user) prefs have been loaded we can initialize the main
5473 // thread's JSContext.
5474 if (!initializedJSContext) {
5475 xpc::InitializeJSContext();
5476 }
5477
5478 // Finally, now that JS has been initialized, we can finish pref loading.
5479 // This needs to happen after JS and XPConnect initialization because
5480 // AutoConfig files require JS execution. Note that this means AutoConfig
5481 // files can't override JS engine start-up prefs.
5482 mDirProvider.FinishInitializingUserPrefs();
5483
5484 // Do a canary load of a JS based module here. This will help us detect
5485 // missing resources during startup and make us react appropriate, that
5486 // is inform the user before exiting with a crash.
5487 {
5488 mozilla::dom::AutoJSAPI jsapi;
5489 MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()))do { if ((__builtin_expect(!!(jsapi.Init(xpc::PrivilegedJunkScope
())), 1))) { } else { do { static_assert( mozilla::detail::AssertionConditionType
<decltype(false)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(false))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("false" " (" "jsapi.Init(xpc::PrivilegedJunkScope())"
")", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5489); AnnotateMozCrashReason("MOZ_DIAGNOSTIC_ASSERT" "(" "false"
") (" "jsapi.Init(xpc::PrivilegedJunkScope())" ")"); do { *(
(volatile int*)__null) = 5489; __attribute__((nomerge)) ::abort
(); } while (false); } } while (false); } } while (false)
;
5490 JS::Rooted<JSObject*> mod(jsapi.cx());
5491 // AppConstants.sys.mjs is small, widely used and most likely will
5492 // never go away.
5493 rv = mozJSModuleLoader::Get()->ImportESModule(
5494 jsapi.cx(), "resource://gre/modules/AppConstants.sys.mjs"_ns, &mod);
5495 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5496 return NS_ERROR_OMNIJAR_OR_DIR_MISSING;
5497 }
5498 }
5499
5500#if defined(MOZ_SANDBOX1) && defined(XP_WIN)
5501 // Now that we have preferences and the directory provider, we can
5502 // finish initializing SandboxBroker. This must happen before the GFX
5503 // platform is initialized (which will launch the GPU process), which
5504 // occurs when the "app-startup" category is started up below
5505 //
5506 // After this completes, we are ready to launch sandboxed processes
5507 mozilla::SandboxBroker::GeckoDependentInitialize();
5508#endif
5509
5510 nsCOMPtr<nsIFile> workingDir;
5511 rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR"CurWorkD",
5512 getter_AddRefs(workingDir));
5513 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5514 // No working dir? This can happen if it gets deleted before we start.
5515 workingDir = nullptr;
5516 }
5517
5518 cmdLine = new nsCommandLine();
5519
5520 rv = cmdLine->Init(gArgc, gArgv, workingDir,
5521 nsICommandLine::STATE_INITIAL_LAUNCH);
5522 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5522); return NS_ERROR_FAILURE; } } while (false)
;
5523
5524 // "app-startup" is the name of both the category and the event
5525 NS_CreateServicesFromCategory("app-startup", cmdLine, "app-startup",
5526 nullptr);
5527
5528 appStartup = components::AppStartup::Service();
5529 NS_ENSURE_TRUE(appStartup, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(appStartup)), 0))) { NS_DebugBreak
(NS_DEBUG_WARNING, "NS_ENSURE_TRUE(" "appStartup" ") failed",
nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5529); return NS_ERROR_FAILURE; } } while (false)
;
5530
5531 mDirProvider.DoStartup();
5532
5533#ifdef XP_WIN
5534 // It needs to be called on the main thread because it has to use
5535 // nsObserverService.
5536 EnsureWin32kInitialized();
5537#endif
5538
5539 // As FilePreferences need the profile directory, we must initialize right
5540 // here.
5541 mozilla::FilePreferences::InitDirectoriesAllowlist();
5542 mozilla::FilePreferences::InitPrefs();
5543
5544 nsCString userAgentLocale;
5545 LocaleService::GetInstance()->GetAppLocaleAsBCP47(userAgentLocale);
5546 CrashReporter::RecordAnnotationNSCString(
5547 CrashReporter::Annotation::useragent_locale, userAgentLocale);
5548
5549 if (!AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
5550 /* Special-case services that need early access to the command
5551 line. */
5552 nsCOMPtr<nsIObserverService> obsService =
5553 mozilla::services::GetObserverService();
5554 if (obsService) {
5555 obsService->NotifyObservers(cmdLine, "command-line-startup", nullptr);
5556 }
5557 }
5558
5559#ifdef XP_WIN
5560 // Hack to sync up the various environment storages. XUL_APP_FILE is special
5561 // in that it comes from a different CRT (firefox.exe's static-linked copy).
5562 // Ugly details in http://bugzil.la/1175039#c27
5563 char appFile[MAX_PATH];
5564 if (GetEnvironmentVariableA("XUL_APP_FILE", appFile, sizeof(appFile))) {
5565 SmprintfPointer saved = mozilla::Smprintf("XUL_APP_FILE=%s", appFile);
5566 // We intentionally leak the string here since it is required by
5567 // PR_SetEnv.
5568 PR_SetEnv(saved.release());
5569 }
5570#endif
5571
5572 mozilla::AppShutdown::SaveEnvVarsForPotentialRestart();
5573
5574 // clear out any environment variables which may have been set
5575 // during the relaunch process now that we know we won't be relaunching.
5576 SaveToEnv("XRE_PROFILE_PATH=");
5577 SaveToEnv("XRE_PROFILE_LOCAL_PATH=");
5578 SaveToEnv("XRE_START_OFFLINE=");
5579 SaveToEnv("XUL_APP_FILE=");
5580 SaveToEnv("XRE_BINARY_PATH=");
5581 SaveToEnv("XRE_RESTARTED_BY_PROFILE_MANAGER=");
5582
5583 if (!AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
5584 // Don't create the hidden window during startup on
5585 // platforms that don't always need it.
5586#ifdef XP_MACOSX
5587 bool lazyHiddenWindow = false;
5588#else
5589 bool lazyHiddenWindow = true;
5590#endif
5591
5592#ifdef MOZ_BACKGROUNDTASKS1
5593 if (BackgroundTasks::IsBackgroundTaskMode()) {
5594 // Background tasks aren't going to load a chrome XUL document.
5595 lazyHiddenWindow = true;
5596 }
5597#endif
5598
5599 if (!lazyHiddenWindow) {
5600 rv = appStartup->CreateHiddenWindow();
5601 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5601); return NS_ERROR_FAILURE; } } while (false)
;
5602 }
5603
5604#ifdef XP_WIN
5605 Preferences::RegisterCallbackAndCall(
5606 RegisterApplicationRestartChanged,
5607 PREF_WIN_REGISTER_APPLICATION_RESTART);
5608 SetupAlteredPrefetchPref();
5609 SetupSkeletonUIPrefs();
5610# if defined(MOZ_LAUNCHER_PROCESS)
5611 SetupLauncherProcessPref();
5612# endif // defined(MOZ_LAUNCHER_PROCESS)
5613# if defined(MOZ_DEFAULT_BROWSER_AGENT)
5614# if defined(MOZ_BACKGROUNDTASKS1)
5615 // The backgroundtask profile is not a browsing profile, let alone the new
5616 // default profile, so don't mirror its properties into the registry.
5617 if (!BackgroundTasks::IsBackgroundTaskMode())
5618# endif // defined(MOZ_BACKGROUNDTASKS)
5619 {
5620 Preferences::RegisterCallbackAndCall(
5621 &OnDefaultAgentTelemetryPrefChanged,
5622 kPrefHealthReportUploadEnabled);
5623 Preferences::RegisterCallbackAndCall(
5624 &OnDefaultAgentTelemetryPrefChanged, kPrefDefaultAgentEnabled);
5625
5626 Preferences::RegisterCallbackAndCall(
5627 &OnDefaultAgentRemoteSettingsPrefChanged,
5628 kPrefServicesSettingsServer);
5629
5630 Preferences::RegisterCallbackAndCall(
5631 &OnSetDefaultBrowserUserChoicePrefChanged,
5632 kPrefSetDefaultBrowserUserChoicePref);
5633
5634 SetDefaultAgentLastRunTime();
5635 }
5636# endif // defined(MOZ_DEFAULT_BROWSER_AGENT)
5637#endif
5638
5639#if defined(MOZ_WIDGET_GTK1)
5640 // Clear the environment variables so they won't be inherited by child
5641 // processes and confuse things.
5642 for (const auto& name : kStartupTokenNames) {
5643 g_unsetenv(name.get());
5644 }
5645#endif
5646
5647#ifdef XP_MACOSX
5648 InitializeMacApp();
5649
5650 // we re-initialize the command-line service and do appleevents munging
5651 // after we are sure that we're not restarting
5652 cmdLine = new nsCommandLine();
5653
5654 char** tempArgv = static_cast<char**>(malloc(gArgc * sizeof(char*)));
5655 for (int i = 0; i < gArgc; i++) {
5656 tempArgv[i] = strdup(gArgv[i]);
5657 }
5658 CommandLineServiceMac::SetupMacCommandLine(gArgc, tempArgv, false);
5659 rv = cmdLine->Init(gArgc, tempArgv, workingDir,
5660 nsICommandLine::STATE_INITIAL_LAUNCH);
5661 free(tempArgv);
5662 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "NS_ERROR_FAILURE", static_cast
<uint32_t>(__rv), name ? " (" : "", name ? name : "", name
? ")" : ""); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5662); return NS_ERROR_FAILURE; } } while (false)
;
5663
5664# ifdef MOZILLA_OFFICIAL
5665 // Check if we're running from a DMG or an app translocated location and
5666 // allow the user to install to the Applications directory.
5667 if (MacRunFromDmgUtils::MaybeInstallAndRelaunch()) {
5668 bool userAllowedQuit = true;
5669 appStartup->Quit(nsIAppStartup::eForceQuit, 0, &userAllowedQuit);
5670 }
5671# endif
5672#endif
5673
5674 nsCOMPtr<nsIObserverService> obsService =
5675 mozilla::services::GetObserverService();
5676 if (obsService)
5677 obsService->NotifyObservers(nullptr, "final-ui-startup", nullptr);
5678
5679 (void)appStartup->DoneStartingUp();
5680
5681 CrashReporter::RecordAnnotationBool(
5682 CrashReporter::Annotation::StartupCrash, false);
5683
5684 AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed);
5685 }
5686
5687 if (!AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
5688 rv = cmdLine->Run();
5689 NS_ENSURE_SUCCESS_LOG(rv, NS_ERROR_FAILURE)do { if ((__builtin_expect(!!(!(((bool)(__builtin_expect(!!(!
NS_FAILED_impl(rv)), 1))))), 0))) { NS_DebugBreak(NS_DEBUG_WARNING
, "NS_ENSURE_TRUE(" "NS_SUCCEEDED(rv)" ") failed", nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5689); gLogConsoleErrors = true; return NS_ERROR_FAILURE; }
} while (0)
;
5690 }
5691
5692 if (!AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
5693#if defined(MOZ_HAS_REMOTE1)
5694 // if we have X remote support, start listening for requests on the
5695 // proxy window.
5696 if (mRemoteService && !mDisableRemoteServer) {
5697 mRemoteService->StartupServer();
5698 mRemoteService->UnlockStartup();
5699 gRemoteService = nullptr;
5700 }
5701#endif /* MOZ_WIDGET_GTK */
5702
5703 mNativeApp->Enable();
5704 }
5705
5706#ifdef MOZ_INSTRUMENT_EVENT_LOOP1
5707 if (PR_GetEnv("MOZ_INSTRUMENT_EVENT_LOOP")) {
5708 bool logToConsole = true;
5709 mozilla::InitEventTracing(logToConsole);
5710 }
5711#endif /* MOZ_INSTRUMENT_EVENT_LOOP */
5712
5713 // Send Telemetry about Gecko version and buildid
5714 mozilla::glean::gecko::version.Set(nsDependentCString(gAppData->version));
5715 mozilla::glean::gecko::build_id.Set(nsDependentCString(gAppData->buildID));
5716
5717#if defined(MOZ_SANDBOX1) && defined(XP_LINUX1)
5718 // If we're on Linux, we now have information about the OS capabilities
5719 // available to us.
5720 SandboxInfo sandboxInfo = SandboxInfo::Get();
5721 Telemetry::Accumulate(Telemetry::SANDBOX_HAS_SECCOMP_BPF,
5722 sandboxInfo.Test(SandboxInfo::kHasSeccompBPF));
5723 Telemetry::Accumulate(Telemetry::SANDBOX_HAS_SECCOMP_TSYNC,
5724 sandboxInfo.Test(SandboxInfo::kHasSeccompTSync));
5725 Telemetry::Accumulate(
5726 Telemetry::SANDBOX_HAS_USER_NAMESPACES_PRIVILEGED,
5727 sandboxInfo.Test(SandboxInfo::kHasPrivilegedUserNamespaces));
5728 Telemetry::Accumulate(Telemetry::SANDBOX_HAS_USER_NAMESPACES,
5729 sandboxInfo.Test(SandboxInfo::kHasUserNamespaces));
5730 Telemetry::Accumulate(Telemetry::SANDBOX_CONTENT_ENABLED,
5731 sandboxInfo.Test(SandboxInfo::kEnabledForContent));
5732 Telemetry::Accumulate(Telemetry::SANDBOX_MEDIA_ENABLED,
5733 sandboxInfo.Test(SandboxInfo::kEnabledForMedia));
5734
5735 CrashReporter::RecordAnnotationU32(
5736 CrashReporter::Annotation::ContentSandboxCapabilities,
5737 sandboxInfo.AsInteger());
5738#endif /* MOZ_SANDBOX && XP_LINUX */
5739
5740#if defined(XP_WIN)
5741 LauncherResult<bool> isAdminWithoutUac = IsAdminWithoutUac();
5742 if (isAdminWithoutUac.isOk()) {
5743 Telemetry::ScalarSet(
5744 Telemetry::ScalarID::OS_ENVIRONMENT_IS_ADMIN_WITHOUT_UAC,
5745 isAdminWithoutUac.unwrap());
5746 }
5747#endif /* XP_WIN */
5748
5749#if defined(MOZ_SANDBOX1)
5750 AddSandboxAnnotations();
5751#endif /* MOZ_SANDBOX */
5752
5753 mProfileSvc->CompleteStartup();
5754 }
5755
5756#ifdef MOZ_BACKGROUNDTASKS1
5757 if (BackgroundTasks::IsBackgroundTaskMode()) {
5758 // In background task mode, we don't fire various delayed initialization
5759 // notifications, which in the regular browser is how startup crash tracking
5760 // is marked as finished. Here, getting this far means we don't have a
5761 // startup crash.
5762 rv = appStartup->TrackStartupCrashEnd();
5763 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5763); return rv; } } while (false)
;
5764
5765 // We never open a window, but don't want to exit immediately.
5766 rv = appStartup->EnterLastWindowClosingSurvivalArea();
5767 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5767); return rv; } } while (false)
;
5768
5769 // Avoid some small differences in initialization order across platforms.
5770 nsCOMPtr<nsIPowerManagerService> powerManagerService =
5771 do_GetService(POWERMANAGERSERVICE_CONTRACTID"@mozilla.org/power/powermanagerservice;1");
5772 nsCOMPtr<nsIStringBundleService> stringBundleService =
5773 do_GetService(NS_STRINGBUNDLE_CONTRACTID"@mozilla.org/intl/stringbundle;1");
5774
5775 rv = BackgroundTasks::RunBackgroundTask(cmdLine);
5776 NS_ENSURE_SUCCESS(rv, rv)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "rv", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5776); return rv; } } while (false)
;
5777 }
5778#endif
5779
5780 {
5781 rv = appStartup->Run();
5782 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5783 NS_ERROR("failed to run appstartup")do { NS_DebugBreak(NS_DEBUG_ASSERTION, "failed to run appstartup"
, "Error", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5783); MOZ_PretendNoReturn(); } while (0)
;
5784 gLogConsoleErrors = true;
5785 }
5786 }
5787
5788 return rv;
5789}
5790
5791#if defined(MOZ_WIDGET_ANDROID)
5792static already_AddRefed<nsIFile> GreOmniPath(int argc, char** argv) {
5793 nsresult rv;
5794
5795 const char* path = nullptr;
5796 ArgResult ar = CheckArg(argc, argv, "greomni", &path, CheckArgFlag::None);
5797 if (ar == ARG_BAD) {
5798 PR_fprintf(PR_STDERRPR_GetSpecialFD(PR_StandardError),
5799 "Error: argument --greomni requires a path argument\n");
5800 return nullptr;
5801 }
5802
5803 if (!path) return nullptr;
5804
5805 nsCOMPtr<nsIFile> greOmni;
5806 rv = XRE_GetFileFromPath(path, getter_AddRefs(greOmni));
5807 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5808 PR_fprintf(PR_STDERRPR_GetSpecialFD(PR_StandardError), "Error: argument --greomni requires a valid path\n");
5809 return nullptr;
5810 }
5811
5812 return greOmni.forget();
5813}
5814#endif
5815
5816/*
5817 * XRE_main - A class based main entry point used by most platforms.
5818 * Note that on OSX, aAppData->xreDirectory will point to
5819 * .app/Contents/Resources.
5820 */
5821int XREMain::XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig) {
5822 gArgc = argc;
5823 gArgv = argv;
5824
5825 ScopedLogging log;
5826
5827 mozilla::LogModule::Init(gArgc, gArgv);
5828
5829#ifndef XP_LINUX1
5830 NS_SetCurrentThreadName("MainThread");
5831#endif
5832
5833 AUTO_BASE_PROFILER_LABEL("XREMain::XRE_main (around Gecko Profiler)", OTHER)::mozilla::baseprofiler::AutoProfilerLabel raiiObject5833( "XREMain::XRE_main (around Gecko Profiler)"
, nullptr, ::mozilla::baseprofiler::ProfilingCategoryPair::OTHER
)
;
5834 AUTO_PROFILER_INITmozilla::AutoProfilerInit raiiObject5834;
5835 AUTO_PROFILER_LABEL("XREMain::XRE_main", OTHER)mozilla::AutoProfilerLabel raiiObject5835( "XREMain::XRE_main"
, nullptr, JS::ProfilingCategoryPair::OTHER)
;
5836
5837#ifdef XP_MACOSX
5838 // We call this early because it will kick off a background-thread task
5839 // to register the fonts, and we'd like it to have a chance to complete
5840 // before gfxPlatform initialization actually requires it.
5841 gfxPlatformMac::RegisterSupplementalFonts();
5842#endif
5843
5844#ifdef MOZ_CODE_COVERAGE
5845 CodeCoverageHandler::Init();
5846#endif
5847
5848 nsresult rv = NS_OK;
5849
5850 if (aConfig.appData) {
5851 mAppData = MakeUnique<XREAppData>(*aConfig.appData);
5852 } else {
5853 MOZ_RELEASE_ASSERT(aConfig.appDataPath)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aConfig.appDataPath)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aConfig.appDataPath))), 0)))
{ do { } while (false); MOZ_ReportAssertionFailure("aConfig.appDataPath"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5853); AnnotateMozCrashReason("MOZ_RELEASE_ASSERT" "(" "aConfig.appDataPath"
")"); do { *((volatile int*)__null) = 5853; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
5854 nsCOMPtr<nsIFile> appini;
5855 rv = XRE_GetFileFromPath(aConfig.appDataPath, getter_AddRefs(appini));
5856 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5857 Output(true, "Error: unrecognized path: %s\n", aConfig.appDataPath);
5858 return 1;
5859 }
5860
5861 mAppData = MakeUnique<XREAppData>();
5862 rv = XRE_ParseAppData(appini, *mAppData);
5863 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5864 Output(true, "Couldn't read application.ini");
5865 return 1;
5866 }
5867
5868 appini->GetParent(getter_AddRefs(mAppData->directory));
5869 }
5870
5871 const char* appRemotingName = getenv("MOZ_APP_REMOTINGNAME");
5872 if (appRemotingName) {
5873 mAppData->remotingName = appRemotingName;
5874 } else if (!mAppData->remotingName) {
5875 mAppData->remotingName = mAppData->name;
5876 }
5877 // used throughout this file
5878 gAppData = mAppData.get();
5879
5880 nsCOMPtr<nsIFile> binFile;
5881 rv = XRE_GetBinaryPath(getter_AddRefs(binFile));
5882 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5882); return 1; } } while (false)
;
5883
5884 rv = binFile->GetPath(gAbsoluteArgv0Path);
5885 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5885); return 1; } } while (false)
;
5886
5887 if (!mAppData->xreDirectory) {
5888 nsCOMPtr<nsIFile> greDir;
5889
5890#if defined(MOZ_WIDGET_ANDROID)
5891 greDir = GreOmniPath(argc, argv);
5892 if (!greDir) {
5893 return 2;
5894 }
5895#else
5896 rv = binFile->GetParent(getter_AddRefs(greDir));
5897 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return 2;
5898#endif
5899
5900#ifdef XP_MACOSX
5901 nsCOMPtr<nsIFile> parent;
5902 greDir->GetParent(getter_AddRefs(parent));
5903 greDir = parent.forget();
5904 greDir->AppendNative("Resources"_ns);
5905#endif
5906
5907 mAppData->xreDirectory = greDir;
5908 }
5909
5910#if defined(MOZ_WIDGET_ANDROID)
5911 nsCOMPtr<nsIFile> dataDir;
5912 rv = binFile->GetParent(getter_AddRefs(dataDir));
5913 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return 2;
5914
5915 mAppData->directory = dataDir;
5916#else
5917 if (aConfig.appData && aConfig.appDataPath) {
5918 mAppData->xreDirectory->Clone(getter_AddRefs(mAppData->directory));
5919 mAppData->directory->AppendNative(nsDependentCString(aConfig.appDataPath));
5920 }
5921#endif
5922
5923 if (!mAppData->directory) {
5924 mAppData->directory = mAppData->xreDirectory;
5925 }
5926
5927#if defined(XP_WIN)
5928# if defined(MOZ_SANDBOX1)
5929 mAppData->sandboxBrokerServices = aConfig.sandboxBrokerServices;
5930# endif // defined(MOZ_SANDBOX)
5931
5932 {
5933 DebugOnly<bool> result = WindowsBCryptInitialization();
5934 MOZ_ASSERT(result)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(result)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(result))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("result", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5934); AnnotateMozCrashReason("MOZ_ASSERT" "(" "result" ")"
); do { *((volatile int*)__null) = 5934; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
5935 }
5936
5937# if defined(_M_IX86) || defined(_M_X64)
5938 {
5939 DebugOnly<bool> result = WindowsMsctfInitialization();
5940 MOZ_ASSERT(result)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(result)>::isValid, "invalid assertion condition")
; if ((__builtin_expect(!!(!(!!(result))), 0))) { do { } while
(false); MOZ_ReportAssertionFailure("result", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 5940); AnnotateMozCrashReason("MOZ_ASSERT" "(" "result" ")"
); do { *((volatile int*)__null) = 5940; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
5941 }
5942# endif // _M_IX86 || _M_X64
5943
5944#endif // defined(XP_WIN)
5945
5946 // Once we unset the exception handler, we lose the ability to properly
5947 // detect hangs -- they show up as crashes. We do this as late as possible.
5948 // In particular, after ProcessRuntime is destroyed on Windows.
5949 auto unsetExceptionHandler = MakeScopeExit([&] {
5950 if (mAppData->flags & NS_XRE_ENABLE_CRASH_REPORTER(1 << 3))
5951 return CrashReporter::UnsetExceptionHandler();
5952 return NS_OK;
5953 });
5954
5955 mozilla::AutoIOInterposer ioInterposerGuard;
5956 ioInterposerGuard.Init();
5957
5958#if defined(XP_WIN)
5959 // We should have already done this when we created the skeleton UI. However,
5960 // there is code in here which needs xul in order to work, like EnsureMTA. It
5961 // should be setup that running it again is safe.
5962 mozilla::mscom::ProcessRuntime msCOMRuntime;
5963#endif
5964
5965 // init
5966 bool exit = false;
5967 int result = XRE_mainInit(&exit);
5968 if (result != 0 || exit) return result;
5969
5970 // If we exit gracefully, remove the startup crash canary file.
5971 auto cleanup = MakeScopeExit([&]() -> nsresult {
5972 if (mProfLD) {
5973 nsCOMPtr<nsIFile> crashFile;
5974 MOZ_TRY_VAR(crashFile, GetIncompleteStartupFile(mProfLD))do { auto mozTryVarTempResult_ = (GetIncompleteStartupFile(mProfLD
)); if ((__builtin_expect(!!(mozTryVarTempResult_.isErr()), 0
))) { return mozTryVarTempResult_.propagateErr(); } (crashFile
) = mozTryVarTempResult_.unwrap(); } while (0)
;
5975 crashFile->Remove(false);
5976 }
5977 return NS_OK;
5978 });
5979
5980 // startup
5981 result = XRE_mainStartup(&exit);
5982 if (result != 0 || exit) return result;
5983
5984 // Start the real application. We use |aInitJSContext = false| because
5985 // XRE_mainRun wants to initialize the JSContext after reading user prefs.
5986
5987 mScopedXPCOM = MakeUnique<ScopedXPCOMStartup>();
5988
5989 rv = mScopedXPCOM->Initialize(/* aInitJSContext = */ false);
5990 if (rv == NS_ERROR_OMNIJAR_CORRUPT) {
5991 if (XRE_IsParentProcess()
5992#ifdef MOZ_BACKGROUNDTASKS1
5993 && !mozilla::BackgroundTasks::IsBackgroundTaskMode()
5994#endif
5995 ) {
5996 Output(
5997 true,
5998 "The installation seems to be corrupt.\nPlease check your hardware "
5999 "and disk setup\nand/or re-install.\n");
6000 }
6001 MOZ_CRASH("NS_ERROR_OMNIJAR_CORRUPT")do { do { } while (false); MOZ_ReportCrash("" "NS_ERROR_OMNIJAR_CORRUPT"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6001); AnnotateMozCrashReason("MOZ_CRASH(" "NS_ERROR_OMNIJAR_CORRUPT"
")"); do { *((volatile int*)__null) = 6001; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
6002 }
6003 NS_ENSURE_SUCCESS(rv, 1)do { nsresult __rv = rv; if (((bool)(__builtin_expect(!!(NS_FAILED_impl
(__rv)), 0)))) { const char* name = mozilla::GetStaticErrorName
(__rv); mozilla::SmprintfPointer msg = mozilla::Smprintf( "NS_ENSURE_SUCCESS(%s, %s) failed with "
"result 0x%" "X" "%s%s%s", "rv", "1", static_cast<uint32_t
>(__rv), name ? " (" : "", name ? name : "", name ? ")" : ""
); NS_DebugBreak(NS_DEBUG_WARNING, msg.get(), nullptr, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6003); return 1; } } while (false)
;
6004
6005 // run!
6006 rv = XRE_mainRun();
6007 if (rv == NS_ERROR_OMNIJAR_OR_DIR_MISSING) {
6008 if (XRE_IsParentProcess()
6009#ifdef MOZ_BACKGROUNDTASKS1
6010 && !mozilla::BackgroundTasks::IsBackgroundTaskMode()
6011#endif
6012 ) {
6013 Output(true,
6014 "The installation seems to be incomplete.\nPlease check your "
6015 "hardware and disk setup\nand/or re-install.\n");
6016 }
6017 if (mozilla::IsPackagedBuild()) {
6018 // IsPackagedBuild just looks for omni.ja on disk. If we were able to
6019 // find and open it before but not to load the expected JS module from
6020 // it now, signal corruption.
6021 MOZ_CRASH("NS_ERROR_OMNIJAR_CORRUPT")do { do { } while (false); MOZ_ReportCrash("" "NS_ERROR_OMNIJAR_CORRUPT"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6021); AnnotateMozCrashReason("MOZ_CRASH(" "NS_ERROR_OMNIJAR_CORRUPT"
")"); do { *((volatile int*)__null) = 6021; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
6022 }
6023 MOZ_CRASH("NS_ERROR_OMNIJAR_OR_DIR_MISSING")do { do { } while (false); MOZ_ReportCrash("" "NS_ERROR_OMNIJAR_OR_DIR_MISSING"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6023); AnnotateMozCrashReason("MOZ_CRASH(" "NS_ERROR_OMNIJAR_OR_DIR_MISSING"
")"); do { *((volatile int*)__null) = 6023; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
6024 }
6025
6026#ifdef MOZ_X111
6027 XRE_CleanupX11ErrorHandler();
6028#endif
6029
6030#ifdef MOZ_INSTRUMENT_EVENT_LOOP1
6031 mozilla::ShutdownEventTracing();
6032#endif
6033
6034 gAbsoluteArgv0Path.Truncate();
6035
6036#if defined(MOZ_HAS_REMOTE1)
6037 // Shut down the remote service. We must do this before calling LaunchChild
6038 // if we're restarting because otherwise the new instance will attempt to
6039 // remote to this instance.
6040 if (mRemoteService && !mDisableRemoteServer) {
6041 mRemoteService->ShutdownServer();
6042 }
6043#endif /* MOZ_WIDGET_GTK */
6044
6045 mScopedXPCOM = nullptr;
6046
6047 // unlock the profile after ScopedXPCOMStartup object (xpcom)
6048 // has gone out of scope. see bug #386739 for more details
6049 mProfileLock->Unlock();
6050 gProfileLock = nullptr;
6051
6052 gLastAppVersion.Truncate();
6053 gLastAppBuildID.Truncate();
6054
6055#ifdef MOZ_WIDGET_GTK1
6056 // gdk_display_close also calls gdk_display_manager_set_default_display
6057 // appropriately when necessary.
6058 if (!gfxPlatform::IsHeadless()) {
6059# ifdef MOZ_WAYLAND1
6060 WaylandDisplayRelease();
6061 gWaylandProxy = nullptr;
6062# endif
6063 }
6064#endif
6065
6066 mozilla::AppShutdown::MaybeDoRestart();
6067
6068 XRE_DeinitCommandLine();
6069
6070 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
6071 return 1;
6072 }
6073 return mozilla::AppShutdown::GetExitCode();
6074}
6075
6076void XRE_StopLateWriteChecks(void) { mozilla::StopLateWriteChecks(); }
6077
6078int XRE_main(int argc, char* argv[], const BootstrapConfig& aConfig) {
6079 XREMain main;
6080
6081 int result = main.XRE_main(argc, argv, aConfig);
6082 mozilla::RecordShutdownEndTimeStamp();
6083#ifdef MOZ_BACKGROUNDTASKS1
6084 // This is well after the profile has been unlocked, so it's okay if this does
6085 // delete this background task's temporary profile.
6086 mozilla::BackgroundTasks::Shutdown();
6087#endif
6088 return result;
6089}
6090
6091nsresult XRE_InitCommandLine(int aArgc, char* aArgv[]) {
6092 nsresult rv = NS_OK;
6093
6094#if defined(XP_WIN)
6095 CommandLine::Init(aArgc, aArgv);
6096#else
6097
6098 // these leak on error, but that's OK: we'll just exit()
6099 char** canonArgs = new char*[aArgc];
6100
6101 // get the canonical version of the binary's path
6102 nsCOMPtr<nsIFile> binFile;
6103 rv = XRE_GetBinaryPath(getter_AddRefs(binFile));
6104 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return NS_ERROR_FAILURE;
6105
6106 nsAutoCString canonBinPath;
6107 rv = binFile->GetNativePath(canonBinPath);
6108 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) return NS_ERROR_FAILURE;
6109
6110 canonArgs[0] = strdup(canonBinPath.get());
6111
6112 for (int i = 1; i < aArgc; ++i) {
6113 if (aArgv[i]) {
6114 canonArgs[i] = strdup(aArgv[i]);
6115 }
6116 }
6117
6118 NS_ASSERTION(!CommandLine::IsInitialized(), "Bad news!")do { if (!(!CommandLine::IsInitialized())) { NS_DebugBreak(NS_DEBUG_ASSERTION
, "Bad news!", "!CommandLine::IsInitialized()", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6118); MOZ_PretendNoReturn(); } } while (0)
;
6119 CommandLine::Init(aArgc, canonArgs);
6120
6121 for (int i = 0; i < aArgc; ++i) free(canonArgs[i]);
6122 delete[] canonArgs;
6123#endif
6124
6125#if defined(MOZ_WIDGET_ANDROID)
6126 // gAppData is non-null iff this is the parent process. Otherwise,
6127 // the `-greomni`/`-appomni` flags are cross-platform and handled in
6128 // ContentProcess::Init.
6129 if (gAppData) {
6130 nsCOMPtr<nsIFile> greOmni = gAppData->xreDirectory;
6131 if (!greOmni) {
6132 return NS_ERROR_FAILURE;
6133 }
6134 mozilla::Omnijar::Init(greOmni, greOmni);
6135 }
6136#endif
6137
6138 return rv;
6139}
6140
6141nsresult XRE_DeinitCommandLine() {
6142 nsresult rv = NS_OK;
6143
6144 CommandLine::Terminate();
6145
6146 return rv;
6147}
6148
6149GeckoProcessType XRE_GetProcessType() { return GetGeckoProcessType(); }
6150
6151const char* XRE_GetProcessTypeString() {
6152 return XRE_GeckoProcessTypeToString(XRE_GetProcessType());
6153}
6154
6155bool XRE_IsE10sParentProcess() {
6156#ifdef MOZ_WIDGET_ANDROID
6157 return XRE_IsParentProcess() && BrowserTabsRemoteAutostart() &&
6158 mozilla::jni::IsAvailable();
6159#else
6160 return XRE_IsParentProcess() && BrowserTabsRemoteAutostart();
6161#endif
6162}
6163
6164#define GECKO_PROCESS_TYPE(enum_value, enum_name, string_name, proc_typename, \
6165 process_bin_type, procinfo_typename, \
6166 webidl_typename, allcaps_name) \
6167 bool XRE_Is##proc_typename##Process() { \
6168 return XRE_GetProcessType() == GeckoProcessType_##enum_name; \
6169 }
6170#include "mozilla/GeckoProcessTypes.h"
6171#undef GECKO_PROCESS_TYPE
6172
6173bool XRE_UseNativeEventProcessing() {
6174 switch (XRE_GetProcessType()) {
6175#if defined(XP_MACOSX) || defined(XP_WIN)
6176 case GeckoProcessType_RDD:
6177 case GeckoProcessType_Socket:
6178 return false;
6179 case GeckoProcessType_Utility: {
6180# if defined(XP_WIN)
6181 auto upc = mozilla::ipc::UtilityProcessChild::Get();
6182 MOZ_ASSERT(upc)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(upc)>::isValid, "invalid assertion condition"); if
((__builtin_expect(!!(!(!!(upc))), 0))) { do { } while (false
); MOZ_ReportAssertionFailure("upc", "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6182); AnnotateMozCrashReason("MOZ_ASSERT" "(" "upc" ")"); do
{ *((volatile int*)__null) = 6182; __attribute__((nomerge)) ::
abort(); } while (false); } } while (false)
;
6183
6184 using SboxKind = mozilla::ipc::SandboxingKind;
6185 // These processes are used as external hosts for accessing Windows
6186 // APIs which (may) require a Windows native event loop.
6187 return upc->mSandbox == SboxKind::WINDOWS_UTILS ||
6188 upc->mSandbox == SboxKind::WINDOWS_FILE_DIALOG;
6189# else
6190 return false;
6191# endif // defined(XP_WIN)
6192 }
6193#endif // defined(XP_MACOSX) || defined(XP_WIN)
6194 case GeckoProcessType_GMPlugin:
6195 return mozilla::gmp::GMPProcessChild::UseNativeEventProcessing();
6196 case GeckoProcessType_Content:
6197 return StaticPrefs::dom_ipc_useNativeEventProcessing_content();
6198 default:
6199 return true;
6200 }
6201}
6202
6203namespace mozilla {
6204
6205uint32_t GetMaxWebProcessCount() {
6206 // multiOptOut is in int to allow us to run multiple experiments without
6207 // introducing multiple prefs a la the autostart.N prefs.
6208 if (Preferences::GetInt("dom.ipc.multiOptOut", 0) >=
6209 nsIXULRuntime::E10S_MULTI_EXPERIMENT) {
6210 return 1;
6211 }
6212
6213 const char* optInPref = "dom.ipc.processCount";
6214 uint32_t optInPrefValue = Preferences::GetInt(optInPref, 1);
6215 return std::max(1u, optInPrefValue);
6216}
6217
6218const char* PlatformBuildID() { return gToolkitBuildID; }
6219
6220} // namespace mozilla
6221
6222void SetupErrorHandling(const char* progname) {
6223#ifdef XP_WIN
6224 /* On Windows XPSP3 and Windows Vista if DEP is configured off-by-default
6225 we still want DEP protection: enable it explicitly and programmatically.
6226
6227 This function is not available on WinXPSP2 so we dynamically load it.
6228 */
6229
6230 HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll");
6231 SetProcessDEPPolicyFunc _SetProcessDEPPolicy =
6232 (SetProcessDEPPolicyFunc)GetProcAddress(kernel32, "SetProcessDEPPolicy");
6233 if (_SetProcessDEPPolicy) _SetProcessDEPPolicy(PROCESS_DEP_ENABLE);
6234#endif
6235
6236#ifdef XP_WIN
6237 // Suppress the "DLL Foo could not be found" dialog, such that if dependent
6238 // libraries (such as GDI+) are not preset, we gracefully fail to load those
6239 // XPCOM components, instead of being ungraceful.
6240 UINT realMode = SetErrorMode(0);
6241 realMode |= SEM_FAILCRITICALERRORS;
6242 // If XRE_NO_WINDOWS_CRASH_DIALOG is set, suppress displaying the "This
6243 // application has crashed" dialog box. This is mainly useful for
6244 // automated testing environments, e.g. tinderbox, where there's no need
6245 // for a dozen of the dialog boxes to litter the console
6246 if (getenv("XRE_NO_WINDOWS_CRASH_DIALOG"))
6247 realMode |= SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
6248
6249 SetErrorMode(realMode);
6250
6251#endif
6252
6253 InstallSignalHandlers(progname);
6254
6255 // Unbuffer stdout, needed for tinderbox tests.
6256 setbuf(stdoutstdout, 0);
6257}
6258
6259static bool gRunSelfAsContentProc = false;
6260
6261void XRE_EnableSameExecutableForContentProc() {
6262 if (!PR_GetEnv("MOZ_SEPARATE_CHILD_PROCESS")) {
6263 gRunSelfAsContentProc = true;
6264 }
6265}
6266
6267mozilla::BinPathType XRE_GetChildProcBinPathType(
6268 GeckoProcessType aProcessType) {
6269 MOZ_ASSERT(aProcessType != GeckoProcessType_Default)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aProcessType != GeckoProcessType_Default)>::isValid
, "invalid assertion condition"); if ((__builtin_expect(!!(!(
!!(aProcessType != GeckoProcessType_Default))), 0))) { do { }
while (false); MOZ_ReportAssertionFailure("aProcessType != GeckoProcessType_Default"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6269); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aProcessType != GeckoProcessType_Default"
")"); do { *((volatile int*)__null) = 6269; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
6270
6271 if (!gRunSelfAsContentProc) {
6272 return BinPathType::PluginContainer;
6273 }
6274
6275#ifdef XP_WIN
6276 // On Windows, plugin-container may or may not be used depending on
6277 // the process type (e.g., actual plugins vs. content processes)
6278 switch (aProcessType) {
6279# define GECKO_PROCESS_TYPE(enum_value, enum_name, string_name, \
6280 proc_typename, process_bin_type, \
6281 procinfo_typename, webidl_typename, allcaps_name) \
6282 case GeckoProcessType_##enum_name: \
6283 return BinPathType::process_bin_type;
6284# include "mozilla/GeckoProcessTypes.h"
6285# undef GECKO_PROCESS_TYPE
6286 default:
6287 return BinPathType::PluginContainer;
6288 }
6289#else
6290 // On (non-macOS) Unix, plugin-container isn't used (except in cases
6291 // like xpcshell that are handled by the gRunSelfAsContentProc check
6292 // above). It isn't necessary the way it is on other platforms, and
6293 // it interferes with using the fork server.
6294 return BinPathType::Self;
6295#endif
6296}
6297
6298// From mozglue/static/rust/lib.rs
6299extern "C" void install_rust_hooks();
6300
6301struct InstallRustHooks {
6302 InstallRustHooks() { install_rust_hooks(); }
6303};
6304
6305InstallRustHooks sInstallRustHooks;
6306
6307#ifdef MOZ_ASAN_REPORTER
6308void setASanReporterPath(nsIFile* aDir) {
6309 nsCOMPtr<nsIFile> dir;
6310 aDir->Clone(getter_AddRefs(dir));
6311
6312 dir->Append(u"asan"_ns);
6313 nsresult rv = dir->Create(nsIFile::DIRECTORY_TYPE, 0700);
6314 if (NS_WARN_IF(NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS)NS_warn_if_impl(((bool)(__builtin_expect(!!(NS_FAILED_impl(rv
)), 0))) && rv != NS_ERROR_FILE_ALREADY_EXISTS, "NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS"
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6314)
) {
6315 MOZ_CRASH("[ASan Reporter] Unable to create crash directory.")do { do { } while (false); MOZ_ReportCrash("" "[ASan Reporter] Unable to create crash directory."
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6315); AnnotateMozCrashReason("MOZ_CRASH(" "[ASan Reporter] Unable to create crash directory."
")"); do { *((volatile int*)__null) = 6315; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
6316 }
6317
6318 dir->Append(u"ff_asan_log"_ns);
6319
6320# ifdef XP_WIN
6321 nsAutoString nspathW;
6322 rv = dir->GetPath(nspathW);
6323 NS_ConvertUTF16toUTF8 nspath(nspathW);
6324# else
6325 nsAutoCString nspath;
6326 rv = dir->GetNativePath(nspath);
6327# endif
6328 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
6329 MOZ_CRASH("[ASan Reporter] Unable to get native path for crash directory.")do { do { } while (false); MOZ_ReportCrash("" "[ASan Reporter] Unable to get native path for crash directory."
, "/var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre/nsAppRunner.cpp"
, 6329); AnnotateMozCrashReason("MOZ_CRASH(" "[ASan Reporter] Unable to get native path for crash directory."
")"); do { *((volatile int*)__null) = 6329; __attribute__((nomerge
)) ::abort(); } while (false); } while (false)
;
6330 }
6331
6332 __sanitizer_set_report_path(nspath.get());
6333}
6334#endif

/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/CmdLineAndEnvUtils.h

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 https://mozilla.org/MPL/2.0/. */
6
7#ifndef mozilla_CmdLineAndEnvUtils_h
8#define mozilla_CmdLineAndEnvUtils_h
9
10// NB: This code may be used outside of xul and thus must not depend on XPCOM
11
12#if defined(MOZILLA_INTERNAL_API1)
13# include "prenv.h"
14# include "prprf.h"
15# include <string.h>
16#endif
17
18#if defined(XP_WIN)
19# include "mozilla/UniquePtr.h"
20# include "mozilla/Vector.h"
21# include "mozilla/WinHeaderOnlyUtils.h"
22
23# include <wchar.h>
24# include <windows.h>
25#endif // defined(XP_WIN)
26
27#include "mozilla/Maybe.h"
28#include "mozilla/MemoryChecking.h"
29#include "mozilla/TypedEnumBits.h"
30
31#include <ctype.h>
32#include <stdint.h>
33#include <stdlib.h>
34
35#ifndef NS_NO_XPCOM
36# include "nsIFile.h"
37# include "mozilla/AlreadyAddRefed.h"
38#endif
39
40// Undo X11/X.h's definition of None
41#undef None
42
43namespace mozilla {
44
45enum ArgResult {
46 ARG_NONE = 0,
47 ARG_FOUND = 1,
48 ARG_BAD = 2 // you wanted a param, but there isn't one
49};
50
51template <typename CharT>
52inline void RemoveArg(int& argc, CharT** argv) {
53 do {
54 *argv = *(argv + 1);
55 ++argv;
56 } while (*argv);
57
58 --argc;
59}
60
61namespace internal {
62
63#if 'a' == '\x61'
64// Valid option characters must have the same representation in every locale
65// (which is true for most of ASCII, barring \x5C and \x7E).
66static inline constexpr bool isValidOptionCharacter(char c) {
67 // We specifically avoid the use of `islower` here; it's locale-dependent, and
68 // may return true for non-ASCII values in some locales.
69 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || c == '-';
70};
71
72// Convert uppercase to lowercase, locale-insensitively.
73static inline constexpr char toLowercase(char c) {
74 // We specifically avoid the use of `tolower` here; it's locale-dependent, and
75 // may output ASCII values for non-ASCII input (or vice versa) in some
76 // locales.
77 return ('A' <= c && c <= 'Z') ? char(c | ' ') : c;
78};
79
80// Convert a CharT to a char, ensuring that no CharT is mapped to any valid
81// option character except the unique CharT naturally corresponding thereto.
82template <typename CharT>
83static inline constexpr char toNarrow(CharT c) {
84 // confirmed to compile down to nothing when `CharT` is `char`
85 return (c & static_cast<CharT>(0xff)) == c ? c : 0xff;
86};
87#else
88// The target system's character set isn't even ASCII-compatible. If you're
89// porting Gecko to such a platform, you'll have to implement these yourself.
90# error Character conversion functions not implemented for this platform.
91#endif
92
93// Case-insensitively compare a string taken from the command-line (`mixedstr`)
94// to the text of some known command-line option (`lowerstr`).
95template <typename CharT>
96static inline bool strimatch(const char* lowerstr, const CharT* mixedstr) {
97 while (*lowerstr) {
98 if (!*mixedstr) return false; // mixedstr is shorter
99
100 // Non-ASCII strings may compare incorrectly depending on the user's locale.
101 // Some ASCII-safe characters are also dispermitted for semantic reasons
102 // and simplicity.
103 if (!isValidOptionCharacter(*lowerstr)) return false;
104
105 if (toLowercase(toNarrow(*mixedstr)) != *lowerstr) {
106 return false; // no match
107 }
108
109 ++lowerstr;
110 ++mixedstr;
111 }
112
113 if (*mixedstr) return false; // lowerstr is shorter
114
115 return true;
116}
117
118// Given a command-line argument, return Nothing if it isn't structurally a
119// command-line option, and Some(<the option text>) if it is.
120template <typename CharT>
121mozilla::Maybe<const CharT*> ReadAsOption(const CharT* str) {
122 if (!str) {
123 return Nothing();
124 }
125 if (*str == '-') {
126 str++;
127 if (*str == '-') {
128 str++;
129 }
130 return Some(str);
131 }
132#ifdef XP_WIN
133 if (*str == '/') {
134 return Some(str + 1);
135 }
136#endif
137 return Nothing();
138}
139
140} // namespace internal
141
142using internal::strimatch;
143
144const wchar_t kCommandLineDelimiter[] = L" \t";
145
146enum class CheckArgFlag : uint32_t {
147 None = 0,
148 // (1 << 0) Used to be CheckOSInt
149 RemoveArg = (1 << 1) // Remove the argument from the argv array.
150};
151
152MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(CheckArgFlag)inline constexpr mozilla::CastableTypedEnumResult<CheckArgFlag
> operator |( CheckArgFlag a, CheckArgFlag b) { typedef mozilla
::CastableTypedEnumResult<CheckArgFlag> Result; typedef
mozilla::detail::UnsignedIntegerTypeForEnum<CheckArgFlag>
::Type U; return Result(CheckArgFlag(U(a) | U(b))); } inline CheckArgFlag
& operator |=(CheckArgFlag & a, CheckArgFlag b) { return
a = a | b; } inline constexpr mozilla::CastableTypedEnumResult
<CheckArgFlag> operator &( CheckArgFlag a, CheckArgFlag
b) { typedef mozilla::CastableTypedEnumResult<CheckArgFlag
> Result; typedef mozilla::detail::UnsignedIntegerTypeForEnum
<CheckArgFlag>::Type U; return Result(CheckArgFlag(U(a)
& U(b))); } inline CheckArgFlag& operator &=(CheckArgFlag
& a, CheckArgFlag b) { return a = a & b; } inline constexpr
mozilla::CastableTypedEnumResult<CheckArgFlag> operator
^( CheckArgFlag a, CheckArgFlag b) { typedef mozilla::CastableTypedEnumResult
<CheckArgFlag> Result; typedef mozilla::detail::UnsignedIntegerTypeForEnum
<CheckArgFlag>::Type U; return Result(CheckArgFlag(U(a)
^ U(b))); } inline CheckArgFlag& operator ^=(CheckArgFlag
& a, CheckArgFlag b) { return a = a ^ b; } inline constexpr
mozilla::CastableTypedEnumResult<CheckArgFlag> operator
~(CheckArgFlag a) { typedef mozilla::CastableTypedEnumResult<
CheckArgFlag> Result; typedef mozilla::detail::UnsignedIntegerTypeForEnum
<CheckArgFlag>::Type U; return Result(CheckArgFlag(~(U(
a)))); }
153
154/**
155 * Check for a commandline flag. If the flag takes a parameter, the
156 * parameter is returned in aParam. Flags may be in the form -arg or
157 * --arg (or /arg on win32).
158 *
159 * @param aArgc The argc value.
160 * @param aArgv The original argv.
161 * @param aArg the parameter to check. Must be lowercase.
162 * @param aParam if non-null, the -arg <data> will be stored in this pointer.
163 * This is *not* allocated, but rather a pointer to the argv data.
164 * @param aFlags Flags @see CheckArgFlag
165 */
166template <typename CharT>
167inline ArgResult CheckArg(int& aArgc, CharT** aArgv, const char* aArg,
168 const CharT** aParam = nullptr,
169 CheckArgFlag aFlags = CheckArgFlag::RemoveArg) {
170 using internal::ReadAsOption;
171 MOZ_ASSERT(aArgv && aArg)do { static_assert( mozilla::detail::AssertionConditionType<
decltype(aArgv && aArg)>::isValid, "invalid assertion condition"
); if ((__builtin_expect(!!(!(!!(aArgv && aArg))), 0)
)) { do { } while (false); MOZ_ReportAssertionFailure("aArgv && aArg"
, "/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/mozilla/CmdLineAndEnvUtils.h"
, 171); AnnotateMozCrashReason("MOZ_ASSERT" "(" "aArgv && aArg"
")"); do { *((volatile int*)__null) = 171; __attribute__((nomerge
)) ::abort(); } while (false); } } while (false)
;
172
173 CharT** curarg = aArgv + 1; // skip argv[0]
174 ArgResult ar = ARG_NONE;
175
176 while (*curarg) {
177 if (const auto arg = ReadAsOption(*curarg)) {
178 if (strimatch(aArg, arg.value())) {
179 if (aFlags & CheckArgFlag::RemoveArg) {
180 RemoveArg(aArgc, curarg);
181 } else {
182 ++curarg;
183 }
184
185 if (!aParam) {
186 ar = ARG_FOUND;
187 break;
188 }
189
190 if (*curarg) {
191 if (ReadAsOption(*curarg)) {
192 return ARG_BAD;
193 }
194
195 *aParam = *curarg;
196
197 if (aFlags & CheckArgFlag::RemoveArg) {
198 RemoveArg(aArgc, curarg);
199 }
200
201 ar = ARG_FOUND;
202 break;
203 }
204
205 return ARG_BAD;
206 }
207 }
208
209 ++curarg;
210 }
211
212 return ar;
213}
214
215template <typename CharT>
216inline ArgResult CheckArg(int& aArgc, CharT** aArgv, const char* aArg,
217 std::nullptr_t,
218 CheckArgFlag aFlags = CheckArgFlag::RemoveArg) {
219 return CheckArg<CharT>(aArgc, aArgv, aArg,
220 static_cast<const CharT**>(nullptr), aFlags);
221}
222
223namespace internal {
224// template <typename T>
225// constexpr bool IsStringRange =
226// std::convertible_to<std::ranges::range_value_t<T>, const char *>;
227
228template <typename CharT, typename ListT>
229// requires IsStringRange<ListT>
230static bool MatchesAnyOf(CharT const* unknown, ListT const& known) {
231 for (const char* k : known) {
232 if (strimatch(k, unknown)) {
233 return true;
234 }
235 }
236 return false;
237}
238
239template <typename CharT, typename ReqContainerT, typename OptContainerT>
240// requires IsStringRange<ReqContainerT> && IsStringRange<OptContainerT>
241inline bool EnsureCommandlineSafeImpl(int aArgc, CharT** aArgv,
242 ReqContainerT const& requiredParams,
243 OptContainerT const& optionalParams) {
244 // We expect either no -osint, or the full commandline to be:
245 //
246 // app -osint [<optional-param>...] <required-param> <required-argument>
247 //
248 // Otherwise, we abort to avoid abuse of other command-line handlers from apps
249 // that do a poor job escaping links they give to the OS.
250 //
251 // Note that the above implies that optional parameters do not themselves take
252 // arguments. This is a security feature, to prevent the possible injection of
253 // additional parameters via such arguments. (See, e.g., bug 384384.)
254
255 static constexpr const char* osintLit = "osint";
256
257 // If "-osint" (or the equivalent) is not present, then this is trivially
258 // satisfied.
259 if (CheckArg(aArgc, aArgv, osintLit, nullptr, CheckArgFlag::None) !=
260 ARG_FOUND) {
261 return true;
262 }
263
264 // There should be at least 4 items present:
265 // <app name> -osint <required param> <arg>.
266 if (aArgc < 4) {
267 return false;
268 }
269
270 // The first parameter must be osint.
271 const auto arg1 = ReadAsOption(aArgv[1]);
272 if (!arg1) return false;
273 if (!strimatch(osintLit, arg1.value())) {
274 return false;
275 }
276 // Following this is any number of optional parameters, terminated by a
277 // required parameter.
278 int pos = 2;
279 while (true) {
280 if (pos >= aArgc) return false;
281
282 auto const arg = ReadAsOption(aArgv[pos]);
283 if (!arg) return false;
284
285 if (MatchesAnyOf(arg.value(), optionalParams)) {
286 ++pos;
287 continue;
288 }
289
290 if (MatchesAnyOf(arg.value(), requiredParams)) {
291 ++pos;
292 break;
293 }
294
295 return false;
296 }
297
298 // There must be one argument remaining...
299 if (pos + 1 != aArgc) return false;
300 // ... which must not be another option.
301 if (ReadAsOption(aArgv[pos])) {
302 return false;
303 }
304
305 // Nothing ill-formed was passed.
306 return true;
307}
308
309// C (and so C++) disallows empty arrays. Rather than require callers to jump
310// through hoops to specify an empty optional-argument list, allow either its
311// omission or its specification as `nullptr`, and do the hoop-jumping here.
312//
313// No such facility is provided for requiredParams, which must have at least one
314// entry.
315template <typename CharT, typename ReqContainerT>
316inline bool EnsureCommandlineSafeImpl(int aArgc, CharT** aArgv,
317 ReqContainerT const& requiredParams,
318 std::nullptr_t _ = nullptr) {
319 struct {
320 inline const char** begin() const { return nullptr; }
321 inline const char** end() const { return nullptr; }
322 } emptyContainer;
323 return EnsureCommandlineSafeImpl(aArgc, aArgv, requiredParams,
324 emptyContainer);
325}
326} // namespace internal
327
328template <typename CharT, typename ReqContainerT,
329 typename OptContainerT = std::nullptr_t>
330inline void EnsureCommandlineSafe(
331 int aArgc, CharT** aArgv, ReqContainerT const& requiredParams,
332 OptContainerT const& optionalParams = nullptr) {
333 if (!internal::EnsureCommandlineSafeImpl(aArgc, aArgv, requiredParams,
334 optionalParams)) {
335 exit(127);
336 }
337}
338
339#if defined(XP_WIN)
340namespace internal {
341
342/**
343 * Attempt to copy the string `s` (considered as a command-line argument) into
344 * the buffer `d` with all necessary escaping and quoting. Returns the number of
345 * characters written.
346 *
347 * If `d` is NULL, doesn't actually write anything to `d`, and merely returns
348 * the number of characters that _would_ have been written.
349 *
350 * (This moderately-awkward conflation ensures that the pre-allocation counting
351 * step and post-allocation copying step use the same algorithm.)
352 */
353inline size_t CopyArgImpl_(wchar_t* d, const wchar_t* s) {
354 size_t len = 0;
355
356 bool const actuallyCopy = d != nullptr;
357 auto const appendChar = [&](wchar_t c) {
358 if (actuallyCopy) {
359 *d++ = c;
360 }
361 len++;
362 };
363
364 bool hasDoubleQuote = wcschr(s, L'"') != nullptr;
365 // Only add doublequotes if...
366 bool addDoubleQuotes =
367 // ... the string is empty, or...
368 *s == '\0' ||
369 // ... the string contains a space or a tab.
370 wcspbrk(s, kCommandLineDelimiter) != nullptr;
371
372 if (addDoubleQuotes) {
373 appendChar('"');
374 }
375
376 if (hasDoubleQuote) {
377 size_t backslashes = 0;
378 while (*s) {
379 if (*s == '\\') {
380 ++backslashes;
381 } else {
382 if (*s == '"') {
383 // Escape the doublequote and all backslashes preceding the
384 // doublequote
385 for (size_t i = 0; i <= backslashes; ++i) {
386 appendChar('\\');
387 }
388 }
389
390 backslashes = 0;
391 }
392
393 appendChar(*s);
394 ++s;
395 }
396 } else {
397 // optimization: just blit
398 auto const src_len = wcslen(s);
399 if (actuallyCopy) {
400 ::wcscpy(d, s);
401 d += src_len;
402 }
403 len += src_len;
404 }
405
406 if (addDoubleQuotes) {
407 appendChar('"');
408 }
409
410 return len;
411}
412
413/**
414 * Compute the space required for the serialized form of this argument. Includes
415 * any additional space needed for quotes and backslash-escapes.
416 */
417inline size_t ArgStrLen(const wchar_t* s) { return CopyArgImpl_(nullptr, s); }
418
419/**
420 * Copy string "s" to string "d", quoting the argument as appropriate and
421 * escaping doublequotes along with any backslashes that immediately precede
422 * doublequotes.
423 * The CRT parses this to retrieve the original argc/argv that we meant,
424 * see STDARGV.C in the MSVC CRT sources.
425 *
426 * @return the end of the string
427 */
428inline wchar_t* ArgToString(wchar_t* d, const wchar_t* s) {
429 return d + CopyArgImpl_(d, s);
430}
431
432} // namespace internal
433
434/**
435 * Creates a command line from a list of arguments.
436 *
437 * @param argc Number of elements in |argv|
438 * @param argv Array of arguments
439 * @param aArgcExtra Number of elements in |aArgvExtra|
440 * @param aArgvExtra Optional array of arguments to be appended to the resulting
441 * command line after those provided by |argv|.
442 */
443inline UniquePtr<wchar_t[]> MakeCommandLine(
444 int argc, const wchar_t* const* argv, int aArgcExtra = 0,
445 const wchar_t* const* aArgvExtra = nullptr) {
446 int i;
447 size_t len = 0;
448
449 // The + 1 for each argument reserves space for either a ' ' or the null
450 // terminator, depending on the position of the argument.
451 for (i = 0; i < argc; ++i) {
452 len += internal::ArgStrLen(argv[i]) + 1;
453 }
454
455 for (i = 0; i < aArgcExtra; ++i) {
456 len += internal::ArgStrLen(aArgvExtra[i]) + 1;
457 }
458
459 // Protect against callers that pass 0 arguments
460 if (len == 0) {
461 len = 1;
462 }
463
464 auto s = MakeUnique<wchar_t[]>(len);
465
466 int totalArgc = argc + aArgcExtra;
467
468 wchar_t* c = s.get();
469 for (i = 0; i < argc; ++i) {
470 c = internal::ArgToString(c, argv[i]);
471 if (i + 1 != totalArgc) {
472 *c = ' ';
473 ++c;
474 }
475 }
476
477 for (i = 0; i < aArgcExtra; ++i) {
478 c = internal::ArgToString(c, aArgvExtra[i]);
479 if (i + 1 != aArgcExtra) {
480 *c = ' ';
481 ++c;
482 }
483 }
484
485 *c = '\0';
486
487 return s;
488}
489
490inline bool SetArgv0ToFullBinaryPath(wchar_t* aArgv[]) {
491 if (!aArgv) {
492 return false;
493 }
494
495 UniquePtr<wchar_t[]> newArgv_0(GetFullBinaryPath());
496 if (!newArgv_0) {
497 return false;
498 }
499
500 // We intentionally leak newArgv_0 into argv[0]
501 aArgv[0] = newArgv_0.release();
502 MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(aArgv[0]);
503 return true;
504}
505
506# if defined(MOZILLA_INTERNAL_API1)
507// This class converts a command line string into an array of the arguments.
508// It's basically the opposite of MakeCommandLine. However, the behavior is
509// different from ::CommandLineToArgvW in several ways, such as escaping a
510// backslash or quoting an argument containing whitespaces. This satisfies
511// the examples at:
512// https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args#results-of-parsing-command-lines
513// https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85)
514template <typename T>
515class CommandLineParserWin final {
516 int mArgc;
517 T** mArgv;
518
519 void Release() {
520 if (mArgv) {
521 while (mArgc) {
522 delete[] mArgv[--mArgc];
523 }
524 delete[] mArgv;
525 mArgv = nullptr;
526 }
527 }
528
529 public:
530 CommandLineParserWin() : mArgc(0), mArgv(nullptr) {}
531 ~CommandLineParserWin() { Release(); }
532
533 CommandLineParserWin(const CommandLineParserWin&) = delete;
534 CommandLineParserWin(CommandLineParserWin&&) = delete;
535 CommandLineParserWin& operator=(const CommandLineParserWin&) = delete;
536 CommandLineParserWin& operator=(CommandLineParserWin&&) = delete;
537
538 int Argc() const { return mArgc; }
539 const T* const* Argv() const { return mArgv; }
540
541 // Returns the number of characters handled
542 int HandleCommandLine(const nsTSubstring<T>& aCmdLineString) {
543 Release();
544
545 if (aCmdLineString.IsEmpty()) {
546 return 0;
547 }
548
549 int justCounting = 1;
550 // Flags, etc.
551 int init = 1;
552 int between, quoted, bSlashCount;
553 const T* p;
554 const T* const pEnd = aCmdLineString.EndReading();
555 nsTAutoString<T> arg;
556
557 // We loop if we've not finished the second pass through.
558 while (1) {
559 // Initialize if required.
560 if (init) {
561 p = aCmdLineString.BeginReading();
562 between = 1;
563 mArgc = quoted = bSlashCount = 0;
564
565 init = 0;
566 }
567
568 const T charCurr = (p < pEnd) ? *p : 0;
569 const T charNext = (p + 1 < pEnd) ? *(p + 1) : 0;
570
571 if (between) {
572 // We are traversing whitespace between args.
573 // Check for start of next arg.
574 if (charCurr != 0 && !wcschr(kCommandLineDelimiter, charCurr)) {
575 // Start of another arg.
576 between = 0;
577 arg.Truncate();
578 switch (charCurr) {
579 case '\\':
580 // Count the backslash.
581 bSlashCount = 1;
582 break;
583 case '"':
584 // Remember we're inside quotes.
585 quoted = 1;
586 break;
587 default:
588 // Add character to arg.
589 arg += charCurr;
590 break;
591 }
592 } else {
593 // Another space between args, ignore it.
594 }
595 } else {
596 // We are processing the contents of an argument.
597 // Check for whitespace or end.
598 if (charCurr == 0 ||
599 (!quoted && wcschr(kCommandLineDelimiter, charCurr))) {
600 // Process pending backslashes (interpret them
601 // literally since they're not followed by a ").
602 while (bSlashCount) {
603 arg += '\\';
604 bSlashCount--;
605 }
606 // End current arg.
607 if (!justCounting) {
608 mArgv[mArgc] = new T[arg.Length() + 1];
609 memcpy(mArgv[mArgc], arg.get(), (arg.Length() + 1) * sizeof(T));
610 }
611 mArgc++;
612 // We're now between args.
613 between = 1;
614 } else {
615 // Still inside argument, process the character.
616 switch (charCurr) {
617 case '"':
618 // First, digest preceding backslashes (if any).
619 while (bSlashCount > 1) {
620 // Put one backsplash in arg for each pair.
621 arg += '\\';
622 bSlashCount -= 2;
623 }
624 if (bSlashCount) {
625 // Quote is literal.
626 arg += '"';
627 bSlashCount = 0;
628 } else {
629 // Quote starts or ends a quoted section.
630 if (quoted) {
631 // Check for special case of consecutive double
632 // quotes inside a quoted section.
633 if (charNext == '"') {
634 // This implies a literal double-quote. Fake that
635 // out by causing next double-quote to look as
636 // if it was preceded by a backslash.
637 bSlashCount = 1;
638 } else {
639 quoted = 0;
640 }
641 } else {
642 quoted = 1;
643 }
644 }
645 break;
646 case '\\':
647 // Add to count.
648 bSlashCount++;
649 break;
650 default:
651 // Accept any preceding backslashes literally.
652 while (bSlashCount) {
653 arg += '\\';
654 bSlashCount--;
655 }
656 // Just add next char to the current arg.
657 arg += charCurr;
658 break;
659 }
660 }
661 }
662
663 // Check for end of input.
664 if (charCurr) {
665 // Go to next character.
666 p++;
667 } else {
668 // If on first pass, go on to second.
669 if (justCounting) {
670 // Allocate argv array.
671 mArgv = new T*[mArgc];
672
673 // Start second pass
674 justCounting = 0;
675 init = 1;
676 } else {
677 // Quit.
678 break;
679 }
680 }
681 }
682
683 return p - aCmdLineString.BeginReading();
684 }
685};
686# endif // defined(MOZILLA_INTERNAL_API)
687
688#endif // defined(XP_WIN)
689
690// SaveToEnv and EnvHasValue are only available on Windows or when
691// MOZILLA_INTERNAL_API is defined
692#if defined(MOZILLA_INTERNAL_API1) || defined(XP_WIN)
693
694// Save literal putenv string to environment variable.
695MOZ_NEVER_INLINE__attribute__((noinline)) inline void SaveToEnv(const char* aEnvString) {
696# if defined(MOZILLA_INTERNAL_API1)
697 char* expr = strdup(aEnvString);
6
Memory is allocated
698 if (expr) {
7
Assuming 'expr' is non-null
8
Taking true branch
699 PR_SetEnv(expr);
700 }
701
702 // We intentionally leak |expr| here since it is required by PR_SetEnv.
703 MOZ_LSAN_INTENTIONALLY_LEAK_OBJECT(expr);
704# elif defined(XP_WIN)
705 // This is the same as the NSPR implementation
706 // (Note that we don't need to do a strdup for this case; the CRT makes a
707 // copy)
708 _putenv(aEnvString);
709# endif
710}
9
Potential leak of memory pointed to by 'expr'
711
712inline bool EnvHasValue(const char* aVarName) {
713# if defined(MOZILLA_INTERNAL_API1)
714 const char* val = PR_GetEnv(aVarName);
715 return val && *val;
716# elif defined(XP_WIN)
717 // This is the same as the NSPR implementation
718 const char* val = getenv(aVarName);
719 return val && *val;
720# endif
721}
722
723#endif // end windows/internal_api-only definitions
724
725#ifndef NS_NO_XPCOM
726already_AddRefed<nsIFile> GetFileFromEnv(const char* name);
727#endif
728
729} // namespace mozilla
730
731#endif // mozilla_CmdLineAndEnvUtils_h