Skip to content

Commit

Permalink
Remove dangerous implicit cast
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Jul 23, 2024
1 parent bd64ecb commit 5121a21
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lsplant/src/main/jni/art/runtime/art_method.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class ArtMethod {
env,
JNI_GetObjectField(
env,
env->ToReflectedField(executable,
JNI_ToReflectedField(env, executable,
JNI_GetFieldID(env, executable, name, sig), false),
art_field_field),
field_offset);
Expand Down Expand Up @@ -284,7 +284,7 @@ export class ArtMethod {
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();
Expand Down
24 changes: 16 additions & 8 deletions lsplant/src/main/jni/include/utils/jni_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ class ScopedLocalRef {
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_;
Expand Down Expand Up @@ -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};
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -941,8 +953,6 @@ class ScopedLocalRef<T> {

T get() const { return local_ref_; }

explicit operator T() const { return local_ref_; }

JArrayUnderlyingType<T> &operator[](size_t index) {
modified_ = true;
return elements_[index];
Expand Down Expand Up @@ -1064,7 +1074,7 @@ class JObjectArrayElement {
}

template<JObject T>
JObjectArrayElement &operator=(ScopedLocalRef<T> &s) {
JObjectArrayElement &operator=(const ScopedLocalRef<T> &s) {
reset(s.clone());
return *this;
}
Expand All @@ -1081,8 +1091,6 @@ class JObjectArrayElement {

ScopedLocalRef<jobject> clone() const { return item_.clone(); }

operator jobject() const { return item_; }

jobject get() const { return item_.get(); }

jobject release() { return item_.release(); }
Expand Down
2 changes: 2 additions & 0 deletions lsplant/src/main/jni/include/utils/jni_helper.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions lsplant/src/main/jni/lsplant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand All @@ -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);

Expand Down

0 comments on commit 5121a21

Please sign in to comment.