forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssemblyFile.java
More file actions
388 lines (348 loc) · 13.8 KB
/
AssemblyFile.java
File metadata and controls
388 lines (348 loc) · 13.8 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
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
package soot.dotnet;
import com.google.common.base.Strings;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2022 Fraunhofer SIT
* %%
* 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 java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.dotnet.members.DotnetEvent;
import soot.dotnet.proto.ProtoAssemblyAllTypes;
import soot.dotnet.proto.ProtoDotnetNativeHost;
import soot.dotnet.proto.ProtoIlInstructions;
import soot.options.Options;
import soot.toolkits.scalar.Pair;
/**
* Represents an Assembly File
*
* @author Thomas Schmeiduch
*/
public class AssemblyFile extends File {
private static final Logger logger = LoggerFactory.getLogger(AssemblyFile.class);
/**
* Constructs a new AssemblyFile with the path to the file
*
* @param fullyQualifiedAssemblyPathFilename
* e.g. /home/user/cs/myassembly.dll
*/
public AssemblyFile(String fullyQualifiedAssemblyPathFilename) {
super(fullyQualifiedAssemblyPathFilename);
this.fullyQualifiedAssemblyPathFilename = fullyQualifiedAssemblyPathFilename;
this.pathNativeHost = Options.v().dotnet_nativehost_path();
// load JNI library
System.load(this.pathNativeHost);
}
/**
* e.g. /home/user/cs/myassembly.dll
*/
private final String fullyQualifiedAssemblyPathFilename;
/**
* all types of this assembly file
*/
private ProtoAssemblyAllTypes.AssemblyAllTypes protoAllTypes;
/**
* e.g. "/Users/user/Soot.Dotnet.NativeHost/bin/Debug/libNativeHost.dylib"
*/
private final String pathNativeHost;
/**
* Store state if all references of this assembly were requested. Is needed not to add basic classes to scene twice.
*/
private boolean gotAllReferencesModuleTypes = false;
/**
* Get all Types of this assembly
*
* @return proto message with all types of this assembly
*/
public ProtoAssemblyAllTypes.AssemblyAllTypes getAllTypes() {
if (protoAllTypes != null) {
return protoAllTypes;
}
try {
ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder analyzerParamsBuilder
= createAnalyzerParamsBuilder("", ProtoDotnetNativeHost.AnalyzerMethodCall.GET_ALL_TYPES);
ProtoDotnetNativeHost.AnalyzerParamsMsg analyzerParamsMsg = analyzerParamsBuilder.build();
byte[] protobufMessageBytes = nativeGetAllTypesMsg(pathNativeHost, analyzerParamsMsg.toByteArray());
ProtoAssemblyAllTypes.AssemblyAllTypes a = ProtoAssemblyAllTypes.AssemblyAllTypes.parseFrom(protobufMessageBytes);
protoAllTypes = a;
return a;
} catch (Exception e) {
if (Options.v().verbose()) {
logger.warn(getAssemblyFileName() + " has no types. Error of protobuf message: " + e.getMessage());
}
return null;
}
}
/**
* Get Method Body with IL Instructions
*
* @param className
* given class
* @param method
* given method name
* @return list/tree of il instructions otherwise null
*/
public ProtoIlInstructions.IlFunctionMsg getMethodBody(String className, String method, int peToken) {
ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder analyzerParamsBuilder
= createAnalyzerParamsBuilder(className, ProtoDotnetNativeHost.AnalyzerMethodCall.GET_METHOD_BODY);
Pair<String, String> methodNameSuffixPair = helperExtractMethodNameSuffix(method);
analyzerParamsBuilder.setMethodName(methodNameSuffixPair.getO1());
analyzerParamsBuilder.setMethodNameSuffix(methodNameSuffixPair.getO2());
analyzerParamsBuilder.setMethodPeToken(peToken);
ProtoDotnetNativeHost.AnalyzerParamsMsg analyzerParamsMsg = analyzerParamsBuilder.build();
try {
byte[] protoMsgBytes = nativeGetMethodBodyMsg(pathNativeHost, analyzerParamsMsg.toByteArray());
return ProtoIlInstructions.IlFunctionMsg.parseFrom(protoMsgBytes);
} catch (Exception e) {
if (Options.v().verbose()) {
logger.warn("Exception while getting method body of method " + className + "." + method + ": " + e.getMessage());
}
return null;
}
}
private Pair<String, String> helperExtractMethodNameSuffix(String sootMethodName) {
// if name mangling, extract suffix (due to cil and java bytecode differences)
if (!(sootMethodName.contains("[[") && sootMethodName.contains("]]"))) {
return new Pair<>(sootMethodName, "");
}
int startSuffix = sootMethodName.indexOf("[[");
String suffix = sootMethodName.substring(startSuffix);
String cilMethodName = sootMethodName.substring(0, startSuffix);
return new Pair<>(cilMethodName, suffix);
}
/**
* Get Method Body of property methods
*
* @param className
* declaring class
* @param propertyName
* name of property
* @param isSetter
* request setter or getter
* @return proto message with method body
*/
public ProtoIlInstructions.IlFunctionMsg getMethodBodyOfProperty(String className, String propertyName,
boolean isSetter) {
ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder analyzerParamsBuilder
= createAnalyzerParamsBuilder(className, ProtoDotnetNativeHost.AnalyzerMethodCall.GET_METHOD_BODY_OF_PROPERTY);
analyzerParamsBuilder.setPropertyName(propertyName);
analyzerParamsBuilder.setPropertyIsSetter(isSetter);
ProtoDotnetNativeHost.AnalyzerParamsMsg analyzerParamsMsg = analyzerParamsBuilder.build();
try {
byte[] protoMsgBytes = nativeGetMethodBodyOfPropertyMsg(pathNativeHost, analyzerParamsMsg.toByteArray());
return ProtoIlInstructions.IlFunctionMsg.parseFrom(protoMsgBytes);
} catch (Exception e) {
if (Options.v().verbose()) {
logger.warn(
"Exception while getting method body of property " + className + "." + propertyName + ": " + e.getMessage());
logger.warn("Return null");
}
return null;
}
}
/**
* Get Method Body of event methods
*
* @param className
* declaring class
* @param eventName
* name of event
* @param eventDirective
* method request
* @return proto message with method body
*/
public ProtoIlInstructions.IlFunctionMsg getMethodBodyOfEvent(String className, String eventName,
DotnetEvent.EventDirective eventDirective) {
// set parameter for request to Soot.Dotnet.Decompiler
ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder analyzerParamsBuilder
= createAnalyzerParamsBuilder(className, ProtoDotnetNativeHost.AnalyzerMethodCall.GET_METHOD_BODY_OF_EVENT);
analyzerParamsBuilder.setEventName(eventName);
ProtoDotnetNativeHost.EventAccessorType accessorType;
switch (eventDirective) {
case ADD:
accessorType = ProtoDotnetNativeHost.EventAccessorType.ADD_ACCESSOR;
break;
case REMOVE:
accessorType = ProtoDotnetNativeHost.EventAccessorType.REMOVE_ACCESSOR;
break;
case INVOKE:
accessorType = ProtoDotnetNativeHost.EventAccessorType.INVOKE_ACCESSOR;
break;
default:
throw new RuntimeException("Wrong Event Accessor Type!");
}
analyzerParamsBuilder.setEventAccessorType(accessorType);
ProtoDotnetNativeHost.AnalyzerParamsMsg analyzerParamsMsg = analyzerParamsBuilder.build();
try {
byte[] protoMsgBytes = nativeGetMethodBodyOfEventMsg(pathNativeHost, analyzerParamsMsg.toByteArray());
return ProtoIlInstructions.IlFunctionMsg.parseFrom(protoMsgBytes);
} catch (Exception e) {
if (Options.v().verbose()) {
logger.warn("Exception while getting method body of event " + className + "." + eventName + ": " + e.getMessage());
}
return null;
}
}
/**
* Check if given file is an assembly file
*
* @return true if this object referenced to a file is an assembly
*/
public boolean isAssembly() {
return nativeIsAssembly(pathNativeHost, fullyQualifiedAssemblyPathFilename);
}
/**
* Get Type definition as Proto Message
*
* @param className
* requested type
* @return proto message with the given type definition
*/
public ProtoAssemblyAllTypes.TypeDefinition getTypeDefinition(String className) {
if (Strings.isNullOrEmpty(className)) {
return null;
}
ProtoAssemblyAllTypes.AssemblyAllTypes allTypes = getAllTypes();
if (allTypes == null) {
return null;
}
List<ProtoAssemblyAllTypes.TypeDefinition> allTypesList = allTypes.getListOfTypesList();
Optional<ProtoAssemblyAllTypes.TypeDefinition> c
= allTypesList.stream().filter(x -> x.getFullname().equals(className)).findFirst();
return c.orElse(null);
}
/**
* Get all types of given assembly as a list of strings
*
* @return list of strings with all types
*/
public List<String> getAllTypeNames() {
ProtoAssemblyAllTypes.AssemblyAllTypes allTypes = getAllTypes();
if (allTypes == null) {
return null;
}
List<ProtoAssemblyAllTypes.TypeDefinition> listOfTypesList = allTypes.getListOfTypesList();
return listOfTypesList.stream().map(ProtoAssemblyAllTypes.TypeDefinition::getFullname).collect(Collectors.toList());
}
/**
* Get all module type names which are references from this assembly
*
* @return list of strings with all possible referenced module type names
*/
public List<String> getAllReferencedModuleTypes() {
ProtoAssemblyAllTypes.AssemblyAllTypes allTypes = getAllTypes();
if (allTypes == null || gotAllReferencesModuleTypes) {
return new ArrayList<>();
}
gotAllReferencesModuleTypes = true;
return allTypes.getAllReferencedModuleTypesList();
}
/**
* Helper method
*
* @param className
* @param methodCall
* @return
*/
private ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder createAnalyzerParamsBuilder(String className,
ProtoDotnetNativeHost.AnalyzerMethodCall methodCall) {
ProtoDotnetNativeHost.AnalyzerParamsMsg.Builder analyzerParamsBuilder
= ProtoDotnetNativeHost.AnalyzerParamsMsg.newBuilder();
analyzerParamsBuilder.setAnalyzerMethodCall(methodCall);
analyzerParamsBuilder.setAssemblyFileAbsolutePath(fullyQualifiedAssemblyPathFilename);
analyzerParamsBuilder.setTypeReflectionName(className);
if (Options.v().verbose() || Options.v().debug()) {
analyzerParamsBuilder.setDebugMode(true);
}
return analyzerParamsBuilder;
}
public String getFullPath() {
return FilenameUtils.getFullPath(fullyQualifiedAssemblyPathFilename);
}
public String getAssemblyFileName() {
return FilenameUtils.getName(fullyQualifiedAssemblyPathFilename);
}
// --- native declarations ---
/**
* Get all classes of given assembly
*
* @param pathToNativeHost
* Path where Soot.Dotnet.Nativehost binary is located
* @param disassemblerParams
* disassembler parameter, such as: path to assembly file, type/class name, method name
* @return list of classes
*/
private native byte[] nativeGetAllTypesMsg(String pathToNativeHost, byte[] disassemblerParams);
/**
* Get method body of given method and type (class)
*
* @param pathToNativeHost
* Path where Soot.Dotnet.Nativehost binary is located
* @param disassemblerParams
* parameter, such as: path to assembly file, type/class name, method name
* @return list/trees of il instructions
*/
private native byte[] nativeGetMethodBodyMsg(String pathToNativeHost, byte[] disassemblerParams);
/**
* Get method body of getter/setter of a property
*
* @param pathToNativeHost
* Path where Soot.Dotnet.Nativehost binary is located
* @param disassemblerParams
* parameter, such as: path to assembly file, type/class name, method name
* @return byte array with requested proto message as response
*/
private native byte[] nativeGetMethodBodyOfPropertyMsg(String pathToNativeHost, byte[] disassemblerParams);
/**
* Get method body of method of an event
*
* @param pathToNativeHost
* Path where Soot.Dotnet.Nativehost binary is located
* @param disassemblerParams
* parameter, such as: path to assembly file, type/class name, method name
* @return byte array with requested proto message as response
*/
private native byte[] nativeGetMethodBodyOfEventMsg(String pathToNativeHost, byte[] disassemblerParams);
/**
* Universal method for getting content of Soot.Dotnet.Decompiler. Purpose of this method is that we do not need to edit
* the bridge Soot.Dotnet.NativeHost
*
* @param pathToNativeHost
* Path where the library file of the native host is located, e.g.
* /Users/user/soot-dotnet/src/Soot.Dotnet.NativeHost/bin/Debug/libNativeHost.dylib
* @param disassemblerParams
* parameter, such as: path to assembly file, type/class name, method name
* @return byte array with requested proto message as response
*/
private native byte[] nativeGetAssemblyContentMsg(String pathToNativeHost, byte[] disassemblerParams);
/**
* Check if given assembly file is an assembly
*
* @param absolutePathAssembly
* e.g. /home/user/cs/myassembly.dll
* @return true if given file is assembly
*/
private native boolean nativeIsAssembly(String pathToNativeHost, String absolutePathAssembly);
}