Revert "[core] Clear ProfilingInfo in backup method (#1147)" (#1165)

This reverts commit bf89c754dc.
This commit is contained in:
LoveSy 2021-09-24 15:54:47 +08:00 committed by GitHub
parent bdbe9ca3f2
commit 1e4ffb0a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 21 deletions

View File

@ -17,7 +17,7 @@ namespace yahfa {
jboolean backupAndHookNative(JNIEnv *env, jclass clazz, jboolean backupAndHookNative(JNIEnv *env, jclass clazz,
jobject target, jobject hook, jobject target, jobject hook,
jobject backup, jboolean clearData); jobject backup);
void *getArtMethod(JNIEnv *env, jobject jmethod); void *getArtMethod(JNIEnv *env, jobject jmethod);

View File

@ -9,7 +9,6 @@
int SDKVersion; int SDKVersion;
size_t OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod; size_t OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod;
size_t OFFSET_data_in_ArtMethod;
namespace yahfa { namespace yahfa {
namespace { namespace {
@ -58,7 +57,7 @@ namespace yahfa {
} }
} }
int doBackupAndHook(void *targetMethod, void *hookMethod, void *backupMethod, bool clearData) { int doBackupAndHook(void *targetMethod, void *hookMethod, void *backupMethod) {
LOGI("target method is at %p, hook method is at %p, backup method is at %p", LOGI("target method is at %p, hook method is at %p, backup method is at %p",
targetMethod, hookMethod, backupMethod); targetMethod, hookMethod, backupMethod);
@ -102,9 +101,6 @@ namespace yahfa {
setAccessFlags(targetMethod, access_flags); setAccessFlags(targetMethod, access_flags);
} }
if (clearData)
writeAddr((char *) backupMethod + OFFSET_data_in_ArtMethod, nullptr);
LOGI("hook and backup done"); LOGI("hook and backup done");
return 0; return 0;
} }
@ -140,7 +136,6 @@ namespace yahfa {
LOGE("not compatible with SDK %d", sdkVersion); LOGE("not compatible with SDK %d", sdkVersion);
break; break;
} }
OFFSET_data_in_ArtMethod = OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod - pointer_size;
setupTrampoline(); setupTrampoline();
} }
@ -189,14 +184,13 @@ namespace yahfa {
return ret; return ret;
} }
jboolean backupAndHookNative(JNIEnv *env, jclass, jboolean backupAndHookNative(JNIEnv *env, [[maybe_unused]] jclass clazz,
jobject target, jobject hook, jobject target, jobject hook,
jobject backup, jboolean clearData) { jobject backup) {
if (!doBackupAndHook(getArtMethod(env, target), if (!doBackupAndHook(getArtMethod(env, target),
getArtMethod(env, hook), getArtMethod(env, hook),
getArtMethod(env, backup), getArtMethod(env, backup)
clearData == JNI_TRUE
)) { )) {
env->NewGlobalRef(hook); // keep a global ref so that the hook method would not be GCed env->NewGlobalRef(hook); // keep a global ref so that the hook method would not be GCed
if (backup) env->NewGlobalRef(backup); if (backup) env->NewGlobalRef(backup);

View File

@ -42,12 +42,12 @@ namespace lspd {
} }
LSP_DEF_NATIVE_METHOD(jboolean, Yahfa, backupAndHookNative, jobject target, LSP_DEF_NATIVE_METHOD(jboolean, Yahfa, backupAndHookNative, jobject target,
jobject hook, jobject backup, jboolean clearData) { jobject hook, jobject backup) {
art::gc::ScopedGCCriticalSection section(art::Thread::Current().Get(), art::gc::ScopedGCCriticalSection section(art::Thread::Current().Get(),
art::gc::kGcCauseDebugger, art::gc::kGcCauseDebugger,
art::gc::kCollectorTypeDebugger); art::gc::kCollectorTypeDebugger);
art::thread_list::ScopedSuspendAll suspend("Yahfa Hook", false); art::thread_list::ScopedSuspendAll suspend("Yahfa Hook", false);
return yahfa::backupAndHookNative(env, clazz, target, hook, backup, clearData); return yahfa::backupAndHookNative(env, clazz, target, hook, backup);
} }
LSP_DEF_NATIVE_METHOD(void, Yahfa, recordHooked, jobject member) { LSP_DEF_NATIVE_METHOD(void, Yahfa, recordHooked, jobject member) {
@ -172,7 +172,7 @@ namespace lspd {
LSP_NATIVE_METHOD(Yahfa, findMethodNative, LSP_NATIVE_METHOD(Yahfa, findMethodNative,
"(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/reflect/Executable;"), "(Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/reflect/Executable;"),
LSP_NATIVE_METHOD(Yahfa, backupAndHookNative, LSP_NATIVE_METHOD(Yahfa, backupAndHookNative,
"(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;Z)Z"), "(Ljava/lang/reflect/Executable;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;)Z"),
LSP_NATIVE_METHOD(Yahfa, recordHooked, "(Ljava/lang/reflect/Executable;)V"), LSP_NATIVE_METHOD(Yahfa, recordHooked, "(Ljava/lang/reflect/Executable;)V"),
LSP_NATIVE_METHOD(Yahfa, isHooked, "(Ljava/lang/reflect/Executable;)Z"), LSP_NATIVE_METHOD(Yahfa, isHooked, "(Ljava/lang/reflect/Executable;)Z"),
LSP_NATIVE_METHOD(Yahfa, buildHooker, LSP_NATIVE_METHOD(Yahfa, buildHooker,

View File

@ -27,7 +27,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -52,11 +51,7 @@ public class HookMain {
// backup is just a placeholder and the constraint could be less strict // backup is just a placeholder and the constraint could be less strict
checkCompatibleMethods(target, backup, "Backup"); checkCompatibleMethods(target, backup, "Backup");
} }
// Since Android 7.0, the data_ member is used to save a profiling info which used for optimizing if(!Yahfa.backupAndHookNative(target, hook, backup)){
// This may cause some crashes, clear the member to avoid it
// Note that this cannot be applied to native and proxy methods as their data_ member has been used for other purposes
boolean clearData = !(Modifier.isNative(target.getModifiers()) || Proxy.isProxyClass(target.getDeclaringClass()));
if(!Yahfa.backupAndHookNative(target, hook, backup, clearData)) {
throw new RuntimeException("Failed to hook " + target + " with " + hook); throw new RuntimeException("Failed to hook " + target + " with " + hook);
} else { } else {
Yahfa.recordHooked(target); Yahfa.recordHooked(target);

View File

@ -25,7 +25,7 @@ import java.lang.reflect.Method;
public class Yahfa { public class Yahfa {
public static native boolean backupAndHookNative(Executable target, Method hook, Method backup, boolean clearData); public static native boolean backupAndHookNative(Executable target, Method hook, Method backup);
// JNI.ToReflectedMethod() could return either Method or Constructor // JNI.ToReflectedMethod() could return either Method or Constructor
public static native Executable findMethodNative(Class<?> targetClass, String methodName, String methodSig); public static native Executable findMethodNative(Class<?> targetClass, String methodName, String methodSig);