Minor zygisk refactoring

This commit is contained in:
topjohnwu 2024-01-25 00:17:47 -08:00
parent f2c4288d2d
commit 930bb8687f
3 changed files with 13 additions and 15 deletions

View File

@ -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;

View File

@ -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
// -----------------------------------------------------------------

View File

@ -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);