Commit c4f618d1 authored by swift_gan's avatar swift_gan

[DexMaker]load Dex by InMemoryDexClassLoader in some case

parent 8b1814e6
...@@ -35,7 +35,6 @@ import com.android.dx.rop.type.StdTypeList; ...@@ -35,7 +35,6 @@ import com.android.dx.rop.type.StdTypeList;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
...@@ -46,8 +45,6 @@ import java.util.Set; ...@@ -46,8 +45,6 @@ import java.util.Set;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
import dalvik.system.PathClassLoader;
import static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR; import static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR;
import static java.lang.reflect.Modifier.PRIVATE; import static java.lang.reflect.Modifier.PRIVATE;
import static java.lang.reflect.Modifier.STATIC; import static java.lang.reflect.Modifier.STATIC;
...@@ -537,8 +534,6 @@ public final class DexMaker { ...@@ -537,8 +534,6 @@ public final class DexMaker {
} catch (Throwable throwable) {} } catch (Throwable throwable) {}
} }
byte[] dex = generate();
/* /*
* This implementation currently dumps the dex to the filesystem. It * This implementation currently dumps the dex to the filesystem. It
* jars the emitted .dex for the benefit of Gingerbread and earlier * jars the emitted .dex for the benefit of Gingerbread and earlier
...@@ -555,6 +550,7 @@ public final class DexMaker { ...@@ -555,6 +550,7 @@ public final class DexMaker {
result.createNewFile(); result.createNewFile();
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result)); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME); JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME);
byte[] dex = generate();
entry.setSize(dex.length); entry.setSize(dex.length);
jarOut.putNextEntry(entry); jarOut.putNextEntry(entry);
jarOut.write(dex); jarOut.write(dex);
......
package com.swift.sandhook.xposedcompat.methodgen; package com.swift.sandhook.xposedcompat.methodgen;
import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.dx.BinaryOp; import com.android.dx.BinaryOp;
...@@ -12,18 +13,22 @@ import com.android.dx.Local; ...@@ -12,18 +13,22 @@ import com.android.dx.Local;
import com.android.dx.MethodId; import com.android.dx.MethodId;
import com.android.dx.TypeId; import com.android.dx.TypeId;
import com.swift.sandhook.SandHook; import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.SandHookMethodResolver; import com.swift.sandhook.SandHookMethodResolver;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.xposedcompat.XposedCompat; import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.utils.DexLog; import com.swift.sandhook.xposedcompat.utils.DexLog;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Member; import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Map; import java.util.Map;
import dalvik.system.InMemoryDexClassLoader;
import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedBridge;
...@@ -213,12 +218,29 @@ public class HookerDexMaker implements HookMaker { ...@@ -213,12 +218,29 @@ public class HookerDexMaker implements HookMaker {
generateCallBackupMethod(); generateCallBackupMethod();
generateHookMethod(); generateHookMethod();
ClassLoader loader; ClassLoader loader = null;
if (TextUtils.isEmpty(mDexDirPath)) { if (TextUtils.isEmpty(mDexDirPath)) {
throw new IllegalArgumentException("dexDirPath should not be empty!!!"); if (SandHookConfig.SDK_INT < Build.VERSION_CODES.O) {
throw new IllegalArgumentException("dexDirPath should not be empty!!!");
} else {
byte[] dexBytes = mDexMaker.generate();
loader = new InMemoryDexClassLoader(ByteBuffer.wrap(dexBytes), mAppClassLoader);
}
} else {
// Create the dex file and load it.
try {
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
} catch (IOException e) {
//can not write file
if (SandHookConfig.SDK_INT >= Build.VERSION_CODES.O) {
byte[] dexBytes = mDexMaker.generate();
loader = new InMemoryDexClassLoader(ByteBuffer.wrap(dexBytes), mAppClassLoader);
}
}
} }
// Create the dex file and load it. if (loader == null)
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName); return null;
return loadHookerClass(loader, className); return loadHookerClass(loader, className);
} }
......
package com.swift.sandhook.xposedcompat.methodgen; package com.swift.sandhook.xposedcompat.methodgen;
import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import com.android.dx.Code; import com.android.dx.Code;
...@@ -9,16 +10,20 @@ import com.android.dx.Local; ...@@ -9,16 +10,20 @@ import com.android.dx.Local;
import com.android.dx.MethodId; import com.android.dx.MethodId;
import com.android.dx.TypeId; import com.android.dx.TypeId;
import com.swift.sandhook.SandHook; import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.xposedcompat.hookstub.HookStubManager; import com.swift.sandhook.xposedcompat.hookstub.HookStubManager;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Member; import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Map; import java.util.Map;
import dalvik.system.InMemoryDexClassLoader;
import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.XposedHelpers;
...@@ -167,12 +172,28 @@ public class HookerDexMakerNew implements HookMaker { ...@@ -167,12 +172,28 @@ public class HookerDexMakerNew implements HookMaker {
generateHookMethod(); generateHookMethod();
generateBackupMethod(); generateBackupMethod();
ClassLoader loader; ClassLoader loader = null;
if (TextUtils.isEmpty(mDexDirPath)) { if (TextUtils.isEmpty(mDexDirPath)) {
throw new IllegalArgumentException("dexDirPath should not be empty!!!"); if (SandHookConfig.SDK_INT < Build.VERSION_CODES.O) {
throw new IllegalArgumentException("dexDirPath should not be empty!!!");
} else {
byte[] dexBytes = mDexMaker.generate();
loader = new InMemoryDexClassLoader(ByteBuffer.wrap(dexBytes), mAppClassLoader);
}
} else {
// Create the dex file and load it.
try {
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
} catch (IOException e) {
//can not write file
if (SandHookConfig.SDK_INT >= Build.VERSION_CODES.O) {
byte[] dexBytes = mDexMaker.generate();
loader = new InMemoryDexClassLoader(ByteBuffer.wrap(dexBytes), mAppClassLoader);
}
}
} }
// Create the dex file and load it. if (loader == null)
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName); return null;
return loadHookerClass(loader, className); return loadHookerClass(loader, className);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment