190 lines
6.6 KiB
Java
Raw Normal View History

package com.topjohnwu.magisk;
import static com.topjohnwu.magisk.BuildConfig.APPLICATION_ID;
import android.app.AppComponentFactory;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
2022-02-13 06:22:42 -08:00
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
2021-12-15 03:18:23 +08:00
import com.topjohnwu.magisk.utils.APKInstall;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
2022-02-13 06:22:42 -08:00
import java.util.HashMap;
2021-09-02 21:31:33 -07:00
import io.michaelrocks.paranoid.Obfuscate;
@Obfuscate
2021-12-15 03:18:23 +08:00
@SuppressWarnings("ResultOfMethodCallIgnored")
2021-12-13 03:57:27 -08:00
public class DynLoad {
static Object componentFactory;
2022-02-13 03:32:11 -08:00
static StubApk.Data createApkData() {
var data = new StubApk.Data();
data.setVersion(BuildConfig.STUB_VERSION);
2022-02-13 07:24:34 -08:00
data.setClassToComponent(new HashMap<>());
2022-02-13 03:32:11 -08:00
data.setRootService(DelegateRootService.class);
return data;
}
static void attachContext(Object o, Context context) {
if (!(o instanceof ContextWrapper))
return;
try {
Method m = ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class);
m.setAccessible(true);
m.invoke(o, context);
} catch (Exception ignored) { /* Impossible */ }
}
2022-02-13 07:24:34 -08:00
// Dynamically load APK from internal, external storage, or previous app
static AppClassLoader loadApk(Context context) {
File apk = StubApk.current(context);
File update = StubApk.update(context);
if (update.exists()) {
// Rename from update
update.renameTo(apk);
}
// Copy from external for easier development
2022-02-13 07:24:34 -08:00
if (BuildConfig.DEBUG) {
File external = new File(context.getExternalFilesDir(null), "magisk.apk");
if (external.exists()) {
try {
2021-12-15 03:18:23 +08:00
var in = new FileInputStream(external);
var out = new FileOutputStream(apk);
try (in; out) {
APKInstall.transfer(in, out);
}
} catch (IOException e) {
2021-12-13 03:57:27 -08:00
Log.e(DynLoad.class.getSimpleName(), "", e);
apk.delete();
} finally {
external.delete();
}
}
}
if (apk.exists()) {
2022-02-13 07:24:34 -08:00
return new AppClassLoader(apk);
}
2022-02-02 05:06:12 -08:00
// If no APK is loaded, attempt to copy from previous app
2022-02-13 07:24:34 -08:00
if (!context.getPackageName().equals(APPLICATION_ID)) {
try {
var info = context.getPackageManager().getApplicationInfo(APPLICATION_ID, 0);
var src = new FileInputStream(info.sourceDir);
var out = new FileOutputStream(apk);
try (src; out) {
APKInstall.transfer(src, out);
}
2022-02-13 07:24:34 -08:00
return new AppClassLoader(apk);
} catch (PackageManager.NameNotFoundException ignored) {
} catch (IOException e) {
2021-12-13 03:57:27 -08:00
Log.e(DynLoad.class.getSimpleName(), "", e);
apk.delete();
}
}
2022-02-13 07:24:34 -08:00
return null;
2021-12-13 03:57:27 -08:00
}
2022-02-13 03:32:11 -08:00
// Dynamically load APK and create the Application instance from the loaded APK
static Application createAndSetupApp(Application context) {
2022-02-13 03:32:11 -08:00
// On API >= 29, AppComponentFactory will replace the ClassLoader for us
if (Build.VERSION.SDK_INT < 29)
replaceClassLoader(context);
// noinspection InlinedApi
2022-02-13 06:22:42 -08:00
int flags = PackageManager.GET_ACTIVITIES | PackageManager.GET_SERVICES
| PackageManager.GET_PROVIDERS | PackageManager.GET_RECEIVERS
| PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
2022-02-13 14:23:06 -08:00
var pm = context.getPackageManager();
2022-02-13 06:22:42 -08:00
final PackageInfo info;
try {
// noinspection WrongConstant
2022-02-13 14:23:06 -08:00
info = pm.getPackageInfo(context.getPackageName(), flags);
2022-02-13 06:22:42 -08:00
} catch (PackageManager.NameNotFoundException e) {
// Impossible
throw new RuntimeException(e);
}
2022-02-13 03:32:11 -08:00
File apk = StubApk.current(context);
2022-02-13 07:24:34 -08:00
final var cl = loadApk(context);
if (cl != null) try {
// noinspection WrongConstant
2022-02-13 14:23:06 -08:00
var pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), flags);
2022-02-13 07:24:34 -08:00
cl.updateComponentMap(info, pkgInfo);
2022-02-13 06:22:42 -08:00
var appInfo = pkgInfo.applicationInfo;
2022-02-13 07:24:34 -08:00
var data = createApkData();
var map = data.getClassToComponent();
// Create the inverse mapping (class to component name)
for (var e : cl.mapping.entrySet()) {
map.put(e.getValue(), e.getKey());
}
2022-02-13 03:32:11 -08:00
// Create the receiver Application
2022-02-13 07:24:34 -08:00
var app = (Application) cl.loadClass(appInfo.className)
2022-02-13 03:32:11 -08:00
.getConstructor(Object.class)
.newInstance(data.getObject());
// Create the receiver component factory
if (Build.VERSION.SDK_INT >= 28 && componentFactory != null) {
2022-02-13 07:24:34 -08:00
Object factory = cl.loadClass(appInfo.appComponentFactory).newInstance();
2022-02-13 03:32:11 -08:00
var delegate = (DelegateComponentFactory) componentFactory;
delegate.receiver = (AppComponentFactory) factory;
}
2022-02-13 07:24:34 -08:00
DelegateClassLoader.cl = cl;
// Send real application to attachBaseContext
attachContext(app, context);
2022-02-13 03:32:11 -08:00
return app;
} catch (Exception e) {
Log.e(DynLoad.class.getSimpleName(), "", e);
apk.delete();
}
2022-02-13 03:32:11 -08:00
2022-02-13 07:24:34 -08:00
DelegateClassLoader.cl = new StubClassLoader(info);
2022-02-13 03:32:11 -08:00
return null;
}
// Replace LoadedApk mClassLoader
private static void replaceClassLoader(Context context) {
2021-12-13 03:57:27 -08:00
// Get ContextImpl
while (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
try {
Field mInfo = context.getClass().getDeclaredField("mPackageInfo");
mInfo.setAccessible(true);
Object loadedApk = mInfo.get(context);
assert loadedApk != null;
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
mcl.setAccessible(true);
mcl.set(loadedApk, new DelegateClassLoader());
} catch (Exception e) {
// Actually impossible as this method is only called on API < 29,
2022-02-02 05:06:12 -08:00
// and API 21 - 28 do not restrict access to these fields.
Log.e(DynLoad.class.getSimpleName(), "", e);
}
}
}