Bug 1035125 Part 1: Back out changeset 1910714b56c6 and associated subsequent changes. r?bsmedberg draft
authorBob Owen <bobowencode@gmail.com>
Sun, 15 May 2016 16:23:56 +0100
changeset 367178 9e28ea4a4b78e77bf85415ab4a1adf1fbd570320
parent 365730 3461f3cae78495f100a0f7d3d2e0b89292d3ec02
child 367179 7c0b40daff6ef0a77ae116cc3c9b6aeef5c325f2
push id18159
push userbobowencode@gmail.com
push dateSun, 15 May 2016 15:37:30 +0000
reviewersbsmedberg
bugs1035125, 1023941
milestone49.0a1
Bug 1035125 Part 1: Back out changeset 1910714b56c6 and associated subsequent changes. r?bsmedberg The original changeset that is being backed out had comment: Bug 1023941 - Part 5: Loader hook to redirect the missing import. The changes made in bug 1023941 were to work around the fact that with VS2013, msvcr120.dll imports kernel32!GetLogicalProcessorInformation, which is not available on Windows XP SP2. In VS2015, the GetLogicalProcessorInformation requirement has moved into concrt140.dll (concurrency runtime), which we don't use. So, now that our build infra is building with VS2015, we can remove the hooking and static runtime linking required to get the VS2013 fix to work. In addition we need to do that to be able us to link the Chromium sandbox code into firefox.exe and get it to build and run with both VS2015 and VS2013. MozReview-Commit-ID: 1tlXaYJ8dHH
b2g/app/nsBrowserApp.cpp
ipc/ipdl/test/cxx/app/TestIPDL.cpp
js/xpconnect/shell/xpcshell.cpp
toolkit/xre/WindowsCrtPatch.h
toolkit/xre/nsWindowsWMain.cpp
--- a/b2g/app/nsBrowserApp.cpp
+++ b/b2g/app/nsBrowserApp.cpp
@@ -20,17 +20,16 @@
 #include <string.h>
 
 #include "nsCOMPtr.h"
 #include "nsIFile.h"
 #include "nsStringGlue.h"
 
 #ifdef XP_WIN
 // we want a wmain entry point
-#define XRE_DONT_SUPPORT_XPSP2 // See https://bugzil.la/1023941#c32
 #include "nsWindowsWMain.cpp"
 #if defined(_MSC_VER) && (_MSC_VER < 1900)
 #define snprintf _snprintf
 #endif
 #define strcasecmp _stricmp
 #endif
 
 #ifdef MOZ_WIDGET_GONK
--- a/ipc/ipdl/test/cxx/app/TestIPDL.cpp
+++ b/ipc/ipdl/test/cxx/app/TestIPDL.cpp
@@ -1,17 +1,16 @@
 /* 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/. */
 
 #include "nsXULAppAPI.h"
 
 #if defined(XP_WIN)
 #include <windows.h>
