Skip to content

Commit

Permalink
修复低版本崩溃的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyesiqiu committed Feb 26, 2023
1 parent 38b261c commit 6c1d287
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
Binary file added shell/src/main/assets/i11111i111
Binary file not shown.
66 changes: 35 additions & 31 deletions shell/src/main/cpp/dpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//缓存变量
static jobject g_realApplicationInstance = nullptr;
static jclass g_realApplicationClass = nullptr;
static jobject g_context = nullptr;
void* zip_addr = nullptr;
off_t zip_size;
char *appComponentFactoryChs = nullptr;
Expand Down Expand Up @@ -352,7 +351,7 @@ void replaceApplicationOnLoadedApk(JNIEnv *env, jclass klass,jobject proxyApplic
DLOGD("replaceApplicationOnLoadedApk success!");
}

bool registerNativeMethods(JNIEnv *env) {
static bool registerNativeMethods(JNIEnv *env) {
jclass JniBridgeClass = env->FindClass("com/luoyesiqiu/shell/JniBridge");
if (env->RegisterNatives(JniBridgeClass, gMethods, sizeof(gMethods) / sizeof(gMethods[0])) ==
0) {
Expand All @@ -361,55 +360,60 @@ bool registerNativeMethods(JNIEnv *env) {
return JNI_FALSE;
}

static void loadApk(JNIEnv *env){
char apkPathChs[256] = {0};
getApkPath(env,apkPathChs,256);

if(zip_addr == nullptr){
load_zip(apkPathChs,&zip_addr,&zip_size);
}
}
static void extractDexes(){
char compressedDexesPathChs[256] = {0};
getCompressedDexesPath(compressedDexesPathChs, 256);

if(access(compressedDexesPathChs, F_OK) == -1){
zip_uint64_t dex_files_size = 0;
void *dexFilesData = read_zip_file_entry(zip_addr,zip_size,"i11111i111",&dex_files_size);
DLOGD("zipCode open = %s",compressedDexesPathChs);
int fd = open(compressedDexesPathChs, O_CREAT | O_WRONLY ,S_IRWXU);
if(fd > 0){
write(fd,dexFilesData,dex_files_size);
close(fd);
}
else {
DLOGE("zipCode write fail: %s", strerror(fd));
}
}
}

void init_app(JNIEnv *env, jclass klass, jobject context, jobject classLoader) {
DLOGD("init_app!");
clock_t start = clock();
if (nullptr == context) {
clock_t start = clock();
zip_uint64_t entry_size;

char apkPathChs[256] = {0};
getApkPath(env,apkPathChs,256);

if(zip_addr == nullptr){
load_zip(apkPathChs,&zip_addr,&zip_size);
}

char compressedDexesPathChs[256] = {0};
getCompressedDexesPath(compressedDexesPathChs, 256);

if(access(compressedDexesPathChs, F_OK) == -1){
zip_uint64_t dex_files_size = 0;
void *dexFilesData = read_zip_file_entry(zip_addr,zip_size,"i11111i111",&dex_files_size);
DLOGD("zipCode open = %s",compressedDexesPathChs);
int fd = open(compressedDexesPathChs, O_CREAT | O_WRONLY ,S_IRWXU);
if(fd > 0){
write(fd,dexFilesData,dex_files_size);
close(fd);
}
else {
DLOGE("zipCode write fail: %s", strerror(fd));
}
}
loadApk(env);
extractDexes();

zip_uint64_t entry_size;
if(codeItemFilePtr == nullptr) {
codeItemFilePtr = read_zip_file_entry(zip_addr,zip_size,"OoooooOooo",&entry_size);
}
//hexDump("read_zip_file_item item hexdump", (char *) codeItemFilePtr, 1024);
readCodeItem(env, klass,(uint8_t*)codeItemFilePtr,entry_size);

printTime("readCodeItem took =" , start);
} else {
AAsset *aAsset = getAsset(env, context, "OoooooOooo");
loadApk(env);

g_context = env->NewGlobalRef(context);
extractDexes();

AAsset *aAsset = getAsset(env, context, "OoooooOooo");
if (aAsset != nullptr) {
int len = AAsset_getLength(aAsset);
auto buf = (uint8_t *) AAsset_getBuffer(aAsset);
readCodeItem(env, klass,buf,len);
}
}
printTime("read apk data took =" , start);
}

void readCodeItem(JNIEnv *env, jclass klass,uint8_t *data,size_t data_len) {
Expand Down
10 changes: 10 additions & 0 deletions shell/src/main/java/com/luoyesiqiu/shell/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.luoyesiqiu.shell;

/**
* @author luoyesiqiu
*/
public class Global {
public volatile static boolean sIsReplacedClassLoader = false;
public volatile static boolean sNeedCalledApplication = true;
public volatile static boolean sLoadedDexes = false;
}
12 changes: 4 additions & 8 deletions shell/src/main/java/com/luoyesiqiu/shell/ProxyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
public class ProxyApplication extends Application {
private static final String TAG = ProxyApplication.class.getSimpleName();

public volatile static boolean initialized = false;

@Override
public void onCreate() {
super.onCreate();
Expand All @@ -25,11 +23,11 @@ public void onCreate() {

String realApplicationName = FileUtils.readAppName(getApplicationContext());

if (!initialized && !TextUtils.isEmpty(realApplicationName)) {
if (Global.sNeedCalledApplication && !TextUtils.isEmpty(realApplicationName)) {
Log.d(TAG, "onCreate: " + realApplicationName);
JniBridge.ra(realApplicationName);
JniBridge.craa(getApplicationContext(), realApplicationName);
JniBridge.craoc(realApplicationName);
Global.sNeedCalledApplication = false;
}
}

Expand All @@ -40,8 +38,7 @@ protected void attachBaseContext(Context base) {

Log.d(TAG,"attachBaseContext classloader = " + base.getClassLoader());


if(!initialized) {
if(!Global.sIsReplacedClassLoader) {

Log.d(TAG,"ProxyApplication init");
JniBridge.ia(base,base.getClassLoader());
Expand All @@ -51,8 +48,7 @@ protected void attachBaseContext(Context base) {
ClassLoader shellClassLoader = ShellClassLoader.loadDex(base);

JniBridge.mde(oldClassLoader,shellClassLoader);
initialized = true;

Global.sIsReplacedClassLoader = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
public class ProxyComponentFactory extends AppComponentFactory {
private static final String TAG = "dpt " + ProxyComponentFactory.class.getSimpleName();
private static AppComponentFactory sAppComponentFactory;
private ClassLoader originalClassLoader;
private ClassLoader newClassLoader;

private String getTargetClassName(ClassLoader classLoader){
Expand Down Expand Up @@ -51,8 +50,8 @@ private AppComponentFactory getTargetAppComponentFactory(ClassLoader appClassLoa
}

private ClassLoader init(ClassLoader cl){
if(!ProxyApplication.initialized){
ProxyApplication.initialized = true;
if(!Global.sLoadedDexes){
Global.sLoadedDexes = true;

JniBridge.ia(null,cl);
String apkPath = JniBridge.gap();
Expand Down Expand Up @@ -92,9 +91,11 @@ public Application instantiateApplication(ClassLoader cl, String className) thro
AppComponentFactory targetAppComponentFactory = getTargetAppComponentFactory(appClassLoader);

String applicationName = JniBridge.rapn(null);
if(originalClassLoader == null){
if(!Global.sIsReplacedClassLoader){
JniBridge.mde(cl, appClassLoader);
Global.sIsReplacedClassLoader = true;
}
Global.sNeedCalledApplication = false;
if(targetAppComponentFactory != null) {
try {
Method method = targetAppComponentFactory.getClass().getDeclaredMethod("instantiateApplication", ClassLoader.class, String.class);
Expand Down Expand Up @@ -134,11 +135,12 @@ public Application instantiateApplication(ClassLoader cl, String className) thro
@Override
public ClassLoader instantiateClassLoader(ClassLoader cl, ApplicationInfo aInfo) {
Log.d(TAG, "instantiateClassLoader() called with: cl = [" + cl + "], aInfo = [" + aInfo + "]");
originalClassLoader = cl;
ClassLoader classLoader = init(cl);

AppComponentFactory targetAppComponentFactory = getTargetAppComponentFactory(classLoader);

Global.sIsReplacedClassLoader = true;

if(targetAppComponentFactory != null) {
try {
Method method = AppComponentFactory.class.getDeclaredMethod("instantiateClassLoader", ClassLoader.class, ApplicationInfo.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.luoyesiqiu.shell.JniBridge;

import java.io.File;
import java.net.URL;

import dalvik.system.PathClassLoader;

Expand Down Expand Up @@ -48,7 +47,6 @@ protected Class<?> loadClass(String name, boolean resolve) {
return clazz;
}


public static ClassLoader loadDex(String apkPath,String dexPath){
String nativePath = apkPath.substring(0,apkPath.lastIndexOf("/")) + File.separator + "lib" + File.separator + (SystemUtils.is64Bits() ? "arm64":"arm");
Log.d(TAG, "loadDex() called with: sourcePath = [" + apkPath + "]");
Expand All @@ -61,6 +59,4 @@ public static ClassLoader loadDex(String apkPath,String dexPath){
public static ClassLoader loadDex(Context context){
return new ShellClassLoader(JniBridge.gdp(),context.getApplicationInfo().nativeLibraryDir,ClassLoader.getSystemClassLoader());
}


}

0 comments on commit 6c1d287

Please sign in to comment.