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;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
......@@ -46,8 +45,6 @@ import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import dalvik.system.PathClassLoader;
import static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR;
import static java.lang.reflect.Modifier.PRIVATE;
import static java.lang.reflect.Modifier.STATIC;
......@@ -537,8 +534,6 @@ public final class DexMaker {
} catch (Throwable throwable) {}
}
byte[] dex = generate();
/*
* This implementation currently dumps the dex to the filesystem. It
* jars the emitted .dex for the benefit of Gingerbread and earlier
......@@ -555,6 +550,7 @@ public final class DexMaker {
result.createNewFile();
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME);
byte[] dex = generate();
entry.setSize(dex.length);
jarOut.putNextEntry(entry);
jarOut.write(dex);
......
package com.swift.sandhook.xposedcompat.methodgen;
import android.os.Build;
import android.text.TextUtils;
import com.android.dx.BinaryOp;
......@@ -12,18 +13,22 @@ import com.android.dx.Local;
import com.android.dx.MethodId;
import com.android.dx.TypeId;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.SandHookMethodResolver;
import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.utils.DexLog;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Map;
import dalvik.system.InMemoryDexClassLoader;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
......@@ -213,12 +218,29 @@ public class HookerDexMaker implements HookMaker {
generateCallBackupMethod();
generateHookMethod();
ClassLoader loader;
ClassLoader loader = null;
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.
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
if (loader == null)
return null;
return loadHookerClass(loader, className);
}
......
package com.swift.sandhook.xposedcompat.methodgen;
import android.os.Build;
import android.text.TextUtils;
import com.android.dx.Code;
......@@ -9,16 +10,20 @@ import com.android.dx.Local;
import com.android.dx.MethodId;
import com.android.dx.TypeId;
import com.swift.sandhook.SandHook;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.xposedcompat.hookstub.HookStubManager;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Map;
import dalvik.system.InMemoryDexClassLoader;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
......@@ -167,12 +172,28 @@ public class HookerDexMakerNew implements HookMaker {
generateHookMethod();
generateBackupMethod();
ClassLoader loader;
ClassLoader loader = null;
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.
loader = mDexMaker.generateAndLoad(mAppClassLoader, new File(mDexDirPath), dexName);
if (loader == null)
return null;
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