-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Patches.java
397 lines (363 loc) · 14.2 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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
public void markDirty(CtClass ctClass) {
// A NOOP patch to make sure META-INF is removed
}
@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));
replaceMethod(method, replacingMethod);
} else if (code != null) {
Log.info("Replacing " + new MethodDescription(method).getMCPName() + " with " + code);
method.setBody(code);
} else {
Log.severe("Missing required attributes for replaceMethod");
}
}
private void replaceMethod(CtMethod oldMethod, CtMethod newMethod) throws CannotCompileException, BadBytecode {
Log.info("Replacing " + new MethodDescription(oldMethod).getMCPName() + " with " + new MethodDescription(newMethod).getMCPName());
ClassMap classMap = new ClassMap();
classMap.put(newMethod.getClass().getName(), oldMethod.getDeclaringClass().getName());
oldMethod.setBody(newMethod, classMap);
oldMethod.getMethodInfo().rebuildStackMap(classRegistry.classes);
oldMethod.getMethodInfo().rebuildStackMapForME(classRegistry.classes);
}
@Patch (
requiredAttributes = "field"
)
public void replaceFieldUsage(CtBehavior ctBehavior, Map<String, String> attributes) throws CannotCompileException {
final String field = attributes.get("field");
final String readCode = attributes.get("readCode");
final String writeCode = attributes.get("writeCode");
if (readCode == null && writeCode == null) {
throw new IllegalArgumentException("readCode or writeCode must be set");
}
Log.info("Replacing field usages in " + ctBehavior.getName() + " write: " + writeCode
+ "\nread: " + readCode);
ctBehavior.instrument(new ExprEditor() {
@Override
public void edit(FieldAccess fieldAccess) throws CannotCompileException {
if (fieldAccess.getFieldName().equals(field)) {
if (fieldAccess.isWriter() && writeCode != null) {
fieldAccess.replace(writeCode);
} else if (fieldAccess.isReader() && readCode != null) {
fieldAccess.replace(readCode);
}
}
}
});
}
@Patch (
requiredAttributes = "fromClass"
)
public void addAll(CtClass ctClass, Map<String, String> attributes) throws NotFoundException, CannotCompileException, BadBytecode {
String fromClass = attributes.get("fromClass");
CtClass from = classRegistry.getClass(fromClass);
ClassMap classMap = new ClassMap();
classMap.put(fromClass, ctClass.getName());
for (CtField ctField : from.getDeclaredFields()) {
Log.info("Added " + ctField);
ctClass.addField(new CtField(ctField, ctClass));
}
for (CtMethod newMethod : from.getDeclaredMethods()) {
Log.info("Replaced " + newMethod.getName());
try {
CtMethod oldMethod = ctClass.getDeclaredMethod(newMethod.getName(), newMethod.getParameterTypes());
replaceMethod(oldMethod, newMethod);
} catch (NotFoundException ignored) {
ctClass.addMethod(CtNewMethod.copy(newMethod, ctClass, classMap));
}
}
}
@Patch (
requiredAttributes = "field,threadLocalField,type"
)
public void threadLocal(CtClass ctClass, Map<String, String> attributes) throws CannotCompileException {
final String field = attributes.get("field");
final String threadLocalField = attributes.get("threadLocalField");
final String type = attributes.get("type");
String setExpression_ = attributes.get("setExpression");
final String setExpression = setExpression_ == null ? '(' + type + ") $1" : setExpression_;
Log.info(field + " -> " + threadLocalField);
ctClass.instrument(new ExprEditor() {
@Override
public void edit(FieldAccess e) throws CannotCompileException {
if (e.getFieldName().equals(field)) {
if (e.isReader()) {
e.replace("{ $_ = (" + type + ") " + threadLocalField + ".get(); }");
} else if (e.isWriter()) {
e.replace("{ " + threadLocalField + ".set(" + setExpression + "); }");
}
}
}
});
}
@Patch (
name = "public"
)
public void makePublic(CtMethod ctMethod) {
ctMethod.setModifiers(Modifier.setPublic(ctMethod.getModifiers()));
}
@Patch (
requiredAttributes = "field"
)
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 = attributes.get("code");
String arraySize = attributes.get("arraySize");
initialise = "{ " + field + " = " + (initialise == null ? ("new " + clazz + (arraySize == null ? "()" : '[' + arraySize + ']')) : initialise) + "; }";
// 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);
}
if (clazz != null) {
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
public void insertSuper(CtBehavior ctBehavior) throws CannotCompileException {
ctBehavior.insertBefore("super." + ctBehavior.getName() + "($$);");
}
@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 " + (attributes.get("methodcall") == null ? "$proceed" : ctMethod.getName()) + "($$); } 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("/", ".");
}
}