-
Notifications
You must be signed in to change notification settings - Fork 18
/
dpInternal.h
600 lines (514 loc) · 17.1 KB
/
dpInternal.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// created by i-saint
// distributed under Creative Commons Attribution (CC BY) license.
// https://github.com/i-saint/DynamicPatcher
#include <windows.h>
#include <dbghelp.h>
#include <process.h>
#include <cstdint>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <regex>
#include "DynamicPatcher.h"
typedef unsigned long long dpTime;
class dpBinary;
class dpObjFile;
class dpLibFile;
class dpDllFile;
class dpLoader;
class dpPatcher;
class dpBuilder;
class dpContext;
enum dpFileType {
dpE_Obj,
dpE_Lib,
dpE_Dll,
};
enum dpEventType {
dpE_OnLoad,
dpE_OnUnload,
};
enum dpLinkFlags {
dpE_NeedsLink=1,
dpE_NeedsBase=2,
};
enum dpSymbolFlagsEx {
dpE_HostSymbol = 0x10000,
dpE_NameNeedsDelete = 0x20000,
dpE_LinkFailed = 0x40000,
};
#define dpIsLinkFailed(flag) ((flag&dpE_LinkFailed)!=0)
struct dpSymbol
{
const char *name;
void *address;
int flags;
int section;
dpBinary *binary;
dpSymbol(const char *nam, void *addr, int fla, int sect, dpBinary *bin);
~dpSymbol();
const dpSymbolS& simplify() const;
bool partialLink();
};
inline bool operator< (const dpSymbol &a, const dpSymbol &b) { return strcmp(a.name, b.name)<0; }
inline bool operator==(const dpSymbol &a, const dpSymbol &b) { return strcmp(a.name, b.name)==0; }
inline bool operator< (const dpSymbol &a, const char *b) { return strcmp(a.name, b)<0; }
inline bool operator==(const dpSymbol &a, const char *b) { return strcmp(a.name, b)==0; }
template<class T>
struct dpLTPtr {
bool operator()(const T *a, const T *b) const { return *a<*b; }
};
template<class T>
struct dpEQPtr {
bool operator()(const T *a, const T *b) const { return *a==*b; }
};
struct dpPatchData
{
const dpSymbol *target;
const dpSymbol *hook;
void *unpatched;
void *trampoline;
size_t unpatched_size;
dpPatchData() : target(), hook(), unpatched(), trampoline(), unpatched_size() {}
};
inline bool operator< (const dpPatchData &a, const dpPatchData &b) { return a.target< b.target; }
inline bool operator==(const dpPatchData &a, const dpPatchData &b) { return a.target==b.target; }
template<class Container, class F> inline void dpEach(Container &cont, const F &f);
template<class Container, class F> inline auto dpFind(Container &cont, const F &f) -> decltype(cont.begin());
class dpMutex
{
public:
class ScopedLock
{
public:
ScopedLock(dpMutex &v);
~ScopedLock();
dpMutex &mutex;
};
dpMutex();
~dpMutex();
void lock();
void unlock();
private:
CRITICAL_SECTION m_cs;
};
dpConfig& dpGetConfig();
void dpPrintError(const char* fmt, ...);
void dpPrintWarning(const char* fmt, ...);
void dpPrintInfo(const char* fmt, ...);
void dpPrintDetail(const char* fmt, ...);
void* dpAllocateForward(size_t size, void *location);
void* dpAllocateBackward(size_t size, void *location);
void* dpAllocateModule(size_t size);
void dpDeallocate(void *location, size_t size);
dpTime dpGetMTime(const char *path);
dpTime dpGetSystemTime();
bool dpCopyFile(const char *srcpath, const char *dstpath);
template<class F> void dpGlob(const char *path, const F &f);
template<class F> bool dpMapFile(const char *path, void *&o_data, size_t &o_size, const F &alloc);
bool dpWriteFile(const char *path, const void *data, size_t size);
bool dpDeleteFile(const char *path);
bool dpFileExists(const char *path);
size_t dpSeparateDirFile(const char *path, std::string *dir, std::string *file);
size_t dpSeparateFileExt(const char *filename, std::string *file, std::string *ext);
size_t dpGetCurrentModulePath(char *buf, size_t buflen);
size_t dpGetMainModulePath(char *buf, size_t buflen);
void dpSanitizePath(std::string &path);
// アラインが必要な section データを再配置するための単純なアロケータ
class dpSectionAllocator
{
public:
// data=NULL, size_t size=0xffffffff で初期化した場合、必要な容量を調べるのに使える
dpSectionAllocator(void *data=NULL, size_t size=0xffffffff);
// align: 2 の n 乗である必要がある
void* allocate(size_t size, size_t align);
size_t getUsed() const;
private:
void *m_data;
size_t m_size;
size_t m_used;
};
class dpTrampolineAllocator
{
public:
static const size_t page_size = 1024*64;
static const size_t block_size = 32;
dpTrampolineAllocator();
~dpTrampolineAllocator();
void* allocate(void *location);
bool deallocate(void *v);
private:
class Page;
typedef std::vector<Page*> page_cont;
page_cont m_pages;
Page* createPage(void *location);
Page* findOwnerPage(void *location);
Page* findCandidatePage(void *location);
};
template<size_t PageSize, size_t BlockSize>
class dpBlockAllocator
{
public:
static const size_t page_size = PageSize;
static const size_t block_size = BlockSize;
dpBlockAllocator();
~dpBlockAllocator();
void* allocate();
bool deallocate(void *v);
private:
class Page;
typedef std::vector<Page*> page_cont;
page_cont m_pages;
};
typedef dpBlockAllocator<1024*256, sizeof(dpSymbol)> dpSymbolAllocator;
class dpSymbolTable
{
public:
dpSymbolTable();
void addSymbol(dpSymbol *v);
void merge(const dpSymbolTable &v);
void sort();
void clear();
void enablePartialLink(bool v);
size_t getNumSymbols() const;
dpSymbol* getSymbol(size_t i);
dpSymbol* findSymbolByName(const char *name);
dpSymbol* findSymbolByAddress(void *sym);
// F: [](const dpSymbol *sym)
template<class F>
void eachSymbols(const F &f)
{
size_t n = getNumSymbols();
for(size_t i=0; i<n; ++i) { f(getSymbol(i)); }
}
private:
typedef std::vector<dpSymbol*> symbol_cont;
symbol_cont m_symbols;
bool m_partial_link;
};
#define dpGetBuilder() m_context->getBuilder()
#define dpGetPatcher() m_context->getPatcher()
#define dpGetLoader() m_context->getLoader()
struct dpConfigFile
{
int log_flags;
int sys_flags;
int vc_ver;
std::vector<std::string> loads;
std::vector<std::string> source_paths;
std::vector<std::string> module_paths;
std::vector<std::string> preload_paths;
std::vector<std::string> msbuild_commands;
std::vector<std::string> build_commands;
std::vector<std::string> force_host_symbol_patterns;
std::string config_path;
dpConfigFile();
bool copy(const char *name);
bool load();
bool load(const char *path);
bool load(const wchar_t *path);
};
class dpBinary
{
public:
dpBinary(dpContext *ctx);
virtual ~dpBinary();
virtual bool loadFile(const char *path)=0;
virtual bool loadMemory(const char *name, void *data, size_t datasize, dpTime filetime)=0;
virtual bool link()=0;
virtual bool partialLink(size_t section)=0;
virtual bool callHandler(dpEventType e)=0;
virtual dpSymbolTable& getSymbolTable()=0;
virtual const char* getPath() const=0;
virtual dpTime getLastModifiedTime() const=0;
virtual dpFileType getFileType() const=0;
// F: [](const dpSymbol *sym)
template<class F> void eachSymbols(const F &f) { getSymbolTable().eachSymbols(f); }
protected:
dpContext *m_context;
};
class dpObjFile : public dpBinary
{
public:
static const dpFileType FileType= dpE_Obj;
dpObjFile(dpContext *ctx);
~dpObjFile();
void unload();
virtual bool loadFile(const char *path);
virtual bool loadMemory(const char *name, void *data, size_t datasize, dpTime filetime);
virtual bool link();
virtual bool partialLink(size_t section);
virtual bool callHandler(dpEventType e);
virtual dpSymbolTable& getSymbolTable();
virtual const char* getPath() const;
virtual dpTime getLastModifiedTime() const;
virtual dpFileType getFileType() const;
void* getBaseAddress() const;
private:
struct RelocationData
{
uint32_t base;
RelocationData() : base(0) {}
};
struct LinkData // section と対になるデータ
{
uint32_t flags;
LinkData() : flags(dpE_NeedsLink|dpE_NeedsBase) {}
};
typedef std::vector<LinkData> link_cont;
typedef std::vector<RelocationData> reloc_cont;
void *m_data;
size_t m_size;
void *m_aligned_data;
size_t m_aligned_datasize;
std::string m_path;
dpTime m_mtime;
dpSymbolTable m_symbols;
link_cont m_linkdata;
reloc_cont m_relocdata;
void* resolveSymbol(const char *name);
};
class dpLibFile : public dpBinary
{
public:
static const dpFileType FileType = dpE_Lib;
dpLibFile(dpContext *ctx);
~dpLibFile();
void unload();
virtual bool loadFile(const char *path);
virtual bool loadMemory(const char *name, void *data, size_t datasize, dpTime filetime);
virtual bool link();
virtual bool partialLink(size_t section);
virtual bool callHandler(dpEventType e);
virtual dpSymbolTable& getSymbolTable();
virtual const char* getPath() const;
virtual dpTime getLastModifiedTime() const;
virtual dpFileType getFileType() const;
size_t getNumObjFiles() const;
dpObjFile* getObjFile(size_t index);
dpObjFile* findObjFile(const char *name);
template<class F>
void eachObjs(const F &f) { dpEach(m_objs, f); }
private:
typedef std::vector<dpObjFile*> obj_cont;
obj_cont m_objs;
dpSymbolTable m_symbols;
std::string m_path;
dpTime m_mtime;
};
// ロード中の dll は上書き不可能で、そのままだと実行時リビルドできない。
// そのため、指定のファイルをコピーしてそれを扱う。(関連する .pdb もコピーする)
// また、.dll だけでなく .exe も扱える (export された symbol がないと意味がないが)
class dpDllFile : public dpBinary
{
public:
static const dpFileType FileType = dpE_Dll;
dpDllFile(dpContext *ctx);
~dpDllFile();
void unload();
virtual bool loadFile(const char *path);
virtual bool loadMemory(const char *name, void *data, size_t datasize, dpTime filetime);
virtual bool link();
virtual bool partialLink(size_t section);
virtual bool callHandler(dpEventType e);
virtual dpSymbolTable& getSymbolTable();
virtual const char* getPath() const;
virtual dpTime getLastModifiedTime() const;
virtual dpFileType getFileType() const;
private:
HMODULE m_module;
bool m_needs_freelibrary;
std::string m_path;
std::string m_actual_file;
std::string m_pdb_path;
dpTime m_mtime;
dpSymbolTable m_symbols;
};
class dpLoader
{
public:
dpLoader(dpContext *ctx);
~dpLoader();
dpBinary* load(const char *path); // path to .obj, .lib, .dll, .exe
dpObjFile* loadObj(const char *path);
dpLibFile* loadLib(const char *path);
dpDllFile* loadDll(const char *path);
bool unload(const char *path);
size_t reload();
bool link();
size_t getNumBinaries() const;
dpBinary* getBinary(size_t index);
dpBinary* findBinary(const char *name);
dpSymbol* findSymbolByName(const char *name);
dpSymbol* findSymbolByAddress(void *addr);
dpSymbol* findHostSymbolByName(const char *name);
dpSymbol* findHostSymbolByAddress(void *addr);
// F: [](dpBinary *bin)
template<class F>
void eachBinaries(const F &f)
{
size_t n = getNumBinaries();
for(size_t i=0; i<n; ++i) { f(getBinary(i)); }
}
void addOnLoadList(dpBinary *bin);
dpSymbol* newSymbol(const char *nam=nullptr, void *addr=nullptr, int fla=0, int sect=0, dpBinary *bin=nullptr);
void deleteSymbol(dpSymbol *sym);
void addForceHostSymbolPattern(const char *pattern);
bool doesForceHostSymbol(const char *name);
bool loadMapFile(const char *path, void *imagebase);
size_t loadMapFiles();
private:
typedef std::vector<dpBinary*> binary_cont;
typedef std::vector<std::regex> pattern_cont;
typedef std::set<std::string> string_set;
dpContext *m_context;
string_set m_mapfiles_read;
pattern_cont m_force_host_symbol_patterns;
binary_cont m_binaries;
binary_cont m_onload_queue;
dpSymbolTable m_hostsymbols;
dpSymbolAllocator m_symalloc;
void unloadImpl(dpBinary *bin);
template<class BinaryType>
BinaryType* loadBinaryImpl(const char *path);
};
class dpPatcher
{
public:
dpPatcher(dpContext *ctx);
~dpPatcher();
void* patchByBinary(dpBinary *obj, const std::function<bool (const dpSymbolS&)> &condition);
void* patch(dpSymbol *target, dpSymbol *hook);
size_t unpatchByBinary(dpBinary *obj);
bool unpatchByAddress(void *patched);
void unpatchAll();
dpPatchData* findPatchByName(const char *name);
dpPatchData* findPatchByAddress(void *addr);
private:
typedef std::set<dpPatchData> patch_cont;
dpContext *m_context;
dpTrampolineAllocator m_talloc;
patch_cont m_patches;
void patchImpl(dpPatchData &pi);
void unpatchImpl(const dpPatchData &pi);
patch_cont::iterator findPatchByNameImpl(const char *name);
patch_cont::iterator findPatchByAddressImpl(void *addr);
};
class dpBuilder
{
public:
class ScopedPreloadLock
{
public:
ScopedPreloadLock(dpBuilder *v) : m_builder(v) { m_builder->lockPreload(); }
~ScopedPreloadLock() { m_builder->unlockPreload(); }
dpBuilder *m_builder;
};
dpBuilder(dpContext *ctx);
~dpBuilder();
void addModulePath(const char *path);
void addSourcePath(const char *path);
void addPreloadPath(const char *path);
void addMSBuildCommand(const char *msbuild_options);
void addCLBuildCommand(const char *cl_options);
void addBuildCommand(const char *any_command);
bool startAutoBuild();
bool stopAutoBuild();
bool startPreload();
bool stopPreload();
void lockPreload();
void unlockPreload();
void update();
size_t reload();
void watchFiles();
bool build();
void preload();
const char* getVCVarsPath() const;
private:
struct SourcePath
{
std::string path;
HANDLE notifier;
};
dpContext *m_context;
std::string m_vcvars;
std::vector<std::string> m_build_commands;
std::vector<SourcePath> m_srcpathes;
std::vector<std::string> m_loadpathes;
std::vector<std::string> m_preloadpathes;
mutable bool m_build_done;
bool m_watchfile_stop;
HANDLE m_thread_autobuild;
HANDLE m_thread_preload;
bool m_preload_stop;
dpMutex m_mtx_preload;
};
class dpContext
{
public:
dpContext();
~dpContext();
dpBuilder* getBuilder();
dpPatcher* getPatcher();
dpLoader* getLoader();
size_t load(const char *path);
size_t patchByFile(const char *filename, const char *filter_regex);
size_t patchByFile(const char *filename, const std::function<bool (const dpSymbolS&)> &condition);
bool patchNameToName(const char *target_name, const char *hook_name);
bool patchAddressToName(const char *target_name, void *hook);
bool patchAddressToAddress(void *target, void *hook);
bool patchByAddress(void *hook);
bool unpatchByAddress(void *target_or_hook_addr);
void unpatchAll();
void* getUnpatched(void *target_or_hook_addr);
void addForceHostSymbolPattern(const char *pattern);
private:
dpBuilder *m_builder;
dpPatcher *m_patcher;
dpLoader *m_loader;
};
template<class F>
inline void dpGlob(const char *path, const F &f)
{
std::string dir;
dpSeparateDirFile(path, &dir, nullptr);
WIN32_FIND_DATAA wfdata;
HANDLE handle = ::FindFirstFileA(path, &wfdata);
if(handle!=INVALID_HANDLE_VALUE) {
do {
f( dir+wfdata.cFileName );
} while(::FindNextFileA(handle, &wfdata));
::FindClose(handle);
}
}
// F: [](size_t size) -> void* : alloc func
template<class F>
inline bool dpMapFile(const char *path, void *&o_data, size_t &o_size, const F &alloc)
{
o_data = NULL;
o_size = 0;
if(FILE *f=fopen(path, "rb")) {
fseek(f, 0, SEEK_END);
o_size = ftell(f);
if(o_size > 0) {
o_data = alloc(o_size);
fseek(f, 0, SEEK_SET);
fread(o_data, 1, o_size, f);
}
fclose(f);
return true;
}
return false;
}
template<class Container, class F>
inline void dpEach(Container &cont, const F &f)
{
std::for_each(cont.begin(), cont.end(), f);
}
template<class Container, class F>
inline auto dpFind(Container &cont, const F &f) -> decltype(cont.begin())
{
return std::find_if(cont.begin(), cont.end(), f);
}