-#define XRE_DONT_SUPPORT_XPSP2 // this app doesn't ship
 #include "nsWindowsWMain.cpp"
 #endif
 
 int
 main(int argc, char** argv)
 {
     // the first argument specifies which IPDL test case/suite to load
     if (argc < 2)
--- a/js/xpconnect/shell/xpcshell.cpp
+++ b/js/xpconnect/shell/xpcshell.cpp
@@ -15,17 +15,16 @@
 #include "xpcshellMacUtils.h"
 #endif
 #ifdef XP_WIN
 #include <windows.h>
 #include <shlobj.h>
 
 // we want a wmain entry point
 #define XRE_DONT_PROTECT_DLL_LOAD
-#define XRE_DONT_SUPPORT_XPSP2 // xpcshell does not ship
 #define XRE_WANT_ENVIRON
 #include "nsWindowsWMain.cpp"
 #endif
 
 #ifdef MOZ_WIDGET_GTK
 #include <gtk/gtk.h>
 #endif
 
deleted file mode 100644
--- a/toolkit/xre/WindowsCrtPatch.h
+++ /dev/null
@@ -1,146 +0,0 @@
-/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* 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/. */
-
-/*
- * This file works around an incompatibility between Visual Studio 2013's
- * C Runtime DLL and Windows XP Service Pack 2.
- *
- * On XP SP2, msvcr120.dll fails to load, because it has a load-time dependency
- * on a kernel32 export named GetLogicalProcessorInformation, which is only
- * available in XP SP3 and newer. Microsoft has declared this to be by design.
- * See: https://connect.microsoft.com/VisualStudio/feedback/details/811379/
- *
- * The CRT calls GetLogicalProcessorInformation only from the concurrency
- * runtime, which our code does not use. A potential workaround is to 
- * static-link the CRT into all of our binaries and let the linker drop the
- * unused API calls. We don't want to take that approach, due to concerns
- * about binary bloat and jemalloc integration.
- *
- * Instead we hook the Windows loader and patch out the missing import.
- * We intercept ntdll!RtlImageNtHeader, which is a helper API called during
- * the DLL loading process. We walk the PE image and redirect the
- * GetLogicalProcessorInformation import to something benign like DebugBreak,
- * before the loader populates msvcr120.dll's import table.
- *
- * This is a fragile hack that only works if we can set up the hook before
- * Windows tries to load msvcr120.dll. This means that all .exe files:
- *  1) must static-link the CRT
- *  2) must delay-load anything with ties to msvcr120.dll (e.g. mozglue.dll)
- *  3) must not call malloc, because the linker would substitute our mozglue
- *     replacements, which leads to the CRT loading mozglue before main.
- * The remainder of our binaries can continue to dynamic-link the CRT.
- * Assertions enforce that our hooks are installed before msvcr120.dll.
- */
-
-#ifndef WindowsCrtPatch_h
-#define WindowsCrtPatch_h
-
-#include "nsWindowsDllInterceptor.h"
-#include "mozilla/WindowsVersion.h"
-
-namespace WindowsCrtPatch {
-
-mozilla::WindowsDllInterceptor NtdllIntercept;
-
-typedef PIMAGE_NT_HEADERS (NTAPI *RtlImageNtHeader_func)(HMODULE module);
-static RtlImageNtHeader_func stub_RtlImageNtHeader = 0;
-
-// A helper to simplify the use of Relative Virtual Addresses.
-template <typename T>
-class RVAPtr
-{
-public:
-  RVAPtr(HMODULE module, size_t rva)
-    : _ptr(reinterpret_cast<T*>(reinterpret_cast<char*>(module) + rva)) {}
-  operator T*() { return _ptr; }
-  T* operator ->() { return _ptr; }
-  T* operator ++() { return ++_ptr; }
-
-private:
-  T* _ptr;
-};
-
-void
-PatchModuleImports(HMODULE module, PIMAGE_NT_HEADERS headers)
-{
-  static const WORD MAGIC_DOS = 0x5a4d; // "MZ"
-  static const DWORD MAGIC_PE = 0x4550; // "PE\0\0"
-  RVAPtr<IMAGE_DOS_HEADER> dosStub(module, 0);
-
-  if (!module ||
-      !headers ||
-      dosStub->e_magic != MAGIC_DOS ||
-      headers != RVAPtr<IMAGE_NT_HEADERS>(module, dosStub->e_lfanew) ||
-      headers->Signature != MAGIC_PE ||
-      headers->FileHeader.SizeOfOptionalHeader < sizeof(IMAGE_OPTIONAL_HEADER)) {
-    return;
-  }
-
-  // The format of the import directory is described in:
-  // "An In-Depth Look into the Win32 Portable Executable File Format, Part 2"
-  // http://msdn.microsoft.com/en-us/magazine/cc301808.aspx
-
-  IMAGE_DATA_DIRECTORY* importDirectory =
-    &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
-  RVAPtr<IMAGE_IMPORT_DESCRIPTOR> descriptor(module, importDirectory->VirtualAddress);
-
-  for (; descriptor->OriginalFirstThunk; ++descriptor) {
-    RVAPtr<char> importedModule(module, descriptor->Name);
-    if (!stricmp(importedModule, "kernel32.dll")) {
-      RVAPtr<IMAGE_THUNK_DATA> thunk(module, descriptor->OriginalFirstThunk);
-      for (; thunk->u1.AddressOfData; ++thunk) {
-        RVAPtr<IMAGE_IMPORT_BY_NAME> import(module, thunk->u1.AddressOfData);
-        if (!strcmp((char*)import->Name, "GetLogicalProcessorInformation")) {
-          memcpy(import->Name, "DebugBreak", sizeof("DebugBreak"));
-        }
-      }
-    }
-  }
-}
-
-PIMAGE_NT_HEADERS NTAPI
-patched_RtlImageNtHeader(HMODULE module)
-{
-  PIMAGE_NT_HEADERS headers = stub_RtlImageNtHeader(module);
-
-  if (module == GetModuleHandleW(L"msvcr120.dll")) {
-    PatchModuleImports(module, headers);
-  }
-
-  return headers;
-}
-
-// Non-inline to make the asserts stand out
-MOZ_NEVER_INLINE void
-Init()
-{
-  // If the C Runtime DLL is already loaded, our hooks will be ineffective,
-  // and we will fail to load on XP SP2 when built with Visual Studio 2013.
-  // We assert the absence of these modules on all Windows builds in order to
-  // catch breakage faster.
-  //
-  // If these assertions fail, see the comment at the top of this file for
-  // possible causes. Any changes to the lines below MUST be tested on XP SP2!
-  MOZ_ASSERT(!GetModuleHandleA("mozglue.dll"));
-  MOZ_ASSERT(!GetModuleHandleA("msvcr120.dll"));
-  MOZ_ASSERT(!GetModuleHandleA("msvcr120d.dll"));
-
-#if defined(_M_IX86) && defined(_MSC_VER)
-  if (!mozilla::IsWin2003OrLater()) {
-    // Test for the export because we can't trust the SP version (bug 1137609)
-    HMODULE kernel = GetModuleHandleA("kernel32.dll");
-    if (!kernel || !GetProcAddress(kernel, "GetLogicalProcessorInformation")) {
-      NtdllIntercept.Init("ntdll.dll");
-      NtdllIntercept.AddHook("RtlImageNtHeader",
-                             reinterpret_cast<intptr_t>(patched_RtlImageNtHeader),
-                             reinterpret_cast<void**>(&stub_RtlImageNtHeader));
-    }
-  }
-#endif
-}
-
-} // namespace WindowsCrtPatch
-
-#endif // WindowsCrtPatch_h
--- a/toolkit/xre/nsWindowsWMain.cpp
+++ b/toolkit/xre/nsWindowsWMain.cpp
@@ -18,20 +18,16 @@
 #ifndef XRE_DONT_PROTECT_DLL_LOAD
 #include "nsSetDllDirectory.h"
 #endif
 
 #if defined(__GNUC__)
 #define XRE_DONT_SUPPORT_XPSP2
 #endif
 
-#ifndef XRE_DONT_SUPPORT_XPSP2
-#include "WindowsCrtPatch.h"
-#endif
-
 #ifdef __MINGW32__
 
 /* MingW currently does not implement a wide version of the
    startup routines.  Workaround is to implement something like
    it ourselves.  See bug 411826 */
 
 #include <shellapi.h>
 
@@ -82,20 +78,16 @@ FreeAllocStrings(int argc, char **argv)
     delete [] argv[argc];
   }
 
   delete [] argv;
 }
 
 int wmain(int argc, WCHAR **argv)
 {
-#if !defined(XRE_DONT_SUPPORT_XPSP2)
-  WindowsCrtPatch::Init();
-#endif
-
 #if defined(_MSC_VER) && _MSC_VER < 1900 && defined(_M_X64)
   // Disable CRT use of FMA3 on non-AVX2 CPUs and on Win7RTM due to bug 1160148
   int cpuid0[4] = {0};
   int cpuid7[4] = {0};
   __cpuid(cpuid0, 0); // Get the maximum supported CPUID function
   __cpuid(cpuid7, 7); // AVX2 is function 7, subfunction 0, EBX, bit 5
   if (cpuid0[0] < 7 || !(cpuid7[1] & 0x20) || !mozilla::IsWin7SP1OrLater()) {
     _set_FMA3_enable(0);