mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-02-19 20:38:30 +00:00
Minor code changes for hook.cpp
This commit is contained in:
parent
f247759a6e
commit
33cb39c8af
@ -212,8 +212,8 @@ server_samsung_q = ForkServer('samsung_q', [uid, gid, gids, runtime_flags, Anon(
|
|||||||
|
|
||||||
def gen_jni_def(name, clz, methods):
|
def gen_jni_def(name, clz, methods):
|
||||||
decl = ''
|
decl = ''
|
||||||
decl += ind(0) + f'constexpr auto {name}_class = "{clz}";'
|
decl += ind(0) + f'static constexpr auto {name}_class = "{clz}";'
|
||||||
decl += ind(0) + f'std::array<JNINativeMethod, {len(methods)}> {name}_methods = {{{{'
|
decl += ind(0) + f'static std::array<JNINativeMethod, {len(methods)}> {name}_methods = {{{{'
|
||||||
for i, m in enumerate(methods):
|
for i, m in enumerate(methods):
|
||||||
decl += ind(1) + '{'
|
decl += ind(1) + '{'
|
||||||
decl += ind(2) + f'"{m.base_name()}",'
|
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:
|
with open('jni_hooks.hpp', 'w') as f:
|
||||||
f.write('// Generated by gen_jni_hooks.py\n')
|
f.write('// Generated by gen_jni_hooks.py\n')
|
||||||
f.write('\nnamespace {\n')
|
|
||||||
|
|
||||||
zygote = 'com/android/internal/os/Zygote'
|
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_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]))
|
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')
|
||||||
|
@ -77,13 +77,12 @@ using namespace std;
|
|||||||
// Some notes regarding the important functions/symbols during bootstrap:
|
// Some notes regarding the important functions/symbols during bootstrap:
|
||||||
//
|
//
|
||||||
// * NativeBridgeItf: this symbol is the entry point for android::LoadNativeBridge
|
// * 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
|
// * dlclose: the final step before android::LoadNativeBridge returns
|
||||||
// * strdup: called in AndroidRuntime::start before calling specializations routines
|
// * strdup: called in AndroidRuntime::start before calling specializations routines
|
||||||
// * HookContext::hook_jni_env(): replace the |RegisterNatives| function pointer in JNIEnv.
|
// * HookContext::replace_jni_methods: replace the function pointers registered in
|
||||||
// * replace_jni_methods: replace the function pointers registered in register_jni_procs,
|
// register_jni_procs, most importantly the process specialization routines, which are our
|
||||||
// most importantly the process specialization routines, which are our main targets.
|
// main targets. This marks the final step of the code injection bootstrap process.
|
||||||
// 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
|
// * 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.
|
// 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.
|
// features, such as loading modules and customizing process fork/specialization.
|
||||||
|
|
||||||
ZygiskContext *g_ctx;
|
ZygiskContext *g_ctx;
|
||||||
namespace {
|
|
||||||
HookContext *g_hook;
|
static HookContext *g_hook;
|
||||||
bool should_unmap_zygisk = false;
|
static bool should_unmap_zygisk = false;
|
||||||
void *self_handle = nullptr;
|
static void *self_handle = nullptr;
|
||||||
constexpr const char *kZygiskInit = "com.android.internal.os.ZygoteInit";
|
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 (*old_##func)(__VA_ARGS__); \
|
||||||
ret new_##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) {
|
if (strcmp(kZygiskInit, str) == 0) {
|
||||||
g_hook->replace_jni_methods();
|
g_hook->replace_jni_methods();
|
||||||
}
|
}
|
||||||
@ -404,7 +402,7 @@ void HookContext::hook_plt() {
|
|||||||
plt_backup.erase(
|
plt_backup.erase(
|
||||||
std::remove_if(plt_backup.begin(), plt_backup.end(),
|
std::remove_if(plt_backup.begin(), plt_backup.end(),
|
||||||
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
||||||
g_hook->plt_backup.end());
|
plt_backup.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookContext::hook_unloader() {
|
void HookContext::hook_unloader() {
|
||||||
@ -439,6 +437,7 @@ void HookContext::restore_plt_hook() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
void HookContext::replace_jni_methods() {
|
void HookContext::replace_jni_methods() {
|
||||||
using method_sig = jint(*)(JavaVM **, jsize, jsize *);
|
using method_sig = jint(*)(JavaVM **, jsize, jsize *);
|
||||||
auto get_created_vms = reinterpret_cast<method_sig>(
|
auto get_created_vms = reinterpret_cast<method_sig>(
|
||||||
@ -510,7 +509,7 @@ void hookJniNativeMethods(JNIEnv *env, const char *clz, JNINativeMethod *methods
|
|||||||
// It's normal that the method is not found
|
// It's normal that the method is not found
|
||||||
if (env->RegisterNatives(clazz, &method, 1) == JNI_ERR ||
|
if (env->RegisterNatives(clazz, &method, 1) == JNI_ERR ||
|
||||||
env->ExceptionCheck() == JNI_TRUE) {
|
env->ExceptionCheck() == JNI_TRUE) {
|
||||||
if (auto *exception = env->ExceptionOccurred(); exception) {
|
if (auto *exception = env->ExceptionOccurred()) {
|
||||||
env->DeleteLocalRef(exception);
|
env->DeleteLocalRef(exception);
|
||||||
}
|
}
|
||||||
env->ExceptionClear();
|
env->ExceptionClear();
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
// Generated by gen_jni_hooks.py
|
// Generated by gen_jni_hooks.py
|
||||||
|
|
||||||
namespace {
|
static constexpr auto zygote_class = "com/android/internal/os/Zygote";
|
||||||
|
static std::array<JNINativeMethod, 17> zygote_methods = {{
|
||||||
constexpr auto zygote_class = "com/android/internal/os/Zygote";
|
|
||||||
std::array<JNINativeMethod, 17> zygote_methods = {{
|
|
||||||
{
|
{
|
||||||
"nativeForkAndSpecialize",
|
"nativeForkAndSpecialize",
|
||||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
|
"(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
|
||||||
@ -280,4 +278,3 @@ std::array<JNINativeMethod, 17> zygote_methods = {{
|
|||||||
},
|
},
|
||||||
}};
|
}};
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user