Skip to content

Commit f2c15e3

Browse files
committed
add mymap
1 parent c4c3290 commit f2c15e3

File tree

6 files changed

+765
-54
lines changed

6 files changed

+765
-54
lines changed

Android.mk

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ LOCAL_MODULE_FILENAME := libbootstrap
1111
cmd-strip = $(TOOLCHAIN_PREFIX)strip --strip-unneeded -x $1
1212

1313
LOCAL_SRC_FILES := \
14-
xhook/xhook.c \
15-
xhook/xh_core.c \
16-
xhook/xh_elf.c \
17-
xhook/xh_jni.c \
18-
xhook/xh_log.c \
19-
xhook/xh_util.c \
20-
xhook/xh_version.c \
14+
xhook/xhook.c \
15+
xhook/xh_core.c \
16+
xhook/xh_elf.c \
17+
xhook/xh_jni.c \
18+
xhook/xh_log.c \
19+
xhook/xh_util.c \
20+
xhook/xh_version.c \
21+
map/mymap32.cpp \
2122
bootstrap.cpp \
2223
zip/shadow_zip.cpp \
2324
zip/ZipEntry.cpp \
2425
zip/ZipFile.cpp
2526

26-
LOCAL_CFLAGS := -Izip -DTARGET_ARCH_ABI=\"$(TARGET_ARCH_ABI)\" -std=c11 -fvisibility=hidden
27-
LOCAL_CPPFLAGS := -Izip -DTARGET_ARCH_ABI=\"$(TARGET_ARCH_ABI)\" -std=c++11 -fvisibility=hidden
27+
LOCAL_CFLAGS := -Izip -Imap -DTARGET_ARCH_ABI=\"$(TARGET_ARCH_ABI)\" -std=c11 -fvisibility=hidden
28+
LOCAL_CPPFLAGS := -Izip -Imap -DTARGET_ARCH_ABI=\"$(TARGET_ARCH_ABI)\" -std=c++11 -fvisibility=hidden
2829

2930
LOCAL_LDLIBS := -llog -L$(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)
3031

bootstrap.cpp

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
#include "zip/shadow_zip.h"
1010
#include "xhook/xhook.h"
1111
#include "io_github_noodle1983_Boostrap.h"
12+
#include "mymap32.h"
1213

1314
struct GlobalData
1415
{
15-
std::map<int, FILE*> g_fd_to_file;
16-
std::map<FILE*, ShadowZip*> g_file_to_shadowzip;
16+
/*std::map<int, FILE*>*/MyMap32 g_fd_to_file;
17+
/*std::map<FILE*, ShadowZip*>*/MyMap32 g_file_to_shadowzip;
1718
PthreadRwMutex g_file_to_shadowzip_mutex;
1819
};
1920
#define g_global_data (LeakSingleton<GlobalData, 0>::instance())
@@ -45,7 +46,10 @@ JNIEXPORT void JNICALL Java_io_github_noodle1983_Boostrap_init
4546
jenv->ReleaseStringUTFChars(path, data_file_path);
4647
MY_INFO("data file path:%s", g_data_file_path);
4748

49+
//all leak
4850
LeakSingleton<GlobalData, 0>::init();
51+
g_global_data->g_fd_to_file.init(4096, 4096, "fd_to_file");
52+
g_global_data->g_file_to_shadowzip.init(8192, 8192, "file_to_shadowzip");
4953
bootstrap();
5054
}
5155

@@ -539,19 +543,20 @@ static int my_stat(const char *path, struct stat *file_stat)
539543
static ShadowZip* get_cached_shadowzip(FILE *stream)
540544
{
541545
PthreadReadGuard(g_global_data->g_file_to_shadowzip_mutex);
542-
std::map<FILE*, ShadowZip*>::iterator it = g_global_data->g_file_to_shadowzip.find(stream);
543-
ShadowZip* shadow_zip = (it == g_global_data->g_file_to_shadowzip.end()) ? NULL : it->second;
546+
ShadowZip* shadow_zip = NULL;
547+
g_global_data->g_file_to_shadowzip.find((intptr_t)stream, (intptr_t&)shadow_zip);
544548
return shadow_zip;
545549
}
546550

