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