soot-oss / SootUp

A new version of Soot with a completely overhauled architecture

Home Page:https://soot-oss.github.io/SootUp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fix BlockGraphIteratorAndTrapAggregator to handle traps closing on the last iterated block

paganma opened this issue · comments

I've encountered a problem while analyzing the following Java 8 code:

public class TryWithResourcesFinally {

	public static void nop() {}

	public void test0(final AutoCloseable ac0) throws Exception {
		try (AutoCloseable ac1 = ac0) {
			nop();
		} finally {
			throw new Exception();
		}
	}
}

This code generates a block graph whose last element (in topological order) closes a trap. For the example above this corresponds to block 21 in the corresponding graph.

Retrieving the traps of this block graph using sootup.core.graph.StmtGraph$BlockGraphIteratorAndTrapAggregator will result in the error:

java.lang.IllegalArgumentException: Invalid StmtGraph. A Trap is not created as a traps endStmt was not visited during the iteration of all Stmts.

	at sootup.core.graph.StmtGraph$BlockGraphIteratorAndTrapAggregator.getTraps(StmtGraph.java:505)
	at sootup.core.graph.MutableBlockStmtGraph.getTraps(MutableBlockStmtGraph.java:1495)
	at sootup.core.graph.ForwardingStmtGraph.getTraps(ForwardingStmtGraph.java:136)
	at sootup.core.graph.StmtGraph.getLabeledStmts(StmtGraph.java:718)
	at sootup.core.util.printer.LabeledStmtPrinter.getStmts(LabeledStmtPrinter.java:116)
	at sootup.core.util.printer.LabeledStmtPrinter.initializeSootMethod(LabeledStmtPrinter.java:110)
	at sootup.core.util.printer.JimplePrinter.printStmts(JimplePrinter.java:327)
	at sootup.core.util.printer.JimplePrinter.printStatementsInBody(JimplePrinter.java:323)
	at sootup.core.util.printer.JimplePrinter.printBody(JimplePrinter.java:310)
	at sootup.core.util.printer.JimplePrinter.printTo(JimplePrinter.java:275)
	at sootup.core.util.printer.JimplePrinter.printTo(JimplePrinter.java:266)
	at sootup.core.model.Body.toString(Body.java:226)
	at java.base/java.lang.String.valueOf(String.java:4216)
	at java.base/java.io.PrintStream.println(PrintStream.java:1047)
	at sootup.java.bytecode.TryWithResourcesFinallyTests.lambda$test$0(TryWithResourcesFinallyTests.java:23)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at sootup.java.bytecode.TryWithResourcesFinallyTests.test(TryWithResourcesFinallyTests.java:23)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

This can be fixed by closing the traps on the last block after the iterations of the aggregator.