From 807c7ea9d77bdeae4aafcd31f9fd5e78c5ac52f8 Mon Sep 17 00:00:00 2001 From: Stefan Schott Date: Thu, 31 Oct 2024 13:33:55 +0100 Subject: [PATCH 01/13] add failing test case --- .../sootup/tests/JimpleSerializationTest.java | 29 ++++++++++++++++++ .../repository/git/GitRepository.class | Bin 0 -> 61890 bytes 2 files changed, 29 insertions(+) create mode 100644 sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java create mode 100644 sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepository.class diff --git a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java new file mode 100644 index 00000000000..96084645e26 --- /dev/null +++ b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java @@ -0,0 +1,29 @@ +package sootup.tests; + +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import sootup.core.inputlocation.AnalysisInputLocation; +import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; +import sootup.java.core.JavaSootMethod; +import sootup.java.core.views.JavaView; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@Tag("Java8") +public class JimpleSerializationTest { + + @Test + public void testTrapSerialization() { + AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization"); + JavaView view = new JavaView(inputLocation); + + Optional methodOpt = view.getMethod(view.getIdentifierFactory().parseMethodSignature( + "" + )); + assertTrue(methodOpt.isPresent()); + JavaSootMethod method = methodOpt.get(); + method.getBody().toString(); + } +} diff --git a/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepository.class b/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/GitRepository.class new file mode 100644 index 0000000000000000000000000000000000000000..c148dac3cc2b69b4fc87fba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D`dI(iL5lY$Q@>-{DSMXTtCe9Z@AuQZuL{KN!)Th zd|bmv_GVuDx%fb)#$@niexHC*FKc@G?WD3?xK&niqhvE%&CXy)daGHPwN05!$!52j zCw1?caPW||K>d1|Di2B<@!ohMG6!r!k-y>)=$2xG8q0E%pAn~;Bb7`VhL|4PDpMh2 z?16D~D$#sBHc$Fv(y7G8?Fb*WFNZbe-tbY3MtQS|6e6PVm0)pHEarobXjybqF(1w` zt*)(@j~c@V+*^`&r`@m{l043vYErEW?xYWQ1$XnKxmCfhg4Idx@nB<;+ZXJQb752Y jUYHxpT@hZHe7iHr-4(tQw-@dU_lF1c+wfR;JRJNIH{iU& literal 0 HcmV?d00001 From a29549086db7d5f8ef79973a1b48153c5ea33d9b Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Sat, 9 Nov 2024 12:05:36 +0100 Subject: [PATCH 02/13] moved buildtraps API to JimplePrinter --- .../core/graph/ForwardingStmtGraph.java | 6 --- .../core/graph/ImmutableBlockStmtGraph.java | 6 --- .../core/graph/MutableBlockStmtGraph.java | 33 ------------ .../java/sootup/core/graph/StmtGraph.java | 33 ++++++------ .../src/main/java/sootup/core/model/Body.java | 2 +- .../core/util/printer/JimplePrinter.java | 53 ++++++++++++++++--- .../core/util/printer/LabeledStmtPrinter.java | 2 +- .../core/graph/MutableBlockStmtGraphTest.java | 24 +++++---- .../interceptors/TrapTightenerTest.java | 7 ++- .../UnreachableCodeEliminatorTest.java | 7 ++- 10 files changed, 91 insertions(+), 82 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java index 80c4393323e..a01278307fd 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java @@ -128,12 +128,6 @@ public Iterator iterator() { return backingGraph.iterator(); } - @Nonnull - @Override - public List buildTraps() { - return backingGraph.buildTraps(); - } - @Override public void removeExceptionalFlowFromAllBlocks( @Nonnull ClassType exceptionType, @Nonnull Stmt exceptionHandlerStmt) { diff --git a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java index 84e0b4fe91a..43fcd101521 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java @@ -137,12 +137,6 @@ public boolean hasEdgeConnecting(@Nonnull Stmt source, @Nonnull Stmt target) { throw new UnsupportedOperationException("Not implemented yet!"); } - @Nonnull - @Override - public List buildTraps() { - throw new UnsupportedOperationException("Not implemented yet!"); - } - @Override public void removeExceptionalFlowFromAllBlocks(ClassType classType, Stmt exceptionHandlerStmt) { throw new UnsupportedOperationException("Not implemented yet!"); diff --git a/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java index d27f2170116..2798f6640fb 100644 --- a/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java @@ -1596,39 +1596,6 @@ public boolean hasEdgeConnecting(@Nonnull Stmt source, @Nonnull Stmt target) { } } - /** Comparator which sorts the trap output in getTraps() */ - public Comparator getTrapComparator(@Nonnull Map stmtsBlockIdx) { - return (a, b) -> - ComparisonChain.start() - .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) - .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) - // [ms] would be nice to have the traps ordered by exception hierarchy as well - .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) - .result(); - } - - /** hint: little expensive getter - its more of a build/create - currently no overlaps */ - @Override - public List buildTraps() { - // [ms] try to incorporate it into the serialisation of jimple printing so the other half of - // iteration information is not wasted.. - BlockGraphIteratorAndTrapAggregator it = - new BlockGraphIteratorAndTrapAggregator(new MutableBasicBlockImpl()); - // it.getTraps() is valid/completely build when the iterator is done. - Map stmtsBlockIdx = new IdentityHashMap<>(); - int i = 0; - // collect BlockIdx positions to sort the traps according to the numbering - while (it.hasNext()) { - final BasicBlock nextBlock = it.next(); - stmtsBlockIdx.put(nextBlock.getHead(), i); - stmtsBlockIdx.put(nextBlock.getTail(), i); - i++; - } - final List traps = it.getTraps(); - traps.sort(getTrapComparator(stmtsBlockIdx)); - return traps; - } - @Override public void removeExceptionalFlowFromAllBlocks( @Nonnull ClassType exceptionType, @Nonnull Stmt exceptionHandlerStmt) { diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index 17482b62e41..15df3c01580 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -65,6 +65,8 @@ */ public abstract class StmtGraph> implements Iterable { + public final JimplePrinter jimplePrinter = new JimplePrinter(); + public abstract Stmt getStartingStmt(); public abstract BasicBlock getStartingStmtBlock(); @@ -148,15 +150,6 @@ public int degree(@Nonnull Stmt node) { */ public abstract boolean hasEdgeConnecting(@Nonnull Stmt source, @Nonnull Stmt target); - /** - * returns a (reconstructed) list of traps like the traptable in the bytecode - * - *

Note: if you need exceptionional flow information in more augmented with the affected - * blocks/stmts and not just a (reconstructed, possibly more verbose) traptable - have a look at - * BasicBlock.getExceptionalSuccessor() - */ - public abstract List buildTraps(); - /** * Removes the specified exceptional flow from all blocks. * @@ -206,15 +199,25 @@ public Collection getEntrypoints() { /** validates whether the each Stmt has the correct amount of outgoing flows. */ public void validateStmtConnectionsInGraph() { try { + List handlerStmts = new ArrayList<>(); + for (Stmt stmt: getNodes()){ + if (stmt instanceof JIdentityStmt) { + //JThrowStmt? + IdentityRef rightOp = ((JIdentityStmt) stmt).getRightOp(); + if (rightOp instanceof JCaughtExceptionRef) { + handlerStmts.add(stmt); + } + } + } for (Stmt stmt : getNodes()) { final List successors = successors(stmt); final int successorCount = successors.size(); if (predecessors(stmt).isEmpty()) { + if (!(stmt == getStartingStmt() - || buildTraps().stream() - .map(Trap::getHandlerStmt) + || handlerStmts.stream() .anyMatch(handler -> handler == stmt))) { throw new IllegalStateException( "Stmt '" @@ -353,7 +356,7 @@ public boolean equals(Object o) { return false; } - if (!buildTraps().equals(otherGraph.buildTraps())) { + if (!jimplePrinter.buildTraps(this).equals(jimplePrinter.buildTraps(otherGraph))) { return false; } @@ -517,7 +520,7 @@ public List getTraps() { } /** Iterates over the blocks */ - protected class BlockGraphIterator implements Iterator> { + public class BlockGraphIterator implements Iterator> { @Nonnull private final ArrayDeque> trapHandlerBlocks = new ArrayDeque<>(); @@ -717,7 +720,7 @@ public Collection getLabeledStmts() { } } - for (Trap trap : buildTraps()) { + for (Trap trap : jimplePrinter.buildTraps(this)) { stmtList.add(trap.getBeginStmt()); stmtList.add(trap.getEndStmt()); stmtList.add(trap.getHandlerStmt()); @@ -730,7 +733,7 @@ public Collection getLabeledStmts() { public String toString() { StringWriter writer = new StringWriter(); try (PrintWriter writerOut = new PrintWriter(new EscapedWriter(writer))) { - new JimplePrinter().printTo(this, writerOut); + jimplePrinter.printTo(this, writerOut); } return writer.toString(); } diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index 7e8606a9088..1c161f05aa6 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -117,7 +117,7 @@ public Set getLocals() { @Nonnull @Deprecated() public List getTraps() { - return graph.buildTraps(); + return new JimplePrinter().buildTraps(graph); } /** Return unit containing the \@this-assignment * */ diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index daf83dc9794..a5cd90c5f5c 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -22,11 +22,8 @@ * #L% */ -import java.io.PrintWriter; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; -import sootup.core.graph.StmtGraph; +import com.google.common.collect.ComparisonChain; +import sootup.core.graph.*; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.Local; import sootup.core.jimple.basic.Trap; @@ -38,6 +35,12 @@ import sootup.core.types.ClassType; import sootup.core.types.Type; +import javax.annotation.Nonnull; +import java.io.PrintWriter; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; + /** * Prints out a class and all its methods. * @@ -297,6 +300,44 @@ public void printTo(StmtGraph graph, PrintWriter out, LabeledStmtPrinter prin out.print(printer); } + /** + * returns a (reconstructed) list of traps like the traptable in the bytecode + * + *

Note: if you need exceptionional flow information in more augmented with the affected + * blocks/stmts and not just a (reconstructed, possibly more verbose) traptable - have a look at + * BasicBlock.getExceptionalSuccessor() + */ + /** hint: little expensive getter - its more of a build/create - currently no overlaps */ + public List buildTraps(StmtGraph stmtGraph) { + // [ms] try to incorporate it into the serialisation of jimple printing so the other half of + // iteration information is not wasted.. + StmtGraph.BlockGraphIteratorAndTrapAggregator it = stmtGraph.new BlockGraphIteratorAndTrapAggregator(new MutableBasicBlockImpl()); + // it.getTraps() is valid/completely build when the iterator is done. + Map stmtsBlockIdx = new IdentityHashMap<>(); + int i = 0; + // collect BlockIdx positions to sort the traps according to the numbering + while (it.hasNext()) { + final BasicBlock nextBlock = it.next(); + stmtsBlockIdx.put(nextBlock.getHead(), i); + stmtsBlockIdx.put(nextBlock.getTail(), i); + i++; + } + final List traps = it.getTraps(); + traps.sort(getTrapComparator(stmtsBlockIdx)); + return traps; + } + + /** Comparator which sorts the trap output in getTraps() */ + public Comparator getTrapComparator(@Nonnull Map stmtsBlockIdx) { + return (a, b) -> + ComparisonChain.start() + .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) + .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) + // [ms] would be nice to have the traps ordered by exception hierarchy as well + .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) + .result(); + } + /** * Prints out the method corresponding to b Body, (declaration and body), in the textual format * corresponding to the IR used to encode b body. @@ -380,7 +421,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { // Print out exceptions { - Iterator trapIt = stmtGraph.buildTraps().iterator(); + Iterator trapIt = buildTraps(stmtGraph).iterator(); if (trapIt.hasNext()) { printer.newline(); diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index 01f4eb5eb75..f49d463cb60 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -114,7 +114,7 @@ public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph) { @Nonnull public List getStmts(@Nonnull StmtGraph stmtGraph) { final Collection targetStmtsOfBranches = stmtGraph.getLabeledStmts(); - final List traps = stmtGraph.buildTraps(); + final List traps = new JimplePrinter().buildTraps(stmtGraph); final int maxEstimatedSize = targetStmtsOfBranches.size() + traps.size() * 3; labels = new HashMap<>(maxEstimatedSize, 1); diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java index e64299713f9..2b88a74afdd 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java @@ -19,10 +19,13 @@ import sootup.core.types.ClassType; import sootup.core.types.PrimitiveType; import sootup.core.types.UnknownType; +import sootup.core.util.printer.JimplePrinter; @Tag("Java8") public class MutableBlockStmtGraphTest { + public JimplePrinter jimplePrinter = new JimplePrinter(); + BranchingStmt firstGoto = new JGotoStmt(StmtPositionInfo.getNoStmtPositionInfo()); JNopStmt firstNop = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); JNopStmt secondNop = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); @@ -621,7 +624,7 @@ public PackageName getPackageName() { graph0.putEdge(stmt2, 0, returnStmt); { - final List traps = graph0.buildTraps(); + final List traps = jimplePrinter.buildTraps(graph0); assertEquals(2, traps.size()); // as @caughtexception gets currently in their way. assertEquals(stmt2, traps.get(1).getBeginStmt()); assertEquals(returnStmt, traps.get(1).getEndStmt()); @@ -659,7 +662,7 @@ public PackageName getPackageName() { graph2.putEdge(stmt2, JGotoStmt.BRANCH_IDX, returnStmt); { assertEquals(5, graph2.getBlocks().size()); - final List traps = graph2.buildTraps(); + final List traps = jimplePrinter.buildTraps(graph2); assertEquals(2, traps.size()); } @@ -675,7 +678,7 @@ public PackageName getPackageName() { graph3.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = graph3.buildTraps(); + final List traps = jimplePrinter.buildTraps(graph3); assertEquals(5, graph2.getBlocks().size()); assertEquals(2, traps.size()); } @@ -698,7 +701,7 @@ public PackageName getPackageName() { graph4.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph4.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); - assertEquals(3, graph4.buildTraps().size()); + assertEquals(3, jimplePrinter.buildTraps(graph4).size()); // mixed 2 MutableBlockStmtGraph graph5 = new MutableBlockStmtGraph(); @@ -733,7 +736,7 @@ public PackageName getPackageName() { graph5.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = graph5.buildTraps(); + final List traps = jimplePrinter.buildTraps(graph5); assertEquals(6, traps.size()); assertEquals(6, graph5.getBlocks().size()); } @@ -769,7 +772,7 @@ public PackageName getPackageName() { graph6.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph6.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = graph6.buildTraps(); + final List traps = jimplePrinter.buildTraps(graph6); assertEquals(5, traps.size()); assertEquals(6, graph6.getBlocks().size()); assertEquals( @@ -1030,6 +1033,7 @@ public void simpleInsertion() { assertTrue(graph.successors(stmt1).contains(stmt2)); } + // It is an invalid graph, just for the test @Test public void testRemoveSingleTrap() { MutableBlockStmtGraph graph = new MutableBlockStmtGraph(); @@ -1052,7 +1056,7 @@ public void testRemoveSingleTrap() { graph.addExceptionalEdge(stmt1, throwableSig, handlerStmt); // Verify the trap is present - List traps = graph.buildTraps(); + List traps = jimplePrinter.buildTraps(graph); assertEquals(1, traps.size()); assertEquals(stmt1, traps.get(0).getBeginStmt()); assertEquals(handlerStmt, traps.get(0).getHandlerStmt()); @@ -1061,7 +1065,7 @@ public void testRemoveSingleTrap() { Trap trapToRemove = traps.get(0); graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = graph.buildTraps(); + traps = jimplePrinter.buildTraps(graph); assertEquals(0, traps.size()); } @@ -1095,7 +1099,7 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.addExceptionalEdge(stmt2, ioExceptionSig, handlerStmt2); // Verify both traps are present - List traps = graph.buildTraps(); + List traps = jimplePrinter.buildTraps(graph); assertEquals(2, traps.size()); // Remove one trap and verify the remaining @@ -1104,7 +1108,7 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = graph.buildTraps(); + traps = jimplePrinter.buildTraps(graph); assertEquals(1, traps.size()); assertEquals(stmt2, trapToKeep.getBeginStmt()); assertEquals(handlerStmt2, trapToKeep.getHandlerStmt()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java index 9097545d073..f6d1bcb1f3d 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java @@ -23,6 +23,7 @@ import sootup.core.types.ClassType; import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; +import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.TrapTightener; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; @@ -33,6 +34,8 @@ @Tag(TestCategories.JAVA_8_CATEGORY) @Disabled("FIXME: needs .setTraps() adapted to MutableBlockStmtGraph") public class TrapTightenerTest { + public JimplePrinter jimplePrinter = new JimplePrinter(); + JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); @@ -131,7 +134,7 @@ public void testSimpleBody() { List excepted = new ArrayList<>(); excepted.add(trap3); - List actual = stmtGraph.buildTraps(); + List actual = jimplePrinter.buildTraps(stmtGraph); AssertUtils.assertTrapsEquiv(excepted, actual); } /** @@ -172,7 +175,7 @@ public void testMonitoredBody() { List excepted = new ArrayList<>(); excepted.add(trap1); - List actual = stmtGraph.buildTraps(); + List actual = jimplePrinter.buildTraps(stmtGraph); AssertUtils.assertTrapsEquiv(excepted, actual); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java index cc114b2fb14..77845f7b464 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java @@ -23,6 +23,7 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; +import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.UnreachableCodeEliminator; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; @@ -33,6 +34,8 @@ @Tag(TestCategories.JAVA_8_CATEGORY) public class UnreachableCodeEliminatorTest { + public final JimplePrinter jimplePrinter = new JimplePrinter(); + JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); JavaJimple javaJimple = JavaJimple.getInstance(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); @@ -139,7 +142,7 @@ public void testTrappedBody1() { new UnreachableCodeEliminator().interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, builder.getStmtGraph().buildTraps().size()); + assertEquals(0, jimplePrinter.buildTraps(builder.getStmtGraph()).size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); AssertUtils.assertSetsEquiv(expectedStmtsSet, builder.getStmtGraph().getNodes()); @@ -175,7 +178,7 @@ public void testTrappedBody2() { UnreachableCodeEliminator eliminator = new UnreachableCodeEliminator(); eliminator.interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, builder.getStmtGraph().buildTraps().size()); + assertEquals(0, jimplePrinter.buildTraps(builder.getStmtGraph()).size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); assertEquals(expectedStmtsSet, builder.getStmtGraph().getNodes()); From 21a29571d43a083b6fe1adbb1e1bc64671df16e6 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Fri, 29 Nov 2024 12:01:30 +0100 Subject: [PATCH 03/13] smallest tc where trap fails --- sootup.tests/pom.xml | 6 + .../sootup/tests/JimpleSerializationTest.java | 153 +++++++++++++++++- .../repository/git/TrapSerialization.class | Bin 0 -> 735 bytes .../repository/git/TrapSerialization.java | 26 +++ 4 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class create mode 100644 sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java diff --git a/sootup.tests/pom.xml b/sootup.tests/pom.xml index d2c9e05b0bd..1562de85a68 100644 --- a/sootup.tests/pom.xml +++ b/sootup.tests/pom.xml @@ -44,6 +44,12 @@ slf4j-simple test + + com.squareup + javapoet + 1.13.0 + test + diff --git a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java index 96084645e26..1b131a16222 100644 --- a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java +++ b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java @@ -1,22 +1,33 @@ package sootup.tests; +import com.squareup.javapoet.*; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.inputlocation.AnalysisInputLocation; +import sootup.core.model.SourceType; +import sootup.core.signatures.MethodSignature; +import sootup.core.types.PrimitiveType; +import sootup.core.types.VoidType; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.core.JavaSootMethod; import sootup.java.core.views.JavaView; +import sootup.jimple.frontend.JimpleAnalysisInputLocation; +import sootup.jimple.frontend.JimpleStringAnalysisInputLocation; +import sootup.jimple.frontend.JimpleView; -import java.util.Optional; +import javax.lang.model.element.Modifier; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.*; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("Java8") public class JimpleSerializationTest { @Test public void testTrapSerialization() { - AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization"); + AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList()); JavaView view = new JavaView(inputLocation); Optional methodOpt = view.getMethod(view.getIdentifierFactory().parseMethodSignature( @@ -26,4 +37,140 @@ public void testTrapSerialization() { JavaSootMethod method = methodOpt.get(); method.getBody().toString(); } + + @Test + public void testBasicTrapSerialization() { + AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList()); + JavaView javaView = new JavaView(inputLocation); + Optional nestedTrap = javaView.getMethod(javaView.getIdentifierFactory().parseMethodSignature( + "" + )); + + assertTrue(nestedTrap.isPresent()); + JavaSootMethod nestedTrapMethod = nestedTrap.get(); + System.out.println(nestedTrapMethod.getBody()); + } + + @Test + public void testJimpleTrapSerialization() { + String jimpleString = "class DummyClass extends java.lang.Object {\n" + + "\tint testTrapSerialization() {\n" + + "\t\tsootUp.RQ1.jb_a.JB_A this;\n" + + "\t\tunknown $stack4, $stack5, a, b, e;\n" + + "\n" + + "\n" + + "\t\tthis := @this: sootUp.RQ1.jb_a.JB_A;\n" + + "\t\ta = 0;\n" + + "\t\tb = 10;\n" + + "\n" + + "\t label1:\n" + + "\t\tb = b / a;\n" + + "\t\t$stack4 = b;\n" + + "\t\treturn a;\n" + + "\n" + + "\t label2:\n" + + "\t\treturn $stack4;\n" + + "\n" + + "\t label3:\n" + + "\t\t$stack5 := @caughtexception;\n" + + "\t\te = $stack5;\n" + + "\n" + + "\t\treturn b;\n" + + "\n" + + "\t catch java.lang.ArithmeticException from label1 to label2 with label3;\n" + + "\t catch java.lang.NullPointerException from label1 to label2 with label3;\n" + + "\t}\n" + + "}"; + + JimpleStringAnalysisInputLocation analysisInputLocation = new JimpleStringAnalysisInputLocation(jimpleString, SourceType.Application, Collections.emptyList()); + JimpleView view = new JimpleView(analysisInputLocation); + assertTrue(view.getClass(analysisInputLocation.getClassType()).isPresent()); + MethodSignature methodSig = + view.getIdentifierFactory() + .getMethodSignature( + analysisInputLocation.getClassType(), + "testTrapSerialization", + PrimitiveType.IntType.getInstance(), + Collections.emptyList()); + assertTrue(view.getMethod(methodSig).isPresent()); + } + + + @Test + public void addNopInEndOfTryCatchFinally() throws IOException { + // Define the method body + CodeBlock methodBody = CodeBlock.builder() + .addStatement("$T> result = new $T<>()", Map.class, Map.Entry.class, HashMap.class) + .beginControlFlow("try") + .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "try", AbstractMap.class, "Key1", "Value1") + .addStatement("return result") + .nextControlFlow("catch ($T e)", Exception.class) + .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "catch", AbstractMap.class, "Key2", "Value2") + .addStatement("return result") + .nextControlFlow("finally") + .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "finally", AbstractMap.class, "Key3", "Value3") + .endControlFlow() + .addStatement("return result") + .addStatement("return result") + .build(); + + // Create the method + MethodSpec tryCatchFinallyMethod = MethodSpec.methodBuilder("tryCatchFinallyMethod") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(ParameterizedTypeName.get(Map.class, String.class)) + .addCode(methodBody) + .build(); + + // Create the class + TypeSpec tryCatchFinallyClass = TypeSpec.classBuilder("TryCatchFinallyExample") + .addModifiers(Modifier.PUBLIC) + .addMethod(tryCatchFinallyMethod) + .build(); + + // Write to a Java file + JavaFile javaFile = JavaFile.builder("com.example", tryCatchFinallyClass) + .build(); + + // Write the generated Java file to the file system + javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/")); + } + + @Test + public void addNopInEndOfNestedTrap() throws IOException { + // Define the method body with nested try-catch blocks + CodeBlock methodBody = CodeBlock.builder() + .beginControlFlow("try") + .addStatement("System.out.println($S)", "Outer try block") + .beginControlFlow("try") + .addStatement("System.out.println($S)", "Inner try block") + .addStatement("throw new RuntimeException($S)", "Inner exception") + .nextControlFlow("catch (Exception e)") + .addStatement("System.out.println($S + e.getMessage())", "Caught inner exception: ") + .endControlFlow() + .nextControlFlow("catch (Exception e)") + .addStatement("System.out.println($S + e.getMessage())", "Caught outer exception: ") + .endControlFlow() + .build(); + + // Create the method + MethodSpec nestedTryCatchMethod = MethodSpec.methodBuilder("nestedTryCatch") + .addModifiers(Modifier.PUBLIC, Modifier.STATIC) + .returns(void.class) + .addCode(methodBody) + .build(); + + // Create the class + TypeSpec nestedTryCatchClass = TypeSpec.classBuilder("NestedTryCatchExample") + .addModifiers(Modifier.PUBLIC) + .addMethod(nestedTryCatchMethod) + .build(); + + // Write to a Java file + JavaFile javaFile = JavaFile.builder("com.example", nestedTryCatchClass) + .build(); + + // Write the generated Java file to the file system + javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/")); + } + } diff --git a/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class b/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.class new file mode 100644 index 0000000000000000000000000000000000000000..33ff6c5999cf2778152886358ae672d06f44165f GIT binary patch literal 735 zcmZuuO>fgc5Pcgvbun?`wxMkbEgvVRRq=rfG{B`QQc+0-iHbOG+{NAEc&+s&6n+gq z0Zu_6QO^7*#B6~!sCeZ!GjC^h-p>B``Q;mcN4V*u0>^_}LlrX&;V~ccIOBO5?+=be zVi;x~%3PYq3{KQO@G*-k9{d`vqQ)?Hk-nQ7kqXV=jd?Z_`>z=mquuu2rHqb;I>W*R zzjKm^!bp|-2;f+lvufrUmZPci{*hL1`5+UW_JM~c!;3@><4oovQM!l|ksHmkLzNDB zTnarFI+mnK=RAu`qcl%NtVN+pNx{1~l_u_MUi5^PJd^LaRlwkGt3$!i*rRj1qv1g4 zzU|4dQfQTkvV1Ac(auSc$wZoMUK*LF3=gJ-_KYU7GbQ~G^S@y*1U#qXPPg2!`8RI46NIN*)o{%+t*YIOth@OgvmdD* zsM89_-oY(GV1rgb9cM#<~$<0zy_MwBrW%_gcfDi;dDI&&poU> SC5s!xxuiUxf0g_S)_ws}nW Date: Sun, 8 Dec 2024 23:05:25 +0100 Subject: [PATCH 04/13] BlockGraphIterator, BlockGraphIteratorAndTrapAggregator separate classes, buildtraps to LabeledStmtPrinter --- .../sootup/core/graph/BlockGraphIterator.java | 194 ++++++++++++ .../java/sootup/core/graph/StmtGraph.java | 293 +----------------- .../src/main/java/sootup/core/model/Body.java | 3 +- .../BlockGraphIteratorAndTrapAggregator.java | 105 +++++++ .../core/util/printer/JimplePrinter.java | 40 +-- .../core/util/printer/LabeledStmtPrinter.java | 89 +++++- .../core/graph/MutableBlockStmtGraphTest.java | 22 +- .../interceptors/TrapTightenerTest.java | 6 +- .../UnreachableCodeEliminatorTest.java | 6 +- .../sootup/tests/JimpleSerializationTest.java | 1 + .../repository/git/TrapSerialization.java | 8 +- 11 files changed, 417 insertions(+), 350 deletions(-) create mode 100644 sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java create mode 100644 sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java diff --git a/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java new file mode 100644 index 00000000000..ffcb1a6d827 --- /dev/null +++ b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java @@ -0,0 +1,194 @@ +package sootup.core.graph; + +import sootup.core.jimple.common.stmt.JGotoStmt; +import sootup.core.jimple.common.stmt.Stmt; +import sootup.core.types.ClassType; +import sootup.core.util.DotExporter; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.*; +import java.util.stream.Collectors; + +/** + * Iterates over the blocks + */ +public class BlockGraphIterator implements Iterator> { + + private final StmtGraph stmtGraph; + @Nonnull + private final ArrayDeque> trapHandlerBlocks = new ArrayDeque<>(); + + @Nonnull + private final ArrayDeque> nestedBlocks = new ArrayDeque<>(); + @Nonnull + private final ArrayDeque> otherBlocks = new ArrayDeque<>(); + @Nonnull + private final Set> iteratedBlocks; + + public BlockGraphIterator(StmtGraph stmtGraph) { + this.stmtGraph = stmtGraph; + final Collection> blocks = stmtGraph.getBlocks(); + iteratedBlocks = new LinkedHashSet<>(blocks.size(), 1); + Stmt startingStmt = stmtGraph.getStartingStmt(); + if (startingStmt != null) { + final BasicBlock startingBlock = stmtGraph.getStartingStmtBlock(); + updateFollowingBlocks(startingBlock); + nestedBlocks.addFirst(startingBlock); + } + } + + @Nullable + private BasicBlock retrieveNextBlock() { + BasicBlock nextBlock; + do { + if (!nestedBlocks.isEmpty()) { + nextBlock = nestedBlocks.pollFirst(); + } else if (!trapHandlerBlocks.isEmpty()) { + nextBlock = trapHandlerBlocks.pollFirst(); + } else if (!otherBlocks.isEmpty()) { + nextBlock = otherBlocks.pollFirst(); + } else { + Collection> blocks = stmtGraph.getBlocks(); + if (iteratedBlocks.size() < blocks.size()) { + // graph is not connected! iterate/append all not connected blocks at the end in no + // particular order. + for (BasicBlock block : blocks) { + if (!iteratedBlocks.contains(block)) { + nestedBlocks.addLast(block); + } + } + if (!nestedBlocks.isEmpty()) { + return nestedBlocks.pollFirst(); + } + } + + return null; + } + + // skip retrieved nextBlock if its already returned + } while (iteratedBlocks.contains(nextBlock)); + return nextBlock; + } + + @Override + @Nonnull + public BasicBlock next() { + BasicBlock currentBlock = retrieveNextBlock(); + if (currentBlock == null) { + throw new NoSuchElementException("Iterator has no more Blocks."); + } + updateFollowingBlocks(currentBlock); + iteratedBlocks.add(currentBlock); + return currentBlock; + } + + private void updateFollowingBlocks(BasicBlock currentBlock) { + // collect traps + final Stmt tailStmt = currentBlock.getTail(); + for (Map.Entry> entry : + currentBlock.getExceptionalSuccessors().entrySet()) { + BasicBlock trapHandlerBlock = entry.getValue(); + trapHandlerBlocks.addLast(trapHandlerBlock); + nestedBlocks.addFirst(trapHandlerBlock); + } + + final List> successors = currentBlock.getSuccessors(); + + for (int i = successors.size() - 1; i >= 0; i--) { + if (i == 0 && tailStmt.fallsThrough()) { + // non-branching successors i.e. not a BranchingStmt or is the first successor (i.e. its + // false successor) of + // JIfStmt + nestedBlocks.addFirst(successors.get(0)); + } else { + + // create the longest FallsThroughStmt sequence possible + final BasicBlock successorBlock = successors.get(i); + BasicBlock leaderOfFallsthroughBlocks = successorBlock; + while (true) { + final List> itPreds = + leaderOfFallsthroughBlocks.getPredecessors(); + + BasicBlock finalLeaderOfFallsthroughBlocks = leaderOfFallsthroughBlocks; + final Optional> fallsthroughPredOpt = + itPreds.stream() + .filter( + b -> + b.getTail().fallsThrough() + && b.getSuccessors().get(0) == finalLeaderOfFallsthroughBlocks) + .findAny(); + if (!fallsthroughPredOpt.isPresent()) { + break; + } + BasicBlock predecessorBlock = fallsthroughPredOpt.get(); + if (predecessorBlock.getTail().fallsThrough() + && predecessorBlock.getSuccessors().get(0) == leaderOfFallsthroughBlocks) { + leaderOfFallsthroughBlocks = predecessorBlock; + } else { + break; + } + } + + // find a return Stmt inside the current Block + Stmt succTailStmt = successorBlock.getTail(); + boolean hasNoSuccessorStmts = succTailStmt.getExpectedSuccessorCount() == 0; + boolean isExceptionFree = successorBlock.getExceptionalSuccessors().isEmpty(); + + boolean isLastStmtCandidate = hasNoSuccessorStmts && isExceptionFree; + // remember branching successors + if (tailStmt instanceof JGotoStmt) { + if (isLastStmtCandidate) { + nestedBlocks.removeFirstOccurrence(currentBlock); + otherBlocks.addLast(leaderOfFallsthroughBlocks); + } else { + otherBlocks.addFirst(leaderOfFallsthroughBlocks); + } + } else if (!nestedBlocks.contains(leaderOfFallsthroughBlocks)) { + // JSwitchStmt, JIfStmt + if (isLastStmtCandidate) { + nestedBlocks.addLast(leaderOfFallsthroughBlocks); + } else { + nestedBlocks.addFirst(leaderOfFallsthroughBlocks); + } + } + } + } + } + + @Override + public boolean hasNext() { + final boolean hasIteratorMoreElements; + BasicBlock b = retrieveNextBlock(); + if (b != null) { + // reinsert at FIRST position -> not great for performance - but easier handling in + // next() + nestedBlocks.addFirst(b); + hasIteratorMoreElements = true; + } else { + hasIteratorMoreElements = false; + } + + // "assertion" that all elements are iterated + if (!hasIteratorMoreElements) { + final int returnedSize = iteratedBlocks.size(); + final Collection> blocks = stmtGraph.getBlocks(); + final int actualSize = blocks.size(); + if (returnedSize != actualSize) { + String info = + blocks.stream() + .filter(n -> !iteratedBlocks.contains(n)) + .map(BasicBlock::getStmts) + .collect(Collectors.toList()) + .toString(); + throw new IllegalStateException( + "There are " + + (actualSize - returnedSize) + + " Blocks that are not iterated! i.e. the StmtGraph is not connected from its startingStmt!" + + info + + DotExporter.createUrlToWebeditor(stmtGraph)); + } + } + return hasIteratorMoreElements; + } +} diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index 15df3c01580..bb3b919e273 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -37,6 +37,7 @@ import sootup.core.types.ClassType; import sootup.core.util.DotExporter; import sootup.core.util.EscapedWriter; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; /** @@ -66,6 +67,7 @@ public abstract class StmtGraph> implements Iterable { public final JimplePrinter jimplePrinter = new JimplePrinter(); + public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); public abstract Stmt getStartingStmt(); @@ -91,7 +93,7 @@ public List getStmts() { public abstract List> getBlocksSorted(); public Iterator> getBlockIterator() { - return new BlockGraphIterator(); + return new BlockGraphIterator(this); } public abstract BasicBlock getBlockOf(@Nonnull Stmt stmt); @@ -356,7 +358,7 @@ public boolean equals(Object o) { return false; } - if (!jimplePrinter.buildTraps(this).equals(jimplePrinter.buildTraps(otherGraph))) { + if (!briefStmtPrinter.buildTraps(this).equals(briefStmtPrinter.buildTraps(otherGraph))) { return false; } @@ -418,7 +420,7 @@ private class BlockStmtGraphIterator implements Iterator { @Nonnull private Iterator currentBlockIt = Collections.emptyIterator(); public BlockStmtGraphIterator() { - this(new BlockGraphIterator()); + this(new BlockGraphIterator(StmtGraph.this)); } public BlockStmtGraphIterator(@Nonnull BlockGraphIterator blockIterator) { @@ -444,291 +446,6 @@ public Stmt next() { } } - /** Iterates over the Blocks and collects/aggregates Trap information */ - public class BlockGraphIteratorAndTrapAggregator extends BlockGraphIterator { - - @Nonnull private final List collectedTraps = new ArrayList<>(); - - Map activeTraps = new HashMap<>(); - BasicBlock lastIteratedBlock; // dummy value to remove n-1 unnecessary null-checks - - /* - * @param dummyBlock is just an empty instantiation of type V - as neither BasicBlock nor V instantiable we need a concrete object from the using subclass itclass. - * */ - public BlockGraphIteratorAndTrapAggregator(V dummyBlock) { - super(); - lastIteratedBlock = dummyBlock; - } - - @Nonnull - @Override - public BasicBlock next() { - final BasicBlock block = super.next(); - - final Map> currentBlocksExceptions = - block.getExceptionalSuccessors(); - final Map> lastBlocksExceptions = - lastIteratedBlock.getExceptionalSuccessors(); - - // former trap info is not in the current blocks info -> add it to the trap collection - lastBlocksExceptions.forEach( - (type, trapHandlerBlock) -> { - if (trapHandlerBlock != block.getExceptionalSuccessors().get(type)) { - final Stmt trapBeginStmt = activeTraps.remove(type); - if (trapBeginStmt == null) { - throw new IllegalStateException("Trap start for '" + type + "' is not in the Map!"); - } - // trapend is exclusive! - collectedTraps.add( - new Trap(type, trapBeginStmt, block.getHead(), trapHandlerBlock.getHead())); - } - }); - - // is there a new trap in the current block -> add it to currentTraps - block - .getExceptionalSuccessors() - .forEach( - (type, trapHandlerBlock) -> { - if (trapHandlerBlock != lastBlocksExceptions.get(type)) { - activeTraps.put(type, block.getHead()); - } - }); - - lastIteratedBlock = block; - return block; - } - - /** - * for jimple serialization - this info contains only valid/useful information if all stmts are - * iterated i.e. hasNext() == false! - * - * @return List of Traps - */ - public List getTraps() { - - if (hasNext()) { - throw new IllegalStateException("Iterator needs to be iterated completely!"); - } - - // check for dangling traps that are not collected as the endStmt was not visited. - if (!activeTraps.isEmpty()) { - throw new IllegalArgumentException( - "Invalid StmtGraph. A Trap is not created as a traps endStmt was not visited during the iteration of all Stmts."); - } - return collectedTraps; - } - } - - /** Iterates over the blocks */ - public class BlockGraphIterator implements Iterator> { - - @Nonnull private final ArrayDeque> trapHandlerBlocks = new ArrayDeque<>(); - - @Nonnull private final ArrayDeque> nestedBlocks = new ArrayDeque<>(); - @Nonnull private final ArrayDeque> otherBlocks = new ArrayDeque<>(); - @Nonnull private final Set> iteratedBlocks; - - public BlockGraphIterator() { - final Collection> blocks = getBlocks(); - iteratedBlocks = new LinkedHashSet<>(blocks.size(), 1); - Stmt startingStmt = getStartingStmt(); - if (startingStmt != null) { - final BasicBlock startingBlock = getStartingStmtBlock(); - updateFollowingBlocks(startingBlock); - nestedBlocks.addFirst(startingBlock); - } - } - - @Nullable - private BasicBlock retrieveNextBlock() { - BasicBlock nextBlock; - do { - if (!nestedBlocks.isEmpty()) { - nextBlock = nestedBlocks.pollFirst(); - } else if (!trapHandlerBlocks.isEmpty()) { - nextBlock = trapHandlerBlocks.pollFirst(); - } else if (!otherBlocks.isEmpty()) { - nextBlock = otherBlocks.pollFirst(); - } else { - Collection> blocks = getBlocks(); - if (iteratedBlocks.size() < blocks.size()) { - // graph is not connected! iterate/append all not connected blocks at the end in no - // particular order. - for (BasicBlock block : blocks) { - if (!iteratedBlocks.contains(block)) { - nestedBlocks.addLast(block); - } - } - if (!nestedBlocks.isEmpty()) { - return nestedBlocks.pollFirst(); - } - } - - return null; - } - - // skip retrieved nextBlock if its already returned - } while (iteratedBlocks.contains(nextBlock)); - return nextBlock; - } - - @Override - @Nonnull - public BasicBlock next() { - BasicBlock currentBlock = retrieveNextBlock(); - if (currentBlock == null) { - throw new NoSuchElementException("Iterator has no more Blocks."); - } - updateFollowingBlocks(currentBlock); - iteratedBlocks.add(currentBlock); - return currentBlock; - } - - private void updateFollowingBlocks(BasicBlock currentBlock) { - // collect traps - final Stmt tailStmt = currentBlock.getTail(); - for (Map.Entry> entry : - currentBlock.getExceptionalSuccessors().entrySet()) { - BasicBlock trapHandlerBlock = entry.getValue(); - trapHandlerBlocks.addLast(trapHandlerBlock); - nestedBlocks.addFirst(trapHandlerBlock); - } - - final List> successors = currentBlock.getSuccessors(); - - for (int i = successors.size() - 1; i >= 0; i--) { - if (i == 0 && tailStmt.fallsThrough()) { - // non-branching successors i.e. not a BranchingStmt or is the first successor (i.e. its - // false successor) of - // JIfStmt - nestedBlocks.addFirst(successors.get(0)); - } else { - - // create the longest FallsThroughStmt sequence possible - final BasicBlock successorBlock = successors.get(i); - BasicBlock leaderOfFallsthroughBlocks = successorBlock; - while (true) { - final List> itPreds = - leaderOfFallsthroughBlocks.getPredecessors(); - - BasicBlock finalLeaderOfFallsthroughBlocks = leaderOfFallsthroughBlocks; - final Optional> fallsthroughPredOpt = - itPreds.stream() - .filter( - b -> - b.getTail().fallsThrough() - && b.getSuccessors().get(0) == finalLeaderOfFallsthroughBlocks) - .findAny(); - if (!fallsthroughPredOpt.isPresent()) { - break; - } - BasicBlock predecessorBlock = fallsthroughPredOpt.get(); - if (predecessorBlock.getTail().fallsThrough() - && predecessorBlock.getSuccessors().get(0) == leaderOfFallsthroughBlocks) { - leaderOfFallsthroughBlocks = predecessorBlock; - } else { - break; - } - } - - // find a return Stmt inside the current Block - Stmt succTailStmt = successorBlock.getTail(); - boolean hasNoSuccessorStmts = succTailStmt.getExpectedSuccessorCount() == 0; - boolean isExceptionFree = successorBlock.getExceptionalSuccessors().isEmpty(); - - boolean isLastStmtCandidate = hasNoSuccessorStmts && isExceptionFree; - // remember branching successors - if (tailStmt instanceof JGotoStmt) { - if (isLastStmtCandidate) { - nestedBlocks.removeFirstOccurrence(currentBlock); - otherBlocks.addLast(leaderOfFallsthroughBlocks); - } else { - otherBlocks.addFirst(leaderOfFallsthroughBlocks); - } - } else if (!nestedBlocks.contains(leaderOfFallsthroughBlocks)) { - // JSwitchStmt, JIfStmt - if (isLastStmtCandidate) { - nestedBlocks.addLast(leaderOfFallsthroughBlocks); - } else { - nestedBlocks.addFirst(leaderOfFallsthroughBlocks); - } - } - } - } - } - - @Override - public boolean hasNext() { - final boolean hasIteratorMoreElements; - BasicBlock b = retrieveNextBlock(); - if (b != null) { - // reinsert at FIRST position -> not great for performance - but easier handling in - // next() - nestedBlocks.addFirst(b); - hasIteratorMoreElements = true; - } else { - hasIteratorMoreElements = false; - } - - // "assertion" that all elements are iterated - if (!hasIteratorMoreElements) { - final int returnedSize = iteratedBlocks.size(); - final Collection> blocks = getBlocks(); - final int actualSize = blocks.size(); - if (returnedSize != actualSize) { - String info = - blocks.stream() - .filter(n -> !iteratedBlocks.contains(n)) - .map(BasicBlock::getStmts) - .collect(Collectors.toList()) - .toString(); - throw new IllegalStateException( - "There are " - + (actualSize - returnedSize) - + " Blocks that are not iterated! i.e. the StmtGraph is not connected from its startingStmt!" - + info - + DotExporter.createUrlToWebeditor(StmtGraph.this)); - } - } - return hasIteratorMoreElements; - } - } - - /** - * Returns the result of iterating through all Stmts in this body. All Stmts thus found are - * returned. Branching Stmts and statements which use PhiExpr will have Stmts; a Stmt contains a - * Stmt that is either a target of a branch or is being used as a pointer to the end of a CFG - * block. - * - *

This method was typically used for pointer patching, e.g. when the unit chain is cloned. - * - * @return A collection of all the Stmts that are targets of a BranchingStmt - */ - @Nonnull - public Collection getLabeledStmts() { - Set stmtList = new HashSet<>(); - for (Stmt stmt : getNodes()) { - if (stmt instanceof BranchingStmt) { - if (stmt instanceof JIfStmt) { - stmtList.add(getBranchTargetsOf((JIfStmt) stmt).get(JIfStmt.FALSE_BRANCH_IDX)); - } else if (stmt instanceof JGotoStmt) { - // [ms] bounds are validated in Body if its a valid StmtGraph - stmtList.add(getBranchTargetsOf((JGotoStmt) stmt).get(JGotoStmt.BRANCH_IDX)); - } else if (stmt instanceof JSwitchStmt) { - stmtList.addAll(getBranchTargetsOf((BranchingStmt) stmt)); - } - } - } - - for (Trap trap : jimplePrinter.buildTraps(this)) { - stmtList.add(trap.getBeginStmt()); - stmtList.add(trap.getEndStmt()); - stmtList.add(trap.getHandlerStmt()); - } - - return stmtList; - } - @Override public String toString() { StringWriter writer = new StringWriter(); diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index 1c161f05aa6..22838f71ae6 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -38,6 +38,7 @@ import sootup.core.jimple.common.stmt.*; import sootup.core.signatures.MethodSignature; import sootup.core.util.EscapedWriter; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; /** @@ -117,7 +118,7 @@ public Set getLocals() { @Nonnull @Deprecated() public List getTraps() { - return new JimplePrinter().buildTraps(graph); + return new BriefStmtPrinter().buildTraps(graph); } /** Return unit containing the \@this-assignment * */ diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java new file mode 100644 index 00000000000..34010746104 --- /dev/null +++ b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java @@ -0,0 +1,105 @@ +package sootup.core.util.printer; + +import sootup.core.graph.BasicBlock; +import sootup.core.graph.BlockGraphIterator; +import sootup.core.graph.MutableBasicBlockImpl; +import sootup.core.graph.StmtGraph; +import sootup.core.jimple.basic.StmtPositionInfo; +import sootup.core.jimple.basic.Trap; +import sootup.core.jimple.common.stmt.JNopStmt; +import sootup.core.jimple.common.stmt.Stmt; +import sootup.core.types.ClassType; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Iterates over the Blocks and collects/aggregates Trap information + * It is used to collect and aggregate traps for serializing Jimple in the JimplePrinter + */ +class BlockGraphIteratorAndTrapAggregator extends BlockGraphIterator { + + @Nonnull + private final List collectedTraps = new ArrayList<>(); + + Map activeTraps = new HashMap<>(); + BasicBlock lastIteratedBlock; // dummy value to remove n-1 unnecessary null-checks + JNopStmt lastStmt = null; + + /* + * @param dummyBlock is just an empty instantiation of type V - as neither BasicBlock nor V instantiable we need a concrete object from the using subclass itclass. + * */ + public BlockGraphIteratorAndTrapAggregator(StmtGraph stmtGraph) { + super(stmtGraph); + lastIteratedBlock = new MutableBasicBlockImpl(); + } + + @Nullable + public JNopStmt getLastStmt() { + return lastStmt; + } + + @Nonnull + @Override + public BasicBlock next() { + final BasicBlock block = super.next(); + + final Map> currentBlocksExceptions = + block.getExceptionalSuccessors(); + final Map> lastBlocksExceptions = + lastIteratedBlock.getExceptionalSuccessors(); + + // former trap info is not in the current blocks info -> add it to the trap collection + lastBlocksExceptions.forEach( + (type, trapHandlerBlock) -> { + if (trapHandlerBlock != block.getExceptionalSuccessors().get(type)) { + final Stmt trapBeginStmt = activeTraps.remove(type); + if (trapBeginStmt == null) { + throw new IllegalStateException("Trap start for '" + type + "' is not in the Map!"); + } + // trapend is exclusive! + collectedTraps.add( + new Trap(type, trapBeginStmt, block.getHead(), trapHandlerBlock.getHead())); + } + }); + + // is there a new trap in the current block -> add it to currentTraps + block + .getExceptionalSuccessors() + .forEach( + (type, trapHandlerBlock) -> { + if (trapHandlerBlock != lastBlocksExceptions.get(type)) { + activeTraps.put(type, block.getHead()); + } + }); + + lastIteratedBlock = block; + return block; + } + + /** + * for jimple serialization - this info contains only valid/useful information if all stmts are + * iterated i.e. hasNext() == false! + * + * @return List of Traps + */ + public List getTraps() { + + if (hasNext()) { + throw new IllegalStateException("Iterator needs to be iterated completely!"); + } + + // check for dangling traps that are not collected as the endStmt was not visited. + if (!activeTraps.isEmpty()) { + lastStmt = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); +// throw new IllegalArgumentException( +// "Invalid StmtGraph. A Trap is not created as a traps endStmt was not visited during the iteration of all Stmts."); + activeTraps.forEach((type, beginning) -> collectedTraps.add(new Trap(type, beginning, lastStmt, lastIteratedBlock.getExceptionalSuccessors().get(type).getHead()))); + } + return collectedTraps; + } +} diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index a5cd90c5f5c..d05ff99b198 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -300,44 +300,6 @@ public void printTo(StmtGraph graph, PrintWriter out, LabeledStmtPrinter prin out.print(printer); } - /** - * returns a (reconstructed) list of traps like the traptable in the bytecode - * - *

Note: if you need exceptionional flow information in more augmented with the affected - * blocks/stmts and not just a (reconstructed, possibly more verbose) traptable - have a look at - * BasicBlock.getExceptionalSuccessor() - */ - /** hint: little expensive getter - its more of a build/create - currently no overlaps */ - public List buildTraps(StmtGraph stmtGraph) { - // [ms] try to incorporate it into the serialisation of jimple printing so the other half of - // iteration information is not wasted.. - StmtGraph.BlockGraphIteratorAndTrapAggregator it = stmtGraph.new BlockGraphIteratorAndTrapAggregator(new MutableBasicBlockImpl()); - // it.getTraps() is valid/completely build when the iterator is done. - Map stmtsBlockIdx = new IdentityHashMap<>(); - int i = 0; - // collect BlockIdx positions to sort the traps according to the numbering - while (it.hasNext()) { - final BasicBlock nextBlock = it.next(); - stmtsBlockIdx.put(nextBlock.getHead(), i); - stmtsBlockIdx.put(nextBlock.getTail(), i); - i++; - } - final List traps = it.getTraps(); - traps.sort(getTrapComparator(stmtsBlockIdx)); - return traps; - } - - /** Comparator which sorts the trap output in getTraps() */ - public Comparator getTrapComparator(@Nonnull Map stmtsBlockIdx) { - return (a, b) -> - ComparisonChain.start() - .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) - .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) - // [ms] would be nice to have the traps ordered by exception hierarchy as well - .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) - .result(); - } - /** * Prints out the method corresponding to b Body, (declaration and body), in the textual format * corresponding to the IR used to encode b body. @@ -421,7 +383,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { // Print out exceptions { - Iterator trapIt = buildTraps(stmtGraph).iterator(); + Iterator trapIt = printer.buildTraps(stmtGraph).iterator(); if (trapIt.hasNext()) { printer.newline(); diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index f49d463cb60..abaf19a05b8 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -24,11 +24,15 @@ import java.util.*; import javax.annotation.Nonnull; + +import com.google.common.collect.ComparisonChain; +import sootup.core.graph.BasicBlock; import sootup.core.graph.StmtGraph; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.ref.IdentityRef; -import sootup.core.jimple.common.stmt.Stmt; +import sootup.core.jimple.common.stmt.*; +import sootup.core.jimple.javabytecode.stmt.JSwitchStmt; import sootup.core.model.SootField; import sootup.core.model.SootMethod; import sootup.core.signatures.FieldSignature; @@ -113,8 +117,8 @@ public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph) { @Nonnull public List getStmts(@Nonnull StmtGraph stmtGraph) { - final Collection targetStmtsOfBranches = stmtGraph.getLabeledStmts(); - final List traps = new JimplePrinter().buildTraps(stmtGraph); + final Collection targetStmtsOfBranches = getLabeledStmts(stmtGraph); + final List traps = buildTraps(stmtGraph); final int maxEstimatedSize = targetStmtsOfBranches.size() + traps.size() * 3; labels = new HashMap<>(maxEstimatedSize, 1); @@ -136,7 +140,7 @@ public List getStmts(@Nonnull StmtGraph stmtGraph) { // or is the begin of a trap-range) or does it mark the end of a trap range // does it need a label for (Stmt stmt : targetStmtsOfBranches) { - if (stmtGraph.isStmtBranchTarget(stmt) || trapStmts.contains(stmt)) { + if ((stmt instanceof JNopStmt) || stmtGraph.isStmtBranchTarget(stmt) || trapStmts.contains(stmt)) { labelStmts.add(stmt); } else { refStmts.add(stmt); @@ -195,4 +199,81 @@ public void fieldSignature(FieldSignature fieldSig) { typeSignature(subSignature.getType()); output.append(' ').append(Jimple.escape(subSignature.getName())).append('>'); } + + /** + * returns a (reconstructed) list of traps like the traptable in the bytecode + * + *

Note: if you need exceptionional flow information in more augmented with the affected + * blocks/stmts and not just a (reconstructed, possibly more verbose) traptable - have a look at + * BasicBlock.getExceptionalSuccessor() + */ + /** hint: little expensive getter - its more of a build/create - currently no overlaps */ + public List buildTraps(StmtGraph stmtGraph) { + // [ms] try to incorporate it into the serialisation of jimple printing so the other half of + // iteration information is not wasted.. + BlockGraphIteratorAndTrapAggregator it = new BlockGraphIteratorAndTrapAggregator(stmtGraph); + // it.getTraps() is valid/completely build when the iterator is done. + Map stmtsBlockIdx = new IdentityHashMap<>(); + int i = 0; + // collect BlockIdx positions to sort the traps according to the numbering + while (it.hasNext()) { + final BasicBlock nextBlock = it.next(); + stmtsBlockIdx.put(nextBlock.getHead(), i); + stmtsBlockIdx.put(nextBlock.getTail(), i); + i++; + } + final List traps = it.getTraps(); + if (it.getLastStmt() != null) { + stmtsBlockIdx.put(it.getLastStmt(), i); + } + traps.sort(getTrapComparator(stmtsBlockIdx)); + return traps; + } + + /** Comparator which sorts the trap output in getTraps() */ + public Comparator getTrapComparator(@Nonnull Map stmtsBlockIdx) { + return (a, b) -> + ComparisonChain.start() + .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) + .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) + // [ms] would be nice to have the traps ordered by exception hierarchy as well + .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) + .result(); + } + + /** + * Returns the result of iterating through all Stmts in this body. All Stmts thus found are + * returned. Branching Stmts and statements which use PhiExpr will have Stmts; a Stmt contains a + * Stmt that is either a target of a branch or is being used as a pointer to the end of a CFG + * block. + * + *

This method was typically used for pointer patching, e.g. when the unit chain is cloned. + * + * @return A collection of all the Stmts that are targets of a BranchingStmt + */ + @Nonnull + public Collection getLabeledStmts(StmtGraph stmtGraph) { + Set stmtList = new HashSet<>(); + Collection stmtGraphNodes = stmtGraph.getNodes(); + for (Stmt stmt : stmtGraphNodes) { + if (stmt instanceof BranchingStmt) { + if (stmt instanceof JIfStmt) { + stmtList.add((Stmt) stmtGraph.getBranchTargetsOf((JIfStmt) stmt).get(JIfStmt.FALSE_BRANCH_IDX)); + } else if (stmt instanceof JGotoStmt) { + // [ms] bounds are validated in Body if its a valid StmtGraph + stmtList.add((Stmt) stmtGraph.getBranchTargetsOf((JGotoStmt) stmt).get(JGotoStmt.BRANCH_IDX)); + } else if (stmt instanceof JSwitchStmt) { + stmtList.addAll(stmtGraph.getBranchTargetsOf((BranchingStmt) stmt)); + } + } + } + + for (Trap trap : buildTraps(stmtGraph)) { + stmtList.add(trap.getBeginStmt()); + stmtList.add(trap.getEndStmt()); + stmtList.add(trap.getHandlerStmt()); + } + + return stmtList; + } } diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java index 2b88a74afdd..92faac8bbaa 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java @@ -19,12 +19,14 @@ import sootup.core.types.ClassType; import sootup.core.types.PrimitiveType; import sootup.core.types.UnknownType; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; @Tag("Java8") public class MutableBlockStmtGraphTest { public JimplePrinter jimplePrinter = new JimplePrinter(); + public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); BranchingStmt firstGoto = new JGotoStmt(StmtPositionInfo.getNoStmtPositionInfo()); JNopStmt firstNop = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); @@ -624,7 +626,7 @@ public PackageName getPackageName() { graph0.putEdge(stmt2, 0, returnStmt); { - final List traps = jimplePrinter.buildTraps(graph0); + final List traps = briefStmtPrinter.buildTraps(graph0); assertEquals(2, traps.size()); // as @caughtexception gets currently in their way. assertEquals(stmt2, traps.get(1).getBeginStmt()); assertEquals(returnStmt, traps.get(1).getEndStmt()); @@ -662,7 +664,7 @@ public PackageName getPackageName() { graph2.putEdge(stmt2, JGotoStmt.BRANCH_IDX, returnStmt); { assertEquals(5, graph2.getBlocks().size()); - final List traps = jimplePrinter.buildTraps(graph2); + final List traps = briefStmtPrinter.buildTraps(graph2); assertEquals(2, traps.size()); } @@ -678,7 +680,7 @@ public PackageName getPackageName() { graph3.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = jimplePrinter.buildTraps(graph3); + final List traps = briefStmtPrinter.buildTraps(graph3); assertEquals(5, graph2.getBlocks().size()); assertEquals(2, traps.size()); } @@ -701,7 +703,7 @@ public PackageName getPackageName() { graph4.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph4.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); - assertEquals(3, jimplePrinter.buildTraps(graph4).size()); + assertEquals(3, briefStmtPrinter.buildTraps(graph4).size()); // mixed 2 MutableBlockStmtGraph graph5 = new MutableBlockStmtGraph(); @@ -736,7 +738,7 @@ public PackageName getPackageName() { graph5.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = jimplePrinter.buildTraps(graph5); + final List traps = briefStmtPrinter.buildTraps(graph5); assertEquals(6, traps.size()); assertEquals(6, graph5.getBlocks().size()); } @@ -772,7 +774,7 @@ public PackageName getPackageName() { graph6.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph6.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = jimplePrinter.buildTraps(graph6); + final List traps = briefStmtPrinter.buildTraps(graph6); assertEquals(5, traps.size()); assertEquals(6, graph6.getBlocks().size()); assertEquals( @@ -1056,7 +1058,7 @@ public void testRemoveSingleTrap() { graph.addExceptionalEdge(stmt1, throwableSig, handlerStmt); // Verify the trap is present - List traps = jimplePrinter.buildTraps(graph); + List traps = briefStmtPrinter.buildTraps(graph); assertEquals(1, traps.size()); assertEquals(stmt1, traps.get(0).getBeginStmt()); assertEquals(handlerStmt, traps.get(0).getHandlerStmt()); @@ -1065,7 +1067,7 @@ public void testRemoveSingleTrap() { Trap trapToRemove = traps.get(0); graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = jimplePrinter.buildTraps(graph); + traps = briefStmtPrinter.buildTraps(graph); assertEquals(0, traps.size()); } @@ -1099,7 +1101,7 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.addExceptionalEdge(stmt2, ioExceptionSig, handlerStmt2); // Verify both traps are present - List traps = jimplePrinter.buildTraps(graph); + List traps = briefStmtPrinter.buildTraps(graph); assertEquals(2, traps.size()); // Remove one trap and verify the remaining @@ -1108,7 +1110,7 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = jimplePrinter.buildTraps(graph); + traps = briefStmtPrinter.buildTraps(graph); assertEquals(1, traps.size()); assertEquals(stmt2, trapToKeep.getBeginStmt()); assertEquals(handlerStmt2, trapToKeep.getHandlerStmt()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java index f6d1bcb1f3d..b75ceae5431 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java @@ -23,6 +23,7 @@ import sootup.core.types.ClassType; import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.TrapTightener; import sootup.java.core.JavaIdentifierFactory; @@ -35,6 +36,7 @@ @Disabled("FIXME: needs .setTraps() adapted to MutableBlockStmtGraph") public class TrapTightenerTest { public JimplePrinter jimplePrinter = new JimplePrinter(); + public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); StmtPositionInfo noStmtPositionInfo = StmtPositionInfo.getNoStmtPositionInfo(); @@ -134,7 +136,7 @@ public void testSimpleBody() { List excepted = new ArrayList<>(); excepted.add(trap3); - List actual = jimplePrinter.buildTraps(stmtGraph); + List actual = briefStmtPrinter.buildTraps(stmtGraph); AssertUtils.assertTrapsEquiv(excepted, actual); } /** @@ -175,7 +177,7 @@ public void testMonitoredBody() { List excepted = new ArrayList<>(); excepted.add(trap1); - List actual = jimplePrinter.buildTraps(stmtGraph); + List actual = briefStmtPrinter.buildTraps(stmtGraph); AssertUtils.assertTrapsEquiv(excepted, actual); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java index 77845f7b464..80a45f4dd27 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java @@ -23,6 +23,7 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.UnreachableCodeEliminator; import sootup.java.core.JavaIdentifierFactory; @@ -35,6 +36,7 @@ public class UnreachableCodeEliminatorTest { public final JimplePrinter jimplePrinter = new JimplePrinter(); + public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); JavaJimple javaJimple = JavaJimple.getInstance(); @@ -142,7 +144,7 @@ public void testTrappedBody1() { new UnreachableCodeEliminator().interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, jimplePrinter.buildTraps(builder.getStmtGraph()).size()); + assertEquals(0, briefStmtPrinter.buildTraps(builder.getStmtGraph()).size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); AssertUtils.assertSetsEquiv(expectedStmtsSet, builder.getStmtGraph().getNodes()); @@ -178,7 +180,7 @@ public void testTrappedBody2() { UnreachableCodeEliminator eliminator = new UnreachableCodeEliminator(); eliminator.interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, jimplePrinter.buildTraps(builder.getStmtGraph()).size()); + assertEquals(0, briefStmtPrinter.buildTraps(builder.getStmtGraph()).size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); assertEquals(expectedStmtsSet, builder.getStmtGraph().getNodes()); diff --git a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java index 1b131a16222..b8d9e2812da 100644 --- a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java +++ b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java @@ -49,6 +49,7 @@ public void testBasicTrapSerialization() { assertTrue(nestedTrap.isPresent()); JavaSootMethod nestedTrapMethod = nestedTrap.get(); System.out.println(nestedTrapMethod.getBody()); + nestedTrapMethod.getBody().getStmtGraph().toString(); } @Test diff --git a/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java b/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java index fde6aef70e4..dae76bf9379 100644 --- a/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java +++ b/sootup.tests/src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/TrapSerialization.java @@ -9,16 +9,16 @@ public Integer processWithExplicitCasting(String var2, String var4) throws Excep Object var19; try { try { - var19 = 10; + var19 = 10; // Label1 throw new Exception(); } catch (Exception e) { - var19 = 20; + var19 = 20; // Label2 throw new Exception(e); } finally { - var19 = 30; + var19 = 30; // Label5 } } catch (Exception ex) { - var19 = 40; + var19 = 40; // Label3 } return (Integer) var19; } From 51d049b924d4a522b02c8603375d9860a3a15b5e Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 12 Dec 2024 21:27:59 +0100 Subject: [PATCH 05/13] refactor buildtraps method, removed javapoet testcase --- .../sootup/core/graph/BlockGraphIterator.java | 337 +++++++++--------- .../core/graph/ForwardingStmtGraph.java | 1 - .../core/graph/ImmutableBlockStmtGraph.java | 1 - .../core/graph/MutableBlockStmtGraph.java | 1 - .../java/sootup/core/graph/StmtGraph.java | 8 +- .../BlockGraphIteratorAndTrapAggregator.java | 155 ++++---- .../core/util/printer/JimplePrinter.java | 22 +- .../core/util/printer/LabeledStmtPrinter.java | 48 +-- sootup.tests/pom.xml | 7 - .../sootup/tests/JimpleSerializationTest.java | 204 +++-------- 10 files changed, 326 insertions(+), 458 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java index ffcb1a6d827..3a094fdd342 100644 --- a/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java +++ b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java @@ -1,194 +1,187 @@ package sootup.core.graph; +import java.util.*; +import java.util.stream.Collectors; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import sootup.core.jimple.common.stmt.JGotoStmt; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.types.ClassType; import sootup.core.util.DotExporter; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.*; -import java.util.stream.Collectors; - -/** - * Iterates over the blocks - */ +/** Iterates over the blocks */ public class BlockGraphIterator implements Iterator> { - private final StmtGraph stmtGraph; - @Nonnull - private final ArrayDeque> trapHandlerBlocks = new ArrayDeque<>(); - - @Nonnull - private final ArrayDeque> nestedBlocks = new ArrayDeque<>(); - @Nonnull - private final ArrayDeque> otherBlocks = new ArrayDeque<>(); - @Nonnull - private final Set> iteratedBlocks; - - public BlockGraphIterator(StmtGraph stmtGraph) { - this.stmtGraph = stmtGraph; - final Collection> blocks = stmtGraph.getBlocks(); - iteratedBlocks = new LinkedHashSet<>(blocks.size(), 1); - Stmt startingStmt = stmtGraph.getStartingStmt(); - if (startingStmt != null) { - final BasicBlock startingBlock = stmtGraph.getStartingStmtBlock(); - updateFollowingBlocks(startingBlock); - nestedBlocks.addFirst(startingBlock); - } + private final StmtGraph stmtGraph; + @Nonnull private final ArrayDeque> trapHandlerBlocks = new ArrayDeque<>(); + + @Nonnull private final ArrayDeque> nestedBlocks = new ArrayDeque<>(); + @Nonnull private final ArrayDeque> otherBlocks = new ArrayDeque<>(); + @Nonnull private final Set> iteratedBlocks; + + public BlockGraphIterator(StmtGraph stmtGraph) { + this.stmtGraph = stmtGraph; + final Collection> blocks = stmtGraph.getBlocks(); + iteratedBlocks = new LinkedHashSet<>(blocks.size(), 1); + Stmt startingStmt = stmtGraph.getStartingStmt(); + if (startingStmt != null) { + final BasicBlock startingBlock = stmtGraph.getStartingStmtBlock(); + updateFollowingBlocks(startingBlock); + nestedBlocks.addFirst(startingBlock); } - - @Nullable - private BasicBlock retrieveNextBlock() { - BasicBlock nextBlock; - do { - if (!nestedBlocks.isEmpty()) { - nextBlock = nestedBlocks.pollFirst(); - } else if (!trapHandlerBlocks.isEmpty()) { - nextBlock = trapHandlerBlocks.pollFirst(); - } else if (!otherBlocks.isEmpty()) { - nextBlock = otherBlocks.pollFirst(); - } else { - Collection> blocks = stmtGraph.getBlocks(); - if (iteratedBlocks.size() < blocks.size()) { - // graph is not connected! iterate/append all not connected blocks at the end in no - // particular order. - for (BasicBlock block : blocks) { - if (!iteratedBlocks.contains(block)) { - nestedBlocks.addLast(block); - } - } - if (!nestedBlocks.isEmpty()) { - return nestedBlocks.pollFirst(); - } - } - - return null; + } + + @Nullable + private BasicBlock retrieveNextBlock() { + BasicBlock nextBlock; + do { + if (!nestedBlocks.isEmpty()) { + nextBlock = nestedBlocks.pollFirst(); + } else if (!trapHandlerBlocks.isEmpty()) { + nextBlock = trapHandlerBlocks.pollFirst(); + } else if (!otherBlocks.isEmpty()) { + nextBlock = otherBlocks.pollFirst(); + } else { + Collection> blocks = stmtGraph.getBlocks(); + if (iteratedBlocks.size() < blocks.size()) { + // graph is not connected! iterate/append all not connected blocks at the end in no + // particular order. + for (BasicBlock block : blocks) { + if (!iteratedBlocks.contains(block)) { + nestedBlocks.addLast(block); } + } + if (!nestedBlocks.isEmpty()) { + return nestedBlocks.pollFirst(); + } + } - // skip retrieved nextBlock if its already returned - } while (iteratedBlocks.contains(nextBlock)); - return nextBlock; - } + return null; + } - @Override - @Nonnull - public BasicBlock next() { - BasicBlock currentBlock = retrieveNextBlock(); - if (currentBlock == null) { - throw new NoSuchElementException("Iterator has no more Blocks."); - } - updateFollowingBlocks(currentBlock); - iteratedBlocks.add(currentBlock); - return currentBlock; + // skip retrieved nextBlock if its already returned + } while (iteratedBlocks.contains(nextBlock)); + return nextBlock; + } + + @Override + @Nonnull + public BasicBlock next() { + BasicBlock currentBlock = retrieveNextBlock(); + if (currentBlock == null) { + throw new NoSuchElementException("Iterator has no more Blocks."); + } + updateFollowingBlocks(currentBlock); + iteratedBlocks.add(currentBlock); + return currentBlock; + } + + private void updateFollowingBlocks(BasicBlock currentBlock) { + // collect traps + final Stmt tailStmt = currentBlock.getTail(); + for (Map.Entry> entry : + currentBlock.getExceptionalSuccessors().entrySet()) { + BasicBlock trapHandlerBlock = entry.getValue(); + trapHandlerBlocks.addLast(trapHandlerBlock); + nestedBlocks.addFirst(trapHandlerBlock); } - private void updateFollowingBlocks(BasicBlock currentBlock) { - // collect traps - final Stmt tailStmt = currentBlock.getTail(); - for (Map.Entry> entry : - currentBlock.getExceptionalSuccessors().entrySet()) { - BasicBlock trapHandlerBlock = entry.getValue(); - trapHandlerBlocks.addLast(trapHandlerBlock); - nestedBlocks.addFirst(trapHandlerBlock); + final List> successors = currentBlock.getSuccessors(); + + for (int i = successors.size() - 1; i >= 0; i--) { + if (i == 0 && tailStmt.fallsThrough()) { + // non-branching successors i.e. not a BranchingStmt or is the first successor (i.e. its + // false successor) of + // JIfStmt + nestedBlocks.addFirst(successors.get(0)); + } else { + + // create the longest FallsThroughStmt sequence possible + final BasicBlock successorBlock = successors.get(i); + BasicBlock leaderOfFallsthroughBlocks = successorBlock; + while (true) { + final List> itPreds = + leaderOfFallsthroughBlocks.getPredecessors(); + + BasicBlock finalLeaderOfFallsthroughBlocks = leaderOfFallsthroughBlocks; + final Optional> fallsthroughPredOpt = + itPreds.stream() + .filter( + b -> + b.getTail().fallsThrough() + && b.getSuccessors().get(0) == finalLeaderOfFallsthroughBlocks) + .findAny(); + if (!fallsthroughPredOpt.isPresent()) { + break; + } + BasicBlock predecessorBlock = fallsthroughPredOpt.get(); + if (predecessorBlock.getTail().fallsThrough() + && predecessorBlock.getSuccessors().get(0) == leaderOfFallsthroughBlocks) { + leaderOfFallsthroughBlocks = predecessorBlock; + } else { + break; + } } - final List> successors = currentBlock.getSuccessors(); - - for (int i = successors.size() - 1; i >= 0; i--) { - if (i == 0 && tailStmt.fallsThrough()) { - // non-branching successors i.e. not a BranchingStmt or is the first successor (i.e. its - // false successor) of - // JIfStmt - nestedBlocks.addFirst(successors.get(0)); - } else { - - // create the longest FallsThroughStmt sequence possible - final BasicBlock successorBlock = successors.get(i); - BasicBlock leaderOfFallsthroughBlocks = successorBlock; - while (true) { - final List> itPreds = - leaderOfFallsthroughBlocks.getPredecessors(); - - BasicBlock finalLeaderOfFallsthroughBlocks = leaderOfFallsthroughBlocks; - final Optional> fallsthroughPredOpt = - itPreds.stream() - .filter( - b -> - b.getTail().fallsThrough() - && b.getSuccessors().get(0) == finalLeaderOfFallsthroughBlocks) - .findAny(); - if (!fallsthroughPredOpt.isPresent()) { - break; - } - BasicBlock predecessorBlock = fallsthroughPredOpt.get(); - if (predecessorBlock.getTail().fallsThrough() - && predecessorBlock.getSuccessors().get(0) == leaderOfFallsthroughBlocks) { - leaderOfFallsthroughBlocks = predecessorBlock; - } else { - break; - } - } - - // find a return Stmt inside the current Block - Stmt succTailStmt = successorBlock.getTail(); - boolean hasNoSuccessorStmts = succTailStmt.getExpectedSuccessorCount() == 0; - boolean isExceptionFree = successorBlock.getExceptionalSuccessors().isEmpty(); - - boolean isLastStmtCandidate = hasNoSuccessorStmts && isExceptionFree; - // remember branching successors - if (tailStmt instanceof JGotoStmt) { - if (isLastStmtCandidate) { - nestedBlocks.removeFirstOccurrence(currentBlock); - otherBlocks.addLast(leaderOfFallsthroughBlocks); - } else { - otherBlocks.addFirst(leaderOfFallsthroughBlocks); - } - } else if (!nestedBlocks.contains(leaderOfFallsthroughBlocks)) { - // JSwitchStmt, JIfStmt - if (isLastStmtCandidate) { - nestedBlocks.addLast(leaderOfFallsthroughBlocks); - } else { - nestedBlocks.addFirst(leaderOfFallsthroughBlocks); - } - } - } + // find a return Stmt inside the current Block + Stmt succTailStmt = successorBlock.getTail(); + boolean hasNoSuccessorStmts = succTailStmt.getExpectedSuccessorCount() == 0; + boolean isExceptionFree = successorBlock.getExceptionalSuccessors().isEmpty(); + + boolean isLastStmtCandidate = hasNoSuccessorStmts && isExceptionFree; + // remember branching successors + if (tailStmt instanceof JGotoStmt) { + if (isLastStmtCandidate) { + nestedBlocks.removeFirstOccurrence(currentBlock); + otherBlocks.addLast(leaderOfFallsthroughBlocks); + } else { + otherBlocks.addFirst(leaderOfFallsthroughBlocks); + } + } else if (!nestedBlocks.contains(leaderOfFallsthroughBlocks)) { + // JSwitchStmt, JIfStmt + if (isLastStmtCandidate) { + nestedBlocks.addLast(leaderOfFallsthroughBlocks); + } else { + nestedBlocks.addFirst(leaderOfFallsthroughBlocks); + } } + } + } + } + + @Override + public boolean hasNext() { + final boolean hasIteratorMoreElements; + BasicBlock b = retrieveNextBlock(); + if (b != null) { + // reinsert at FIRST position -> not great for performance - but easier handling in + // next() + nestedBlocks.addFirst(b); + hasIteratorMoreElements = true; + } else { + hasIteratorMoreElements = false; } - @Override - public boolean hasNext() { - final boolean hasIteratorMoreElements; - BasicBlock b = retrieveNextBlock(); - if (b != null) { - // reinsert at FIRST position -> not great for performance - but easier handling in - // next() - nestedBlocks.addFirst(b); - hasIteratorMoreElements = true; - } else { - hasIteratorMoreElements = false; - } - - // "assertion" that all elements are iterated - if (!hasIteratorMoreElements) { - final int returnedSize = iteratedBlocks.size(); - final Collection> blocks = stmtGraph.getBlocks(); - final int actualSize = blocks.size(); - if (returnedSize != actualSize) { - String info = - blocks.stream() - .filter(n -> !iteratedBlocks.contains(n)) - .map(BasicBlock::getStmts) - .collect(Collectors.toList()) - .toString(); - throw new IllegalStateException( - "There are " - + (actualSize - returnedSize) - + " Blocks that are not iterated! i.e. the StmtGraph is not connected from its startingStmt!" - + info - + DotExporter.createUrlToWebeditor(stmtGraph)); - } - } - return hasIteratorMoreElements; + // "assertion" that all elements are iterated + if (!hasIteratorMoreElements) { + final int returnedSize = iteratedBlocks.size(); + final Collection> blocks = stmtGraph.getBlocks(); + final int actualSize = blocks.size(); + if (returnedSize != actualSize) { + String info = + blocks.stream() + .filter(n -> !iteratedBlocks.contains(n)) + .map(BasicBlock::getStmts) + .collect(Collectors.toList()) + .toString(); + throw new IllegalStateException( + "There are " + + (actualSize - returnedSize) + + " Blocks that are not iterated! i.e. the StmtGraph is not connected from its startingStmt!" + + info + + DotExporter.createUrlToWebeditor(stmtGraph)); + } } + return hasIteratorMoreElements; + } } diff --git a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java index a01278307fd..32e161e4757 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ForwardingStmtGraph.java @@ -23,7 +23,6 @@ */ import java.util.*; import javax.annotation.Nonnull; -import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.types.ClassType; diff --git a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java index 43fcd101521..03ed3d469a1 100644 --- a/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/ImmutableBlockStmtGraph.java @@ -26,7 +26,6 @@ import java.util.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.types.ClassType; diff --git a/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java index 2798f6640fb..9cd15096eeb 100644 --- a/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/MutableBlockStmtGraph.java @@ -22,7 +22,6 @@ * #L% */ -import com.google.common.collect.ComparisonChain; import com.google.common.collect.Lists; import java.util.*; import javax.annotation.Nonnull; diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index bb3b919e273..b3f4be517bd 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -29,7 +29,6 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.ref.IdentityRef; import sootup.core.jimple.common.ref.JCaughtExceptionRef; import sootup.core.jimple.common.stmt.*; @@ -202,9 +201,9 @@ public Collection getEntrypoints() { public void validateStmtConnectionsInGraph() { try { List handlerStmts = new ArrayList<>(); - for (Stmt stmt: getNodes()){ + for (Stmt stmt : getNodes()) { if (stmt instanceof JIdentityStmt) { - //JThrowStmt? + // JThrowStmt? IdentityRef rightOp = ((JIdentityStmt) stmt).getRightOp(); if (rightOp instanceof JCaughtExceptionRef) { handlerStmts.add(stmt); @@ -219,8 +218,7 @@ public void validateStmtConnectionsInGraph() { if (predecessors(stmt).isEmpty()) { if (!(stmt == getStartingStmt() - || handlerStmts.stream() - .anyMatch(handler -> handler == stmt))) { + || handlerStmts.stream().anyMatch(handler -> handler == stmt))) { throw new IllegalStateException( "Stmt '" + stmt diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java index 34010746104..4914156a231 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java @@ -1,5 +1,11 @@ package sootup.core.util.printer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import sootup.core.graph.BasicBlock; import sootup.core.graph.BlockGraphIterator; import sootup.core.graph.MutableBasicBlockImpl; @@ -10,96 +16,93 @@ import sootup.core.jimple.common.stmt.Stmt; import sootup.core.types.ClassType; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** - * Iterates over the Blocks and collects/aggregates Trap information - * It is used to collect and aggregate traps for serializing Jimple in the JimplePrinter + * Iterates over the Blocks and collects/aggregates Trap information It is used to collect and + * aggregate traps for serializing Jimple in the JimplePrinter */ class BlockGraphIteratorAndTrapAggregator extends BlockGraphIterator { - @Nonnull - private final List collectedTraps = new ArrayList<>(); + @Nonnull private final List collectedTraps = new ArrayList<>(); - Map activeTraps = new HashMap<>(); - BasicBlock lastIteratedBlock; // dummy value to remove n-1 unnecessary null-checks - JNopStmt lastStmt = null; + Map activeTraps = new HashMap<>(); + BasicBlock lastIteratedBlock; // dummy value to remove n-1 unnecessary null-checks + JNopStmt lastStmt = null; - /* - * @param dummyBlock is just an empty instantiation of type V - as neither BasicBlock nor V instantiable we need a concrete object from the using subclass itclass. - * */ - public BlockGraphIteratorAndTrapAggregator(StmtGraph stmtGraph) { - super(stmtGraph); - lastIteratedBlock = new MutableBasicBlockImpl(); - } + /* + * @param dummyBlock is just an empty instantiation of type V - as neither BasicBlock nor V instantiable we need a concrete object from the using subclass itclass. + * */ + public BlockGraphIteratorAndTrapAggregator(StmtGraph stmtGraph) { + super(stmtGraph); + lastIteratedBlock = new MutableBasicBlockImpl(); + } - @Nullable - public JNopStmt getLastStmt() { - return lastStmt; - } + @Nullable + public JNopStmt getLastStmt() { + return lastStmt; + } - @Nonnull - @Override - public BasicBlock next() { - final BasicBlock block = super.next(); + @Nonnull + @Override + public BasicBlock next() { + final BasicBlock block = super.next(); - final Map> currentBlocksExceptions = - block.getExceptionalSuccessors(); - final Map> lastBlocksExceptions = - lastIteratedBlock.getExceptionalSuccessors(); + final Map> currentBlocksExceptions = + block.getExceptionalSuccessors(); + final Map> lastBlocksExceptions = + lastIteratedBlock.getExceptionalSuccessors(); - // former trap info is not in the current blocks info -> add it to the trap collection - lastBlocksExceptions.forEach( - (type, trapHandlerBlock) -> { - if (trapHandlerBlock != block.getExceptionalSuccessors().get(type)) { - final Stmt trapBeginStmt = activeTraps.remove(type); - if (trapBeginStmt == null) { - throw new IllegalStateException("Trap start for '" + type + "' is not in the Map!"); - } - // trapend is exclusive! - collectedTraps.add( - new Trap(type, trapBeginStmt, block.getHead(), trapHandlerBlock.getHead())); - } - }); + // former trap info is not in the current blocks info -> add it to the trap collection + lastBlocksExceptions.forEach( + (type, trapHandlerBlock) -> { + if (trapHandlerBlock != block.getExceptionalSuccessors().get(type)) { + final Stmt trapBeginStmt = activeTraps.remove(type); + if (trapBeginStmt == null) { + throw new IllegalStateException("Trap start for '" + type + "' is not in the Map!"); + } + // trapend is exclusive! + collectedTraps.add( + new Trap(type, trapBeginStmt, block.getHead(), trapHandlerBlock.getHead())); + } + }); - // is there a new trap in the current block -> add it to currentTraps - block - .getExceptionalSuccessors() - .forEach( - (type, trapHandlerBlock) -> { - if (trapHandlerBlock != lastBlocksExceptions.get(type)) { - activeTraps.put(type, block.getHead()); - } - }); + // is there a new trap in the current block -> add it to currentTraps + block + .getExceptionalSuccessors() + .forEach( + (type, trapHandlerBlock) -> { + if (trapHandlerBlock != lastBlocksExceptions.get(type)) { + activeTraps.put(type, block.getHead()); + } + }); - lastIteratedBlock = block; - return block; - } + lastIteratedBlock = block; + return block; + } - /** - * for jimple serialization - this info contains only valid/useful information if all stmts are - * iterated i.e. hasNext() == false! - * - * @return List of Traps - */ - public List getTraps() { + /** + * for jimple serialization - this info contains only valid/useful information if all stmts are + * iterated i.e. hasNext() == false! + * + * @return List of Traps + */ + public List getTraps() { - if (hasNext()) { - throw new IllegalStateException("Iterator needs to be iterated completely!"); - } + if (hasNext()) { + throw new IllegalStateException("Iterator needs to be iterated completely!"); + } - // check for dangling traps that are not collected as the endStmt was not visited. - if (!activeTraps.isEmpty()) { - lastStmt = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); -// throw new IllegalArgumentException( -// "Invalid StmtGraph. A Trap is not created as a traps endStmt was not visited during the iteration of all Stmts."); - activeTraps.forEach((type, beginning) -> collectedTraps.add(new Trap(type, beginning, lastStmt, lastIteratedBlock.getExceptionalSuccessors().get(type).getHead()))); - } - return collectedTraps; + // check for dangling traps that are not collected as the endStmt was not visited. + if (!activeTraps.isEmpty()) { + lastStmt = new JNopStmt(StmtPositionInfo.getNoStmtPositionInfo()); + activeTraps.forEach( + (type, beginning) -> + collectedTraps.add( + new Trap( + type, + beginning, + lastStmt, + lastIteratedBlock.getExceptionalSuccessors().get(type).getHead()))); } + return collectedTraps; + } } diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index d05ff99b198..69525e58d49 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -22,7 +22,10 @@ * #L% */ -import com.google.common.collect.ComparisonChain; +import java.io.PrintWriter; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; import sootup.core.graph.*; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.Local; @@ -35,12 +38,6 @@ import sootup.core.types.ClassType; import sootup.core.types.Type; -import javax.annotation.Nonnull; -import java.io.PrintWriter; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; - /** * Prints out a class and all its methods. * @@ -339,7 +336,8 @@ private void printStatementsInBody(Body body, LabeledStmtPrinter printer) { } private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { - Iterable linearizedStmtGraph = printer.initializeSootMethod(stmtGraph); + final List traps = printer.buildTraps(stmtGraph); + Iterable linearizedStmtGraph = printer.initializeSootMethod(stmtGraph, traps); Stmt previousStmt; @@ -357,10 +355,10 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { // a trap) final boolean currentStmtHasLabel = labels.get(currentStmt) != null; - if (previousStmt.branches() + if (currentStmtHasLabel + || previousStmt.branches() || stmtGraph.predecessors(currentStmt).size() != 1 - || previousStmt.getExpectedSuccessorCount() == 0 - || currentStmtHasLabel) { + || previousStmt.getExpectedSuccessorCount() == 0) { printer.newline(); } @@ -383,7 +381,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { // Print out exceptions { - Iterator trapIt = printer.buildTraps(stmtGraph).iterator(); + Iterator trapIt = traps.iterator(); if (trapIt.hasNext()) { printer.newline(); diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index abaf19a05b8..6fa53f05066 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -22,12 +22,10 @@ * #L% */ +import com.google.common.collect.ComparisonChain; import java.util.*; import javax.annotation.Nonnull; - -import com.google.common.collect.ComparisonChain; -import sootup.core.graph.BasicBlock; -import sootup.core.graph.StmtGraph; +import sootup.core.graph.*; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.ref.IdentityRef; @@ -109,16 +107,15 @@ public void stmtRef(Stmt stmt, boolean branchTarget) { * * @return the linearized StmtGraph */ - public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph) { + public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph, List traps) { this.graph = stmtGraph; - final List linearizedStmtGraph = getStmts(stmtGraph); + final List linearizedStmtGraph = getStmts(stmtGraph, traps); return linearizedStmtGraph; } @Nonnull - public List getStmts(@Nonnull StmtGraph stmtGraph) { - final Collection targetStmtsOfBranches = getLabeledStmts(stmtGraph); - final List traps = buildTraps(stmtGraph); + public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { + final Collection targetStmtsOfBranches = getLabeledStmts(stmtGraph, traps); final int maxEstimatedSize = targetStmtsOfBranches.size() + traps.size() * 3; labels = new HashMap<>(maxEstimatedSize, 1); @@ -140,7 +137,7 @@ public List getStmts(@Nonnull StmtGraph stmtGraph) { // or is the begin of a trap-range) or does it mark the end of a trap range // does it need a label for (Stmt stmt : targetStmtsOfBranches) { - if ((stmt instanceof JNopStmt) || stmtGraph.isStmtBranchTarget(stmt) || trapStmts.contains(stmt)) { + if (trapStmts.contains(stmt) || stmtGraph.isStmtBranchTarget(stmt)) { labelStmts.add(stmt); } else { refStmts.add(stmt); @@ -167,6 +164,15 @@ public List getStmts(@Nonnull StmtGraph stmtGraph) { references.put(s, Integer.toString(refCount++)); } } + + // add Nop Stmt to Jimple just for serialization + for (Stmt s : targetStmtsOfBranches) { + if (s instanceof JNopStmt) { + linearizedStmtGraph.add(s); + labels.put(s, String.format(formatString, ++labelCount)); + } + } + return linearizedStmtGraph; } @@ -233,12 +239,12 @@ public List buildTraps(StmtGraph stmtGraph) { /** Comparator which sorts the trap output in getTraps() */ public Comparator getTrapComparator(@Nonnull Map stmtsBlockIdx) { return (a, b) -> - ComparisonChain.start() - .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) - .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) - // [ms] would be nice to have the traps ordered by exception hierarchy as well - .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) - .result(); + ComparisonChain.start() + .compare(stmtsBlockIdx.get(a.getBeginStmt()), stmtsBlockIdx.get(b.getBeginStmt())) + .compare(stmtsBlockIdx.get(a.getEndStmt()), stmtsBlockIdx.get(b.getEndStmt())) + // [ms] would be nice to have the traps ordered by exception hierarchy as well + .compare(a.getExceptionType().toString(), b.getExceptionType().toString()) + .result(); } /** @@ -252,23 +258,25 @@ public Comparator getTrapComparator(@Nonnull Map stmtsBlock * @return A collection of all the Stmts that are targets of a BranchingStmt */ @Nonnull - public Collection getLabeledStmts(StmtGraph stmtGraph) { + public Collection getLabeledStmts(StmtGraph stmtGraph, List traps) { Set stmtList = new HashSet<>(); Collection stmtGraphNodes = stmtGraph.getNodes(); for (Stmt stmt : stmtGraphNodes) { if (stmt instanceof BranchingStmt) { if (stmt instanceof JIfStmt) { - stmtList.add((Stmt) stmtGraph.getBranchTargetsOf((JIfStmt) stmt).get(JIfStmt.FALSE_BRANCH_IDX)); + stmtList.add( + (Stmt) stmtGraph.getBranchTargetsOf((JIfStmt) stmt).get(JIfStmt.FALSE_BRANCH_IDX)); } else if (stmt instanceof JGotoStmt) { // [ms] bounds are validated in Body if its a valid StmtGraph - stmtList.add((Stmt) stmtGraph.getBranchTargetsOf((JGotoStmt) stmt).get(JGotoStmt.BRANCH_IDX)); + stmtList.add( + (Stmt) stmtGraph.getBranchTargetsOf((JGotoStmt) stmt).get(JGotoStmt.BRANCH_IDX)); } else if (stmt instanceof JSwitchStmt) { stmtList.addAll(stmtGraph.getBranchTargetsOf((BranchingStmt) stmt)); } } } - for (Trap trap : buildTraps(stmtGraph)) { + for (Trap trap : traps) { stmtList.add(trap.getBeginStmt()); stmtList.add(trap.getEndStmt()); stmtList.add(trap.getHandlerStmt()); diff --git a/sootup.tests/pom.xml b/sootup.tests/pom.xml index 1562de85a68..ff609544283 100644 --- a/sootup.tests/pom.xml +++ b/sootup.tests/pom.xml @@ -44,13 +44,6 @@ slf4j-simple test - - com.squareup - javapoet - 1.13.0 - test - - diff --git a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java index b8d9e2812da..0fdc463796e 100644 --- a/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java +++ b/sootup.tests/src/test/java/sootup/tests/JimpleSerializationTest.java @@ -1,177 +1,55 @@ package sootup.tests; -import com.squareup.javapoet.*; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.*; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.inputlocation.AnalysisInputLocation; import sootup.core.model.SourceType; -import sootup.core.signatures.MethodSignature; -import sootup.core.types.PrimitiveType; -import sootup.core.types.VoidType; import sootup.java.bytecode.frontend.inputlocation.JavaClassPathAnalysisInputLocation; import sootup.java.core.JavaSootMethod; import sootup.java.core.views.JavaView; -import sootup.jimple.frontend.JimpleAnalysisInputLocation; -import sootup.jimple.frontend.JimpleStringAnalysisInputLocation; -import sootup.jimple.frontend.JimpleView; - -import javax.lang.model.element.Modifier; -import java.io.IOException; -import java.nio.file.Paths; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.assertTrue; @Tag("Java8") public class JimpleSerializationTest { - @Test - public void testTrapSerialization() { - AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList()); - JavaView view = new JavaView(inputLocation); - - Optional methodOpt = view.getMethod(view.getIdentifierFactory().parseMethodSignature( - "" - )); - assertTrue(methodOpt.isPresent()); - JavaSootMethod method = methodOpt.get(); - method.getBody().toString(); - } - - @Test - public void testBasicTrapSerialization() { - AnalysisInputLocation inputLocation = new JavaClassPathAnalysisInputLocation("src/test/resources/bugs/1119_trap-serialization", SourceType.Application, Collections.emptyList()); - JavaView javaView = new JavaView(inputLocation); - Optional nestedTrap = javaView.getMethod(javaView.getIdentifierFactory().parseMethodSignature( - "" - )); - - assertTrue(nestedTrap.isPresent()); - JavaSootMethod nestedTrapMethod = nestedTrap.get(); - System.out.println(nestedTrapMethod.getBody()); - nestedTrapMethod.getBody().getStmtGraph().toString(); - } - - @Test - public void testJimpleTrapSerialization() { - String jimpleString = "class DummyClass extends java.lang.Object {\n" + - "\tint testTrapSerialization() {\n" + - "\t\tsootUp.RQ1.jb_a.JB_A this;\n" + - "\t\tunknown $stack4, $stack5, a, b, e;\n" + - "\n" + - "\n" + - "\t\tthis := @this: sootUp.RQ1.jb_a.JB_A;\n" + - "\t\ta = 0;\n" + - "\t\tb = 10;\n" + - "\n" + - "\t label1:\n" + - "\t\tb = b / a;\n" + - "\t\t$stack4 = b;\n" + - "\t\treturn a;\n" + - "\n" + - "\t label2:\n" + - "\t\treturn $stack4;\n" + - "\n" + - "\t label3:\n" + - "\t\t$stack5 := @caughtexception;\n" + - "\t\te = $stack5;\n" + - "\n" + - "\t\treturn b;\n" + - "\n" + - "\t catch java.lang.ArithmeticException from label1 to label2 with label3;\n" + - "\t catch java.lang.NullPointerException from label1 to label2 with label3;\n" + - "\t}\n" + - "}"; - - JimpleStringAnalysisInputLocation analysisInputLocation = new JimpleStringAnalysisInputLocation(jimpleString, SourceType.Application, Collections.emptyList()); - JimpleView view = new JimpleView(analysisInputLocation); - assertTrue(view.getClass(analysisInputLocation.getClassType()).isPresent()); - MethodSignature methodSig = - view.getIdentifierFactory() - .getMethodSignature( - analysisInputLocation.getClassType(), - "testTrapSerialization", - PrimitiveType.IntType.getInstance(), - Collections.emptyList()); - assertTrue(view.getMethod(methodSig).isPresent()); - } - - - @Test - public void addNopInEndOfTryCatchFinally() throws IOException { - // Define the method body - CodeBlock methodBody = CodeBlock.builder() - .addStatement("$T> result = new $T<>()", Map.class, Map.Entry.class, HashMap.class) - .beginControlFlow("try") - .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "try", AbstractMap.class, "Key1", "Value1") - .addStatement("return result") - .nextControlFlow("catch ($T e)", Exception.class) - .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "catch", AbstractMap.class, "Key2", "Value2") - .addStatement("return result") - .nextControlFlow("finally") - .addStatement("result.put($S, new $T.SimpleEntry<>($S, $S))", "finally", AbstractMap.class, "Key3", "Value3") - .endControlFlow() - .addStatement("return result") - .addStatement("return result") - .build(); - - // Create the method - MethodSpec tryCatchFinallyMethod = MethodSpec.methodBuilder("tryCatchFinallyMethod") - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .returns(ParameterizedTypeName.get(Map.class, String.class)) - .addCode(methodBody) - .build(); - - // Create the class - TypeSpec tryCatchFinallyClass = TypeSpec.classBuilder("TryCatchFinallyExample") - .addModifiers(Modifier.PUBLIC) - .addMethod(tryCatchFinallyMethod) - .build(); - - // Write to a Java file - JavaFile javaFile = JavaFile.builder("com.example", tryCatchFinallyClass) - .build(); - - // Write the generated Java file to the file system - javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/")); - } - - @Test - public void addNopInEndOfNestedTrap() throws IOException { - // Define the method body with nested try-catch blocks - CodeBlock methodBody = CodeBlock.builder() - .beginControlFlow("try") - .addStatement("System.out.println($S)", "Outer try block") - .beginControlFlow("try") - .addStatement("System.out.println($S)", "Inner try block") - .addStatement("throw new RuntimeException($S)", "Inner exception") - .nextControlFlow("catch (Exception e)") - .addStatement("System.out.println($S + e.getMessage())", "Caught inner exception: ") - .endControlFlow() - .nextControlFlow("catch (Exception e)") - .addStatement("System.out.println($S + e.getMessage())", "Caught outer exception: ") - .endControlFlow() - .build(); - - // Create the method - MethodSpec nestedTryCatchMethod = MethodSpec.methodBuilder("nestedTryCatch") - .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .returns(void.class) - .addCode(methodBody) - .build(); - - // Create the class - TypeSpec nestedTryCatchClass = TypeSpec.classBuilder("NestedTryCatchExample") - .addModifiers(Modifier.PUBLIC) - .addMethod(nestedTryCatchMethod) - .build(); - - // Write to a Java file - JavaFile javaFile = JavaFile.builder("com.example", nestedTryCatchClass) - .build(); - - // Write the generated Java file to the file system - javaFile.writeTo(Paths.get("./src/test/resources/bugs/1119_trap-serialization/com/linecorp/centraldogma/server/internal/storage/repository/git/")); - } - + @Test + public void testTrapSerialization() { + AnalysisInputLocation inputLocation = + new JavaClassPathAnalysisInputLocation( + "src/test/resources/bugs/1119_trap-serialization", + SourceType.Application, + Collections.emptyList()); + JavaView view = new JavaView(inputLocation); + + Optional methodOpt = + view.getMethod( + view.getIdentifierFactory() + .parseMethodSignature( + "")); + assertTrue(methodOpt.isPresent()); + JavaSootMethod method = methodOpt.get(); + method.getBody().toString(); + } + + @Test + public void testBasicTrapSerialization() { + AnalysisInputLocation inputLocation = + new JavaClassPathAnalysisInputLocation( + "src/test/resources/bugs/1119_trap-serialization", + SourceType.Application, + Collections.emptyList()); + JavaView javaView = new JavaView(inputLocation); + Optional nestedTrap = + javaView.getMethod( + javaView + .getIdentifierFactory() + .parseMethodSignature( + "")); + + assertTrue(nestedTrap.isPresent()); + JavaSootMethod nestedTrapMethod = nestedTrap.get(); + nestedTrapMethod.getBody().getStmtGraph().toString(); + } } From 69cfa14b73c26bd2640f298ca0f59f971d5a18eb Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 12 Dec 2024 21:38:17 +0100 Subject: [PATCH 06/13] replace jimplePrinter by briefStmtPrinter --- .../test/java/sootup/core/graph/MutableBlockStmtGraphTest.java | 2 -- .../java/bytecode/frontend/interceptors/TrapTightenerTest.java | 2 -- .../frontend/interceptors/UnreachableCodeEliminatorTest.java | 2 -- 3 files changed, 6 deletions(-) diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java index 92faac8bbaa..e8d938dbeb6 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java @@ -20,12 +20,10 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.UnknownType; import sootup.core.util.printer.BriefStmtPrinter; -import sootup.core.util.printer.JimplePrinter; @Tag("Java8") public class MutableBlockStmtGraphTest { - public JimplePrinter jimplePrinter = new JimplePrinter(); public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); BranchingStmt firstGoto = new JGotoStmt(StmtPositionInfo.getNoStmtPositionInfo()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java index b75ceae5431..52185a9306c 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java @@ -24,7 +24,6 @@ import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; import sootup.core.util.printer.BriefStmtPrinter; -import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.TrapTightener; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; @@ -35,7 +34,6 @@ @Tag(TestCategories.JAVA_8_CATEGORY) @Disabled("FIXME: needs .setTraps() adapted to MutableBlockStmtGraph") public class TrapTightenerTest { - public JimplePrinter jimplePrinter = new JimplePrinter(); public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java index 80a45f4dd27..a51c0bd9061 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java @@ -24,7 +24,6 @@ import sootup.core.types.VoidType; import sootup.core.util.ImmutableUtils; import sootup.core.util.printer.BriefStmtPrinter; -import sootup.core.util.printer.JimplePrinter; import sootup.interceptors.UnreachableCodeEliminator; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.language.JavaJimple; @@ -35,7 +34,6 @@ @Tag(TestCategories.JAVA_8_CATEGORY) public class UnreachableCodeEliminatorTest { - public final JimplePrinter jimplePrinter = new JimplePrinter(); public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); JavaIdentifierFactory factory = JavaIdentifierFactory.getInstance(); From 4496f296a2222badab646c46578ce9d9edc54720 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 12 Dec 2024 21:45:31 +0100 Subject: [PATCH 07/13] copyright headers --- .../sootup/core/graph/BlockGraphIterator.java | 22 +++++++++++++++++++ .../BlockGraphIteratorAndTrapAggregator.java | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java index 3a094fdd342..59e21430235 100644 --- a/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java +++ b/sootup.core/src/main/java/sootup/core/graph/BlockGraphIterator.java @@ -9,6 +9,28 @@ import sootup.core.types.ClassType; import sootup.core.util.DotExporter; +/*- + * #%L + * Soot + * %% + * Copyright (C) 2024 Sahil Agichani + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + /** Iterates over the blocks */ public class BlockGraphIterator implements Iterator> { diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java index 4914156a231..564494a6427 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java @@ -16,6 +16,28 @@ import sootup.core.jimple.common.stmt.Stmt; import sootup.core.types.ClassType; +/*- + * #%L + * Soot + * %% + * Copyright (C) 2024 Sahil Agichani + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 2.1 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * . + * #L% + */ + /** * Iterates over the Blocks and collects/aggregates Trap information It is used to collect and * aggregate traps for serializing Jimple in the JimplePrinter From 48cbe43cab4e90cf72fa529ed04202da3e2fd555 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Thu, 12 Dec 2024 21:56:45 +0100 Subject: [PATCH 08/13] modify conditions --- .../main/java/sootup/core/util/printer/LabeledStmtPrinter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index 6fa53f05066..dd1f8363a25 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -167,7 +167,7 @@ public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { // add Nop Stmt to Jimple just for serialization for (Stmt s : targetStmtsOfBranches) { - if (s instanceof JNopStmt) { + if (trapStmts.contains(s) && s instanceof JNopStmt) { linearizedStmtGraph.add(s); labels.put(s, String.format(formatString, ++labelCount)); } From a9c79134e093d7acc04199be8a5dbbc0dae093ab Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Mon, 6 Jan 2025 14:35:23 +0100 Subject: [PATCH 09/13] review changes --- .../util/printer/BlockGraphIteratorAndTrapAggregator.java | 2 +- .../java/sootup/core/util/printer/LabeledStmtPrinter.java | 8 ++++---- .../jimple/frontend/JimpleAnalysisInputLocationTest.java | 1 - 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java index 564494a6427..d88d5fbb938 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java @@ -47,7 +47,7 @@ class BlockGraphIteratorAndTrapAggregator extends BlockGraphIterator { @Nonnull private final List collectedTraps = new ArrayList<>(); Map activeTraps = new HashMap<>(); - BasicBlock lastIteratedBlock; // dummy value to remove n-1 unnecessary null-checks + BasicBlock lastIteratedBlock; JNopStmt lastStmt = null; /* diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index dd1f8363a25..c49879ef6f9 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -115,9 +115,9 @@ public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph, List @Nonnull public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { - final Collection targetStmtsOfBranches = getLabeledStmts(stmtGraph, traps); + final Collection labeledStmts = getLabeledStmts(stmtGraph, traps); - final int maxEstimatedSize = targetStmtsOfBranches.size() + traps.size() * 3; + final int maxEstimatedSize = labeledStmts.size() + traps.size() * 3; labels = new HashMap<>(maxEstimatedSize, 1); references = new HashMap<>(maxEstimatedSize, 1); @@ -136,7 +136,7 @@ public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { // Build labelStmts and refStmts -> is stmt head of a block (as its a branch target/trapHandler // or is the begin of a trap-range) or does it mark the end of a trap range // does it need a label - for (Stmt stmt : targetStmtsOfBranches) { + for (Stmt stmt : labeledStmts) { if (trapStmts.contains(stmt) || stmtGraph.isStmtBranchTarget(stmt)) { labelStmts.add(stmt); } else { @@ -166,7 +166,7 @@ public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { } // add Nop Stmt to Jimple just for serialization - for (Stmt s : targetStmtsOfBranches) { + for (Stmt s : labeledStmts) { if (trapStmts.contains(s) && s instanceof JNopStmt) { linearizedStmtGraph.add(s); labels.put(s, String.format(formatString, ++labelCount)); diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java index 1f37a6f673f..ccad1d000ee 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleAnalysisInputLocationTest.java @@ -120,7 +120,6 @@ public void testIfBodyInterceptorsApplied() { m -> { if (m.getSignature().getName().contains("tc1")) { String s = m.getBody().toString(); - System.out.println(s); } }); }); From c77d2c3853e80d46900b1761ff9633a53fce0f54 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Tue, 7 Jan 2025 14:31:26 +0100 Subject: [PATCH 10/13] suggested changes --- .../src/main/java/sootup/core/model/Body.java | 12 ------- .../BlockGraphIteratorAndTrapAggregator.java | 6 ++-- .../core/util/printer/JimplePrinter.java | 5 ++- .../core/util/printer/LabeledStmtPrinter.java | 34 +++++++++---------- .../core/validation/JimpleTrapValidator.java | 6 +++- .../core/validation/TrapsValidator.java | 6 +++- .../core/graph/MutableBlockStmtGraphTest.java | 31 +++++++++++------ .../TryWithResourcesFinallyTests.java | 7 ++-- .../interceptors/TrapTightenerTest.java | 6 ++-- .../UnreachableCodeEliminatorTest.java | 11 +++--- .../jimple/frontend/JimpleConverterTest.java | 6 +++- 11 files changed, 70 insertions(+), 60 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/model/Body.java b/sootup.core/src/main/java/sootup/core/model/Body.java index ab8939d268d..d7cbcc0dcaf 100644 --- a/sootup.core/src/main/java/sootup/core/model/Body.java +++ b/sootup.core/src/main/java/sootup/core/model/Body.java @@ -40,7 +40,6 @@ import sootup.core.jimple.common.stmt.*; import sootup.core.signatures.MethodSignature; import sootup.core.util.EscapedWriter; -import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.util.printer.JimplePrinter; /** @@ -112,17 +111,6 @@ public Set getLocals() { return locals; } - /** - * Returns an unmodifiable view of the traps found in this Body. @Deprecated the exceptional flow - * information is already integrated into the StmtGraphs BasicBlocks.getExceptionalFlows() - - * exists to make porting tools from Soot easier - */ - @Nonnull - @Deprecated() - public List getTraps() { - return new BriefStmtPrinter().buildTraps(graph); - } - /** Return unit containing the \@this-assignment * */ @Nullable public Stmt getThisStmt() { diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java index d88d5fbb938..4ce08c8695c 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/BlockGraphIteratorAndTrapAggregator.java @@ -46,9 +46,9 @@ class BlockGraphIteratorAndTrapAggregator extends BlockGraphIterator { @Nonnull private final List collectedTraps = new ArrayList<>(); - Map activeTraps = new HashMap<>(); - BasicBlock lastIteratedBlock; - JNopStmt lastStmt = null; + @Nonnull protected final Map activeTraps = new HashMap<>(); + @Nonnull protected BasicBlock lastIteratedBlock; + @Nullable protected JNopStmt lastStmt = null; /* * @param dummyBlock is just an empty instantiation of type V - as neither BasicBlock nor V instantiable we need a concrete object from the using subclass itclass. diff --git a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java index 69525e58d49..c02555c048c 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/JimplePrinter.java @@ -336,8 +336,7 @@ private void printStatementsInBody(Body body, LabeledStmtPrinter printer) { } private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { - final List traps = printer.buildTraps(stmtGraph); - Iterable linearizedStmtGraph = printer.initializeSootMethod(stmtGraph, traps); + Iterable linearizedStmtGraph = printer.initializeSootMethod(stmtGraph); Stmt previousStmt; @@ -381,7 +380,7 @@ private void printStmts(StmtGraph stmtGraph, LabeledStmtPrinter printer) { // Print out exceptions { - Iterator trapIt = traps.iterator(); + Iterator trapIt = printer.getTraps().iterator(); if (trapIt.hasNext()) { printer.newline(); diff --git a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java index c49879ef6f9..dfaf73543ff 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/LabeledStmtPrinter.java @@ -47,6 +47,8 @@ public abstract class LabeledStmtPrinter extends AbstractStmtPrinter { */ protected Map references; + private List traps; + public LabeledStmtPrinter() {} public Map getLabels() { @@ -107,15 +109,10 @@ public void stmtRef(Stmt stmt, boolean branchTarget) { * * @return the linearized StmtGraph */ - public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph, List traps) { + public Iterable initializeSootMethod(@Nonnull StmtGraph stmtGraph) { this.graph = stmtGraph; - final List linearizedStmtGraph = getStmts(stmtGraph, traps); - return linearizedStmtGraph; - } - - @Nonnull - public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { - final Collection labeledStmts = getLabeledStmts(stmtGraph, traps); + JNopStmt needsNopAtEnd = buildTraps(stmtGraph); + final Collection labeledStmts = getLabeledStmts(stmtGraph, this.traps); final int maxEstimatedSize = labeledStmts.size() + traps.size() * 3; labels = new HashMap<>(maxEstimatedSize, 1); @@ -165,12 +162,9 @@ public List getStmts(@Nonnull StmtGraph stmtGraph, List traps) { } } - // add Nop Stmt to Jimple just for serialization - for (Stmt s : labeledStmts) { - if (trapStmts.contains(s) && s instanceof JNopStmt) { - linearizedStmtGraph.add(s); - labels.put(s, String.format(formatString, ++labelCount)); - } + if (needsNopAtEnd != null) { + linearizedStmtGraph.add(needsNopAtEnd); + labels.put(needsNopAtEnd, String.format(formatString, ++labelCount)); } return linearizedStmtGraph; @@ -214,7 +208,7 @@ public void fieldSignature(FieldSignature fieldSig) { * BasicBlock.getExceptionalSuccessor() */ /** hint: little expensive getter - its more of a build/create - currently no overlaps */ - public List buildTraps(StmtGraph stmtGraph) { + public JNopStmt buildTraps(StmtGraph stmtGraph) { // [ms] try to incorporate it into the serialisation of jimple printing so the other half of // iteration information is not wasted.. BlockGraphIteratorAndTrapAggregator it = new BlockGraphIteratorAndTrapAggregator(stmtGraph); @@ -229,11 +223,13 @@ public List buildTraps(StmtGraph stmtGraph) { i++; } final List traps = it.getTraps(); - if (it.getLastStmt() != null) { + boolean b = it.getLastStmt() != null; + if (b) { stmtsBlockIdx.put(it.getLastStmt(), i); } traps.sort(getTrapComparator(stmtsBlockIdx)); - return traps; + this.traps = traps; + return it.getLastStmt(); } /** Comparator which sorts the trap output in getTraps() */ @@ -284,4 +280,8 @@ public Collection getLabeledStmts(StmtGraph stmtGraph, List traps) { return stmtList; } + + public List getTraps() { + return traps; + } } diff --git a/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java b/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java index 3b211381dd2..7f4301dd250 100644 --- a/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/JimpleTrapValidator.java @@ -31,6 +31,7 @@ import sootup.core.jimple.common.stmt.JIdentityStmt; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.views.View; /** @@ -53,7 +54,10 @@ public List validate(Body body, View view) { List exceptions = new ArrayList<>(); Set caughtStmts = new HashSet(); - for (Trap trap : body.getTraps()) { + BriefStmtPrinter stmtPrinter = new BriefStmtPrinter(); + stmtPrinter.buildTraps(body.getStmtGraph()); + Iterable traps = stmtPrinter.getTraps(); + for (Trap trap : traps) { caughtStmts.add(trap.getHandlerStmt()); if (!(trap.getHandlerStmt() instanceof JIdentityStmt)) { exceptions.add( diff --git a/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java b/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java index e9019e475b0..cec0b87428a 100644 --- a/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java +++ b/sootup.core/src/main/java/sootup/core/validation/TrapsValidator.java @@ -27,6 +27,7 @@ import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.stmt.Stmt; import sootup.core.model.Body; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.core.views.View; public class TrapsValidator implements BodyValidator { @@ -39,9 +40,12 @@ public class TrapsValidator implements BodyValidator { @Override public List validate(Body body, View view) { List exceptions = new ArrayList<>(); + BriefStmtPrinter stmtPrinter = new BriefStmtPrinter(); + stmtPrinter.buildTraps(body.getStmtGraph()); + Iterable traps = stmtPrinter.getTraps(); List stmts = body.getStmts(); - for (Trap t : body.getTraps()) { + for (Trap t : traps) { if (!stmts.contains(t.getBeginStmt())) exceptions.add( new ValidationException( diff --git a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java index e8d938dbeb6..6143763a4d4 100644 --- a/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java +++ b/sootup.core/src/test/java/sootup/core/graph/MutableBlockStmtGraphTest.java @@ -624,7 +624,8 @@ public PackageName getPackageName() { graph0.putEdge(stmt2, 0, returnStmt); { - final List traps = briefStmtPrinter.buildTraps(graph0); + briefStmtPrinter.buildTraps(graph0); + List traps = briefStmtPrinter.getTraps(); assertEquals(2, traps.size()); // as @caughtexception gets currently in their way. assertEquals(stmt2, traps.get(1).getBeginStmt()); assertEquals(returnStmt, traps.get(1).getEndStmt()); @@ -662,7 +663,8 @@ public PackageName getPackageName() { graph2.putEdge(stmt2, JGotoStmt.BRANCH_IDX, returnStmt); { assertEquals(5, graph2.getBlocks().size()); - final List traps = briefStmtPrinter.buildTraps(graph2); + briefStmtPrinter.buildTraps(graph2); + List traps = briefStmtPrinter.getTraps(); assertEquals(2, traps.size()); } @@ -678,7 +680,8 @@ public PackageName getPackageName() { graph3.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = briefStmtPrinter.buildTraps(graph3); + briefStmtPrinter.buildTraps(graph3); + List traps = briefStmtPrinter.getTraps(); assertEquals(5, graph2.getBlocks().size()); assertEquals(2, traps.size()); } @@ -701,7 +704,8 @@ public PackageName getPackageName() { graph4.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph4.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); - assertEquals(3, briefStmtPrinter.buildTraps(graph4).size()); + briefStmtPrinter.buildTraps(graph4); + assertEquals(3, briefStmtPrinter.getTraps().size()); // mixed 2 MutableBlockStmtGraph graph5 = new MutableBlockStmtGraph(); @@ -736,8 +740,8 @@ public PackageName getPackageName() { graph5.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = briefStmtPrinter.buildTraps(graph5); - assertEquals(6, traps.size()); + briefStmtPrinter.buildTraps(graph5); + assertEquals(6, briefStmtPrinter.getTraps().size()); assertEquals(6, graph5.getBlocks().size()); } @@ -772,7 +776,8 @@ public PackageName getPackageName() { graph6.putEdge(stmt2, JGotoStmt.BRANCH_IDX, stmt3); graph6.putEdge(stmt3, JGotoStmt.BRANCH_IDX, returnStmt); { - final List traps = briefStmtPrinter.buildTraps(graph6); + briefStmtPrinter.buildTraps(graph6); + List traps = briefStmtPrinter.getTraps(); assertEquals(5, traps.size()); assertEquals(6, graph6.getBlocks().size()); assertEquals( @@ -1056,7 +1061,8 @@ public void testRemoveSingleTrap() { graph.addExceptionalEdge(stmt1, throwableSig, handlerStmt); // Verify the trap is present - List traps = briefStmtPrinter.buildTraps(graph); + briefStmtPrinter.buildTraps(graph); + List traps = briefStmtPrinter.getTraps(); assertEquals(1, traps.size()); assertEquals(stmt1, traps.get(0).getBeginStmt()); assertEquals(handlerStmt, traps.get(0).getHandlerStmt()); @@ -1065,7 +1071,8 @@ public void testRemoveSingleTrap() { Trap trapToRemove = traps.get(0); graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = briefStmtPrinter.buildTraps(graph); + briefStmtPrinter.buildTraps(graph); + traps = briefStmtPrinter.getTraps(); assertEquals(0, traps.size()); } @@ -1099,7 +1106,8 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.addExceptionalEdge(stmt2, ioExceptionSig, handlerStmt2); // Verify both traps are present - List traps = briefStmtPrinter.buildTraps(graph); + briefStmtPrinter.buildTraps(graph); + List traps = briefStmtPrinter.getTraps(); assertEquals(2, traps.size()); // Remove one trap and verify the remaining @@ -1108,7 +1116,8 @@ public void testRemoveMultipleTrapsWithDifferentExceptionTypes() { graph.removeExceptionalFlowFromAllBlocks( trapToRemove.getExceptionType(), trapToRemove.getHandlerStmt()); - traps = briefStmtPrinter.buildTraps(graph); + briefStmtPrinter.buildTraps(graph); + traps = briefStmtPrinter.getTraps(); assertEquals(1, traps.size()); assertEquals(stmt2, trapToKeep.getBeginStmt()); assertEquals(handlerStmt2, trapToKeep.getHandlerStmt()); diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/TryWithResourcesFinallyTests.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/TryWithResourcesFinallyTests.java index b78111e421d..3153479079c 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/TryWithResourcesFinallyTests.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/TryWithResourcesFinallyTests.java @@ -4,13 +4,12 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; -import java.util.List; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import sootup.core.inputlocation.AnalysisInputLocation; -import sootup.core.jimple.basic.Trap; import sootup.core.model.SourceType; import sootup.core.signatures.MethodSignature; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.java.bytecode.frontend.inputlocation.PathBasedAnalysisInputLocation; import sootup.java.core.views.JavaView; @@ -29,6 +28,8 @@ public void test() { MethodSignature methodSignature = view.getIdentifierFactory() .parseMethodSignature(""); - List traps = view.getMethod(methodSignature).get().getBody().getTraps(); + BriefStmtPrinter stmtPrinter = new BriefStmtPrinter(); + stmtPrinter.buildTraps(view.getMethod(methodSignature).get().getBody().getStmtGraph()); + stmtPrinter.getTraps(); } } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java index 52185a9306c..59e9b2fa051 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/TrapTightenerTest.java @@ -134,7 +134,8 @@ public void testSimpleBody() { List excepted = new ArrayList<>(); excepted.add(trap3); - List actual = briefStmtPrinter.buildTraps(stmtGraph); + briefStmtPrinter.buildTraps(stmtGraph); + List actual = briefStmtPrinter.getTraps(); AssertUtils.assertTrapsEquiv(excepted, actual); } /** @@ -175,7 +176,8 @@ public void testMonitoredBody() { List excepted = new ArrayList<>(); excepted.add(trap1); - List actual = briefStmtPrinter.buildTraps(stmtGraph); + briefStmtPrinter.buildTraps(stmtGraph); + List actual = briefStmtPrinter.getTraps(); AssertUtils.assertTrapsEquiv(excepted, actual); } diff --git a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java index a51c0bd9061..a3109f6fc15 100644 --- a/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java +++ b/sootup.java.bytecode.frontend/src/test/java/sootup/java/bytecode/frontend/interceptors/UnreachableCodeEliminatorTest.java @@ -13,10 +13,7 @@ import sootup.core.jimple.basic.StmtPositionInfo; import sootup.core.jimple.common.constant.IntConstant; import sootup.core.jimple.common.ref.IdentityRef; -import sootup.core.jimple.common.stmt.BranchingStmt; -import sootup.core.jimple.common.stmt.FallsThroughStmt; -import sootup.core.jimple.common.stmt.JGotoStmt; -import sootup.core.jimple.common.stmt.Stmt; +import sootup.core.jimple.common.stmt.*; import sootup.core.model.Body; import sootup.core.signatures.MethodSignature; import sootup.core.types.ClassType; @@ -142,7 +139,8 @@ public void testTrappedBody1() { new UnreachableCodeEliminator().interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, briefStmtPrinter.buildTraps(builder.getStmtGraph()).size()); + briefStmtPrinter.buildTraps(builder.getStmtGraph()); + assertEquals(0, briefStmtPrinter.getTraps().size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); AssertUtils.assertSetsEquiv(expectedStmtsSet, builder.getStmtGraph().getNodes()); @@ -178,7 +176,8 @@ public void testTrappedBody2() { UnreachableCodeEliminator eliminator = new UnreachableCodeEliminator(); eliminator.interceptBody(builder, new JavaView(Collections.emptyList())); - assertEquals(0, briefStmtPrinter.buildTraps(builder.getStmtGraph()).size()); + briefStmtPrinter.buildTraps(builder.getStmtGraph()); + assertEquals(0, briefStmtPrinter.getTraps().size()); Set expectedStmtsSet = ImmutableUtils.immutableSet(startingStmt, stmt1, ret1); assertEquals(expectedStmtsSet, builder.getStmtGraph().getNodes()); diff --git a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java index 2c10cad184e..88fc0ba6bf9 100644 --- a/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java +++ b/sootup.jimple.frontend/src/test/java/sootup/jimple/frontend/JimpleConverterTest.java @@ -22,6 +22,7 @@ import sootup.core.types.PrimitiveType; import sootup.core.types.VoidType; import sootup.core.util.StringTools; +import sootup.core.util.printer.BriefStmtPrinter; import sootup.jimple.JimpleLexer; import sootup.jimple.JimpleParser; @@ -830,7 +831,10 @@ public void testRedundantTrapHandler() throws IOException { CharStreams.fromFileName("src/test/java/resources/jimple/RedundantTrapHandler.jimple")); Set methods = clazz.getMethods(); SootMethod method = methods.iterator().next(); - List traps = method.getBody().getTraps(); + + BriefStmtPrinter stmtPrinter = new BriefStmtPrinter(); + stmtPrinter.buildTraps(method.getBody().getStmtGraph()); + List traps = stmtPrinter.getTraps(); assertEquals(0, traps.size()); } } From 5722d825f064acbbf5e845eb666a0220f5e79cc3 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Tue, 7 Jan 2025 16:19:36 +0100 Subject: [PATCH 11/13] updated tests --- .../src/main/java/sootup/core/graph/StmtGraph.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index b3f4be517bd..3af75849e00 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -29,6 +29,8 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; + +import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.ref.IdentityRef; import sootup.core.jimple.common.ref.JCaughtExceptionRef; import sootup.core.jimple.common.stmt.*; @@ -356,7 +358,11 @@ public boolean equals(Object o) { return false; } - if (!briefStmtPrinter.buildTraps(this).equals(briefStmtPrinter.buildTraps(otherGraph))) { + briefStmtPrinter.buildTraps(this); + List currTraps = briefStmtPrinter.getTraps(); + briefStmtPrinter.buildTraps(otherGraph); + List otherGraphTraps = briefStmtPrinter.getTraps(); + if (!currTraps.equals(otherGraphTraps)) { return false; } From 30d00ccbf72fc59cdcbb40e0bb3142deb13f1820 Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Tue, 7 Jan 2025 16:31:09 +0100 Subject: [PATCH 12/13] updated tests --- sootup.core/src/main/java/sootup/core/graph/StmtGraph.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index 3af75849e00..e6be095b6a7 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -29,7 +29,6 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; - import sootup.core.jimple.basic.Trap; import sootup.core.jimple.common.ref.IdentityRef; import sootup.core.jimple.common.ref.JCaughtExceptionRef; From b4add46b376c8a108f1e74d57c5bf92f325391ea Mon Sep 17 00:00:00 2001 From: sahilagichani Date: Wed, 8 Jan 2025 15:55:35 +0100 Subject: [PATCH 13/13] remove fields --- .../src/main/java/sootup/core/graph/StmtGraph.java | 11 +++-------- .../sootup/core/util/printer/BriefStmtPrinter.java | 5 +++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java index e6be095b6a7..45833cf8ec9 100644 --- a/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java +++ b/sootup.core/src/main/java/sootup/core/graph/StmtGraph.java @@ -66,9 +66,6 @@ */ public abstract class StmtGraph> implements Iterable { - public final JimplePrinter jimplePrinter = new JimplePrinter(); - public final BriefStmtPrinter briefStmtPrinter = new BriefStmtPrinter(); - public abstract Stmt getStartingStmt(); public abstract BasicBlock getStartingStmtBlock(); @@ -357,10 +354,8 @@ public boolean equals(Object o) { return false; } - briefStmtPrinter.buildTraps(this); - List currTraps = briefStmtPrinter.getTraps(); - briefStmtPrinter.buildTraps(otherGraph); - List otherGraphTraps = briefStmtPrinter.getTraps(); + List currTraps = new BriefStmtPrinter(this).getTraps(); + List otherGraphTraps = new BriefStmtPrinter(otherGraph).getTraps(); if (!currTraps.equals(otherGraphTraps)) { return false; } @@ -453,7 +448,7 @@ public Stmt next() { public String toString() { StringWriter writer = new StringWriter(); try (PrintWriter writerOut = new PrintWriter(new EscapedWriter(writer))) { - jimplePrinter.printTo(this, writerOut); + new JimplePrinter().printTo(this, writerOut); } return writer.toString(); } diff --git a/sootup.core/src/main/java/sootup/core/util/printer/BriefStmtPrinter.java b/sootup.core/src/main/java/sootup/core/util/printer/BriefStmtPrinter.java index 12f7f250366..ce1f0d19367 100644 --- a/sootup.core/src/main/java/sootup/core/util/printer/BriefStmtPrinter.java +++ b/sootup.core/src/main/java/sootup/core/util/printer/BriefStmtPrinter.java @@ -22,6 +22,7 @@ * #L% */ +import sootup.core.graph.StmtGraph; import sootup.core.jimple.Jimple; import sootup.core.jimple.common.ref.IdentityRef; import sootup.core.jimple.common.ref.JCaughtExceptionRef; @@ -37,6 +38,10 @@ public BriefStmtPrinter() { super(); } + public BriefStmtPrinter(StmtGraph stmtGraph) { + buildTraps(stmtGraph); + } + @Override public void method(SootMethod m) { handleIndent();