547551
static ShadowZip* get_cached_shadowzip(int fd)
548552
{
549553
PthreadReadGuard(g_global_data->g_file_to_shadowzip_mutex);
550-
std::map<int, FILE*>::iterator it_fd = g_global_data->g_fd_to_file.find(fd);
551-
FILE* stream = (it_fd == g_global_data->g_fd_to_file.end()) ? NULL : it_fd->second;
554+
FILE * stream = NULL;
555+
g_global_data->g_fd_to_file.find((intptr_t)fd, (intptr_t&)stream);
552556
if (stream == NULL){return NULL;}
553-
std::map<FILE*, ShadowZip*>::iterator it = g_global_data->g_file_to_shadowzip.find(stream);
554-
ShadowZip* shadow_zip = (it == g_global_data->g_file_to_shadowzip.end()) ? NULL : it->second;
557+
558+
ShadowZip* shadow_zip = NULL;
559+
g_global_data->g_file_to_shadowzip.find((intptr_t)stream, (intptr_t&)shadow_zip);
555560
return shadow_zip;
556561
}
557562

@@ -586,7 +591,7 @@ static FILE *my_fopen(const char *path, const char *mode)
586591

587592
MY_LOG("shadow apk in fopen: %s, fd:0x%08x, file*: 0x%08llx", path, fileno(fp), (unsigned long long)fp);
588593
PthreadWriteGuard(g_global_data->g_file_to_shadowzip_mutex);
589-
g_global_data->g_file_to_shadowzip[fp] = shadow_zip;
594+
g_global_data->g_file_to_shadowzip.set((intptr_t)fp, (intptr_t)shadow_zip);
590595
return fp;
591596
}
592597

@@ -667,27 +672,25 @@ static int my_fclose(FILE* stream)
667672
MY_METHOD("my_fclose: file*: 0x%08llx", (unsigned long long)stream);
668673

669674
ShadowZip* shadow_zip = NULL;
670-
{
675+
{
671676
PthreadWriteGuard(g_global_data->g_file_to_shadowzip_mutex);
672-
std::map<FILE*, ShadowZip*>::iterator it = g_global_data->g_file_to_shadowzip.find(stream);
673-
shadow_zip = (it == g_global_data->g_file_to_shadowzip.end()) ? NULL : it->second;
674-
if (it != g_global_data->g_file_to_shadowzip.end()){
675-
g_global_data->g_file_to_shadowzip.erase(it);
676-
}
677-
678-
int fd = fileno(stream);
679-
MY_METHOD("my_fclose: fd: 0x%08x, file*: 0x%08llx", fd, (unsigned long long)stream);
680-
std::map<int, FILE*>::iterator it_fd = g_global_data->g_fd_to_file.find(fd);
681-
if(it_fd != g_global_data->g_fd_to_file.end())
677+
if (g_global_data->g_file_to_shadowzip.find((intptr_t)stream, (intptr_t &)shadow_zip))
682678
{
683-
g_global_data->g_fd_to_file.erase(it_fd);
679+
g_global_data->g_file_to_shadowzip.del((intptr_t)stream);
680+
681+
int fd = fileno(stream);
682+
MY_METHOD("my_fclose: fd: 0x%08x, file*: 0x%08llx", fd, (unsigned long long)stream);
683+
g_global_data->g_fd_to_file.del((intptr_t)fd);
684684
}
685+
685686
}
686687

687688
if (shadow_zip == NULL){
688689
return old_fclose(stream);
689690
}else{
690-
return shadow_zip->fclose(stream);
691+
int ret = shadow_zip->fclose(stream);
692+
delete shadow_zip;
693+
return ret;
691694
}
692695
}
693696

@@ -752,8 +755,8 @@ static int my_open(const char *path, int flags, ...)
752755

753756
MY_LOG("shadow apk: %s, fd:0x%08x, file*: 0x%08llx", path, fd, (unsigned long long)fp);
754757
PthreadWriteGuard(g_global_data->g_file_to_shadowzip_mutex);
755-
g_global_data->g_fd_to_file[fd] = fp;
756-
g_global_data->g_file_to_shadowzip[fp] = shadow_zip;
758+
g_global_data->g_fd_to_file.set((intptr_t)fd, (intptr_t)fp);
759+
g_global_data->g_file_to_shadowzip.set((intptr_t)fp, (intptr_t)shadow_zip);
757760
return fd;
758761
}
759762

@@ -827,23 +830,25 @@ static int my_close(int fd)
827830
ShadowZip* shadow_zip = NULL;
828831
{
829832
PthreadWriteGuard(g_global_data->g_file_to_shadowzip_mutex);
830-
std::map<int, FILE*>::iterator it_fd = g_global_data->g_fd_to_file.find(fd);
831-
FILE* stream = (it_fd == g_global_data->g_fd_to_file.end()) ? NULL : it_fd->second;
832-
if (stream == NULL){return old_close(fd);}
833-
g_global_data->g_fd_to_file.erase(it_fd);
834-
835-
MY_METHOD("my_close: fd: 0x%08x, file*: 0x%08llx", fd, (unsigned long long)stream);
836-
std::map<FILE*, ShadowZip*>::iterator it = g_global_data->g_file_to_shadowzip.find(stream);
837-
shadow_zip = (it == g_global_data->g_file_to_shadowzip.end()) ? NULL : it->second;
838-
if (it != g_global_data->g_file_to_shadowzip.end()){
839-
g_global_data->g_file_to_shadowzip.erase(it);
840-
}
833+
FILE* stream = NULL;
834+
if(g_global_data->g_fd_to_file.find((intptr_t)fd, (intptr_t &)stream))
835+
{
836+
g_global_data->g_fd_to_file.del((intptr_t)fd);
837+
838+
MY_METHOD("my_close: fd: 0x%08x, file*: 0x%08llx", fd, (unsigned long long)stream);
839+
if (g_global_data->g_file_to_shadowzip.find((intptr_t)stream, (intptr_t &)shadow_zip))
840+
{
841+
g_global_data->g_file_to_shadowzip.del((intptr_t)stream);
842+
}
843+
}
841844
}
842845

843846
if (shadow_zip == NULL){
844847
return old_close(fd);
845848
}else{
846-
return shadow_zip->fclose((FILE*)(size_t)fd);
849+
int ret = shadow_zip->fclose((FILE*)(size_t)fd);
850+
delete shadow_zip;
851+
return ret;
847852
}
848853
}
849854

log.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@
66
#if 1
77
#include <android/log.h>
88
#define MY_VERBOSE(...)
9-
#define MY_LOG(fmt,...) __android_log_print(ANDROID_LOG_DEBUG , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
10-
#define MY_METHOD(fmt,...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
11-
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
12-
#define MY_ERROR(fmt,...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
9+
#define MY_LOG(fmt,...) __android_log_print(ANDROID_LOG_DEBUG , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
10+
#define MY_METHOD(fmt,...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
11+
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
12+
#define MY_ERROR(fmt,...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
1313

1414
#elif 0
1515
#define MY_VERBOSE(fmt, ...)
16-
#define MY_LOG(fmt, ...) printf( "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
17-
#define MY_METHOD(fmt, ...) printf( "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
18-
#define MY_ERROR(fmt, ...) printf( "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
16+
#define MY_LOG(fmt, ...) printf( "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
17+
#define MY_METHOD(fmt, ...) printf( "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
18+
#define MY_ERROR(fmt, ...) printf( "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
1919

2020
#elif __ANDROID__
2121
#include <android/log.h>
2222
#define MY_VERBOSE(fmt, ...)
2323
#define MY_LOG(fmt, ...)
2424
#define MY_METHOD(fmt, ...)
25-
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
26-
#define MY_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%ld]" fmt "\n", pthread_self(), ##__VA_ARGS__)
25+
#define MY_INFO(fmt, ...) __android_log_print(ANDROID_LOG_INFO , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
26+
#define MY_ERROR(fmt, ...) __android_log_print(ANDROID_LOG_ERROR , "il2cpp", "[%lu]" fmt "\n", pthread_self(), ##__VA_ARGS__)
2727
#endif
2828

2929
#define ALOGD MY_LOG

0 commit comments

Comments
 (0)