Skip to content

Commit

Permalink
Fix bootloop on R Public Beta 2 (#583)
Browse files Browse the repository at this point in the history
* Fix bootloop on R Public Beta 2

* Fix crashes with some apps
  • Loading branch information
kotori2 authored Jul 30, 2020
1 parent 56fd1ec commit 27f44d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
35 changes: 32 additions & 3 deletions edxp-core/src/main/cpp/external/yahfa/src/HookMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ static int kAccNative = 0x0100;
static int kAccCompileDontBother = 0x01000000;
static int kAccFastInterpreterToInterpreterInvoke = 0x40000000;

static jfieldID fieldArtMethod = NULL;

static inline uint16_t read16(void *addr) {
return *((uint16_t *) addr);
}
Expand All @@ -37,8 +39,12 @@ static inline void write32(void *addr, uint32_t value) {
void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVersion) {
int i;
SDKVersion = sdkVersion;
jclass classExecutable;
LOGI("init to SDK %d", sdkVersion);
switch (sdkVersion) {
case ANDROID_R:
classExecutable = (*env)->FindClass(env, "java/lang/reflect/Executable");
fieldArtMethod = (*env)->GetFieldID(env, classExecutable, "artMethod", "J");
case ANDROID_Q:
case ANDROID_P:
kAccCompileDontBother = 0x02000000;
Expand Down Expand Up @@ -211,6 +217,10 @@ static int doBackupAndHook(JNIEnv *env, void *targetMethod, void *hookMethod, vo

static void ensureMethodCached(void *hookMethod, void *backupMethod,
void *hookClassResolvedMethods) {
if (!backupMethod || (long) backupMethod < 0x1000) {
LOGW("ensureMethodCached: backupMethod is null or illegal: %p", backupMethod);
return;
}
void *dexCacheResolvedMethods;
// then we get the dex method index of the static backup method
int methodIndex = read32(
Expand Down Expand Up @@ -255,6 +265,25 @@ static void ensureMethodCached(void *hookMethod, void *backupMethod,
}
}

static void *getArtMethod(JNIEnv *env, jobject jmethod) {
void *artMethod = NULL;

if(jmethod == NULL) {
return artMethod;
}

if(SDKVersion == ANDROID_R) {
artMethod = (void *) (*env)->GetLongField(env, jmethod, fieldArtMethod);
}
else {
artMethod = (void *) (*env)->FromReflectedMethod(env, jmethod);
}

LOGI("ArtMethod: %p", artMethod);
return artMethod;

}

jobject Java_lab_galaxy_yahfa_HookMain_findMethodNative(JNIEnv *env, jclass clazz,
jclass targetClass, jstring methodName,
jstring methodSig) {
Expand Down Expand Up @@ -287,9 +316,9 @@ jboolean Java_lab_galaxy_yahfa_HookMain_backupAndHookNative(JNIEnv *env, jclass
jobject backup) {

if (!doBackupAndHook(env,
(void *) (*env)->FromReflectedMethod(env, target),
(void *) (*env)->FromReflectedMethod(env, hook),
backup == NULL ? NULL : (void *) (*env)->FromReflectedMethod(env, backup)
getArtMethod(env, target),
getArtMethod(env, hook),
getArtMethod(env, backup)
)) {
(*env)->NewGlobalRef(env,
hook); // keep a global ref so that the hook method would not be GCed
Expand Down
1 change: 1 addition & 0 deletions edxp-core/src/main/cpp/external/yahfa/src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define ANDROID_O2 27
#define ANDROID_P 28
#define ANDROID_Q 29
#define ANDROID_R 30

#define roundUpTo4(v) ((v+4-1) - ((v+4-1)&3))
#define roundUpTo8(v) ((v+8-1) - ((v+8-1)&7))
Expand Down
5 changes: 3 additions & 2 deletions edxp-core/src/main/cpp/main/src/jni/edxp_yahfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ namespace edxp {
return;
}
void *art_method = env->FromReflectedMethod(member);
if (!art_method) {
LOGE("setNonCompilableNative: art_method is null");

if (!art_method || (long)art_method < 0x1000) {
LOGE("setNonCompilableNative: art_method is null or invalid: %p", art_method);
return;
}
setNonCompilable(art_method);
Expand Down

0 comments on commit 27f44d3

Please sign in to comment.