Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bootloop on R Public Beta 2 #583

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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