-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcapstone.h
104 lines (91 loc) · 2.49 KB
/
capstone.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
#include "capstone/capstone.h"
#ifdef USE_TA
void* my_cs_malloc(size_t size) {
stub_log("malloc %d\n", size);
void *ret = ta_alloc(size);
stub_log("malloc end");
return ret;
}
void* my_cs_calloc(size_t nmemb, size_t size) {
stub_log("calloc: %x\n", nmemb * size);
void *ret = ta_calloc(nmemb, size);
stub_log("calloc_ret: %llx\n", ret);
stub_log("calloc end\n");
return ret;
}
void* my_cs_realloc(void *ptr, size_t size){
stub_log("realloc %d\n", size);
void *new = ta_alloc(size);
memcpy(new, ptr, size);
ta_free(ptr);
stub_log("realloc end\n");
return new;
}
void my_cs_free(void *ptr) {
stub_log("free\n");
//ta_free(ptr);
//free(ptr);
}
int my_cs_vsnprintf(char *str, size_t size, const char *format, va_list ap){
stub_log("%s", format);
return vsnprintf(str, size, format, ap);
}
#else
void* my_cs_malloc(size_t size) {
stub_log("malloc %d\n", size);
//void *ret = ta_alloc(size);
void *ret = malloc(size);
stub_log("malloc end\n");
return ret;
}
void* my_cs_calloc(size_t nmemb, size_t size) {
stub_log("calloc %d\n", nmemb * size);
void *ret = malloc(nmemb * size);
memset(ret, 0, nmemb * size);
//void *ret = ta_calloc(nmemb, size);
stub_log("calloc_ret: %llx\n", ret);
stub_log("calloc end\n");
return ret;
}
void* my_cs_realloc(void *ptr, size_t size){
stub_log("realloc %d\n", size);
//void *new = ta_alloc(size);
void *new = malloc(size);
memcpy(new, ptr, size);
free(ptr);
//ta_free(ptr);
stub_log("realloc end\n");
return new;
}
void my_cs_free(void *ptr) {
stub_log("free\n");
//ta_free(ptr);
//free(ptr);
}
int my_cs_vsnprintf(char *str, size_t size, const char *format, va_list ap){
stub_log("%s", format);
return vsnprintf(str, size, format, ap);
}
#endif
#undef vsnprintf
cs_opt_mem cs_mem_setup = {
.malloc = my_cs_malloc,
.calloc = my_cs_calloc,
.realloc = my_cs_realloc,
.free = my_cs_free,
.vsnprintf_ = my_cs_vsnprintf,
};
csh cs_handle;
void setup_capstone() {
printf("Set up cs_opt_mem\n", 0);
cs_option(0, CS_OPT_MEM, &cs_mem_setup);
printf("Starting to cs_open\n", 0);
int ret = cs_open(CS_ARCH_ARM64, CS_MODE_ARM, &cs_handle);
if (ret) {
printf("ERROR: Failed to initialize engine!, err=%d\n", ret);
return;
}
printf("Successfully initialized capstone engine\n", 0);
cs_option(cs_handle, CS_OPT_SKIPDATA, CS_OPT_ON);
printf("Set up skipdata\n", 0);
}