forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMethodBuilder.java
More file actions
345 lines (311 loc) · 11.3 KB
/
MethodBuilder.java
File metadata and controls
345 lines (311 loc) · 11.3 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
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
package soot.asm;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2014 Raja Vallee-Rai and others
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import com.google.common.base.Optional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.TypePath;
import org.objectweb.asm.commons.JSRInlinerAdapter;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.LocalVariableNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import soot.ArrayType;
import soot.MethodSource;
import soot.RefType;
import soot.SootMethod;
import soot.Type;
import soot.tagkit.AnnotationConstants;
import soot.tagkit.AnnotationDefaultTag;
import soot.tagkit.AnnotationTag;
import soot.tagkit.ParamNamesTag;
import soot.tagkit.VisibilityAnnotationTag;
import soot.tagkit.VisibilityLocalVariableAnnotationTag;
import soot.tagkit.VisibilityParameterAnnotationTag;
/**
* Soot method builder.
*
* @author Aaloan Miftah
*/
public class MethodBuilder extends JSRInlinerAdapter {
private TagBuilder tb;
private VisibilityAnnotationTag[] visibleParamAnnotations;
private VisibilityAnnotationTag[] invisibleParamAnnotations;
private List<VisibilityAnnotationTag> visibleLocalVarAnnotations;
private List<VisibilityAnnotationTag> invisibleLocalVarAnnotations;
private final SootMethod method;
private final SootClassBuilder scb;
private final String[] parameterNames;
private final Map<Integer, Integer> slotToParameter;
public MethodBuilder(SootMethod method, SootClassBuilder scb, String desc, String[] ex) {
super(Opcodes.ASM6, null, method.getModifiers(), method.getName(), desc, null, ex);
this.method = method;
this.scb = scb;
this.parameterNames = new String[method.getParameterCount()];
this.slotToParameter = createSlotToParameterMap();
}
private Map<Integer, Integer> createSlotToParameterMap() {
final int paramCount = method.getParameterCount();
Map<Integer, Integer> slotMap = new HashMap<>(paramCount);
int curSlot = method.isStatic() ? 0 : 1;
for (int i = 0; i < paramCount; i++) {
slotMap.put(curSlot, i);
curSlot++;
if (AsmUtil.isDWord(method.getParameterType(i))) {
curSlot++;
}
}
return slotMap;
}
private TagBuilder getTagBuilder() {
TagBuilder t = tb;
if (t == null) {
t = tb = new TagBuilder(method, scb);
}
return t;
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return getTagBuilder().visitAnnotation(desc, visible);
}
@Override
public AnnotationVisitor visitAnnotationDefault() {
return new AnnotationElemBuilder(1) {
@Override
public void visitEnd() {
method.addTag(new AnnotationDefaultTag(elems.get(0)));
}
};
}
@Override
public void visitAttribute(Attribute attr) {
getTagBuilder().visitAttribute(attr);
}
@Override
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
super.visitLocalVariable(name, descriptor, signature, start, end, index);
if (name != null && !name.isEmpty() && index > 0) {
Integer paramIdx = slotToParameter.get(index);
if (paramIdx != null) {
parameterNames[paramIdx] = name;
}
}
}
@Override
public AnnotationVisitor visitLocalVariableAnnotation(final int typeRef, final TypePath typePath, final Label[] start,
final Label[] end, final int[] index, final String descriptor, final boolean visible) {
final VisibilityAnnotationTag vat
= new VisibilityAnnotationTag(visible ? AnnotationConstants.RUNTIME_VISIBLE : AnnotationConstants.RUNTIME_INVISIBLE);
if (visible) {
if (visibleLocalVarAnnotations == null) {
visibleLocalVarAnnotations = new ArrayList<VisibilityAnnotationTag>(2);
}
visibleLocalVarAnnotations.add(vat);
} else {
if (invisibleLocalVarAnnotations == null) {
invisibleLocalVarAnnotations = new ArrayList<VisibilityAnnotationTag>(2);
}
invisibleLocalVarAnnotations.add(vat);
}
return new AnnotationElemBuilder() {
@Override
public void visitEnd() {
AnnotationTag annotTag = new AnnotationTag(desc, elems);
vat.addAnnotation(annotTag);
}
};
}
@Override
public AnnotationVisitor visitParameterAnnotation(int parameter, final String desc, boolean visible) {
VisibilityAnnotationTag vat;
VisibilityAnnotationTag[] vats;
if (visible) {
vats = visibleParamAnnotations;
if (vats == null) {
vats = new VisibilityAnnotationTag[method.getParameterCount()];
visibleParamAnnotations = vats;
}
vat = vats[parameter];
if (vat == null) {
vat = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE);
vats[parameter] = vat;
}
} else {
vats = invisibleParamAnnotations;
if (vats == null) {
vats = new VisibilityAnnotationTag[method.getParameterCount()];
invisibleParamAnnotations = vats;
}
vat = vats[parameter];
if (vat == null) {
vat = new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_INVISIBLE);
vats[parameter] = vat;
}
}
final VisibilityAnnotationTag _vat = vat;
return new AnnotationElemBuilder() {
@Override
public void visitEnd() {
AnnotationTag annotTag = new AnnotationTag(desc, elems);
_vat.addAnnotation(annotTag);
}
};
}
@Override
public void visitTypeInsn(int op, String t) {
super.visitTypeInsn(op, t);
Type rt = AsmUtil.toJimpleRefType(t, Optional.fromNullable(this.scb.getKlass().moduleName));
if (rt instanceof ArrayType) {
scb.addDep(((ArrayType) rt).baseType);
} else {
scb.addDep(rt);
}
}
@Override
public void visitFieldInsn(int opcode, String owner, String name, String desc) {
super.visitFieldInsn(opcode, owner, name, desc);
for (Type t : AsmUtil.toJimpleDesc(desc, Optional.fromNullable(this.scb.getKlass().moduleName))) {
if (t instanceof RefType) {
scb.addDep(t);
}
}
scb.addDep(AsmUtil.toQualifiedName(owner));
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean isInterf) {
super.visitMethodInsn(opcode, owner, name, desc, isInterf);
for (Type t : AsmUtil.toJimpleDesc(desc, Optional.fromNullable(this.scb.getKlass().moduleName))) {
addDeps(t);
}
scb.addDep(AsmUtil.toJimpleRefType(owner, Optional.fromNullable(this.scb.getKlass().moduleName)));
}
@Override
public void visitLdcInsn(Object cst) {
super.visitLdcInsn(cst);
if (cst instanceof Handle) {
Handle methodHandle = (Handle) cst;
scb.addDep(AsmUtil.toBaseType(methodHandle.getOwner(), Optional.fromNullable(this.scb.getKlass().moduleName)));
}
}
private void addDeps(Type t) {
if (t instanceof RefType) {
scb.addDep(t);
} else if (t instanceof ArrayType) {
ArrayType at = (ArrayType) t;
addDeps(at.getElementType());
}
}
@Override
public void visitTryCatchBlock(Label start, Label end, Label handler, String type) {
super.visitTryCatchBlock(start, end, handler, type);
if (type != null) {
scb.addDep(AsmUtil.toQualifiedName(type));
}
}
@Override
public void visitEnd() {
super.visitEnd();
if (visibleParamAnnotations != null) {
VisibilityParameterAnnotationTag tag
= new VisibilityParameterAnnotationTag(visibleParamAnnotations.length, AnnotationConstants.RUNTIME_VISIBLE);
for (VisibilityAnnotationTag vat : visibleParamAnnotations) {
tag.addVisibilityAnnotation(vat);
}
method.addTag(tag);
}
if (invisibleParamAnnotations != null) {
VisibilityParameterAnnotationTag tag
= new VisibilityParameterAnnotationTag(invisibleParamAnnotations.length, AnnotationConstants.RUNTIME_INVISIBLE);
for (VisibilityAnnotationTag vat : invisibleParamAnnotations) {
tag.addVisibilityAnnotation(vat);
}
method.addTag(tag);
}
if (visibleLocalVarAnnotations != null) {
VisibilityLocalVariableAnnotationTag tag
= new VisibilityLocalVariableAnnotationTag(visibleLocalVarAnnotations.size(), AnnotationConstants.RUNTIME_VISIBLE);
for (VisibilityAnnotationTag vat : visibleLocalVarAnnotations) {
tag.addVisibilityAnnotation(vat);
}
method.addTag(tag);
}
if (invisibleLocalVarAnnotations != null) {
VisibilityLocalVariableAnnotationTag tag = new VisibilityLocalVariableAnnotationTag(
invisibleLocalVarAnnotations.size(), AnnotationConstants.RUNTIME_INVISIBLE);
for (VisibilityAnnotationTag vat : invisibleLocalVarAnnotations) {
tag.addVisibilityAnnotation(vat);
}
method.addTag(tag);
}
if (!isFullyEmpty(parameterNames)) {
method.addTag(new ParamNamesTag(parameterNames));
}
if (method.isConcrete()) {
method.setSource(
createAsmMethodSource(maxLocals, instructions, localVariables, tryCatchBlocks, scb.getKlass().moduleName));
}
}
protected MethodSource createAsmMethodSource(int maxLocals, InsnList instructions, List<LocalVariableNode> localVariables,
List<TryCatchBlockNode> tryCatchBlocks, String moduleName) {
return new AsmMethodSource(maxLocals, instructions, localVariables, tryCatchBlocks, moduleName);
}
/**
* Gets whether the given array is fully empty, i.e., contains only <code>null</code> values
*
* @param array
* The array to check
* @return True if the given arry contains only <code>null</code> values, false otherwise
*/
private boolean isFullyEmpty(String[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] != null && !array[i].isEmpty()) {
return false;
}
}
return true;
}
@Override
public String toString() {
return method.toString();
}
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle,
Object... bootstrapMethodArguments) {
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
// convert info on bootstrap method
String bsmClsName = AsmUtil.toQualifiedName(bootstrapMethodHandle.getOwner());
scb.addDep(RefType.v(bsmClsName));
for (Object arg : bootstrapMethodArguments) {
if (arg instanceof Handle) {
Handle argHandle = (Handle) arg;
String handleClsName = AsmUtil.toQualifiedName(argHandle.getOwner());
scb.addDep(RefType.v(handleClsName));
}
}
}
}