-
Notifications
You must be signed in to change notification settings - Fork 18
/
DynamicPatcher.cpp
298 lines (252 loc) · 8.88 KB
/
DynamicPatcher.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// created by i-saint
// distributed under Creative Commons Attribution (CC BY) license.
// https://github.com/i-saint/DynamicPatcher
#include "DynamicPatcher.h"
#include "dpInternal.h"
#ifdef dpWithTDisasm
#pragma comment(lib,"disasm" dpLibArch ".lib")
#endif // dpWithTDisasm
static dpContext *g_dpDefaultContext = nullptr;
static __declspec(thread) dpContext *g_dpCurrentContext = nullptr;
dpAPI dpContext* dpCreateContext()
{
return new dpContext();
}
dpAPI void dpDeleteContext(dpContext *ctx)
{
delete ctx;
};
dpAPI dpContext* dpGetDefaultContext()
{
return g_dpDefaultContext;
}
dpAPI void dpSetCurrentContext(dpContext *ctx)
{
g_dpCurrentContext = ctx;
}
dpAPI dpContext* dpGetCurrentContext()
{
if(!g_dpCurrentContext) { g_dpCurrentContext=g_dpDefaultContext; }
return g_dpCurrentContext;
}
dpAPI bool dpInitialize(const dpConfig &conf)
{
if(!g_dpDefaultContext) {
DWORD opt = ::SymGetOptions();
opt |= SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES;
opt &= ~SYMOPT_UNDNAME;
::SymSetOptions(opt);
::SymInitialize(::GetCurrentProcess(), NULL, TRUE);
dpConfig &g_dpConfig = dpGetConfig();
g_dpConfig = conf;
g_dpConfig.starttime = dpGetSystemTime();
dpConfigFile cf;
bool config_loaded = false;
if((conf.sys_flags&dpE_SysLoadConfig)!=0 && (conf.configfile ? cf.load(conf.configfile) : cf.load())) {
config_loaded = true;
if(cf.log_flags!=-1) { g_dpConfig.log_flags=cf.log_flags; }
if(cf.sys_flags!=-1) { g_dpConfig.sys_flags=cf.sys_flags; }
if(cf.vc_ver!=-1) { g_dpConfig.vc_ver=cf.vc_ver; }
}
g_dpDefaultContext = new dpContext();
if(config_loaded) {
if(!cf.loads.empty()) {
dpEach(cf.loads, [](const std::string &path){
dpLoad(path.c_str());
});
dpLink();
}
dpEach(cf.source_paths, [](const std::string &v){ dpAddSourcePath(v.c_str()); });
dpEach(cf.module_paths, [](const std::string &v){ dpAddModulePath(v.c_str()); });
dpEach(cf.preload_paths, [](const std::string &v){ dpAddPreloadPath(v.c_str()); });
dpEach(cf.msbuild_commands, [](const std::string &v){ dpAddMSBuildCommand(v.c_str()); });
dpEach(cf.build_commands, [](const std::string &v){ dpAddBuildCommand(v.c_str()); });
dpEach(cf.force_host_symbol_patterns, [](const std::string &v){ dpAddForceHostSymbolPattern(v.c_str()); });
if(!cf.source_paths.empty() && (!cf.msbuild_commands.empty() || !cf.build_commands.empty())) {
dpStartAutoBuild();
}
if(!cf.preload_paths.empty()) {
dpStartPreload();
}
}
if((g_dpConfig.sys_flags & dpE_SysOpenConsole)!=0) {
::AllocConsole();
}
return true;
}
return false;
}
dpAPI bool dpFinalize()
{
if(g_dpDefaultContext) {
delete g_dpDefaultContext;
g_dpDefaultContext = nullptr;
if((dpGetConfig().sys_flags & dpE_SysOpenConsole)!=0) {
::FreeConsole();
}
return true;
}
return false;
}
dpAPI size_t dpLoad(const char *path) { return dpGetCurrentContext()->load(path); }
dpAPI bool dpLoadObj(const char *path) { return dpGetCurrentContext()->getLoader()->loadObj(path)!=nullptr; }
dpAPI bool dpLoadLib(const char *path) { return dpGetCurrentContext()->getLoader()->loadLib(path)!=nullptr; }
dpAPI bool dpLoadDll(const char *path) { return dpGetCurrentContext()->getLoader()->loadDll(path)!=nullptr; }
dpAPI bool dpUnload(const char *path) { return dpGetCurrentContext()->getLoader()->unload(path); };
dpAPI bool dpLink() { return dpGetCurrentContext()->getLoader()->link(); }
dpAPI size_t dpLoadMapFiles() { return dpGetCurrentContext()->getLoader()->loadMapFiles(); };
dpAPI size_t dpPatchByFile(const char *filename, const char *filter_regex)
{
return dpGetCurrentContext()->patchByFile(filename, filter_regex);
}
dpAPI size_t dpPatchByFile(const char *filename, const std::function<bool (const dpSymbolS&)> &condition)
{
return dpGetCurrentContext()->patchByFile(filename, condition);
}
dpAPI bool dpPatchNameToName(const char *target_name, const char *hook_name)
{
return dpGetCurrentContext()->patchNameToName(target_name, hook_name);
}
dpAPI bool dpPatchAddressToName(const char *target_name, void *hook_addr)
{
return dpGetCurrentContext()->patchAddressToName(target_name, hook_addr);
}
dpAPI bool dpPatchAddressToAddress(void *target_adr, void *hook_addr)
{
return dpGetCurrentContext()->patchAddressToAddress(target_adr, hook_addr);
}
dpAPI bool dpPatchByAddress(void *hook_addr)
{
return dpGetCurrentContext()->patchByAddress(hook_addr);
}
dpAPI bool dpUnpatchByAddress(void *target_or_hook_addr)
{
return dpGetCurrentContext()->unpatchByAddress(target_or_hook_addr);
}
dpAPI void dpUnpatchAll()
{
return dpGetCurrentContext()->unpatchAll();
}
dpAPI void* dpGetUnpatched(void *target_or_hook_addr)
{
return dpGetCurrentContext()->getUnpatched(target_or_hook_addr);
}
dpAPI void dpAddForceHostSymbolPattern(const char *pattern)
{
return dpGetCurrentContext()->addForceHostSymbolPattern(pattern);
}
dpAPI void dpAddModulePath(const char *path)
{
dpGetCurrentContext()->getBuilder()->addModulePath(path);
}
dpAPI void dpAddSourcePath(const char *path)
{
dpGetCurrentContext()->getBuilder()->addSourcePath(path);
}
dpAPI void dpAddPreloadPath(const char *path)
{
dpGetCurrentContext()->getBuilder()->addPreloadPath(path);
}
dpAPI void dpAddMSBuildCommand(const char *msbuild_option)
{
dpGetCurrentContext()->getBuilder()->addMSBuildCommand(msbuild_option);
}
dpAPI void dpAddCLBuildCommand(const char *cl_option)
{
dpGetCurrentContext()->getBuilder()->addCLBuildCommand(cl_option);
}
dpAPI void dpAddBuildCommand(const char *any_command)
{
dpGetCurrentContext()->getBuilder()->addBuildCommand(any_command);
}
dpAPI bool dpStartAutoBuild()
{
return dpGetCurrentContext()->getBuilder()->startAutoBuild();
}
dpAPI bool dpStopAutoBuild()
{
return dpGetCurrentContext()->getBuilder()->stopAutoBuild();
}
dpAPI bool dpStartPreload()
{
return dpGetCurrentContext()->getBuilder()->startPreload();
}
dpAPI bool dpStopPreload()
{
return dpGetCurrentContext()->getBuilder()->stopPreload();
}
dpAPI void dpUpdate()
{
dpGetCurrentContext()->getBuilder()->update();
}
dpAPI const char* dpGetVCVarsPath()
{
return dpGetCurrentContext()->getBuilder()->getVCVarsPath();
}
#include <tlhelp32.h>
// F: [](DWORD thread_id)->void
template<class F>
inline void dpEnumerateThreads(DWORD pid, const F &f)
{
HANDLE ss = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if(ss!=INVALID_HANDLE_VALUE) {
THREADENTRY32 te;
te.dwSize = sizeof(te);
if(::Thread32First(ss, &te)) {
do {
if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID)+sizeof(te.th32OwnerProcessID) &&
te.th32OwnerProcessID==pid)
{
f(te.th32ThreadID);
}
te.dwSize = sizeof(te);
} while(::Thread32Next(ss, &te));
}
::CloseHandle(ss);
}
}
void dpExecExclusive(const std::function<void ()> &f)
{
std::vector<HANDLE> threads;
DWORD pid = ::GetCurrentProcessId();
dpEnumerateThreads(pid, [&](DWORD tid){
if(tid==::GetCurrentThreadId()) { return; }
if(HANDLE thread=::OpenThread(THREAD_ALL_ACCESS, FALSE, tid)) {
::SuspendThread(thread);
threads.push_back(thread);
}
});
f();
std::for_each(threads.begin(), threads.end(), [](HANDLE thread){
::ResumeThread(thread);
::CloseHandle(thread);
});
}
bool g_dp_stop_periodic_update = false;
bool g_dp_periodic_update_running = false;
unsigned __stdcall dpPeriodicUpdate(void *)
{
while(!g_dp_stop_periodic_update) {
dpExecExclusive([&](){ dpUpdate(); });
::Sleep(1000);
}
g_dp_periodic_update_running = false;
return 0;
}
dpAPI void dpBeginPeriodicUpdate()
{
dpExecExclusive([&](){ dpInitialize(); });
g_dp_stop_periodic_update = false;
g_dp_periodic_update_running = true;
// std::thread 使いたいが VS2010 対応を考慮して使わない方向で
_beginthreadex(nullptr, 0, &dpPeriodicUpdate, nullptr, 0, nullptr);
}
dpAPI void dpEndPeriodicUpdate()
{
if(g_dp_periodic_update_running) {
g_dp_stop_periodic_update = true;
while(!g_dp_periodic_update_running) {
::SwitchToThread();
}
}
}