From 114b4505347bd2eaf78721531c2a5fc577f0ce3d Mon Sep 17 00:00:00 2001 From: LoveSy Date: Thu, 7 Jan 2021 22:13:19 +0800 Subject: [PATCH] let compiler handle abi --- .../cpp/main/include/art/runtime/art_method.h | 2 +- .../main/include/art/runtime/class_linker.h | 4 +- .../cpp/main/include/art/runtime/gc/heap.h | 2 +- .../main/include/art/runtime/jni_env_ext.h | 8 +-- .../main/include/art/runtime/mirror/class.h | 4 +- .../cpp/main/include/art/runtime/runtime.h | 2 +- .../cpp/main/include/art/runtime/thread.h | 24 ++------ .../src/main/cpp/main/include/base/object.h | 55 +++++++++++++++++++ 8 files changed, 70 insertions(+), 31 deletions(-) diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/art_method.h b/edxp-core/src/main/cpp/main/include/art/runtime/art_method.h index 095b6583..5cadbf5c 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/art_method.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/art_method.h @@ -18,7 +18,7 @@ namespace art { if (UNLIKELY(thiz == nullptr)) return "null"; if (LIKELY(PrettyMethodSym)) - return PrettyMethodSym(thiz, with_signature); + return edxp::call_as_member_func(PrettyMethodSym, thiz, with_signature); else return "null sym"; } 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 b9c3a3b1..06b4b32c 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 @@ -22,7 +22,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(void, SetEntryPointsToInterpreter, void *thiz, void *art_method) { if (LIKELY(SetEntryPointsToInterpreterSym)) - SetEntryPointsToInterpreterSym(thiz, art_method); + edxp::call_as_member_func(SetEntryPointsToInterpreterSym, thiz, art_method); } CREATE_HOOK_STUB_ENTRIES(void *, Constructor, void *thiz, void *intern_table) { @@ -49,7 +49,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(void, MakeInitializedClassesVisiblyInitialized, void *thiz, void *self, bool wait) { if (LIKELY(MakeInitializedClassesVisiblyInitializedSym)) - MakeInitializedClassesVisiblyInitializedSym(thiz, self, wait); + edxp::call_as_member_func(MakeInitializedClassesVisiblyInitializedSym, thiz, self, wait); } diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/gc/heap.h b/edxp-core/src/main/cpp/main/include/art/runtime/gc/heap.h index 6d77303b..eefa87fd 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/gc/heap.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/gc/heap.h @@ -19,7 +19,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(collector::GcType, WaitForGcToComplete, void *thiz, GcCause cause, void *threadSelf) { if (LIKELY(WaitForGcToCompleteSym)) - return WaitForGcToCompleteSym(thiz, cause, threadSelf); + return edxp::call_as_member_func(WaitForGcToCompleteSym, thiz, cause, threadSelf); return art::gc::collector::GcType::kGcTypeNone; } diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/jni_env_ext.h b/edxp-core/src/main/cpp/main/include/art/runtime/jni_env_ext.h index b04cd167..10ff5648 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/jni_env_ext.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/jni_env_ext.h @@ -9,12 +9,12 @@ namespace art { class JNIEnvExt : edxp::HookedObject { private: - CREATE_FUNC_SYMBOL_ENTRY(jobject, NewLocalRef, void *env, void *mirror_ptr) { - return NewLocalRefSym(env, mirror_ptr); + CREATE_FUNC_SYMBOL_ENTRY(jobject, NewLocalRef, void *thiz, void *mirror_ptr) { + return edxp::call_as_member_func(NewLocalRefSym, thiz, mirror_ptr); } - CREATE_FUNC_SYMBOL_ENTRY(void, DeleteLocalRef, void *env, jobject obj) { - DeleteLocalRefSym(env, obj); + CREATE_FUNC_SYMBOL_ENTRY(void, DeleteLocalRef, void *thiz, jobject obj) { + return edxp::call_as_member_func(DeleteLocalRefSym, thiz, obj); } public: 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 1b3b614c..68963ac9 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 @@ -17,7 +17,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(const char *, GetDescriptor, void *thiz, std::string *storage) { if (GetDescriptorSym) - return GetDescriptorSym(thiz, storage); + return edxp::call_as_member_func(GetDescriptorSym, thiz, storage); else return ""; } @@ -45,7 +45,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(void*, GetClassDef, void* thiz) { if (LIKELY(GetClassDefSym)) - return GetClassDefSym(thiz); + return edxp::call_as_member_func(GetClassDefSym, thiz); return nullptr; } diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/runtime.h b/edxp-core/src/main/cpp/main/include/art/runtime/runtime.h index 3436396e..2ec107d6 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/runtime.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/runtime.h @@ -13,7 +13,7 @@ namespace art { CREATE_FUNC_SYMBOL_ENTRY(void, DeoptimizeBootImage, void *thiz) { if (LIKELY(DeoptimizeBootImageSym)) - DeoptimizeBootImageSym(thiz); + edxp::call_as_member_func(DeoptimizeBootImageSym, thiz); } public: diff --git a/edxp-core/src/main/cpp/main/include/art/runtime/thread.h b/edxp-core/src/main/cpp/main/include/art/runtime/thread.h index 63ea9ec5..bc27ca86 100644 --- a/edxp-core/src/main/cpp/main/include/art/runtime/thread.h +++ b/edxp-core/src/main/cpp/main/include/art/runtime/thread.h @@ -7,28 +7,12 @@ namespace art { class Thread : public edxp::HookedObject { -#ifdef __i386__ - typedef void (*DecodeJObjectType)(void **, void *thiz, jobject obj); - inline static void (*DecodeJObjectSym)(void **, void *thiz, jobject obj); - static void *DecodeJObject(void *thiz, jobject obj) { - if (LIKELY(DecodeJObjectSym)) { - // Special call conversion - void *ret = nullptr; - DecodeJObjectSym(&ret, thiz, obj); - // Stack unbalanced since we faked return value as 1st param - __asm__("sub $0x4, %esp"); - return ret; - } else - return nullptr; - } -#else - CREATE_FUNC_SYMBOL_ENTRY(void *, DecodeJObject, void *thiz, jobject obj) { + CREATE_FUNC_SYMBOL_ENTRY(edxp::ObjPtr, DecodeJObject, void *thiz, jobject obj) { if (DecodeJObjectSym) - return DecodeJObjectSym(thiz, obj); + return edxp::call_as_member_func(DecodeJObjectSym, thiz, obj); else - return nullptr; + return {.data=nullptr}; } -#endif CREATE_FUNC_SYMBOL_ENTRY(void *, CurrentFromGdb) { if (LIKELY(CurrentFromGdbSym)) return CurrentFromGdbSym(); @@ -51,7 +35,7 @@ namespace art { void *DecodeJObject(jobject obj) { if (LIKELY(thiz_ && DecodeJObjectSym)) { - return DecodeJObject(thiz_, obj); + return DecodeJObject(thiz_, obj).data; } return nullptr; } diff --git a/edxp-core/src/main/cpp/main/include/base/object.h b/edxp-core/src/main/cpp/main/include/base/object.h index f7ff2fe0..bfc27b4b 100644 --- a/edxp-core/src/main/cpp/main/include/base/object.h +++ b/edxp-core/src/main/cpp/main/include/base/object.h @@ -82,6 +82,10 @@ namespace edxp { } }; + struct ObjPtr { + void* data; + }; + ALWAYS_INLINE static void *Dlsym(void *handle, const char *name) { return dlsym(handle, name); } @@ -125,4 +129,55 @@ namespace edxp { } } + template + inline auto memfun_cast(Return (*func)(T *, Args...)) { + static_assert(std::is_same_v || std::is_same_v, + "Not viable cast"); + union { + Return (Class::*f)(Args...); + + struct { + decltype(func) p; + std::ptrdiff_t adj; + } data; + } u{.data = {func, 0}}; + static_assert(sizeof(u.f) == sizeof(u.data), "Try different T"); + return u.f; + } + + template>> + inline auto memfun_cast(Return (*func)(T *, Args...)) { + return memfun_cast(func); + } + + template || !std::is_same_v>> + inline Return + call_as_member_func(Return (*func)(U *, std::remove_reference_t...), T *thiz, + Args &&... args) { + using Class = std::conditional_t, U, T>; + return (reinterpret_cast(thiz)->*memfun_cast + (func))( + std::forward(args)...); + } + + template + inline Return + call_as_member_func(Return (*func)(void *, std::remove_reference_t...), void *thiz, + Args &&... args) { + return (reinterpret_cast(thiz)->*memfun_cast(func))( + std::forward(args)...); + } + + template + inline Return + call_as_member_func(Return (*func)(void *, std::remove_reference_t...), void *thiz, + Args &&... args) { + struct DummyClass { + }; + return (reinterpret_cast(thiz)->*memfun_cast(func))( + std::forward(args)...); + } + } // namespace edxp