VirtualBox

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

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

IPRT/R3: Made the core work on NT 3.51 (still experimental). [build fix]

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