5858import soot .jimple .infoflow .entryPointCreators .BaseEntryPointCreator ;
5959import soot .jimple .infoflow .entryPointCreators .IEntryPointCreator ;
6060import soot .jimple .infoflow .entryPointCreators .SimulatedCodeElementTag ;
61+ import soot .jimple .infoflow .solver .cfg .IInfoflowCFG ;
6162import soot .jimple .infoflow .sourcesSinks .manager .ISourceSinkManager ;
6263import soot .jimple .infoflow .taintWrappers .ITaintPropagationWrapper ;
6364import soot .jimple .infoflow .util .SystemClassHandler ;
@@ -127,7 +128,7 @@ public InterproceduralConstantValuePropagator(InfoflowManager manager) {
127128 public InterproceduralConstantValuePropagator (InfoflowManager manager , Collection <SootMethod > excludedMethods ,
128129 ISourceSinkManager sourceSinkManager , ITaintPropagationWrapper taintWrapper ) {
129130 this .manager = manager ;
130- this .excludedMethods = new HashSet <SootMethod >(excludedMethods );
131+ this .excludedMethods = new HashSet <>(excludedMethods );
131132 this .sourceSinkManager = sourceSinkManager ;
132133 this .taintWrapper = taintWrapper ;
133134 }
@@ -256,7 +257,7 @@ protected void internalTransform(String phaseName, Map<String, String> options)
256257 continue ;
257258
258259 boolean allCalleesRemoved = true ;
259- Set <SootClass > exceptions = new HashSet <SootClass >();
260+ Set <SootClass > exceptions = new HashSet <>();
260261 for (Iterator <Edge > edgeIt = Scene .v ().getCallGraph ().edgesOutOf (s ); edgeIt .hasNext ();) {
261262 Edge edge = edgeIt .next ();
262263 SootMethod callee = edge .tgt ();
@@ -392,10 +393,11 @@ private boolean typeSupportsConstants(Type returnType) {
392393 * @param sm The method whose value to propagate
393394 */
394395 private void propagateReturnValueIntoCallers (SootMethod sm ) {
396+ final IInfoflowCFG icfg = manager .getICFG ();
395397 // We need to make sure that all exit nodes agree on the same
396398 // constant value
397399 Constant value = null ;
398- for (Unit retSite : manager . getICFG () .getEndPointsOf (sm )) {
400+ for (Unit retSite : icfg .getEndPointsOf (sm )) {
399401 // Skip exceptional exits
400402 if (!(retSite instanceof ReturnStmt ))
401403 continue ;
@@ -411,7 +413,7 @@ private void propagateReturnValueIntoCallers(SootMethod sm) {
411413
412414 // Propagate the return value into the callers
413415 if (value != null )
414- for (Unit callSite : manager . getICFG () .getCallersOf (sm ))
416+ for (Unit callSite : icfg .getCallersOf (sm ))
415417 if (callSite instanceof AssignStmt ) {
416418 AssignStmt assign = (AssignStmt ) callSite ;
417419
@@ -427,13 +429,13 @@ private void propagateReturnValueIntoCallers(SootMethod sm) {
427429
428430 // Make sure that we don't access anything we have already
429431 // removed
430- SootMethod caller = manager . getICFG () .getMethodOf (assign );
432+ SootMethod caller = icfg .getMethodOf (assign );
431433 if (caller == null || !caller .getActiveBody ().getUnits ().contains (assign ))
432434 continue ;
433435
434436 // If the call site has multiple callees, we cannot
435437 // propagate a single constant
436- Collection <SootMethod > callees = manager . getICFG () .getCalleesOfCallAt (callSite );
438+ Collection <SootMethod > callees = icfg .getCalleesOfCallAt (callSite );
437439 if (callees != null && callees .size () > 1 )
438440 continue ;
439441
@@ -478,7 +480,7 @@ private void propagateReturnValueIntoCallers(SootMethod sm) {
478480 }
479481
480482 private void fixExceptions (SootMethod caller , Unit callSite ) {
481- fixExceptions (caller , callSite , new HashSet <SootClass >());
483+ fixExceptions (caller , callSite , new HashSet <>());
482484 }
483485
484486 private void fixExceptions (SootMethod caller , Unit callSite , Set <SootClass > doneSet ) {
@@ -493,6 +495,7 @@ private void fixExceptions(SootMethod caller, Unit callSite, Set<SootClass> done
493495 if (exceptionClass == null ) {
494496 exceptionClass = Scene .v ().makeSootClass ("FLOWDROID_EXCEPTIONS" , Modifier .PUBLIC );
495497 exceptionClass .setSuperclass (Scene .v ().getSootClass ("java.lang.Object" ));
498+ exceptionClass .addTag (SimulatedCodeElementTag .TAG );
496499 Scene .v ().addClass (exceptionClass );
497500 }
498501
@@ -541,9 +544,10 @@ protected SootMethod createDummyMainInternal() {
541544 protected void createEmptyMainMethod () {
542545 // Make sure that we don't end up with duplicate method names
543546 int methodIdx = exceptionThrowers .size ();
547+ String baseName = "throw_" + t .getException ().getName ().replaceAll ("\\ W+" , "_" ) + "_" ;
544548 String methodName ;
545549 do {
546- methodName = "throw" + methodIdx ++;
550+ methodName = baseName + methodIdx ++;
547551 } while (exceptionClass .declaresMethodByName (methodName ));
548552
549553 // Create the new method
@@ -582,6 +586,7 @@ public Collection<SootField> getAdditionalFields() {
582586 Stmt throwCall = Jimple .v ().newInvokeStmt (Jimple .v ().newStaticInvokeExpr (thrower .makeRef ()));
583587 throwCall .addTag (SimulatedCodeElementTag .TAG );
584588 caller .getActiveBody ().getUnits ().insertBefore (throwCall , callSite );
589+
585590 }
586591 }
587592
@@ -594,7 +599,7 @@ public Collection<SootField> getAdditionalFields() {
594599 * side-effects or calls a sink method, otherwise false.
595600 */
596601 private boolean hasSideEffectsOrCallsSink (SootMethod method ) {
597- return hasSideEffectsOrCallsSink (method , new HashSet <SootMethod >());
602+ return hasSideEffectsOrCallsSink (method , new HashSet <>());
598603 }
599604
600605 /**
@@ -778,7 +783,7 @@ private boolean methodIsAndroidStub(SootMethod method) {
778783 // Check for super class constructor invocation
779784 if (!(method .getDeclaringClass ().hasSuperclass ()
780785 && callee .getDeclaringClass () == method .getDeclaringClass ().getSuperclass ()
781- && callee .getName (). equals ( "<init>" )))
786+ && callee .isConstructor ( )))
782787 return false ;
783788 } else if (!(u instanceof ThrowStmt ))
784789 return false ;
@@ -794,7 +799,11 @@ private boolean methodIsAndroidStub(SootMethod method) {
794799 * @param sm The method for which to look for call sites.
795800 */
796801 private void propagateConstantsIntoCallee (SootMethod sm ) {
797- Collection <Unit > callSites = manager .getICFG ().getCallersOf (sm );
802+
803+ // icfg field is final in InfoflowManager, hence it can't change
804+ // and we can cache it here so we don't have to retrieve it again and again.
805+ final IInfoflowCFG icfg = manager .getICFG ();
806+ Collection <Unit > callSites = icfg .getCallersOf (sm );
798807 if (callSites .isEmpty ())
799808 return ;
800809
@@ -807,9 +816,14 @@ private void propagateConstantsIntoCallee(SootMethod sm) {
807816 boolean hasCallSites = false ;
808817 for (Unit callSite : callSites ) {
809818 // If this call site is in an excluded method, we ignore it
810- if (excludedMethods != null && manager .getICFG ().isReachable (callSite )
811- && excludedMethods .contains (manager .getICFG ().getMethodOf (callSite )))
812- continue ;
819+ if (excludedMethods != null && icfg .isReachable (callSite )) {
820+ SootMethod caller = icfg .getMethodOf (callSite );
821+ // synthetic methods e.g. created by FlowDroid are excluded by default
822+ if (excludedMethods .contains (caller ) || caller .hasTag (SimulatedCodeElementTag .TAG_NAME )) {
823+ logger .trace ("Ignoring calls from {}" , caller );
824+ continue ;
825+ }
826+ }
813827
814828 // We do not support special edges that do not provide a 1:1 argument mapping
815829 InvokeExpr iiExpr = ((Stmt ) callSite ).getInvokeExpr ();
@@ -821,7 +835,7 @@ private void propagateConstantsIntoCallee(SootMethod sm) {
821835 // If we have a reflective call site, we never have constant
822836 // arguments, because
823837 // they are always passed in using an array
824- if (manager . getICFG () .isReflectiveCallSite (callSite )) {
838+ if (icfg .isReflectiveCallSite (callSite )) {
825839 for (int i = 0 ; i < isConstant .length ; i ++)
826840 isConstant [i ] = false ;
827841 } else {
@@ -837,8 +851,9 @@ private void propagateConstantsIntoCallee(SootMethod sm) {
837851 isConstant [i ] = false ;
838852 else
839853 values [i ] = (Constant ) argVal ;
840- } else
854+ } else {
841855 isConstant [i ] = false ;
856+ }
842857 }
843858 }
844859 }
@@ -856,7 +871,7 @@ private void propagateConstantsIntoCallee(SootMethod sm) {
856871 sm .getActiveBody ().getUnits ().insertBefore (assignConst , point );
857872
858873 if (inserted == null )
859- inserted = new ArrayList <Unit >();
874+ inserted = new ArrayList <>();
860875 inserted .add (assignConst );
861876 }
862877 }
@@ -869,7 +884,7 @@ private void propagateConstantsIntoCallee(SootMethod sm) {
869884
870885 // This might lead to more opportunities of constant propagation
871886 for (Unit u : sm .getActiveBody ().getUnits ())
872- for (SootMethod callee : manager . getICFG () .getCalleesOfCallAt (u ))
887+ for (SootMethod callee : icfg .getCalleesOfCallAt (u ))
873888 checkAndAddMethod (callee );
874889 }
875890 }
0 commit comments