-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgumpp.hpp
More file actions
447 lines (372 loc) · 13.1 KB
/
gumpp.hpp
File metadata and controls
447 lines (372 loc) · 13.1 KB
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
#ifndef __GUMPP_HPP__
#define __GUMPP_HPP__
#if !defined(GUMPP_STATIC) && defined(WIN32)
#ifdef GUMPP_EXPORTS
#define GUMPP_API __declspec(dllexport)
#else
#define GUMPP_API __declspec(dllimport)
#endif
#else
#define GUMPP_API
#endif
#define GUMPP_CAPI extern "C" GUMPP_API
#define GUMPP_MAX_BACKTRACE_DEPTH 16
#define GUMPP_MAX_PATH 260
#define GUMPP_MAX_SYMBOL_NAME 2048
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
#include <string>
#include <optional>
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#ifdef ABSOLUTE
#undef ABSOLUTE
#endif
#endif
namespace Gum {
template<typename T>
class RefPtr;
struct InvocationContext;
struct InvocationListener;
struct NoLeaveInvocationListener;
struct NoEnterInvocationListener;
struct CpuContext;
struct ReturnAddressArray;
struct Object {
virtual ~Object() {}
virtual void ref() = 0;
virtual void unref() = 0;
virtual void *get_handle() const = 0;
};
struct String : public Object {
virtual const char *c_str() = 0;
virtual size_t length() const = 0;
};
struct PtrArray : public Object {
virtual int length() = 0;
virtual void *nth(int n) = 0;
};
struct Interceptor : public Object {
virtual bool attach(void *function_address, InvocationListener *listener, void *listener_function_data = 0) = 0;
virtual void detach(InvocationListener *listener) = 0;
virtual bool attach(void *function_address, NoLeaveInvocationListener *listener, void *listener_function_data = 0) = 0;
virtual void detach(NoLeaveInvocationListener *listener) = 0;
virtual bool attach(void *function_address, NoEnterInvocationListener *listener, void *listener_function_data = 0) = 0;
virtual void detach(NoEnterInvocationListener *listener) = 0;
virtual void replace(void *function_address, void *replacement_address, void *replacement_data = 0, void **origin_function = 0) = 0;
virtual void revert(void *function_address) = 0;
virtual void begin_transaction() = 0;
virtual void end_transaction() = 0;
virtual void ignore_current_thread() = 0;
virtual void unignore_current_thread() = 0;
virtual void ignore_other_threads() = 0;
virtual void unignore_other_threads() = 0;
};
RefPtr<Interceptor> Interceptor_obtain(void);
std::unique_ptr<InvocationContext> get_current_invocation();
struct InvocationContext {
virtual ~InvocationContext() {}
virtual void *get_function() const = 0;
template<typename T>
T get_nth_argument(unsigned int n) const {
return reinterpret_cast<T>(get_nth_argument_ptr(n));
}
virtual void *get_nth_argument_ptr(unsigned int n) const = 0;
virtual void replace_nth_argument(unsigned int n, void *value) = 0;
template<typename T>
T get_return_value() const {
return static_cast<T>(get_return_value_ptr());
}
virtual void *get_return_value_ptr() const = 0;
virtual unsigned int get_thread_id() const = 0;
template<typename T>
T *get_listener_thread_data() const {
return static_cast<T *>(get_listener_thread_data_ptr(sizeof(T)));
}
virtual void *get_listener_thread_data_ptr(size_t required_size) const = 0;
template<typename T>
T *get_listener_function_data() const {
return static_cast<T *>(get_listener_function_data_ptr());
}
virtual void *get_listener_function_data_ptr() const = 0;
template<typename T>
T *get_listener_invocation_data() const {
return static_cast<T *>(get_listener_invocation_data_ptr(sizeof(T)));
}
virtual void *get_listener_invocation_data_ptr(
size_t required_size) const = 0;
template<typename T>
T *get_replacement_data() const {
return static_cast<T *>(get_replacement_data_ptr());
}
virtual void *get_replacement_data_ptr() const = 0;
virtual CpuContext *get_cpu_context() const = 0;
};
struct InvocationListener {
virtual ~InvocationListener() {}
virtual void on_enter(InvocationContext *context) = 0;
virtual void on_leave(InvocationContext *context) = 0;
};
struct NoLeaveInvocationListener {
virtual ~NoLeaveInvocationListener() {}
virtual void on_enter(InvocationContext *context) = 0;
};
struct NoEnterInvocationListener {
virtual ~NoEnterInvocationListener() {}
virtual void on_leave(InvocationContext *context) = 0;
};
struct Backtracer : public Object {
virtual void generate(const CpuContext *cpu_context, ReturnAddressArray &return_addresses) const = 0;
};
RefPtr<Backtracer> Backtracer_make_accurate();
RefPtr<Backtracer> Backtracer_make_fuzzy();
typedef void *ReturnAddress;
struct ReturnAddressArray {
unsigned int len;
ReturnAddress items[GUMPP_MAX_BACKTRACE_DEPTH];
};
struct ReturnAddressDetails {
ReturnAddress address;
char module_name[GUMPP_MAX_PATH + 1];
char function_name[GUMPP_MAX_SYMBOL_NAME + 1];
char file_name[GUMPP_MAX_PATH + 1];
unsigned int line_number;
unsigned int column;
};
GUMPP_CAPI bool ReturnAddressDetails_from_address(
ReturnAddress address,
ReturnAddressDetails &details);
RefPtr<PtrArray> find_matching_functions_array(const char *str);
RefPtr<String> get_function_name_from_addr(void *addr);
template<typename T>
class RefPtr {
public:
RefPtr(T *ptr_)
: ptr(ptr_) {}
explicit RefPtr(const RefPtr<T> &other)
: ptr(other.ptr) {
if (ptr)
ptr->ref();
}
template<class U>
RefPtr(const RefPtr<U> &other)
: ptr(other.operator->()) {
if (ptr)
ptr->ref();
}
RefPtr()
: ptr(0) {}
bool is_null() const { return ptr == 0 || ptr->get_handle() == 0; }
RefPtr &operator=(const RefPtr &other) {
RefPtr tmp(other);
swap(*this, tmp);
return *this;
}
RefPtr &operator=(T *other) {
RefPtr tmp(other);
swap(*this, tmp);
return *this;
}
T *operator->() const { return ptr; }
T &operator*() const { return *ptr; }
operator T *() { return ptr; }
T *get() const { return ptr; }
static void swap(RefPtr &a, RefPtr &b) {
T *tmp = a.ptr;
a.ptr = b.ptr;
b.ptr = tmp;
}
~RefPtr() {
if (ptr)
ptr->unref();
}
private:
T *ptr;
};
struct Module;
struct DebugSymbolDetails {
virtual ~DebugSymbolDetails() = default;
virtual void *address() const = 0;
virtual const char *module_name() const = 0;
virtual const char *symbol_name() const = 0;
virtual const char *file_name() const = 0;
virtual uint32_t line_number() const = 0;
virtual uint32_t column() const = 0;
};
struct MemoryRange;
namespace SymbolUtil {
std::unique_ptr<DebugSymbolDetails> details_from_address(void *addr);
std::optional<MemoryRange> function_range_from_address(void *addr);
void *find_function_end(void *addr, size_t max_size);
inline bool is_public_code(void *addr) {
#ifdef _WIN32
uint8_t *byte = (uint8_t *) addr;
#if defined(_M_AMD64)
if (byte[0] == 0xE9 || byte[0] == 0xEB)
return true;
if (byte[0] == 0xFF)
if ((byte[1] & 0x07) == 4 || (byte[1] & 0x07) == 5)
return true;
#elif defined(_M_IX86)
if (byte[0] == 0xE9 || byte[0] == 0xEA || byte[0] == 0xEB)
return true;
if (byte[0] == 0xFF)
if ((byte[1] & 0x07) == 4 || (byte[1] & 0x07) == 5)
return true;
#else
#error "unsupport"
#endif
#endif
return false;
}
void *find_function(const char *name);
inline std::vector<void *> find_matching_functions(const char *str, bool skip_public_code) {
RefPtr<PtrArray> functions =
RefPtr<PtrArray>(find_matching_functions_array(str));
std::vector<void *> result;
result.reserve(functions->length());
for (int i = functions->length() - 1; i >= 0; i--) {
auto addr = functions->nth(i);
if (!skip_public_code || !is_public_code(addr))
result.push_back(addr);
}
return result;
}
};// namespace SymbolUtil
struct MemoryRange {
void *base_address;
size_t size;
bool contains(void *addr) const noexcept {
return addr >= base_address && addr <= (void *) ((intptr_t) base_address + size);
}
};
enum class DependencyType {
DEPENDENCY_REGULAR,
DEPENDENCY_WEAK,
DEPENDENCY_REEXPORT,
DEPENDENCY_UPWARD,
};
struct DependencyDetails {
const char *name;
DependencyType type;
};
using FoundModuleFunc = std::function<bool(const Module &m)>;
enum class ExportType {
FUNCTION = 1,
VARIABLE
};
struct ExportDetails {
ExportType type;
const char *name;
void *address;
};
enum class ImportType {
UNKNOWN,
FUNCTION,
VARIABLE,
};
struct ImportDetails {
ImportType type;
const char *name;
const char *module;
void *address;
void **slot;
};
enum PageProtection {
NO_ACCESS = 0,
READ = (1 << 0),
WRITE = (1 << 1),
EXECUTE = (1 << 2),
};
enum SymbolType {
/* Common */
UNKNOWN,
SECTION,
/* Mach-O */
UNDEFINED,
ABSOLUTE,
PREBOUND_UNDEFINED,
INDIRECT,
/* ELF */
OBJECT,
FUNCTION,
FILE,
COMMON,
TLS,
};
struct SymbolSection {
const char *id;
PageProtection protection;
};
struct SectionDetails {
const char *id;
const char *name;
void *address;
size_t size;
};
struct SymbolDetails {
bool is_global;
SymbolType type;
const SymbolSection *section;
const char *name;
void *address;
ssize_t size;
};
struct FileMapping {
const char *path;
size_t offset;
size_t size;
};
struct RangeDetails {
const MemoryRange *range;
PageProtection protection;
const FileMapping *file;
};
struct MallocRangeDetails {
const MemoryRange *range;
};
struct Module : public Object {
virtual ~Module() = default;
virtual const char *name() const = 0;
virtual MemoryRange range() const = 0;
virtual const char *path() const = 0;
virtual void ensure_initialized() const = 0;
virtual void enumerate_imports(const std::function<bool(const ImportDetails &details)> &callback) const = 0;
virtual void enumerate_exports(const std::function<bool(const ExportDetails &details)> &callback) const = 0;
virtual void enumerate_symbols(const std::function<bool(const SymbolDetails &details)> &callback) const = 0;
virtual void enumerate_ranges(PageProtection prot,
const std::function<bool(const RangeDetails &range)> &callback) const = 0;
virtual void enumerate_sections(const std::function<bool(const SectionDetails §ion)> &callback) const = 0;
virtual void enumerate_dependencies(const std::function<bool(const DependencyDetails &details)> &callback) const = 0;
virtual void *find_export_by_name(const char *symbol_name) const = 0;
virtual void *find_symbol_by_name(const char *symbol_name) const = 0;
};
Module *module_load(const char *name, std::string *error = nullptr);
void *find_global_export_by_name(const char *symbol_name);
using ProcessId = uint32_t;
using ThreadId = size_t;
namespace Process {
Module *get_main_module();
Module *get_libc_module();
Module *find_module_by_name(const char *name);
Module *find_module_by_address(void *address);
void enumerate_modules(const FoundModuleFunc &func);
void enumerate_ranges(PageProtection prot, const std::function<bool(const RangeDetails &range)> &callback);
void enumerate_malloc_ranges(const std::function<bool(const MallocRangeDetails &range)> &callback);
ProcessId get_id();
ThreadId get_current_thread_id();
}// namespace Process
void runtime_init();
void runtime_deinit();
struct signature {
std::string pattern;
int8_t offset;
};
std::vector<void *> search_module_function(const Module &m, const char *pattern);
std::vector<const char *> search_module_string(const Module &m, const char *str);
}// namespace Gum
#endif