VirtualBox

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

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

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

  • 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 70195 2017-12-18 13:40:26Z 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 {
364AssertMsgFailed(("rc=%Rrc\n", rc));
365 return;
366 }
367 g_fOldWinSock = true;
368 }
369 g_hModWinSock = (HMODULE)RTLdrGetNativeHandle(hLdrMod);
370 RTLdrClose(hLdrMod);
371 }
372RTAssertMsg2("g_hModWinSock=%p\n", g_hModWinSock);
373
374 g_pfnWSAStartup = (decltype(g_pfnWSAStartup)) GetProcAddress(g_hModWinSock, "WSAStartup");
375 g_pfnWSACleanup = (decltype(g_pfnWSACleanup)) GetProcAddress(g_hModWinSock, "WSACleanup");
376 g_pfnWSAGetLastError = (decltype(g_pfnWSAGetLastError)) GetProcAddress(g_hModWinSock, "WSAGetLastError");
377 g_pfnWSASetLastError = (decltype(g_pfnWSASetLastError)) GetProcAddress(g_hModWinSock, "WSASetLastError");
378 g_pfnWSACreateEvent = (decltype(g_pfnWSACreateEvent)) GetProcAddress(g_hModWinSock, "WSACreateEvent");
379 g_pfnWSACloseEvent = (decltype(g_pfnWSACloseEvent)) GetProcAddress(g_hModWinSock, "WSACloseEvent");
380 g_pfnWSAEventSelect = (decltype(g_pfnWSAEventSelect)) GetProcAddress(g_hModWinSock, "WSAEventSelect");
381 g_pfnWSAEnumNetworkEvents = (decltype(g_pfnWSAEnumNetworkEvents))GetProcAddress(g_hModWinSock,"WSAEnumNetworkEvents");
382 g_pfnWSASend = (decltype(g_pfnWSASend)) GetProcAddress(g_hModWinSock, "WSASend");
383 g_pfnsocket = (decltype(g_pfnsocket)) GetProcAddress(g_hModWinSock, "socket");
384 g_pfnclosesocket = (decltype(g_pfnclosesocket)) GetProcAddress(g_hModWinSock, "closesocket");
385 g_pfnrecv = (decltype(g_pfnrecv)) GetProcAddress(g_hModWinSock, "recv");
386 g_pfnsend = (decltype(g_pfnsend)) GetProcAddress(g_hModWinSock, "send");
387 g_pfnrecvfrom = (decltype(g_pfnrecvfrom)) GetProcAddress(g_hModWinSock, "recvfrom");
388 g_pfnsendto = (decltype(g_pfnsendto)) GetProcAddress(g_hModWinSock, "sendto");
389 g_pfnbind = (decltype(g_pfnbind)) GetProcAddress(g_hModWinSock, "bind");
390 g_pfnlisten = (decltype(g_pfnlisten)) GetProcAddress(g_hModWinSock, "listen");
391 g_pfnaccept = (decltype(g_pfnaccept)) GetProcAddress(g_hModWinSock, "accept");
392 g_pfnconnect = (decltype(g_pfnconnect)) GetProcAddress(g_hModWinSock, "connect");
393 g_pfnshutdown = (decltype(g_pfnshutdown)) GetProcAddress(g_hModWinSock, "shutdown");
394 g_pfngetsockopt = (decltype(g_pfngetsockopt)) GetProcAddress(g_hModWinSock, "getsockopt");
395 g_pfnsetsockopt = (decltype(g_pfnsetsockopt)) GetProcAddress(g_hModWinSock, "setsockopt");
396 g_pfnioctlsocket = (decltype(g_pfnioctlsocket)) GetProcAddress(g_hModWinSock, "ioctlsocket");
397 g_pfngetpeername = (decltype(g_pfngetpeername)) GetProcAddress(g_hModWinSock, "getpeername");
398 g_pfngetsockname = (decltype(g_pfngetsockname)) GetProcAddress(g_hModWinSock, "getsockname");
399 g_pfn__WSAFDIsSet = (decltype(g_pfn__WSAFDIsSet)) GetProcAddress(g_hModWinSock, "__WSAFDIsSet");
400 g_pfnselect = (decltype(g_pfnselect)) GetProcAddress(g_hModWinSock, "select");
401 g_pfngethostbyname = (decltype(g_pfngethostbyname)) GetProcAddress(g_hModWinSock, "gethostbyname");
402
403 Assert(g_pfnWSAStartup);
404 Assert(g_pfnWSACleanup);
405 Assert(g_pfnWSAGetLastError);
406 Assert(g_pfnWSASetLastError);
407 Assert(g_pfnWSACreateEvent || g_fOldWinSock);
408 Assert(g_pfnWSACloseEvent || g_fOldWinSock);
409 Assert(g_pfnWSAEventSelect || g_fOldWinSock);
410 Assert(g_pfnWSAEnumNetworkEvents || g_fOldWinSock);
411 Assert(g_pfnWSASend || g_fOldWinSock);
412 Assert(g_pfnsocket);
413 Assert(g_pfnclosesocket);
414 Assert(g_pfnrecv);
415 Assert(g_pfnsend);
416 Assert(g_pfnrecvfrom);
417 Assert(g_pfnsendto);
418 Assert(g_pfnbind);
419 Assert(g_pfnlisten);
420 Assert(g_pfnaccept);
421 Assert(g_pfnconnect);
422 Assert(g_pfnshutdown);
423 Assert(g_pfngetsockopt);
424 Assert(g_pfnsetsockopt);
425 Assert(g_pfnioctlsocket);
426 Assert(g_pfngetpeername);
427 Assert(g_pfngetsockname);
428 Assert(g_pfn__WSAFDIsSet);
429 Assert(g_pfnselect);
430 Assert(g_pfngethostbyname);
431}
432
433
434static int rtR3InitNativeObtrusiveWorker(uint32_t fFlags)
435{
436 /*
437 * Disable error popups.
438 */
439 UINT fOldErrMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
440 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX | fOldErrMode);
441
442 /*
443 * Restrict DLL searching for the process on windows versions which allow
444 * us to do so.
445 * - The first trick works on XP SP1+ and disables the searching of the
446 * current directory.
447 * - The second trick is W7 w/ KB2533623 and W8+, it restrict the DLL
448 * searching to the application directory (except when
449 * RTR3INIT_FLAGS_STANDALONE_APP is given) and the System32 directory.
450 */
451 int rc = VINF_SUCCESS;
452
453 typedef BOOL (WINAPI *PFNSETDLLDIRECTORY)(LPCWSTR);
454 PFNSETDLLDIRECTORY pfnSetDllDir = (PFNSETDLLDIRECTORY)GetProcAddress(g_hModKernel32, "SetDllDirectoryW");
455 if (pfnSetDllDir)
456 {
457 if (pfnSetDllDir(L""))
458 g_enmWinLdrProt = RTR3WINLDRPROT_NO_CWD;
459 else
460 rc = VERR_INTERNAL_ERROR_3;
461 }
462
463 /** @bugref{6861} Observed GUI issues on Vista (32-bit and 64-bit) when using
464 * SetDefaultDllDirectories.
465 * @bugref{8194} Try use SetDefaultDllDirectories on Vista for standalone apps
466 * despite potential GUI issues. */
467 if ( g_enmWinVer > kRTWinOSType_VISTA
468 || (fFlags & RTR3INIT_FLAGS_STANDALONE_APP))
469 {
470 typedef BOOL(WINAPI *PFNSETDEFAULTDLLDIRECTORIES)(DWORD);
471 PFNSETDEFAULTDLLDIRECTORIES pfnSetDefDllDirs;
472 pfnSetDefDllDirs = (PFNSETDEFAULTDLLDIRECTORIES)GetProcAddress(g_hModKernel32, "SetDefaultDllDirectories");
473 if (pfnSetDefDllDirs)
474 {
475 DWORD fDllDirs = LOAD_LIBRARY_SEARCH_SYSTEM32;
476 if (!(fFlags & RTR3INIT_FLAGS_STANDALONE_APP))
477 fDllDirs |= LOAD_LIBRARY_SEARCH_APPLICATION_DIR;
478 if (pfnSetDefDllDirs(fDllDirs))
479 g_enmWinLdrProt = fDllDirs & LOAD_LIBRARY_SEARCH_APPLICATION_DIR ? RTR3WINLDRPROT_SAFE : RTR3WINLDRPROT_SAFER;
480 else if (RT_SUCCESS(rc))
481 rc = VERR_INTERNAL_ERROR_4;
482 }
483 }
484
485 /*
486 * Register an unhandled exception callback if we can.
487 */
488 g_pfnGetCurrentThreadStackLimits = (PFNGETCURRENTTHREADSTACKLIMITS)GetProcAddress(g_hModKernel32, "GetCurrentThreadStackLimits");
489 g_pfnSetUnhandledExceptionFilter = (PFNSETUNHANDLEDEXCEPTIONFILTER)GetProcAddress(g_hModKernel32, "SetUnhandledExceptionFilter");
490 if (g_pfnSetUnhandledExceptionFilter && !g_pfnUnhandledXcptFilter)
491 {
492 g_pfnUnhandledXcptFilter = g_pfnSetUnhandledExceptionFilter(rtR3WinUnhandledXcptFilter);
493 AssertStmt(g_pfnUnhandledXcptFilter != rtR3WinUnhandledXcptFilter, g_pfnUnhandledXcptFilter = NULL);
494 }
495
496 return rc;
497}
498
499
500DECLHIDDEN(int) rtR3InitNativeFirst(uint32_t fFlags)
501{
502 /*
503 * Make sure we've got the handles of the two main Windows NT dlls.
504 */
505 g_hModKernel32 = GetModuleHandleW(L"kernel32.dll");
506 if (g_hModKernel32 == NULL)
507 return VERR_INTERNAL_ERROR_2;
508 g_hModNtDll = GetModuleHandleW(L"ntdll.dll");
509 if (g_hModNtDll == NULL)
510 return VERR_INTERNAL_ERROR_2;
511
512 rtR3InitWindowsVersion();
513
514 int rc = VINF_SUCCESS;
515 if (!(fFlags & RTR3INIT_FLAGS_UNOBTRUSIVE))
516 rc = rtR3InitNativeObtrusiveWorker(fFlags);
517
518 /*
519 * Resolve some kernel32.dll APIs we may need but aren't necessarily
520 * present in older windows versions.
521 */
522 g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetSystemWindowsDirectoryW");
523 if (g_pfnGetSystemWindowsDirectoryW)
524 g_pfnGetSystemWindowsDirectoryW = (PFNGETWINSYSDIR)GetProcAddress(g_hModKernel32, "GetWindowsDirectoryW");
525
526 /*
527 * Resolve some ntdll.dll APIs that weren't there in early NT versions.
528 */
529 g_pfnNtQueryFullAttributesFile = (PFNNTQUERYFULLATTRIBUTESFILE)GetProcAddress(g_hModNtDll, "NtQueryFullAttributesFile");
530 g_pfnNtDuplicateToken = (PFNNTDUPLICATETOKEN)GetProcAddress( g_hModNtDll, "NtDuplicateToken");
531
532 /*
533 * Resolve the winsock error getter and setter so assertions can save those too.
534 */
535 rtR3InitWinSockApis();
536
537 return rc;
538}
539
540
541DECLHIDDEN(void) rtR3InitNativeObtrusive(uint32_t fFlags)
542{
543 rtR3InitNativeObtrusiveWorker(fFlags);
544}
545
546
547DECLHIDDEN(int) rtR3InitNativeFinal(uint32_t fFlags)
548{
549 /* Nothing to do here. */
550 RT_NOREF_PV(fFlags);
551 return VINF_SUCCESS;
552}
553
554
555/**
556 * Unhandled exception filter callback.
557 *
558 * Will try log stuff.
559 */
560static LONG CALLBACK rtR3WinUnhandledXcptFilter(PEXCEPTION_POINTERS pPtrs)
561{
562 /*
563 * Try get the logger and log exception details.
564 *
565 * Note! We'll be using RTLogLogger for now, though we should probably add
566 * a less deadlock prone API here and gives up pretty fast if it
567 * cannot get the lock...
568 */
569 PRTLOGGER pLogger = RTLogRelGetDefaultInstance();
570 if (!pLogger)
571 pLogger = RTLogGetDefaultInstance();
572 if (pLogger)
573 {
574 RTLogLogger(pLogger, NULL, "\n!!! rtR3WinUnhandledXcptFilter caught an exception on thread %p!!!\n", RTThreadNativeSelf());
575
576 /*
577 * Dump the exception record.
578 */
579 uintptr_t uXcptPC = 0;
580 PEXCEPTION_RECORD pXcptRec = RT_VALID_PTR(pPtrs) && RT_VALID_PTR(pPtrs->ExceptionRecord) ? pPtrs->ExceptionRecord : NULL;
581 if (pXcptRec)
582 {
583 RTLogLogger(pLogger, NULL, "\nExceptionCode=%#010x ExceptionFlags=%#010x ExceptionAddress=%p\n",
584 pXcptRec->ExceptionCode, pXcptRec->ExceptionFlags, pXcptRec->ExceptionAddress);
585 for (uint32_t i = 0; i < RT_MIN(pXcptRec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS); i++)
586 RTLogLogger(pLogger, NULL, "ExceptionInformation[%d]=%p\n", i, pXcptRec->ExceptionInformation[i]);
587 uXcptPC = (uintptr_t)pXcptRec->ExceptionAddress;
588
589 /* Nested? Display one level only. */
590 PEXCEPTION_RECORD pNestedRec = pXcptRec->ExceptionRecord;
591 if (RT_VALID_PTR(pNestedRec))
592 {
593 RTLogLogger(pLogger, NULL, "Nested: ExceptionCode=%#010x ExceptionFlags=%#010x ExceptionAddress=%p (nested %p)\n",
594 pNestedRec->ExceptionCode, pNestedRec->ExceptionFlags, pNestedRec->ExceptionAddress,
595 pNestedRec->ExceptionRecord);
596 for (uint32_t i = 0; i < RT_MIN(pNestedRec->NumberParameters, EXCEPTION_MAXIMUM_PARAMETERS); i++)
597 RTLogLogger(pLogger, NULL, "Nested: ExceptionInformation[%d]=%p\n", i, pNestedRec->ExceptionInformation[i]);
598 uXcptPC = (uintptr_t)pNestedRec->ExceptionAddress;
599 }
600 }
601
602 /*
603 * Dump the context record.
604 */
605 volatile char szMarker[] = "stackmarker";
606 uintptr_t uXcptSP = (uintptr_t)&szMarker[0];
607 PCONTEXT pXcptCtx = RT_VALID_PTR(pPtrs) && RT_VALID_PTR(pPtrs->ContextRecord) ? pPtrs->ContextRecord : NULL;
608 if (pXcptCtx)
609 {
610#ifdef RT_ARCH_AMD64
611 RTLogLogger(pLogger, NULL, "\ncs:rip=%04x:%016RX64\n", pXcptCtx->SegCs, pXcptCtx->Rip);
612 RTLogLogger(pLogger, NULL, "ss:rsp=%04x:%016RX64 rbp=%016RX64\n", pXcptCtx->SegSs, pXcptCtx->Rsp, pXcptCtx->Rbp);
613 RTLogLogger(pLogger, NULL, "rax=%016RX64 rcx=%016RX64 rdx=%016RX64 rbx=%016RX64\n",
614 pXcptCtx->Rax, pXcptCtx->Rcx, pXcptCtx->Rdx, pXcptCtx->Rbx);
615 RTLogLogger(pLogger, NULL, "rsi=%016RX64 rdi=%016RX64 rsp=%016RX64 rbp=%016RX64\n",
616 pXcptCtx->Rsi, pXcptCtx->Rdi, pXcptCtx->Rsp, pXcptCtx->Rbp);
617 RTLogLogger(pLogger, NULL, "r8 =%016RX64 r9 =%016RX64 r10=%016RX64 r11=%016RX64\n",
618 pXcptCtx->R8, pXcptCtx->R9, pXcptCtx->R10, pXcptCtx->R11);
619 RTLogLogger(pLogger, NULL, "r12=%016RX64 r13=%016RX64 r14=%016RX64 r15=%016RX64\n",
620 pXcptCtx->R12, pXcptCtx->R13, pXcptCtx->R14, pXcptCtx->R15);
621 RTLogLogger(pLogger, NULL, "ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n",
622 pXcptCtx->SegDs, pXcptCtx->SegEs, pXcptCtx->SegFs, pXcptCtx->SegGs, pXcptCtx->EFlags);
623 RTLogLogger(pLogger, NULL, "p1home=%016RX64 p2home=%016RX64 pe3home=%016RX64\n",
624 pXcptCtx->P1Home, pXcptCtx->P2Home, pXcptCtx->P3Home);
625 RTLogLogger(pLogger, NULL, "p4home=%016RX64 p5home=%016RX64 pe6home=%016RX64\n",
626 pXcptCtx->P4Home, pXcptCtx->P5Home, pXcptCtx->P6Home);
627 RTLogLogger(pLogger, NULL, " LastBranchToRip=%016RX64 LastBranchFromRip=%016RX64\n",
628 pXcptCtx->LastBranchToRip, pXcptCtx->LastBranchFromRip);
629 RTLogLogger(pLogger, NULL, "LastExceptionToRip=%016RX64 LastExceptionFromRip=%016RX64\n",
630 pXcptCtx->LastExceptionToRip, pXcptCtx->LastExceptionFromRip);
631 uXcptSP = pXcptCtx->Rsp;
632 uXcptPC = pXcptCtx->Rip;
633
634#elif defined(RT_ARCH_X86)
635 RTLogLogger(pLogger, NULL, "\ncs:eip=%04x:%08RX32\n", pXcptCtx->SegCs, pXcptCtx->Eip);
636 RTLogLogger(pLogger, NULL, "ss:esp=%04x:%08RX32 ebp=%08RX32\n", pXcptCtx->SegSs, pXcptCtx->Esp, pXcptCtx->Ebp);
637 RTLogLogger(pLogger, NULL, "eax=%08RX32 ecx=%08RX32 edx=%08RX32 ebx=%08RX32\n",
638 pXcptCtx->Eax, pXcptCtx->Ecx, pXcptCtx->Edx, pXcptCtx->Ebx);
639 RTLogLogger(pLogger, NULL, "esi=%08RX32 edi=%08RX32 esp=%08RX32 ebp=%08RX32\n",
640 pXcptCtx->Esi, pXcptCtx->Edi, pXcptCtx->Esp, pXcptCtx->Ebp);
641 RTLogLogger(pLogger, NULL, "ds=%04x es=%04x fs=%04x gs=%04x eflags=%08x\n",
642 pXcptCtx->SegDs, pXcptCtx->SegEs, pXcptCtx->SegFs, pXcptCtx->SegGs, pXcptCtx->EFlags);
643 uXcptSP = pXcptCtx->Esp;
644 uXcptPC = pXcptCtx->Eip;
645#endif
646 }
647
648 /*
649 * Dump stack.
650 */
651 uintptr_t uStack = (uintptr_t)(void *)&szMarker[0];
652 uStack -= uStack & 15;
653
654 size_t cbToDump = PAGE_SIZE - (uStack & PAGE_OFFSET_MASK);
655 if (cbToDump < 512)
656 cbToDump += PAGE_SIZE;
657 size_t cbToXcpt = uXcptSP - uStack;
658 while (cbToXcpt > cbToDump && cbToXcpt <= _16K)
659 cbToDump += PAGE_SIZE;
660 ULONG_PTR uLow = (uintptr_t)&szMarker[0];
661 ULONG_PTR uHigh = (uintptr_t)&szMarker[0];
662 if (g_pfnGetCurrentThreadStackLimits)
663 {
664 g_pfnGetCurrentThreadStackLimits(&uLow, &uHigh);
665 size_t cbToTop = RT_MAX(uLow, uHigh) - uStack;
666 if (cbToTop < _1M)
667 cbToDump = cbToTop;
668 }
669
670 RTLogLogger(pLogger, NULL, "\nStack %p, dumping %#x bytes (low=%p, high=%p)\n", uStack, cbToDump, uLow, uHigh);
671 RTLogLogger(pLogger, NULL, "%.*RhxD\n", cbToDump, uStack);
672
673 /*
674 * Try figure the thread name.
675 *
676 * Note! This involves the thread db lock, so it may deadlock, which
677 * is why it's at the end.
678 */
679 RTLogLogger(pLogger, NULL, "Thread ID: %p\n", RTThreadNativeSelf());
680 RTLogLogger(pLogger, NULL, "Thread name: %s\n", RTThreadSelfName());
681 RTLogLogger(pLogger, NULL, "Thread IPRT: %p\n", RTThreadSelf());
682
683 /*
684 * Try dump the load information.
685 */
686 PPEB pPeb = RTNtCurrentPeb();
687 if (RT_VALID_PTR(pPeb))
688 {
689 PPEB_LDR_DATA pLdrData = pPeb->Ldr;
690 if (RT_VALID_PTR(pLdrData))
691 {
692 PLDR_DATA_TABLE_ENTRY pFound = NULL;
693 LIST_ENTRY * const pList = &pLdrData->InMemoryOrderModuleList;
694 LIST_ENTRY *pListEntry = pList->Flink;
695 uint32_t cLoops = 0;
696 RTLogLogger(pLogger, NULL,
697 "\nLoaded Modules:\n"
698 "%-*s[*] Timestamp Path\n", sizeof(void *) * 4 + 2 - 1, "Address range"
699 );
700 while (pListEntry != pList && RT_VALID_PTR(pListEntry) && cLoops < 1024)
701 {
702 PLDR_DATA_TABLE_ENTRY pLdrEntry = RT_FROM_MEMBER(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
703 uint32_t const cbLength = (uint32_t)(uintptr_t)pLdrEntry->Reserved3[1];
704 char chInd = ' ';
705 if (uXcptPC - (uintptr_t)pLdrEntry->DllBase < cbLength)
706 {
707 chInd = '*';
708 pFound = pLdrEntry;
709 }
710
711 if ( RT_VALID_PTR(pLdrEntry->FullDllName.Buffer)
712 && pLdrEntry->FullDllName.Length > 0
713 && pLdrEntry->FullDllName.Length < _8K
714 && (pLdrEntry->FullDllName.Length & 1) == 0
715 && pLdrEntry->FullDllName.Length <= pLdrEntry->FullDllName.MaximumLength)
716 RTLogLogger(pLogger, NULL, "%p..%p%c %08RX32 %.*ls\n",
717 pLdrEntry->DllBase, (uintptr_t)pLdrEntry->DllBase + cbLength - 1, chInd,
718 pLdrEntry->TimeDateStamp, pLdrEntry->FullDllName.Length / sizeof(RTUTF16),
719 pLdrEntry->FullDllName.Buffer);
720 else
721 RTLogLogger(pLogger, NULL, "%p..%p%c %08RX32 <bad or missing: %p LB %#x max %#x\n",
722 pLdrEntry->DllBase, (uintptr_t)pLdrEntry->DllBase + cbLength - 1, chInd,
723 pLdrEntry->TimeDateStamp, pLdrEntry->FullDllName.Buffer, pLdrEntry->FullDllName.Length,
724 pLdrEntry->FullDllName.MaximumLength);
725
726 /* advance */
727 pListEntry = pListEntry->Flink;
728 cLoops++;
729 }
730
731 /*
732 * Use the above to pick out code addresses on the stack.
733 */
734 if ( cLoops < 1024
735 && uXcptSP - uStack < cbToDump)
736 {
737 RTLogLogger(pLogger, NULL, "\nPotential code addresses on the stack:\n");
738 if (pFound)
739 {
740 if ( RT_VALID_PTR(pFound->FullDllName.Buffer)
741 && pFound->FullDllName.Length > 0
742 && pFound->FullDllName.Length < _8K
743 && (pFound->FullDllName.Length & 1) == 0
744 && pFound->FullDllName.Length <= pFound->FullDllName.MaximumLength)
745 RTLogLogger(pLogger, NULL, "%-*s: %p - %#010RX32 bytes into %.*ls\n",
746 sizeof(void *) * 2, "Xcpt PC", uXcptPC, (uint32_t)(uXcptPC - (uintptr_t)pFound->DllBase),
747 pFound->FullDllName.Length / sizeof(RTUTF16), pFound->FullDllName.Buffer);
748 else
749 RTLogLogger(pLogger, NULL, "%-*s: %p - %08RX32 into module at %p\n",
750 sizeof(void *) * 2, "Xcpt PC", uXcptPC, (uint32_t)(uXcptPC - (uintptr_t)pFound->DllBase),
751 pFound->DllBase);
752 }
753
754 uintptr_t const *puStack = (uintptr_t const *)uXcptSP;
755 uintptr_t cLeft = (cbToDump - (uXcptSP - uStack)) / sizeof(uintptr_t);
756 while (cLeft-- > 0)
757 {
758 uintptr_t uPtr = *puStack;
759 if (RT_VALID_PTR(uPtr))
760 {
761 /* Search the module table. */
762 pFound = NULL;
763 cLoops = 0;
764 pListEntry = pList->Flink;
765 while (pListEntry != pList && RT_VALID_PTR(pListEntry) && cLoops < 1024)
766 {
767 PLDR_DATA_TABLE_ENTRY pLdrEntry = RT_FROM_MEMBER(pListEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderLinks);
768 uint32_t const cbLength = (uint32_t)(uintptr_t)pLdrEntry->Reserved3[1];
769 if (uPtr - (uintptr_t)pLdrEntry->DllBase < cbLength)
770 {
771 pFound = pLdrEntry;
772 break;
773 }
774
775 /* advance */
776 pListEntry = pListEntry->Flink;
777 cLoops++;
778 }
779
780 if (pFound)
781 {
782 if ( RT_VALID_PTR(pFound->FullDllName.Buffer)
783 && pFound->FullDllName.Length > 0
784 && pFound->FullDllName.Length < _8K
785 && (pFound->FullDllName.Length & 1) == 0
786 && pFound->FullDllName.Length <= pFound->FullDllName.MaximumLength)
787 RTLogLogger(pLogger, NULL, "%p: %p - %#010RX32 bytes into %.*ls\n",
788 puStack, uPtr, (uint32_t)(uPtr - (uintptr_t)pFound->DllBase),
789 pFound->FullDllName.Length / sizeof(RTUTF16), pFound->FullDllName.Buffer);
790 else
791 RTLogLogger(pLogger, NULL, "%p: %p - %08RX32 into module at %p\n",
792 puStack, uPtr, (uint32_t)(uPtr - (uintptr_t)pFound->DllBase), pFound->DllBase);
793 }
794 }
795
796 puStack++;
797 }
798 }
799 }
800 }
801 }
802
803 /*
804 * Do the default stuff, never mind us.
805 */
806 if (g_pfnUnhandledXcptFilter)
807 return g_pfnUnhandledXcptFilter(pPtrs);
808 return EXCEPTION_CONTINUE_SEARCH;
809}
810
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