-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Patches.java
306 lines (278 loc) · 10.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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;
@SuppressWarnings ("MethodMayBeStatic")
public class Patches {
private final ClassRegistry classRegistry;
public Patches(ClassRegistry classRegistry) {
this.classRegistry = classRegistry;
}
@Patch (
name = "volatile"
)
public void volatile_(CtClass ctClass, Map<String, String> attributes) throws NotFoundException {
String field = attributes.get("field");
if (field == null) {
for (CtField ctField : ctClass.getDeclaredFields()) {
if (ctField.getType().isPrimitive()) {
ctField.setModifiers(ctField.getModifiers() | Modifier.VOLATILE);
}
}
} else {
CtField ctField = ctClass.getDeclaredField(field);
ctField.setModifiers(ctField.getModifiers() | Modifier.VOLATILE);
}
}
@Patch (
requiredAttributes = "class"
)
public CtClass replace(CtClass clazz, Map<String, String> attributes) throws NotFoundException, CannotCompileException {
Log.info("Replacing " + clazz.getName() + " with " + attributes.get("class"));
String oldName = clazz.getName();
clazz.setName(oldName + "_old");
CtClass newClass = classRegistry.getClass(attributes.get("class"));
newClass.getClassFile2().setSuperclass(null);
newClass.setName(oldName);
return newClass;
}
@Patch
public void replaceMethod(CtMethod method, Map<String, String> attributes) throws NotFoundException, CannotCompileException, BadBytecode {
String fromClass = attributes.get("fromClass");
String code = attributes.get("code");
if (fromClass != null) {
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);
} else if (code != null) {
Log.info("Replacing " + new MethodDescription(method).getMCPName() + " with " + code);
method.setBody(code);
} else {
Log.severe("Missing required attributes for replaceMethod");
}
}
@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 + "(); }";
// Return value ignored - just used to cause a NotFoundException if the field doesn't exist.
ctClass.getDeclaredField(field);
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);
if (attributes.get("static") != null) {
ctField.setModifiers(ctField.getModifiers() | Modifier.STATIC | Modifier.PUBLIC);
}
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 $proceed($$); } finally { this." + field + ".unlock(); } }", "this", ctMethod.getName());
ctClass.addMethod(replacement);
}
@Patch(
emptyConstructor = false
)
public void synchronize(Object o, Map<String, String> attributes) throws CannotCompileException {
if (o instanceof CtMethod) {
synchronize((CtMethod) o, attributes.get("field"));
} else {
for (CtMethod ctMethod : ((CtClass) o).getDeclaredMethods()) {
synchronize(ctMethod, attributes.get("field"));
}
}
}
private void synchronize(CtMethod ctMethod, String field) throws CannotCompileException {
if (field == null) {
int currentModifiers = ctMethod.getModifiers();
if (Modifier.isSynchronized(currentModifiers)) {
Log.warning("Method: " + ctMethod.getLongName() + " is already synchronized");
} else {
ctMethod.setModifiers(currentModifiers | Modifier.SYNCHRONIZED);
}
} else {
CtClass ctClass = ctMethod.getDeclaringClass();
CtMethod replacement = CtNewMethod.copy(ctMethod, ctClass, null);
ctMethod.setName(ctMethod.getName() + "_nosynchronize");
replacement.setBody("synchronized(this." + field + ") { return $proceed($$); }", "this", ctMethod.getName());
ctClass.addMethod(replacement);
}
}
@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
public void replaceInstantiations(CtBehavior object, Map<String, String> attributes) {
// TODO: Implement this, change some newInitializer patches to use this
}
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("/", ".");
}
}