Skip to content

Commit dac598e

Browse files
fix hook static method
1 parent e589299 commit dac598e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

java/com/lody/whale/xposed/XposedBridge.java

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public static XC_MethodHook.Unhook hookMethod(final Member hookMethod, final XC_
9898
callbacks.add(callback);
9999

100100
if (newMethod) {
101+
XposedHelpers.resolveStaticMethod(hookMethod);
101102
AdditionalHookInfo additionalInfo = new AdditionalHookInfo(callbacks);
102103
long slot = WhaleRuntime.hookMethodNative(hookMethod.getDeclaringClass(), hookMethod, additionalInfo);
103104
if (slot <= 0) {

java/com/lody/whale/xposed/XposedHelpers.java

+22
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,28 @@ static HashSet<Method> getOverriddenMethods(final Class<?> clazz) {
18371837
return methods;
18381838
}
18391839

1840+
/**
1841+
* the static method is lazy resolved, when not resolved, the entry point is a trampoline of
1842+
* a bridge, we can not hook these entry. this method force the static method to be resolved.
1843+
*/
1844+
public static void resolveStaticMethod(Member method) {
1845+
//ignore result, just call to trigger resolve
1846+
if (method == null)
1847+
return;
1848+
try {
1849+
if (method instanceof Method && Modifier.isStatic(method.getModifiers())) {
1850+
((Method) method).setAccessible(true);
1851+
((Method) method).invoke(new Object(), getFakeArgs((Method) method));
1852+
}
1853+
} catch (Exception ignored) {
1854+
// we should never make a successful call.
1855+
}
1856+
}
1857+
1858+
private static Object[] getFakeArgs(Method method) {
1859+
return method.getParameterTypes().length == 0 ? new Object[]{new Object()} : null;
1860+
}
1861+
18401862
//#################################################################################################
18411863
// TODO helpers for view traversing
18421864
/*To make it easier, I will try and implement some more helpers:

whale/src/android/art/art_runtime.cc

+5-7
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ ArtRuntime::HookMethod(JNIEnv *env, jclass decl_class, jobject hooked_java_metho
191191
ArtMethod hooked_method(hooked_jni_method);
192192
auto *param = new ArtHookParam();
193193

194-
param->origin_compiled_code_ = hooked_method.GetEntryPointFromQuickCompiledCode();
195-
param->origin_code_item_off = hooked_method.GetDexCodeItemOffset();
196-
param->origin_jni_code_ = hooked_method.GetEntryPointFromJni();
197194
param->class_Loader_ = env->NewGlobalRef(
198195
env->CallObjectMethod(
199196
decl_class,
@@ -203,10 +200,11 @@ ArtRuntime::HookMethod(JNIEnv *env, jclass decl_class, jobject hooked_java_metho
203200
param->shorty_ = hooked_method.GetShorty(env, hooked_java_method);
204201
param->is_static_ = hooked_method.HasAccessFlags(kAccStatic);
205202

206-
if (param->is_static_) {
207-
EnsureClassInitialized(env, decl_class);
208-
}
209-
jobject origin_java_method = hooked_method.Clone(env, hooked_method.GetAccessFlags());
203+
param->origin_compiled_code_ = hooked_method.GetEntryPointFromQuickCompiledCode();
204+
param->origin_code_item_off = hooked_method.GetDexCodeItemOffset();
205+
param->origin_jni_code_ = hooked_method.GetEntryPointFromJni();
206+
param->origin_access_flags = hooked_method.GetAccessFlags();
207+
jobject origin_java_method = hooked_method.Clone(env, param->origin_access_flags);
210208

211209
ResolvedSymbols *symbols = GetSymbols();
212210
if (symbols->ProfileSaver_ForceProcessProfiles) {

0 commit comments

Comments
 (0)