com.ibm.wala.ipa.cha.ClassHierarchyException Java Examples

The following examples show how to use com.ibm.wala.ipa.cha.ClassHierarchyException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallGraphConstructor.java    From fasten with Apache License 2.0 6 votes vote down vote up
/**
 * Build a {@link PartialCallGraph} given classpath.
 *
 * @param coordinate Coordinate
 * @return Partial call graph
 */
public static PartialCallGraph build(final MavenCoordinate coordinate)
        throws IOException, ClassHierarchyException, CallGraphBuilderCancelException {
    final NumberFormat timeFormatter = new DecimalFormat("#0.000");
    logger.info("Generating call graph for the Maven coordinate using WALA: {}",
            coordinate.getCoordinate());
    final long startTime = System.currentTimeMillis();

    final var rawGraph = generateCallGraph(MavenCoordinate.MavenResolver
            .downloadJar(coordinate).orElse(null)
            .getAbsolutePath());

    logger.info("Generated the call graph in {} seconds.",
            timeFormatter.format((System.currentTimeMillis() - startTime) / 1000d));
    return WalaResultAnalyzer.wrap(rawGraph);
}
 
Example #2
Source File: LibraryIdentifier.java    From LibScout with Apache License 2.0 6 votes vote down vote up
private void createClassHierarchy() throws IOException, ClassHierarchyException {
	long s = System.currentTimeMillis();

	// check if we have a multi-dex file
	stats.isMultiDex = ApkUtils.isMultiDexApk(stats.appFile);
	if (stats.isMultiDex)
		logger.info("Multi-dex apk detected - Code is merged to single class hierarchy!");

	// create analysis scope and generate class hierarchy
	// we do not need additional libraries like support libraries,
	// as they are statically linked in the app code.
	final AnalysisScope scope = AndroidAnalysisScope.setUpAndroidAnalysisScope(new File(stats.appFile.getAbsolutePath()).toURI(), null /* no exclusions */, null /* we always pass an android lib */, LibScoutConfig.pathToAndroidJar.toURI());

	cha = ClassHierarchyFactory.makeWithRoot(scope);
	logger.info("Generated class hierarchy (in " + Utils.millisecondsToFormattedTime(System.currentTimeMillis() - s) + ")");
	WalaUtils.getChaStats(cha);
}
 
Example #3
Source File: LibraryApiAnalysis.java    From LibScout with Apache License 2.0 6 votes vote down vote up
private IClassHierarchy createClassHierarchy(File libCodeFile)  throws ClassHierarchyException, IOException, ClassNotFoundException {
    // create analysis scope and generate class hierarchy
    final AnalysisScope scope = AnalysisScope.createJavaAnalysisScope();

    JarFile jf = libCodeFile.getName().endsWith(".aar")? new AarFile(libCodeFile).getJarFile() : new JarFile((libCodeFile));
    scope.addToScope(ClassLoaderReference.Application, jf);
    scope.addToScope(ClassLoaderReference.Primordial, new JarFile(LibScoutConfig.pathToAndroidJar));
    IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope);

    // cleanup tmp files if library input was an .aar file
    if (libCodeFile.getName().endsWith(".aar")) {
        File tmpJar = new File(jf.getName());
        tmpJar.delete();
        logger.trace(Utils.indent() + "tmp jar-file deleted at " + tmpJar.getName());
    }

    return cha;
}
 
Example #4
Source File: WALAPluginTest.java    From fasten with Apache License 2.0 6 votes vote down vote up
@Test
public void sendToKafkaTest() throws IOException, ClassHierarchyException, CallGraphBuilderCancelException {
    KafkaProducer<Object, String> producer = Mockito.mock(KafkaProducer.class);

    Mockito.when(producer.send(Mockito.any())).thenReturn(null);

    JSONObject coordinateJSON = new JSONObject("{\n" +
            "    \"groupId\": \"org.slf4j\",\n" +
            "    \"artifactId\": \"slf4j-api\",\n" +
            "    \"version\": \"1.7.29\",\n" +
            "    \"date\":\"1574072773\"\n" +
            "}");

    walaPlugin.consume(coordinateJSON.toString());

    var coordinate = new MavenCoordinate("org.slf4j", "slf4j-api", "1.7.29");
    var revisionCallGraph = PartialCallGraph.createExtendedRevisionCallGraph(coordinate,
            1574072773);

    assertTrue(walaPlugin.produce().isPresent());
    assertEquals(revisionCallGraph.toJSON().toString(), walaPlugin.produce().get());
}
 
