diff --git a/edxp-core/src/main/cpp/external/yahfa/src/HookMain.c b/edxp-core/src/main/cpp/external/yahfa/src/HookMain.c index c5e85208..f96372fd 100644 --- a/edxp-core/src/main/cpp/external/yahfa/src/HookMain.c +++ b/edxp-core/src/main/cpp/external/yahfa/src/HookMain.c @@ -118,16 +118,6 @@ void *getEntryPoint(void* method) { } static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) { - if (hookCount >= hookCap) { - LOGI("not enough capacity. Allocating..."); - if (doInitHookCap()) { - LOGE("cannot hook method"); - return 1; - } - LOGI("Allocating done"); - } - - LOGI("replace method from %p to %p", fromMethod, toMethod); // replace entry point void *newEntrypoint = NULL; if(isBackup) { @@ -158,8 +148,6 @@ static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) { interpEntrypoint); } - hookCount += 1; - return 0; } diff --git a/edxp-core/src/main/cpp/external/yahfa/src/trampoline.c b/edxp-core/src/main/cpp/external/yahfa/src/trampoline.c index 58360b6c..da3523b6 100644 --- a/edxp-core/src/main/cpp/external/yahfa/src/trampoline.c +++ b/edxp-core/src/main/cpp/external/yahfa/src/trampoline.c @@ -16,10 +16,6 @@ #define MAX(a, b) ((a) > (b) ? (a) : (b)) -static _Thread_local unsigned char *trampolineCode; // place where trampolines are saved - -_Thread_local unsigned int hookCap = 1; -_Thread_local unsigned int hookCount = 1; // trampoline: // 1. set eax/rdi/r0/x0 to the hook ArtMethod addr @@ -127,22 +123,22 @@ unsigned char trampolineForBackup[] = { #endif -// trampoline size required for each hook -static unsigned int trampolineSize = roundUpToPtrSize(MAX(sizeof(trampoline), sizeof(trampolineForBackup))); - static inline void FlushCache(void *addr, size_t size) { __builtin___clear_cache((char *) addr, (char *) ((uintptr_t) addr + size)); } void *genTrampoline(void *toMethod, void *entrypoint) { - unsigned char *targetAddr = trampolineCode + trampolineSize * hookCount; + size_t size = entrypoint == NULL ? sizeof(trampoline) : sizeof(trampolineForBackup); + + // TODO: make use of thread_local to avoid frequent memory allocate + void *targetAddr = doInitHookCap(size); if (targetAddr == NULL) return NULL; if (entrypoint != NULL) { - memcpy(targetAddr, trampolineForBackup, sizeof(trampolineForBackup)); + memcpy(targetAddr, trampolineForBackup, size); } else { - memcpy(targetAddr, trampoline, sizeof(trampoline)); // do not use trampolineSize since it's a rounded size + memcpy(targetAddr, trampoline, size); } // replace with the actual ArtMethod addr @@ -184,7 +180,7 @@ void *genTrampoline(void *toMethod, void *entrypoint) { #else #error Unsupported architecture #endif - FlushCache(targetAddr, trampolineSize); + FlushCache(targetAddr, size); return targetAddr; } @@ -204,20 +200,17 @@ void setupTrampoline(uint8_t offset) { #endif } -int doInitHookCap() { - if (hookCap > hookCount) { - return 0; +void *doInitHookCap(size_t size) { + if (size == 0) { + LOGE("invalid capacity: %zx", size); + return NULL; } - hookCap *= 2; - unsigned int allSize = trampolineSize * hookCap; - unsigned char *buf = mmap(NULL, allSize, PROT_READ | PROT_WRITE | PROT_EXEC, + unsigned char *buf = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (buf == MAP_FAILED) { LOGE("mmap failed, errno = %s", strerror(errno)); - return 1; + return NULL; } - hookCount = 0; - trampolineCode = buf; - return 0; + return buf; } diff --git a/edxp-core/src/main/cpp/external/yahfa/src/trampoline.h b/edxp-core/src/main/cpp/external/yahfa/src/trampoline.h index eea4ea19..f5fca72a 100644 --- a/edxp-core/src/main/cpp/external/yahfa/src/trampoline.h +++ b/edxp-core/src/main/cpp/external/yahfa/src/trampoline.h @@ -7,12 +7,9 @@ extern int SDKVersion; -extern _Thread_local unsigned int hookCap; // capacity for trampolines -extern _Thread_local unsigned int hookCount; // current count of used trampolines - extern unsigned char trampoline[]; -int doInitHookCap(); +void* doInitHookCap(size_t cap); void setupTrampoline(uint8_t offset); void *genTrampoline(void *toMethod, void *entrypoint);