-
Notifications
You must be signed in to change notification settings - Fork 1
/
version-shim.c
169 lines (152 loc) · 5.34 KB
/
version-shim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* fool the compiler optimizer to make a jmp */
typedef void (*FUNCSHIM)();
void shim_prepare();
#ifdef _WIN64
#define MANGLE(x) x
#else
#define MANGLE(x) "_" x
#define BEGIN_NAKED_FUNCTION(name) __asm__(MANGLE(#name) ":");
#define END_NAKED_FUNCTION()
#endif
#if defined(_MSC_VER) && !defined(_WIN64)
#define DECLARE_SHIM(name) \
FUNCSHIM p##name; \
__declspec(naked) void Shim##name() { \
__asm call shim_prepare \
__asm jmp [p##name] \
}
#elif defined(__GNUC__)
#ifdef _WIN64
#define BEGIN_NAKED_FUNCTION(name) __declspec(naked) void name() {
#define END_NAKED_FUNCTION() }
BEGIN_NAKED_FUNCTION(shim_prepare2)
__asm__ ("cmp $0, " MANGLE("shim_prepared") "(%rip)");
__asm__ ("jz 114514f");
__asm__ ("ret");
__asm__ ("114514:");
__asm__ ("push %r9");
__asm__ ("push %r8");
__asm__ ("push %rdx");
__asm__ ("push %rcx");
__asm__ ("call " MANGLE("shim_prepare"));
__asm__ ("pop %rcx");
__asm__ ("pop %rdx");
__asm__ ("pop %r8");
__asm__ ("pop %r9");
__asm__ ("ret");
END_NAKED_FUNCTION()
#define DECLARE_SHIM(name) \
FUNCSHIM p##name; \
BEGIN_NAKED_FUNCTION( Shim##name ) \
__asm__ ("call " MANGLE("shim_prepare2")); \
__asm__ ("jmp *" MANGLE("p" #name) "(%rip)"); \
END_NAKED_FUNCTION()
#else
#define BEGIN_NAKED_FUNCTION(name) __asm__(MANGLE(#name) ":");
#define END_NAKED_FUNCTION()
BEGIN_NAKED_FUNCTION(shim_prepare2)
__asm__ ("cmp $0, " MANGLE("shim_prepared"));
__asm__ ("jz 114514f");
__asm__ ("ret");
__asm__ ("114514:");
__asm__ ("jmp " MANGLE("shim_prepare"));
END_NAKED_FUNCTION()
#define DECLARE_SHIM(name) \
FUNCSHIM p##name; \
BEGIN_NAKED_FUNCTION( Shim##name ) \
__asm__ ("call " MANGLE("shim_prepare2")); \
__asm__ ("jmp *" MANGLE("p" #name)); \
END_NAKED_FUNCTION()
#endif
#elif !defined(_WIN64)
#if defined(__GNUC__) && !defined(__clang__)
#define OPTIMIZE_ATTR __attribute__((optimize("O2")))
#define HAS_OPTIMIZE_ATTR 1
#elif defined(_MSC_VER)
#define OPTIMIZE_ATTR __pragma(optimize("g2", on))
#define HAS_OPTIMIZE_ATTR 1
#else
#define OPTIMIZE_ATTR
#define HAS_OPTIMIZE_ATTR 0
#endif
#pragma message("depending on compiler optimization: check compiled code for tail call optimization!!")
#if !defined(_MSC_VER) && !defined(__OPTIMIZE__) && !HAS_OPTIMIZE_ATTR
#pragma message("not optimizing shim functions - shim is likely to fail")
#endif
/*
we hope this will compile into:
name:
call shim_prepare
jmp [pname]
usually this can be achieved by reasonable optimization flags.
*/
#define DECLARE_SHIM(name) \
FUNCSHIM p##name; \
OPTIMIZE_ATTR void Shim##name() { \
shim_prepare(); \
p##name(); \
}
#else
#define DECLARE_SHIM(name) \
FUNCSHIM p##name; \
extern void Shim##name();
#endif
DECLARE_SHIM(GetFileVersionInfoA)
DECLARE_SHIM(GetFileVersionInfoByHandle)
DECLARE_SHIM(GetFileVersionInfoExA)
DECLARE_SHIM(GetFileVersionInfoExW)
DECLARE_SHIM(GetFileVersionInfoSizeA)
DECLARE_SHIM(GetFileVersionInfoSizeExA)
DECLARE_SHIM(GetFileVersionInfoSizeExW)
DECLARE_SHIM(GetFileVersionInfoSizeW)
DECLARE_SHIM(GetFileVersionInfoW)
DECLARE_SHIM(VerFindFileA)
DECLARE_SHIM(VerFindFileW)
DECLARE_SHIM(VerInstallFileA)
DECLARE_SHIM(VerInstallFileW)
DECLARE_SHIM(VerLanguageNameA)
DECLARE_SHIM(VerLanguageNameW)
DECLARE_SHIM(VerQueryValueA)
DECLARE_SHIM(VerQueryValueW)
#include <windows.h>
#define FILL_SHIM(hmod, name) p##name = (FUNCSHIM)GetProcAddress(hmod, #name)
static HMODULE hDll;
volatile LONG shim_preparing = 0, shim_prepared = 0;
void shim_prepare() {
if(shim_prepared) return;
if(InterlockedCompareExchange(&shim_preparing, 1, 0) == 1) {
// spin wait for other thread to prepare
while(!shim_prepared);
// prepared by other thread
return;
}
wchar_t *dllPath = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, 65536);
if (GetSystemDirectoryW(dllPath, 32768) != 0) {
lstrcatW(dllPath, L"\\version.dll");
hDll = LoadLibraryW(dllPath);
HeapFree(GetProcessHeap(), 0, dllPath);
if(!hDll) {
InterlockedCompareExchange(&shim_preparing, 0, 1);
return;
}
FILL_SHIM(hDll, GetFileVersionInfoA);
FILL_SHIM(hDll, GetFileVersionInfoByHandle);
FILL_SHIM(hDll, GetFileVersionInfoExA);
FILL_SHIM(hDll, GetFileVersionInfoExW);
FILL_SHIM(hDll, GetFileVersionInfoSizeA);
FILL_SHIM(hDll, GetFileVersionInfoSizeExA);
FILL_SHIM(hDll, GetFileVersionInfoSizeExW);
FILL_SHIM(hDll, GetFileVersionInfoSizeW);
FILL_SHIM(hDll, GetFileVersionInfoW);
FILL_SHIM(hDll, VerFindFileA);
FILL_SHIM(hDll, VerFindFileW);
FILL_SHIM(hDll, VerInstallFileA);
FILL_SHIM(hDll, VerInstallFileW);
FILL_SHIM(hDll, VerLanguageNameA);
FILL_SHIM(hDll, VerLanguageNameW);
FILL_SHIM(hDll, VerQueryValueA);
FILL_SHIM(hDll, VerQueryValueW);
}
InterlockedCompareExchange(&shim_preparing, 0, 1);
InterlockedCompareExchange(&shim_prepared, 1, 0);
}