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