Skip to content

Commit

Permalink
No need to declare constants for Android API versions
Browse files Browse the repository at this point in the history
The NDK has constants for these already :)
  • Loading branch information
paulo-raca committed Mar 1, 2019
1 parent 87abb05 commit ee20e5f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 22 deletions.
26 changes: 13 additions & 13 deletions library/src/main/jni/HookMain.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers
SDKVersion = sdkVersion;
LOGI("init to SDK %d", sdkVersion);
switch (sdkVersion) {
case ANDROID_P:
case __ANDROID_API_P__:
kAccCompileDontBother = 0x02000000;
OFFSET_ArtMehod_in_Object = 0;
OFFSET_access_flags_in_ArtMethod = 4;
Expand All @@ -46,9 +46,9 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers
roundUpToPtrSize(4 * 4 + 2 * 2) + pointer_size;
ArtMethodSize = roundUpToPtrSize(4 * 4 + 2 * 2) + pointer_size * 2;
break;
case ANDROID_O2:
case __ANDROID_API_O_MR1__:
kAccCompileDontBother = 0x02000000;
case ANDROID_O:
case __ANDROID_API_O__:
OFFSET_ArtMehod_in_Object = 0;
OFFSET_access_flags_in_ArtMethod = 4;
OFFSET_dex_method_index_in_ArtMethod = 4 * 3;
Expand All @@ -58,8 +58,8 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers
roundUpToPtrSize(4 * 4 + 2 * 2) + pointer_size * 2;
ArtMethodSize = roundUpToPtrSize(4 * 4 + 2 * 2) + pointer_size * 3;
break;
case ANDROID_N2:
case ANDROID_N:
case __ANDROID_API_N_MR1__:
case __ANDROID_API_N__:
OFFSET_ArtMehod_in_Object = 0;
OFFSET_access_flags_in_ArtMethod = 4; // sizeof(GcRoot<mirror::Class>) = 4
OFFSET_dex_method_index_in_ArtMethod = 4 * 3;
Expand All @@ -72,7 +72,7 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers

ArtMethodSize = roundUpToPtrSize(4 * 4 + 2 * 2) + pointer_size * 4;
break;
case ANDROID_M:
case __ANDROID_API_M__:
OFFSET_ArtMehod_in_Object = 0;
OFFSET_entry_point_from_interpreter_in_ArtMethod = roundUpToPtrSize(4 * 7);
OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod =
Expand All @@ -82,7 +82,7 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers
OFFSET_array_in_PointerArray = 4 * 3;
ArtMethodSize = roundUpToPtrSize(4 * 7) + pointer_size * 3;
break;
case ANDROID_L2:
case __ANDROID_API_L_MR1__:
OFFSET_ArtMehod_in_Object = 4 * 2;
OFFSET_entry_point_from_interpreter_in_ArtMethod = roundUpToPtrSize(
OFFSET_ArtMehod_in_Object + 4 * 7);
Expand All @@ -93,7 +93,7 @@ void Java_lab_galaxy_yahfa_HookMain_init(JNIEnv *env, jclass clazz, jint sdkVers
OFFSET_array_in_PointerArray = 12;
ArtMethodSize = OFFSET_entry_point_from_interpreter_in_ArtMethod + pointer_size * 3;
break;
case ANDROID_L:
case __ANDROID_API_L__:
OFFSET_ArtMehod_in_Object = 4 * 2;
OFFSET_entry_point_from_interpreter_in_ArtMethod = OFFSET_ArtMehod_in_Object + 4 * 4;
OFFSET_entry_point_from_quick_compiled_code_in_ArtMethod =
Expand Down Expand Up @@ -139,7 +139,7 @@ static int doBackupAndHook(void *targetMethod, void *hookMethod, void *backupMet

// set kAccCompileDontBother for a method we do not want the compiler to compile
// so that we don't need to worry about hotness_count_
if (SDKVersion >= ANDROID_N) {
if (SDKVersion >= __ANDROID_API_N__) {
setNonCompilable(targetMethod);
setNonCompilable(hookMethod);
}
Expand Down Expand Up @@ -177,7 +177,7 @@ static int doBackupAndHook(void *targetMethod, void *hookMethod, void *backupMet
}

// set the target method to native so that Android O wouldn't invoke it with interpreter
if (SDKVersion >= ANDROID_O) {
if (SDKVersion >= __ANDROID_API_O__) {
int access_flags = read32((char *) targetMethod + OFFSET_access_flags_in_ArtMethod);
access_flags |= kAccNative;
memcpy(
Expand All @@ -194,7 +194,7 @@ static int doBackupAndHook(void *targetMethod, void *hookMethod, void *backupMet
}

static void ensureMethodCached(void *hookMethod, void *backupMethod) {
if (SDKVersion <= ANDROID_O2) {
if (SDKVersion <= __ANDROID_API_O_MR1__) {
// update the cached method manually
// first we find the array of cached methods
void *dexCacheResolvedMethods = (void *) readAddr(
Expand All @@ -206,7 +206,7 @@ static void ensureMethodCached(void *hookMethod, void *backupMethod) {
(void *) ((char *) backupMethod + OFFSET_dex_method_index_in_ArtMethod));

// finally the addr of backup method is put at the corresponding location in cached methods array
if (SDKVersion == ANDROID_O2) {
if (SDKVersion == __ANDROID_API_O_MR1__) {
// array of MethodDexCacheType is used as dexCacheResolvedMethods in Android 8.1
// struct:
// struct NativeDexCachePair<T> = { T*, size_t idx }
Expand Down Expand Up @@ -299,4 +299,4 @@ void Java_lab_galaxy_yahfa_HookMain_ensureMethodCached(JNIEnv *env, jclass clazz
jobject hook,
jobject backup) {
ensureMethodCached((void *) (*env)->FromReflectedMethod(env, hook), backup == NULL ? NULL : (void *) (*env)->FromReflectedMethod(env, backup));
}
}
9 changes: 0 additions & 9 deletions library/src/main/jni/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
#ifndef YAHFA_ENV_H
#define YAHFA_ENV_H

#define ANDROID_L 21
#define ANDROID_L2 22
#define ANDROID_M 23
#define ANDROID_N 24
#define ANDROID_N2 25
#define ANDROID_O 26
#define ANDROID_O2 27
#define ANDROID_P 28

#define pointer_size sizeof(void*)
#define roundUpToPtrSize(v) (v + pointer_size - 1 - (v - 1) % pointer_size)

Expand Down

0 comments on commit ee20e5f

Please sign in to comment.