VirtualBox

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

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

iprt/process-win.cpp: shut up some annoying assertions on NT3.1.

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

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