VirtualBox

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

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

IPRT/win: Try detect windows 11 and server 2019 & 2022 by the stupid build number logic.

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

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