diff --git a/native/src/core/zygisk/gen_jni_hooks.py b/native/src/core/zygisk/gen_jni_hooks.py index a4b740d20..d4eb5a82c 100755 --- a/native/src/core/zygisk/gen_jni_hooks.py +++ b/native/src/core/zygisk/gen_jni_hooks.py @@ -212,8 +212,8 @@ server_samsung_q = ForkServer('samsung_q', [uid, gid, gids, runtime_flags, Anon( def gen_jni_def(name, clz, methods): decl = '' - decl += ind(0) + f'constexpr auto {name}_class = "{clz}";' - decl += ind(0) + f'std::array {name}_methods = {{{{' + decl += ind(0) + f'static constexpr auto {name}_class = "{clz}";' + decl += ind(0) + f'static std::array {name}_methods = {{{{' for i, m in enumerate(methods): decl += ind(1) + '{' decl += ind(2) + f'"{m.base_name()}",' @@ -231,7 +231,6 @@ def gen_jni_def(name, clz, methods): with open('jni_hooks.hpp', 'w') as f: f.write('// Generated by gen_jni_hooks.py\n') - f.write('\nnamespace {\n') zygote = 'com/android/internal/os/Zygote' @@ -239,4 +238,4 @@ with open('jni_hooks.hpp', 'w') as f: fas_l, fas_o, fas_p, fas_q_alt, fas_r, fas_u, fas_samsung_m, fas_samsung_n, fas_samsung_o, fas_samsung_p, spec_q, spec_q_alt, spec_r, spec_u, spec_samsung_q, server_l, server_samsung_q])) - f.write('\n} // namespace\n') + f.write('\n') diff --git a/native/src/core/zygisk/hook.cpp b/native/src/core/zygisk/hook.cpp index 0cb28fa71..d46d91806 100644 --- a/native/src/core/zygisk/hook.cpp +++ b/native/src/core/zygisk/hook.cpp @@ -77,13 +77,12 @@ using namespace std; // Some notes regarding the important functions/symbols during bootstrap: // // * NativeBridgeItf: this symbol is the entry point for android::LoadNativeBridge -// * HookContext::hook_plt(): hook functions like |dlclose| and |androidSetCreateThreadFunc| +// * HookContext::hook_plt(): hook functions like |dlclose| and |strdup| // * dlclose: the final step before android::LoadNativeBridge returns // * strdup: called in AndroidRuntime::start before calling specializations routines -// * HookContext::hook_jni_env(): replace the |RegisterNatives| function pointer in JNIEnv. -// * replace_jni_methods: replace the function pointers registered in register_jni_procs, -// most importantly the process specialization routines, which are our main targets. -// This marks the final step of the code injection bootstrap process. +// * HookContext::replace_jni_methods: replace the function pointers registered in +// register_jni_procs, most importantly the process specialization routines, which are our +// main targets. This marks the final step of the code injection bootstrap process. // * pthread_attr_destroy: called whenever the JVM tries to setup threads for itself. We use // this method to cleanup and unload Zygisk from the process. @@ -111,12 +110,11 @@ private: // features, such as loading modules and customizing process fork/specialization. ZygiskContext *g_ctx; -namespace { -HookContext *g_hook; -bool should_unmap_zygisk = false; -void *self_handle = nullptr; -constexpr const char *kZygiskInit = "com.android.internal.os.ZygoteInit"; -} + +static HookContext *g_hook; +static bool should_unmap_zygisk = false; +static void *self_handle = nullptr; +static constexpr const char *kZygiskInit = "com.android.internal.os.ZygoteInit"; // ----------------------------------------------------------------- @@ -124,7 +122,7 @@ constexpr const char *kZygiskInit = "com.android.internal.os.ZygoteInit"; ret (*old_##func)(__VA_ARGS__); \ ret new_##func(__VA_ARGS__) -DCL_HOOK_FUNC(char *, strdup, const char * str) { +DCL_HOOK_FUNC(static char *, strdup, const char * str) { if (strcmp(kZygiskInit, str) == 0) { g_hook->replace_jni_methods(); } @@ -404,7 +402,7 @@ void HookContext::hook_plt() { plt_backup.erase( std::remove_if(plt_backup.begin(), plt_backup.end(), [](auto &t) { return *std::get<3>(t) == nullptr;}), - g_hook->plt_backup.end()); + plt_backup.end()); } void HookContext::hook_unloader() { @@ -439,6 +437,7 @@ void HookContext::restore_plt_hook() { } // ----------------------------------------------------------------- + void HookContext::replace_jni_methods() { using method_sig = jint(*)(JavaVM **, jsize, jsize *); auto get_created_vms = reinterpret_cast( @@ -510,7 +509,7 @@ void hookJniNativeMethods(JNIEnv *env, const char *clz, JNINativeMethod *methods // It's normal that the method is not found if (env->RegisterNatives(clazz, &method, 1) == JNI_ERR || env->ExceptionCheck() == JNI_TRUE) { - if (auto *exception = env->ExceptionOccurred(); exception) { + if (auto *exception = env->ExceptionOccurred()) { env->DeleteLocalRef(exception); } env->ExceptionClear(); diff --git a/native/src/core/zygisk/jni_hooks.hpp b/native/src/core/zygisk/jni_hooks.hpp index e807350ce..4cf391255 100644 --- a/native/src/core/zygisk/jni_hooks.hpp +++ b/native/src/core/zygisk/jni_hooks.hpp @@ -1,9 +1,7 @@ // Generated by gen_jni_hooks.py -namespace { - -constexpr auto zygote_class = "com/android/internal/os/Zygote"; -std::array zygote_methods = {{ +static constexpr auto zygote_class = "com/android/internal/os/Zygote"; +static std::array zygote_methods = {{ { "nativeForkAndSpecialize", "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I", @@ -280,4 +278,3 @@ std::array zygote_methods = {{ }, }}; -} // namespace