Skip to content

Commit

Permalink
try not using URLClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandolby committed Nov 12, 2014
1 parent 339de95 commit 71f16d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.JarFile;

import org.junit.Test;

Expand All @@ -31,16 +32,25 @@
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.shrikeBT.analysis.Analyzer.FailureException;
import com.ibm.wala.shrikeCT.InvalidClassFileException;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.util.CancelException;
import com.ibm.wala.util.Predicate;
import com.ibm.wala.util.io.TemporaryFile;

public class Java7CallGraphTest extends DynamicCallGraphTestBase {

@Test public void testOcamlHelloHash() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {

AnalysisScope scope = CallGraphTestUtil.makeJ2SEAnalysisScope("ocaml_hello_hash.txt", CallGraphTestUtil.REGRESSION_EXCLUSIONS);

@Test public void testOcamlHelloHash() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InterruptedException {
testOCamlJar("hello_hash.jar");
}

private void testOCamlJar(String jarFile, String... args) throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InterruptedException {
File F = TemporaryFile.urlToFile(jarFile.replace('.', '_') + ".jar", getClass().getClassLoader().getResource(jarFile));
F.deleteOnExit();

AnalysisScope scope = CallGraphTestUtil.makeJ2SEAnalysisScope("base.txt", CallGraphTestUtil.REGRESSION_EXCLUSIONS);
scope.addToScope(ClassLoaderReference.Application, new JarFile(F));

ClassHierarchy cha = ClassHierarchy.make(scope);
Iterable<Entrypoint> entrypoints = com.ibm.wala.ipa.callgraph.impl.Util.makeMainEntrypoints(scope, cha, "Lpack/ocamljavaMain");
AnalysisOptions options = CallGraphTestUtil.makeAnalysisOptions(scope, entrypoints);
Expand All @@ -53,10 +63,8 @@ public class Java7CallGraphTest extends DynamicCallGraphTestBase {

CallGraph cg = builder.makeCallGraph(options, null);

File F = TemporaryFile.urlToFile("hello_hash_test_jar.jar", getClass().getClassLoader().getResource("hello_hash.jar"));
F.deleteOnExit();
instrument(F.getAbsolutePath());
run("pack.ocamljavaMain", null);
run("pack.ocamljavaMain", null, args);

checkNodes(cg, new Predicate<MethodReference>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ private CallGraph staticCG(String exclusionsFile) throws IOException, ClassHiera
}

@Test
public void testGraph() throws IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassHierarchyException, CancelException {
public void testGraph() throws IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassHierarchyException, CancelException, InterruptedException {
instrument(testJarLocation);
run(testMain, null);
CallGraph staticCG = staticCG(null);
checkEdges(staticCG);
}

@Test
public void testExclusions() throws IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassHierarchyException, CancelException {
public void testExclusions() throws IOException, ClassNotFoundException, InvalidClassFileException, FailureException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassHierarchyException, CancelException, InterruptedException {
instrument(testJarLocation);
run(testMain, "ShrikeTestExclusions.txt");
CallGraph staticCG = staticCG("ShrikeTestExclusions.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Path;
import org.junit.Assert;

import com.ibm.wala.core.tests.util.WalaTestCase;
Expand Down Expand Up @@ -89,46 +92,38 @@ protected void instrument(String testJarLocation) throws IOException, ClassNotFo
}
}

protected void run(String mainClass, String exclusionsFile, String... args) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
String shrikeBin = getClasspathEntry("com.ibm.wala.shrike");
String utilBin = getClasspathEntry("com.ibm.wala.util");
URLClassLoader jcl = new URLClassLoader(new URL[]{ new URL("file://" + instrumentedJarLocation), new URL("file://" + shrikeBin), new URL("file://" + utilBin) }, DynamicCallGraphTestBase.class.getClassLoader().getParent());

Class<?> testClass = jcl.loadClass(mainClass);
Assert.assertNotNull(testClass);
Method testMain = testClass.getDeclaredMethod("main", String[].class);
Assert.assertNotNull(testMain);
protected void run(String mainClass, String exclusionsFile, String... args) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InterruptedException {
Project p = new Project();
p.setBaseDir(new File(System.getProperty("java.io.tmpdir")));
p.init();
p.fireBuildStarted();

Java childJvm = new Java();
childJvm.setTaskName("test_" + mainClass.replace('.', '_'));
childJvm.setClasspath(new Path(p, getClasspathEntry("com.ibm.wala.shrike") + ":" + getClasspathEntry("com.ibm.wala.util") + ":" + instrumentedJarLocation));
childJvm.setClassname(mainClass);

System.setProperty("dynamicCGFile", cgLocation);
System.setProperty("dynamicCGHandleMissing", "true");
String jvmArgs = "-DdynamicCGFile=" + cgLocation + " -DdynamicCGHandleMissing=true";
if (exclusionsFile != null) {
File tmpFile = TemporaryFile.urlToFile("exclusions.txt", getClass().getClassLoader().getResource(exclusionsFile));
System.setProperty("dynamicCGFilter", tmpFile.getCanonicalPath());
jvmArgs += " -DdynamicCGFilter=" + tmpFile.getCanonicalPath();
}
try {
testMain.invoke(null, args==null? new Object[0]: new Object[]{args});
} catch (Throwable e) {
// exceptions here are from the instrumented program
// this is fine, since we are collecting its call graph
// and exceptions are possible behavior.

// well, most errors are fine. On the other hand, low-level
// class loading errors likely indicate a bug in instrumentation,
// which is often tested with this test.
while (e.getCause() != null) {
Assert.assertFalse(String.valueOf(e.getCause()), e.getCause() instanceof LinkageError);
e = e.getCause();
}
childJvm.setJvmargs(jvmArgs);

StringBuffer argsStr = new StringBuffer();
for(String a : args) {
argsStr.append(a).append(" ");
}
childJvm.setArgs(argsStr.toString());

// the VM is not exiting, so stop tracing explicitly
Class<?> runtimeClass = jcl.loadClass("com.ibm.wala.shrike.cg.Runtime");
Assert.assertNotNull(runtimeClass);
Method endTrace = runtimeClass.getDeclaredMethod("endTrace");
Assert.assertNotNull(endTrace);
endTrace.invoke(null);

Assert.assertTrue("expected to create call graph", new File(System.getProperty("dynamicCGFile")).exists());
childJvm.setFailonerror(true);
childJvm.setFork(true);

childJvm.init();
Process x = Runtime.getRuntime().exec(childJvm.getCommandLine().toString());
x.waitFor();
Assert.assertTrue("expected to create call graph", new File(cgLocation).exists());
}

interface EdgesTest {
Expand All @@ -144,8 +139,8 @@ protected void checkEdges(CallGraph staticCG) throws IOException {
}

protected void checkEdges(CallGraph staticCG, Predicate<MethodReference> filter) throws IOException {
final Set<Pair<CGNode,CGNode>> edges = HashSetFactory.make();
check(staticCG, new EdgesTest() {
private final Set<Pair<CGNode,CGNode>> edges = HashSetFactory.make();
@Override
public void edgesTest(CallGraph staticCG, CGNode caller, MethodReference calleeRef) {
if (! calleeRef.getName().equals(MethodReference.clinitName)) {
Expand Down Expand Up @@ -186,7 +181,7 @@ public void edgesTest(CallGraph staticCG, CGNode caller, MethodReference callee)
}

protected void check(CallGraph staticCG, EdgesTest test, Predicate<MethodReference> filter) throws IOException {
BufferedReader dynamicEdgesFile = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(System.getProperty("dynamicCGFile")))));
BufferedReader dynamicEdgesFile = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(cgLocation))));
String line;
int lines = 0;
loop: while ((line = dynamicEdgesFile.readLine()) != null) {
Expand Down

0 comments on commit 71f16d4

Please sign in to comment.