-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Patches.java
253 lines (227 loc) · 8.55 KB
/
Patches.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package me.nallar.tickthreading.patcher;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javassist.CannotCompileException;
import javassist.ClassMap;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.Modifier;
import javassist.NotFoundException;
import javassist.bytecode.BadBytecode;
import javassist.expr.Cast;
import javassist.expr.ConstructorCall;
import javassist.expr.ExprEditor;
import javassist.expr.FieldAccess;
import javassist.expr.Handler;
import javassist.expr.Instanceof;
import javassist.expr.MethodCall;
import javassist.expr.NewArray;
import javassist.expr.NewExpr;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.mappings.MethodDescription;
public class Patches {
private final ClassRegistry classRegistry;
public Patches(ClassRegistry classRegistry) {
this.classRegistry = classRegistry;
}
@Patch (
requiredAttributes = "fromClass"
)
public void replaceMethod(CtMethod method, Map<String, String> attributes) throws NotFoundException, CannotCompileException, BadBytecode {
String fromClass = attributes.get("fromClass");
String fromMethod = attributes.get("fromMethod");
CtMethod replacingMethod = fromMethod == null ?
classRegistry.getClass(fromClass).getDeclaredMethod(method.getName(), method.getParameterTypes())
: MethodDescription.fromString(fromClass, fromMethod).inClass(classRegistry.getClass(fromClass));
Log.info("Replacing " + new MethodDescription(method).getMCPName() + " with " + new MethodDescription(replacingMethod).getMCPName());
ClassMap classMap = new ClassMap();
classMap.put(fromClass, method.getDeclaringClass().getName());
method.setBody(replacingMethod, classMap);
method.getMethodInfo().rebuildStackMap(classRegistry.classes);
method.getMethodInfo().rebuildStackMapForME(classRegistry.classes);
}
@Patch (
name = "public"
)
public void makePublic(CtMethod ctMethod) {
ctMethod.setModifiers(Modifier.setPublic(ctMethod.getModifiers()));
}
@Patch (
requiredAttributes = "field,class"
)
public void newInitializer(CtClass ctClass, Map<String, String> attributes) throws NotFoundException, CannotCompileException, IOException {
String field = attributes.get("field");
String clazz = attributes.get("class");
String initialise = "{ " + field + " = new " + clazz + "(); }";
ctClass.getDeclaredField(field);
// Return value ignored - just used to cause a NotFoundException if the field doestn't exist.
for (CtConstructor ctConstructor : ctClass.getConstructors()) {
ctConstructor.insertAfter(initialise);
}
classRegistry.add(ctClass, clazz);
}
@Patch (
requiredAttributes = "field,class"
)
public void newField(CtClass ctClass, Map<String, String> attributes) throws NotFoundException, CannotCompileException, IOException {
String field = attributes.get("field");
String clazz = attributes.get("class");
String initialise = attributes.get("code");
if (initialise == null) {
initialise = "new " + clazz + "();";
}
CtClass newType = classRegistry.getClass(clazz);
CtField ctField = new CtField(newType, field, ctClass);
CtField.Initializer initializer = CtField.Initializer.byExpr(initialise);
ctClass.addField(ctField, initializer);
classRegistry.add(ctClass, clazz);
}
@Patch (
requiredAttributes = "code"
)
public void insertBefore(CtBehavior ctBehavior, Map<String, String> attributes) throws NotFoundException, CannotCompileException, IOException {
String field = attributes.get("field");
String code = attributes.get("code");
if (field != null) {
code = code.replace("$field", field);
}
ctBehavior.insertBefore(code);
}
@Patch (
requiredAttributes = "code"
)
public void insertAfter(CtBehavior ctBehavior, Map<String, String> attributes) throws NotFoundException, CannotCompileException, IOException {
String field = attributes.get("field");
String code = attributes.get("code");
if (field != null) {
code = code.replace("$field", field);
}
ctBehavior.insertAfter(code);
}
@Patch (
requiredAttributes = "field"
)
public void lock(CtMethod ctMethod, Map<String, String> attributes) throws NotFoundException, CannotCompileException, IOException {
String field = attributes.get("field");
CtClass ctClass = ctMethod.getDeclaringClass();
CtMethod replacement = CtNewMethod.copy(ctMethod, ctClass, null);
ctMethod.setName(ctMethod.getName() + "_nolock");
replacement.setBody("{ this." + field + ".lock(); try { return " + ctMethod.getName() + "($$); } finally { this." + field + ".unlock(); } }");
ctClass.addMethod(replacement);
}
@Patch
public void synchronize(CtMethod method) {
int currentModifiers = method.getModifiers();
if (Modifier.isSynchronized(currentModifiers)) {
Log.warning("Method: " + method.getLongName() + " is already synchronized");
} else {
method.setModifiers(currentModifiers | Modifier.SYNCHRONIZED);
}
}
@Patch
public void ignoreExceptions(CtMethod ctMethod, Map<String, String> attributes) throws CannotCompileException, NotFoundException {
String returnCode = attributes.get("code");
if (returnCode == null) {
returnCode = "return;";
}
ctMethod.addCatch("{ " + returnCode + "}", classRegistry.getClass("java.lang.Exception"));
}
@Patch (
requiredAttributes = "code"
)
public void replaceBody(CtMethod ctMethod, Map<String, String> attributes) throws CannotCompileException, BadBytecode {
String body = attributes.get("code");
ctMethod.setBody(body);
}
@Patch
public void replaceInstantiations(Object object, Map<String, String> attributes) {
}
public void replaceInstantiationsImplementation(CtBehavior ctBehavior, final Map<String, List<String>> replacementClasses) throws CannotCompileException {
// TODO: Learn to use ASM, javassist isn't nice for some things. :(
final Map<Integer, String> newExprType = new HashMap<Integer, String>();
ctBehavior.instrument(new ExprEditor() {
NewExpr lastNewExpr;
int newPos = 0;
@Override
public void edit(NewExpr e) {
lastNewExpr = null;
newPos++;
if (replacementClasses.containsKey(e.getClassName())) {
lastNewExpr = e;
}
}
@Override
public void edit(FieldAccess e) {
NewExpr myLastNewExpr = lastNewExpr;
lastNewExpr = null;
if (myLastNewExpr != null) {
Log.fine("(" + myLastNewExpr.getSignature() + ") " + myLastNewExpr.getClassName() + " at " + myLastNewExpr.getFileName() + ":" + myLastNewExpr.getLineNumber() + ":" + newPos);
newExprType.put(newPos, classSignatureToName(e.getSignature()));
}
}
@Override
public void edit(MethodCall e) {
lastNewExpr = null;
}
@Override
public void edit(NewArray e) {
lastNewExpr = null;
}
@Override
public void edit(Cast e) {
lastNewExpr = null;
}
@Override
public void edit(Instanceof e) {
lastNewExpr = null;
}
@Override
public void edit(Handler e) {
lastNewExpr = null;
}
@Override
public void edit(ConstructorCall e) {
lastNewExpr = null;
}
});
ctBehavior.instrument(new ExprEditor() {
int newPos = 0;
@Override
public void edit(NewExpr e) throws CannotCompileException {
newPos++;
try {
Log.fine(e.getFileName() + ":" + e.getLineNumber() + ", pos: " + newPos);
if (newExprType.containsKey(newPos)) {
String replacementType = null, assignedType = newExprType.get(newPos);
Log.fine(assignedType + " at " + e.getFileName() + ":" + e.getLineNumber());
Class<?> assignTo = Class.forName(assignedType);
for (String replacementClass : replacementClasses.get(e.getClassName())) {
if (assignTo.isAssignableFrom(Class.forName(replacementClass))) {
replacementType = replacementClass;
break;
}
}
if (replacementType == null) {
return;
}
String block = "{$_=new " + replacementType + "($$);}";
Log.fine("Replaced with " + block + ", " + replacementType.length() + ":" + assignedType.length());
e.replace(block);
}
} catch (ClassNotFoundException el) {
Log.severe("Could not replace instantiation, class not found.", el);
}
}
});
}
private static String classSignatureToName(String signature) {
//noinspection HardcodedFileSeparator
return signature.substring(1, signature.length() - 1).replace("/", ".");
}
}