Skip to content
Navigation Menu
{{ message }}
forked from souffle-lang/java-pts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
280 lines (244 loc) · 9.41 KB
/
Main.java
File metadata and controls
280 lines (244 loc) · 9.41 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
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import soot.ClassProvider;
import soot.Scene;
import soot.SootClass;
public class Main {
public static enum Mode {INPUTS, FULL;}
private static Mode _mode = null;
private static List<String> _inputs = new ArrayList<String>();
private static List<String> _libraries = new ArrayList<String>();
private static String _outputDir = null;
private static String _main = null;
private static boolean _ssa = false;
private static boolean _trap = false;
private static boolean _allowPhantom = false;
private static int shift(String[] args, int index) {
if(args.length == index + 1) {
System.err.println("error: option " + args[index] + " requires an argument");
System.exit(1);
}
return index + 1;
}
public static void main(String[] args)
{
try
{
if(args.length == 0)
{
System.err.println("usage: soot-fact-generation [options] file...");
System.exit(0);
}
for(int i = 0; i < args.length; i++)
{
if(args[i].equals("--full") || args[i].equals("-full"))
{
if( _mode != null)
{
System.err.println("error: duplicate mode argument");
System.exit(1);
}
_mode = Mode.FULL;
}
else if(args[i].equals("-d"))
{
i = shift(args, i);
_outputDir = args[i];
}
else if(args[i].equals("-main"))
{
i = shift(args, i);
_main = args[i];
}
else if(args[i].equals("-ssa"))
{
_ssa = true;
}
else if(args[i].equals("-l"))
{
i = shift(args, i);
_libraries.add(args[i]);
}
else if(args[i].equals("-lsystem") || args[i].equals("--lsystem"))
{
String javaHome = System.getProperty("java.home");
_libraries.add(javaHome + File.separator + "lib" + File.separator + "rt.jar");
_libraries.add(javaHome + File.separator + "lib" + File.separator + "jce.jar");
_libraries.add(javaHome + File.separator + "lib" + File.separator + "jsse.jar");
}
else if(args[i].equals("-deps")) {
i = shift(args, i);
String folderName = args[i];
File f = new File(folderName);
if(!f.exists()) {
System.err.println("Dependency folder " + folderName + " does not exist");
System.exit(0);
}
else if(!f.isDirectory()) {
System.err.println("Dependency folder " + folderName + " is not a directory");
System.exit(0);
}
for(File file : f.listFiles()) {
if(file.isFile() && file.getName().endsWith(".jar")) {
_libraries.add(file.getCanonicalPath());
}
}
}
else if(args[i].equals("-trap"))
{
_trap = true;
}
else if(args[i].equals("-allow-phantom"))
{
_allowPhantom = true;
}
else if(args[i].equals("-h") || args[i].equals("--help") || args[i].equals("-help")) {
System.err.println("usage: soot-fact-generation [options] file...");
System.err.println("options:");
System.err.println(" -main <class> Specify the main name of the main class");
System.err.println(" -full Generate facts by full transitive resolution");
System.err.println(" -ssa Generate SSA facts, enabling flow-sensitive analysis");
System.err.println(" -d <directory> Specify where to generate csv fact files.");
System.err.println(" -l <archive> Find classes in jar/zip archive.");
System.err.println(" -lsystem Find classes in default system classes.");
System.err.println(" -deps <directory> Add jars in this directory to the class lookup path");
System.err.println(" -trap Generate TRAP files rather than CSV files");
System.err.println(" -h, -help, Print this help message.");
System.exit(0);
}
else
{
if(args[i].charAt(0) == '-')
{
System.err.println("error: unrecognized option: " + args[i]);
System.exit(0);
}
else
{
_inputs.add(args[i]);
}
}
}
if(_mode == null) {
_mode = Mode.INPUTS;
}
if(_outputDir == null) {
_outputDir = System.getProperty("user.dir");
}
run();
}
catch(Exception exc)
{
exc.printStackTrace();
System.exit(1);
}
}
private static void run() throws Exception
{
NoSearchingClassProvider provider = new NoSearchingClassProvider();
for(String arg : _inputs)
{
if(arg.endsWith(".jar") || arg.endsWith(".zip"))
{
System.out.println("Adding archive: " + arg);
provider.addArchive(new File(arg));
}
else
{
System.out.println("Adding file: " + arg);
provider.addClass(new File(arg));
}
}
for(String lib: _libraries) {
System.out.println("Adding archive for resolving: " + lib);
provider.addArchiveForResolving(new File(lib));
}
soot.SourceLocator.v().setClassProviders(Collections.singletonList((ClassProvider) provider));
Scene scene = Scene.v();
if(_main != null)
{
soot.options.Options.v().set_main_class(_main);
}
if(_mode == Mode.FULL)
{
soot.options.Options.v().set_full_resolver(true);
}
if(_allowPhantom)
{
soot.options.Options.v().set_allow_phantom_refs(true);
}
Collection<SootClass> classes = new ArrayList<SootClass>();
for(String className : provider.getClassNames())
{
scene.loadClass(className, SootClass.SIGNATURES);
SootClass c = scene.loadClass(className, SootClass.BODIES);
c.setApplicationClass();
classes.add(c);
}
/* For simulating the FileSystem class, we need the implementation
of the FileSystem, but the classes are not loaded automatically
due to the indirection via native code.
*/
addCommonDynamicClass(scene, provider, "java.io.UnixFileSystem");
addCommonDynamicClass(scene, provider, "java.io.WinNTFileSystem");
addCommonDynamicClass(scene, provider, "java.io.Win32FileSystem");
/* java.net.URL loads handlers dynamically */
addCommonDynamicClass(scene, provider, "sun.net.www.protocol.file.Handler");
addCommonDynamicClass(scene, provider, "sun.net.www.protocol.ftp.Handler");
addCommonDynamicClass(scene, provider, "sun.net.www.protocol.http.Handler");
addCommonDynamicClass(scene, provider, "sun.net.www.protocol.https.Handler");
addCommonDynamicClass(scene, provider, "sun.net.www.protocol.jar.Handler");
scene.loadNecessaryClasses();
Database db;
if(_trap)
db = new TRAPDatabase(new File(_outputDir));
else
db = new CSVDatabase(new File(_outputDir));
String[] rels = {"ActualParam", "ApplicationClass", "ArrayType", "AssignCast", "AssignHeapAllocation",
"AssignLocal", "AssignReturnValue", "ClassConstant", "ClassType", "ComponentType",
"DirectSuperclass", "DirectSuperinterface", "ExceptionHandlerBegin", "ExceptionHandlerEnd",
"ExceptionHandlerFormalParam", "ExceptionHandlerMethod", "ExceptionHandlerPrevious",
"ExceptionHandlerRef", "ExceptionHandlerType", "FieldModifier", "FieldSignature",
"FieldSignatureRef", "FormalParam", "HeapAllocationType", "InstructionIndex",
"InterfaceType", "LoadArrayIndex", "LoadInstanceField", "LoadPrimStaticField",
"LoadStaticField", "MethodDeclaration", "MethodDeclarationException", "MethodDescriptorRef",
"MethodInvocationRef", "MethodModifier", "MethodSignatureDescriptor", "MethodSignatureRef",
"MethodSignatureSimpleName", "MethodSignatureType", "ModifierRef", "NormalHeapAllocationRef",
"NullType", "PrimitiveType", "ReifiedClass", "ReturnVar", "SimpleExceptionHandler",
"SimpleNameRef", "SpecialMethodInvocationBase", "SpecialMethodInvocationIn",
"SpecialMethodInvocationSignature", "StaticMethodInvocation", "StaticMethodInvocationIn",
"StaticMethodInvocationSignature", "StoreArrayIndex", "StoreInstanceField",
"StorePrimStaticField", "StoreStaticField", "StringConstant", "ThisVar",
"Throw", "ThrowMethod", "ThrowRef", "VarDeclaringMethod", "VarRef", "VarType",
"VirtualMethodInvocation", "VirtualMethodInvocationBase", "VirtualMethodInvocationIn",
"VirtualMethodInvocationSignature"};
for(String name : rels) {
db.register(name);
}
FactWriter writer = new FactWriter(db);
FactGenerator generator = new FactGenerator(writer, _ssa);
for(SootClass c : classes)
{
writer.writeApplicationClass(c);
}
if(_mode == Mode.FULL)
{
classes = scene.getClasses();
}
for(SootClass c : classes)
{
generator.generate(c);
}
db.close();
}
public static void addCommonDynamicClass(Scene scene, ClassProvider provider, String className)
{
if(provider.find(className) != null)
{
scene.addBasicClass(className);
}
}
}
You can’t perform that action at this time.
