Skip to content

Commit

Permalink
Optimize dexmaker for yahfa
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Dec 23, 2020
1 parent 6ea5186 commit 1dde146
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 324 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ private void forkPostCommon(int pid, boolean isSystem, String appDataDir, String
mRouter.initResourcesHook();
mRouter.prepare(isSystem);
PrebuiltMethodsDeopter.deoptBootMethods(); // do it once for secondary zygote
mRouter.installBootstrapHooks(isSystem);
ConfigManager.appDataDir = appDataDir;
ConfigManager.niceName = niceName;
mRouter.installBootstrapHooks(isSystem);
XposedInit.prefsBasePath = ConfigManager.getPrefsPath("");
mRouter.onEnterChildProcess();
Utils.logI("Loading modules for " + niceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ public static String getPackageName(String dataDir) {
}

public static String getDataPathPrefix() {
return ConfigManager.getDataPathPrefix();
return ConfigManager.getDataPathPrefix() + "/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import de.robv.android.xposed.EdHooker;
import de.robv.android.xposed.XposedBridge;

import static com.elderdrivers.riru.edxp.util.FileUtils.getDataPathPrefix;
Expand All @@ -21,7 +22,7 @@

public final class DynamicBridge {

private static final HashMap<Member, Method> hookedInfo = new HashMap<>();
private static final HashMap<Member, EdHooker> hookedInfo = new HashMap<>();
private static final HookerDexMaker dexMaker = new HookerDexMaker();
private static final AtomicBoolean dexPathInited = new AtomicBoolean(false);
private static File dexDir;
Expand All @@ -47,17 +48,20 @@ public static synchronized void hookMethod(Member hookMethod, XposedBridge.Addit
}

DexLog.d("start to generate class for: " + hookMethod);
long startTime = System.nanoTime();
try {
// for Android Oreo and later use InMemoryClassLoader
if (!shouldUseInMemoryHook()) {
setupDexCachePath();
}
dexMaker.start(hookMethod, additionalHookInfo,
hookMethod.getDeclaringClass().getClassLoader(), getDexDirPath());
hookedInfo.put(hookMethod, dexMaker.getCallBackupMethod());
hookedInfo.put(hookMethod, dexMaker.getHooker());
} catch (Exception e) {
DexLog.e("error occur when generating dex. dexDir=" + dexDir, e);
}
long endTime = System.nanoTime();
DexLog.d("generated class for " + hookMethod + " in " + ((endTime-startTime) * 1.e-6) + "ms");
}

private static String getDexDirPath() {
Expand Down Expand Up @@ -106,28 +110,11 @@ private static boolean checkMember(Member member) {

public static Object invokeOriginalMethod(Member method, Object thisObject, Object[] args)
throws InvocationTargetException, IllegalAccessException {
Method callBackup = hookedInfo.get(method);
if (callBackup == null) {
EdHooker hooker = hookedInfo.get(method);
if (hooker == null) {
throw new IllegalStateException("method not hooked, cannot call original method.");
}
if (!Modifier.isStatic(callBackup.getModifiers())) {
throw new IllegalStateException("original method is not static, something must be wrong!");
}
callBackup.setAccessible(true);
if (args == null) {
args = new Object[0];
}
final int argsSize = args.length;
if (Modifier.isStatic(method.getModifiers())) {
return callBackup.invoke(null, args);
} else {
Object[] newArgs = new Object[argsSize + 1];
newArgs[0] = thisObject;
for (int i = 1; i < newArgs.length; i++) {
newArgs[i] = args[i - 1];
}
return callBackup.invoke(null, newArgs);
}
return hooker.callBackup(thisObject, args);
}
}

Expand Down
Loading

0 comments on commit 1dde146

Please sign in to comment.