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