2002-08-21 23:21:36 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-08-21 23:21:36 +04:00
|
|
|
|
2012-02-22 11:12:15 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2016-12-02 17:07:24 +03:00
|
|
|
#include "mozilla/XREAppData.h"
|
2011-11-22 11:05:59 +04:00
|
|
|
#include "application.ini.h"
|
2017-01-13 01:56:05 +03:00
|
|
|
#include "mozilla/Bootstrap.h"
|
2011-05-22 10:24:32 +04:00
|
|
|
#if defined(XP_WIN)
|
2002-08-21 23:21:36 +04:00
|
|
|
#include <windows.h>
|
2004-06-18 01:23:51 +04:00
|
|
|
#include <stdlib.h>
|
2011-05-22 10:24:32 +04:00
|
|
|
#elif defined(XP_UNIX)
|
|
|
|
#include <sys/resource.h>
|
2013-01-03 19:04:42 +04:00
|
|
|
#include <unistd.h>
|
2002-08-21 23:21:36 +04:00
|
|
|
#endif
|
2004-06-18 01:23:51 +04:00
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2012-06-16 21:44:07 +04:00
|
|
|
#include <time.h>
|
2007-07-26 00:24:37 +04:00
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2007-07-02 22:20:24 +04:00
|
|
|
#include "nsStringGlue.h"
|
|
|
|
|
2007-12-31 18:15:43 +03:00
|
|
|
#ifdef XP_WIN
|
2014-11-24 21:42:33 +03:00
|
|
|
#ifdef MOZ_ASAN
|
|
|
|
// ASAN requires firefox.exe to be built with -MD, and it's OK if we don't
|
|
|
|
// support Windows XP SP2 in ASAN builds.
|
|
|
|
#define XRE_DONT_SUPPORT_XPSP2
|
|
|
|
#endif
|
2015-11-19 09:31:44 +03:00
|
|
|
#define XRE_WANT_ENVIRON
|
2011-05-22 10:23:20 +04:00
|
|
|
#define strcasecmp _stricmp
|
2016-05-15 18:41:40 +03:00
|
|
|
#ifdef MOZ_SANDBOX
|
|
|
|
#include "mozilla/sandboxing/SandboxInitialization.h"
|
|
|
|
#endif
|
2007-12-31 18:15:43 +03:00
|
|
|
#endif
|
2011-05-22 10:23:20 +04:00
|
|
|
#include "BinaryPath.h"
|
|
|
|
|
|
|
|
#include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
|
2007-12-31 18:15:43 +03:00
|
|
|
|
2016-10-04 18:57:51 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2011-05-22 10:24:32 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-11-12 17:31:32 +04:00
|
|
|
#include "mozilla/WindowsDllBlocklist.h"
|
2011-05-22 10:24:32 +04:00
|
|
|
|
2017-01-12 08:44:18 +03:00
|
|
|
#ifdef LIBFUZZER
|
|
|
|
#include "FuzzerDefs.h"
|
|
|
|
#endif
|
|
|
|
|
2016-09-14 12:40:53 +03:00
|
|
|
#ifdef MOZ_LINUX_32_SSE2_STARTUP_ERROR
|
|
|
|
#include <cpuid.h>
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsSSE2Available()
|
|
|
|
{
|
|
|
|
// The rest of the app has been compiled to assume that SSE2 is present
|
|
|
|
// unconditionally, so we can't use the normal copy of SSE.cpp here.
|
|
|
|
// Since SSE.cpp caches the results and we need them only transiently,
|
|
|
|
// instead of #including SSE.cpp here, let's just inline the specific check
|
|
|
|
// that's needed.
|
|
|
|
unsigned int level = 1u;
|
|
|
|
unsigned int eax, ebx, ecx, edx;
|
|
|
|
unsigned int bits = (1u<<26);
|
|
|
|
unsigned int max = __get_cpuid_max(0, nullptr);
|
|
|
|
if (level > max) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
__cpuid_count(level, 0, eax, ebx, ecx, edx);
|
|
|
|
return (edx & bits) == bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char sSSE2Message[] =
|
|
|
|
"This browser version requires a processor with the SSE2 instruction "
|
|
|
|
"set extension.\nYou may be able to obtain a version that does not "
|
|
|
|
"require SSE2 from your Linux distribution.\n";
|
|
|
|
|
|
|
|
__attribute__((constructor))
|
|
|
|
static void
|
|
|
|
SSE2Check()
|
|
|
|
{
|
|
|
|
if (IsSSE2Available()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Using write() in order to avoid jemalloc-based buffering. Ignoring return
|
|
|
|
// values, since there isn't much we could do on failure and there is no
|
|
|
|
// point in trying to recover from errors.
|
|
|
|
MOZ_UNUSED(write(STDERR_FILENO,
|
|
|
|
sSSE2Message,
|
|
|
|
MOZ_ARRAY_LENGTH(sSSE2Message) - 1));
|
|
|
|
// _exit() instead of exit() to avoid running the usual "at exit" code.
|
|
|
|
_exit(255);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 01:06:36 +03:00
|
|
|
#if !defined(MOZ_WIDGET_COCOA) && !defined(MOZ_WIDGET_ANDROID)
|
2016-06-03 22:49:39 +03:00
|
|
|
#define MOZ_BROWSER_CAN_BE_CONTENTPROC
|
|
|
|
#include "../../ipc/contentproc/plugin-container.cpp"
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 19:04:42 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-09-29 22:51:04 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
#define kOSXResourcesFolder "Resources"
|
|
|
|
#endif
|
2013-02-13 00:51:24 +04:00
|
|
|
#define kDesktopFolder "browser"
|
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
static void Output(const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
2012-07-24 21:04:03 +04:00
|
|
|
#ifndef XP_WIN
|
2007-07-02 22:20:24 +04:00
|
|
|
vfprintf(stderr, fmt, ap);
|
2012-07-24 21:04:03 +04:00
|
|
|
#else
|
|
|
|
char msg[2048];
|
|
|
|
vsnprintf_s(msg, _countof(msg), _TRUNCATE, fmt, ap);
|
|
|
|
|
|
|
|
wchar_t wide_msg[2048];
|
|
|
|
MultiByteToWideChar(CP_UTF8,
|
|
|
|
0,
|
|
|
|
msg,
|
|
|
|
-1,
|
|
|
|
wide_msg,
|
|
|
|
_countof(wide_msg));
|
|
|
|
#if MOZ_WINCONSOLE
|
|
|
|
fwprintf_s(stderr, wide_msg);
|
|
|
|
#else
|
2013-11-12 17:31:32 +04:00
|
|
|
// Linking user32 at load-time interferes with the DLL blocklist (bug 932100).
|
|
|
|
// This is a rare codepath, so we can load user32 at run-time instead.
|
|
|
|
HMODULE user32 = LoadLibraryW(L"user32.dll");
|
|
|
|
if (user32) {
|
2014-02-18 02:32:52 +04:00
|
|
|
decltype(MessageBoxW)* messageBoxW =
|
|
|
|
(decltype(MessageBoxW)*) GetProcAddress(user32, "MessageBoxW");
|
2013-11-12 17:31:32 +04:00
|
|
|
if (messageBoxW) {
|
|
|
|
messageBoxW(nullptr, wide_msg, L"Firefox", MB_OK
|
|
|
|
| MB_ICONERROR
|
|
|
|
| MB_SETFOREGROUND);
|
|
|
|
}
|
|
|
|
FreeLibrary(user32);
|
|
|
|
}
|
2012-07-24 21:04:03 +04:00
|
|
|
#endif
|
2007-05-26 02:09:29 +04:00
|
|
|
#endif
|
2007-07-02 22:20:24 +04:00
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2002-08-21 23:21:36 +04:00
|
|
|
|
2007-07-26 00:24:37 +04:00
|
|
|
/**
|
|
|
|
* Return true if |arg| matches the given argument name.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsArg(const char* arg, const char* s)
|
2007-07-26 00:24:37 +04:00
|
|
|
{
|
|
|
|
if (*arg == '-')
|
|
|
|
{
|
|
|
|
if (*++arg == '-')
|
|
|
|
++arg;
|
2011-05-22 10:23:20 +04:00
|
|
|
return !strcasecmp(arg, s);
|
2007-07-26 00:24:37 +04:00
|
|
|
}
|
|
|
|
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_WIN)
|
2007-07-26 00:24:37 +04:00
|
|
|
if (*arg == '/')
|
2011-05-22 10:23:20 +04:00
|
|
|
return !strcasecmp(++arg, s);
|
2007-07-26 00:24:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-07-26 00:24:37 +04:00
|
|
|
}
|
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
Bootstrap::UniquePtr gBootstrap;
|
2011-05-22 10:22:27 +04:00
|
|
|
|
2017-01-10 02:02:43 +03:00
|
|
|
static int do_main(int argc, char* argv[], char* envp[])
|
2011-05-22 10:23:20 +04:00
|
|
|
{
|
2007-07-26 00:24:37 +04:00
|
|
|
// Allow firefox.exe to launch XULRunner apps via -app <application.ini>
|
|
|
|
// Note that -app must be the *first* argument.
|
2011-05-22 10:23:20 +04:00
|
|
|
const char *appDataFile = getenv("XUL_APP_FILE");
|
2017-01-10 10:43:23 +03:00
|
|
|
if ((!appDataFile || !*appDataFile) &&
|
|
|
|
(argc > 1 && IsArg(argv[1], "app"))) {
|
2007-07-26 00:24:37 +04:00
|
|
|
if (argc == 2) {
|
|
|
|
Output("Incorrect number of arguments passed to -app");
|
|
|
|
return 255;
|
|
|
|
}
|
2017-01-10 10:43:23 +03:00
|
|
|
appDataFile = argv[2];
|
2007-07-26 00:24:37 +04:00
|
|
|
|
2011-05-22 10:23:20 +04:00
|
|
|
char appEnv[MAXPATHLEN];
|
2016-10-04 18:57:51 +03:00
|
|
|
SprintfLiteral(appEnv, "XUL_APP_FILE=%s", argv[2]);
|
Bug 1271574 - Purposefully leak the XUL_APP_FILE string passed to putenv. r=bsmedberg
Before bug 552864, the string was created with PR_smprintf, and
PR_SetEnv'ed (which, under the hood, just calls putenv). PR_smprintf was
allocating the string on the heap. Now, it's allocated on the stack, and
still putenv'ed.
putenv kind of takes ownership of the strings it's being passed, so
stack allocated strings are dangerous to use. It looks like we've been
fairly lucky that it worked, presumably because compilers would keep the
stack frame with the variable, but that's not guaranteed to happen, and
in some case, doesn't.
So we strdup the string and purposefully leak it instead, which matches
what happened before bug 552864, and is the only "sane" way to use
putenv.
--HG--
extra : rebase_source : e39349f90f5346b591e20696c0c3c7fdb26c3cfb
2016-06-02 02:44:16 +03:00
|
|
|
if (putenv(strdup(appEnv))) {
|
2011-05-22 10:23:20 +04:00
|
|
|
Output("Couldn't set %s.\n", appEnv);
|
|
|
|
return 255;
|
|
|
|
}
|
2007-07-26 00:24:37 +04:00
|
|
|
argv[2] = argv[0];
|
|
|
|
argv += 2;
|
|
|
|
argc -= 2;
|
2015-11-19 09:31:44 +03:00
|
|
|
} else if (argc > 1 && IsArg(argv[1], "xpcshell")) {
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
argv[i] = argv[i + 1];
|
|
|
|
}
|
2016-05-15 18:41:40 +03:00
|
|
|
|
|
|
|
XREShellData shellData;
|
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
shellData.sandboxBrokerServices =
|
|
|
|
sandboxing::GetInitializedBrokerServices();
|
|
|
|
#endif
|
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
return gBootstrap->XRE_XPCShellMain(--argc, argv, envp, &shellData);
|
2007-07-26 00:24:37 +04:00
|
|
|
}
|
|
|
|
|
2017-01-10 10:43:23 +03:00
|
|
|
BootstrapConfig config;
|
2016-12-13 17:15:31 +03:00
|
|
|
|
2017-01-10 10:43:23 +03:00
|
|
|
if (appDataFile && *appDataFile) {
|
|
|
|
config.appData = nullptr;
|
|
|
|
config.appDataPath = appDataFile;
|
2016-12-13 17:15:31 +03:00
|
|
|
} else {
|
|
|
|
// no -app flag so we use the compiled-in app data
|
2017-01-10 10:43:23 +03:00
|
|
|
config.appData = &sAppData;
|
|
|
|
config.appDataPath = kDesktopFolder;
|
2016-12-13 17:15:31 +03:00
|
|
|
}
|
2013-02-13 00:51:24 +04:00
|
|
|
|
2016-05-15 18:41:40 +03:00
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
sandbox::BrokerServices* brokerServices =
|
|
|
|
sandboxing::GetInitializedBrokerServices();
|
2017-02-15 02:08:40 +03:00
|
|
|
sandboxing::PermissionsService* permissionsService =
|
|
|
|
sandboxing::GetPermissionsService();
|
2016-05-15 18:41:40 +03:00
|
|
|
#if defined(MOZ_CONTENT_SANDBOX)
|
|
|
|
if (!brokerServices) {
|
|
|
|
Output("Couldn't initialize the broker services.\n");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-10 10:43:23 +03:00
|
|
|
config.sandboxBrokerServices = brokerServices;
|
2017-02-15 02:08:40 +03:00
|
|
|
config.sandboxPermissionsService = permissionsService;
|
2016-05-15 18:41:40 +03:00
|
|
|
#endif
|
|
|
|
|
2016-09-01 16:07:01 +03:00
|
|
|
#ifdef LIBFUZZER
|
|
|
|
if (getenv("LIBFUZZER"))
|
2017-01-12 08:44:18 +03:00
|
|
|
gBootstrap->XRE_LibFuzzerSetDriver(fuzzer::FuzzerDriver);
|
2016-09-01 16:07:01 +03:00
|
|
|
#endif
|
|
|
|
|
2017-01-10 10:43:23 +03:00
|
|
|
return gBootstrap->XRE_main(argc, argv, config);
|
2011-05-22 10:23:20 +04:00
|
|
|
}
|
|
|
|
|
2013-01-03 19:04:42 +04:00
|
|
|
static nsresult
|
2017-01-10 02:02:43 +03:00
|
|
|
InitXPCOMGlue(const char *argv0)
|
2013-01-03 19:04:42 +04:00
|
|
|
{
|
2017-01-13 03:40:54 +03:00
|
|
|
UniqueFreePtr<char> exePath = BinaryPath::Get(argv0);
|
|
|
|
if (!exePath) {
|
2013-01-03 19:04:42 +04:00
|
|
|
Output("Couldn't find the application directory.\n");
|
2017-01-13 03:40:54 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-05-22 10:23:20 +04:00
|
|
|
}
|
|
|
|
|
2017-01-13 03:40:54 +03:00
|
|
|
gBootstrap = mozilla::GetBootstrap(exePath.get());
|
2016-12-16 05:10:02 +03:00
|
|
|
if (!gBootstrap) {
|
2011-05-22 10:23:20 +04:00
|
|
|
Output("Couldn't load XPCOM.\n");
|
2016-12-16 05:10:02 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2013-01-03 19:04:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-23 22:10:30 +03:00
|
|
|
// This will set this thread as the main thread.
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->NS_LogInit();
|
2013-01-19 12:40:02 +04:00
|
|
|
|
2017-01-10 02:02:43 +03:00
|
|
|
return NS_OK;
|
2013-01-03 19:04:42 +04:00
|
|
|
}
|
|
|
|
|
2015-11-19 09:31:44 +03:00
|
|
|
int main(int argc, char* argv[], char* envp[])
|
2013-01-03 19:04:42 +04:00
|
|
|
{
|
2016-07-08 02:00:23 +03:00
|
|
|
mozilla::TimeStamp start = mozilla::TimeStamp::Now();
|
|
|
|
|
|
|
|
#ifdef HAS_DLL_BLOCKLIST
|
|
|
|
DllBlocklist_Initialize();
|
|
|
|
#endif
|
|
|
|
|
2016-06-03 22:49:39 +03:00
|
|
|
#ifdef MOZ_BROWSER_CAN_BE_CONTENTPROC
|
|
|
|
// We are launching as a content process, delegate to the appropriate
|
|
|
|
// main
|
|
|
|
if (argc > 1 && IsArg(argv[1], "contentproc")) {
|
|
|
|
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
|
|
|
|
// We need to initialize the sandbox TargetServices before InitXPCOMGlue
|
|
|
|
// because we might need the sandbox broker to give access to some files.
|
2016-06-07 16:03:51 +03:00
|
|
|
if (IsSandboxedProcess() && !sandboxing::GetInitializedTargetServices()) {
|
2016-06-03 22:49:39 +03:00
|
|
|
Output("Failed to initialize the sandbox target services.");
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 02:02:43 +03:00
|
|
|
nsresult rv = InitXPCOMGlue(argv[0]);
|
2016-06-03 22:49:39 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
int result = content_process_main(gBootstrap.get(), argc, argv);
|
2016-06-03 22:49:39 +03:00
|
|
|
|
|
|
|
// InitXPCOMGlue calls NS_LogInit, so we need to balance it here.
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->NS_LogTerm();
|
2016-06-03 22:49:39 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 19:04:42 +04:00
|
|
|
|
2017-01-10 02:02:43 +03:00
|
|
|
nsresult rv = InitXPCOMGlue(argv[0]);
|
2013-01-03 19:04:42 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2011-05-22 10:23:20 +04:00
|
|
|
return 255;
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->XRE_StartupTimelineRecord(mozilla::StartupTimeline::START, start);
|
2012-06-15 10:23:08 +04:00
|
|
|
|
2016-06-03 22:49:39 +03:00
|
|
|
#ifdef MOZ_BROWSER_CAN_BE_CONTENTPROC
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->XRE_EnableSameExecutableForContentProc();
|
2016-06-03 22:49:39 +03:00
|
|
|
#endif
|
|
|
|
|
2017-01-10 02:02:43 +03:00
|
|
|
int result = do_main(argc, argv, envp);
|
2013-01-19 12:40:02 +04:00
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->NS_LogTerm();
|
2011-05-22 10:23:20 +04:00
|
|
|
|
2013-01-04 19:27:41 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
// Allow writes again. While we would like to catch writes from static
|
|
|
|
// destructors to allow early exits to use _exit, we know that there is
|
|
|
|
// at least one such write that we don't control (see bug 826029). For
|
|
|
|
// now we enable writes again and early exits will have to use exit instead
|
|
|
|
// of _exit.
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap->XRE_StopLateWriteChecks();
|
2013-01-04 19:27:41 +04:00
|
|
|
#endif
|
|
|
|
|
2016-12-16 05:10:02 +03:00
|
|
|
gBootstrap.reset();
|
|
|
|
|
2007-07-02 22:20:24 +04:00
|
|
|
return result;
|
2002-08-21 23:21:36 +04:00
|
|
|
}
|