6
6
*/
7
7
package minijar ;
8
8
9
- import java .io .File ;
10
- import java .io .IOException ;
9
+ import java .io .*;
11
10
import java .nio .charset .StandardCharsets ;
12
11
import java .util .ArrayList ;
13
12
import java .util .HashSet ;
17
16
18
17
import com .google .common .collect .Sets ;
19
18
import com .google .common .io .Files ;
20
- import com .ibm .wala .classLoader .IMethod ;
19
+ import com .ibm .wala .analysis .reflection .ClassFactoryContextInterpreter ;
20
+ import com .ibm .wala .analysis .reflection .ClassFactoryContextSelector ;
21
+ import com .ibm .wala .analysis .reflection .JavaTypeContext ;
22
+ import com .ibm .wala .classLoader .*;
21
23
import com .ibm .wala .core .java11 .Java9AnalysisScopeReader ;
22
24
import com .ibm .wala .core .util .warnings .Warnings ;
23
- import com .ibm .wala .ipa .callgraph .AnalysisCache ;
24
- import com .ibm .wala .ipa .callgraph .AnalysisCacheImpl ;
25
- import com .ibm .wala .ipa .callgraph .AnalysisOptions ;
26
- import com .ibm .wala .ipa .callgraph .AnalysisScope ;
27
- import com .ibm .wala .ipa .callgraph .CGNode ;
28
- import com .ibm .wala .ipa .callgraph .CallGraph ;
29
- import com .ibm .wala .ipa .callgraph .CallGraphBuilder ;
30
- import com .ibm .wala .ipa .callgraph .CallGraphBuilderCancelException ;
31
- import com .ibm .wala .ipa .callgraph .Entrypoint ;
25
+ import com .ibm .wala .ipa .callgraph .*;
26
+ import com .ibm .wala .ipa .callgraph .impl .ContextInsensitiveSelector ;
32
27
import com .ibm .wala .ipa .callgraph .impl .Everywhere ;
33
28
import com .ibm .wala .ipa .callgraph .impl .Util ;
34
29
import com .ibm .wala .ipa .callgraph .propagation .InstanceKey ;
30
+ import com .ibm .wala .ipa .callgraph .propagation .PropagationCallGraphBuilder ;
35
31
import com .ibm .wala .ipa .cha .ClassHierarchyException ;
36
32
import com .ibm .wala .ipa .cha .ClassHierarchyFactory ;
37
33
import com .ibm .wala .ipa .cha .IClassHierarchy ;
42
38
import com .ibm .wala .shrike .shrikeCT .ConstantPoolParser ;
43
39
import com .ibm .wala .types .MethodReference ;
44
40
import com .ibm .wala .util .collections .HashMapFactory ;
41
+ import com .ibm .wala .util .config .FileOfClasses ;
42
+ import com .ibm .wala .util .intset .IntSet ;
45
43
46
44
public class MiniJar {
47
45
private static final String USAGE =
@@ -127,7 +125,7 @@ public static void main(String[] args) throws Exception {
127
125
128
126
String [] mClasses = new String [mainClasses .size ()];
129
127
Set <String > cg = cw .getReachableMethods (mainClasses .toArray (mClasses ), scopeFileData , entryClass , entrypointMethods , includedPaths );
130
-
128
+
131
129
while ((ci = instrumenter .nextClass ()) != null ) {
132
130
try {
133
131
cw .processClass (ci , cg , includedPaths );
@@ -231,6 +229,7 @@ private Set<String> getReachableMethods(String[] mainClasses, String scopeFileDa
231
229
AnalysisScope scope = new Java9AnalysisScopeReader ().readJavaScope (scopeFileData , null , MiniJar .class .getClassLoader ());
232
230
233
231
IClassHierarchy cha = ClassHierarchyFactory .make (scope );
232
+ addDefaultExclusions (scope );
234
233
System .out .println (cha .getNumberOfClasses () + " classes" );
235
234
System .out .println (Warnings .asString ());
236
235
Warnings .clear ();
@@ -252,16 +251,70 @@ private Set<String> getReachableMethods(String[] mainClasses, String scopeFileDa
252
251
}
253
252
254
253
// you can dial down reflection handling if you like
255
- //options.setReflectionOptions(AnalysisOptions.ReflectionOptions.NO_FLOW_TO_CASTS);
256
254
options .setReflectionOptions (AnalysisOptions .ReflectionOptions .NO_FLOW_TO_CASTS );
255
+ //options.setReflectionOptions(AnalysisOptions.ReflectionOptions.NONE);
257
256
AnalysisCache cache = new AnalysisCacheImpl ();
258
-
259
- CallGraphBuilder <InstanceKey > builder = Util .makeRTABuilder (options , cache , cha , scope );
260
- //CallGraphBuilder builder = Util.makeZeroCFABuilder(Language.JAVA, options, cache, cha, scope);
257
+
258
+ //CallGraphBuilder builder = Util.makeZeroOneCFABuilder(Language.JAVA, options, cache, cha, scope);
259
+
260
+ //CallGraphBuilder<InstanceKey> builder = Util.makeRTABuilder(options, cache, cha, scope);
261
+ CallGraphBuilder builder = Util .makeZeroCFABuilder (Language .JAVA , options , cache , cha , scope );
261
262
//CallGraphBuilder builder = Util.makeNCFABuilder(2, options, cache, cha, scope);
262
263
//CallGraphBuilder builder = Util.makeVanillaNCFABuilder(2, options, cache, cha, scope);
263
264
//CallGraphBuilder builder = Util.makeZeroOneContainerCFABuilder(options, cache, cha, scope);
264
-
265
+
266
+ options .setSelector (new ClassTargetSelector () {
267
+ ClassTargetSelector base = options .getClassTargetSelector ();
268
+ Set <IClass > seen = new HashSet <>();
269
+ FileWriter log = new FileWriter ("seenNOFLOWClasses.txt" );
270
+
271
+ @ Override
272
+ public IClass getAllocatedTarget (CGNode caller , NewSiteReference site ) {
273
+ IClass baseC = base .getAllocatedTarget (caller , site );
274
+
275
+ if (!seen .contains (baseC )) {
276
+ seen .add (baseC );
277
+ try {
278
+ log .write ("**** SEEN: " + baseC + "\n " );
279
+ log .flush ();
280
+ } catch (IOException e ) {
281
+ System .err .println (e );
282
+ }
283
+
284
+ }
285
+ return baseC ;
286
+ }
287
+ });
288
+
289
+ PropagationCallGraphBuilder propBuilder = ((PropagationCallGraphBuilder ) builder );
290
+
291
+ propBuilder .setContextSelector (new ContextSelector () {
292
+ ContextSelector base = propBuilder .getContextSelector ();
293
+ ClassFactoryContextSelector fac = new ClassFactoryContextSelector ();
294
+ FileWriter log = new FileWriter ("seenReflectiveClasses.txt" );
295
+
296
+ @ Override
297
+ public Context getCalleeTarget (CGNode caller , CallSiteReference site , IMethod callee , InstanceKey [] actualParameters ) {
298
+ Context con = fac .getCalleeTarget (caller , site , callee , actualParameters );
299
+ if (con instanceof JavaTypeContext ) {
300
+ JavaTypeContext javaCon = (JavaTypeContext ) con ;
301
+ try {
302
+ log .write (javaCon .getType ().getType ().toString () + "\n " );
303
+ log .flush ();
304
+ } catch (IOException e ) {
305
+ System .err .println (e );
306
+ }
307
+ }
308
+ return base .getCalleeTarget (caller , site , callee , actualParameters );
309
+ }
310
+
311
+ @ Override
312
+ public IntSet getRelevantParameters (CGNode caller , CallSiteReference site ) {
313
+ return base .getRelevantParameters (caller , site );
314
+ }
315
+ });
316
+
317
+
265
318
System .out .println ("building call graph..." );
266
319
CallGraph cg = builder .makeCallGraph (options , null );
267
320
System .out .println ("done! " + cg .getNumberOfNodes ());
@@ -283,7 +336,7 @@ private static void processMethod(Set<String> allMethods, CGNode n, IClassHierar
283
336
allMethods .addAll (MethodUtil .getSuperMethods (m ,cha ));
284
337
285
338
// Process for call sites
286
- n .iterateCallSites ().forEachRemaining (call -> {
339
+ /* n.iterateCallSites().forEachRemaining(call -> {
287
340
if (call.getDeclaredTarget().equals(MethodReference.JavaLangClassNewInstance)) {
288
341
Set<CGNode> targets = cg.getPossibleTargets(n, call);
289
342
if (targets.isEmpty()) {
@@ -305,10 +358,29 @@ private static void processMethod(Set<String> allMethods, CGNode n, IClassHierar
305
358
}
306
359
307
360
});
361
+ */
362
+
308
363
309
364
}
310
365
311
-
366
+ private static final String EXCLUSIONS = "java\\ /awt\\ /.*\n " +
367
+ "javax\\ /swing\\ /.*\n " +
368
+ "sun\\ /awt\\ /.*\n " +
369
+ "sun\\ /swing\\ /.*\n " +
370
+ "com\\ /sun\\ /.*\n " +
371
+ "sun\\ /.*\n " +
372
+ "org\\ /netbeans\\ /.*\n " +
373
+ "org\\ /openide\\ /.*\n " +
374
+ "com\\ /ibm\\ /crypto\\ /.*\n " +
375
+ "com\\ /ibm\\ /security\\ /.*\n " +
376
+ "org\\ /apache\\ /xerces\\ /.*\n " +
377
+ "java\\ /security\\ /.*\n " +
378
+ "jdk\\ /.*\n " +
379
+ "" ;
380
+
381
+ public static void addDefaultExclusions (AnalysisScope scope ) throws UnsupportedEncodingException , IOException {
382
+ scope .setExclusions (new FileOfClasses (new ByteArrayInputStream (EXCLUSIONS .getBytes ("UTF-8" ))));
383
+ }
312
384
313
385
314
386
}
0 commit comments