nsBrowserApp.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. #include "nsXULAppAPI.h"
  6. #include "mozilla/AppData.h"
  7. #include "application.ini.h"
  8. #include "nsXPCOMGlue.h"
  9. #if defined(XP_WIN)
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #elif defined(XP_UNIX)
  13. #include <sys/resource.h>
  14. #include <unistd.h>
  15. #endif
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <time.h>
  19. #include "nsCOMPtr.h"
  20. #include "nsIFile.h"
  21. #include "nsStringGlue.h"
  22. #ifdef XP_WIN
  23. #define XRE_WANT_ENVIRON
  24. #define strcasecmp _stricmp
  25. #endif
  26. #include "BinaryPath.h"
  27. #include "nsXPCOMPrivate.h" // for MAXPATHLEN and XPCOM_DLL
  28. #include "mozilla/Sprintf.h"
  29. #include "mozilla/Telemetry.h"
  30. #include "mozilla/WindowsDllBlocklist.h"
  31. #define MOZ_BROWSER_CAN_BE_CONTENTPROC
  32. #include "../../ipc/contentproc/plugin-container.cpp"
  33. using namespace mozilla;
  34. #define kDesktopFolder "browser"
  35. static void Output(const char *fmt, ... )
  36. {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. #ifndef XP_WIN
  40. vfprintf(stderr, fmt, ap);
  41. #else
  42. char msg[2048];
  43. vsnprintf_s(msg, _countof(msg), _TRUNCATE, fmt, ap);
  44. wchar_t wide_msg[2048];
  45. MultiByteToWideChar(CP_UTF8,
  46. 0,
  47. msg,
  48. -1,
  49. wide_msg,
  50. _countof(wide_msg));
  51. #if MOZ_WINCONSOLE
  52. fwprintf_s(stderr, wide_msg);
  53. #else
  54. // Linking user32 at load-time interferes with the DLL blocklist (bug 932100).
  55. // This is a rare codepath, so we can load user32 at run-time instead.
  56. HMODULE user32 = LoadLibraryW(L"user32.dll");
  57. if (user32) {
  58. decltype(MessageBoxW)* messageBoxW =
  59. (decltype(MessageBoxW)*) GetProcAddress(user32, "MessageBoxW");
  60. if (messageBoxW) {
  61. messageBoxW(nullptr, wide_msg, L"Pale Moon", MB_OK
  62. | MB_ICONERROR
  63. | MB_SETFOREGROUND);
  64. }
  65. FreeLibrary(user32);
  66. }
  67. #endif
  68. #endif
  69. va_end(ap);
  70. }
  71. /**
  72. * Return true if |arg| matches the given argument name.
  73. */
  74. static bool IsArg(const char* arg, const char* s)
  75. {
  76. if (*arg == '-')
  77. {
  78. if (*++arg == '-')
  79. ++arg;
  80. return !strcasecmp(arg, s);
  81. }
  82. #if defined(XP_WIN)
  83. if (*arg == '/')
  84. return !strcasecmp(++arg, s);
  85. #endif
  86. return false;
  87. }
  88. XRE_GetFileFromPathType XRE_GetFileFromPath;
  89. XRE_CreateAppDataType XRE_CreateAppData;
  90. XRE_FreeAppDataType XRE_FreeAppData;
  91. XRE_TelemetryAccumulateType XRE_TelemetryAccumulate;
  92. XRE_StartupTimelineRecordType XRE_StartupTimelineRecord;
  93. XRE_mainType XRE_main;
  94. XRE_StopLateWriteChecksType XRE_StopLateWriteChecks;
  95. XRE_XPCShellMainType XRE_XPCShellMain;
  96. XRE_GetProcessTypeType XRE_GetProcessType;
  97. XRE_SetProcessTypeType XRE_SetProcessType;
  98. XRE_InitChildProcessType XRE_InitChildProcess;
  99. XRE_EnableSameExecutableForContentProcType XRE_EnableSameExecutableForContentProc;
  100. #ifdef LIBFUZZER
  101. XRE_LibFuzzerSetMainType XRE_LibFuzzerSetMain;
  102. XRE_LibFuzzerGetFuncsType XRE_LibFuzzerGetFuncs;
  103. #endif
  104. static const nsDynamicFunctionLoad kXULFuncs[] = {
  105. { "XRE_GetFileFromPath", (NSFuncPtr*) &XRE_GetFileFromPath },
  106. { "XRE_CreateAppData", (NSFuncPtr*) &XRE_CreateAppData },
  107. { "XRE_FreeAppData", (NSFuncPtr*) &XRE_FreeAppData },
  108. { "XRE_TelemetryAccumulate", (NSFuncPtr*) &XRE_TelemetryAccumulate },
  109. { "XRE_StartupTimelineRecord", (NSFuncPtr*) &XRE_StartupTimelineRecord },
  110. { "XRE_main", (NSFuncPtr*) &XRE_main },
  111. { "XRE_StopLateWriteChecks", (NSFuncPtr*) &XRE_StopLateWriteChecks },
  112. { "XRE_XPCShellMain", (NSFuncPtr*) &XRE_XPCShellMain },
  113. { "XRE_GetProcessType", (NSFuncPtr*) &XRE_GetProcessType },
  114. { "XRE_SetProcessType", (NSFuncPtr*) &XRE_SetProcessType },
  115. { "XRE_InitChildProcess", (NSFuncPtr*) &XRE_InitChildProcess },
  116. { "XRE_EnableSameExecutableForContentProc", (NSFuncPtr*) &XRE_EnableSameExecutableForContentProc },
  117. #ifdef LIBFUZZER
  118. { "XRE_LibFuzzerSetMain", (NSFuncPtr*) &XRE_LibFuzzerSetMain },
  119. { "XRE_LibFuzzerGetFuncs", (NSFuncPtr*) &XRE_LibFuzzerGetFuncs },
  120. #endif
  121. { nullptr, nullptr }
  122. };
  123. #ifdef LIBFUZZER
  124. int libfuzzer_main(int argc, char **argv);
  125. /* This wrapper is used by the libFuzzer main to call into libxul */
  126. void libFuzzerGetFuncs(const char* moduleName, LibFuzzerInitFunc* initFunc,
  127. LibFuzzerTestingFunc* testingFunc) {
  128. return XRE_LibFuzzerGetFuncs(moduleName, initFunc, testingFunc);
  129. }
  130. #endif
  131. static int do_main(int argc, char* argv[], char* envp[], nsIFile *xreDirectory)
  132. {
  133. nsCOMPtr<nsIFile> appini;
  134. nsresult rv;
  135. uint32_t mainFlags = 0;
  136. // Allow palemoon.exe to launch XULRunner apps via -app <application.ini>
  137. // Note that -app must be the *first* argument.
  138. const char *appDataFile = getenv("XUL_APP_FILE");
  139. if (appDataFile && *appDataFile) {
  140. rv = XRE_GetFileFromPath(appDataFile, getter_AddRefs(appini));
  141. if (NS_FAILED(rv)) {
  142. Output("Invalid path found: '%s'", appDataFile);
  143. return 255;
  144. }
  145. }
  146. else if (argc > 1 && IsArg(argv[1], "app")) {
  147. if (argc == 2) {
  148. Output("Incorrect number of arguments passed to -app");
  149. return 255;
  150. }
  151. rv = XRE_GetFileFromPath(argv[2], getter_AddRefs(appini));
  152. if (NS_FAILED(rv)) {
  153. Output("application.ini path not recognized: '%s'", argv[2]);
  154. return 255;
  155. }
  156. char appEnv[MAXPATHLEN];
  157. SprintfLiteral(appEnv, "XUL_APP_FILE=%s", argv[2]);
  158. if (putenv(strdup(appEnv))) {
  159. Output("Couldn't set %s.\n", appEnv);
  160. return 255;
  161. }
  162. argv[2] = argv[0];
  163. argv += 2;
  164. argc -= 2;
  165. } else if (argc > 1 && IsArg(argv[1], "xpcshell")) {
  166. for (int i = 1; i < argc; i++) {
  167. argv[i] = argv[i + 1];
  168. }
  169. return XRE_XPCShellMain(--argc, argv, envp);
  170. }
  171. if (appini) {
  172. nsXREAppData *appData;
  173. rv = XRE_CreateAppData(appini, &appData);
  174. if (NS_FAILED(rv)) {
  175. Output("Couldn't read application.ini");
  176. return 255;
  177. }
  178. #if defined(HAS_DLL_BLOCKLIST)
  179. // The dll blocklist operates in the exe vs. xullib. Pass a flag to
  180. // xullib so automated tests can check the result once the browser
  181. // is up and running.
  182. appData->flags |=
  183. DllBlocklist_CheckStatus() ? NS_XRE_DLL_BLOCKLIST_ENABLED : 0;
  184. #endif
  185. // xreDirectory already has a refcount from NS_NewLocalFile
  186. appData->xreDirectory = xreDirectory;
  187. int result = XRE_main(argc, argv, appData, mainFlags);
  188. XRE_FreeAppData(appData);
  189. return result;
  190. }
  191. ScopedAppData appData(&sAppData);
  192. nsCOMPtr<nsIFile> exeFile;
  193. rv = mozilla::BinaryPath::GetFile(argv[0], getter_AddRefs(exeFile));
  194. if (NS_FAILED(rv)) {
  195. Output("Couldn't find the application directory.\n");
  196. return 255;
  197. }
  198. nsCOMPtr<nsIFile> greDir;
  199. exeFile->GetParent(getter_AddRefs(greDir));
  200. nsCOMPtr<nsIFile> appSubdir;
  201. greDir->Clone(getter_AddRefs(appSubdir));
  202. appSubdir->Append(NS_LITERAL_STRING(kDesktopFolder));
  203. SetStrongPtr(appData.directory, static_cast<nsIFile*>(appSubdir.get()));
  204. // xreDirectory already has a refcount from NS_NewLocalFile
  205. appData.xreDirectory = xreDirectory;
  206. #if defined(HAS_DLL_BLOCKLIST)
  207. appData.flags |=
  208. DllBlocklist_CheckStatus() ? NS_XRE_DLL_BLOCKLIST_ENABLED : 0;
  209. #endif
  210. #ifdef LIBFUZZER
  211. if (getenv("LIBFUZZER"))
  212. XRE_LibFuzzerSetMain(argc, argv, libfuzzer_main);
  213. #endif
  214. return XRE_main(argc, argv, &appData, mainFlags);
  215. }
  216. static bool
  217. FileExists(const char *path)
  218. {
  219. #ifdef XP_WIN
  220. wchar_t wideDir[MAX_PATH];
  221. MultiByteToWideChar(CP_UTF8, 0, path, -1, wideDir, MAX_PATH);
  222. DWORD fileAttrs = GetFileAttributesW(wideDir);
  223. return fileAttrs != INVALID_FILE_ATTRIBUTES;
  224. #else
  225. return access(path, R_OK) == 0;
  226. #endif
  227. }
  228. static nsresult
  229. InitXPCOMGlue(const char *argv0, nsIFile **xreDirectory)
  230. {
  231. char exePath[MAXPATHLEN];
  232. nsresult rv = mozilla::BinaryPath::Get(argv0, exePath);
  233. if (NS_FAILED(rv)) {
  234. Output("Couldn't find the application directory.\n");
  235. return rv;
  236. }
  237. char *lastSlash = strrchr(exePath, XPCOM_FILE_PATH_SEPARATOR[0]);
  238. if (!lastSlash ||
  239. (size_t(lastSlash - exePath) > MAXPATHLEN - sizeof(XPCOM_DLL) - 1))
  240. return NS_ERROR_FAILURE;
  241. strcpy(lastSlash + 1, XPCOM_DLL);
  242. if (!FileExists(exePath)) {
  243. Output("Could not find the Mozilla runtime.\n");
  244. return NS_ERROR_FAILURE;
  245. }
  246. // We do this because of data in bug 771745
  247. XPCOMGlueEnablePreload();
  248. rv = XPCOMGlueStartup(exePath);
  249. if (NS_FAILED(rv)) {
  250. Output("Couldn't load XPCOM.\n");
  251. return rv;
  252. }
  253. rv = XPCOMGlueLoadXULFunctions(kXULFuncs);
  254. if (NS_FAILED(rv)) {
  255. Output("Couldn't load XRE functions.\n");
  256. return rv;
  257. }
  258. // This will set this thread as the main thread.
  259. NS_LogInit();
  260. if (xreDirectory) {
  261. // chop XPCOM_DLL off exePath
  262. *lastSlash = '\0';
  263. #ifdef XP_WIN
  264. rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(exePath), false,
  265. xreDirectory);
  266. #else
  267. rv = NS_NewNativeLocalFile(nsDependentCString(exePath), false,
  268. xreDirectory);
  269. #endif
  270. }
  271. return rv;
  272. }
  273. int main(int argc, char* argv[], char* envp[])
  274. {
  275. mozilla::TimeStamp start = mozilla::TimeStamp::Now();
  276. #ifdef HAS_DLL_BLOCKLIST
  277. DllBlocklist_Initialize();
  278. #ifdef DEBUG
  279. // In order to be effective against AppInit DLLs, the blocklist must be
  280. // initialized before user32.dll is loaded into the process (bug 932100).
  281. if (GetModuleHandleA("user32.dll")) {
  282. fprintf(stderr, "DLL blocklist was unable to intercept AppInit DLLs.\n");
  283. }
  284. #endif
  285. #endif
  286. #ifdef MOZ_BROWSER_CAN_BE_CONTENTPROC
  287. // We are launching as a content process, delegate to the appropriate
  288. // main
  289. if (argc > 1 && IsArg(argv[1], "contentproc")) {
  290. nsresult rv = InitXPCOMGlue(argv[0], nullptr);
  291. if (NS_FAILED(rv)) {
  292. return 255;
  293. }
  294. int result = content_process_main(argc, argv);
  295. // InitXPCOMGlue calls NS_LogInit, so we need to balance it here.
  296. NS_LogTerm();
  297. return result;
  298. }
  299. #endif
  300. nsIFile *xreDirectory;
  301. nsresult rv = InitXPCOMGlue(argv[0], &xreDirectory);
  302. if (NS_FAILED(rv)) {
  303. return 255;
  304. }
  305. XRE_StartupTimelineRecord(mozilla::StartupTimeline::START, start);
  306. #ifdef MOZ_BROWSER_CAN_BE_CONTENTPROC
  307. XRE_EnableSameExecutableForContentProc();
  308. #endif
  309. int result = do_main(argc, argv, envp, xreDirectory);
  310. NS_LogTerm();
  311. return result;
  312. }