[778] | 1 | #include "stdafx.h"
|
---|
[800] | 2 | #include <cstdint>
|
---|
| 3 | #include <process.h>
|
---|
[798] | 4 | #include "OSVersion.h"
|
---|
[778] | 5 |
|
---|
[800] | 6 | extern "C"
|
---|
| 7 | {
|
---|
| 8 | extern void* TrapTableFirst;
|
---|
| 9 | extern FARPROC alias__imp__IsDebuggerPresent;
|
---|
| 10 | extern FARPROC alias__imp__EncodePointer;
|
---|
| 11 | extern FARPROC alias__imp__DecodePointer;
|
---|
| 12 | extern FARPROC alias__imp__HeapSetInformation;
|
---|
| 13 | extern FARPROC alias__imp__InitializeCriticalSectionAndSpinCount;
|
---|
| 14 | extern FARPROC alias__imp__InterlockedCompareExchange;
|
---|
| 15 | extern FARPROC alias__imp__IsProcessorFeaturePresent;
|
---|
| 16 | extern FARPROC alias__imp__InterlockedPushEntrySList;
|
---|
| 17 | extern FARPROC alias__imp__InterlockedPopEntrySList;
|
---|
| 18 | extern void* TrapTableLast;
|
---|
| 19 | } // extern "C"
|
---|
[787] | 20 |
|
---|
[800] | 21 | namespace {
|
---|
| 22 |
|
---|
| 23 | HMODULE GetKernelModule()
|
---|
[785] | 24 | {
|
---|
[800] | 25 | static HMODULE hmodKernel;
|
---|
[787] | 26 | if (hmodKernel == nullptr)
|
---|
| 27 | {
|
---|
| 28 | hmodKernel = GetModuleHandle(TEXT("kernel32"));
|
---|
| 29 | }
|
---|
| 30 | return hmodKernel;
|
---|
[785] | 31 | }
|
---|
[778] | 32 |
|
---|
[800] | 33 | BOOL WINAPI Alternative_IsDebuggerPresent()
|
---|
| 34 | {
|
---|
| 35 | ::SetLastError(ERROR_NOT_SUPPORTED);
|
---|
| 36 | return FALSE;
|
---|
| 37 | }
|
---|
[778] | 38 |
|
---|
[800] | 39 | void* WINAPI Alternative_EncodePointer(void* p)
|
---|
| 40 | {
|
---|
| 41 | return reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(p) ^ __security_cookie);
|
---|
| 42 | }
|
---|
[787] | 43 |
|
---|
[800] | 44 | #define Alternative_DecodePointer Alternative_EncodePointer
|
---|
[787] | 45 |
|
---|
[800] | 46 | BOOL WINAPI Alternative_HeapSetInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
|
---|
[778] | 47 | {
|
---|
[800] | 48 | return TRUE;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | #pragma warning(push)
|
---|
| 52 | #pragma warning(disable: 6320 6322)
|
---|
| 53 | BOOL WINAPI Alternative_InitializeCriticalSectionAndSpinCount(LPCRITICAL_SECTION lpcs, DWORD spinCount)
|
---|
| 54 | {
|
---|
| 55 | __try
|
---|
[778] | 56 | {
|
---|
[800] | 57 | ::InitializeCriticalSection(lpcs);
|
---|
| 58 | return TRUE;
|
---|
[778] | 59 | }
|
---|
[800] | 60 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
[778] | 61 | {
|
---|
| 62 | }
|
---|
[800] | 63 | return FALSE;
|
---|
| 64 | }
|
---|
| 65 | #pragma warning(pop)
|
---|
[778] | 66 |
|
---|
[800] | 67 | __declspec(naked) long __stdcall Alternative_InterlockedCompareExchange(long volatile*, long, long)
|
---|
| 68 | {
|
---|
| 69 | __asm
|
---|
[778] | 70 | {
|
---|
[800] | 71 | mov ecx,[esp+4]
|
---|
| 72 | mov edx,[esp+8]
|
---|
| 73 | mov eax,[esp+12]
|
---|
| 74 | lock cmpxchg [ecx],edx
|
---|
| 75 | ret 12
|
---|
[778] | 76 | }
|
---|
[800] | 77 | }
|
---|
[778] | 78 |
|
---|
[800] | 79 | BOOL WINAPI Alternative_IsProcessorFeaturePresent(DWORD /*feture*/)
|
---|
| 80 | {
|
---|
| 81 | // ATLからfeture = PF_NX_ENABLEDで呼ばれる
|
---|
| 82 | return FALSE;
|
---|
| 83 | }
|
---|
[787] | 84 |
|
---|
[800] | 85 | #define FUNCTION_INIT(name) \
|
---|
| 86 | if (auto f = ::GetProcAddress(GetKernelModule(), #name)) { \
|
---|
| 87 | alias__imp__ ## name = f; \
|
---|
| 88 | } else { \
|
---|
| 89 | alias__imp__ ## name = reinterpret_cast<FARPROC>(Alternative_ ## name); \
|
---|
[778] | 90 | }
|
---|
| 91 |
|
---|
[800] | 92 | void InitializeOldWindowsHelper()
|
---|
| 93 | {
|
---|
| 94 | DWORD oldProtect;
|
---|
| 95 | ::VirtualProtect(&TrapTableFirst,
|
---|
| 96 | reinterpret_cast<std::uintptr_t>(&TrapTableLast) - reinterpret_cast<std::uintptr_t>(&TrapTableFirst),
|
---|
| 97 | PAGE_READWRITE, &oldProtect);
|
---|
| 98 | FUNCTION_INIT(IsDebuggerPresent);
|
---|
| 99 | FUNCTION_INIT(EncodePointer);
|
---|
| 100 | FUNCTION_INIT(DecodePointer);
|
---|
| 101 | FUNCTION_INIT(HeapSetInformation);
|
---|
| 102 | alias__imp__InitializeCriticalSectionAndSpinCount =
|
---|
| 103 | ActiveBasic::Common::Is9x()
|
---|
| 104 | ? reinterpret_cast<FARPROC>(Alternative_InitializeCriticalSectionAndSpinCount)
|
---|
| 105 | : ::GetProcAddress(GetKernelModule(), "InitializeCriticalSectionAndSpinCount");
|
---|
| 106 | FUNCTION_INIT(InterlockedCompareExchange);
|
---|
| 107 | FUNCTION_INIT(IsProcessorFeaturePresent);
|
---|
| 108 | alias__imp__InterlockedPushEntrySList = ::GetProcAddress(GetKernelModule(), "InterlockedPushEntrySList");
|
---|
| 109 | alias__imp__InterlockedPopEntrySList = ::GetProcAddress(GetKernelModule(), "InterlockedPopEntrySList");
|
---|
[787] | 110 |
|
---|
[800] | 111 | ::VirtualProtect(&TrapTableFirst,
|
---|
| 112 | reinterpret_cast<std::uintptr_t>(&TrapTableLast) - reinterpret_cast<std::uintptr_t>(&TrapTableFirst),
|
---|
| 113 | PAGE_READONLY, &oldProtect);
|
---|
| 114 | }
|
---|
[778] | 115 |
|
---|
[800] | 116 | } // unnamed namespace
|
---|
[785] | 117 |
|
---|
[800] | 118 | extern "C"
|
---|
| 119 | {
|
---|
| 120 | #ifdef _UNICODE
|
---|
| 121 | int wWinMainCRTStartup();
|
---|
| 122 | #define tWinMainCRTStartup wWinMainCRTStartup
|
---|
| 123 | #else
|
---|
| 124 | int WinMainCRTStartup();
|
---|
| 125 | #define tWinMainCRTStartup WinMainCRTStartup
|
---|
| 126 | #endif
|
---|
[778] | 127 |
|
---|
[800] | 128 | void WinMainStartup_OldWindowsHelper()
|
---|
| 129 | {
|
---|
| 130 | __security_init_cookie();
|
---|
[778] | 131 |
|
---|
[800] | 132 | InitializeOldWindowsHelper();
|
---|
[778] | 133 |
|
---|
[800] | 134 | __asm
|
---|
[778] | 135 | {
|
---|
[800] | 136 | jmp tWinMainCRTStartup;
|
---|
[778] | 137 | }
|
---|
| 138 | }
|
---|
[800] | 139 |
|
---|
| 140 | } // extern "C"
|
---|