diff --git a/native/src/core/zygisk/entry.cpp b/native/src/core/zygisk/entry.cpp index e8def767d..5a5b3abc6 100644 --- a/native/src/core/zygisk/entry.cpp +++ b/native/src/core/zygisk/entry.cpp @@ -13,12 +13,11 @@ using namespace std; -void *self_handle = nullptr; string native_bridge = "0"; static bool is_compatible_with(uint32_t) { auto name = get_prop(NBPROP); - android_logging(); + zygisk_logging(); hook_functions(); ZLOGD("load success\n"); return false; diff --git a/native/src/core/zygisk/hook.cpp b/native/src/core/zygisk/hook.cpp index 53f970305..b70f9a120 100644 --- a/native/src/core/zygisk/hook.cpp +++ b/native/src/core/zygisk/hook.cpp @@ -124,6 +124,7 @@ private: ZygiskContext *g_ctx; static HookContext *g_hook; static bool should_unmap_zygisk = false; +static void *self_handle = nullptr; // ----------------------------------------------------------------- @@ -173,6 +174,16 @@ DCL_HOOK_FUNC(static void, android_log_close) { old_android_log_close(); } +// It should be safe to assume all dlclose's in libnativebridge are for zygisk_loader +DCL_HOOK_FUNC(static int, dlclose, void *handle) { + if (!self_handle) { + ZLOGV("dlclose zygisk_loader\n"); + self_handle = handle; + g_hook->post_native_bridge_load(); + } + return 0; +} + // We cannot directly call `dlclose` to unload ourselves, otherwise when `dlclose` returns, // it will return to our code which has been unmapped, causing segmentation fault. // Instead, we hook `pthread_attr_destroy` which will be called when VM daemon threads start. @@ -193,7 +204,7 @@ DCL_HOOK_FUNC(static int, pthread_attr_destroy, void *target) { // Because both `pthread_attr_destroy` and `dlclose` have the same function signature, // we can use `musttail` to let the compiler reuse our stack frame and thus // `dlclose` will directly return to the caller of `pthread_attr_destroy`. - [[clang::musttail]] return dlclose(self_handle); + [[clang::musttail]] return old_dlclose(self_handle); } } @@ -201,16 +212,6 @@ DCL_HOOK_FUNC(static int, pthread_attr_destroy, void *target) { return res; } -// it should be safe to assume all dlclose's in libnativebridge are for zygisk_loader -DCL_HOOK_FUNC(static int, dlclose, void *handle) { - if (!self_handle) { - ZLOGV("dlclose zygisk_loader\n"); - self_handle = handle; - g_hook->post_native_bridge_load(); - } - return 0; -} - #undef DCL_HOOK_FUNC // ----------------------------------------------------------------- diff --git a/native/src/core/zygisk/zygisk.hpp b/native/src/core/zygisk/zygisk.hpp index 49ce762f1..46c794dd8 100644 --- a/native/src/core/zygisk/zygisk.hpp +++ b/native/src/core/zygisk/zygisk.hpp @@ -31,8 +31,6 @@ enum : int { #define ZLOGV(...) ZLOGD(__VA_ARGS__) //#define ZLOGV(...) (void*)0 -extern void *self_handle; - void hook_functions(); void hookJniNativeMethods(JNIEnv *env, const char *clz, JNINativeMethod *methods, int numMethods);