VirtualBox

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

最後變更 在這個檔案從96476是 96476,由 vboxsync 提交於 3 年 前

IPRT: Resolve IsDebuggerPresent and GetSystemTimeAsFileTime dynamically as they are not present on windows version before NT4. bugref:10261

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

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