Remove dangerous implicit cast

This commit is contained in:
LoveSy 2024-07-23 11:40:35 +08:00
parent bd64ecbc16
commit 5121a21064
No known key found for this signature in database
4 changed files with 32 additions and 22 deletions

View File

@ -228,7 +228,7 @@ public:
env,
JNI_GetObjectField(
env,
env->ToReflectedField(executable,
JNI_ToReflectedField(env, executable,
JNI_GetFieldID(env, executable, name, sig), false),
art_field_field),
field_offset);
@ -284,7 +284,7 @@ public:
RETRIEVE_MEM_FUNC_SYMBOL(ThrowInvocationTimeError,
"_ZN3art9ArtMethod24ThrowInvocationTimeErrorEv");
auto abstract_method = FromReflectedMethod(
env, JNI_ToReflectedMethod(env, executable, executable_get_name, false));
env, JNI_ToReflectedMethod(env, executable, executable_get_name, false).get());
uint32_t access_flags = abstract_method->GetAccessFlags();
abstract_method->SetAccessFlags(access_flags | kAccDefaultConflict);
abstract_method->ThrowInvocationTimeError();

View File

@ -58,8 +58,6 @@ public:
return ScopedLocalRef<T>(env_, (T)env_->NewLocalRef(local_ref_));
}
operator T() const { return local_ref_; }
ScopedLocalRef &operator=(ScopedLocalRef &&s) noexcept {
reset(s.release());
env_ = s.env_;
@ -126,10 +124,11 @@ concept ScopeOrObject = ScopeOrRaw<T, jobject>;
inline ScopedLocalRef<jstring> ClearException(JNIEnv *env) {
if (auto exception = env->ExceptionOccurred()) {
env->ExceptionClear();
static jclass log = (jclass)env->NewGlobalRef(env->FindClass("android/util/Log"));
jclass log = (jclass)env->FindClass("android/util/Log");
static jmethodID toString = env->GetStaticMethodID(
log, "getStackTraceString", "(Ljava/lang/Throwable;)Ljava/lang/String;");
auto str = (jstring)env->CallStaticObjectMethod(log, toString, exception);
env->DeleteLocalRef(log);
env->DeleteLocalRef(exception);
return {env, str};
}
@ -506,6 +505,13 @@ template <ScopeOrClass Class>
isStatic);
}
template <ScopeOrClass Class>
[[maybe_unused]] inline auto JNI_ToReflectedField(JNIEnv *env, Class &&clazz, jfieldID field,
jboolean isStatic = JNI_FALSE) {
return JNI_SafeInvoke(env, &JNIEnv::ToReflectedField, std::forward<Class>(clazz), field,
isStatic);
}
// functions to method
// virtual methods
@ -763,6 +769,12 @@ template <ScopeOrObject Object, ScopeOrClass Class>
std::forward<Class>(clazz));
}
template <ScopeOrObject Object1, ScopeOrObject Object2>
[[maybe_unused]] inline auto JNI_IsSameObject(JNIEnv *env, Object1 &&a, Object2 &&b) {
return JNI_SafeInvoke(env, &JNIEnv::IsSameObject, std::forward<Object1>(a),
std::forward<Object2>(b));
}
template <ScopeOrObject Object>
[[maybe_unused]] inline auto JNI_NewGlobalRef(JNIEnv *env, Object &&x) {
return (decltype(UnwrapScope(std::forward<Object>(x))))env->NewGlobalRef(
@ -941,8 +953,6 @@ public:
T get() const { return local_ref_; }
explicit operator T() const { return local_ref_; }
JArrayUnderlyingType<T> &operator[](size_t index) {
modified_ = true;
return elements_[index];
@ -1064,7 +1074,7 @@ public:
}
template<JObject T>
JObjectArrayElement &operator=(ScopedLocalRef<T> &s) {
JObjectArrayElement &operator=(const ScopedLocalRef<T> &s) {
reset(s.clone());
return *this;
}
@ -1081,8 +1091,6 @@ public:
ScopedLocalRef<jobject> clone() const { return item_.clone(); }
operator jobject() const { return item_; }
jobject get() const { return item_.get(); }
jobject release() { return item_.release(); }

View File

@ -115,6 +115,7 @@ using lsplant::JNI_CallVoidMethod;
using lsplant::JNI_GetMethodID;
using lsplant::JNI_GetStaticMethodID;
using lsplant::JNI_ToReflectedMethod;
using lsplant::JNI_ToReflectedField;
using lsplant::JNI_NewBooleanArray;
using lsplant::JNI_NewByteArray;
@ -134,6 +135,7 @@ using lsplant::JNI_GetArrayLength;
using lsplant::JNI_GetObjectClass;
using lsplant::JNI_GetObjectFieldOf;
using lsplant::JNI_IsInstanceOf;
using lsplant::JNI_IsSameObject;
using lsplant::JNI_NewGlobalRef;
using lsplant::JNI_NewStringUTF;
using lsplant::JNI_RegisterNatives;

View File

@ -664,15 +664,15 @@ std::string GetProxyMethodShorty(JNIEnv *env, jobject proxy_method) {
std::string out;
auto type_to_shorty = [&](const ScopedLocalRef<jobject> &type) {
if (env->IsSameObject(type, int_type)) return 'I';
if (env->IsSameObject(type, long_type)) return 'J';
if (env->IsSameObject(type, float_type)) return 'F';
if (env->IsSameObject(type, double_type)) return 'D';
if (env->IsSameObject(type, boolean_type)) return 'Z';
if (env->IsSameObject(type, byte_type)) return 'B';
if (env->IsSameObject(type, char_type)) return 'C';
if (env->IsSameObject(type, short_type)) return 'S';
if (env->IsSameObject(type, void_type)) return 'V';
if (JNI_IsSameObject(env, type, int_type)) return 'I';
if (JNI_IsSameObject(env, type, long_type)) return 'J';
if (JNI_IsSameObject(env, type, float_type)) return 'F';
if (JNI_IsSameObject(env, type, double_type)) return 'D';
if (JNI_IsSameObject(env, type, boolean_type)) return 'Z';
if (JNI_IsSameObject(env, type, byte_type)) return 'B';
if (JNI_IsSameObject(env, type, char_type)) return 'C';
if (JNI_IsSameObject(env, type, short_type)) return 'S';
if (JNI_IsSameObject(env, type, void_type)) return 'V';
return 'L';
};
out += type_to_shorty(return_type);
@ -740,7 +740,7 @@ using ::lsplant::IsHooked;
}
std::tie(built_class, hooker_field, hook_method, backup_method) = WrapScope(
env,
BuildDex(env, callback_class_loader,
BuildDex(env, callback_class_loader.get(),
__builtin_expect(is_proxy, 0) ? GetProxyMethodShorty(env, target_method)
: ArtMethod::GetMethodShorty(env, target_method),
is_static, target->IsConstructor() ? "constructor" : target_method_name.get(),
@ -756,8 +756,8 @@ using ::lsplant::IsHooked;
JNI_CallVoidMethod(env, reflected_backup, set_accessible, JNI_TRUE);
auto *hook = ArtMethod::FromReflectedMethod(env, reflected_hook);
auto *backup = ArtMethod::FromReflectedMethod(env, reflected_backup);
auto *hook = ArtMethod::FromReflectedMethod(env, reflected_hook.get());
auto *backup = ArtMethod::FromReflectedMethod(env, reflected_backup.get());
JNI_SetStaticObjectField(env, built_class, hooker_field, hooker_object);