1 19 20 package soot.tools; 21 import soot.*; 22 import java.io.*; 23 import java.util.*; 24 import soot.jimple.*; 25 import soot.jimple.toolkits.callgraph.*; 26 27 public class BadFields extends SceneTransformer { 28 public static void main(String [] args) 29 { 30 PackManager.v().getPack("cg").add( 31 new Transform("cg.badfields", new BadFields())); 32 soot.Main.main(args); 33 } 34 35 private SootClass lastClass; 36 private SootClass currentClass; 37 38 protected void internalTransform(String phaseName, Map options) 39 { 40 lastClass = null; 41 42 for( Iterator clIt = Scene.v().getApplicationClasses().iterator(); clIt.hasNext(); ) { 43 44 final SootClass cl = (SootClass) clIt.next(); 45 currentClass = cl; 46 handleClass( cl ); 47 for( Iterator it = cl.methodIterator(); it.hasNext(); ) { 48 handleMethod( (SootMethod) it.next() ); 49 } 50 } 51 Scene.v().setCallGraph( new CallGraph() ); 52 } 53 54 private void handleClass( SootClass cl ) { 55 for( Iterator fIt = cl.getFields().iterator(); fIt.hasNext(); ) { 56 final SootField f = (SootField) fIt.next(); 57 if( !f.isStatic() ) continue; 58 String typeName = f.getType().toString(); 59 if( typeName.equals( "java.lang.Class" ) ) continue; 60 if( f.isFinal() ) { 61 if( f.getType() instanceof PrimType ) continue; 62 if( typeName.equals( "java.io.PrintStream" ) ) continue; 63 if( typeName.equals( "java.lang.String" ) ) continue; 64 if( typeName.equals( "java.lang.Object" ) ) continue; 65 if( typeName.equals( "java.lang.Integer" ) ) continue; 66 if( typeName.equals( "java.lang.Boolean" ) ) continue; 67 } 68 warn( "Bad field "+f ); 69 } 70 } 71 72 private void warn( String warning ) { 73 if( lastClass != currentClass ) 74 G.v().out.println( "In class "+currentClass ); 75 lastClass = currentClass; 76 G.v().out.println( " "+warning ); 77 } 78 79 private void handleMethod( SootMethod m ) { 80 if( !m.isConcrete() ) return; 81 for( Iterator bIt = m.retrieveActiveBody().getUseAndDefBoxes().iterator(); bIt.hasNext(); ) { 82 final ValueBox b = (ValueBox) bIt.next(); 83 Value v = b.getValue(); 84 if( !(v instanceof StaticFieldRef) ) continue; 85 StaticFieldRef sfr = (StaticFieldRef) v; 86 SootField f = sfr.getField(); 87 if( !f.getDeclaringClass().getName().equals( "java.lang.System" ) ) 88 continue; 89 if( f.getName().equals( "err" ) ) { 90 G.v().out.println( "Use of System.err in "+m ); 91 } 92 if( f.getName().equals( "out" ) ) { 93 G.v().out.println( "Use of System.out in "+m ); 94 } 95 } 96 for( Iterator sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) { 97 final Stmt s = (Stmt) sIt.next(); 98 if( !s.containsInvokeExpr() ) continue; 99 InvokeExpr ie = s.getInvokeExpr(); 100 SootMethod target = ie.getMethod(); 101 if( target.getDeclaringClass().getName().equals( "java.lang.System" ) 102 && target.getName().equals( "exit" ) ) { 103 warn( ""+m+" calls System.exit" ); 104 } 105 } 106 if( m.getName().equals( "<clinit>" ) ) { 107 for( Iterator sIt = m.getActiveBody().getUnits().iterator(); sIt.hasNext(); ) { 108 final Stmt s = (Stmt) sIt.next(); 109 for( Iterator bIt = s.getUseBoxes().iterator(); bIt.hasNext(); ) { 110 final ValueBox b = (ValueBox) bIt.next(); 111 Value v = b.getValue(); 112 if( v instanceof FieldRef ) { 113 warn( m.getName()+" reads field "+v ); 114 } 115 } 116 if( !s.containsInvokeExpr() ) continue; 117 InvokeExpr ie = (InvokeExpr) s.getInvokeExpr(); 118 SootMethod target = ie.getMethod(); 119 calls( target ); 120 } 121 } 122 } 123 private void calls( SootMethod target ) { 124 if( target.getName().equals("<init>") ) { 125 if( target.getDeclaringClass().getName().equals( "java.io.PrintStream" ) ) return; 126 if( target.getDeclaringClass().getName().equals( "java.lang.Boolean" ) ) return; 127 if( target.getDeclaringClass().getName().equals( "java.lang.Integer" ) ) return; 128 if( target.getDeclaringClass().getName().equals( "java.lang.String" ) ) return; 129 if( target.getDeclaringClass().getName().equals( "java.lang.Object" ) ) return; 130 } 131 if( target.getName().equals("getProperty") ) { 132 if( target.getDeclaringClass().getName().equals( "java.lang.System" ) ) return; 133 } 134 if( target.getName().equals("charAt") ) { 135 if( target.getDeclaringClass().getName().equals( "java.lang.String" ) ) return; 136 } 137 warn( "<clinit> invokes "+target ); 138 } 139 } 140 141 142 | Popular Tags |