2
2
3
3
import com .elderdrivers .riru .edxp .art .Heap ;
4
4
import com .elderdrivers .riru .edxp .core .Yahfa ;
5
- import com .elderdrivers .riru .edxp .util .ClassUtils ;
6
5
import com .elderdrivers .riru .edxp .util .Utils ;
7
6
8
7
import java .lang .reflect .Constructor ;
11
10
import java .lang .reflect .Modifier ;
12
11
import java .util .ArrayList ;
13
12
import java .util .Arrays ;
14
- import java .util .HashSet ;
15
- import java .util .Set ;
16
-
17
- import de .robv .android .xposed .XposedHelpers ;
18
-
19
13
public class HookMain {
20
-
21
- private static final Set <String > hookItemWhiteList = new HashSet <String >();
22
-
23
- public static void addHookItemWhiteList (String className ) {
24
- hookItemWhiteList .add (className );
25
- }
26
-
27
- public static void doHookDefault (ClassLoader patchClassLoader , ClassLoader originClassLoader , String hookInfoClassName ) {
28
- try {
29
- Class <?> hookInfoClass = Class .forName (hookInfoClassName , true , patchClassLoader );
30
- String [] hookItemNames = (String []) hookInfoClass .getField ("hookItemNames" ).get (null );
31
- for (String hookItemName : hookItemNames ) {
32
- doHookItemDefault (patchClassLoader , hookItemName , originClassLoader );
33
- }
34
- } catch (Throwable e ) {
35
- Utils .logE ("error when hooking all in: " + hookInfoClassName , e );
36
- }
37
- }
38
-
39
- private static void doHookItemDefault (ClassLoader patchClassLoader , String hookItemName , ClassLoader originClassLoader ) {
40
- try {
41
- Utils .logD ("Start hooking with item " + hookItemName );
42
- Class <?> hookItem = Class .forName (hookItemName , true , patchClassLoader );
43
-
44
- String className = (String ) hookItem .getField ("className" ).get (null );
45
- String methodName = (String ) hookItem .getField ("methodName" ).get (null );
46
- String methodSig = (String ) hookItem .getField ("methodSig" ).get (null );
47
-
48
- if (className == null || className .equals ("" )) {
49
- Utils .logW ("No target class. Skipping..." );
50
- return ;
51
- }
52
- Class <?> clazz = null ;
53
- try {
54
- clazz = Class .forName (className , true , originClassLoader );
55
- } catch (ClassNotFoundException cnfe ) {
56
- Utils .logE (className + " not found in " + originClassLoader );
57
- return ;
58
- }
59
- if (Modifier .isAbstract (clazz .getModifiers ())) {
60
- Utils .logW ("Hook may fail for abstract class: " + className );
61
- }
62
-
63
- Method hook = null ;
64
- Method backup = null ;
65
- for (Method method : hookItem .getDeclaredMethods ()) {
66
- if (method .getName ().equals ("hook" ) && Modifier .isStatic (method .getModifiers ())) {
67
- hook = method ;
68
- } else if (method .getName ().equals ("backup" ) && Modifier .isStatic (method .getModifiers ())) {
69
- backup = method ;
70
- }
71
- }
72
- if (hook == null ) {
73
- Utils .logE ("Cannot find hook for " + methodName );
74
- return ;
75
- }
76
- findAndBackupAndHook (clazz , methodName , methodSig , hook , backup );
77
- } catch (Throwable e ) {
78
- if (!hookItemWhiteList .contains (hookItemName )) {
79
- Utils .logE ("error when hooking " + hookItemName , e );
80
- }
81
- }
82
- }
83
-
84
- public static void findAndHook (Class targetClass , String methodName , String methodSig , Method hook ) {
85
- hook (findMethod (targetClass , methodName , methodSig ), hook );
86
- }
87
-
88
- public static void findAndBackupAndHook (Class targetClass , String methodName , String methodSig ,
89
- Method hook , Method backup ) {
90
- backupAndHook (findMethod (targetClass , methodName , methodSig ), hook , backup );
91
- }
92
-
93
- public static void hook (Member target , Method hook ) {
94
- backupAndHook (target , hook , null );
95
- }
96
-
97
14
public static void backupAndHook (Member target , Method hook , Method backup ) {
98
15
Utils .logD (String .format ("target=%s, hook=%s, backup=%s" , target , hook , backup ));
99
16
if (target == null ) {
@@ -106,13 +23,13 @@ public static void backupAndHook(Member target, Method hook, Method backup) {
106
23
if (!Modifier .isStatic (hook .getModifiers ())) {
107
24
throw new IllegalArgumentException ("Hook must be a static method: " + hook );
108
25
}
109
- checkCompatibleMethods (target , hook , "Original" , " Hook" );
26
+ checkCompatibleMethods (target , hook , "Hook" );
110
27
if (backup != null ) {
111
28
if (!Modifier .isStatic (backup .getModifiers ())) {
112
29
throw new IllegalArgumentException ("Backup must be a static method: " + backup );
113
30
}
114
31
// backup is just a placeholder and the constraint could be less strict
115
- checkCompatibleMethods (target , backup , "Original" , " Backup" );
32
+ checkCompatibleMethods (target , backup , "Backup" );
116
33
}
117
34
// make sure GC completed before hook
118
35
int lastGcType = Heap .waitForGcToComplete ();
@@ -121,28 +38,15 @@ public static void backupAndHook(Member target, Method hook, Method backup) {
121
38
Runtime .getRuntime ().gc ();
122
39
}
123
40
124
- if (!Yahfa .backupAndHookNative (target , hook , backup )) {
41
+ if (!Yahfa .backupAndHookNative (target , hook , backup )){
125
42
throw new RuntimeException ("Failed to hook " + target + " with " + hook );
126
43
} else {
127
44
Yahfa .recordHooked (target );
128
- Yahfa .recordHooked (backup );
129
- }
130
- }
131
-
132
- public static Member findMethod (Class cls , String methodName , String methodSig ) {
133
- if (cls == null ) {
134
- throw new IllegalArgumentException ("null class" );
135
- }
136
- if (methodName == null ) {
137
- throw new IllegalArgumentException ("null method name" );
138
- }
139
- if (methodSig == null ) {
140
- throw new IllegalArgumentException ("null method signature" );
45
+ // Yahfa.recordHooked(backup);
141
46
}
142
- return Yahfa .findMethodNative (cls , methodName , methodSig );
143
47
}
144
48
145
- private static void checkCompatibleMethods (Object original , Method replacement , String originalName , String replacementName ) {
49
+ private static void checkCompatibleMethods (Object original , Method replacement , String replacementName ) {
146
50
ArrayList <Class <?>> originalParams ;
147
51
if (original instanceof Method ) {
148
52
originalParams = new ArrayList <>(Arrays .asList (((Method ) original ).getParameterTypes ()));
@@ -168,20 +72,20 @@ private static void checkCompatibleMethods(Object original, Method replacement,
168
72
169
73
if (original instanceof Method
170
74
&& !replacement .getReturnType ().isAssignableFrom (((Method ) original ).getReturnType ())) {
171
- throw new IllegalArgumentException ("Incompatible return types. " + originalName + ": " + ((Method ) original ).getReturnType () + ", " + replacementName + ": " + replacement .getReturnType ());
75
+ throw new IllegalArgumentException ("Incompatible return types. " + "Original" + ": " + ((Method ) original ).getReturnType () + ", " + replacementName + ": " + replacement .getReturnType ());
172
76
} else if (original instanceof Constructor ) {
173
77
if (replacement .getReturnType ().equals (Void .class )) {
174
78
throw new IllegalArgumentException ("Incompatible return types. " + "<init>" + ": " + "V" + ", " + replacementName + ": " + replacement .getReturnType ());
175
79
}
176
80
}
177
81
178
82
if (originalParams .size () != replacementParams .size ()) {
179
- throw new IllegalArgumentException ("Number of arguments don't match. " + originalName + ": " + originalParams .size () + ", " + replacementName + ": " + replacementParams .size ());
83
+ throw new IllegalArgumentException ("Number of arguments don't match. " + "Original" + ": " + originalParams .size () + ", " + replacementName + ": " + replacementParams .size ());
180
84
}
181
85
182
86
for (int i = 0 ; i < originalParams .size (); i ++) {
183
87
if (!replacementParams .get (i ).isAssignableFrom (originalParams .get (i ))) {
184
- throw new IllegalArgumentException ("Incompatible argument #" + i + ": " + originalName + ": " + originalParams .get (i ) + ", " + replacementName + ": " + replacementParams .get (i ));
88
+ throw new IllegalArgumentException ("Incompatible argument #" + i + ": " + "Original" + ": " + originalParams .get (i ) + ", " + replacementName + ": " + replacementParams .get (i ));
185
89
}
186
90
}
187
91
}
0 commit comments