Skip to content

Commit 26f1a4d

Browse files
ftcmbenz89
authored andcommitted
Fix Null Pointer Exception in EnhancedUnitGraph soot-oss#618
1 parent 14cc0b6 commit 26f1a4d

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

src/main/java/soot/toolkits/graph/pdg/EnhancedUnitGraph.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import soot.jimple.ThrowStmt;
3535
import soot.jimple.internal.JNopStmt;
3636
import soot.toolkits.graph.DominatorNode;
37-
import soot.toolkits.graph.MHGDominatorsFinder;
3837
import soot.toolkits.graph.MHGPostDominatorsFinder;
38+
import soot.toolkits.graph.MHGDominatorsFinder;
3939
import soot.toolkits.graph.UnitGraph;
4040
import soot.util.Chain;
4141

@@ -74,6 +74,11 @@ public EnhancedUnitGraph(Body body) {
7474
unitToSuccs = new HashMap<Unit, List<Unit>>(size * 2 + 1, 0.7f);
7575
unitToPreds = new HashMap<Unit, List<Unit>>(size * 2 + 1, 0.7f);
7676

77+
// Initialize all units in the unitToSuccs and unitsToPreds
78+
for(Unit u : body.getUnits()){
79+
unitToSuccs.put(u,new ArrayList<>());
80+
unitToPreds.put(u,new ArrayList<>());
81+
}
7782
/*
7883
* Compute the head and tails at each phase because other phases might rely on them.
7984
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package soot.toolkits.graph;
2+
import org.junit.BeforeClass;
3+
import org.junit.Test;
4+
import soot.*;
5+
import soot.options.Options;
6+
import soot.toolkits.graph.pdg.EnhancedUnitGraph;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class EnhancedUnitGraphTest {
14+
private static EnhancedUnitGraphTestUtility testUtility;
15+
16+
private static String TARGET_CLASS = "soot.toolkits.graph.targets.TestException";
17+
@BeforeClass
18+
public static void setUp() throws IOException {
19+
G.reset();
20+
List<String> processDir = new ArrayList<>();
21+
File f = new File("./target/test-classes");
22+
if (f.exists()) {
23+
processDir.add(f.getCanonicalPath());
24+
}
25+
Options.v().set_process_dir(processDir);
26+
27+
Options.v().set_src_prec(Options.src_prec_only_class);
28+
Options.v().set_allow_phantom_refs(true);
29+
Options.v().set_output_format(Options.output_format_none);
30+
Scene.v().addBasicClass(TARGET_CLASS);
31+
Scene.v().loadNecessaryClasses();
32+
Options.v().set_prepend_classpath(true);
33+
Scene.v().forceResolve("soot.toolkits.graph.targets.TestException", SootClass.BODIES);
34+
testUtility = new EnhancedUnitGraphTestUtility();
35+
PackManager.v().getPack("jtp")
36+
.add(new Transform("jtp.TestEnhancedGraphUtility", testUtility));
37+
PackManager.v().runPacks();
38+
}
39+
40+
@Test
41+
public void exceptionIsReachable(){
42+
43+
EnhancedUnitGraph unitGraph = testUtility.getUnitGraph();
44+
UnitPatchingChain allUnits = unitGraph.body.getUnits();
45+
46+
int targetUnitsFound = 0;
47+
for(Unit u : allUnits){
48+
if(u.toString().contains("@caughtexception")){
49+
assert(unitGraph.unitToPreds.get(u).size() > 0);
50+
++targetUnitsFound;
51+
}
52+
}
53+
assert(targetUnitsFound == 2);
54+
}
55+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package soot.toolkits.graph;
2+
3+
import soot.Body;
4+
import soot.BodyTransformer;
5+
import soot.toolkits.graph.pdg.EnhancedUnitGraph;
6+
7+
public class EnhancedUnitGraphTestUtility extends BodyTransformer {
8+
private EnhancedUnitGraph unitGraph = null;
9+
protected void internalTransform(Body body, String phase, java.util.Map<String,String> options)
10+
{
11+
String methodSig = body.getMethod().getSignature();
12+
if(methodSig.contains("soot.toolkits.graph.targets.TestException")
13+
&& body.getMethod().getName().contains("main"))
14+
unitGraph = new EnhancedUnitGraph(body);
15+
}
16+
17+
public EnhancedUnitGraph getUnitGraph() {
18+
return unitGraph;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package soot.toolkits.graph.targets;
2+
3+
public class TestException {
4+
5+
public static void main(String[] args) {
6+
int a = 0;
7+
int b = 0;
8+
9+
try {
10+
a = a / b;
11+
}
12+
catch (Exception e) {
13+
a = Math.abs(-1);
14+
}
15+
finally {
16+
b = 0;
17+
}
18+
}
19+
20+
}

0 commit comments

Comments
 (0)