| 1 19 20 package soot.jimple.toolkits.pointer; 21 22 import soot.*; 23 import soot.jimple.*; 24 import java.util.*; 25 26 35 public class PASideEffectTester implements SideEffectTester 36 { 37 PointsToAnalysis pa = Scene.v().getPointsToAnalysis(); 38 SideEffectAnalysis sea = Scene.v().getSideEffectAnalysis(); 39 HashMap unitToRead; 40 HashMap unitToWrite; 41 HashMap localToReachingObjects; 42 SootMethod currentMethod; 43 44 public PASideEffectTester() { 45 if( G.v().Union_factory == null ) { 46 G.v().Union_factory = new UnionFactory() { 47 public Union newUnion() { return FullObjectSet.v(); } 48 }; 49 } 50 } 51 52 53 public void newMethod( SootMethod m ) { 54 unitToRead = new HashMap(); 55 unitToWrite = new HashMap(); 56 localToReachingObjects = new HashMap(); 57 currentMethod = m; 58 sea.findNTRWSets( currentMethod ); 59 } 60 61 protected RWSet readSet( Unit u ) { 62 RWSet ret = (RWSet) unitToRead.get( u ); 63 if( ret == null ) { 64 unitToRead.put( u, ret = sea.readSet( currentMethod, (Stmt) u ) ); 65 } 66 return ret; 67 } 68 69 protected RWSet writeSet( Unit u ) { 70 RWSet ret = (RWSet) unitToWrite.get( u ); 71 if( ret == null ) { 72 unitToWrite.put( u, ret = sea.writeSet( currentMethod, (Stmt) u ) ); 73 } 74 return ret; 75 } 76 77 protected PointsToSet reachingObjects( Local l ) { 78 PointsToSet ret = (PointsToSet) localToReachingObjects.get( l ); 79 if( ret == null ) { 80 localToReachingObjects.put( l, 81 ret = pa.reachingObjects( l ) ); 82 } 83 return ret; 84 } 85 86 88 public boolean unitCanReadFrom(Unit u, Value v) 89 { 90 return valueTouchesRWSet( readSet( u ), v, u.getUseBoxes() ); 91 } 92 93 95 public boolean unitCanWriteTo(Unit u, Value v) 96 { 97 return valueTouchesRWSet( writeSet( u ), v, u.getDefBoxes() ); 98 } 99 100 protected boolean valueTouchesRWSet(RWSet s, Value v, List boxes) 101 { 102 for( Iterator useIt = v.getUseBoxes().iterator(); useIt.hasNext(); ) { 103 final ValueBox use = (ValueBox) useIt.next(); 104 if( valueTouchesRWSet( s, use.getValue(), boxes ) ) return true; 105 } 106 if (v instanceof Constant) 108 return false; 109 110 if (v instanceof Expr) 111 throw new RuntimeException ("can't deal with expr"); 112 113 for( Iterator boxIt = boxes.iterator(); boxIt.hasNext(); ) { 114 115 final ValueBox box = (ValueBox) boxIt.next(); 116 Value boxed = box.getValue(); 117 if( boxed.equivTo( v ) ) return true; 118 } 119 120 if (v instanceof Local) { 121 return false; 122 } 123 124 if( v instanceof InstanceFieldRef ) { 125 InstanceFieldRef ifr = (InstanceFieldRef) v; 126 if( s == null ) return false; 127 PointsToSet o1 = s.getBaseForField( ifr.getField() ); 128 if( o1 == null ) return false; 129 PointsToSet o2 = reachingObjects( (Local) ifr.getBase() ); 130 if( o2 == null ) return false; 131 return o1.hasNonEmptyIntersection( o2 ); 132 } 133 134 if( v instanceof ArrayRef ) { 135 ArrayRef ar = (ArrayRef) v; 136 if( s == null ) return false; 137 PointsToSet o1 = s.getBaseForField( PointsToAnalysis.ARRAY_ELEMENTS_NODE ); 138 if( o1 == null ) return false; 139 PointsToSet o2 = reachingObjects( (Local) ar.getBase() ); 140 if( o2 == null ) return false; 141 return o1.hasNonEmptyIntersection( o2 ); 142 } 143 144 if( v instanceof StaticFieldRef ) { 145 StaticFieldRef sfr = (StaticFieldRef) v; 146 if( s == null ) return false; 147 return s.getGlobals().contains( sfr.getField() ); 148 } 149 150 throw new RuntimeException ( "Forgot to handle value "+v ); 151 } 152 } 153 | Popular Tags |