Skip to content

Commit

Permalink
Merge pull request #228 from ElderDrivers/resources-hook
Browse files Browse the repository at this point in the history
Resources hook for ROMs with custom Resources subclass
  • Loading branch information
solohsu authored May 1, 2019
2 parents edda665 + 17c703e commit 71449e2
Show file tree
Hide file tree
Showing 38 changed files with 283 additions and 184 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.elderdrivers.riru.edxp.config;

import android.content.res.Resources;
import android.content.res.XResources;

import com.elderdrivers.riru.edxp.deopt.PrebuiltMethodsDeopter;
import com.elderdrivers.riru.edxp.hook.HookProvider;

Expand Down Expand Up @@ -40,9 +37,4 @@ public void deoptMethodNative(Object method) {
public boolean initXResourcesNative() {
return false;
}

@Override
public void rewriteXmlReferencesNative(long parserPtr, XResources origRes, Resources repRes) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,22 @@ 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;"}
{"android.app.Instrumentation", "newApplication", "(Ljava/lang/ClassLoader;Ljava/lang/String;Landroid/content/Context;)Landroid/app/Application;"},

// for MIUI resources hooking
{"android.content.res.MiuiResources", "init", "(Ljava/lang/String;)V"},
{"android.content.res.MiuiResources", "updateMiuiImpl", "()V"},
{"android.content.res.MiuiResources", "setImpl", "(Landroid/content/res/ResourcesImpl;)V"},
{"android.content.res.MiuiResources", "loadOverlayValue", "(Landroid/util/TypedValue;I)V"},
{"android.content.res.MiuiResources", "getThemeString", "(I)Ljava/lang/CharSequence;"},
{"android.content.res.MiuiResources", "<init>", "(Ljava/lang/ClassLoader;)V"},
{"android.content.res.MiuiResources", "<init>", "()V"},
{"android.content.res.MiuiResources", "<init>", "(Landroid/content/res/AssetManager;Landroid/util/DisplayMetrics;Landroid/content/res/Configuration;)V"},
{"android.miui.ResourcesManager", "initMiuiResource", "(Landroid/content/res/Resources;Ljava/lang/String;)V"},
{"android.app.LoadedApk", "getResources", "()Landroid/content/res/Resources;"},
{"android.content.res.Resources", "getSystem", "()Landroid/content/res/Resources;"},
{"android.app.ApplicationPackageManager", "getResourcesForApplication", "(Landroid/content/pm/ApplicationInfo;)Landroid/content/res/Resources;"},
{"android.app.ContextImpl", "setResources", "(Landroid/content/res/Resources;)V"},
};

