-
-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix UnitBox memory leak in UCE #1655
Open
tim-hoffman
wants to merge
3
commits into
soot-oss:develop
Choose a base branch
from
tim-hoffman:PR_fix_UCE
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/systemTest/java/soot/jimple/toolkit/scalar/UnreachableCodeEliminatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package soot.jimple.toolkit.scalar; | ||
|
||
/*- | ||
* #%L | ||
* Soot - a J*va Optimization Framework | ||
* %% | ||
* Copyright (C) 2020 Timothy Hoffman | ||
* %% | ||
* 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 | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
|
||
import soot.Body; | ||
import soot.SootMethod; | ||
import soot.Trap; | ||
import soot.Unit; | ||
import soot.UnitBox; | ||
import soot.jimple.toolkits.scalar.UnreachableCodeEliminator; | ||
import soot.options.Options; | ||
import soot.tagkit.CodeAttribute; | ||
import soot.tagkit.Tag; | ||
import soot.testing.framework.AbstractTestingFramework; | ||
|
||
@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*" }) | ||
public class UnreachableCodeEliminatorTest extends AbstractTestingFramework { | ||
|
||
private static final String TEST_TARGET_CLASS = "soot.jimple.toolkits.scalar.UnreachableCodeEliminatorTestInput"; | ||
|
||
@Override | ||
protected void setupSoot() { | ||
// Skip JimpleBody UnreachableCodeEliminator so we can | ||
// observe the effect of running it manually below. | ||
Options.v().setPhaseOption("jb.uce", "enabled:false"); | ||
|
||
// Disable validation because it will give an error with UCE disabled. | ||
Options.v().set_validate(false); | ||
} | ||
|
||
@Test | ||
public void unitBoxConsistency() { | ||
SootMethod target = | ||
prepareTarget(methodSigFromComponents(TEST_TARGET_CLASS, "void", "unreachableTrap"), TEST_TARGET_CLASS); | ||
|
||
Body body = target.retrieveActiveBody(); | ||
|
||
// There is 1 Trap before the optimization. | ||
Assert.assertEquals(1, body.getTraps().size()); | ||
|
||
// Assert that the set of boxes obtained from all units equals the set | ||
// obtained by checking Unit#getBoxesPointingToThis() on all units. | ||
Assert.assertEquals(new HashSet<>(getAllUnitBoxes(body)), new HashSet<>(getAllBoxesPointingToUnits(body))); | ||
|
||
UnreachableCodeEliminator.v().transform(body); | ||
|
||
// There are no Traps after the optimization. | ||
Assert.assertEquals(0, body.getTraps().size()); | ||
|
||
// Assert that the set of boxes obtained from all units equals the set | ||
// obtained by checking Unit#getBoxesPointingToThis() on all units. | ||
Assert.assertEquals(new HashSet<>(getAllUnitBoxes(body)), new HashSet<>(getAllBoxesPointingToUnits(body))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please assert that the transformer changed sth i.e. removed the trap There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
} | ||
|
||
private static List<UnitBox> getAllUnitBoxes(Body body) { | ||
ArrayList<UnitBox> unitBoxList = new ArrayList<>(); | ||
for (Unit u : body.getUnits()) { | ||
unitBoxList.addAll(u.getUnitBoxes()); | ||
} | ||
for (Trap t : body.getTraps()) { | ||
unitBoxList.addAll(t.getUnitBoxes()); | ||
} | ||
for (Tag t : body.getTags()) { | ||
if (t instanceof CodeAttribute) { | ||
unitBoxList.addAll(((CodeAttribute) t).getUnitBoxes()); | ||
} | ||
} | ||
return unitBoxList; | ||
} | ||
|
||
private static List<UnitBox> getAllBoxesPointingToUnits(Body body) { | ||
ArrayList<UnitBox> unitBoxList = new ArrayList<>(); | ||
for (Unit u : body.getUnits()) { | ||
unitBoxList.addAll(u.getBoxesPointingToThis()); | ||
} | ||
return unitBoxList; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/systemTest/targets/soot/jimple/toolkit/scalar/UnreachableCodeEliminatorTestInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package soot.jimple.toolkits.scalar; | ||
|
||
/*- | ||
* #%L | ||
* Soot - a J*va Optimization Framework | ||
* %% | ||
* Copyright (C) 2020 Timothy Hoffman | ||
* %% | ||
* 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 | ||
* <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
* #L% | ||
*/ | ||
import java.io.PrintStream; | ||
|
||
/** | ||
* @author Timothy Hoffman | ||
*/ | ||
public class UnreachableCodeEliminatorTestInput { | ||
|
||
public void unreachableTrap() { | ||
final PrintStream out = System.out; | ||
out.println("Before"); | ||
try { | ||
int x = 2134;// dead code here allows the entire trap to be removed | ||
} catch (RuntimeException ex) { | ||
out.println("Handler"); | ||
} | ||
out.println("After"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please restructure the UnitBox assignments inside the if block so the assignments are just executed when needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think any improvement can be made. The first two (beginBox and endBox) will always be used since they are in the first condition. The final variable (handlerBox) will always be used as well. If the first condition is true, it will be used inside the if statement body and if the first condition is false, it will be used in the second condition (and possibly again inside the body).