1 | /* $Id: vcc100-kernel32-fakes.cpp 60035 2016-03-15 12:01:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Tricks to make the Visual C++ 2010 CRT work on NT4, W2K and XP.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/cdefs.h>
|
---|
32 | #include <iprt/types.h>
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/string.h>
|
---|
35 |
|
---|
36 | #ifndef RT_ARCH_X86
|
---|
37 | # error "This code is X86 only"
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #define DecodePointer Ignore_DecodePointer
|
---|
41 | #define EncodePointer Ignore_EncodePointer
|
---|
42 | #define InitializeCriticalSectionAndSpinCount Ignore_InitializeCriticalSectionAndSpinCount
|
---|
43 | #define HeapSetInformation Ignore_HeapSetInformation
|
---|
44 | #define HeapQueryInformation Ignore_HeapQueryInformation
|
---|
45 | #define CreateTimerQueue Ignore_CreateTimerQueue
|
---|
46 | #define CreateTimerQueueTimer Ignore_CreateTimerQueueTimer
|
---|
47 | #define DeleteTimerQueueTimer Ignore_DeleteTimerQueueTimer
|
---|
48 | #define InitializeSListHead Ignore_InitializeSListHead
|
---|
49 | #define InterlockedFlushSList Ignore_InterlockedFlushSList
|
---|
50 | #define InterlockedPopEntrySList Ignore_InterlockedPopEntrySList
|
---|
51 | #define InterlockedPushEntrySList Ignore_InterlockedPushEntrySList
|
---|
52 | #define QueryDepthSList Ignore_QueryDepthSList
|
---|
53 | #define VerifyVersionInfoA Ignore_VerifyVersionInfoA
|
---|
54 | #define VerSetConditionMask Ignore_VerSetConditionMask
|
---|
55 |
|
---|
56 | #include <Windows.h>
|
---|
57 |
|
---|
58 | #undef DecodePointer
|
---|
59 | #undef EncodePointer
|
---|
60 | #undef InitializeCriticalSectionAndSpinCount
|
---|
61 | #undef HeapSetInformation
|
---|
62 | #undef HeapQueryInformation
|
---|
63 | #undef CreateTimerQueue
|
---|
64 | #undef CreateTimerQueueTimer
|
---|
65 | #undef DeleteTimerQueueTimer
|
---|
66 | #undef InitializeSListHead
|
---|
67 | #undef InterlockedFlushSList
|
---|
68 | #undef InterlockedPopEntrySList
|
---|
69 | #undef InterlockedPushEntrySList
|
---|
70 | #undef QueryDepthSList
|
---|
71 | #undef VerifyVersionInfoA
|
---|
72 | #undef VerSetConditionMask
|
---|
73 |
|
---|
74 |
|
---|
75 | #ifndef HEAP_STANDARD
|
---|
76 | # define HEAP_STANDARD 0
|
---|
77 | #endif
|
---|
78 |
|
---|
79 |
|
---|
80 | /** Dynamically resolves an kernel32 API. */
|
---|
81 | #define RESOLVE_ME(ApiNm) \
|
---|
82 | static bool volatile s_fInitialized = false; \
|
---|
83 | static decltype(ApiNm) *s_pfnApi = NULL; \
|
---|
84 | decltype(ApiNm) *pfnApi; \
|
---|
85 | if (!s_fInitialized) \
|
---|
86 | pfnApi = s_pfnApi; \
|
---|
87 | else \
|
---|
88 | { \
|
---|
89 | pfnApi = (decltype(pfnApi))GetProcAddress(GetModuleHandleW(L"kernel32"), #ApiNm); \
|
---|
90 | s_pfnApi = pfnApi; \
|
---|
91 | s_fInitialized = true; \
|
---|
92 | } do {} while (0) \
|
---|
93 |
|
---|
94 |
|
---|
95 | extern "C"
|
---|
96 | __declspec(dllexport) PVOID WINAPI
|
---|
97 | DecodePointer(PVOID pvEncoded)
|
---|
98 | {
|
---|
99 | RESOLVE_ME(DecodePointer);
|
---|
100 | if (pfnApi)
|
---|
101 | return pfnApi(pvEncoded);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Fallback code.
|
---|
105 | */
|
---|
106 | return pvEncoded;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | extern "C"
|
---|
111 | __declspec(dllexport) PVOID WINAPI
|
---|
112 | EncodePointer(PVOID pvNative)
|
---|
113 | {
|
---|
114 | RESOLVE_ME(EncodePointer);
|
---|
115 | if (pfnApi)
|
---|
116 | return pfnApi(pvNative);
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Fallback code.
|
---|
120 | */
|
---|
121 | return pvNative;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | extern "C"
|
---|
126 | __declspec(dllexport) BOOL WINAPI
|
---|
127 | InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION pCritSect, DWORD cSpin)
|
---|
128 | {
|
---|
129 | RESOLVE_ME(InitializeCriticalSectionAndSpinCount);
|
---|
130 | if (pfnApi)
|
---|
131 | return pfnApi(pCritSect, cSpin);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Fallback code.
|
---|
135 | */
|
---|
136 | InitializeCriticalSection(pCritSect);
|
---|
137 | return TRUE;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | extern "C"
|
---|
142 | __declspec(dllexport) BOOL WINAPI
|
---|
143 | HeapSetInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS enmInfoClass, PVOID pvBuf, SIZE_T cbBuf)
|
---|
144 | {
|
---|
145 | RESOLVE_ME(HeapSetInformation);
|
---|
146 | if (pfnApi)
|
---|
147 | return pfnApi(hHeap, enmInfoClass, pvBuf, cbBuf);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Fallback code.
|
---|
151 | */
|
---|
152 | if (enmInfoClass == HeapCompatibilityInformation)
|
---|
153 | {
|
---|
154 | if ( cbBuf != sizeof(ULONG)
|
---|
155 | || !pvBuf
|
---|
156 | || *(PULONG)pvBuf == HEAP_STANDARD
|
---|
157 | )
|
---|
158 | {
|
---|
159 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
160 | return FALSE;
|
---|
161 | }
|
---|
162 | return TRUE;
|
---|
163 | }
|
---|
164 |
|
---|
165 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
166 | return FALSE;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | extern "C"
|
---|
171 | __declspec(dllexport) BOOL WINAPI
|
---|
172 | HeapQueryInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS enmInfoClass, PVOID pvBuf, SIZE_T cbBuf, PSIZE_T pcbRet)
|
---|
173 | {
|
---|
174 | RESOLVE_ME(HeapQueryInformation);
|
---|
175 | if (pfnApi)
|
---|
176 | return pfnApi(hHeap, enmInfoClass, pvBuf, cbBuf, pcbRet);
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * Fallback code.
|
---|
180 | */
|
---|
181 | if (enmInfoClass == HeapCompatibilityInformation)
|
---|
182 | {
|
---|
183 | *pcbRet = sizeof(ULONG);
|
---|
184 | if (cbBuf < sizeof(ULONG) || !pvBuf)
|
---|
185 | {
|
---|
186 | SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
---|
187 | return FALSE;
|
---|
188 | }
|
---|
189 | *(PULONG)pvBuf = HEAP_STANDARD;
|
---|
190 | return TRUE;
|
---|
191 | }
|
---|
192 |
|
---|
193 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
194 | return FALSE;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | /* These are used by INTEL\mt_obj\Timer.obj: */
|
---|
199 |
|
---|
200 | extern "C"
|
---|
201 | __declspec(dllexport)
|
---|
202 | HANDLE WINAPI CreateTimerQueue(void)
|
---|
203 | {
|
---|
204 | RESOLVE_ME(CreateTimerQueue);
|
---|
205 | if (pfnApi)
|
---|
206 | return pfnApi();
|
---|
207 | SetLastError(ERROR_NOT_SUPPORTED);
|
---|
208 | return NULL;
|
---|
209 | }
|
---|
210 |
|
---|
211 | extern "C"
|
---|
212 | __declspec(dllexport)
|
---|
213 | BOOL WINAPI CreateTimerQueueTimer(PHANDLE phTimer, HANDLE hTimerQueue, WAITORTIMERCALLBACK pfnCallback, PVOID pvUser,
|
---|
214 | DWORD msDueTime, DWORD msPeriod, ULONG fFlags)
|
---|
215 | {
|
---|
216 | RESOLVE_ME(CreateTimerQueueTimer);
|
---|
217 | if (pfnApi)
|
---|
218 | return pfnApi(phTimer, hTimerQueue, pfnCallback, pvUser, msDueTime, msPeriod, fFlags);
|
---|
219 | SetLastError(ERROR_NOT_SUPPORTED);
|
---|
220 | return FALSE;
|
---|
221 | }
|
---|
222 |
|
---|
223 | extern "C"
|
---|
224 | __declspec(dllexport)
|
---|
225 | BOOL WINAPI DeleteTimerQueueTimer(HANDLE hTimerQueue, HANDLE hTimer, HANDLE hEvtCompletion)
|
---|
226 | {
|
---|
227 | RESOLVE_ME(DeleteTimerQueueTimer);
|
---|
228 | if (pfnApi)
|
---|
229 | return pfnApi(hTimerQueue, hTimer, hEvtCompletion);
|
---|
230 | SetLastError(ERROR_NOT_SUPPORTED);
|
---|
231 | return FALSE;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /* This is used by several APIs. */
|
---|
235 |
|
---|
236 | extern "C"
|
---|
237 | __declspec(dllexport)
|
---|
238 | VOID WINAPI InitializeSListHead(PSLIST_HEADER pHead)
|
---|
239 | {
|
---|
240 | RESOLVE_ME(InitializeSListHead);
|
---|
241 | if (pfnApi)
|
---|
242 | pfnApi(pHead);
|
---|
243 | else /* fallback: */
|
---|
244 | pHead->Alignment = 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | extern "C"
|
---|
249 | __declspec(dllexport)
|
---|
250 | PSLIST_ENTRY WINAPI InterlockedFlushSList(PSLIST_HEADER pHead)
|
---|
251 | {
|
---|
252 | RESOLVE_ME(InterlockedFlushSList);
|
---|
253 | if (pfnApi)
|
---|
254 | return pfnApi(pHead);
|
---|
255 |
|
---|
256 | /* fallback: */
|
---|
257 | PSLIST_ENTRY pRet = NULL;
|
---|
258 | if (pHead->Next.Next)
|
---|
259 | {
|
---|
260 | for (;;)
|
---|
261 | {
|
---|
262 | SLIST_HEADER OldHead = *pHead;
|
---|
263 | SLIST_HEADER NewHead;
|
---|
264 | NewHead.Alignment = 0;
|
---|
265 | NewHead.Sequence = OldHead.Sequence + 1;
|
---|
266 | if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
|
---|
267 | {
|
---|
268 | pRet = OldHead.Next.Next;
|
---|
269 | break;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | return pRet;
|
---|
274 | }
|
---|
275 |
|
---|
276 | extern "C"
|
---|
277 | __declspec(dllexport)
|
---|
278 | PSLIST_ENTRY WINAPI InterlockedPopEntrySList(PSLIST_HEADER pHead)
|
---|
279 | {
|
---|
280 | RESOLVE_ME(InterlockedPopEntrySList);
|
---|
281 | if (pfnApi)
|
---|
282 | return pfnApi(pHead);
|
---|
283 |
|
---|
284 | /* fallback: */
|
---|
285 | PSLIST_ENTRY pRet = NULL;
|
---|
286 | for (;;)
|
---|
287 | {
|
---|
288 | SLIST_HEADER OldHead = *pHead;
|
---|
289 | pRet = OldHead.Next.Next;
|
---|
290 | if (pRet)
|
---|
291 | {
|
---|
292 | SLIST_HEADER NewHead;
|
---|
293 | __try
|
---|
294 | {
|
---|
295 | NewHead.Next.Next = pRet->Next;
|
---|
296 | }
|
---|
297 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
298 | {
|
---|
299 | continue;
|
---|
300 | }
|
---|
301 | NewHead.Depth = OldHead.Depth - 1;
|
---|
302 | NewHead.Sequence = OldHead.Sequence + 1;
|
---|
303 | if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
|
---|
304 | break;
|
---|
305 | }
|
---|
306 | else
|
---|
307 | break;
|
---|
308 | }
|
---|
309 | return pRet;
|
---|
310 | }
|
---|
311 |
|
---|
312 | extern "C"
|
---|
313 | __declspec(dllexport)
|
---|
314 | PSLIST_ENTRY WINAPI InterlockedPushEntrySList(PSLIST_HEADER pHead, PSLIST_ENTRY pEntry)
|
---|
315 | {
|
---|
316 | RESOLVE_ME(InterlockedPushEntrySList);
|
---|
317 | if (pfnApi)
|
---|
318 | return pfnApi(pHead, pEntry);
|
---|
319 |
|
---|
320 | /* fallback: */
|
---|
321 | PSLIST_ENTRY pRet = NULL;
|
---|
322 | for (;;)
|
---|
323 | {
|
---|
324 | SLIST_HEADER OldHead = *pHead;
|
---|
325 | pRet = OldHead.Next.Next;
|
---|
326 | pEntry->Next = pRet;
|
---|
327 | SLIST_HEADER NewHead;
|
---|
328 | NewHead.Next.Next = pEntry;
|
---|
329 | NewHead.Depth = OldHead.Depth + 1;
|
---|
330 | NewHead.Sequence = OldHead.Sequence + 1;
|
---|
331 | if (ASMAtomicCmpXchgU64(&pHead->Alignment, NewHead.Alignment, OldHead.Alignment))
|
---|
332 | break;
|
---|
333 | }
|
---|
334 | return pRet;
|
---|
335 | }
|
---|
336 |
|
---|
337 | extern "C"
|
---|
338 | __declspec(dllexport)
|
---|
339 | WORD WINAPI QueryDepthSList(PSLIST_HEADER pHead)
|
---|
340 | {
|
---|
341 | RESOLVE_ME(QueryDepthSList);
|
---|
342 | if (pfnApi)
|
---|
343 | return pfnApi(pHead);
|
---|
344 | return pHead->Depth;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | /* curl drags these in: */
|
---|
349 | extern "C"
|
---|
350 | __declspec(dllexport)
|
---|
351 | BOOL WINAPI VerifyVersionInfoA(LPOSVERSIONINFOEXA pInfo, DWORD fTypeMask, DWORDLONG fConditionMask)
|
---|
352 | {
|
---|
353 | RESOLVE_ME(VerifyVersionInfoA);
|
---|
354 | if (pfnApi)
|
---|
355 | return pfnApi(pInfo, fTypeMask, fConditionMask);
|
---|
356 |
|
---|
357 | /* fallback to make curl happy: */
|
---|
358 | OSVERSIONINFOEXA VerInfo;
|
---|
359 | RT_ZERO(VerInfo);
|
---|
360 | VerInfo.dwOSVersionInfoSize = sizeof(VerInfo);
|
---|
361 | if (!GetVersionEx((OSVERSIONINFO *)&VerInfo))
|
---|
362 | {
|
---|
363 | RT_ZERO(VerInfo);
|
---|
364 | VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
---|
365 | AssertReturn(GetVersionEx((OSVERSIONINFO *)&VerInfo), FALSE);
|
---|
366 | }
|
---|
367 |
|
---|
368 | BOOL fRet = TRUE;
|
---|
369 | for (unsigned i = 0; i < 8 && fRet; i++)
|
---|
370 | if (fTypeMask & RT_BIT_32(i))
|
---|
371 | {
|
---|
372 | uint32_t uLeft, uRight;
|
---|
373 | switch (RT_BIT_32(i))
|
---|
374 | {
|
---|
375 | #define MY_CASE(a_Num, a_Member) case a_Num: uLeft = VerInfo.a_Member; uRight = pInfo->a_Member; break
|
---|
376 | MY_CASE(VER_MINORVERSION, dwMinorVersion);
|
---|
377 | MY_CASE(VER_MAJORVERSION, dwMajorVersion);
|
---|
378 | MY_CASE(VER_BUILDNUMBER, dwBuildNumber);
|
---|
379 | MY_CASE(VER_PLATFORMID, dwPlatformId);
|
---|
380 | MY_CASE(VER_SERVICEPACKMINOR, wServicePackMinor);
|
---|
381 | MY_CASE(VER_SERVICEPACKMAJOR, wServicePackMinor);
|
---|
382 | MY_CASE(VER_SUITENAME, wSuiteMask);
|
---|
383 | MY_CASE(VER_PRODUCT_TYPE, wProductType);
|
---|
384 | #undef MY_CASE
|
---|
385 | default: uLeft = uRight = 0; AssertFailed();
|
---|
386 | }
|
---|
387 | switch ((uint8_t)(fConditionMask >> (i*8)))
|
---|
388 | {
|
---|
389 | case VER_EQUAL: fRet = uLeft == uRight; break;
|
---|
390 | case VER_GREATER: fRet = uLeft > uRight; break;
|
---|
391 | case VER_GREATER_EQUAL: fRet = uLeft >= uRight; break;
|
---|
392 | case VER_LESS: fRet = uLeft < uRight; break;
|
---|
393 | case VER_LESS_EQUAL: fRet = uLeft <= uRight; break;
|
---|
394 | case VER_AND: fRet = (uLeft & uRight) == uRight; break;
|
---|
395 | case VER_OR: fRet = (uLeft & uRight) != 0; break;
|
---|
396 | default: fRet = FALSE; AssertFailed(); break;
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | return fRet;
|
---|
401 | }
|
---|
402 |
|
---|
403 | extern "C"
|
---|
404 | __declspec(dllexport)
|
---|
405 | ULONGLONG WINAPI VerSetConditionMask(ULONGLONG fConditionMask, DWORD fTypeMask, BYTE bOperator)
|
---|
406 | {
|
---|
407 | RESOLVE_ME(VerSetConditionMask);
|
---|
408 | if (pfnApi)
|
---|
409 | return pfnApi(fConditionMask, fTypeMask, bOperator);
|
---|
410 |
|
---|
411 | /* fallback: */
|
---|
412 | for (unsigned i = 0; i < 8; i++)
|
---|
413 | if (fTypeMask & RT_BIT_32(i))
|
---|
414 | {
|
---|
415 | uint64_t fMask = UINT64_C(0xff) << (i*8);
|
---|
416 | fConditionMask &= ~fMask;
|
---|
417 | fConditionMask |= (uint64_t)bOperator << (i*8);
|
---|
418 |
|
---|
419 | }
|
---|
420 | return fConditionMask;
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 |
|
---|
425 | /* Dummy to force dragging in this object in the link, so the linker
|
---|
426 | won't accidentally use the symbols from kernel32. */
|
---|
427 | extern "C" int vcc100_kernel32_fakes_cpp(void)
|
---|
428 | {
|
---|
429 | return 42;
|
---|
430 | }
|
---|
431 |
|
---|