Bug 1460996: Ensure the bootstrap process is always working with an argv[0] containing an absolute path (v2); r=mhowell
authorAaron Klotz <aklotz@mozilla.com>
Mon, 14 May 2018 11:40:08 -0600
changeset 418268 7a6b6db92a0e95f19dd8d9ddf665981e02b6b198
parent 418267 9986e21cfe36076928268ef8064e72225b77bfaa
child 418269 64c4562813fa24925769f3f8d3490d142a8b629a
push id33997
push userncsoregi@mozilla.com
push dateTue, 15 May 2018 09:53:53 +0000
treeherdermozilla-central@cf3ee1402348 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1460996
milestone62.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1460996: Ensure the bootstrap process is always working with an argv[0] containing an absolute path (v2); r=mhowell
browser/app/LauncherProcessWin.cpp
--- a/browser/app/LauncherProcessWin.cpp
+++ b/browser/app/LauncherProcessWin.cpp
@@ -75,17 +75,41 @@ ShowError(DWORD aError = ::GetLastError(
   if (!result) {
     return;
   }
 
   ::MessageBoxW(nullptr, rawMsgBuf, L"Firefox", MB_OK | MB_ICONERROR);
   ::LocalFree(rawMsgBuf);
 }
 
-static wchar_t gAbsPath[MAX_PATH];
+static bool
+SetArgv0ToFullBinaryPath(wchar_t* aArgv[])
+{
+  DWORD bufLen = MAX_PATH;
+  mozilla::UniquePtr<wchar_t[]> buf;
+
+  while (true) {
+    buf = mozilla::MakeUnique<wchar_t[]>(bufLen);
+    DWORD retLen = ::GetModuleFileNameW(nullptr, buf.get(), bufLen);
+    if (!retLen) {
+      return false;
+    }
+
+    if (retLen == bufLen && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
+      bufLen *= 2;
+      continue;
+    }
+
+    break;
+  }
+
+  // We intentionally leak buf into argv[0]
+  aArgv[0] = buf.release();
+  return true;
+}
 
 namespace mozilla {
 
 // Eventually we want to be able to set a build config flag such that, when set,
 // this function will always return true.
 bool
 RunAsLauncherProcess(int& argc, wchar_t** argv)
 {
@@ -107,28 +131,21 @@ LauncherMain(int argc, wchar_t* argv[])
 
       DebugOnly<BOOL> setOk = pSetProcessMitigationPolicy(ProcessImageLoadPolicy,
                                                           &imgLoadPol,
                                                           sizeof(imgLoadPol));
       MOZ_ASSERT(setOk);
     }
   }
 
-  // Convert argv[0] to an absolute path if necessary
-  DWORD absPathLen = ::SearchPathW(nullptr, argv[0], L".exe",
-                                   ArrayLength(gAbsPath), gAbsPath, nullptr);
-  if (!absPathLen) {
+  if (!SetArgv0ToFullBinaryPath(argv)) {
     ShowError();
     return 1;
   }
 
-  if (absPathLen < ArrayLength(gAbsPath)) {
-    argv[0] = gAbsPath;
-  }
-
   // If we're elevated, we should relaunch ourselves as a normal user
   Maybe<bool> isElevated = IsElevated();
   if (!isElevated) {
     return 1;
   }
 
   if (isElevated.value()) {
     return !LaunchUnelevated(argc, argv);