From 200665c48af53cc5612af77880e0518713b970bf Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 7 Dec 2025 02:43:56 -0800 Subject: [PATCH] Make jni_hooks.hpp a normal C++ header [skip ci] --- native/src/core/zygisk/gen_jni_hooks.py | 9 ++++-- native/src/core/zygisk/hook.cpp | 36 +++++++++++---------- native/src/core/zygisk/jni_hooks.hpp | 43 ++++++++++++++----------- 3 files changed, 50 insertions(+), 38 deletions(-) diff --git a/native/src/core/zygisk/gen_jni_hooks.py b/native/src/core/zygisk/gen_jni_hooks.py index 9adf953f2..b35597727 100755 --- a/native/src/core/zygisk/gen_jni_hooks.py +++ b/native/src/core/zygisk/gen_jni_hooks.py @@ -544,7 +544,7 @@ def gen_jni_def(field: str, methods: list[JNIHook]): decl += ind(2) + f'"{m.hook_target()}",' decl += ind(2) + f'"{m.jni_sig()}",' decl += ind(2) + f"(void *) +{m.cpp_lambda_sig()} {{" - orig_fn_ptr = f"g_hook->{field}[{i}].fnPtr" + orig_fn_ptr = f"get_defs()->{field}[{i}].fnPtr" decl += m.body(orig_fn_ptr) decl += ind(2) + "}" decl += ind(1) + "}," @@ -556,7 +556,10 @@ def gen_jni_def(field: str, methods: list[JNIHook]): with open("jni_hooks.hpp", "w") as f: f.write("// Generated by gen_jni_hooks.py\n") - + f.write("#pragma once\n\n") + f.write("struct JniHookDefinitions;\n") + f.write("static JniHookDefinitions *get_defs();\n\n") + f.write("struct JniHookDefinitions {\n") f.write( gen_jni_def( "fork_app_methods", @@ -585,4 +588,4 @@ with open("jni_hooks.hpp", "w") as f: f.write(gen_jni_def("fork_server_methods", [server_l, server_samsung_q])) - f.write("\n") + f.write("\n};\n") diff --git a/native/src/core/zygisk/hook.cpp b/native/src/core/zygisk/hook.cpp index b68f29730..6eedfdb4c 100644 --- a/native/src/core/zygisk/hook.cpp +++ b/native/src/core/zygisk/hook.cpp @@ -11,6 +11,7 @@ #include "zygisk.hpp" #include "module.hpp" +#include "jni_hooks.hpp" using namespace std; @@ -93,27 +94,11 @@ constexpr const char *kForkApp = "nativeForkAndSpecialize"; constexpr const char *kSpecializeApp = "nativeSpecializeAppProcess"; constexpr const char *kForkServer = "nativeForkSystemServer"; -// Global contexts: -// -// HookContext lives as long as Zygisk is loaded in memory. It tracks the process's function -// hooking state and bootstraps code injection until we replace the process specialization methods. -// -// ZygiskContext lives during the process specialization process. It implements Zygisk -// features, such as loading modules and customizing process fork/specialization. - -ZygiskContext *g_ctx; -struct HookContext; -static HookContext *g_hook; - using JNIMethods = std::span; using JNIMethodsDyn = std::pair, size_t>; -struct HookContext { -#include "jni_hooks.hpp" +struct HookContext : JniHookDefinitions { - // std::array fork_app_methods; - // std::array specialize_app_methods; - // std::array fork_server_methods; vector> plt_backup; const NativeBridgeRuntimeCallbacks *runtime_callbacks = nullptr; void *self_handle = nullptr; @@ -135,6 +120,23 @@ private: // ----------------------------------------------------------------- +// Global contexts: +// +// HookContext lives as long as Zygisk is loaded in memory. It tracks the process's function +// hooking state and bootstraps code injection until we replace the process specialization methods. +// +// ZygiskContext lives during the process specialization process. It implements Zygisk +// features, such as loading modules and customizing process fork/specialization. + +ZygiskContext *g_ctx; +static HookContext *g_hook; + +static JniHookDefinitions *get_defs() { + return g_hook; +} + +// ----------------------------------------------------------------- + #define DCL_HOOK_FUNC(ret, func, ...) \ ret (*old_##func)(__VA_ARGS__); \ ret new_##func(__VA_ARGS__) diff --git a/native/src/core/zygisk/jni_hooks.hpp b/native/src/core/zygisk/jni_hooks.hpp index c709780f2..2eadeac1b 100644 --- a/native/src/core/zygisk/jni_hooks.hpp +++ b/native/src/core/zygisk/jni_hooks.hpp @@ -1,4 +1,10 @@ // Generated by gen_jni_hooks.py +#pragma once + +struct JniHookDefinitions; +static JniHookDefinitions *get_defs(); + +struct JniHookDefinitions { std::array fork_app_methods = {{ // nativeForkAndSpecialize_l @@ -9,7 +15,7 @@ std::array fork_app_methods = {{ AppSpecializeArgs_v5 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir); ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[0].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[0].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -25,7 +31,7 @@ std::array fork_app_methods = {{ args.fds_to_ignore = &fds_to_ignore; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[1].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[1].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -42,7 +48,7 @@ std::array fork_app_methods = {{ args.is_child_zygote = &is_child_zygote; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[2].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[2].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -60,7 +66,7 @@ std::array fork_app_methods = {{ args.is_top_app = &is_top_app; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[3].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[3].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app ); ctx.nativeForkAndSpecialize_post(); @@ -82,7 +88,7 @@ std::array fork_app_methods = {{ args.mount_storage_dirs = &mount_storage_dirs; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[4].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[4].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs ); ctx.nativeForkAndSpecialize_post(); @@ -105,7 +111,7 @@ std::array fork_app_methods = {{ args.mount_sysprop_overrides = &mount_sysprop_overrides; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[5].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[5].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs, mount_sysprop_overrides ); ctx.nativeForkAndSpecialize_post(); @@ -128,7 +134,7 @@ std::array fork_app_methods = {{ args.mount_sysprop_overrides = &mount_sysprop_overrides; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[6].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[6].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app, use_fifo_ui, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs, mount_sysprop_overrides ); ctx.nativeForkAndSpecialize_post(); @@ -143,7 +149,7 @@ std::array fork_app_methods = {{ AppSpecializeArgs_v5 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir); ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[7].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[7].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _0, _1, nice_name, fds_to_close, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -158,7 +164,7 @@ std::array fork_app_methods = {{ AppSpecializeArgs_v5 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir); ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[8].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[8].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _2, _3, nice_name, fds_to_close, instruction_set, app_data_dir, _4 ); ctx.nativeForkAndSpecialize_post(); @@ -174,7 +180,7 @@ std::array fork_app_methods = {{ args.fds_to_ignore = &fds_to_ignore; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[9].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[9].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _5, _6, nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -191,7 +197,7 @@ std::array fork_app_methods = {{ args.is_child_zygote = &is_child_zygote; ZygiskContext ctx(env, &args); ctx.nativeForkAndSpecialize_pre(); - reinterpret_cast(g_hook->fork_app_methods[10].fnPtr)( + reinterpret_cast(get_defs()->fork_app_methods[10].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _7, _8, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir ); ctx.nativeForkAndSpecialize_post(); @@ -210,7 +216,7 @@ std::array specialize_app_methods = {{ args.is_child_zygote = &is_child_zygote; ZygiskContext ctx(env, &args); ctx.nativeSpecializeAppProcess_pre(); - reinterpret_cast(g_hook->specialize_app_methods[0].fnPtr)( + reinterpret_cast(get_defs()->specialize_app_methods[0].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir ); ctx.nativeSpecializeAppProcess_post(); @@ -226,7 +232,7 @@ std::array specialize_app_methods = {{ args.is_top_app = &is_top_app; ZygiskContext ctx(env, &args); ctx.nativeSpecializeAppProcess_pre(); - reinterpret_cast(g_hook->specialize_app_methods[1].fnPtr)( + reinterpret_cast(get_defs()->specialize_app_methods[1].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app ); ctx.nativeSpecializeAppProcess_post(); @@ -246,7 +252,7 @@ std::array specialize_app_methods = {{ args.mount_storage_dirs = &mount_storage_dirs; ZygiskContext ctx(env, &args); ctx.nativeSpecializeAppProcess_pre(); - reinterpret_cast(g_hook->specialize_app_methods[2].fnPtr)( + reinterpret_cast(get_defs()->specialize_app_methods[2].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs ); ctx.nativeSpecializeAppProcess_post(); @@ -267,7 +273,7 @@ std::array specialize_app_methods = {{ args.mount_sysprop_overrides = &mount_sysprop_overrides; ZygiskContext ctx(env, &args); ctx.nativeSpecializeAppProcess_pre(); - reinterpret_cast(g_hook->specialize_app_methods[3].fnPtr)( + reinterpret_cast(get_defs()->specialize_app_methods[3].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs, mount_sysprop_overrides ); ctx.nativeSpecializeAppProcess_post(); @@ -282,7 +288,7 @@ std::array specialize_app_methods = {{ args.is_child_zygote = &is_child_zygote; ZygiskContext ctx(env, &args); ctx.nativeSpecializeAppProcess_pre(); - reinterpret_cast(g_hook->specialize_app_methods[4].fnPtr)( + reinterpret_cast(get_defs()->specialize_app_methods[4].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _9, _10, nice_name, is_child_zygote, instruction_set, app_data_dir ); ctx.nativeSpecializeAppProcess_post(); @@ -299,7 +305,7 @@ std::array fork_server_methods = {{ ServerSpecializeArgs_v1 args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities); ZygiskContext ctx(env, &args); ctx.nativeForkSystemServer_pre(); - reinterpret_cast(g_hook->fork_server_methods[0].fnPtr)( + reinterpret_cast(get_defs()->fork_server_methods[0].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, rlimits, permitted_capabilities, effective_capabilities ); ctx.nativeForkSystemServer_post(); @@ -314,7 +320,7 @@ std::array fork_server_methods = {{ ServerSpecializeArgs_v1 args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities); ZygiskContext ctx(env, &args); ctx.nativeForkSystemServer_pre(); - reinterpret_cast(g_hook->fork_server_methods[1].fnPtr)( + reinterpret_cast(get_defs()->fork_server_methods[1].fnPtr)( env, clazz, uid, gid, gids, runtime_flags, _11, _12, rlimits, permitted_capabilities, effective_capabilities ); ctx.nativeForkSystemServer_post(); @@ -323,3 +329,4 @@ std::array fork_server_methods = {{ }, }}; +};