1 | #include "stdafx.h"
|
---|
2 | #include <cstdint>
|
---|
3 | #include <process.h>
|
---|
4 | #include "OSVersion.h"
|
---|
5 |
|
---|
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"
|
---|
20 |
|
---|
21 | namespace {
|
---|
22 |
|
---|
23 | HMODULE GetKernelModule()
|
---|
24 | {
|
---|
25 | static HMODULE hmodKernel;
|
---|
26 | if (hmodKernel == nullptr)
|
---|
27 | {
|
---|
28 | hmodKernel = GetModuleHandle(TEXT("kernel32"));
|
---|
29 | }
|
---|
30 | return hmodKernel;
|
---|
31 | }
|
---|
32 |
|
---|
33 | BOOL WINAPI Alternative_IsDebuggerPresent()
|
---|
34 | {
|
---|
35 | ::SetLastError(ERROR_NOT_SUPPORTED);
|
---|
36 | return FALSE;
|
---|
37 | }
|
---|
38 |
|
---|
39 | void* WINAPI Alternative_EncodePointer(void* p)
|
---|
40 | {
|
---|
41 | return reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(p) ^ __security_cookie);
|
---|
42 | }
|
---|
43 |
|
---|
44 | #define Alternative_DecodePointer Alternative_EncodePointer
|
---|
45 |
|
---|
46 | BOOL WINAPI Alternative_HeapSetInformation(HANDLE hHeap, HEAP_INFORMATION_CLASS hic, void* information, SSIZE_T informationLength)
|
---|
47 | {
|
---|
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
|
---|
56 | {
|
---|
57 | ::InitializeCriticalSection(lpcs);
|
---|
58 | return TRUE;
|
---|
59 | }
|
---|
60 | __except(EXCEPTION_EXECUTE_HANDLER)
|
---|
61 | {
|
---|
62 | }
|
---|
63 | return FALSE;
|
---|
64 | }
|
---|
65 | #pragma warning(pop)
|
---|
66 |
|
---|
67 | __declspec(naked) long __stdcall Alternative_InterlockedCompareExchange(long volatile*, long, long)
|
---|
68 | {
|
---|
69 | __asm
|
---|
70 | {
|
---|
71 | mov ecx,[esp+4]
|
---|
72 | mov edx,[esp+8]
|
---|
73 | mov eax,[esp+12]
|
---|
74 | lock cmpxchg [ecx],edx
|
---|
75 | ret 12
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | BOOL WINAPI Alternative_IsProcessorFeaturePresent(DWORD /*feture*/)
|
---|
80 | {
|
---|
81 | // ATLからfeture = PF_NX_ENABLEDで呼ばれる
|
---|
82 | return FALSE;
|
---|
83 | }
|
---|
84 |
|
---|
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); \
|
---|
90 | }
|
---|
91 |
|
---|
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");
|
---|
110 |
|
---|
111 | ::VirtualProtect(&TrapTableFirst,
|
---|
112 | reinterpret_cast<std::uintptr_t>(&TrapTableLast) - reinterpret_cast<std::uintptr_t>(&TrapTableFirst),
|
---|
113 | PAGE_READONLY, &oldProtect);
|
---|
114 | }
|
---|
115 |
|
---|
116 | } // unnamed namespace
|
---|
117 |
|
---|
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
|
---|
127 |
|
---|
128 | void WinMainStartup_OldWindowsHelper()
|
---|
129 | {
|
---|
130 | __security_init_cookie();
|
---|
131 |
|
---|
132 | InitializeOldWindowsHelper();
|
---|
133 |
|
---|
134 | __asm
|
---|
135 | {
|
---|
136 | jmp tWinMainCRTStartup;
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | } // extern "C"
|
---|