From 596f0f3d099b3d1ec766baaadf3bf2b44d2c2321 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Sun, 20 Feb 2022 12:49:53 +0800 Subject: [PATCH] Refine codes --- library/jni/art/runtime/art_method.hpp | 35 ++++++++++++++++---------- library/jni/lsplant.cc | 8 +++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/library/jni/art/runtime/art_method.hpp b/library/jni/art/runtime/art_method.hpp index f6d22f6..bde98e6 100644 --- a/library/jni/art/runtime/art_method.hpp +++ b/library/jni/art/runtime/art_method.hpp @@ -15,7 +15,22 @@ class ArtMethod { return "null sym"; } + CREATE_MEM_FUNC_SYMBOL_ENTRY(void, ThrowInvocationTimeError, ArtMethod *thiz) { + if (thiz && ThrowInvocationTimeErrorSym) [[likely]] + return ThrowInvocationTimeErrorSym(thiz); + } + + CREATE_FUNC_SYMBOL_ENTRY(const char *, GetMethodShorty, JNIEnv *env, jmethodID mid) { + if (GetMethodShortySym) [[likely]] + return GetMethodShortySym(env, mid); + return nullptr; + } + public: + inline static const char *GetMethodShorty(JNIEnv *env, jobject method) { + return GetMethodShorty(env, env->FromReflectedMethod(method)); + } + void SetNonCompilable() { auto access_flags = GetAccessFlags(); access_flags |= kAccCompileDontBother; @@ -101,7 +116,7 @@ public: return reinterpret_cast(JNI_GetLongField(env, method, art_method_field)); } - static bool Init(JNIEnv *env, const lsplant::InitInfo info) { + static bool Init(JNIEnv *env, const HookHandler handler) { auto executable = JNI_FindClass(env, "java/lang/reflect/Executable"); if (!executable) { LOGE("Failed to found Executable"); @@ -168,16 +183,13 @@ public: if (sdk_int < __ANDROID_API_R__) kAccPreCompiled = 0; if (sdk_int < __ANDROID_API_Q__) kAccFastInterpreterToInterpreterInvoke = 0; - get_method_shorty_symbol = GetArtSymbol( - info.art_symbol_resolver, "_ZN3artL15GetMethodShortyEP7_JNIEnvP10_jmethodID"); - if (!get_method_shorty_symbol) return false; - return true; - } + if (!RETRIEVE_FUNC_SYMBOL(GetMethodShorty, + "_ZN3artL15GetMethodShortyEP7_JNIEnvP10_jmethodID")) { + LOGE("Failed to find GetMethodShorty"); + return false; + } - static const char *GetMethodShorty(_JNIEnv *env, _jmethodID *method) { - if (get_method_shorty_symbol) [[likely]] - return get_method_shorty_symbol(env, method); - return nullptr; + return true; } static size_t GetEntryPointOffset() { return entry_point_offset; } @@ -198,9 +210,6 @@ private: inline static uint32_t kAccFastInterpreterToInterpreterInvoke = 0x40000000; inline static uint32_t kAccPreCompiled = 0x00200000; inline static uint32_t kAccCompileDontBother = 0x02000000; - - inline static const char *(*get_method_shorty_symbol)(_JNIEnv *env, - _jmethodID *method) = nullptr; }; } // namespace lsplant::art diff --git a/library/jni/lsplant.cc b/library/jni/lsplant.cc index 295634f..6246df4 100644 --- a/library/jni/lsplant.cc +++ b/library/jni/lsplant.cc @@ -508,10 +508,10 @@ using ::lsplant::IsHooked; LOGE("callback_method is not a method of hooker_object"); return nullptr; } - std::tie(built_class, hooker_field, hook_method, backup_method) = WrapScope( - env, BuildDex(env, callback_class_loader, - ArtMethod::GetMethodShorty(env, env->FromReflectedMethod(target_method)), - is_static, method_name.get(), class_name.get(), method_name.get())); + std::tie(built_class, hooker_field, hook_method, backup_method) = + WrapScope(env, BuildDex(env, callback_class_loader, + ArtMethod::GetMethodShorty(env, target_method), is_static, + method_name.get(), class_name.get(), method_name.get())); if (!built_class || !hooker_field || !hook_method || !backup_method) { LOGE("Failed to generate hooker"); return nullptr;