| 2030 | } |
| 2031 | |
| 2032 | int uninstall_java_hook(int hook_index) { |
| 2033 | if (hook_index < 0 || hook_index >= g_java_hook_count) { |
| 2034 | LOGE("Invalid hook index: %d", hook_index); |
| 2035 | return -1; |
| 2036 | } |
| 2037 | |
| 2038 | pthread_mutex_lock(&g_java_hook_mutex); |
| 2039 | |
| 2040 | JavaHookInfo* hook = &g_java_hooks[hook_index]; |
| 2041 | |
| 2042 | if (!hook->is_hooked) { |
| 2043 | LOGI("Hook %d already uninstalled", hook_index); |
| 2044 | pthread_mutex_unlock(&g_java_hook_mutex); |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
| 2048 | const ArtMethodOffsets* offsets = get_art_method_offsets(); |
| 2049 | void** entry_point_ptr = (void**)((uintptr_t)hook->art_method + offsets->entry_point_offset); |
| 2050 | |
| 2051 | void* page = (void*)((uintptr_t)entry_point_ptr & ~(PAGE_SIZE - 1)); |
| 2052 | if (mprotect(page, PAGE_SIZE, PROT_READ | PROT_WRITE) != 0) { |
| 2053 | LOGE("Failed to change page protection for unhook: %s", strerror(errno)); |
| 2054 | pthread_mutex_unlock(&g_java_hook_mutex); |
| 2055 | return -1; |
| 2056 | } |
| 2057 | |
| 2058 | if (hook->was_nativized) { |
| 2059 | uint32_t* access_flags_ptr = (uint32_t*)((uintptr_t)hook->art_method + offsets->access_flags_offset); |
| 2060 | LOGI("Restoring original access_flags: 0x%x", hook->original_access_flags); |
| 2061 | *access_flags_ptr = hook->original_access_flags; |
| 2062 | __builtin___clear_cache((char*)access_flags_ptr, (char*)access_flags_ptr + 4); |
| 2063 | } |
| 2064 | |
| 2065 | *entry_point_ptr = hook->original_entry_point; |
| 2066 | __builtin___clear_cache((char*)entry_point_ptr, (char*)entry_point_ptr + 8); |
| 2067 | |
| 2068 | if (hook->hook_trampoline) { |
| 2069 | munmap(hook->hook_trampoline, PAGE_SIZE); |
| 2070 | hook->hook_trampoline = NULL; |
| 2071 | } |
| 2072 | |
| 2073 | if (g_lua_engine) { |
| 2074 | lua_State* L = lua_engine_get_state(g_lua_engine); |
| 2075 | if (L) { |
| 2076 | if (hook->lua_onEnter_ref != LUA_NOREF) { |
| 2077 | luaL_unref(L, LUA_REGISTRYINDEX, hook->lua_onEnter_ref); |
| 2078 | } |
| 2079 | if (hook->lua_onLeave_ref != LUA_NOREF) { |
| 2080 | luaL_unref(L, LUA_REGISTRYINDEX, hook->lua_onLeave_ref); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | hook->lua_onEnter_ref = LUA_NOREF; |
| 2086 | hook->lua_onLeave_ref = LUA_NOREF; |
| 2087 | hook->is_hooked = false; |
| 2088 | |
| 2089 | pthread_mutex_unlock(&g_java_hook_mutex); |
no test coverage detected