コード例 #1
0
ファイル: RunTestModular.java プロジェクト: serialcake/joana
  private static void runComputeModule(
      final String binDir, final Config cfg, final String outputDir)
      throws IOException, IllegalArgumentException, CancelException, PDGFormatException,
          WalaException {
    Main.checkOrCreateOutputDir(outputDir);

    System.out.print("Setting up analysis scope... ");

    // Fuegt die normale Java Bibliothek zum Scope hinzu
    AnalysisScope scope = AnalysisScopeReader.makePrimordialScope(null);

    if (cfg.nativesXML != null) {
      com.ibm.wala.ipa.callgraph.impl.Util.setNativeSpec(cfg.nativesXML);
    }

    // if use stubs
    if (cfg.stubs != null) {
      scope.addToScope(ClassLoaderReference.Primordial, new JarFile(cfg.stubs));
    }

    // Nimmt unnoetige Klassen raus
    SetOfClasses exclusions =
        new FileOfClasses(new ByteArrayInputStream(cfg.exclusions.getBytes()));
    scope.setExclusions(exclusions);

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

    System.out.println("done.");

    ClassHierarchy cha = ClassHierarchy.make(scope);

    System.out.print("Creating MoJo... ");

    MoJo mojo = MoJo.create(cha);

    System.out.println("done.");

    for (IClass cls : cha) {
      for (IMethod im : cls.getDeclaredMethods()) {
        if (im.getDeclaringClass().isInterface() || im.isAbstract()) {
          // skip methods without a body
          continue;
        }

        MethodInfo nfo = cfg.extern.checkForModuleMethod(im);
        if (nfo != null) {
          System.out.println("Found " + nfo + " for " + im);
          final String sig = Dictionary.extractSignature(im);
          final String outputMethodDir =
              outputDir + (outputDir.endsWith(File.separator) ? "" : File.separator) + "m_" + sig;
          Main.checkOrCreateOutputDir(outputMethodDir);
          computeVariants(mojo, im, nfo, outputMethodDir);
        }
      }
    }
  }
コード例 #2
0
 @Test
 public void testProjectScope() throws IOException, CoreException {
   IJavaScriptProject p =
       JavaScriptHeadlessUtil.getJavaScriptProjectFromWorkspace(project.projectName);
   JSCallGraphUtil.setTranslatorFactory(new CAstRhinoTranslatorFactory());
   AnalysisScope s =
       makeProjectPath(p)
           .toAnalysisScope(
               new CAstAnalysisScope(
                   JSCallGraphUtil.makeLoaders(), Collections.singleton(JavaScriptLoader.JS)));
   System.err.println(s);
   Assert.assertTrue("cannot make scope", s != null);
   Assert.assertFalse("cannot find files", s.getModules(JavaScriptTypes.jsLoader).isEmpty());
 }
コード例 #3
0
  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;
  }
コード例 #4
0
ファイル: SourceDirCallGraph.java プロジェクト: wala/WALA
  /**
   * 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));
  }
コード例 #5
0
  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;
  }
コード例 #6
0
  @Override
  protected IClassLoader makeNewClassLoader(
      ClassLoaderReference classLoaderReference,
      IClassHierarchy cha,
      IClassLoader parent,
      AnalysisScope scope)
      throws IOException {
    if (classLoaderReference.equals(JavaScriptTypes.jsLoader)) {
      JavaScriptLoader L = new JavaScriptLoader(cha, jsTranslatorFactory);
      L.init(scope.getModules(classLoaderReference));
      return L;

    } else {
      return super.makeNewClassLoader(classLoaderReference, cha, parent, scope);
    }
  }
コード例 #7
0
ファイル: Util.java プロジェクト: softwareEng/WALA
  public static void addJ2EEBypassLogic(
      AnalysisOptions options,
      AnalysisScope scope,
      DeploymentMetaData dmd,
      IClassHierarchy cha,
      ReceiverTypeInferenceCache typeInference) {

    if (cha == null) {
      throw new IllegalArgumentException("cha is null");
    }
    MethodTargetSelector ms =
        new J2EEMethodTargetSelector(
            scope, options.getMethodTargetSelector(), dmd, cha, typeInference);
    options.setSelector(ms);

    ClassTargetSelector cs =
        new J2EEClassTargetSelector(
            options.getClassTargetSelector(),
            dmd,
            cha,
            cha.getLoader(scope.getLoader(Atom.findOrCreateUnicodeAtom("Synthetic"))));
    options.setSelector(cs);
  }
コード例 #8
0
  // public static void addBypassLogic(AnalysisOptions options, AnalysisScope
  // scope, ClassLoader cl, String xmlFile,
  // IClassHierarchy cha) throws IllegalArgumentException {
  public static void addBypassLogic(
      AnalysisOptions options,
      AnalysisScope scope,
      InputStream xmlIStream,
      IClassHierarchy cha,
      MethodSummary extraSummary)
      throws IllegalArgumentException {

    if (scope == null) {
      throw new IllegalArgumentException("scope is null");
    }
    if (options == null) {
      throw new IllegalArgumentException("options is null");
    }
    // if (cl == null) {
    // throw new IllegalArgumentException("cl is null");
    // }
    if (cha == null) {
      throw new IllegalArgumentException("cha cannot be null");
    }

    InputStream s = null;
    try {
      Set<TypeReference> summaryClasses = HashSetFactory.make();
      Map<MethodReference, MethodSummary> summaries = HashMapFactory.make();

      if (null != xmlIStream) {
        XMLMethodSummaryReader newSummaryXML = loadMethodSummaries(scope, xmlIStream);
        summaryClasses.addAll(newSummaryXML.getAllocatableClasses());
        for (MethodSummary summary : newSummaryXML.getSummaries().values()) {
          logger.trace(
              "SSA instructions for summary of {}:\n{}",
              summary.getMethod().getSignature().toString(),
              Arrays.toString(summary.getStatements()));
        }
        summaries.putAll(newSummaryXML.getSummaries());
      }
      logger.debug("loaded " + summaries.size() + " new summaries");
      // for (MethodReference mr : summaries.keySet()) {
      // logger.debug("summary loaded for: "+mr.getSignature());
      // }

      s =
          new FileProvider()
              .getInputStreamFromClassLoader(
                  pathToSpec + File.separator + methodSpec,
                  AndroidAnalysisContext.class.getClassLoader());

      XMLMethodSummaryReader nativeSummaries = loadMethodSummaries(scope, s);

      logger.debug("loaded " + nativeSummaries.getSummaries().size() + " native summaries");

      summaries.putAll(nativeSummaries.getSummaries());
      summaryClasses.addAll(nativeSummaries.getAllocatableClasses());
      if (extraSummary != null) {
        summaries.put((MethodReference) extraSummary.getMethod(), extraSummary);
      }

      MethodTargetSelector ms =
          new BypassMethodTargetSelector(
              options.getMethodTargetSelector(), summaries,
              nativeSummaries.getIgnoredPackages(), cha);
      options.setSelector(ms);

      ClassTargetSelector cs =
          new BypassClassTargetSelector(
              options.getClassTargetSelector(),
              summaryClasses,
              cha,
              cha.getLoader(scope.getLoader(Atom.findOrCreateUnicodeAtom("Synthetic"))));
      options.setSelector(cs);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      if (null != s) {
        try {
          s.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }