Skip to content

Commit 4d657c7

Browse files
修复bug
1 parent b249e03 commit 4d657c7

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

app/src/main/cpp/il2cpp_trace.cpp

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ char module_path[PATH_MAX];
2121
static uint64_t il2cpp_base = 0;
2222
int hook_fun_num=0;
2323
std::map<long,std::string> fun_name_dict;
24+
unsigned long start_addrs[MAX_VMA_NUM];
25+
unsigned long end_addrs[MAX_VMA_NUM];
26+
unsigned long vma_base[MAX_VMA_NUM];
27+
int vma_num=0;
2428

2529
void init_il2cpp_api(void *handle) {
2630
#define DO_API(r, n, p) { \
@@ -110,9 +114,16 @@ void hook_all_fun(){
110114
for (auto it = fun_name_dict.begin(); it != fun_name_dict.end(); ++it) {
111115
unsigned long fun_offset = it->first;
112116
std::string fun_name = it->second;
113-
int set_uprobe_ret = set_fun_info(fun_offset,(char*)fun_name.c_str());
117+
unsigned long fun_addr = il2cpp_base+fun_offset;
118+
unsigned long uprobe_offset;
119+
for (int i=0;i<vma_num;i++){
120+
if(fun_addr>start_addrs[i] && fun_addr<end_addrs[i]){
121+
uprobe_offset = fun_addr-start_addrs[i]+vma_base[i];
122+
}
123+
}
124+
int set_uprobe_ret = set_fun_info(uprobe_offset,fun_offset,(char*)fun_name.c_str());
114125
if(set_uprobe_ret!=SET_TRACE_SUCCESS){
115-
LOGE("set uprobe in fun_name:%s,fun_offset:0x%llx",fun_name.c_str(),fun_offset);
126+
LOGE("error set uprobe in fun_name:%s,fun_offset:0x%llx,uprobe_offset:%llx",fun_name.c_str(),fun_offset,uprobe_offset);
116127
}
117128
}
118129
LOGD("success hook fun num:%d",hook_fun_num);
@@ -154,6 +165,40 @@ void trace_type_info(Il2CppMetadataType type_info,char *clazzName) {
154165
check_all_methods(klass,clazzName);
155166
}
156167

168+
bool init_vma(){
169+
FILE *f;
170+
char buf[256];
171+
f = fopen("/proc/self/maps","r");
172+
if(!f){
173+
return false;
174+
}
175+
while (fgets(buf,256,f)!=NULL){
176+
unsigned long tstart,tend,tbase;
177+
char permissions[5];
178+
int major,minor;
179+
unsigned long inode;
180+
char path[256];
181+
182+
int fields = sscanf(buf,"%lx-%lx %4s %lx %x:%x %lu %s",&tstart,&tend,permissions,&tbase,&major,&minor,&inode,path);
183+
if(fields==8){
184+
if(strcmp(path,module_path)==0){
185+
// LOGD("start:%lx,end:%lx,permissions:%s,tbase:%lx\n",tstart,tend,permissions,tbase);
186+
if(permissions[2]=='x'){
187+
start_addrs[vma_num] = tstart;
188+
end_addrs[vma_num] = tend;
189+
vma_base[vma_num] = tbase;
190+
vma_num++;
191+
}
192+
}
193+
}
194+
195+
}
196+
fclose(f);
197+
if(vma_num==0){
198+
return false;
199+
}
200+
return true;
201+
}
157202

158203
void start_trace(char* data_dir_path){
159204
char trace_file_path[PATH_MAX];
@@ -164,6 +209,12 @@ void start_trace(char* data_dir_path){
164209
return;
165210
}
166211
LOGD("success get il2cpp api fun");
212+
bool parse_ret = init_vma();
213+
if(!parse_ret){
214+
LOGE("can not get vma info");
215+
return;
216+
}
217+
167218

168219
int set_module_base_ret = set_module_base(il2cpp_base);
169220
int set_target_file_ret = set_target_file(module_path);

app/src/main/cpp/il2cpp_trace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdint.h>
55

66
#define MAX_FULL_NAME_LEN 200
7+
#define MAX_VMA_NUM 10
78

89
struct Il2CppMetadataField
910
{

app/src/main/cpp/uprobe_trace_user.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum trace_info {
1111
SET_TARGET_FILE,
1212
SET_MODULE_BASE,
1313
SET_FUN_INFO,
14+
SET_TARGET_UPROBE,
1415
SET_TARGET_UID,
1516
CLEAR_UPROBE,
1617
};
@@ -32,9 +33,13 @@ int set_target_file(char* file_name){
3233
return ret;
3334
}
3435

35-
int set_fun_info(unsigned long fun_offset,char *fun_name){
36-
int ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
37-
return ret;
36+
int set_fun_info(unsigned long uprobe_offset,unsigned long fun_offset,char *fun_name){
37+
int insert_key_ret = syscall(__NR_mincore,fun_offset,TRACE_FLAG+SET_FUN_INFO,fun_name);
38+
if(insert_key_ret==SET_TRACE_SUCCESS){
39+
int ret = syscall(__NR_mincore,uprobe_offset,TRACE_FLAG+SET_TARGET_UPROBE,"");
40+
return ret;
41+
}
42+
return SET_TRACE_ERROR;
3843
}
3944

4045
int clear_all_uprobes(){

0 commit comments

Comments
 (0)