A minor change in ConflictTracker.java
[jpf-core.git] / src / main / gov / nasa / jpf / listener / ConflictTracker.java
index 51e34fc89f0c9a50b64244968c813f7ca1516d21..a0ecc4bb6721597ba8631d83deeb8929feb207f8 100644 (file)
@@ -43,7 +43,7 @@ public class ConflictTracker extends ListenerAdapter {
   private final HashSet<String> appSet = new HashSet<String>(); // Apps we want to find their conflicts
   private final HashSet<String> manualSet = new HashSet<String>(); // Writer classes with manual inputs to detect direct-direct(No Conflict) interactions
   private final HashMap<Integer, Node> nodes = new HashMap<Integer, Node>(); // Nodes of a graph
-  private HashSet<NameValuePair> tempSetSet = new HashSet<NameValuePair>();
+  private ArrayList<NameValuePair> tempSetSet = new ArrayList<NameValuePair>();
   private long timeout;
   private long startTime;
   private Node parentNode = new Node(-2);
@@ -53,9 +53,10 @@ public class ConflictTracker extends ListenerAdapter {
   private int depth;
   private int id;
   private boolean conflictFound = false;
-  private boolean isSet = false;
   private boolean manual = false;
+
+  private final String SET_LOCATION_METHOD = "setLocationMode";
+  private final String LOCATION_VAR = "locationMode";
   
   public ConflictTracker(Config config, JPF jpf) {
     out = new PrintWriter(System.out, true);
@@ -91,80 +92,74 @@ public class ConflictTracker extends ListenerAdapter {
        HashSet<Node> changed = new HashSet<Node>(currentNode.getSuccessors());
 
        while(!changed.isEmpty()) {
-               // Get the first element of HashSet and remove it from the changed set
-               Node nodeToProcess = changed.iterator().next();
-               changed.remove(nodeToProcess);
-                       
-               // Update the sets, store the outSet to temp before its changes
-               boolean isChanged = updateSets(nodeToProcess);
-
-               // Check for a conflict
-               if (checkForConflict(nodeToProcess))
-                       return true;
-                       
-               // Checking if the out set has changed or not(Add its successors to the change list!)
-               if (isChanged) {
-                       for (Node i : nodeToProcess.getSuccessors()) {
-                               if (!changed.contains(i))
-                                       changed.add(i);
-                       }
-               }
-       }
-
-       return false;
-  }
+               // Get the first element of HashSet and remove it from the changed set
+               Node nodeToProcess = changed.iterator().next();
+               changed.remove(nodeToProcess);
 
-  boolean setOutSet(Node currentNode) {
-       Integer prevSize = currentNode.getOutSet().size();
+               // Update the edge
+               boolean isChanged = updateEdge(currentNode, nodeToProcess);
 
-       // Update based on setSet
-       for (NameValuePair i : currentNode.getSetSet()) {
-               if (currentNode.getOutSet().contains(i))
-                       currentNode.getOutSet().remove(i);      
-               currentNode.getOutSet().add(i);
-       }
+               // Check for a conflict in this transition(currentNode -> nodeToProcess)
+               if (checkForConflict(nodeToProcess))
+                               return true;
 
-       // Add all the inSet
-       currentNode.getOutSet().addAll(currentNode.getInSet());
+               // Checking if the out set has changed or not(Add its successors to the change list!)
+               if (isChanged) {
+                       propagateTheChange(nodeToProcess);
+               }
+      }
 
-       // Check if the outSet is changed
-       if (!prevSize.equals(currentNode.getOutSet().size()))
-               return true;
-       
-       return false;
+      return false;
   }
 
-  void setInSet(Node currentNode) {
-       for (Node i : currentNode.getPredecessors()) {
-               currentNode.getInSet().addAll(i.getOutSet());
-       }
+  String createErrorMessage(NameValuePair pair, HashMap<String, String> valueMap, HashMap<String, Integer> writerMap) {
+       String message = "Conflict found between the two apps. App"+pair.getAppNum()+
+                        " has written the value: "+pair.getValue()+
+                        " to the variable: "+pair.getVarName()+" while App"+
+                        writerMap.get(pair.getVarName())+" is overwriting the value: "
+                        +valueMap.get(pair.getVarName())+" to the same variable!";
+       System.out.println(message);    
+       return message;
   }
 
-  boolean checkForConflict(Node currentNode) {
+  boolean checkForConflict(Node nodeToProcess) {
        HashMap<String, String> valueMap = new HashMap<String, String>(); // HashMap from varName to value
        HashMap<String, Integer> writerMap = new HashMap<String, Integer>(); // HashMap from varName to appNum
 
-       for (NameValuePair i : currentNode.getSetSet()) {
-               if (i.getIsManual()) // Manual input: we have no conflict
-                       continue;
+       // Update the valueMap
+       for (int i = 0;i < nodeToProcess.getSetSet().size();i++) {
+               NameValuePair nameValuePair = nodeToProcess.getSetSet().get(i);
 
-               valueMap.put(i.getVarName(), i.getValue());
-               if (writerMap.containsKey(i.getVarName()))
-                       writerMap.put(i.getVarName(), i.getAppNum()+writerMap.get(i.getVarName())); // We have two writers?
-               else
-                       writerMap.put(i.getVarName(), i.getAppNum());
+               if (valueMap.containsKey(nameValuePair.getVarName())) {
+                       // Check if we have a same writer
+                       if (!writerMap.get(nameValuePair.getVarName()).equals(nameValuePair.getAppNum())) {
+                               // Check if we have a conflict or not
+                               if (!valueMap.get(nameValuePair.getVarName()).equals(nameValuePair.getValue())) {
+                                       errorMessage = createErrorMessage(nameValuePair, valueMap, writerMap);
+                                       return true;
+                               } else { // We have two writers writing the same value
+                                       writerMap.put(nameValuePair.getVarName(), 3); // 3 represents both apps
+                               }       
+                       } else {
+                               // Check if we have more than one value with the same writer
+                               if (!valueMap.get(nameValuePair.getVarName()).equals(nameValuePair.getValue())) {
+                                       valueMap.put(nameValuePair.getVarName(), "twoValue"); // We have one writer writing more than one value in a same event
+                               }
+                       }       
+               } else {
+                       valueMap.put(nameValuePair.getVarName(), nameValuePair.getValue());
+                       writerMap.put(nameValuePair.getVarName(), nameValuePair.getAppNum());
+               }
        }
 
-       // Comparing the inSet and setSet to find the conflict
-       for (NameValuePair i : currentNode.getInSet()) {
+       // Comparing the outSet to setSet
+       for (NameValuePair i : nodeToProcess.getOutSet()) {
                if (valueMap.containsKey(i.getVarName())) {
                        String value = valueMap.get(i.getVarName());
                        Integer writer = writerMap.get(i.getVarName());
                        if ((value != null)&&(writer != null)) {
                                if (!value.equals(i.getValue())&&!writer.equals(i.getAppNum())) { // We have different values
-                                       errorMessage = "Conflict found between the two apps. App"+i.getAppNum()+" has written the value: "+i.getValue()+
-                                                       " to the variable: "+i.getVarName()+" while App"+writerMap.get(i.getVarName())+" is overwriting the value: "
-                                                       +valueMap.get(i.getVarName())+" to the same variable!";
+                                       errorMessage = createErrorMessage(i, valueMap, writerMap);
                                        return true;
                                }
                        }
@@ -174,40 +169,54 @@ public class ConflictTracker extends ListenerAdapter {
        return false;
   }
 
-  boolean updateSets(Node currentNode) {
-       // Set input set according to output set of pred states of current state
-       setInSet(currentNode);
+  boolean updateEdge(Node parentNode, Node currentNode) {
+       ArrayList<NameValuePair> setSet = currentNode.getSetSetMap().get(parentNode);
+       HashSet<String> updatedVarNames = new HashSet<String>();
+       HashMap<Integer, String> writerLastValue = new HashMap<Integer, String>();
 
-       // Set outSet according to inSet, and setSet of current node, check if there is a change
-       return setOutSet(currentNode);
+       boolean isChanged = false;
+       
+       if (setSet != null) {
+               for (int i = 0;i < setSet.size();i++) {
+                       updatedVarNames.add(setSet.get(i).getVarName());
+                       writerLastValue.put(setSet.get(i).getAppNum(), setSet.get(i).getValue());
+               }
+       }
+
+       for (NameValuePair i : parentNode.getOutSet()) {
+               if (!updatedVarNames.contains(i.getVarName()))
+                       isChanged |= currentNode.getOutSet().add(i);
+       }
+
+       if (setSet != null) {
+               for (int i = 0;i < setSet.size();i++) {
+                       if (setSet.get(i).getValue().equals(writerLastValue.get(setSet.get(i).getAppNum()))) {
+                               isChanged |= currentNode.getOutSet().add(setSet.get(i));
+                       }
+               }
+       }
+
+       return isChanged;
   }
 
   static class Node {
-       Integer id;
+        Integer id;
        HashSet<Node> predecessors = new HashSet<Node>();
        HashSet<Node> successors = new HashSet<Node>();
-       HashSet<NameValuePair> inSet = new HashSet<NameValuePair>();
-       HashSet<NameValuePair> setSet = new HashSet<NameValuePair>();
        HashSet<NameValuePair> outSet = new HashSet<NameValuePair>();
+        ArrayList<NameValuePair> setSet = new ArrayList<NameValuePair>();
+       HashMap<Node, ArrayList<NameValuePair>> setSetMap = new HashMap<Node, ArrayList<NameValuePair>>();
 
        Node(Integer id) {
-               this.id = id;
+         this.id = id;
        }
 
        void addPredecessor(Node node) {
-               predecessors.add(node);
+         predecessors.add(node);
        }
 
        void addSuccessor(Node node) {
-               successors.add(node);
-       }
-
-       void setSetSet(HashSet<NameValuePair> setSet, boolean isManual) {
-               if (isManual)
-                       this.setSet = new HashSet<NameValuePair>();
-               for (NameValuePair i : setSet) {
-                       this.setSet.add(new NameValuePair(i.getAppNum(), i.getValue(), i.getVarName(), i.getIsManual()));
-               }
+         successors.add(node);
        }
 
        Integer getId() {
@@ -222,16 +231,16 @@ public class ConflictTracker extends ListenerAdapter {
                return successors;
        }
 
-       HashSet<NameValuePair> getInSet() {
-               return inSet;
+       HashSet<NameValuePair> getOutSet() {
+               return outSet;
        }
 
-       HashSet<NameValuePair> getSetSet() {
+        ArrayList<NameValuePair> getSetSet() {
                return setSet;
-       }
+        }
 
-       HashSet<NameValuePair> getOutSet() {
-               return outSet;
+       HashMap<Node, ArrayList<NameValuePair>> getSetSetMap() {
+               return setSetMap;
        }
   }
 
@@ -242,10 +251,10 @@ public class ConflictTracker extends ListenerAdapter {
        boolean isManual;
 
        NameValuePair(Integer appNum, String value, String varName, boolean isManual) {
-               this.appNum = appNum;
-               this.value = value;
-               this.varName = varName;
-               this.isManual = isManual;
+               this.appNum = appNum;
+               this.value = value;
+               this.varName = varName;
+               this.isManual = isManual;
        }
 
        void setAppNum(Integer appNum) {
@@ -282,12 +291,12 @@ public class ConflictTracker extends ListenerAdapter {
 
        @Override
        public boolean equals(Object o) {
-               if (o instanceof NameValuePair) {
-                       NameValuePair other = (NameValuePair) o;
-                       if (varName.equals(other.getVarName()))
-                               return appNum.equals(other.getAppNum());
-               }
-               return false;
+      if (o instanceof NameValuePair) {
+        NameValuePair other = (NameValuePair) o;
+        if (varName.equals(other.getVarName()))
+          return appNum.equals(other.getAppNum());
+      }
+      return false;
        }
 
        @Override
@@ -307,9 +316,9 @@ public class ConflictTracker extends ListenerAdapter {
   
     // Update the parent node
     if (nodes.containsKey(id)) {
-       parentNode = nodes.get(id);
+         parentNode = nodes.get(id);
     } else {
-       parentNode = new Node(id);
+         parentNode = new Node(id);
     }
   }
 
@@ -326,17 +335,23 @@ public class ConflictTracker extends ListenerAdapter {
     depth = search.getDepth();
     operation = "forward";
 
-    // Add the node to the list of nodes       
-    nodes.put(id, new Node(id));
+    // Add the node to the list of nodes
+    if (nodes.get(id) == null)
+       nodes.put(id, new Node(id));
+
     Node currentNode = nodes.get(id);
 
-    // Update the setSet for this new node
-    if (isSet) {
-      currentNode.setSetSet(tempSetSet, manual);
-      tempSetSet = new HashSet<NameValuePair>(); 
-      isSet = false;
-      manual = false;
-    }
+    if ((currentNode.getSetSetMap().get(parentNode) == null) || manual)
+       currentNode.getSetSetMap().put(parentNode, new ArrayList<NameValuePair>());
+
+    // Update the setSet for the edge
+    currentNode.getSetSetMap().get(parentNode).addAll(tempSetSet);
+    parentNode.getSetSet().addAll(tempSetSet);
+    tempSetSet = new ArrayList<NameValuePair>();
+    manual = false;
+
+    // Check for the conflict in this edge
+    conflictFound = checkForConflict(parentNode);
 
     if (search.isNewState()) {
       detail = "new";
@@ -361,21 +376,18 @@ public class ConflictTracker extends ListenerAdapter {
     if (!(parentNode.getSuccessors().contains(currentNode)))
         parentNode.addSuccessor(currentNode);
 
-    // Update the sets, check if the outSet is changed or not
-    boolean isChanged = updateSets(currentNode);
-   
-    // Check for a conflict
-    conflictFound = checkForConflict(currentNode);
-
+    // Update the edge and check if the outset of the current node is changed or not to propagate the change
+    boolean isChanged = updateEdge(parentNode, currentNode);
+    
     // Check if the outSet of this state has changed, update all of its successors' sets if any
     if (isChanged)
        conflictFound = conflictFound || propagateTheChange(currentNode);
 
     // Update the parent node
     if (nodes.containsKey(id)) {
-       parentNode = nodes.get(id);
+         parentNode = nodes.get(id);
     } else {
-       parentNode = new Node(id);
+         parentNode = new Node(id);
     }
   }
 
@@ -390,9 +402,9 @@ public class ConflictTracker extends ListenerAdapter {
 
     // Update the parent node
     if (nodes.containsKey(id)) {
-       parentNode = nodes.get(id);
+         parentNode = nodes.get(id);
     } else {
-       parentNode = new Node(id);
+         parentNode = new Node(id);
     }
   }
 
@@ -410,13 +422,13 @@ public class ConflictTracker extends ListenerAdapter {
     if ((inst instanceof JVMLocalVariableInstruction) ||
         (inst instanceof JVMFieldInstruction))
     {
-       if (frame.getTopPos() < 0)
-         return(null);
+      if (frame.getTopPos() < 0)
+        return(null);
 
-       lo = frame.peek();
-       hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
+      lo = frame.peek();
+      hi = frame.getTopPos() >= 1 ? frame.peek(1) : 0;
 
-       return(decodeValue(type, lo, hi));
+      return(decodeValue(type, lo, hi));
     }
 
     if (inst instanceof JVMArrayElementInstruction)
@@ -535,9 +547,17 @@ public class ConflictTracker extends ListenerAdapter {
     return null;
   }
 
+  private void writeWriterAndValue(String writer, String value, String var) {
+    // Update the temporary Set set.
+    NameValuePair temp = new NameValuePair(1, value, var, manual);
+    if (writer.equals("App2"))
+       temp = new NameValuePair(2, value, var, manual);
+    
+    tempSetSet.add(temp);
+  }
+
   @Override
   public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
-    // Instantiate timeoutTimer
     if (timeout > 0) {
       if (System.currentTimeMillis() - startTime > timeout) {
         StringBuilder sbTimeOut = new StringBuilder();
@@ -552,36 +572,45 @@ public class ConflictTracker extends ListenerAdapter {
       sb.append(errorMessage);
       Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
       ti.setNextPC(nextIns);
-    } else if (executedInsn instanceof WriteInstruction) {
-      String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
-      for(String var : conflictSet) {
-
-        if (varId.contains(var)) {
-          // Get variable info
+    } else {
+      if (conflictSet.contains(LOCATION_VAR)) {
+        MethodInfo mi = executedInsn.getMethodInfo();
+        // Find the last load before return and get the value here
+        if (mi.getName().equals(SET_LOCATION_METHOD) &&
+                executedInsn instanceof ALOAD && nextInsn instanceof ARETURN) {
           byte type  = getType(ti, executedInsn);
           String value = getValue(ti, executedInsn, type);
-          String writer = getWriter(ti.getStack(), appSet);
-          // Just return if the writer is not one of the listed apps in the .jpf file
-          if (writer == null)
-            return;
-
-         if (getWriter(ti.getStack(), manualSet) != null)
-               manual = true;
-
-         // Update the temporary Set set.
-         if (writer.equals("App1"))
-                 tempSetSet.add(new NameValuePair(1, value, var, manual));
-         else if (writer.equals("App2"))
-                 tempSetSet.add(new NameValuePair(2, value, var, manual));
-         // Set isSet to true
-         isSet = true;
-                 
-       }
-     }
 
-      
-  }
-  
- }
+          // Extract the writer app name
+          ClassInfo ci = mi.getClassInfo();
+          String writer = ci.getName();
 
+          // Update the temporary Set set.
+          writeWriterAndValue(writer, value, LOCATION_VAR);
+        }
+      } else {
+        if (executedInsn instanceof WriteInstruction) {
+          String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
+
+          for (String var : conflictSet) {
+            if (varId.contains(var)) {
+              // Get variable info
+              byte type = getType(ti, executedInsn);
+              String value = getValue(ti, executedInsn, type);
+              String writer = getWriter(ti.getStack(), appSet);
+              // Just return if the writer is not one of the listed apps in the .jpf file
+              if (writer == null)
+                return;
+
+              if (getWriter(ti.getStack(), manualSet) != null)
+                manual = true;
+
+              // Update the temporary Set set.
+              writeWriterAndValue(writer, value, var);
+            }
+          }
+        }
+      }
+    }
+  }
 }