1/* 2 * Copyright (C) 2010 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "CommandLine.h" 28 29#include "PluginProcessMain.h" 30#include "ProcessLauncher.h" 31#include "WebProcessMain.h" 32#include <wtf/text/CString.h> 33 34#if PLATFORM(MAC) 35#include <objc/objc-auto.h> 36#endif 37 38using namespace WebKit; 39 40static int WebKitMain(const CommandLine& commandLine) 41{ 42 ProcessLauncher::ProcessType processType; 43 if (!ProcessLauncher::getProcessTypeFromString(commandLine["type"].utf8().data(), processType)) 44 return EXIT_FAILURE; 45 46 switch (processType) { 47 case ProcessLauncher::WebProcess: 48 return WebProcessMain(commandLine); 49 case ProcessLauncher::PluginProcess: 50#if ENABLE(PLUGIN_PROCESS) 51 return PluginProcessMain(commandLine); 52#else 53 break; 54#endif 55 } 56 57 return EXIT_FAILURE; 58} 59 60#if PLATFORM(MAC) 61 62extern "C" WK_EXPORT int WebKitMain(int argc, char** argv); 63 64int WebKitMain(int argc, char** argv) 65{ 66 ASSERT(!objc_collectingEnabled()); 67 68 CommandLine commandLine; 69 if (!commandLine.parse(argc, argv)) 70 return EXIT_FAILURE; 71 72 return WebKitMain(commandLine); 73} 74 75#elif PLATFORM(WIN) 76 77#ifndef DEBUG_ALL 78#define PROCESS_NAME L"WebKit2WebKitProcess.exe" 79#else 80#define PROCESS_NAME L"WebKit2WebProcess_debug.exe" 81#endif 82 83static void enableDataExecutionPrevention() 84{ 85 // Enable Data Execution prevention at runtime rather than via /NXCOMPAT 86 // http://blogs.msdn.com/michael_howard/archive/2008/01/29/new-nx-apis-added-to-windows-vista-sp1-windows-xp-sp3-and-windows-server-2008.aspx 87 88 const DWORD enableDEP = 0x00000001; 89 90 HMODULE hMod = ::GetModuleHandleW(L"Kernel32.dll"); 91 if (!hMod) 92 return; 93 94 typedef BOOL (WINAPI *PSETDEP)(DWORD); 95 96 PSETDEP procSet = reinterpret_cast<PSETDEP>(::GetProcAddress(hMod, "SetProcessDEPPolicy")); 97 if (!procSet) 98 return; 99 100 // Enable Data Execution Prevention, but allow ATL thunks (for compatibility with the version of ATL that ships with the Platform SDK). 101 procSet(enableDEP); 102} 103 104static void enableTerminationOnHeapCorruption() 105{ 106 // Enable termination on heap corruption on OSes that support it (Vista and XPSP3). 107 // http://msdn.microsoft.com/en-us/library/aa366705(VS.85).aspx 108 109 const HEAP_INFORMATION_CLASS heapEnableTerminationOnCorruption = static_cast<HEAP_INFORMATION_CLASS>(1); 110 111 HMODULE hMod = ::GetModuleHandleW(L"kernel32.dll"); 112 if (!hMod) 113 return; 114 115 typedef BOOL (WINAPI*HSI)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); 116 HSI heapSetInformation = reinterpret_cast<HSI>(::GetProcAddress(hMod, "HeapSetInformation")); 117 if (!heapSetInformation) 118 return; 119 120 heapSetInformation(0, heapEnableTerminationOnCorruption, 0, 0); 121} 122 123static void disableUserModeCallbackExceptionFilter() 124{ 125 const DWORD PROCESS_CALLBACK_FILTER_ENABLED = 0x1; 126 typedef BOOL (NTAPI *getProcessUserModeExceptionPolicyPtr)(LPDWORD lpFlags); 127 typedef BOOL (NTAPI *setProcessUserModeExceptionPolicyPtr)(DWORD dwFlags); 128 129 HMODULE lib = LoadLibrary(TEXT("kernel32.dll")); 130 ASSERT(lib); 131 132 getProcessUserModeExceptionPolicyPtr getPolicyPtr = (getProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "GetProcessUserModeExceptionPolicy"); 133 setProcessUserModeExceptionPolicyPtr setPolicyPtr = (setProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "SetProcessUserModeExceptionPolicy"); 134 135 DWORD dwFlags; 136 if (!getPolicyPtr || !setPolicyPtr || !getPolicyPtr(&dwFlags)) { 137 FreeLibrary(lib); 138 return; 139 } 140 141 // If this flag isn't cleared, exceptions that are thrown when running in a 64-bit version of 142 // Windows are ignored, possibly leaving Safari in an inconsistent state that could cause an 143 // unrelated exception to be thrown. 144 // http://support.microsoft.com/kb/976038 145 // http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/ 146 setPolicyPtr(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED); 147 148 FreeLibrary(lib); 149} 150 151extern "C" __declspec(dllexport) 152int WebKitMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow) 153{ 154#ifndef NDEBUG 155 // Show an alert when Ctrl-Alt-Shift is held down during launch to give the user time to attach a 156 // debugger. This is useful for debugging problems that happen early in the web process's lifetime. 157 const unsigned short highBitMaskShort = 0x8000; 158 if (getenv("WEBKIT2_PAUSE_WEB_PROCESS_ON_LAUNCH") || (::GetKeyState(VK_CONTROL) & highBitMaskShort) && (::GetKeyState(VK_MENU) & highBitMaskShort) && (::GetKeyState(VK_SHIFT) & highBitMaskShort)) 159 ::MessageBoxW(0, L"You can now attach a debugger to " PROCESS_NAME L". You can use\nthe same debugger for WebKit2WebProcessand the UI process, if desired.\nClick OK when you are ready for WebKit2WebProcess to continue.", L"WebKit2WebProcess has launched", MB_OK | MB_ICONINFORMATION); 160#endif 161 162 enableDataExecutionPrevention(); 163 164 enableTerminationOnHeapCorruption(); 165 166 disableUserModeCallbackExceptionFilter(); 167 168 CommandLine commandLine; 169 if (!commandLine.parse(lpstrCmdLine)) 170 return EXIT_FAILURE; 171 172 return WebKitMain(commandLine); 173} 174 175#endif 176