2022-02-16 07:24:35 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "art/mirror/class.hpp"
|
2022-02-19 03:47:26 +08:00
|
|
|
#include "common.hpp"
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
namespace lsplant::art {
|
|
|
|
|
|
|
|
class ArtMethod {
|
|
|
|
CREATE_MEM_FUNC_SYMBOL_ENTRY(std::string, PrettyMethod, ArtMethod *thiz, bool with_signature) {
|
2022-02-19 03:47:26 +08:00
|
|
|
if (thiz == nullptr) [[unlikely]]
|
|
|
|
return "null";
|
|
|
|
else if (PrettyMethodSym) [[likely]]
|
|
|
|
return PrettyMethodSym(thiz, with_signature);
|
|
|
|
else
|
|
|
|
return "null sym";
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void SetNonCompilable() {
|
|
|
|
auto access_flags = GetAccessFlags();
|
|
|
|
access_flags |= kAccCompileDontBother;
|
|
|
|
access_flags &= ~kAccPreCompiled;
|
|
|
|
SetAccessFlags(access_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNonIntrinsic() {
|
|
|
|
auto access_flags = GetAccessFlags();
|
|
|
|
access_flags &= ~kAccFastInterpreterToInterpreterInvoke;
|
|
|
|
SetAccessFlags(access_flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPrivate() {
|
|
|
|
auto access_flags = GetAccessFlags();
|
|
|
|
if (!(access_flags & kAccStatic)) {
|
|
|
|
access_flags |= kAccPrivate;
|
|
|
|
access_flags &= ~kAccProtected;
|
|
|
|
access_flags &= ~kAccPublic;
|
|
|
|
SetAccessFlags(access_flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
bool IsStatic() { return GetAccessFlags() & kAccStatic; }
|
2022-02-16 07:24:35 +08:00
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
bool IsNative() { return GetAccessFlags() & kAccNative; }
|
2022-02-16 16:25:05 +08:00
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
void CopyFrom(const ArtMethod *other) { memcpy(this, other, art_method_size); }
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
void SetEntryPoint(void *entry_point) {
|
2022-02-19 03:47:26 +08:00
|
|
|
*reinterpret_cast<void **>(reinterpret_cast<uintptr_t>(this) + entry_point_offset) =
|
|
|
|
entry_point;
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *GetEntryPoint() {
|
2022-02-19 03:47:26 +08:00
|
|
|
return *reinterpret_cast<void **>(reinterpret_cast<uintptr_t>(this) + entry_point_offset);
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *GetData() {
|
2022-02-19 03:47:26 +08:00
|
|
|
return *reinterpret_cast<void **>(reinterpret_cast<uintptr_t>(this) + data_offset);
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetAccessFlags() {
|
2022-02-19 03:47:26 +08:00
|
|
|
return (reinterpret_cast<const std::atomic<uint32_t> *>(reinterpret_cast<uintptr_t>(this) +
|
|
|
|
access_flags_offset))
|
|
|
|
->load(std::memory_order_relaxed);
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetAccessFlags(uint32_t flags) {
|
2022-02-19 03:47:26 +08:00
|
|
|
return (reinterpret_cast<std::atomic<uint32_t> *>(reinterpret_cast<uintptr_t>(this) +
|
|
|
|
access_flags_offset))
|
|
|
|
->store(flags, std::memory_order_relaxed);
|
2022-02-16 07:24:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PrettyMethod(bool with_signature = true) {
|
|
|
|
return PrettyMethod(this, with_signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
static art::ArtMethod *FromReflectedMethod(JNIEnv *env, jobject method) {
|
|
|
|
return reinterpret_cast<art::ArtMethod *>(JNI_GetLongField(env, method, art_method_field));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool Init(JNIEnv *env, const lsplant::InitInfo info) {
|
|
|
|
auto executable = JNI_FindClass(env, "java/lang/reflect/Executable");
|
|
|
|
if (!executable) {
|
|
|
|
LOGE("Failed to found Executable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
if (art_method_field = JNI_GetFieldID(env, executable, "artMethod", "J");
|
|
|
|
!art_method_field) {
|
2022-02-16 07:24:35 +08:00
|
|
|
LOGE("Failed to find artMethod field");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto throwable = JNI_FindClass(env, "java/lang/Throwable");
|
|
|
|
if (!throwable) {
|
|
|
|
LOGE("Failed to found Executable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto clazz = JNI_FindClass(env, "java/lang/Class");
|
|
|
|
static_assert(std::is_same_v<decltype(clazz)::BaseType, jclass>);
|
|
|
|
jmethodID get_declared_constructors = JNI_GetMethodID(env, clazz, "getDeclaredConstructors",
|
|
|
|
"()[Ljava/lang/reflect/Constructor;");
|
2022-02-19 03:47:26 +08:00
|
|
|
auto constructors =
|
|
|
|
JNI_Cast<jobjectArray>(JNI_CallObjectMethod(env, throwable, get_declared_constructors));
|
2022-02-16 07:24:35 +08:00
|
|
|
auto length = JNI_GetArrayLength(env, constructors);
|
|
|
|
if (length < 2) {
|
|
|
|
LOGE("Throwable has less than 2 constructors");
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-18 08:55:43 +08:00
|
|
|
auto first_ctor = JNI_GetObjectArrayElement(env, constructors, 0);
|
|
|
|
auto second_ctor = JNI_GetObjectArrayElement(env, constructors, 1);
|
|
|
|
auto *first = FromReflectedMethod(env, first_ctor.get());
|
|
|
|
auto *second = FromReflectedMethod(env, second_ctor.get());
|
2022-02-16 07:24:35 +08:00
|
|
|
art_method_size = reinterpret_cast<uintptr_t>(second) - reinterpret_cast<uintptr_t>(first);
|
|
|
|
LOGD("ArtMethod size: %zu", art_method_size);
|
|
|
|
|
|
|
|
if (RoundUpTo(4 * 4 + 2 * 2, kPointerSize) + kPointerSize * 3 < art_method_size) {
|
|
|
|
LOGW("ArtMethod size exceeds maximum assume. There may be something wrong.");
|
|
|
|
}
|
|
|
|
|
|
|
|
entry_point_offset = art_method_size - sizeof(void *);
|
|
|
|
LOGD("ArtMethod::entrypoint offset: %zu", entry_point_offset);
|
|
|
|
|
|
|
|
data_offset = entry_point_offset - sizeof(void *);
|
|
|
|
LOGD("ArtMethod::data offset: %zu", data_offset);
|
|
|
|
|
2022-02-18 08:55:43 +08:00
|
|
|
if (auto access_flags_field = JNI_GetFieldID(env, executable, "accessFlags", "I");
|
2022-02-19 03:47:26 +08:00
|
|
|
access_flags_field) {
|
2022-02-18 08:55:43 +08:00
|
|
|
uint32_t real_flags = JNI_GetIntField(env, first_ctor, access_flags_field);
|
|
|
|
for (size_t i = 0; i < art_method_size; i += sizeof(uint32_t)) {
|
|
|
|
if (*reinterpret_cast<uint32_t *>(reinterpret_cast<uintptr_t>(first) + i) ==
|
|
|
|
real_flags) {
|
|
|
|
access_flags_offset = i;
|
|
|
|
LOGD("ArtMethod::access_flags offset: %zu", access_flags_offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (access_flags_offset == 0) {
|
|
|
|
LOGW("Failed to find accessFlags field. Fallback to 4.");
|
|
|
|
access_flags_offset = 4U;
|
|
|
|
}
|
2022-02-16 07:24:35 +08:00
|
|
|
auto sdk_int = GetAndroidApiLevel();
|
|
|
|
|
|
|
|
if (sdk_int < __ANDROID_API_R__) kAccPreCompiled = 0;
|
|
|
|
if (sdk_int < __ANDROID_API_Q__) kAccFastInterpreterToInterpreterInvoke = 0;
|
|
|
|
|
|
|
|
get_method_shorty_symbol = GetArtSymbol<decltype(get_method_shorty_symbol)>(
|
2022-02-19 03:47:26 +08:00
|
|
|
info.art_symbol_resolver, "_ZN3artL15GetMethodShortyEP7_JNIEnvP10_jmethodID");
|
2022-02-16 07:24:35 +08:00
|
|
|
if (!get_method_shorty_symbol) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *GetMethodShorty(_JNIEnv *env, _jmethodID *method) {
|
2022-02-19 03:47:26 +08:00
|
|
|
if (get_method_shorty_symbol) [[likely]]
|
|
|
|
return get_method_shorty_symbol(env, method);
|
2022-02-16 07:24:35 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
static size_t GetEntryPointOffset() { return entry_point_offset; }
|
2022-02-16 07:24:35 +08:00
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
constexpr static uint32_t kAccPublic = 0x0001; // class, field, method, ic
|
|
|
|
constexpr static uint32_t kAccPrivate = 0x0002; // field, method, ic
|
2022-02-16 07:24:35 +08:00
|
|
|
constexpr static uint32_t kAccProtected = 0x0004; // field, method, ic
|
2022-02-19 03:47:26 +08:00
|
|
|
constexpr static uint32_t kAccStatic = 0x0008; // field, method, ic
|
|
|
|
constexpr static uint32_t kAccNative = 0x0100; // method
|
2022-02-16 07:24:35 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
inline static jfieldID art_method_field = nullptr;
|
|
|
|
inline static size_t art_method_size = 0;
|
|
|
|
inline static size_t entry_point_offset = 0;
|
|
|
|
inline static size_t data_offset = 0;
|
|
|
|
inline static size_t access_flags_offset = 0;
|
|
|
|
inline static uint32_t kAccFastInterpreterToInterpreterInvoke = 0x40000000;
|
|
|
|
inline static uint32_t kAccPreCompiled = 0x00200000;
|
|
|
|
inline static uint32_t kAccCompileDontBother = 0x02000000;
|
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
inline static const char *(*get_method_shorty_symbol)(_JNIEnv *env,
|
|
|
|
_jmethodID *method) = nullptr;
|
2022-02-16 07:24:35 +08:00
|
|
|
};
|
|
|
|
|
2022-02-19 03:47:26 +08:00
|
|
|
} // namespace lsplant::art
|