|
| 1 | +package soot.jimple.toolkits.scalar; |
| 2 | + |
| 3 | +import soot.Body; |
| 4 | +import soot.FastHierarchy; |
| 5 | +import soot.RefType; |
| 6 | +import soot.Scene; |
| 7 | +import soot.Trap; |
| 8 | +import soot.Unit; |
| 9 | +import soot.UnitPatchingChain; |
| 10 | +import soot.Value; |
| 11 | +import soot.ValueBox; |
| 12 | +import soot.jimple.ClassConstant; |
| 13 | +import soot.jimple.DefinitionStmt; |
| 14 | +import soot.toolkits.graph.ExceptionalUnitGraph; |
| 15 | +import soot.toolkits.graph.ExceptionalUnitGraph.ExceptionDest; |
| 16 | + |
| 17 | +/*- |
| 18 | + * #%L |
| 19 | + * Soot - a J*va Optimization Framework |
| 20 | + * %% |
| 21 | + * Copyright (C) 2000 Patrick Lam |
| 22 | + * %% |
| 23 | + * This program is free software: you can redistribute it and/or modify |
| 24 | + * it under the terms of the GNU Lesser General Public License as |
| 25 | + * published by the Free Software Foundation, either version 2.1 of the |
| 26 | + * License, or (at your option) any later version. |
| 27 | + * |
| 28 | + * This program is distributed in the hope that it will be useful, |
| 29 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | + * GNU General Lesser Public License for more details. |
| 32 | + * |
| 33 | + * You should have received a copy of the GNU General Lesser Public |
| 34 | + * License along with this program. If not, see |
| 35 | + * <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 36 | + * #L% |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * Contains utility methods for constant propagation |
| 41 | + */ |
| 42 | +public class ConstantPropagatorUtils { |
| 43 | + |
| 44 | + /** |
| 45 | + * Checks whether propagating <i>propagatedValue</i> from <i>defFrom</i> to <i>defTo</i> is safe. |
| 46 | + * @param graph an exceptional unit graph |
| 47 | + * @param propagatedValue the propagated value |
| 48 | + * @param defFrom definition source |
| 49 | + * @param defTo target |
| 50 | + * @param targetBox the target box |
| 51 | + * @return true if and only if propagation is safe w.r.t. trap handling |
| 52 | + */ |
| 53 | + public static boolean mayPropagate(ExceptionalUnitGraph graph, Value propagatedValue, DefinitionStmt defFrom, Unit defTo, |
| 54 | + ValueBox targetBox) { |
| 55 | + if (!targetBox.canContainValue(propagatedValue)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + if (propagatedValue instanceof ClassConstant) { |
| 59 | + //Class Constants can trigger a NoClassDefFoundError. |
| 60 | + //Therefore, we must not propagate them, since we might change the semantics of the original |
| 61 | + //program w.r.t. traps. |
| 62 | + RefType rt = RefType.v("java.lang.NoClassDefFoundError"); |
| 63 | + Trap trap = null; |
| 64 | + for (ExceptionDest d : graph.getExceptionDests(defFrom)) { |
| 65 | + if (d.getThrowables().catchableAs(rt)) { |
| 66 | + trap = d.getTrap(); |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + Body body = graph.getBody(); |
| 71 | + FastHierarchy fh = Scene.v().getOrMakeFastHierarchy(); |
| 72 | + UnitPatchingChain chain = body.getUnits(); |
| 73 | + |
| 74 | + //this is not super fast, but the exceptional unit graph is not helpful here, since |
| 75 | + //we would need to rebuild it after propagation to reflect the changes, which would be more expensive. |
| 76 | + for (Trap i : body.getTraps()) { |
| 77 | + if (trap != null && i.getHandlerUnit() != trap.getHandlerUnit()) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (fh.canStoreType(rt, i.getException().getType())) { |
| 81 | + Unit u = i.getBeginUnit(); |
| 82 | + while (u != i.getEndUnit()) { |
| 83 | + if (u == defTo) { |
| 84 | + //when the original code had the same handling unit, this is fine. |
| 85 | + //if there is none, this is not fine. |
| 86 | + return trap != null; |
| 87 | + } |
| 88 | + u = chain.getSuccOf(u); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + //we have not found a trap, so the original code must not have one, either |
| 93 | + return trap == null; |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + private static Unit getTrapHandler(ExceptionalUnitGraph graph, Unit def, RefType rtException) { |
| 100 | + for (ExceptionDest d : graph.getExceptionDests(def)) { |
| 101 | + if (d.getThrowables().catchableAs(rtException)) { |
| 102 | + return d.getHandlerNode(); |
| 103 | + } |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments