Commit 6f9c3c4f authored by topjohnwu's avatar topjohnwu

Refactor hook.cpp

parent 9b3efffb
......@@ -33,10 +33,10 @@ template <class Func>
class run_finally {
DISALLOW_COPY_AND_MOVE(run_finally)
public:
explicit run_finally(const Func &fn) : fn(fn) {}
explicit run_finally(Func &&fn) : fn(std::move(fn)) {}
~run_finally() { fn(); }
private:
const Func &fn;
Func fn;
};
template <typename T>
......
......@@ -16,8 +16,8 @@ static void *self_handle = nullptr;
static atomic<int> active_threads = -1;
void self_unload() {
LOGD("hook: Request to self unload\n");
// If unhook failed, do not unload or else it will cause SIGSEGV
LOGD("zygisk: Request to self unload\n");
// If deny failed, do not unload or else it will cause SIGSEGV
if (!unhook_functions())
return;
new_daemon_thread(reinterpret_cast<thread_entry>(&dlclose), self_handle);
......@@ -81,7 +81,7 @@ __attribute__((constructor))
static void inject_init() {
if (char *env = getenv(INJECT_ENV_2)) {
android_logging();
LOGD("zygote: inject 2nd stage\n");
LOGD("zygisk: inject 2nd stage\n");
active_threads = 1;
unsetenv(INJECT_ENV_2);
......@@ -100,7 +100,7 @@ static void inject_init() {
new_daemon_thread(&unload_first_stage, env);
} else if (getenv(INJECT_ENV_1)) {
android_logging();
LOGD("zygote: inject 1st stage\n");
LOGD("zygisk: inject 1st stage\n");
char *ld = getenv("LD_PRELOAD");
char *path;
......
......@@ -128,7 +128,7 @@ spec_r = Method('r', [uid, gid, gids, runtime_flags, rlimits, mount_external, se
spec_samsung_q = Method('samsung_q', [uid, gid, gids, runtime_flags, rlimits, mount_external,
se_info, i1, i2, nice_name, is_child_zygote, instruction_set, app_data_dir])
server_m = Method('m', [uid, gid, gids, runtime_flags, rlimits,
server_l = Method('l', [uid, gid, gids, runtime_flags, rlimits,
permitted_capabilities, effective_capabilities])
server_samsung_q = Method('samsung_q', [uid, gid, gids, runtime_flags, i1, i2, rlimits,
......@@ -151,7 +151,6 @@ def gen_definitions(methods, base_name):
for m in methods:
func_name = f'{base_name}_{m.name}'
decl += ind(0) + f'{cpp_ret} {func_name}(JNIEnv *env, jclass clazz, {m.cpp()}) {{'
decl += ind(1) + 'HookContext ctx{};'
if base_name == 'nativeForkSystemServer':
decl += ind(1) + 'ForkSystemServerArgs args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);'
else:
......@@ -159,12 +158,15 @@ def gen_definitions(methods, base_name):
for a in m.args:
if a.set_arg:
decl += ind(1) + f'args.{a.name} = &{a.name};'
decl += ind(1) + 'HookContext ctx;'
decl += ind(1) + 'ctx.env = env;'
decl += ind(1) + 'ctx.clazz = clazz;'
decl += ind(1) + 'ctx.raw_args = &args;'
decl += ind(1) + f'{base_name}_pre(&ctx, env, clazz);'
decl += ind(1) + f'reinterpret_cast<decltype(&{func_name})>({base_name}_orig)('
decl += ind(1) + f'ctx.{base_name}_pre();'
decl += ind(1) + f'reinterpret_cast<decltype(&{func_name})>(HookContext::{base_name}_orig)('
decl += ind(2) + f'env, clazz, {m.name_list()}'
decl += ind(1) + ');'
decl += ind(1) + f'{base_name}_post(&ctx, env, clazz);'
decl += ind(1) + f'ctx.{base_name}_post();'
decl += ret_stat
decl += ind(0) + '}'
......@@ -189,7 +191,7 @@ def gen_spec():
return gen_definitions(methods, 'nativeSpecializeAppProcess')
def gen_server():
methods = [server_m, server_samsung_q]
methods = [server_l, server_samsung_q]
return gen_definitions(methods, 'nativeForkSystemServer')
with open('jni_hooks.hpp', 'w') as f:
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment