VirtualBox

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

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

IPRT/nocrt: Fail fast and safer stack corruption. bugref:10261

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

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