Example #5
Source File: WALAPluginTest.java    From fasten with Apache License 2.0 6 votes vote down vote up
@Test
public void testShouldNotFaceClassReadingError() throws JSONException, IOException, ClassHierarchyException, CallGraphBuilderCancelException {

    JSONObject coordinateJSON1 = new JSONObject("{\n" +
            "    \"groupId\": \"com.zarbosoft\",\n" +
            "    \"artifactId\": \"coroutines-core\",\n" +
            "    \"version\": \"0.0.3\",\n" +
            "    \"date\":\"1574072773\"\n" +
            "}");

    walaPlugin.consume(coordinateJSON1.toString());

    var coordinate = new MavenCoordinate("com.zarbosoft", "coroutines-core", "0.0.3");
    RevisionCallGraph revisionCallGraph =
            PartialCallGraph.createExtendedRevisionCallGraph(coordinate, 1574072773);

    assertTrue(walaPlugin.produce().isPresent());
    assertEquals(revisionCallGraph.toJSON().toString(), walaPlugin.produce().get());
}
 
Example #6
Source File: WALAPluginTest.java    From fasten with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsume() throws JSONException, IOException, ClassHierarchyException, CallGraphBuilderCancelException {

    JSONObject coordinateJSON = new JSONObject("{\n" +
            "    \"groupId\": \"org.slf4j\",\n" +
            "    \"artifactId\": \"slf4j-api\",\n" +
            "    \"version\": \"1.7.29\",\n" +
            "    \"date\":\"1574072773\"\n" +
            "}");

    walaPlugin.consume(coordinateJSON.toString());

    var coordinate = new MavenCoordinate("org.slf4j", "slf4j-api", "1.7.29");
    var revisionCallGraph = PartialCallGraph.createExtendedRevisionCallGraph(coordinate,
            1574072773);

    assertTrue(walaPlugin.produce().isPresent());
    assertEquals(revisionCallGraph.toJSON().toString(), walaPlugin.produce().get());
}
 
Example #7
Source File: CallGraphConstructor.java    From fasten with Apache License 2.0 6 votes vote down vote up
/**
 * Create a call graph instance given a class path.
 *
 * @param classpath Path to class or jar file
 * @return Call Graph
 */
public static CallGraph generateCallGraph(final String classpath)
        throws IOException, ClassHierarchyException, CallGraphBuilderCancelException {
    final var classLoader = Thread.currentThread().getContextClassLoader();
    final var exclusionFile = new File(Objects.requireNonNull(classLoader
            .getResource("Java60RegressionExclusions.txt")).getFile());

    final var scope = AnalysisScopeReader
            .makeJavaBinaryAnalysisScope(classpath, exclusionFile);

    final var cha = ClassHierarchyFactory.makeWithRoot(scope);

    final var entryPointsGenerator = new EntryPointsGenerator(cha);
    final var entryPoints = entryPointsGenerator.getEntryPoints();
    final var options = new AnalysisOptions(scope, entryPoints);
    final var cache = new AnalysisCacheImpl();

    final var builder = Util.makeZeroCFABuilder(Language.JAVA, options, cache, cha, scope);

    return builder.makeCallGraph(options, null);
}
 
Example #8
Source File: WalaResultAnalyzerTest.java    From fasten with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void setUp() throws ClassHierarchyException, CallGraphBuilderCancelException, IOException {
    var path = new File(Thread.currentThread().getContextClassLoader()
            .getResource("SingleSourceToTarget.jar")
            .getFile()).getAbsolutePath();

    graph = CallGraphConstructor.generateCallGraph(path);
}
 
Example #9
Source File: PartialCallGraphTest.java    From fasten with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void setUp() throws ClassHierarchyException, CallGraphBuilderCancelException, IOException {
    var path = Paths.get(new File(Thread.currentThread().getContextClassLoader()
            .getResource("SingleSourceToTarget.jar")
            .getFile()).getAbsolutePath());

    graph = WalaResultAnalyzer.wrap(CallGraphConstructor.generateCallGraph(path.toString()));

    type = graph.getClassHierarchy()
            .get(new FastenJavaURI("/name.space/SingleSourceToTarget"));
}
 
Example #10
Source File: ArtifactResolverTest.java    From fasten with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void setUp() throws ClassHierarchyException, CallGraphBuilderCancelException, IOException {
    jar = new File(Thread.currentThread().getContextClassLoader()
            .getResource("SingleSourceToTarget.jar")
            .getFile());

    graph = CallGraphConstructor.generateCallGraph(jar.getAbsolutePath());
}
 
Example #11
Source File: WALAPlugin.java    From fasten with Apache License 2.0 5 votes vote down vote up
@Override
public RevisionCallGraph generateCallGraph(final MavenCoordinate mavenCoordinate,
                                           final JSONObject kafkaConsumedJson)
        throws ClassHierarchyException, CallGraphBuilderCancelException, IOException {

    return PartialCallGraph.createExtendedRevisionCallGraph(mavenCoordinate,
            Long.parseLong(kafkaConsumedJson.get("date").toString()));
}
 
Example #12
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 5 votes vote down vote up
MethodParamAnnotations run(String inPaths, String pkgName, boolean includeNonPublicClasses)
    throws IOException, ClassHierarchyException, IllegalArgumentException {
  String outPath = "";
  String firstInPath = inPaths.split(",")[0];
  if (firstInPath.endsWith(".jar") || firstInPath.endsWith(".aar")) {
    outPath =
        FilenameUtils.getFullPath(firstInPath)
            + FilenameUtils.getBaseName(firstInPath)
            + ASTUBX_JAR_SUFFIX;
  } else if (new File(firstInPath).exists()) {
    outPath = FilenameUtils.getFullPath(firstInPath) + DEFAULT_ASTUBX_LOCATION;
  }
  return run(inPaths, pkgName, outPath, false, false, includeNonPublicClasses, DEBUG, VERBOSE);
}
 
Example #13
Source File: PartialCallGraph.java    From fasten with Apache License 2.0 5 votes vote down vote up
/**
 * Creates {@link RevisionCallGraph} using WALA call graph generator for a given maven
 * coordinate. It also sets the forge to "mvn".
 *
 * @param coordinate maven coordinate of the revision to be processed.
 * @param timestamp  timestamp of the revision release.
 * @return {@link RevisionCallGraph} of the given coordinate.
 * @throws FileNotFoundException in case there is no jar file for the given coordinate on the
 *                               Maven central it throws this exception.
 */
public static RevisionCallGraph createExtendedRevisionCallGraph(
        final MavenCoordinate coordinate,
        final long timestamp)
        throws IOException, ClassHierarchyException, CallGraphBuilderCancelException {

    final var partialCallGraph = CallGraphConstructor.build(coordinate);

    return new RevisionCallGraph("mvn", coordinate.getProduct(),
            coordinate.getVersionConstraint(), timestamp, "WALA",
            MavenCoordinate.MavenResolver.resolveDependencies(coordinate),
            partialCallGraph.getClassHierarchy(),
            partialCallGraph.getGraph());
}
 
Example #14
Source File: MethodTest.java    From fasten with Apache License 2.0 4 votes vote down vote up
@BeforeAll
public static void setUp() throws ClassHierarchyException, CallGraphBuilderCancelException, IOException {
    /**
     * SingleSourceToTarget:
     *
     * package name.space;
     *
     * public class SingleSourceToTarget{
     *
     *     public static void sourceMethod() { targetMethod(); }
     *
     *     public static void targetMethod() {}
     * }
     */
    var ssttpath = new File(Thread.currentThread().getContextClassLoader()
            .getResource("SingleSourceToTarget.jar")
            .getFile()).getAbsolutePath();

    ssttgraph = CallGraphConstructor.generateCallGraph(ssttpath);

    /**
     * ClassInit:
     *
     * package name.space;
     *
     * public class ClassInit{
     *
     * public static void targetMethod(){}
     *
     *     static{
     *         targetMethod();
     *     }
     * }
     *
     */
    var cipath = new File(Thread.currentThread().getContextClassLoader()
            .getResource("ClassInit.jar")
            .getFile()).getAbsolutePath();

    cigraph = CallGraphConstructor.generateCallGraph(cipath);

    /**
     * LambdaExample:
     *
     * package name.space;
     *
     * import java.util.function.Function;
     *
     * public class LambdaExample {
     *     Function f = (it) -> "Hello";
     * }
     */
    var lambdapath = new File(Thread.currentThread().getContextClassLoader()
            .getResource("LambdaExample.jar")
            .getFile()).getAbsolutePath();

    lambdagraph = CallGraphConstructor.generateCallGraph(lambdapath);

    /**
     * ArrayExample:
     *
     *package name.space;
     *
     * public class ArrayExample{
     *     public static void sourceMethod() {
     *         Object[] object = new Object[1];
     *         targetMethod(object);
     *     }
     *
     *     public static Object[] targetMethod(Object[] obj) {
     *         return obj;
     *     }
     * }
     */
    var arraypath = new File(Thread.currentThread().getContextClassLoader()
            .getResource("ArrayExample.jar")
            .getFile()).getAbsolutePath();

    arraygraph = CallGraphConstructor.generateCallGraph(arraypath);

    /**
     * Contains arrays of all primitive types including two arrays of types Integer and Object.
     */
    var aepath = new File(Thread.currentThread().getContextClassLoader()
            .getResource("ArrayExtensiveTest.jar")
            .getFile()).getAbsolutePath();

    aegraph = CallGraphConstructor.generateCallGraph(aepath);
}
 
Example #15
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
MethodParamAnnotations run(String inPaths, String pkgName)
    throws IOException, ClassHierarchyException, IllegalArgumentException {
  return run(inPaths, pkgName, false);
}
 
Example #16
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
MethodParamAnnotations runAndAnnotate(
    String inPaths, String pkgName, String outPath, boolean stripJarSignatures)
    throws IOException, ClassHierarchyException {
  return run(inPaths, pkgName, outPath, true, stripJarSignatures, false, DEBUG, VERBOSE);
}
 
Example #17
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
MethodParamAnnotations runAndAnnotate(String inPaths, String pkgName, String outPath)
    throws IOException, ClassHierarchyException {
  return runAndAnnotate(inPaths, pkgName, outPath, false);
}
 
Example #18
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
/**
 * Driver for the analysis. {@link DefinitelyDerefedParams} Usage: DefinitelyDerefedParamsDriver (
 * jar/aar_path, package_name, [output_path])
 *
 * @param inPaths Comma separated paths to input jar/aar file to be analyzed.
 * @param pkgName Qualified package name.
 * @param outPath Path to output processed jar/aar file. Default outPath for 'a/b/c/x.jar' is
 *     'a/b/c/x-ji.jar'. When 'annotatedBytecode' is enabled, this should refer to the directory
 *     that should contain all the output jars.
 * @param annotateBytecode Perform bytecode transformation
 * @param stripJarSignatures Remove jar cryptographic signatures
 * @param includeNonPublicClasses Include non-public/ABI classes (e.g. for testing)
 * @param dbg Output debug level logs
 * @param vbs Output verbose level logs
 * @return MethodParamAnnotations Map of 'method signatures' to their 'list of NonNull
 *     parameters'.
 * @throws IOException on IO error.
 * @throws ClassHierarchyException on Class Hierarchy factory error.
 * @throws IllegalArgumentException on illegal argument to WALA API.
 */
public MethodParamAnnotations run(
    String inPaths,
    String pkgName,
    String outPath,
    boolean annotateBytecode,
    boolean stripJarSignatures,
    boolean includeNonPublicClasses,
    boolean dbg,
    boolean vbs)
    throws IOException, ClassHierarchyException {
  DEBUG = dbg;
  VERBOSE = vbs;
  this.annotateBytecode = annotateBytecode;
  this.stripJarSignatures = stripJarSignatures;
  Set<String> setInPaths = new HashSet<>(Arrays.asList(inPaths.split(",")));
  analysisStartTime = System.currentTimeMillis();
  for (String inPath : setInPaths) {
    analyzeFile(pkgName, inPath, includeNonPublicClasses);
    if (this.annotateBytecode) {
      String outFile = outPath;
      if (setInPaths.size() > 1) {
        outFile =
            outPath
                + "/"
                + FilenameUtils.getBaseName(inPath)
                + "-annotated."
                + FilenameUtils.getExtension(inPath);
      }
      writeAnnotations(inPath, outFile);
    }
  }
  if (!this.annotateBytecode) {
    new File(outPath).getParentFile().mkdirs();
    if (outPath.endsWith(".astubx")) {
      writeModel(new DataOutputStream(new FileOutputStream(outPath)));
    } else {
      writeModelJAR(outPath);
    }
  }
  lastOutPath = outPath;
  return nonnullParams;
}
 
Example #19
Source File: DefinitelyDerefedParamsDriver.java    From NullAway with MIT License 4 votes vote down vote up
private void analyzeFile(String pkgName, String inPath, boolean includeNonPublicClasses)
    throws IOException, ClassHierarchyException {
  InputStream jarIS = null;
  if (inPath.endsWith(".jar") || inPath.endsWith(".aar")) {
    jarIS = getInputStream(inPath);
    if (jarIS == null) {
      return;
    }
  } else if (!new File(inPath).exists()) {
    return;
  }
  AnalysisScope scope = AnalysisScopeReader.makePrimordialScope(null);
  scope.setExclusions(
      new FileOfClasses(
          new ByteArrayInputStream(DEFAULT_EXCLUSIONS.getBytes(StandardCharsets.UTF_8))));
  if (jarIS != null) scope.addInputStreamForJarToScope(ClassLoaderReference.Application, jarIS);
  else AnalysisScopeReader.addClassPathToScope(inPath, scope, ClassLoaderReference.Application);
  AnalysisOptions options = new AnalysisOptions(scope, null);
  AnalysisCache cache = new AnalysisCacheImpl();
  IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope);
  Warnings.clear();

  // Iterate over all classes:methods in the 'Application' and 'Extension' class loaders
  for (IClassLoader cldr : cha.getLoaders()) {
    if (!cldr.getName().toString().equals("Primordial")) {
      for (IClass cls : Iterator2Iterable.make(cldr.iterateAllClasses())) {
        if (cls instanceof PhantomClass) continue;
        // Only process classes in specified classpath and not its dependencies.
        // TODO: figure the right way to do this
        if (!pkgName.isEmpty() && !cls.getName().toString().startsWith(pkgName)) continue;
        // Skip non-public / ABI classes
        if (!cls.isPublic() && !includeNonPublicClasses) continue;
        LOG(DEBUG, "DEBUG", "analyzing class: " + cls.getName().toString());
        for (IMethod mtd : Iterator2Iterable.make(cls.getDeclaredMethods().iterator())) {
          // Skip methods without parameters, abstract methods, native methods
          // some Application classes are Primordial (why?)
          if (shouldCheckMethod(mtd)) {
            Preconditions.checkNotNull(mtd, "method not found");
            DefinitelyDerefedParams analysisDriver = null;
            String sign = "";
            // Parameter analysis
            if (mtd.getNumberOfParameters() > (mtd.isStatic() ? 0 : 1)) {
              // Skip methods by looking at bytecode
              try {
                if (!CodeScanner.getFieldsRead(mtd).isEmpty()
                    || !CodeScanner.getFieldsWritten(mtd).isEmpty()
                    || !CodeScanner.getCallSites(mtd).isEmpty()) {
                  analysisDriver = getAnalysisDriver(mtd, options, cache);
                  Set<Integer> result = analysisDriver.analyze();
                  sign = getSignature(mtd);
                  LOG(DEBUG, "DEBUG", "analyzed method: " + sign);
                  if (!result.isEmpty() || DEBUG) {
                    nonnullParams.put(sign, result);
                    LOG(
                        DEBUG,
                        "DEBUG",
                        "Inferred Nonnull param for method: " + sign + " = " + result.toString());
                  }
                }
              } catch (Exception e) {
                LOG(
                    DEBUG,
                    "DEBUG",
                    "Exception while scanning bytecodes for " + mtd + " " + e.getMessage());
              }
            }
            analyzeReturnValue(options, cache, mtd, analysisDriver, sign);
          }
        }
      }
    }
  }
  long endTime = System.currentTimeMillis();
  LOG(
      VERBOSE,
      "Stats",
      inPath
          + " >> time(ms): "
          + (endTime - analysisStartTime)
          + ", bytecode size: "
          + analyzedBytes
          + ", rate (ms/KB): "
          + (analyzedBytes > 0 ? (((endTime - analysisStartTime) * 1000) / analyzedBytes) : 0));
}
 
