Revert "Thread safe"

This reverts commit f84db35a11.
This commit is contained in:
kotori0 2020-12-15 00:56:31 +08:00
parent 27cb8f0efd
commit 7c2efab7e0
3 changed files with 15 additions and 37 deletions

View File

@ -118,16 +118,6 @@ void *getEntryPoint(void* method) {
} }
static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) { 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 // replace entry point
void *newEntrypoint = NULL; void *newEntrypoint = NULL;
if(isBackup) { if(isBackup) {
@ -158,8 +148,6 @@ static int replaceMethod(void *fromMethod, void *toMethod, int isBackup) {
interpEntrypoint); interpEntrypoint);
} }
hookCount += 1;
return 0; return 0;
} }

View File

@ -16,10 +16,6 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #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: // trampoline:
// 1. set eax/rdi/r0/x0 to the hook ArtMethod addr // 1. set eax/rdi/r0/x0 to the hook ArtMethod addr
@ -127,22 +123,22 @@ unsigned char trampolineForBackup[] = {
#endif #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) { static inline void FlushCache(void *addr, size_t size) {
__builtin___clear_cache((char *) addr, (char *) ((uintptr_t) addr + size)); __builtin___clear_cache((char *) addr, (char *) ((uintptr_t) addr + size));
} }
void *genTrampoline(void *toMethod, void *entrypoint) { 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 (targetAddr == NULL) return NULL;
if (entrypoint != NULL) { if (entrypoint != NULL) {
memcpy(targetAddr, trampolineForBackup, sizeof(trampolineForBackup)); memcpy(targetAddr, trampolineForBackup, size);
} else { } 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 // replace with the actual ArtMethod addr
@ -184,7 +180,7 @@ void *genTrampoline(void *toMethod, void *entrypoint) {
#else #else
#error Unsupported architecture #error Unsupported architecture
#endif #endif
FlushCache(targetAddr, trampolineSize); FlushCache(targetAddr, size);
return targetAddr; return targetAddr;
} }
@ -204,20 +200,17 @@ void setupTrampoline(uint8_t offset) {
#endif #endif
} }
int doInitHookCap() { void *doInitHookCap(size_t size) {
if (hookCap > hookCount) { if (size == 0) {
return 0; LOGE("invalid capacity: %zx", size);
return NULL;
} }
hookCap *= 2; unsigned char *buf = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC,
unsigned int allSize = trampolineSize * hookCap;
unsigned char *buf = mmap(NULL, allSize, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (buf == MAP_FAILED) { if (buf == MAP_FAILED) {
LOGE("mmap failed, errno = %s", strerror(errno)); LOGE("mmap failed, errno = %s", strerror(errno));
return 1; return NULL;
} }
hookCount = 0; return buf;
trampolineCode = buf;
return 0;
} }

View File

@ -7,12 +7,9 @@
extern int SDKVersion; 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[]; extern unsigned char trampoline[];
int doInitHookCap(); void* doInitHookCap(size_t cap);
void setupTrampoline(uint8_t offset); void setupTrampoline(uint8_t offset);
void *genTrampoline(void *toMethod, void *entrypoint); void *genTrampoline(void *toMethod, void *entrypoint);