changes.
[IRC.git] / Robust / src / Analysis / SSJava / FlowDownCheck.java
index 8ff79e398032c8652695b791848a87c93fc22e2b..d01e094eb24759c0f8991a0ebaaf34de383dd7f2 100644 (file)
@@ -25,6 +25,7 @@ import IR.SymbolTable;
 import IR.TypeDescriptor;
 import IR.TypeExtension;
 import IR.VarDescriptor;
+import IR.Flat.FlatNode;
 import IR.Tree.ArrayAccessNode;
 import IR.Tree.AssignmentNode;
 import IR.Tree.BlockExpressionNode;
@@ -225,7 +226,8 @@ public class FlowDownCheck {
           if (state.SSJAVADEBUG) {
             System.out.println("SSJAVA: Checking Flow-down Rules: " + md);
           }
-          checkMethodBody(cd, md, null);
+          CompositeLocation calleePCLOC = ssjava.getPCLocation(md);
+          checkMethodBody(cd, md, calleePCLOC);
         }
       }
     }
@@ -305,23 +307,32 @@ public class FlowDownCheck {
     }
     Vector<AnnotationDescriptor> methodAnnotations = md.getModifiers().getAnnotations();
 
-    // second, check return location annotation
-    if (!md.getReturnType().isVoid()) {
-      CompositeLocation returnLocComp = null;
-
-      boolean hasReturnLocDeclaration = false;
-      if (methodAnnotations != null) {
-        for (int i = 0; i < methodAnnotations.size(); i++) {
-          AnnotationDescriptor an = methodAnnotations.elementAt(i);
-          if (an.getMarker().equals(ssjava.RETURNLOC)) {
-            // this case, developer explicitly defines method lattice
-            String returnLocDeclaration = an.getValue();
-            returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
-            hasReturnLocDeclaration = true;
-          }
+    CompositeLocation returnLocComp = null;
+
+    boolean hasReturnLocDeclaration = false;
+    if (methodAnnotations != null) {
+      for (int i = 0; i < methodAnnotations.size(); i++) {
+        AnnotationDescriptor an = methodAnnotations.elementAt(i);
+        if (an.getMarker().equals(ssjava.RETURNLOC)) {
+          // this case, developer explicitly defines method lattice
+          String returnLocDeclaration = an.getValue();
+          returnLocComp = parseLocationDeclaration(md, null, returnLocDeclaration);
+          hasReturnLocDeclaration = true;
+        } else if (an.getMarker().equals(ssjava.THISLOC)) {
+          String thisLoc = an.getValue();
+          ssjava.getMethodLattice(md).setThisLoc(thisLoc);
+        } else if (an.getMarker().equals(ssjava.GLOBALLOC)) {
+          String globalLoc = an.getValue();
+          ssjava.getMethodLattice(md).setGlobalLoc(globalLoc);
+        } else if (an.getMarker().equals(ssjava.PCLOC)) {
+          String pcLocDeclaration = an.getValue();
+          ssjava.setPCLocation(md, parseLocationDeclaration(md, null, pcLocDeclaration));
         }
       }
+    }
 
+    // second, check return location annotation
+    if (!md.getReturnType().isVoid()) {
       if (!hasReturnLocDeclaration) {
         // if developer does not define method lattice
         // search return location in the method default lattice
@@ -338,19 +349,17 @@ public class FlowDownCheck {
 
       md2ReturnLoc.put(md, returnLocComp);
 
-      // check this location
+    }
+
+    if (!md.getReturnType().isVoid()) {
       MethodLattice<String> methodLattice = ssjava.getMethodLattice(md);
       String thisLocId = methodLattice.getThisLoc();
-      if (thisLocId == null) {
+      if ((!md.isStatic()) && thisLocId == null) {
         throw new Error("Method '" + md + "' does not have the definition of 'this' location at "
             + md.getClassDesc().getSourceFileName());
       }
       CompositeLocation thisLoc = new CompositeLocation(new Location(md, thisLocId));
       paramList.add(0, thisLoc);
-
-      // System.out.println("### ReturnLocGenerator=" + md);
-      // System.out.println("### md2ReturnLoc.get(md)=" + md2ReturnLoc.get(md));
-
       md2ReturnLocGen.put(md, new ReturnLocGenerator(md2ReturnLoc.get(md), md, paramList, md
           + " of " + cd.getSourceFileName()));
     }
@@ -605,6 +614,12 @@ public class FlowDownCheck {
       // check 'for loop' case
       BlockNode bn = ln.getInitializer();
       bn.getVarTable().setParent(nametable);
+      // need to check initialization node
+      // checkLocationFromBlockNode(md, bn.getVarTable(), bn, constraint);
+      for (int i = 0; i < bn.size(); i++) {
+        BlockStatementNode bsn = bn.get(i);
+        checkLocationFromBlockStatementNode(md, bn.getVarTable(), bsn, constraint);
+      }
 
       // calculate glb location of condition and update statements
       CompositeLocation condLoc =
@@ -653,7 +668,6 @@ public class FlowDownCheck {
             constraint, false);
 
     // addLocationType(isn.getCondition().getType(), condLoc);
-
     constraint = generateNewConstraint(constraint, condLoc);
     checkLocationFromBlockNode(md, nametable, isn.getTrueBlock(), constraint);
 
@@ -681,7 +695,7 @@ public class FlowDownCheck {
         }
         if (!isOwned) {
           throw new Error(
-              "It is now allowed to create the reference alias from the reference not owned by the method at "
+              "It is not allowed to create the reference alias from the reference not owned by the method at "
                   + generateErrorMessage(md.getClassDesc(), tn));
         }
 
@@ -728,11 +742,12 @@ public class FlowDownCheck {
 
   private void checkDeclarationInSubBlockNode(MethodDescriptor md, SymbolTable nametable,
       SubBlockNode sbn) {
-    checkDeclarationInBlockNode(md, nametable.getParent(), sbn.getBlockNode());
+    checkDeclarationInBlockNode(md, nametable, sbn.getBlockNode());
   }
 
   private CompositeLocation checkLocationFromBlockExpressionNode(MethodDescriptor md,
       SymbolTable nametable, BlockExpressionNode ben, CompositeLocation constraint) {
+
     CompositeLocation compLoc =
         checkLocationFromExpressionNode(md, nametable, ben.getExpression(), null, constraint, false);
     // addTypeLocation(ben.getExpression().getType(), compLoc);
@@ -887,7 +902,7 @@ public class FlowDownCheck {
       CompositeLocation constraint) {
 
     ClassDescriptor cd = md.getClassDesc();
-    MethodDescriptor calleeMD = min.getMethod();
+    MethodDescriptor calleeMethodDesc = min.getMethod();
 
     NameDescriptor baseName = min.getBaseName();
     boolean isSystemout = false;
@@ -895,8 +910,9 @@ public class FlowDownCheck {
       isSystemout = baseName.getSymbol().equals("System.out");
     }
 
-    if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
-        && !calleeMD.getModifiers().isNative() && !isSystemout) {
+    if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
+        && !ssjava.isTrustMethod(calleeMethodDesc) && !calleeMethodDesc.getModifiers().isNative()
+        && !isSystemout) {
 
       CompositeLocation baseLocation = null;
       if (min.getExpression() != null) {
@@ -921,19 +937,77 @@ public class FlowDownCheck {
       // min.printNode(0)
       // + " baseLocation=" + baseLocation + " constraint=" + constraint);
 
+      // setup the location list of caller's arguments
+      List<CompositeLocation> callerArgList = new ArrayList<CompositeLocation>();
+
+      // setup the location list of callee's parameters
+      MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleeMethodDesc);
+      List<CompositeLocation> calleeParamList = new ArrayList<CompositeLocation>();
+
+      if (min.numArgs() > 0) {
+        if (!calleeMethodDesc.isStatic()) {
+          callerArgList.add(baseLocation);
+        }
+        for (int i = 0; i < min.numArgs(); i++) {
+          ExpressionNode en = min.getArg(i);
+          CompositeLocation callerArgLoc =
+              checkLocationFromExpressionNode(md, nametable, en, new CompositeLocation(),
+                  constraint, false);
+          callerArgList.add(callerArgLoc);
+        }
+
+        if (!calleeMethodDesc.isStatic()) {
+          CompositeLocation calleeThisLoc =
+              new CompositeLocation(new Location(calleeMethodDesc, calleeLattice.getThisLoc()));
+          calleeParamList.add(calleeThisLoc);
+        }
+
+        for (int i = 0; i < calleeMethodDesc.numParameters(); i++) {
+          VarDescriptor calleevd = (VarDescriptor) calleeMethodDesc.getParameter(i);
+          CompositeLocation calleeLoc = d2loc.get(calleevd);
+          calleeParamList.add(calleeLoc);
+        }
+      }
+
       if (constraint != null) {
-        int compareResult =
-            CompositeLattice.compare(constraint, baseLocation, true, generateErrorMessage(cd, min));
-        if (compareResult != ComparisonResult.GREATER) {
-          // if the current constraint is higher than method's THIS location
-          // no need to check constraints!
-          CompositeLocation calleeConstraint =
-              translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
-          // System.out.println("check method body for constraint:" + calleeMD +
-          // " calleeConstraint="
-          // + calleeConstraint);
-          checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
+        // check whether the PC location is lower than one of the
+        // argument locations. If it is lower, the callee has to have @PCLOC
+        // annotation that declares the program counter that is higher than
+        // corresponding parameter
+
+        CompositeLocation calleePCLOC = ssjava.getPCLocation(calleeMethodDesc);
+
+        for (int idx = 0; idx < callerArgList.size(); idx++) {
+          CompositeLocation argLocation = callerArgList.get(idx);
+
+          // need to check that param is higher than PCLOC
+          if (!argLocation.get(0).isTop()
+              && CompositeLattice.compare(argLocation, constraint, true,
+                  generateErrorMessage(cd, min)) == ComparisonResult.GREATER) {
+
+            CompositeLocation paramLocation = calleeParamList.get(idx);
+
+            int paramCompareResult =
+                CompositeLattice.compare(calleePCLOC, paramLocation, true,
+                    generateErrorMessage(cd, min));
+
+            if (paramCompareResult == ComparisonResult.GREATER) {
+              throw new Error(
+                  "The program counter location "
+                      + constraint
+                      + " is lower than the argument(idx="
+                      + idx
+                      + ") location "
+                      + argLocation
+                      + ". Need to specify that the initial PC location of the callee, which is currently set to "
+                      + calleePCLOC + ", is lower than " + paramLocation + " in the method "
+                      + calleeMethodDesc.getSymbol() + ":" + min.getNumLine());
+            }
+
+          }
+
         }
+
       }
 
       checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
@@ -946,6 +1020,10 @@ public class FlowDownCheck {
         // location in the caller's perspective
         CompositeLocation ceilingLoc =
             computeCeilingLocationForCaller(md, nametable, min, baseLocation, constraint);
+
+        if (ceilingLoc == null) {
+          return new CompositeLocation(Location.createTopLocation(md));
+        }
         return ceilingLoc;
       }
     }
@@ -1065,7 +1143,9 @@ public class FlowDownCheck {
     List<CompositeLocation> argList = new ArrayList<CompositeLocation>();
 
     // by default, method has a THIS parameter
-    argList.add(baseLocation);
+    if (!md.isStatic()) {
+      argList.add(baseLocation);
+    }
 
     for (int i = 0; i < min.numArgs(); i++) {
       ExpressionNode en = min.getArg(i);
@@ -1087,11 +1167,10 @@ public class FlowDownCheck {
   private void checkCalleeConstraints(MethodDescriptor md, SymbolTable nametable,
       MethodInvokeNode min, CompositeLocation callerBaseLoc, CompositeLocation constraint) {
 
-    // System.out.println("checkCalleeConstraints=" + min.printNode(0));
-
     MethodDescriptor calleemd = min.getMethod();
 
     MethodLattice<String> calleeLattice = ssjava.getMethodLattice(calleemd);
+
     CompositeLocation calleeThisLoc =
         new CompositeLocation(new Location(calleemd, calleeLattice.getThisLoc()));
 
@@ -1104,7 +1183,8 @@ public class FlowDownCheck {
 
       // setup caller args set
       // first, add caller's base(this) location
-      callerArgList.add(callerBaseLoc);
+      if (!calleemd.isStatic())
+        callerArgList.add(callerBaseLoc);
       // second, add caller's arguments
       for (int i = 0; i < min.numArgs(); i++) {
         ExpressionNode en = min.getArg(i);
@@ -1116,7 +1196,8 @@ public class FlowDownCheck {
 
       // setup callee params set
       // first, add callee's this location
-      calleeParamList.add(calleeThisLoc);
+      if (!calleemd.isStatic())
+        calleeParamList.add(calleeThisLoc);
       // second, add callee's parameters
       for (int i = 0; i < calleemd.numParameters(); i++) {
         VarDescriptor calleevd = (VarDescriptor) calleemd.getParameter(i);
@@ -1424,26 +1505,28 @@ public class FlowDownCheck {
       }
     }
 
+    Set<CompositeLocation> inputGLB = new HashSet<CompositeLocation>();
     if (left instanceof ArrayAccessNode) {
       ArrayAccessNode aan = (ArrayAccessNode) left;
-      left = aan.getExpression();
+      CompositeLocation indexLoc =
+          checkLocationFromExpressionNode(md, nametable, aan.getIndex(), loc, constraint, false);
+      inputGLB.add(indexLoc);
     }
 
     loc = checkLocationFromExpressionNode(md, nametable, left, loc, constraint, false);
-    // System.out.println("### checkLocationFromFieldAccessNode=" +
-    // fan.printNode(0));
-    // System.out.println("### left=" + left.printNode(0));
 
     if (!left.getType().isPrimitive()) {
 
-      if (fd.getSymbol().equals("length")) {
+      if (!fd.getSymbol().equals("length")) {
         // array.length access, return the location of the array
-        return loc;
+        Location fieldLoc = getFieldLocation(fd);
+        loc.addLocation(fieldLoc);
       }
 
-      Location fieldLoc = getFieldLocation(fd);
-      loc.addLocation(fieldLoc);
     }
+
+    inputGLB.add(loc);
+    loc = CompositeLattice.calculateGLB(inputGLB, generateErrorMessage(md.getClassDesc(), fan));
     return loc;
   }
 
@@ -1487,9 +1570,6 @@ public class FlowDownCheck {
 
   private CompositeLocation checkLocationFromAssignmentNode(MethodDescriptor md,
       SymbolTable nametable, AssignmentNode an, CompositeLocation loc, CompositeLocation constraint) {
-
-    // System.out.println("\n# ASSIGNMENTNODE=" + an.printNode(0));
-
     ClassDescriptor cd = md.getClassDesc();
 
     Set<CompositeLocation> inputGLBSet = new HashSet<CompositeLocation>();
@@ -1517,20 +1597,29 @@ public class FlowDownCheck {
           checkLocationFromExpressionNode(md, nametable, an.getSrc(), new CompositeLocation(),
               constraint, false);
 
-      srcLocation = rhsLocation;
+      if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
+        // if assignment contains OP+EQ operator, need to merge location types
+        // of LHS & RHS into the RHS
+        Set<CompositeLocation> srcGLBSet = new HashSet<CompositeLocation>();
+        srcGLBSet.add(rhsLocation);
+        srcGLBSet.add(destLocation);
+        srcLocation = CompositeLattice.calculateGLB(srcGLBSet, generateErrorMessage(cd, an));
+      } else {
+        srcLocation = rhsLocation;
+      }
 
-      // if (!rhsLocation.get(rhsLocation.getSize() - 1).isTop()) {
       if (constraint != null) {
-        inputGLBSet.add(rhsLocation);
-        inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
-      }
-      // }
 
-//      System.out.println("dstLocation=" + destLocation);
-//      System.out.println("rhsLocation=" + rhsLocation);
-//      System.out.println("srcLocation=" + srcLocation);
-//      System.out.println("constraint=" + constraint);
+        if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
+          throw new Error("The value flow from " + constraint + " to " + destLocation
+              + " does not respect location hierarchy on the assignment " + an.printNode(0)
+              + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+        }
+        // inputGLBSet.add(srcLocation);
+        // inputGLBSet.add(constraint);
+        // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
+        // generateErrorMessage(cd, an));
+      }
 
       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
 
@@ -1544,6 +1633,16 @@ public class FlowDownCheck {
             + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
       }
 
+      if (srcLocation.equals(destLocation)) {
+        // keep it for definitely written analysis
+        Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
+        for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
+          FlatNode fn = (FlatNode) iterator.next();
+          ssjava.addSameHeightWriteFlatNode(fn);
+        }
+
+      }
+
     } else {
       destLocation =
           rhsLocation =
@@ -1551,17 +1650,21 @@ public class FlowDownCheck {
                   constraint, false);
 
       if (constraint != null) {
-        inputGLBSet.add(rhsLocation);
-        inputGLBSet.add(constraint);
-        srcLocation = CompositeLattice.calculateGLB(inputGLBSet, generateErrorMessage(cd, an));
+
+        if (!CompositeLattice.isGreaterThan(constraint, destLocation, generateErrorMessage(cd, an))) {
+          throw new Error("The value flow from " + constraint + " to " + destLocation
+              + " does not respect location hierarchy on the assignment " + an.printNode(0)
+              + " at " + cd.getSourceFileName() + "::" + an.getNumLine());
+        }
+        // inputGLBSet.add(rhsLocation);
+        // inputGLBSet.add(constraint);
+        // srcLocation = CompositeLattice.calculateGLB(inputGLBSet,
+        // generateErrorMessage(cd, an));
+        srcLocation = rhsLocation;
       } else {
         srcLocation = rhsLocation;
       }
 
-      // System.out.println("srcLocation=" + srcLocation);
-      // System.out.println("rhsLocation=" + rhsLocation);
-      // System.out.println("constraint=" + constraint);
-
       if (!CompositeLattice.isGreaterThan(srcLocation, destLocation, generateErrorMessage(cd, an))) {
 
         if (srcLocation.equals(destLocation)) {
@@ -1576,6 +1679,15 @@ public class FlowDownCheck {
 
       }
 
+      if (srcLocation.equals(destLocation)) {
+        // keep it for definitely written analysis
+        Set<FlatNode> flatNodeSet = ssjava.getBuildFlat().getFlatNodeSet(an);
+        for (Iterator iterator = flatNodeSet.iterator(); iterator.hasNext();) {
+          FlatNode fn = (FlatNode) iterator.next();
+          ssjava.addSameHeightWriteFlatNode(fn);
+        }
+      }
+
     }
 
     return destLocation;
@@ -1991,7 +2103,6 @@ public class FlowDownCheck {
 
       SSJavaLattice<String> locOrder = getLatticeByDescriptor(priorityDescriptor);
       String glbOfPriorityLoc = locOrder.getGLB(priorityLocIdentifierSet);
-
       glbCompLoc.addLocation(new Location(priorityDescriptor, glbOfPriorityLoc));
       Set<CompositeLocation> compSet = locId2CompLocSet.get(glbOfPriorityLoc);
 
@@ -2253,6 +2364,9 @@ class ReturnLocGenerator {
       // compute GLB of arguments subset that are same or higher than return
       // location
       if (inputGLB.isEmpty()) {
+        if (args.size() == 0) {
+          return null;
+        }
         CompositeLocation rtr =
             new CompositeLocation(Location.createTopLocation(args.get(0).get(0).getDescriptor()));
         return rtr;