private static final String[][] SYSTEM_SERVER = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ public static void deoptMethods(String where, ClassLoader cl) {
}
for (String[] caller : callers) {
try {
Class clazz = XposedHelpers.findClassIfExists(caller[0], cl);
if (clazz == null) {
continue;
}
Object method = EdXpConfigGlobal.getHookProvider().findMethodNative(
XposedHelpers.findClass(caller[0], cl), caller[1], caller[2]);
clazz, caller[1], caller[2]);
if (method != null) {
EdXpConfigGlobal.getHookProvider().deoptMethodNative(method);
}
Expand Down
2 changes: 2 additions & 0 deletions edxp-core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/libs
/obj
/release
/template_override/module.prop
/template_override/riru_module.prop
/template_override/system
/template_override/system_x86
*.iml
31 changes: 24 additions & 7 deletions edxp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import org.gradle.internal.os.OperatingSystem

apply plugin: 'com.android.library'

version "v0.4.0.1_beta-SNAPSHOT"
version "v0.4.1.2_beta"

ext {
versionCode = "4120"
module_name = "EdXposed"
jar_dest_dir = "${projectDir}/template_override/system/framework/"
is_windows = OperatingSystem.current().isWindows()
backends = ["Yahfa", "Sandhook", "Whale"]
backends = ["YAHFA", "SandHook", "Whale"]
yahfa_authors = "solohsu, rk700 & MlgmXyysd"
sandhook_authors = "solohsu, ganyao114 & MlgmXyysd"
whale_authors = "solohsu, asLody & MlgmXyysd"
}

android {
Expand Down Expand Up @@ -72,21 +76,34 @@ afterEvaluate {

backends.each { backend ->

def backendCapped = backend.capitalize()
def backendLowered = backend.toLowerCase()
def backendCapped = backendLowered.capitalize()
def authorList = property("${backendLowered}" + "_authors")

def zipTask = task("zip${backendCapped}${variantCapped}", type: Exec) {
dependsOn cleanTemplate, copyDalvikdxJar, copyDexmakerJar
dependsOn tasks.getByPath(":edxp-${backendLowered}:makeAndCopy${variantCapped}")
workingDir '..'
commandLine 'sh', 'build.sh', project.name,
"${backendLowered}-${project.version}-${variantLowered}", "${module_name}"
"${backend}-${project.version}-${variantLowered}", "${module_name}"
doFirst {
copy {
from "${projectDir}/edconfig.tpl"
from "${projectDir}/tpl/edconfig.tpl"
into templateFrameworkPath
rename "edconfig.tpl", "edconfig.jar"
expand(backend: "$backendCapped")
expand(version: "$version", backend: "$backend")
}
copy {
from "${projectDir}/tpl/module.prop.tpl"
into templateRootPath
rename "module.prop.tpl", "module.prop"
expand(backend: "$backendCapped", versionName: "$version" + "($backend)",
versionCode: "$versionCode", authorList: "$authorList")
}
copy {
from "${templateRootPath}/module.prop"
into templateRootPath
rename "module.prop", "riru_module.prop"
}
}
}
Expand All @@ -95,7 +112,7 @@ afterEvaluate {
dependsOn zipTask
workingDir "${projectDir}/release"
def commands = ["adb", "push",
"magisk-${module_name}-${backendLowered}-${project.version}-${variantLowered}.zip",
"magisk-${module_name}-${backend}-${project.version}-${variantLowered}.zip",
"/sdcard/"]
if (is_windows) {
commandLine 'cmd', '/c', commands.join(" ")
Expand Down
5 changes: 0 additions & 5 deletions edxp-core/edconfig.tpl

This file was deleted.

15 changes: 14 additions & 1 deletion edxp-core/jni/main/java_hook/java_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ void setMethodNonCompilable(JNIEnv *env, jclass, jobject member) {
setNonCompilable(artMethod);
}

static constexpr uint32_t kAccFinal = 0x0010;

jboolean removeFinalFlag(JNIEnv *env, jclass, jclass clazz) {
if (clazz) {
jfieldID java_lang_Class_accessFlags = env->GetFieldID(
env->FindClass("java/lang/Class"), "accessFlags", "I");
jint access_flags = env->GetIntField(clazz, java_lang_Class_accessFlags);
env->SetIntField(clazz, java_lang_Class_accessFlags, access_flags & ~kAccFinal);
return true;
}
return false;
}

static JNINativeMethod hookMethods[] = {
{
"init",
Expand Down Expand Up @@ -131,7 +144,7 @@ static JNINativeMethod hookMethods[] = {
"initXResourcesNative", "()Z", (void *) XposedBridge_initXResourcesNative
},
{
"rewriteXmlReferencesNative", "(JLandroid/content/res/XResources;Landroid/content/res/Resources;)V", (void *) XResources_rewriteXmlReferencesNative
"removeFinalFlagNative", "(Ljava/lang/Class;)Z", (void *) removeFinalFlag
}
};

Expand Down
7 changes: 7 additions & 0 deletions edxp-core/jni/main/native_hook/native_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ static bool onIsInSamePackageCalled(void *thiz, void *that) {
|| strstr(thatDesc, "com/elderdrivers/riru/") != nullptr) {
return true;
}
// for MIUI resources hooking
if (strstr(thisDesc, "android/content/res/MiuiTypedArray") != nullptr
|| strstr(thatDesc, "android/content/res/MiuiTypedArray") != nullptr
|| strstr(thisDesc, "android/content/res/XResources$XTypedArray") != nullptr
|| strstr(thatDesc, "android/content/res/XResources$XTypedArray") != nullptr) {
return true;
}
return (*isInSamePackageBackup)(thiz, that);
}

Expand Down
18 changes: 17 additions & 1 deletion edxp-core/jni/main/native_hook/resource_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <include/ByteOrder.h>
#include <include/logging.h>
#include <dlfcn.h>
#include <java_hook/java_hook.h>
#include "resource_hook.h"

#define CLASS_XRESOURCES "android/content/res/XResources"
Expand Down Expand Up @@ -44,7 +45,7 @@ bool prepareSymbols() {
#if defined(__LP64__)
"_ZNK7android12ResXMLParser18getAttributeNameIDEm"
#else
"_ZNK7android12ResXMLParser18getAttributeNameIDEj"
"_ZNK7android12ResXMLParser18getAttributeNameIDEj"
#endif
));
if (!ResXMLParser_getAttributeNameID) {
Expand All @@ -66,6 +67,15 @@ bool prepareSymbols() {
return true;
}

int register_natives_XResources(JNIEnv *env, jclass clazz) {
const JNINativeMethod methods[] = {
{"rewriteXmlReferencesNative",
"(JLandroid/content/res/XResources;Landroid/content/res/Resources;)V",
(void *) XResources_rewriteXmlReferencesNative},
};
return env->RegisterNatives(clazz, methods, NELEM(methods));
}

jboolean XposedBridge_initXResourcesNative(JNIEnv *env, jclass) {
classXResources = env->FindClass(CLASS_XRESOURCES);
if (classXResources == NULL) {
Expand All @@ -75,6 +85,12 @@ jboolean XposedBridge_initXResourcesNative(JNIEnv *env, jclass) {
}
classXResources = reinterpret_cast<jclass>(env->NewGlobalRef(classXResources));

if (register_natives_XResources(env, classXResources) != JNI_OK) {
LOGE("Could not register natives for '%s'", CLASS_XRESOURCES);
env->ExceptionClear();
return false;
}

methodXResourcesTranslateResId = env->GetStaticMethodID(classXResources, "translateResId",
"(ILandroid/content/res/XResources;Landroid/content/res/Resources;)I");
if (methodXResourcesTranslateResId == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion edxp-core/template_override/common/util_functions.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/system/bin/sh

EDXP_VERSION="0.4.0.1_beta-SNAPSHOT (4010)"
EDXP_VERSION="0.4.1.2_beta (4120)"
ANDROID_SDK=`getprop ro.build.version.sdk`
BUILD_DESC=`getprop ro.build.description`
PRODUCT=`getprop ro.build.product`
Expand Down
2 changes: 1 addition & 1 deletion edxp-core/template_override/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ LATESTARTSERVICE=false

print_modname() {
ui_print "************************************"
ui_print " Riru - Ed Xposed v0.4.0.1 "
ui_print " Riru - Ed Xposed "
ui_print "************************************"
}

Expand Down
5 changes: 0 additions & 5 deletions edxp-core/template_override/riru_module.prop

This file was deleted.

5 changes: 5 additions & 0 deletions edxp-core/tpl/edconfig.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version=90.0-$version ($backend)
arch=arm64
minsdk=26
maxsdk=28
requires:fbe_aware=1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
id=riru_edxposed
name=Riru - Ed Xposed
version=v0.4.0.1_beta-SNAPSHOT
versionCode=4010
author=solohsu & MlgmXyysd
version=${versionName}
versionCode=${versionCode}
author=${authorList}
description=Magisk version of Xposed. Require Riru - Core installed.
minMagisk=17000
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.elderdrivers.riru.edxp;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.content.res.XResources;
import android.os.Build;
import android.os.Process;

import com.elderdrivers.riru.common.KeepAll;
import com.elderdrivers.riru.edxp.sandhook.BuildConfig;
import com.elderdrivers.riru.edxp.config.InstallerChooser;
import com.elderdrivers.riru.edxp.util.Utils;
import com.elderdrivers.riru.edxp.sandhook.BuildConfig;
import com.elderdrivers.riru.edxp.sandhook.core.HookMethodResolver;
import com.elderdrivers.riru.edxp.sandhook.entry.Router;
import com.elderdrivers.riru.edxp.sandhook.proxy.BlackWhiteListProxy;
import com.elderdrivers.riru.edxp.sandhook.proxy.NormalProxy;
import com.elderdrivers.riru.edxp.util.Utils;
import com.swift.sandhook.xposedcompat.XposedCompat;
import com.swift.sandhook.xposedcompat.methodgen.SandHookXposedBridge;

Expand Down Expand Up @@ -152,5 +150,5 @@ public static void forkSystemServerPost(int pid) {

public static native boolean initXResourcesNative();

public static native void rewriteXmlReferencesNative(long parserPtr, XResources origRes, Resources repRes);
public static native boolean removeFinalFlagNative(Class clazz);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.elderdrivers.riru.edxp.sandhook.config;

import android.content.res.Resources;
import android.content.res.XResources;
import android.util.Log;

import com.elderdrivers.riru.edxp.Main;
Expand Down Expand Up @@ -67,7 +65,7 @@ public boolean initXResourcesNative() {
}

@Override
public void rewriteXmlReferencesNative(long parserPtr, XResources origRes, Resources repRes) {
Main.rewriteXmlReferencesNative(parserPtr, origRes, repRes);
public boolean removeFinalFlagNative(Class clazz) {
return Main.removeFinalFlagNative(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.elderdrivers.riru.edxp.sandhook.entry.bootstrap.WorkAroundHookInfo;
import com.elderdrivers.riru.edxp.sandhook.entry.hooker.SystemMainHooker;
import com.elderdrivers.riru.edxp.util.Utils;
import com.swift.sandhook.SandHookConfig;
import com.swift.sandhook.xposedcompat.XposedCompat;

import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -32,10 +31,14 @@ public class Router {

static boolean useSandHook = false;

public static void initResourcesHook() {
startWorkAroundHook(); // for OnePlus devices
XposedBridge.initXResources();
}

public static void prepare(boolean isSystem) {
// this flag is needed when loadModules
startsSystemServer = isSystem;
// InstallerChooser.setup();
}

public static void checkHookState(String appDataDir) {
Expand Down Expand Up @@ -124,8 +127,15 @@ public static void startWorkAroundHook() {
}
}

public static void onEnterChildProcess() {
public static void onForkStart() {
forkCompleted = false;
}

public static void onForkFinish() {
forkCompleted = true;
}

public static void onEnterChildProcess() {
DynamicBridge.onForkPost();
//enable compile in child process
//SandHook.enableCompiler(!XposedInit.startsSystemServer);
Expand All @@ -144,5 +154,6 @@ public static void logE(String prefix, Throwable throwable) {
public static void injectConfig() {
EdXpConfigGlobal.sConfig = new SandHookEdxpConfig();
EdXpConfigGlobal.sHookProvider = new SandHookProvider();
XposedBridge.log("using HookProvider: " + EdXpConfigGlobal.sHookProvider.getClass().getName());
}
}
Loading

0 comments on commit 71449e2

Please sign in to comment.