VirtualBox

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

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

IPRT/rtR3WinUnhandledXcptFilter: Log the PID and command line too.

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