VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/init-win.cpp@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.0 KB
Line 
1/* $Id: init-win.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - Init Ring-3, Windows Specific Code.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DEFAULT
32#include <iprt/nt/nt-and-windows.h>
33#ifndef LOAD_LIBRARY_SEARCH_APPLICATION_DIR
34# define LOAD_LIBRARY_SEARCH_APPLICATION_DIR 0x200
35# define LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
36#endif
37
38#include "internal-r3-win.h"
39#include <iprt/initterm.h>
40#include <iprt/assert.h>
41#include <iprt/err.h>
42#include <iprt/ldr.h>
43#include <iprt/log.h>
44#include <iprt/param.h>
45#include <iprt/string.h>
46#include <iprt/thread.h>
47#include "../init.h"
48
49
50/*********************************************************************************************************************************
51* Structures and Typedefs *
52*********************************************************************************************************************************/
53typedef VOID (WINAPI *PFNGETCURRENTTHREADSTACKLIMITS)(PULONG_PTR puLow, PULONG_PTR puHigh);
54typedef LPTOP_LEVEL_EXCEPTION_FILTER (WINAPI * PFNSETUNHANDLEDEXCEPTIONFILTER)(LPTOP_LEVEL_EXCEPTION_FILTER);
55
56
57/*********************************************************************************************************************************
58* Global Variables *
59*********************************************************************************************************************************/
60/** Windows DLL loader protection level. */
61DECLHIDDEN(RTR3WINLDRPROT) g_enmWinLdrProt = RTR3WINLDRPROT_NONE;
62/** Our simplified windows version. */
63DECLHIDDEN(RTWINOSTYPE) g_enmWinVer = kRTWinOSType_UNKNOWN;
64/** Extended windows version information. */
65DECLHIDDEN(OSVERSIONINFOEXW) g_WinOsInfoEx;
66
67/** The native kernel32.dll handle. */
68DECLHIDDEN(HMODULE) g_hModKernel32 = NULL;
69/** GetSystemWindowsDirectoryW or GetWindowsDirectoryW (NT4). */
70DECLHIDDEN(PFNGETWINSYSDIR) g_pfnGetSystemWindowsDirectoryW = NULL;
71/** The GetCurrentThreadStackLimits API. */
72static PFNGETCURRENTTHREADSTACKLIMITS g_pfnGetCurrentThreadStackLimits = NULL;
73/** SetUnhandledExceptionFilter. */
74static PFNSETUNHANDLEDEXCEPTIONFILTER g_pfnSetUnhandledExceptionFilter = NULL;
75/** The previous unhandled exception filter. */
76static LPTOP_LEVEL_EXCEPTION_FILTER g_pfnUnhandledXcptFilter = NULL;
77/** SystemTimeToTzSpecificLocalTime. */
78decltype(SystemTimeToTzSpecificLocalTime) *g_pfnSystemTimeToTzSpecificLocalTime = NULL;
79
80/** The native ntdll.dll handle. */
81DECLHIDDEN(HMODULE) g_hModNtDll = NULL;
82/** NtQueryFullAttributesFile */
83DECLHIDDEN(PFNNTQUERYFULLATTRIBUTESFILE) g_pfnNtQueryFullAttributesFile = NULL;
84/** NtDuplicateToken (NT 3.51). */
85DECLHIDDEN(PFNNTDUPLICATETOKEN) g_pfnNtDuplicateToken = NULL;
86/** NtAlertThread (NT 3.51). */
87decltype(NtAlertThread) *g_pfnNtAlertThread = NULL;
88
89/** Either ws2_32.dll (NT4+) or wsock32.dll (NT3.x). */
90DECLHIDDEN(HMODULE) g_hModWinSock = NULL;
91/** Set if we're dealing with old winsock. */
92DECLHIDDEN(bool) g_fOldWinSock = false;
93/** WSAStartup */
94DECLHIDDEN(PFNWSASTARTUP) g_pfnWSAStartup = NULL;
95/** WSACleanup */
96DECLHIDDEN(PFNWSACLEANUP) g_pfnWSACleanup = NULL;
97/** Pointner to WSAGetLastError (for RTErrVarsSave). */
98DECLHIDDEN(PFNWSAGETLASTERROR) g_pfnWSAGetLastError = NULL;
99/** Pointner to WSASetLastError (for RTErrVarsRestore). */
100DECLHIDDEN(PFNWSASETLASTERROR) g_pfnWSASetLastError = NULL;
101/** WSACreateEvent */
102DECLHIDDEN(PFNWSACREATEEVENT) g_pfnWSACreateEvent = NULL;
103/** WSACloseEvent */
104DECLHIDDEN(PFNWSACLOSEEVENT) g_pfnWSACloseEvent = NULL;
105/** WSASetEvent */
106DECLHIDDEN(PFNWSASETEVENT) g_pfnWSASetEvent = NULL;
107/** WSAEventSelect */
108DECLHIDDEN(PFNWSAEVENTSELECT) g_pfnWSAEventSelect = NULL;
109/** WSAEnumNetworkEvents */
110DECLHIDDEN(PFNWSAENUMNETWORKEVENTS) g_pfnWSAEnumNetworkEvents = NULL;
111/** WSASend */
112DECLHIDDEN(PFNWSASend) g_pfnWSASend = NULL;
113/** socket */
114DECLHIDDEN(PFNWINSOCKSOCKET) g_pfnsocket = NULL;
115/** closesocket */
116DECLHIDDEN(PFNWINSOCKCLOSESOCKET) g_pfnclosesocket = NULL;
117/** recv */
118DECLHIDDEN(PFNWINSOCKRECV) g_pfnrecv = NULL;
119/** send */
120DECLHIDDEN(PFNWINSOCKSEND) g_pfnsend = NULL;
121/** recvfrom */
122DECLHIDDEN(PFNWINSOCKRECVFROM) g_pfnrecvfrom = NULL;
123/** sendto */
124DECLHIDDEN(PFNWINSOCKSENDTO) g_pfnsendto = NULL;
125/** bind */
126DECLHIDDEN(PFNWINSOCKBIND) g_pfnbind = NULL;
127/** listen */
128DECLHIDDEN(PFNWINSOCKLISTEN) g_pfnlisten = NULL;
129/** accept */
130DECLHIDDEN(PFNWINSOCKACCEPT) g_pfnaccept = NULL;
131/** connect */
132DECLHIDDEN(PFNWINSOCKCONNECT) g_pfnconnect = NULL;
133/** shutdown */
134DECLHIDDEN(PFNWINSOCKSHUTDOWN) g_pfnshutdown = NULL;
135/** getsockopt */
136DECLHIDDEN(PFNWINSOCKGETSOCKOPT) g_pfngetsockopt = NULL;
137/** setsockopt */
138DECLHIDDEN(PFNWINSOCKSETSOCKOPT) g_pfnsetsockopt = NULL;
139/** ioctlsocket */
140DECLHIDDEN(PFNWINSOCKIOCTLSOCKET) g_pfnioctlsocket = NULL;
141/** getpeername */
142DECLHIDDEN(PFNWINSOCKGETPEERNAME) g_pfngetpeername = NULL;
143/** getsockname */
144DECLHIDDEN(PFNWINSOCKGETSOCKNAME) g_pfngetsockname = NULL;
145/** __WSAFDIsSet */
146DECLHIDDEN(PFNWINSOCK__WSAFDISSET) g_pfn__WSAFDIsSet = NULL;
147/** select */
148DECLHIDDEN(PFNWINSOCKSELECT) g_pfnselect = NULL;
149/** gethostbyname */
150DECLHIDDEN(PFNWINSOCKGETHOSTBYNAME) g_pfngethostbyname = NULL;
151
152
153/*********************************************************************************************************************************
154* Internal Functions *
155*********************************************************************************************************************************/
156static LONG CALLBACK rtR3WinUnhandledXcptFilter(PEXCEPTION_POINTERS);
157
158
159/**
160 * Translates OSVERSIONINOFEX into a Windows OS type.
161 *
162 * @returns The Windows OS type.
163 * @param pOSInfoEx The OS info returned by Windows.
164 *
165 * @remarks This table has been assembled from Usenet postings, personal
166 * observations, and reading other people's code. Please feel
167 * free to add to it or correct it.
168 * <pre>
169 dwPlatFormID dwMajorVersion dwMinorVersion dwBuildNumber
17095 1 4 0 950
17195 SP1 1 4 0 >950 && <=1080
17295 OSR2 1 4 <10 >1080
17398 1 4 10 1998
17498 SP1 1 4 10 >1998 && <2183
17598 SE 1 4 10 >=2183
176ME 1 4 90 3000
177
178NT 3.51 2 3 51 1057
179NT 4 2 4 0 1381
1802000 2 5 0 2195
181XP 2 5 1 2600
1822003 2 5 2 3790
183Vista 2 6 0
184
185CE 1.0 3 1 0
186CE 2.0 3 2 0
187CE 2.1 3 2 1
188CE 3.0 3 3 0
189</pre>
190 */
191static RTWINOSTYPE rtR3InitWinSimplifiedVersion(OSVERSIONINFOEXW const *pOSInfoEx)
192{
193 RTWINOSTYPE enmVer = kRTWinOSType_UNKNOWN;
194 BYTE const bProductType = pOSInfoEx->wProductType;
195 DWORD const dwPlatformId = pOSInfoEx->dwPlatformId;
196 DWORD const dwMinorVersion = pOSInfoEx->dwMinorVersion;
197 DWORD const dwMajorVersion = pOSInfoEx->dwMajorVersion;
198 DWORD const dwBuildNumber = pOSInfoEx->dwBuildNumber & 0xFFFF; /* Win 9x needs this. */
199
200 if ( dwPlatformId == VER_PLATFORM_WIN32_WINDOWS
201 && dwMajorVersion == 4)
202 {
203 if ( dwMinorVersion < 10
204 && dwBuildNumber == 950)
205 enmVer = kRTWinOSType_95;
206 else if ( dwMinorVersion < 10
207 && dwBuildNumber > 950
208 && dwBuildNumber <= 1080)
209 enmVer = kRTWinOSType_95SP1;
210 else if ( dwMinorVersion < 10
211 && dwBuildNumber > 1080)
212 enmVer = kRTWinOSType_95OSR2;
213 else if ( dwMinorVersion == 10
214 && dwBuildNumber == 1998)
215 enmVer = kRTWinOSType_98;
216 else if ( dwMinorVersion == 10
217 && dwBuildNumber > 1998
218 && dwBuildNumber < 2183)
219 enmVer = kRTWinOSType_98SP1;
220 else if ( dwMinorVersion == 10
221 && dwBuildNumber >= 2183)
222 enmVer = kRTWinOSType_98SE;
223 else if (dwMinorVersion == 90)
224 enmVer = kRTWinOSType_ME;
225 }
226 else if (dwPlatformId == VER_PLATFORM_WIN32_NT)
227 {
228 if (dwMajorVersion == 3)
229 {
230 if ( dwMinorVersion < 50)
231 enmVer = kRTWinOSType_NT310;
232 else if (dwMinorVersion == 50)
233 enmVer = kRTWinOSType_NT350;
234 else
235 enmVer = kRTWinOSType_NT351;
236 }
237 else if (dwMajorVersion == 4)
238 enmVer = kRTWinOSType_NT4;
239 else if (dwMajorVersion == 5)
240 {
241 if (dwMinorVersion == 0)
242 enmVer = kRTWinOSType_2K;
243 else if (dwMinorVersion == 1)
244 enmVer = kRTWinOSType_XP;
245 else
246 enmVer = kRTWinOSType_2003;
247 }
248 else if (dwMajorVersion == 6)
249 {
250 if (dwMinorVersion == 0)
251 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2008 : kRTWinOSType_VISTA;
252 else if (dwMinorVersion == 1)
253 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2008R2 : kRTWinOSType_7;
254 else if (dwMinorVersion == 2)
255 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2012 : kRTWinOSType_8;
256 else if (dwMinorVersion == 3)
257 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2012R2 : kRTWinOSType_81;
258 else if (dwMinorVersion == 4)
259 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2016 : kRTWinOSType_10;
260 else
261 enmVer = kRTWinOSType_NT_UNKNOWN;
262 }
263 else if (dwMajorVersion == 10)
264 {
265 if (dwMinorVersion == 0)
266 enmVer = bProductType != VER_NT_WORKSTATION ? kRTWinOSType_2016 : kRTWinOSType_10;
267 else
268 enmVer = kRTWinOSType_NT_UNKNOWN;
269 }
270 else
271 enmVer = kRTWinOSType_NT_UNKNOWN;
272 }
273
274 return enmVer;
275}
276
277
278/**
279 * Initializes the global variables related to windows version.
280 */
281static void rtR3InitWindowsVersion(void)
282{
283 Assert(g_hModNtDll != NULL);
284
285 /*
286 * ASSUMES OSVERSIONINFOEX starts with the exact same layout as OSVERSIONINFO (safe).
287 */
288 AssertCompileMembersSameSizeAndOffset(OSVERSIONINFOEX, szCSDVersion, OSVERSIONINFO, szCSDVersion);
289 AssertCompileMemberOffset(OSVERSIONINFOEX, wServicePackMajor, sizeof(OSVERSIONINFO));
290
291 /*
292 * Use the NT version of GetVersionExW so we don't get fooled by
293 * compatability shims.
294 */
295 RT_ZERO(g_WinOsInfoEx);
296 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
297
298 LONG (__stdcall *pfnRtlGetVersion)(OSVERSIONINFOEXW *);
299 *(FARPROC *)&pfnRtlGetVersion = GetProcAddress(g_hModNtDll, "RtlGetVersion");
300 LONG rcNt = -1;
301 if (pfnRtlGetVersion)
302 rcNt = pfnRtlGetVersion(&g_WinOsInfoEx);
303 if (rcNt != 0)
304 {
305 /*
306 * Couldn't find it or it failed, try the windows version of the API.
307 */
308 RT_ZERO(g_WinOsInfoEx);
309 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
310 if (!GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
311 {
312 /*
313 * If that didn't work either, just get the basic version bits.
314 */
315 RT_ZERO(g_WinOsInfoEx);
316 g_WinOsInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
317 if (GetVersionExW((POSVERSIONINFOW)&g_WinOsInfoEx))
318 Assert(g_WinOsInfoEx.dwPlatformId != VER_PLATFORM_WIN32_NT || g_WinOsInfoEx.dwMajorVersion < 5);
319 else
320 {
321 AssertBreakpoint();
322 RT_ZERO(g_WinOsInfoEx);
323 }
324 }
325 }
326
327 if (g_WinOsInfoEx.dwOSVersionInfoSize)
328 g_enmWinVer = rtR3InitWinSimplifiedVersion(&g_WinOsInfoEx);
329}
330
331
332/**
333 * Resolves the winsock error APIs.
334 */
335static void rtR3InitWinSockApis(void)
336{
337 /*
338 * Try get ws2_32.dll, then try load it, then finally fall back to the old
339 * wsock32.dll. We use RTLdrLoadSystem to the loading as it has all the fancy
340 * logic for safely doing that.
341 */
342 g_hModWinSock = GetModuleHandleW(L"ws2_32.dll");
343 if (g_hModWinSock == NULL)
344 {
345 RTLDRMOD hLdrMod;
346 int rc = RTLdrLoadSystem("ws2_32.dll", true /*fNoUnload*/, &hLdrMod);
347 if (RT_FAILURE(rc))
348 {
349 rc = RTLdrLoadSystem("wsock32.dll", true /*fNoUnload*/, &hLdrMod);
350 if (RT_FAILURE(rc))
351 {
352 AssertMsgFailed(("rc=%Rrc\n", rc));
353 return;
354 }
355 g_fOldWinSock = true;
356 }
357 g_hModWinSock = (HMODULE)RTLdrGetNativeHandle(hLdrMod);
358 RTLdrClose(hLdrMod);
359 }
360
361 g_pfnWSAStartup = (decltype(g_pfnWSAStartup)) GetProcAddress(g_hModWinSock, "WSAStartup");
362 g_pfnWSACleanup = (decltype(g_pfnWSACleanup)) GetProcAddress(g_hModWinSock, "WSACleanup");
363 g_pfnWSAGetLastError = (decltype(g_pfnWSAGetLastError)) GetProcAddress(g_hModWinSock, "WSAGetLastError");
364 g_pfnWSASetLastError = (decltype(g_pfnWSASetLastError)) GetProcAddress(g_hModWinSock, "WSASetLastError");
365 g_pfnWSACreateEvent = (decltype(g_pfnWSACreateEvent)) GetProcAddress(g_hModWinSock, "WSACreateEvent");
366 g_pfnWSACloseEvent = (decltype(g_pfnWSACloseEvent)) GetProcAddress(g_hModWinSock, "WSACloseEvent");
367 g_pfnWSASetEvent = (decltype(g_pfnWSASetEvent)) GetProcAddress(g_hModWinSock, "WSASetEvent");
368 g_pfnWSAEventSelect = (decltype(g_pfnWSAEventSelect)) GetProcAddress(g_hModWinSock, "WSAEventSelect");
369 g_pfnWSAEnumNetworkEvents = (decltype(g_pfnWSAEnumNetworkEvents))GetProcAddress(g_hModWinSock,"WSAEnumNetworkEvents");
370 g_pfnWSASend = (decltype(g_pfnWSASend)) GetProcAddress(g_hModWinSock, "WSASend");
371 g_pfnsocket = (decltype(g_pfnsocket)) GetProcAddress(g_hModWinSock, "socket");
372 g_pfnclosesocket = (decltype(g_pfnclosesocket)) GetProcAddress(g_hModWinSock, "closesocket");
373 g_pfnrecv = (decltype(g_pfnrecv)) GetProcAddress(g_hModWinSock, "recv");
374 g_pfnsend = (decltype(g_pfnsend)) GetProcAddress(g_hModWinSock, "send");
375 g_pfnrecvfrom = (decltype(g_pfnrecvfrom)) GetProcAddress(g_hModWinSock, "recvfrom");
376 g_pfnsendto = (decltype(g_pfnsendto)) GetProcAddress(g_hModWinSock, "sendto");
377 g_pfnbind = (decltype(g_pfnbind)) GetProcAddress(g_hModWinSock, "bind");
378 g_pfnlisten = (decltype(g_pfnlisten)) GetProcAddress(g_hModWinSock, "listen");
379 g_pfnaccept = (decltype(g_pfnaccept)) GetProcAddress(g_hModWinSock, "accept");
380 g_pfnconnect = (decltype(g_pfnconnect)) GetProcAddress(g_hModWinSock, "connect");
381 g_pfnshutdown = (decltype(g_pfnshutdown)) GetProcAddress(g_hModWinSock, "shutdown");
382 g_pfngetsockopt = (decltype(g_pfngetsockopt)) GetProcAddress(g_hModWinSock, "getsockopt");
383 g_pfnsetsockopt = (decltype(g_pfnsetsockopt)) GetProcAddress(g_hModWinSock, "setsockopt");
384 g_pfnioctlsocket = (decltype(g_pfnioctlsocket)) GetProcAddress(g_hModWinSock, "ioctlsocket");
385 g_pfngetpeername = (decltype(g_pfngetpeername)) GetProcAddress(g_hModWinSock, "getpeername");
386 g_pfngetsockname = (decltype(g_pfngetsockname)) GetProcAddress(g_hModWinSock, "getsockname");
387 g_pfn__WSAFDIsSet = (decltype(g_pfn__WSAFDIsSet)) GetProcAddress(g_hModWinSock, "__WSAFDIsSet");
388 g_pfnselect = (decltype(g_pfnselect)) GetProcAddress(g_hModWinSock, "select");
389 g_pfngethostbyname = (decltype(g_pfngethostbyname)) GetProcAddress(g_hModWinSock, "gethostbyname");
390
391 Assert(g_pfnWSAStartup);
392 Assert(g_pfnWSACleanup);
393 Assert(g_pfnWSAGetLastError);
394 Assert(g_pfnWSASetLastError);
395 Assert(g_pfnWSACreateEvent || g_fOldWinSock);
396 Assert(g_pfnWSACloseEvent || g_fOldWinSock);
397 Assert(g_pfnWSASetEvent || g_fOldWinSock);
398 Assert(g_pfnWSAEventSelect || g_fOldWinSock);
399 Assert(g_pfnWSAEnumNetworkEvents || g_fOldWinSock);
400 Assert(g_pfnWSASend || g_fOldWinSock);
401 Assert(g_pfnsocket);
402 Assert(g_pfnclosesocket);
403 Assert(g_pfnrecv);
404 Assert(g_pfnsend);
405 Assert(g_pfnrecvfrom);
406 Assert(g_pfnsendto);
407 Assert(g_pfnbind);
408 Assert(g_pfnlisten);
409 Assert(g_pfnaccept);
410 Assert(g_pfnconnect);
411 Assert(g_pfnshutdown);
412 Assert(g_pfngetsockopt);
413 Assert(g_pfnsetsockopt);
414 Assert(g_pfnioctlsocket);
415 Assert(g_pfngetpeername);
416 Assert(g_pfngetsockname);
417 Assert(g_pfn__WSAFDIsSet);
418 Assert(g_pfnselect);
419 Assert(g_pfngethostbyname);
420}
421
422
423static int rtR3InitNativeObtrusiveWorker(uint32_t fFlags)
424{
425 /*
426 * Disable error popups.
427 */
428 UINT fOldErrMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
429 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX | fOldErrMode);
430
431 /*
432 * Restrict DLL searching for the process on windows versions which allow
433 * us to do so.
434 * - The first trick works on XP SP1+ and disables the searching of the
435 * current directory.
436 * - The second trick is W7 w/ KB2533623 and W8+, it restrict the DLL
437 * searching to the application directory (except when
438 * RTR3INIT_FLAGS_STANDALONE_APP is given) and the System32 directory.
439 */
440 int rc = VINF_SUCCESS;
441
442 typedef BOOL (WINAPI *PFNSETDLLDIRECTORY)(LPCWSTR);
443 PFNSETDLLDIRECTORY pfnSetDllDir = (PFNSETDLLDIRECTORY)GetProcAddress(g_hModKernel32, "SetDllDirectoryW");
444 if (pfnSetDllDir)
445 {
446 if (pfnSetDllDir(L""))
447 g_enmWinLdrProt = RTR3WINLDRPROT_NO_CWD;
448 else
449 rc = VERR_INTERNAL_ERROR_3;
450 }
451
452 /** @bugref{6861} Observed GUI issues on Vista (32-bit and 64-bit) when using
453 * SetDefaultDllDirectories.
454 * @bugref{8194} Try use SetDefaultDllDirectories on Vista for standalone apps
455 * despite potential GUI issues. */
456 if ( g_enmWinVer > kRTWinOSType_VISTA
457 || (fFlags & RTR3INIT_FLAGS_STANDALONE_APP))
458 {
459 typedef BOOL(WINAPI *PFNSETDEFAULTDLLDIRECTORIES)(DWORD);
460 PFNSETDEFAULTDLLDIRECTORIES pfnSetDefDllDirs;
461 pfnSetDefDllDirs = (PFNSETDEFAULTDLLDIRECTORIES)GetProcAddress(g_hModKernel32, "SetDefaultDllDirectories");
462 if (pfnSetDefDllDirs)
463 {
464 DWORD fDllDirs = LOAD_LIBRARY_SEARCH_SYSTEM32;
465 if (!(fFlags & RTR3INIT_FLAGS_STANDALONE_APP))
466 fDllDirs |= LOAD_LIBRARY_SEARCH_APPLICATION_DIR;
467 if (pfnSetDefDllDirs(fDllDirs))
468 g_enmWinLdrProt = fDllDirs & LOAD_LIBRARY_SEARCH_APPLICATION_DIR ? RTR3WINLDRPROT_SAFE : RTR3WINLDRPROT_SAFER;
469 else if (RT_SUCCESS(rc))
470 rc = VERR_INTERNAL_ERROR_4;
471 }
472 }
473
474 /*
475 * Register an unhandled exception callback if we can.
476 */
477 g_pfnGetCurrentThreadStackLimits = (PFNGETCURRENTTHREADSTACKLIMITS)GetProcAddress(g_hModKernel32, "GetCurrentThreadStackLimits");
478 g_pfnSetUnhandledExceptionFilter = (PFNSETUNHANDLEDEXCEPTIONFILTER)GetProcAddress(g_hModKernel32, "SetUnhandledExceptionFilter");
479 if (g_pfnSetUnhandledExceptionFilter && !g_pfnUnhandledXcptFilter)
480 {
481 g_pfnUnhandledXcptFilter = g_pfnSetUnhandledExceptionFilter(rtR3WinUnhandledXcptFilter);
482 AssertStmt(g_pfnUnhandledXcptFilter != rtR3WinUnhandledXcptFilter, g_pfnUnhandledXcptFilter = NULL);
483 }
484
485 return rc;
486}
487
488
489DECLHIDDEN(int) rtR3InitNativeFirst(uint32_t fFlags)
490{
491 /*
492 * Make sure we've got the handles of the two main Windows NT dlls.
493 */
494 g_hModKernel32 = GetModuleHandleW(L"kernel32.dll");
495 if (g_hModKernel32 == NULL)
496 return VERR_INTERNAL_ERROR_2;
497 g_hModNtDll = GetModuleHandleW(L"ntdll.dll");
498 if (g_hModNtDll == NULL)
499 return VERR_INTERNAL_ERROR_2;
500
501 rtR3InitWindowsVersion();
502
503 int rc = VINF_SUCCESS;
504 if (!(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
505 rc = rtR3InitNativeObtrusiveWorker(fFlags);
506
507 /*
508 * Resolve some kernel32.dll APIs we may need but aren't necessarily
509 * present in older windows versions.
510 */
511 g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetSystemWindowsDirectoryW");
512 if (g_pfnGetSystemWindowsDirectoryW)
513 g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetWindowsDirectoryW");
514 g_pfnSystemTimeToTzSpecificLocalTime = (decltype(SystemTimeToTzSpecificLocalTime) *)GetProcAddress(g_hModKernel32, "SystemTimeToTzSpecificLocalTime");
515
516 /*
517 * Resolve some ntdll.dll APIs that weren't there in early NT versions.
518 */
519 g_pfnNtQueryFullAttributesFile = (PFNNTQUERYFULLATTRIBUTESFILE)GetProcAddress(g_hModNtDll, "NtQueryFullAttributesFile");
520 g_pfnNtDuplicateToken = (PFNNTDUPLICATETOKEN)GetProcAddress( g_hModNtDll, "NtDuplicateToken");
521 g_pfnNtAlertThread = (decltype(NtAlertThread) *)GetProcAddress( g_hModNtDll, "NtAlertThread");
522
523 /*
524 * Resolve the winsock error getter and setter so assertions can save those too.
525 */
526 rtR3InitWinSockApis();
527
528 return rc;
529}
530
531
532DECLHIDDEN(void) rtR3InitNativeObtrusive(uint32_t fFlags)
533{
534 rtR3InitNativeObtrusiveWorker(fFlags);
535}
536
537
538DECLHIDDEN(int) rtR3InitNativeFinal(uint32_t fFlags)
539{
540 /* Nothing to do here. */
541 RT_NOREF_PV(fFlags);
542 return VINF_SUCCESS;
543}
544
545
546/**
547 * Unhandled exception filter callback.
548 *
549 * Will try log stuff.
550 */
551static LONG CALLBACK rtR3WinUnhandledXcptFilter(PEXCEPTION_POINTERS pPtrs)
552{
553 /*
554 * Try get the logger and log exception details.
555 *
556 * Note! We'll be using RTLogLogger for now, though we should probably add
557 * a less deadlock prone API here and gives up pretty fast if it
558 * cannot get the lock...
559 */
560 PRTLOGGER pLogger = RTLogRelGetDefaultInstance();
561 if (!pLogger)
562 pLogger = RTLogGetDefaultInstance();
563 if (pLogger)
564 {
565 RTLogLogger(pLogger, NULL, "\n!!! rtR3WinUnhandledXcptFilter caught an exception on thread %p!!!\n", RTThreadNativeSelf());
566
567 /*
568 * Dump the exception record.
569 */
570 uintptr_t uXcptPC = 0;
571 PEXCEPTION_RECORD pXcptRec = RT_VALID_PTR(pPtrs) && RT_VALID_PTR(pPtrs->ExceptionRecord) ? pPtrs->ExceptionRecord : NULL;
572 if (pXcptRec)
573 {
574 RTLogLogger(pLogger, NULL, "\nExceptionCode=%#010x ExceptionFlags=%#010x ExceptionAddress=%p\n",
575 pXcptRec->ExceptionCode, pXcptRec->ExceptionFlags, pXcptRec->ExceptionAddress);
576 for (uint32_t i = 0; i < RT_MIN(pXcptRec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS); i++)
577 RTLogLogger(pLogger, NULL, "ExceptionInformation[%d]=%p\n", i, pXcptRec->ExceptionInformation[i]);
578 uXcptPC = (uintptr_t)pXcptRec->ExceptionAddress;
579
580 /* Nested? Display one level only. */
581 PEXCEPTION_RECORD pNestedRec = pXcptRec->ExceptionRecord;
582 if (RT_VALID_PTR(pNestedRec))
583 {
584 RTLogLogger(pLogger, NULL, "Nested: ExceptionCode=%#010x ExceptionFlags=%#010x ExceptionAddress=%p (nested %p)\n",
585 pNestedRec->ExceptionCode, pNestedRec->ExceptionFlags, pNestedRec->ExceptionAddress,
586 pNestedRec->ExceptionRecord);
587 for (uint32_t i = 0; i < RT_MIN(pNestedRec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS); i++)
588 RTLogLogger(pLogger, NULL, "Nested: ExceptionInformation[%d]=%p\n", i, pNestedRec->ExceptionInformation[i]);
589 uXcptPC = (uintptr_t)pNestedRec->ExceptionAddress;
590 }
591 }
592
593 /*
594 * Dump the context record.
595 */
596 volatile char szMarker[] = "stackmarker";
597 uintptr_t uXcptSP = (uintptr_t)&szMarker[0];
598 PCONTEXT pXcptCtx = RT_VALID_PTR(pPtrs) && RT_VALID_PTR(pPtrs->ContextRecord) ? pPtrs->ContextRecord : NULL;
599 if (pXcptCtx)
600 {
601#ifdef RT_ARCH_AMD64
602 RTLogLogger(pLogger, NULL, "\ncs:rip=%04x:%016RX64\n", pXcptCtx->SegCs, pXcptCtx->Rip);
603 RTLogLogger(pLogger, NULL, "ss:rsp=%04x:%016RX64 rbp=%016RX64\n", pXcptCtx->SegSs, pXcptCtx->Rsp, pXcptCtx->Rbp);
604 RTLogLogger(pLogger, NULL, "rax=%016RX64 rcx=%016RX64 rdx=%016RX64 rbx=%016RX64\n",
605 pXcptCtx->Rax, pXcptCtx->Rcx, pXcptCtx->Rdx, pXcptCtx->Rbx);
606 RTLogLogger(pLogger, NULL, "rsi=%016RX64 rdi=%016RX64 rsp=%016RX64 rbp=%016RX64\n",
607 pXcptCtx->Rsi, pXcptCtx->Rdi, pXcptCtx->Rsp, pXcptCtx->Rbp);
608 RTLogLogger(pLogger, NULL, "r8 =%016RX64 r9 =%016RX64 r10=%016RX64 r11=%016RX64\n",
609 pXcptCtx->R8, pXcptCtx->R9, pXcptCtx->R10, pXcptCtx->R11);
610 RTLogLogger(pLogger, NULL, "r12=%016RX64 r13=%016RX64 r14=%016RX64 r15=%016RX64\n",
611 pXcptCtx->R12, pXcptCtx->R13, pXcptCtx->R14, pXcptCtx->R15);
612 RTLogLogger(pLogger, NULL, "ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n",
613 pXcptCtx->SegDs, pXcptCtx->SegEs, pXcptCtx->SegFs, pXcptCtx->SegGs, pXcptCtx->EFlags);
614 RTLogLogger(pLogger, NULL, "p1home=%016RX64 p2home=%016RX64 pe3home=%016RX64\n",
615 pXcptCtx->P1Home, pXcptCtx->P2Home, pXcptCtx->P3Home);
616 RTLogLogger(pLogger, NULL, "p4home=%016RX64 p5home=%016RX64 pe6home=%016RX64\n",
617 pXcptCtx->P4Home, pXcptCtx->P5Home, pXcptCtx->P6Home);
618 RTLogLogger(pLogger, NULL, " LastBranchToRip=%016RX64 LastBranchFromRip=%016RX64\n",
619 pXcptCtx->LastBranchToRip, pXcptCtx->LastBranchFromRip);
620 RTLogLogger(pLogger, NULL, "LastExceptionToRip=%016RX64 LastExceptionFromRip=%016RX64\n",
621 pXcptCtx->LastExceptionToRip, pXcptCtx->LastExceptionFromRip);
622 uXcptSP = pXcptCtx->Rsp;
623 uXcptPC = pXcptCtx->Rip;
624
625#elif defined(RT_ARCH_X86)
626 RTLogLogger(pLogger, NULL, "\ncs:eip=%04x:%08RX32\n", pXcptCtx->SegCs, pXcptCtx->Eip);
627 RTLogLogger(pLogger, NULL, "ss:esp=%04x:%08RX32 ebp=%08RX32\n", pXcptCtx->SegSs, pXcptCtx->Esp, pXcptCtx->Ebp);
628 RTLogLogger(pLogger, NULL, "eax=%08RX32 ecx=%08RX32 edx=%08RX32 ebx=%08RX32\n",
629 pXcptCtx->Eax, pXcptCtx->Ecx, pXcptCtx->Edx, pXcptCtx->Ebx);
630 RTLogLogger(pLogger, NULL, "esi=%08RX32 edi=%08RX32 esp=%08RX32 ebp=%08RX32\n",
631 pXcptCtx->Esi, pXcptCtx->Edi, pXcptCtx->Esp, pXcptCtx->Ebp);
632 RTLogLogger(pLogger, NULL, "ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n",
633 pXcptCtx->SegDs, pXcptCtx->SegEs, pXcptCtx->SegFs, pXcptCtx->SegGs, pXcptCtx->EFlags);
634 uXcptSP = pXcptCtx->Esp;
635 uXcptPC = pXcptCtx->Eip;
636#endif
637 }
638
639 /*
640 * Dump stack.
641 */
642 uintptr_t uStack = (uintptr_t)(void *)&szMarker[0];
643 uStack -= uStack & 15;
644
645 size_t cbToDump = PAGE_SIZE - (uStack & PAGE_OFFSET_MASK);
646 if (cbToDump < 512)
647 cbToDump += PAGE_SIZE;
648 size_t cbToXcpt = uXcptSP - uStack;
649 while (cbToXcpt > cbToDump && cbToXcpt <= _16K)
650 cbToDump += PAGE_SIZE;
651 ULONG_PTR uLow = (uintptr_t)&szMarker[0];
652 ULONG_PTR uHigh = (uintptr_t)&szMarker[0];
653 if (g_pfnGetCurrentThreadStackLimits)
654 {
655 g_pfnGetCurrentThreadStackLimits(&uLow, &uHigh);
656 size_t cbToTop = RT_MAX(uLow, uHigh) - uStack;
657 if (cbToTop < _1M)
658 cbToDump = cbToTop;
659 }
660
661 RTLogLogger(pLogger, NULL, "\nStack %p, dumping %#x bytes (low=%p, high=%p)\n", uStack, cbToDump, uLow, uHigh);
662 RTLogLogger(pLogger, NULL, "%.*RhxD\n", cbToDump, uStack);
663
664 /*
665 * Try figure the thread name.
666 *
667 * Note! This involves the thread db lock, so it may deadlock, which
668 * is why it's at the end.
669 */
670 RTLogLogger(pLogger, NULL, "Thread ID: %p\n", RTThreadNativeSelf());
671 RTLogLogger(pLogger, NULL, "Thread name: %s\n", RTThreadSelfName());
672 RTLogLogger(pLogger, NULL, "Thread IPRT: %p\n", RTThreadSelf());
673
674 /*
675 * Try dump the load information.
676 */
677 PPEB pPeb = RTNtCurrentPeb();
678 if (RT_VALID_PTR(pPeb))
679 {
680 PPEB_LDR_DATA pLdrData = pPeb->Ldr;
681 if (RT_VALID_PTR(pLdrData))
682 {
683 PLDR_DATA_TABLE_ENTRY pFound = NULL;
684 LIST_ENTRY * const pList = &pLdrData->InMemoryOrderModuleList;
685 LIST_ENTRY *pListEntry = pList->Flink;
686 uint32_t cLoops = 0;
687 RTLogLogger(pLogger, NULL,
688 "\nLoaded Modules:\n"
689 "%-*s[*] Timestamp Path\n", sizeof(void *) * 4 + 2 - 1, "Address range"
690 );
691 while (pListEntry != pList && RT_VALID_PTR(pListEntry) && cLoops < 1024)
692 {
693 PLDR_DATA_TABLE_ENTRY pLdrEntry = RT_FROM_MEMBER(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
694 uint32_t const cbLength = (uint32_t)(uintptr_t)pLdrEntry->Reserved3[1];
695 char chInd = ' ';
696 if (uXcptPC - (uintptr_t)pLdrEntry->DllBase < cbLength)
697 {
698 chInd = '*';
699 pFound = pLdrEntry;
700 }
701
702 if ( RT_VALID_PTR(pLdrEntry->FullDllName.Buffer)
703 && pLdrEntry->FullDllName.Length > 0
704 && pLdrEntry->FullDllName.Length < _8K
705 && (pLdrEntry->FullDllName.Length & 1) == 0
706 && pLdrEntry->FullDllName.Length <= pLdrEntry->FullDllName.MaximumLength)
707 RTLogLogger(pLogger, NULL, "%p..%p%c %08RX32 %.*ls\n",
708 pLdrEntry->DllBase, (uintptr_t)pLdrEntry->DllBase + cbLength - 1, chInd,
709 pLdrEntry->TimeDateStamp, pLdrEntry->FullDllName.Length / sizeof(RTUTF16),
710 pLdrEntry->FullDllName.Buffer);
711 else
712 RTLogLogger(pLogger, NULL, "%p..%p%c %08RX32 <bad or missing: %p LB %#x max %#x\n",
713 pLdrEntry->DllBase, (uintptr_t)pLdrEntry->DllBase + cbLength - 1, chInd,
714 pLdrEntry->TimeDateStamp, pLdrEntry->FullDllName.Buffer, pLdrEntry->FullDllName.Length,
715 pLdrEntry->FullDllName.MaximumLength);
716
717 /* advance */
718 pListEntry = pListEntry->Flink;
719 cLoops++;
720 }
721
722 /*
723 * Use the above to pick out code addresses on the stack.
724 */
725 if ( cLoops < 1024
726 && uXcptSP - uStack < cbToDump)
727 {
728 RTLogLogger(pLogger, NULL, "\nPotential code addresses on the stack:\n");
729 if (pFound)
730 {
731 if ( RT_VALID_PTR(pFound->FullDllName.Buffer)
732 && pFound->FullDllName.Length > 0
733 && pFound->FullDllName.Length < _8K
734 && (pFound->FullDllName.Length & 1) == 0
735 && pFound->FullDllName.Length <= pFound->FullDllName.MaximumLength)
736 RTLogLogger(pLogger, NULL, "%-*s: %p - %#010RX32 bytes into %.*ls\n",
737 sizeof(void *) * 2, "Xcpt PC", uXcptPC, (uint32_t)(uXcptPC - (uintptr_t)pFound->DllBase),
738 pFound->FullDllName.Length / sizeof(RTUTF16), pFound->FullDllName.Buffer);
739 else
740 RTLogLogger(pLogger, NULL, "%-*s: %p - %08RX32 into module at %p\n",
741 sizeof(void *) * 2, "Xcpt PC", uXcptPC, (uint32_t)(uXcptPC - (uintptr_t)pFound->DllBase),
742 pFound->DllBase);
743 }
744
745 uintptr_t const *puStack = (uintptr_t const *)uXcptSP;
746 uintptr_t cLeft = (cbToDump - (uXcptSP - uStack)) / sizeof(uintptr_t);
747 while (cLeft-- > 0)
748 {
749 uintptr_t uPtr = *puStack;
750 if (RT_VALID_PTR(uPtr))
751 {
752 /* Search the module table. */
753 pFound = NULL;
754 cLoops = 0;
755 pListEntry = pList->Flink;
756 while (pListEntry != pList && RT_VALID_PTR(pListEntry) && cLoops < 1024)
757 {
758 PLDR_DATA_TABLE_ENTRY pLdrEntry = RT_FROM_MEMBER(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
759 uint32_t const cbLength = (uint32_t)(uintptr_t)pLdrEntry->Reserved3[1];
760 if (uPtr - (uintptr_t)pLdrEntry->DllBase < cbLength)
761 {
762 pFound = pLdrEntry;
763 break;
764 }
765
766 /* advance */
767 pListEntry = pListEntry->Flink;
768 cLoops++;
769 }
770
771 if (pFound)
772 {
773 if ( RT_VALID_PTR(pFound->FullDllName.Buffer)
774 && pFound->FullDllName.Length > 0
775 && pFound->FullDllName.Length < _8K
776 && (pFound->FullDllName.Length & 1) == 0
777 && pFound->FullDllName.Length <= pFound->FullDllName.MaximumLength)
778 RTLogLogger(pLogger, NULL, "%p: %p - %#010RX32 bytes into %.*ls\n",
779 puStack, uPtr, (uint32_t)(uPtr - (uintptr_t)pFound->DllBase),
780 pFound->FullDllName.Length / sizeof(RTUTF16), pFound->FullDllName.Buffer);
781 else
782 RTLogLogger(pLogger, NULL, "%p: %p - %08RX32 into module at %p\n",
783 puStack, uPtr, (uint32_t)(uPtr - (uintptr_t)pFound->DllBase), pFound->DllBase);
784 }
785 }
786
787 puStack++;
788 }
789 }
790 }
791 }
792 }
793
794 /*
795 * Do the default stuff, never mind us.
796 */
797 if (g_pfnUnhandledXcptFilter)
798 return g_pfnUnhandledXcptFilter(pPtrs);
799 return EXCEPTION_CONTINUE_SEARCH;
800}
801
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette