VirtualBox

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

Last change on this file since 70314 was 70215, checked in by vboxsync, 7 years ago

IPRT/r3/win: Some more NT 3.50 adjustments.

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