/**
  * builds a call graph, and sets the corresponding heap model for analysis
  *
  * @param scope
  * @param cha
  * @param options
  * @return
  * @throws CancelException
  * @throws IllegalArgumentException
  */
 private static Pair<CallGraph, PointerAnalysis> buildCallGraph(
     AnalysisScope scope, ClassHierarchy cha, AnalysisOptions options)
     throws IllegalArgumentException, CancelException {
   CallGraph retCG = null;
   PointerAnalysis retPA = null;
   final AnalysisCache cache = new AnalysisCache();
   CallGraphBuilder builder;
   if (CHEAP_CG) {
     builder = Util.makeZeroCFABuilder(options, cache, cha, scope);
     // we want vanilla 0-1 CFA, which has one abstract loc per allocation
     heapModel = Util.makeVanillaZeroOneCFABuilder(options, cache, cha, scope);
   } else {
     builder = Util.makeZeroOneContainerCFABuilder(options, cache, cha, scope);
     heapModel = (HeapModel) builder;
   }
   ProgressMaster master = ProgressMaster.make(new NullProgressMonitor(), 360000, false);
   master.beginTask("runSolver", 1);
   try {
     retCG = builder.makeCallGraph(options, master);
     retPA = builder.getPointerAnalysis();
   } catch (CallGraphBuilderCancelException e) {
     System.err.println("TIMED OUT!!");
     retCG = e.getPartialCallGraph();
     retPA = e.getPartialPointerAnalysis();
   }
   return Pair.make(retCG, retPA);
 }
  public static ExceptionPruneAnalysis createBarcodeTest()
      throws WalaException, IOException, IllegalArgumentException, CallGraphBuilderCancelException {
    com.ibm.wala.ipa.callgraph.impl.Util.setNativeSpec("test_lib/natives_empty.xml");

    AnalysisScope scope = AnalysisScopeReader.makePrimordialScope(null);

    SetOfClasses exclusions = new FileOfClasses(new ByteArrayInputStream("com/sun/.*".getBytes()));
    scope.setExclusions(exclusions);

    ClassLoaderReference primordial = scope.getLoader(AnalysisScope.PRIMORDIAL);
    AnalysisScopeReader.addClassPathToScope("test_lib/jSDG-stubs-j2me2.0.jar", scope, primordial);
    AnalysisScopeReader.addClassPathToScope("test_lib/primordial.jar", scope, primordial);
    ClassLoaderReference loader = scope.getLoader(AnalysisScope.APPLICATION);
    AnalysisScopeReader.addClassPathToScope(classPath, scope, loader);

    // Klassenhierarchie berechnen
    System.out.println("Creating class hierarchy...");
    ClassHierarchy cha = ClassHierarchy.make(scope);
    System.out.println("Done.");

    AnalysisCache cache = new AnalysisCache();

    Iterable<Entrypoint> entries = Util.makeMainEntrypoints(scope, cha, "LMainEmulator");
    //        MethodReference mr = StringStuff.makeMethodReference(Language.JAVA, methodSig);
    //        IMethod m = cha.resolveMethod(mr);
    if (entries == null || !entries.iterator().hasNext()) {
      throw new IllegalStateException("Could not find main method in MainEmulator");
    }

    //        Set<Entrypoint> entries = HashSetFactory.make();
    //        entries.add(new DefaultEntrypoint(m, cha));
    AnalysisOptions options = new AnalysisOptions();
    options.getSSAOptions().setPiNodePolicy(SSAOptions.getAllBuiltInPiNodes());
    options.setEntrypoints(entries);
    CallGraphBuilder builder = Util.makeZeroCFABuilder(options, cache, cha, scope);

    System.out.println("Creating call graph...");
    CallGraph cg = builder.makeCallGraph(options, null);
    System.out.println("Done.");

    PointerAnalysis pta = builder.getPointerAnalysis();

    ExceptionPruneAnalysis mCFG = new ExceptionPruneAnalysis(cg, pta, cache);

    return mCFG;
  }
Beispiel #3
0
 @Test
 public void testZeroOneContainerCopyOf()
     throws IOException, ClassHierarchyException, IllegalArgumentException, CancelException {
   AnalysisScope scope =
       CallGraphTestUtil.makeJ2SEAnalysisScope(
           TestConstants.WALA_TESTDATA, CallGraphTestUtil.REGRESSION_EXCLUSIONS);
   ClassHierarchy cha = ClassHierarchy.make(scope);
   Iterable<Entrypoint> entrypoints =
       Util.makeMainEntrypoints(scope, cha, "Ldemandpa/TestArraysCopyOf");
   AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
   AnalysisCache cache = new AnalysisCache();
   CallGraphBuilder builder = Util.makeZeroOneContainerCFABuilder(options, cache, cha, scope);
   CallGraph cg = builder.makeCallGraph(options, null);
   PointerAnalysis pa = builder.getPointerAnalysis();
   CGNode mainMethod = AbstractPtrTest.findMainMethod(cg);
   PointerKey keyToQuery = AbstractPtrTest.getParam(mainMethod, "testThisVar", pa.getHeapModel());
   OrdinalSet<? extends InstanceKey> pointsToSet = pa.getPointsToSet(keyToQuery);
   Assert.assertEquals(1, pointsToSet.size());
 }