Example #20
Source File: LibraryProfiler.java    From LibScout with Apache License 2.0 4 votes vote down vote up
public static void extractFingerPrints(File libraryFile, File libDescriptionFile) throws ParserConfigurationException, SAXException, IOException, ParseException, ClassHierarchyException, ClassNotFoundException {
	new LibraryProfiler(libraryFile,libDescriptionFile).extractFingerPrints();
}
 
Example #21
Source File: LibraryProfiler.java    From LibScout with Apache License 2.0 4 votes vote down vote up
private void extractFingerPrints() throws IOException, ClassHierarchyException, ClassNotFoundException {
	long starttime = System.currentTimeMillis();
	
	logger.info("Process library: " + libraryFile.getName());
	logger.info("Library description:");
	for (String desc: libDesc.getDescription())
		logger.info(desc);
	
	// create analysis scope and generate class hierarchy
	final AnalysisScope scope = AnalysisScope.createJavaAnalysisScope();
	
	JarFile jf = libraryFile.getName().endsWith(".aar")? new AarFile(libraryFile).getJarFile() : new JarFile(libraryFile); 
	scope.addToScope(ClassLoaderReference.Application, jf);
	scope.addToScope(ClassLoaderReference.Primordial, new JarFile(LibScoutConfig.pathToAndroidJar));

	IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope);
	WalaUtils.getChaStats(cha);
	
	// cleanup tmp files if library input was an .aar file
	if (libraryFile.getName().endsWith(".aar")) {
		File tmpJar = new File(jf.getName());
		tmpJar.delete();
		logger.debug(Utils.indent() + "tmp jar-file deleted at " + tmpJar.getName());
	}
	
	PackageTree pTree = Profile.generatePackageTree(cha);
	if (pTree.getRootPackage() == null) {
		logger.warn(Utils.INDENT + "Library contains multiple root packages");
	}

	List<HashTree> hTrees = Profile.generateHashTrees(cha);

	// if hash tree is empty do not dump a profile
	if (hTrees.isEmpty() || hTrees.get(0).getNumberOfClasses() == 0) {
		logger.error("Empty Hash Tree generated - SKIP");
		return;
	}

	// write profile to disk
	serialize(pTree, hTrees);

	logger.info("");
	logger.info("Processing time: " + Utils.millisecondsToFormattedTime(System.currentTimeMillis() - starttime));
}
 
Example #22
Source File: LibraryIdentifier.java    From LibScout with Apache License 2.0 4 votes vote down vote up
public static AppStats run(File appFile, List<LibProfile> profiles, boolean libUsageAnalysis) throws ClassHierarchyException, NoSuchAlgorithmException, IOException {
 LibraryIdentifier libid = new LibraryIdentifier(appFile);
 return libid.identifyLibraries(profiles, libUsageAnalysis);
}