Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Call Graph Generation to match fuzz-introspector need. #543

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frontends/java/wala/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<artifactId>com.ibm.wala.scandroid</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
11 changes: 10 additions & 1 deletion frontends/java/wala/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

# Process arguments
JARFILE=
ENTRYCLASS=

Expand Down Expand Up @@ -33,5 +34,13 @@ then
exit 1
fi

# Prepare scope file
cat > cg.scope << EOF
Primordial,Java,stdlib,none
Primordial,Java,jarFile,primordial.jar.model
Application,Java,jarFile,$JARFILE
EOF

# Build and execute the call graph generator
mvn clean package
java -jar target/ossf.fuzz.introspector.wala-1.0.jar -jarFile $JARFILE -entryClass $ENTRYCLASS
java -Xmx6144M -jar target/ossf.fuzz.introspector.wala-1.0.jar -jarFile $JARFILE -entryClass $ENTRYCLASS
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@

import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import com.ibm.wala.classLoader.CallSiteReference;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.core.util.config.AnalysisScopeReader;
import com.ibm.wala.core.util.strings.StringStuff;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
Expand All @@ -35,55 +31,52 @@
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.callgraph.CallGraphBuilderCancelException;
import com.ibm.wala.ipa.callgraph.Entrypoint;
import com.ibm.wala.ipa.callgraph.impl.DefaultEntrypoint;
import com.ibm.wala.ipa.callgraph.CallGraphStats;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.ssa.ISSABasicBlock;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.io.CommandLine;

