EventInjectionTransformer.java
9.36 KB
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
package com.mumfrey.liteloader.transformers.event;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.util.CheckClassAdapter;
import com.mumfrey.liteloader.core.runtime.Obf;
import com.mumfrey.liteloader.transformers.ClassTransformer;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* EventInjectionTransformer is the spiritual successor to the CallbackInjectionTransformer and is a more advanced
* and flexible version of the same premise. Like the CallbackInjectionTransformer, it can be used to inject callbacks
* intelligently into a target method, however it has the following additional capabilities which make it more flexible
* and scalable:
*
* + Injections are not restricted to RETURN opcodes or profiler invokations, each injection is determined by
* supplying an InjectionPoint instance to the {@code addEvent} method which is used to find the injection
* point(s) in the method
*
* + Injected events can optionally be specified as *cancellable* which allows method execution to be pre-emptively
* halted based on the cancellation status of the event. For methods with a return value, the return value may
* be specified by the event handler.
*
* + Injected events call back against a dynamically-generated proxy class, this means that it is no longer necessary
* to provide your own implementation of a static callback proxy, events can call back directly against handler
* methods in your own codebase.
*
* + Event injections are more intelligent about injecting at arbitrary points in the bytecode without corrupting the
* local stack, and increase MAXS as required.
*
* + Event injections do not "collide" like callback injections do - this means that if multiple events are injected
* by multiple sources at the same point in the bytecode, then all event handlers will receive and handle the event
* in one go. To provide for this, each event handler is defined with an intrinsic "priority" which determines its
* call order when this situation occurs
*
* @author Adam Mummery-Smith
*/
public abstract class EventInjectionTransformer extends ClassTransformer
{
/**
* Multidimensional map of class names -> target method signatures -> events to inject
*/
private static Map<String, Map<String, Map<Event, InjectionPoint>>> eventMappings = new HashMap<String, Map<String, Map<Event, InjectionPoint>>>();
/**
* Multiple event injection transformers may exist but to allow co-operation the events themselves are registered
* statically. The first EventInjectionTransformer instance to be created becomes the "master" and is actually responsible
* for injecting the events and transforming the EventProxy class.
*/
private static EventInjectionTransformer master;
/**
* Runs the validator on the generated classes, only for debugging purposes
*/
private final boolean runValidator = false;
private int globalEventID = 0;
public EventInjectionTransformer()
{
if (EventInjectionTransformer.master == null)
{
EventInjectionTransformer.master = this;
}
this.addEvents();
}
/**
* Subclasses should register events here
*/
protected abstract void addEvents();
/**
* Register a new event to be injected, the event instance will be created if it does not already exist
*
* @param eventName Name of the event to use/create. Beware that IllegalArgumentException if the event was already defined with incompatible parameters
* @param targetMethod Method descriptor to identify the method to inject into
* @param injectionPoint Delegate which finds the location(s) in the target method to inject into
*
* @return the event - for fluent interface
*/
protected final Event addEvent(String eventName, MethodInfo targetMethod, InjectionPoint injectionPoint)
{
return this.addEvent(Event.getOrCreate(eventName), targetMethod, injectionPoint);
}
/**
* Register an event to be injected
*
* @param event Event to inject
* @param targetMethod Method descriptor to identify the method to inject into
* @param injectionPoint Delegate which finds the location(s) in the target method to inject into
*
* @return the event - for fluent interface
*/
protected final Event addEvent(Event event, MethodInfo targetMethod, InjectionPoint injectionPoint)
{
if (event == null)
throw new IllegalArgumentException("Event cannot be null!");
if (injectionPoint == null)
throw new IllegalArgumentException("Injection point cannot be null for event " + event.getName());
this.addEvent(event, targetMethod.owner, targetMethod.sig, injectionPoint);
this.addEvent(event, targetMethod.owner, targetMethod.sigSrg, injectionPoint);
this.addEvent(event, targetMethod.ownerObf, targetMethod.sigObf, injectionPoint);
return event;
}
private void addEvent(Event event, String className, String signature, InjectionPoint injectionPoint)
{
Map<String, Map<Event, InjectionPoint>> mappings = EventInjectionTransformer.eventMappings.get(className);
if (mappings == null)
{
mappings = new HashMap<String, Map<Event, InjectionPoint>>();
EventInjectionTransformer.eventMappings.put(className, mappings);
}
Map<Event, InjectionPoint> events = mappings.get(signature);
if (events == null)
{
events = new LinkedHashMap<Event, InjectionPoint>();
mappings.put(signature, events);
}
events.put(event, injectionPoint);
}
@Override
public final byte[] transform(String name, String transformedName, byte[] basicClass)
{
if (EventInjectionTransformer.master == this)
{
if (Obf.EventProxy.name.equals(transformedName))
{
return this.transformEventProxy(basicClass);
}
if (basicClass != null && EventInjectionTransformer.eventMappings.containsKey(transformedName))
{
return this.injectEvents(basicClass, EventInjectionTransformer.eventMappings.get(transformedName));
}
}
return basicClass;
}
private byte[] transformEventProxy(byte[] basicClass)
{
ClassNode classNode = this.readClass(basicClass, true);
for (MethodNode method : classNode.methods)
{
// Strip the sanity code out of the EventProxy class initialiser
if ("<clinit>".equals(method.name))
{
method.instructions.clear();
method.instructions.add(new InsnNode(Opcodes.RETURN));
}
}
return this.writeClass(Event.populateProxy(classNode));
}
private byte[] injectEvents(byte[] basicClass, Map<String, Map<Event, InjectionPoint>> mappings)
{
if (mappings == null) return basicClass;
ClassNode classNode = this.readClass(basicClass, true);
for (MethodNode method : classNode.methods)
{
String signature = MethodInfo.generateSignature(method.name, method.desc);
Map<Event, InjectionPoint> methodInjections = mappings.get(signature);
if (methodInjections != null)
{
ReadOnlyInsnList insns = new ReadOnlyInsnList(method.instructions);
Map<AbstractInsnNode, Set<Event>> injectionPoints = new LinkedHashMap<AbstractInsnNode, Set<Event>>();
Collection<AbstractInsnNode> nodes = new ArrayList<AbstractInsnNode>(32);
for (Entry<Event, InjectionPoint> eventEntry : methodInjections.entrySet())
{
Event event = eventEntry.getKey();
event.attach(method);
InjectionPoint injectionPoint = eventEntry.getValue();
nodes.clear();
if (injectionPoint.find(method.desc, insns, nodes, event))
{
for (AbstractInsnNode node : nodes)
{
Set<Event> nodeEvents = injectionPoints.get(node);
if (nodeEvents == null)
{
nodeEvents = new TreeSet<Event>();
injectionPoints.put(node, nodeEvents);
}
nodeEvents.add(event);
}
}
}
for (Entry<AbstractInsnNode, Set<Event>> injectionPoint : injectionPoints.entrySet())
{
AbstractInsnNode insn = injectionPoint.getKey();
Set<Event> events = injectionPoint.getValue();
// Injection is cancellable if ANY of the events on this insn are cancellable
boolean cancellable = false;
for (Event event : events)
cancellable |= event.isCancellable();
Event head = events.iterator().next();
MethodNode handler = head.inject(insn, cancellable, this.globalEventID);
LiteLoaderLogger.info("Injecting event %s with %d handlers in method %s in class %s", head.getName(), events.size(), method.name, classNode.name.replace('/', '.'));
for (Event event : events)
event.addToHandler(handler);
this.globalEventID++;
}
for (Event event : methodInjections.keySet())
{
event.detach();
}
}
}
if (true || this.runValidator)
{
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(new CheckClassAdapter(writer));
}
return this.writeClass(classNode);
}
}