From 27f1abfb3cfaa95c8722066f34ed92d4d1f33422 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Wed, 23 Dec 2020 18:50:48 +0800 Subject: [PATCH] Fix #747 --- .../main/include/art/runtime/class_linker.h | 7 +++-- .../main/include/art/runtime/mirror/class.h | 26 ++++++++++++++----- .../cpp/main/src/jni/edxp_pending_hooks.cpp | 10 +++++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/class_linker.h b/edxp-core/src/main/cpp/main/include/art/runtime/class_linker.h index 53db6ad2..b9c3a3b1 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/class_linker.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/class_linker.h @@ -35,10 +35,13 @@ namespace art { } CREATE_HOOK_STUB_ENTRIES(void, FixupStaticTrampolines, void *thiz, void *clazz_ptr) { - bool should_intercept = edxp::IsClassPending(clazz_ptr); FixupStaticTrampolinesBackup(thiz, clazz_ptr); + art::mirror::Class mirror_class(clazz_ptr); + auto class_def = mirror_class.GetClassDef(); + bool should_intercept = class_def && edxp::IsClassPending(class_def); if (UNLIKELY(should_intercept)) { - LOGD("Pending hook for %p (%s)", clazz_ptr, art::mirror::Class(clazz_ptr).GetDescriptor(nullptr)); + LOGD("Pending hook for %p (%s)", clazz_ptr, + art::mirror::Class(clazz_ptr).GetDescriptor().c_str()); edxp::Context::GetInstance()->CallOnPostFixupStaticTrampolines(clazz_ptr); } } diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/mirror/class.h b/edxp-core/src/main/cpp/main/include/art/runtime/mirror/class.h index 0b3061fb..1b3b614c 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/mirror/class.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/mirror/class.h @@ -43,6 +43,12 @@ namespace art { return IsInSamePackageBackup(thiz, that); } + CREATE_FUNC_SYMBOL_ENTRY(void*, GetClassDef, void* thiz) { + if (LIKELY(GetClassDefSym)) + return GetClassDefSym(thiz); + return nullptr; + } + public: Class(void *thiz) : HookedObject(thiz) {} @@ -51,6 +57,8 @@ namespace art { RETRIEVE_FUNC_SYMBOL(GetDescriptor, "_ZN3art6mirror5Class13GetDescriptorEPNSt3__112" "basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE"); + RETRIEVE_FUNC_SYMBOL(GetClassDef, "_ZN3art6mirror5Class11GetClassDefEv"); + // RETRIEVE_FIELD_SYMBOL(mutator_lock_, "_ZN3art5Locks13mutator_lock_E"); // LOGE("mutator_lock_: %p", mutator_lock_); @@ -64,15 +72,21 @@ namespace art { const char *GetDescriptor(std::string *storage) { if (thiz_ && GetDescriptorSym) { - if (storage == nullptr) { - std::string str; - return GetDescriptor(thiz_, &str); - } else { - return GetDescriptor(thiz_, storage); - } + return GetDescriptor(thiz_, storage); } return ""; } + + std::string GetDescriptor() { + std::string storage; + return GetDescriptor(&storage); + } + + void *GetClassDef() { + if(thiz_ && GetClassDefSym) + return GetClassDef(thiz_); + return nullptr; + } }; } // namespace mirror diff --git a/edxp-core/src/main/cpp/main/src/jni/edxp_pending_hooks.cpp b/edxp-core/src/main/cpp/main/src/jni/edxp_pending_hooks.cpp index 2859ca38..d23024b9 100644 --- a/edxp-core/src/main/cpp/main/src/jni/edxp_pending_hooks.cpp +++ b/edxp-core/src/main/cpp/main/src/jni/edxp_pending_hooks.cpp @@ -22,8 +22,14 @@ namespace edxp { static void PendingHooks_recordPendingMethodNative(JNI_START, jlong thread, jclass class_ref) { art::Thread current_thread(reinterpret_cast(thread)); auto *class_ptr = current_thread.DecodeJObject(class_ref); - LOGD("record pending: %p (%s)", class_ptr, art::mirror::Class(class_ptr).GetDescriptor(nullptr)); - pending_classes_.insert(class_ptr); + art::mirror::Class mirror_class(class_ptr); + if (auto def = mirror_class.GetClassDef(); LIKELY(def)) { + LOGD("record pending: %p (%s)", class_ptr, mirror_class.GetDescriptor().c_str()); + pending_classes_.insert(def); + } else { + LOGW("fail to record pending for : %p (%s)", class_ptr, + mirror_class.GetDescriptor().c_str()); + } } static JNINativeMethod gMethods[] = {