Beispiel #4
0
  /**
   * Usage: ScopeFileCallGraph -sourceDir file_path -mainClass class_name
   *
   * <p>If given -mainClass, uses main() method of class_name as entrypoint. Class name should start
   * with an 'L'.
   *
   * <p>Example args: -sourceDir /tmp/srcTest -mainClass LFoo
   */
  public static void main(String[] args)
      throws ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException,
          IOException {
    long start = System.currentTimeMillis();
    Properties p = CommandLine.parse(args);
    String sourceDir = p.getProperty("sourceDir");
    String mainClass = p.getProperty("mainClass");
    AnalysisScope scope = new JavaSourceAnalysisScope();
    // add standard libraries to scope
    String[] stdlibs = WalaProperties.getJ2SEJarFiles();
    for (int i = 0; i < stdlibs.length; i++) {
      scope.addToScope(ClassLoaderReference.Primordial, new JarFile(stdlibs[i]));
    }
    // add the source directory
    scope.addToScope(
        JavaSourceAnalysisScope.SOURCE, new SourceDirectoryTreeModule(new File(sourceDir)));

    // build the class hierarchy
    IClassHierarchy cha =
        ClassHierarchy.make(scope, new ECJClassLoaderFactory(scope.getExclusions()));
    System.out.println(cha.getNumberOfClasses() + " classes");
    System.out.println(Warnings.asString());
    Warnings.clear();
    AnalysisOptions options = new AnalysisOptions();
    Iterable<Entrypoint> entrypoints =
        Util.makeMainEntrypoints(JavaSourceAnalysisScope.SOURCE, cha, new String[] {mainClass});
    options.setEntrypoints(entrypoints);
    // you can dial down reflection handling if you like
    //    options.setReflectionOptions(ReflectionOptions.NONE);
    AnalysisCache cache = new AnalysisCache(AstIRFactory.makeDefaultFactory());
    // CallGraphBuilder builder = new ZeroCFABuilderFactory().make(options, cache, cha, scope,
    // false);
    CallGraphBuilder builder =
        new ZeroOneContainerCFABuilderFactory().make(options, cache, cha, scope, false);
    System.out.println("building call graph...");
    CallGraph cg = builder.makeCallGraph(options, null);
    long end = System.currentTimeMillis();
    System.out.println("done");
    System.out.println("took " + (end - start) + "ms");
    System.out.println(CallGraphStats.getStats(cg));
  }
  public static ExceptionPruneAnalysis create(String methodSig)
      throws WalaException, IOException, IllegalArgumentException, CallGraphBuilderCancelException {
    AnalysisScope scope = AnalysisScopeReader.makePrimordialScope(null);

    SetOfClasses exclusions =
        new FileOfClasses(new ByteArrayInputStream(exclusionRegExp.getBytes()));
    scope.setExclusions(exclusions);

    ClassLoaderReference loader = scope.getLoader(AnalysisScope.APPLICATION);
    AnalysisScopeReader.addClassPathToScope(classPath, scope, loader);

    // Klassenhierarchie berechnen
    ClassHierarchy cha = ClassHierarchy.make(scope);

    AnalysisCache cache = new AnalysisCache();

    MethodReference mr = StringStuff.makeMethodReference(Language.JAVA, methodSig);
    IMethod m = cha.resolveMethod(mr);
    if (m == null) {
      throw new IllegalStateException("Could not resolve " + mr);
    }

    Set<Entrypoint> entries = HashSetFactory.make();
    entries.add(new DefaultEntrypoint(m, cha));
    AnalysisOptions options = new AnalysisOptions();
    options.getSSAOptions().setPiNodePolicy(SSAOptions.getAllBuiltInPiNodes());
    options.setEntrypoints(entries);
    CallGraphBuilder builder = Util.makeZeroOneContainerCFABuilder(options, cache, cha, scope);
    CallGraph cg = builder.makeCallGraph(options, null);

    PointerAnalysis pta = builder.getPointerAnalysis();

    ExceptionPruneAnalysis mCFG = new ExceptionPruneAnalysis(cg, pta, cache);

    return mCFG;
  }