VirtualBox

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

Last change on this file since 89827 was 89827, checked in by vboxsync, 3 years ago

Runtime: Make use of CreateWaitableTimerEx and CREATE_WAITABLE_TIMER_HIGH_RESOLUTION available on newer Windwos 10 release to improve timer accuracy for sub tick intervals

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