VirtualBox

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

Last change on this file was 100171, checked in by vboxsync, 12 months ago

IPRT: tcp.h+tcp.cpp,socket.h+socket.cpp: Add RTTcpSetKeepAlive() which
allows one to enable or disable sending periodic keep-alive messages on
a socket (SO_KEEPALIVE) as described in RFC 1122. RTTcpSetKeepAlive()
also allows one to adjust several keep-alive options on a per-socket
basis: the idle time before keep-alive probes are sent (TCP_KEEPIDLE
(TCP_KEEPALIVE on macOS)), the amount of time between keep-alive probes
(TCP_KEEPINTVL), and the number of keep-alive probes to send before
closing the connection (TCP_KEEPCNT).

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

© 2023 Oracle
ContactPrivacy policyTerms of Use