2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "CommandLine.h"
29 #include "PluginProcessMain.h"
30 #include "ProcessLauncher.h"
31 #include "WebProcessMain.h"
32 #include <wtf/text/CString.h>
35 #include <objc/objc-auto.h>
40 using namespace WebKit;
42 static int WebKitMain(const CommandLine& commandLine)
44 ProcessLauncher::ProcessType processType;
45 if (!ProcessLauncher::getProcessTypeFromString(commandLine["type"].utf8().data(), processType))
48 switch (processType) {
49 case ProcessLauncher::WebProcess:
50 return WebProcessMain(commandLine);
51 case ProcessLauncher::PluginProcess:
52 #if ENABLE(PLUGIN_PROCESS)
53 return PluginProcessMain(commandLine);
64 extern "C" WK_EXPORT int WebKitMain(int argc, char** argv);
66 int WebKitMain(int argc, char** argv)
68 ASSERT(!objc_collectingEnabled());
70 CommandLine commandLine;
71 if (!commandLine.parse(argc, argv))
74 return WebKitMain(commandLine);
79 static void enableDataExecutionPrevention()
81 // Enable Data Execution prevention at runtime rather than via /NXCOMPAT
82 // 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
84 const DWORD enableDEP = 0x00000001;
86 HMODULE hMod = ::GetModuleHandleW(L"Kernel32.dll");
90 typedef BOOL (WINAPI *PSETDEP)(DWORD);
92 PSETDEP procSet = reinterpret_cast<PSETDEP>(::GetProcAddress(hMod, "SetProcessDEPPolicy"));
96 // Enable Data Execution Prevention, but allow ATL thunks (for compatibility with the version of ATL that ships with the Platform SDK).
100 static void enableTerminationOnHeapCorruption()
102 // Enable termination on heap corruption on OSes that support it (Vista and XPSP3).
103 // http://msdn.microsoft.com/en-us/library/aa366705(VS.85).aspx
105 const HEAP_INFORMATION_CLASS heapEnableTerminationOnCorruption = static_cast<HEAP_INFORMATION_CLASS>(1);
107 HMODULE hMod = ::GetModuleHandleW(L"kernel32.dll");
111 typedef BOOL (WINAPI*HSI)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T);
112 HSI heapSetInformation = reinterpret_cast<HSI>(::GetProcAddress(hMod, "HeapSetInformation"));
113 if (!heapSetInformation)
116 heapSetInformation(0, heapEnableTerminationOnCorruption, 0, 0);
119 static void disableUserModeCallbackExceptionFilter()
121 const DWORD PROCESS_CALLBACK_FILTER_ENABLED = 0x1;
122 typedef BOOL (NTAPI *getProcessUserModeExceptionPolicyPtr)(LPDWORD lpFlags);
123 typedef BOOL (NTAPI *setProcessUserModeExceptionPolicyPtr)(DWORD dwFlags);
125 HMODULE lib = LoadLibrary(TEXT("kernel32.dll"));
128 getProcessUserModeExceptionPolicyPtr getPolicyPtr = (getProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "GetProcessUserModeExceptionPolicy");
129 setProcessUserModeExceptionPolicyPtr setPolicyPtr = (setProcessUserModeExceptionPolicyPtr)GetProcAddress(lib, "SetProcessUserModeExceptionPolicy");
132 if (!getPolicyPtr || !setPolicyPtr || !getPolicyPtr(&dwFlags)) {
137 // If this flag isn't cleared, exceptions that are thrown when running in a 64-bit version of
138 // Windows are ignored, possibly leaving Safari in an inconsistent state that could cause an
139 // unrelated exception to be thrown.
140 // http://support.microsoft.com/kb/976038
141 // http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/
142 setPolicyPtr(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED);
148 static void pauseProcessIfNeeded(HMODULE module)
150 // Show an alert when Ctrl-Alt-Shift is held down during launch to give the user time to attach a
151 // debugger. This is useful for debugging problems that happen early in the web process's lifetime.
152 const unsigned short highBitMaskShort = 0x8000;
153 if (!getenv("WEBKIT2_PAUSE_WEB_PROCESS_ON_LAUNCH") && !((::GetKeyState(VK_CONTROL) & highBitMaskShort) && (::GetKeyState(VK_MENU) & highBitMaskShort) && (::GetKeyState(VK_SHIFT) & highBitMaskShort)))
156 wchar_t path[MAX_PATH];
157 DWORD length = ::GetModuleFileNameW(module, path, WTF_ARRAY_LENGTH(path));
158 if (!length || length == WTF_ARRAY_LENGTH(path))
161 wchar_t* startOfFilename = ::PathFindFileNameW(path);
162 String filenameString(startOfFilename, length - (startOfFilename - path));
164 String message = L"You can now attach a debugger to " + filenameString + L". You can use the same debugger for " + filenameString + L" and the UI process, if desired. Click OK when you are ready for " + filenameString + L" to continue.";
165 String title = filenameString + L" has launched";
166 ::MessageBoxW(0, message.charactersWithNullTermination(), title.charactersWithNullTermination(), MB_OK | MB_ICONINFORMATION);
170 extern "C" __declspec(dllexport)
171 int WebKitMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpstrCmdLine, int nCmdShow)
174 pauseProcessIfNeeded(hInstance);
177 enableDataExecutionPrevention();
179 enableTerminationOnHeapCorruption();
181 disableUserModeCallbackExceptionFilter();
183 CommandLine commandLine;
184 if (!commandLine.parse(lpstrCmdLine))
187 return WebKitMain(commandLine);