1 | #include "stdafx.h"
|
---|
2 | #include <cstdint>
|
---|
3 | #include <cstring>
|
---|
4 | #include <process.h>
|
---|
5 | #include "OSVersion.h"
|
---|
6 |
|
---|
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"
|
---|
21 |
|
---|
22 | namespace {
|
---|
23 |
|
---|
24 | HMODULE GetKernelModule()
|
---|
25 | {
|
---|
26 | static HMODULE hmodKernel;
|
---|
27 | if (hmodKernel == nullptr)
|
---|
28 | {
|
---|
29 | hmodKernel = GetModuleHandleA("kernel32");
|
---|
30 | }
|
---|
31 | return hmodKernel;
|
---|
32 | }
|
---|
33 |
|
---|
34 | BOOL WINAPI Alternative_IsDebuggerPresent()
|
---|
35 | {
|
---|
36 | ::SetLastError(ERROR_NOT_SUPPORTED);
|
---|
37 | return FALSE;
|
---|
38 | }
|
---|
39 |
|
---|
40 | void* WINAPI Alternative_EncodePointer(void* p)
|
---|
41 | {
|
---|
42 | return reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(p) ^ __security_cookie);
|
---|
43 | }
|
---|
44 |
|
---|
45 | #define Alternative_DecodePointer Alternative_EncodePointer
|
---|
46 |
|
---|
47 | BOOL WINAPI Alternative_HeapSetInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
|
---|
48 | {
|
---|
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
|
---|
57 | {
|
---|
58 | ::InitializeCriticalSection(lpcs);
|
---|
59 | return TRUE;
|
---|
60 | }
|
---|
61 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
62 | {
|
---|
63 | }
|
---|
64 | return FALSE;
|
---|
65 | }
|
---|
66 | #pragma warning(pop)
|
---|
67 |
|
---|
68 | __declspec(naked) long WINAPI Alternative_InterlockedCompareExchange(long volatile*, long, long)
|
---|
69 | {
|
---|
70 | __asm
|
---|
71 | {
|
---|
72 | mov ecx,[esp+4]
|
---|
73 | mov edx,[esp+8]
|
---|
74 | mov eax,[esp+12]
|
---|
75 | lock cmpxchg [ecx],edx
|
---|
76 | ret 12
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | BOOL WINAPI Alternative_IsProcessorFeaturePresent(DWORD /*feture*/)
|
---|
81 | {
|
---|
82 | // ATLからfeture = PF_NX_ENABLEDで呼ばれる
|
---|
83 | return FALSE;
|
---|
84 | }
|
---|
85 |
|
---|
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); \
|
---|
91 | }
|
---|
92 |
|
---|
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 |
|
---|
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);
|
---|
110 | FUNCTION_INIT_9X(InitializeCriticalSectionAndSpinCount);
|
---|
111 | FUNCTION_INIT(InterlockedCompareExchange);
|
---|
112 | FUNCTION_INIT(IsProcessorFeaturePresent);
|
---|
113 | alias__imp__InterlockedPushEntrySList = ::GetProcAddress(GetKernelModule(), "InterlockedPushEntrySList");
|
---|
114 | alias__imp__InterlockedPopEntrySList = ::GetProcAddress(GetKernelModule(), "InterlockedPopEntrySList");
|
---|
115 |
|
---|
116 | ::VirtualProtect(&TrapTableFirst,
|
---|
117 | reinterpret_cast<std::uintptr_t>(&TrapTableLast) - reinterpret_cast<std::uintptr_t>(&TrapTableFirst),
|
---|
118 | PAGE_READONLY, &oldProtect);
|
---|
119 | }
|
---|
120 |
|
---|
121 | } // unnamed namespace
|
---|
122 |
|
---|
123 | extern "C"
|
---|
124 | {
|
---|
125 | #ifdef _CONSOLE
|
---|
126 | # ifdef _UNICODE
|
---|
127 | int wmainCRTStartup();
|
---|
128 | # define CRTStartup wmainCRTStartup
|
---|
129 | # else
|
---|
130 | int mainCRTStartup();
|
---|
131 | # define CRTStartup mainCRTStartup
|
---|
132 | # endif
|
---|
133 | #else
|
---|
134 | # ifdef _UNICODE
|
---|
135 | int wWinMainCRTStartup();
|
---|
136 | # define CRTStartup wWinMainCRTStartup
|
---|
137 | # else
|
---|
138 | int WinMainCRTStartup();
|
---|
139 | # define CRTStartup WinMainCRTStartup
|
---|
140 | # endif
|
---|
141 | #endif
|
---|
142 |
|
---|
143 | void Startup_OldWindowsHelper()
|
---|
144 | {
|
---|
145 | __security_init_cookie();
|
---|
146 | InitializeOldWindowsHelper();
|
---|
147 |
|
---|
148 | __asm
|
---|
149 | {
|
---|
150 | jmp CRTStartup;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | } // extern "C"
|
---|