Skip to content

Commit

Permalink
Merge pull request #593 from mayaba/fix-issue-588
Browse files Browse the repository at this point in the history
Fix issue #588: Support for JDK17 in Reachability Analysis using WALA
  • Loading branch information
henrikplate authored Aug 26, 2023
2 parents 8565716 + 52dc457 commit a0e1e19
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;
import java.util.Set;

import com.ibm.wala.classLoader.Language;
import org.apache.commons.configuration.Configuration;
import org.apache.logging.log4j.Logger;
import org.eclipse.steady.cg.CallgraphConstructException;
Expand Down Expand Up @@ -53,7 +54,7 @@
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.core.util.config.AnalysisScopeReader;
import com.ibm.wala.util.graph.Graph;
import com.ibm.wala.util.graph.impl.SlowSparseNumberedGraph;

Expand Down Expand Up @@ -145,13 +146,13 @@ public void setDepClasspath(String _dependenciesClasspath) {
/**
* {@inheritDoc}
*
* Filter and find all entrypoints in scope
* <p>Filter and find all entrypoints in scope
*/
public void setEntrypoints(Set<org.eclipse.steady.shared.json.model.ConstructId> _constructs)
throws CallgraphConstructException {
try {
this.scope =
AnalysisScopeReader.makeJavaBinaryAnalysisScope(
AnalysisScopeReader.instance.makeJavaBinaryAnalysisScope(
this.classpath, this.excludedPackagesFile);

// The removal of ClassHierarchy.make(AnalysisScope) was made with commit
Expand Down Expand Up @@ -207,13 +208,11 @@ public void setEntrypoints(Set<org.eclipse.steady.shared.json.model.ConstructId>
&& (cha.getScope()
.getApplicationLoader()
.equals(klass.getClassLoader().getReference()))) {
for (Iterator<IMethod> m_iter = klass.getDeclaredMethods().iterator();
m_iter.hasNext(); ) {
method = (IMethod) m_iter.next();
if (!method.isClinit()) {
method_qname = getCid(method).getQname();
if (!method.isAbstract() && (_constructs_qname.contains(method_qname))) {
ep.add(new ArgumentTypeEntrypoint(method, cha));
for (IMethod declaredMethod : klass.getDeclaredMethods()) {
if (!declaredMethod.isClinit()) {
method_qname = getCid(declaredMethod).getQname();
if (!declaredMethod.isAbstract() && (_constructs_qname.contains(method_qname))) {
ep.add(new ArgumentTypeEntrypoint(declaredMethod, cha));
}
}
}
Expand Down Expand Up @@ -334,7 +333,7 @@ public Configuration getConstructorConfiguration() {
/**
* {@inheritDoc}
*
* Parse command line arguments, and then build callgraph based on these properties
* <p>Parse command line arguments, and then build callgraph based on these properties
*/
public void buildCallgraph(boolean _policy) throws CallgraphConstructException {
WalaCallgraphConstructor.log.info(
Expand Down Expand Up @@ -374,17 +373,18 @@ public void buildCallgraph(boolean _policy) throws CallgraphConstructException {
if (cg_algorithm.equals("RTA")) {
builder = Util.makeRTABuilder(options, cache, this.cha, this.scope);
} else if (cg_algorithm.equals("0-CFA")) {
builder = Util.makeZeroCFABuilder(options, cache, this.cha, this.scope);
builder = Util.makeZeroCFABuilder(Language.JAVA, options, cache, this.cha, this.scope);
} else if (cg_algorithm.equals("0-ctn-CFA")) {
builder = Util.makeZeroContainerCFABuilder(options, cache, this.cha, this.scope);
} else if (cg_algorithm.equals("vanilla-0-1-CFA")) {
builder = Util.makeVanillaZeroOneCFABuilder(options, cache, this.cha, this.scope);
builder =
Util.makeVanillaZeroOneCFABuilder(Language.JAVA, options, cache, this.cha, this.scope);
} else if (cg_algorithm.equals("0-1-CFA")) {
builder = Util.makeZeroOneCFABuilder(options, cache, this.cha, this.scope);
builder = Util.makeZeroOneCFABuilder(Language.JAVA, options, cache, this.cha, this.scope);
} else if (cg_algorithm.equals("0-1-ctn-CFA")) {
builder = Util.makeZeroOneContainerCFABuilder(options, cache, this.cha, this.scope);
} else {
builder = Util.makeZeroOneCFABuilder(options, cache, this.cha, this.scope);
builder = Util.makeZeroOneCFABuilder(Language.JAVA, options, cache, this.cha, this.scope);
}

// Build callgraph based on options and algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,41 @@ public void examplesWalaTest() {
e.printStackTrace();
}
}

@Test
public void examplesWalaTestJdk17() {
final ReachabilityAnalyzer ra = new ReachabilityAnalyzer(this.getGoalContext());
ra.setCallgraphConstructor(WalaCallgraphConstructor.FRAMEWORK, false);

// Set classpaths
final Set<Path> app_paths = new HashSet<Path>(), dep_paths = new HashSet<Path>();
app_paths.add(Paths.get("./src/test/resources/examplesJdk17.jar"));
dep_paths.add(Paths.get("./src/test/resources/empty.jar"));
ra.setAppClasspaths(app_paths);
ra.setDependencyClasspaths(dep_paths);

// Set the EP manually
final Set<ConstructId> entrypoint = new HashSet<ConstructId>();
entrypoint.add(
JavaId.toSharedType(JavaId.parseMethodQName("org.example.ExamplesJdk17.main(String[])")));
ra.setEntryPoints(entrypoint, PathSource.A2C, false);
ra.setAppConstructs(entrypoint);

// Set the target constructs (manually, rather than using a bug)
final Map<String, Set<ConstructId>> target_constructs = new HashMap<String, Set<ConstructId>>();
final Set<ConstructId> changes = new HashSet<ConstructId>();
changes.add(JavaId.toSharedType(JavaId.parseMethodQName("org.example.Cat.saySomething()")));
changes.add(JavaId.toSharedType(JavaId.parseMethodQName("org.example.Fish.saySomething()")));
changes.add(JavaId.toSharedType(JavaId.parseMethodQName("org.example.Dog.saySomething()")));
changes.add(JavaId.toSharedType(JavaId.parseMethodQName("org.example.Car.saySomething()")));
target_constructs.put("does-not-exist", changes);
ra.setTargetConstructs(target_constructs);

try {
ReachabilityAnalyzer.startAnalysis(ra, 600000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
80 changes: 80 additions & 0 deletions lang-java-reach-wala/src/test/resources/ExamplesJdk17.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* This file is part of Eclipse Steady.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Copyright (c) 2018-2020 SAP SE or an SAP affiliate company and Eclipse Steady contributors
*/
package org.example;

import java.util.SortedSet;
import java.util.TreeSet;

sealed abstract class Animal implements Comparable<Animal> permits Cat, Dog, Fish {

public abstract void saySomething();

public int compareTo(Animal _a) {
return getClass().getName().compareTo(_a.getClass().getName());
}
}

final class Cat extends Animal {
public void saySomething() {
System.out.println("purr");
}
}

final class Dog extends Animal {
public void saySomething() {
System.out.println("woof");
}
}

final class Fish extends Animal {
public void saySomething() {
System.out.println("...");
}
}

class Car {
public void saySomething() {
System.out.println("honk!");
}
}

public class ExamplesJdk17 {
static SortedSet<Animal> animals = new TreeSet<>();

private static Animal createFish() {
return new Fish();
}

private static Animal createCat() {
Animal cat = new Cat();
animals.add(cat);
return cat;
}

public static void main(String[] args) {
Animal animal;
if (args.length == 0) {
animal = createCat();
animal.saySomething();
} else {
animal = createFish();
animal.saySomething();
}
}
}
Binary file not shown.
10 changes: 5 additions & 5 deletions lang-java-reach/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<properties>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.steady</groupId>
Expand All @@ -51,17 +51,17 @@
<dependency>
<groupId>com.ibm.wala</groupId>
<artifactId>com.ibm.wala.core</artifactId>
<version>1.4.3</version>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.ibm.wala</groupId>
<artifactId>com.ibm.wala.util</artifactId>
<version>1.4.3</version>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>com.ibm.wala</groupId>
<artifactId>com.ibm.wala.shrike</artifactId>
<version>1.4.3</version>
<version>1.6.2</version>
</dependency>

<!-- Test -->
Expand All @@ -80,7 +80,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>

<!-- Increase memory for Junit test case execution -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
2 changes: 1 addition & 1 deletion plugin-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.2</version>
<version>3.9.0</version>
<configuration>
<goalPrefix>steady</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
Expand Down

0 comments on commit a0e1e19

Please sign in to comment.