Deoptimize resources hook related methods only if needed

For now deoptimization is only done on MIUI with resources hooking enabled.
This commit is contained in:
solohsu 2019-05-16 21:45:15 +08:00
parent 975bfd20cc
commit 83ecd3ae97
19 changed files with 71 additions and 48 deletions

View File

@ -13,6 +13,7 @@ import java.util.HashMap;
public class InlinedMethodCallers {
public static final String KEY_BOOT_IMAGE = "boot_image";
public static final String KEY_BOOT_IMAGE_MIUI_RES = "boot_image_miui_res";
public static final String KEY_SYSTEM_SERVER = "system_server";
/**
@ -27,7 +28,9 @@ public class InlinedMethodCallers {
private static final String[][] BOOT_IMAGE = {
// callers of Application#attach(Context)
{"android.app.Instrumentation", "newApplication", "(Ljava/lang/ClassLoader;Ljava/lang/String;Landroid/content/Context;)Landroid/app/Application;"},
};
private static final String[][] BOOT_IMAGE_FOR_MIUI_RES = {
// for MIUI resources hooking
{"android.content.res.MiuiResources", "init", "(Ljava/lang/String;)V"},
{"android.content.res.MiuiResources", "updateMiuiImpl", "()V"},
@ -50,6 +53,7 @@ public class InlinedMethodCallers {
static {
CALLERS.put(KEY_BOOT_IMAGE, BOOT_IMAGE);
CALLERS.put(KEY_BOOT_IMAGE_MIUI_RES, BOOT_IMAGE_FOR_MIUI_RES);
CALLERS.put(KEY_SYSTEM_SERVER, SYSTEM_SERVER);
CALLERS.put("com.android.systemui", SYSTEM_UI);
}

View File

@ -1,5 +1,7 @@
package com.elderdrivers.riru.edxp.deopt;
import android.text.TextUtils;
import com.elderdrivers.riru.edxp.config.EdXpConfigGlobal;
import com.elderdrivers.riru.edxp.util.Utils;
@ -8,6 +10,7 @@ import java.util.Arrays;
import de.robv.android.xposed.XposedHelpers;
import static com.elderdrivers.riru.edxp.deopt.InlinedMethodCallers.KEY_BOOT_IMAGE;
import static com.elderdrivers.riru.edxp.deopt.InlinedMethodCallers.KEY_BOOT_IMAGE_MIUI_RES;
import static com.elderdrivers.riru.edxp.deopt.InlinedMethodCallers.KEY_SYSTEM_SERVER;
public class PrebuiltMethodsDeopter {
@ -37,6 +40,11 @@ public class PrebuiltMethodsDeopter {
public static void deoptBootMethods() {
// todo check if has been done before
deoptMethods(KEY_BOOT_IMAGE, null);
if (!TextUtils.isEmpty(Utils.getSysProp("ro.miui.ui.version.code"))
&& EdXpConfigGlobal.getConfig().isResourcesHookEnabled()) {
//deopt these only for MIUI with resources hook enabled
deoptMethods(KEY_BOOT_IMAGE_MIUI_RES, null);
}
}
public static void deoptSystemServerMethods(ClassLoader sysCL) {

View File

@ -4,6 +4,8 @@ import android.util.Log;
import com.elderdrivers.riru.edxp.BuildConfig;
import de.robv.android.xposed.XposedHelpers;
public class Utils {
@ -42,4 +44,9 @@ public class Utils {
public static void logE(String msg, Throwable throwable) {
Log.e(LOG_TAG, msg, throwable);
}
public static String getSysProp(String key) {
Class sysProps = XposedHelpers.findClassIfExists("android.os.SystemProperties", null);
return (String) XposedHelpers.callStaticMethod(sysProps, "get", key);
}
}

View File

@ -3,10 +3,10 @@ import org.gradle.internal.os.OperatingSystem
apply plugin: 'com.android.library'
version "v0.4.2.1_beta"
version "v0.4.2.2_beta"
ext {
versionCode = "4210"
versionCode = "4220"
module_name = "EdXposed"
jar_dest_dir = "${projectDir}/template_override/system/framework/"
is_windows = OperatingSystem.current().isWindows()

View File

@ -34,11 +34,13 @@ static char use_whitelist_path[PATH_MAX];
static char black_white_list_path[PATH_MAX];
static char dynamic_modules_path[PATH_MAX];
static char deopt_boot_image_path[PATH_MAX];
static char resources_hook_disable_path[PATH_MAX];
static const char *installer_package_name;
static bool black_white_list_enabled = false;
static bool dynamic_modules_enabled = false;
static bool deopt_boot_image_enabled = false;
static bool resources_hook_enabled = true;
static bool inited = false;
// snapshot at boot
static bool use_white_list_snapshot = false;
@ -115,14 +117,18 @@ static void init_once() {
installer_package_name, "dynamicmodules");
snprintf(deopt_boot_image_path, PATH_MAX, config_path_tpl, data_path_prefix,
installer_package_name, "deoptbootimage");
snprintf(resources_hook_disable_path, PATH_MAX, config_path_tpl, data_path_prefix,
installer_package_name, "disable_resources");
dynamic_modules_enabled = access(dynamic_modules_path, F_OK) == 0;
black_white_list_enabled = access(black_white_list_path, F_OK) == 0;
// use_white_list snapshot
use_white_list_snapshot = access(use_whitelist_path, F_OK) == 0;
deopt_boot_image_enabled = access(deopt_boot_image_path, F_OK) == 0;
resources_hook_enabled = access(resources_hook_disable_path, F_OK) != 0;
LOGI("black/white list mode: %d, using whitelist: %d", black_white_list_enabled,
use_white_list_snapshot);
LOGI("dynamic modules mode: %d", dynamic_modules_enabled);
LOGI("resources hook: %d", resources_hook_enabled);
if (black_white_list_enabled) {
snapshotBlackWhiteList();
}
@ -200,6 +206,11 @@ bool is_dynamic_modules_enabled() {
return dynamic_modules_enabled;
}
bool is_resources_hook_enabled() {
init_once();
return resources_hook_enabled;
}
bool is_deopt_boot_image_enabled() {
init_once();
return deopt_boot_image_enabled;

View File

@ -13,6 +13,8 @@ bool is_black_white_list_enabled();
bool is_dynamic_modules_enabled();
bool is_resources_hook_enabled();
bool is_deopt_boot_image_enabled();
jstring get_installer_pkg_name(JNIEnv *env, jclass clazz);

View File

@ -116,6 +116,9 @@ static JNINativeMethod hookMethods[] = {
{
"isDynamicModulesEnabled", "()Z", (void *) is_dynamic_modules_enabled
},
{
"isResourcesHookEnabled", "()Z", (void *) is_resources_hook_enabled
},
{
"isAppNeedHook", "(Ljava/lang/String;)Z", (void *) is_app_need_hook
},

View File

@ -1,6 +1,6 @@
#!/system/bin/sh
EDXP_VERSION="0.4.2.1_beta (4210)"
EDXP_VERSION="0.4.2.2_beta (4220)"
ANDROID_SDK=`getprop ro.build.version.sdk`
BUILD_DESC=`getprop ro.build.description`
PRODUCT=`getprop ro.build.product`

View File

@ -151,4 +151,6 @@ public class Main implements KeepAll {
public static native boolean initXResourcesNative();
public static native boolean removeFinalFlagNative(Class clazz);
public static native boolean isResourcesHookEnabled();
}

View File

@ -1,9 +1,9 @@
package com.elderdrivers.riru.edxp.sandhook.config;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.config.EdXpConfig;
import com.elderdrivers.riru.edxp.config.InstallerChooser;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.sandhook.entry.hooker.XposedBlackListHooker;
import com.elderdrivers.riru.edxp.hooker.XposedBlackListHooker;
public class SandHookEdxpConfig implements EdXpConfig {
@Override
@ -20,4 +20,9 @@ public class SandHookEdxpConfig implements EdXpConfig {
public boolean isDynamicModulesMode() {
return Main.isDynamicModulesEnabled();
}
@Override
public boolean isResourcesHookEnabled() {
return Main.isResourcesHookEnabled();
}
}

View File

@ -142,4 +142,6 @@ public class Main implements KeepAll {
public static native boolean initXResourcesNative();
public static native boolean removeFinalFlagNative(Class clazz);
public static native boolean isResourcesHookEnabled();
}

View File

@ -1,9 +1,9 @@
package com.elderdrivers.riru.edxp.whale.config;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.config.EdXpConfig;
import com.elderdrivers.riru.edxp.config.InstallerChooser;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.whale.entry.hooker.XposedBlackListHooker;
import com.elderdrivers.riru.edxp.hooker.XposedBlackListHooker;
public class WhaleEdxpConfig implements EdXpConfig {
@Override
@ -20,4 +20,9 @@ public class WhaleEdxpConfig implements EdXpConfig {
public boolean isDynamicModulesMode() {
return Main.isDynamicModulesEnabled();
}
@Override
public boolean isResourcesHookEnabled() {
return Main.isResourcesHookEnabled();
}
}

View File

@ -144,4 +144,6 @@ public class Main implements KeepAll {
public static native boolean initXResourcesNative();
public static native boolean removeFinalFlagNative(Class clazz);
public static native boolean isResourcesHookEnabled();
}

View File

@ -1,9 +1,9 @@
package com.elderdrivers.riru.edxp.yahfa.config;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.config.EdXpConfig;
import com.elderdrivers.riru.edxp.config.InstallerChooser;
import com.elderdrivers.riru.edxp.Main;
import com.elderdrivers.riru.edxp.yahfa.entry.hooker.XposedBlackListHooker;
import com.elderdrivers.riru.edxp.hooker.XposedBlackListHooker;
public class YahfaEdxpConfig implements EdXpConfig {
@Override
@ -20,4 +20,9 @@ public class YahfaEdxpConfig implements EdXpConfig {
public boolean isDynamicModulesMode() {
return Main.isDynamicModulesEnabled();
}
@Override
public boolean isResourcesHookEnabled() {
return Main.isResourcesHookEnabled();
}
}

View File

@ -2,11 +2,12 @@ package com.elderdrivers.riru.edxp.config;
public interface EdXpConfig {
String getInstallerBaseDir();
String getBlackListModulePackageName();
boolean isDynamicModulesMode();
boolean isResourcesHookEnabled();
}

View File

@ -9,7 +9,7 @@ public class EdXpConfigGlobal {
public static EdXpConfig getConfig() {
if (sConfig == null) {
return defaultConfig;
throw new IllegalArgumentException("sConfig should not be null.");
}
return sConfig;
}
@ -20,23 +20,4 @@ public class EdXpConfigGlobal {
}
return sHookProvider;
}
private static final EdXpConfig defaultConfig = new EdXpConfig() {
@Override
public String getInstallerBaseDir() {
return "";
}
@Override
public String getBlackListModulePackageName() {
return "";
}
@Override
public boolean isDynamicModulesMode() {
return false;
}
};
}

View File

@ -90,15 +90,9 @@ public final class XposedBridge {
public static volatile ClassLoader dummyClassLoader = null;
public static void initXResources() {
if (disableHooks) {
if (!EdXpConfigGlobal.getConfig().isResourcesHookEnabled()) {
return;
}
String BASE_DIR = EdXpConfigGlobal.getConfig().getInstallerBaseDir();
if (SELinuxHelper.getAppDataFileService().checkFileExists(BASE_DIR + "conf/disable_resources")) {
Log.w(TAG, "Found " + BASE_DIR + "conf/disable_resources, not hooking resources");
XposedInit.disableResources = true;
return;
}
if (dummyClassLoader != null) {
return;
}

View File

@ -56,7 +56,7 @@ public final class XposedInit {
private static final String startClassName = ""; // ed: no support for tool process anymore
private static final String INSTANT_RUN_CLASS = "com.android.tools.fd.runtime.BootstrapApplication";
public static boolean disableResources = false;
public static volatile boolean disableResources = false;
private static final String[] XRESOURCES_CONFLICTING_PACKAGES = {"com.sygic.aura"};
private XposedInit() {
@ -85,16 +85,7 @@ public final class XposedInit {
}
private static void hookResources() throws Throwable {
if (disableResources) {
return;
}
String BASE_DIR = EdXpConfigGlobal.getConfig().getInstallerBaseDir();
if (SELinuxHelper.getAppDataFileService().checkFileExists(BASE_DIR + "conf/disable_resources")) {
Log.w(TAG, "Found " + BASE_DIR + "conf/disable_resources, not hooking resources");
disableResources = true;
if (!EdXpConfigGlobal.getConfig().isResourcesHookEnabled() || disableResources) {
return;
}