public class JarCallGraphGenerator {
public static void main(String[] args) throws IOException, ClassHierarchyException, IllegalArgumentException,
CallGraphBuilderCancelException {
// Handle basic parameter
Properties p = CommandLine.parse(args);
String jarFile = p.getProperty("jarFile");
String entryClass = p.getProperty("entryClass");
if (jarFile == null || entryClass == null) {
return;
}

System.out.println("jarFile: " + FileSystems.getDefault().getPath(jarFile).toAbsolutePath());
System.out.println("entryClass: " + entryClass);

AnalysisScope scope = AnalysisScopeReader.instance.makeJavaBinaryAnalysisScope(jarFile, null);
// Setup basic analysing scope
AnalysisScope scope = AnalysisScopeReader.instance.readJavaScope("cg.scope", null, ClassLoader.getSystemClassLoader());
IClassHierarchy ch = ClassHierarchyFactory.make(scope);

List<Entrypoint> entryPoints = new ArrayList<>();
IClass ic = ch.lookupClass(
TypeReference.findOrCreate(ClassLoaderReference.Application,
StringStuff.deployment2CanonicalTypeString(entryClass)));
if (ic != null) {
for (IMethod m : ic.getDeclaredMethods()) {
if (m.getSelector().getName().toString().equals("fuzzerTestOneInput")) {
entryPoints.add(new DefaultEntrypoint(m, ch));
}
}
}
//TODO: Attempt to search for target method as entry point, still have some bugs to solve
// List<Entrypoint> entryPoints = new ArrayList<Entrypoint>();
// IClass klass = ch.lookupClass(TypeReference.findOrCreate(ClassLoaderReference.Application,
// StringStuff.deployment2CanonicalTypeString(entryClass)));
// for (IMethod method : klass.getDeclaredMethods()) {
// System.out.println(method);
// }

// Build up Call Graph
AnalysisOptions opts = new AnalysisOptions(scope, Util.makeMainEntrypoints(ch));
System.out.println(opts.getAnalysisScope());

AnalysisCache cache = new AnalysisCacheImpl();
CallGraphBuilder<InstanceKey> builder = Util.makeZeroOneContainerCFABuilder(opts, cache, ch);
CallGraph cg = builder.makeCallGraph(opts, null);
//System.out.println(cg);

CGNode root = cg.getFakeRootNode();
int nodeCount = cg.getNumberOfNodes();

System.out.println("Root Node: " + root);
System.out.println("Total node count: " + nodeCount);
for (int i=0; i<nodeCount; i++) {
// Print call graph data for verification (or further processing)
for (int i=0; i<cg.getNumberOfNodes(); i++) {
CGNode baseNode = cg.getNode(i);
IMethod method = baseNode.getMethod();
System.out.println("Base Node #" + i + ": " + baseNode);
Expand All @@ -103,5 +96,6 @@ public static void main(String[] args) throws IOException, ClassHierarchyExcepti
System.out.println("\n\n\n");
}
}
System.out.println(CallGraphStats.getStats(cg));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2022 Fuzz Introspector Authors
//
// 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.
///////////////////////////////////////////////////////////////////////////

package ossf.fuzz.introspector.wala;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.util.Properties;
import java.util.jar.JarFile;

import org.apache.commons.io.FileUtils;

import com.ibm.wala.cast.ir.ssa.AstIRFactory;
import com.ibm.wala.cast.java.client.impl.ZeroOneContainerCFABuilderFactory;
import com.ibm.wala.cast.java.ipa.callgraph.JavaSourceAnalysisScope;
import com.ibm.wala.cast.java.translator.jdt.ecj.ECJClassLoaderFactory;
import com.ibm.wala.core.util.warnings.Warnings;
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisOptions.ReflectionOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.callgraph.CallGraphBuilderCancelException;
import com.ibm.wala.ipa.callgraph.IAnalysisCacheView;
import com.ibm.wala.ipa.callgraph.impl.Util;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.properties.WalaProperties;
import com.ibm.wala.ssa.SymbolTable;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.util.io.CommandLine;

public class JavaSourceCallGraphGenerator {
public static void main(String[] args)
throws IOException, ClassHierarchyException, IllegalArgumentException,CallGraphBuilderCancelException {
// Handle basic parameter
Properties p = CommandLine.parse(args);
String sourceDir = p.getProperty("sourceDir");
if (sourceDir == null) {
System.err.println("No sourceDir specified");
return;
}

System.out.println("sourceDir: " + FileSystems.getDefault().getPath(sourceDir).toAbsolutePath());

// Setup basic analysing scope
AnalysisScope scope = new JavaSourceAnalysisScope();

// Add stdlibs of java
for (String stdlib : WalaProperties.getJ2SEJarFiles()) {
System.out.println(stdlib);
scope.addToScope(ClassLoaderReference.Primordial, new JarFile(stdlib));
}

// Add target application source
File root = new File(sourceDir);
if (root.isDirectory()) {
for (File file:FileUtils.listFiles(root, new String[] {"class"}, true)) {
scope.addSourceFileToScope(JavaSourceAnalysisScope.SOURCE, file, file.getName());
}
} else {
System.err.println("Invalid sourceDir specified");
return;
}

// Set class hierarchy and options
IClassHierarchy ch = ClassHierarchyFactory.make(scope, new ECJClassLoaderFactory(scope.getExclusions()));
System.out.println(ch.getNumberOfClasses() + " classes");
System.out.println(Warnings.asString());
Warnings.clear();

AnalysisOptions opts = new AnalysisOptions();
opts.setEntrypoints(Util.makeMainEntrypoints(JavaSourceAnalysisScope.SOURCE, ch, new String[] {"LTestFuzzer"}));
opts.getSSAOptions().setDefaultValues(SymbolTable::getDefaultValue);
opts.setReflectionOptions(ReflectionOptions.NONE);

// Build up Call Graph
IAnalysisCacheView cache =
new AnalysisCacheImpl(AstIRFactory.makeDefaultFactory(), opts.getSSAOptions());
CallGraphBuilder<?> builder = new ZeroOneContainerCFABuilderFactory().make(opts, cache, ch);
System.out.println("building call graph...");
CallGraph cg = builder.makeCallGraph(opts, null);
System.out.println(cg);

// // Print call graph data for verification (or further processing)
// for (int i=0; i<cg.getNumberOfNodes(); i++) {
// CGNode baseNode = cg.getNode(i);
// IMethod method = baseNode.getMethod();
// System.out.println("Base Node #" + i + ": " + baseNode);
// System.out.println("->Context: " + baseNode.getContext());
// System.out.println("->Method: " + method.getSignature() + ":" + method.getLineNumber(0) + "\n");
// Iterator<CallSiteReference> it = baseNode.iterateCallSites();
// while(it.hasNext()) {
// System.out.println(it.next());
// }
// System.out.println("\n");
//
// if (baseNode.getIR() != null) {
// Iterator<ISSABasicBlock> it2 = baseNode.getIR().getBlocks() ;
// while(it2.hasNext()) {
// System.out.println(it2.next());
// }
// System.out.println("\n\n\n");
// }
// }
//
// System.out.println(CallGraphStats.getStats(cg));
}
}