Fixing a bug in ConflictTracker.java
[jpf-core.git] / src / main / gov / nasa / jpf / listener / ConflictTracker.java
index 134346775940c10aaf5acd5db90d67c8b16159b9..ca405b25dacf78abaec4f885cbef1ef0c7852956 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,7 +53,6 @@ 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";
@@ -93,123 +92,139 @@ 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;
-
-      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());
+       // Update the valueMap
+       for (int i = 0;i < nodeToProcess.getSetSet().size();i++) {
+               NameValuePair nameValuePair = nodeToProcess.getSetSet().get(i);
+
+               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()) {
-      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!";
-            return true;
-          }
-        }
-      }
+       // 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 = createErrorMessage(i, valueMap, writerMap);
+                                       return true;
+                               }
+                       }
+               }
        }
 
        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>();
+       boolean isChanged = false;
+       
+       if (setSet != null) {
+               for (int i = 0;i < setSet.size();i++) {
+                       updatedVarNames.add(setSet.get(i).getVarName());
+               }
+       }
+
+       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 (currentNode.getOutSet().contains(setSet.get(i)))
+                               currentNode.getOutSet().remove(setSet.get(i));
+                       isChanged |= currentNode.getOutSet().add(setSet.get(i));
+               }
+       }
 
-       // Set outSet according to inSet, and setSet of current node, check if there is a change
-    return setOutSet(currentNode);
+       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>();
+       HashMap<Node, ArrayList<NameValuePair>> setSetMap = new HashMap<Node, ArrayList<NameValuePair>>();
+       ArrayList<NameValuePair> setSet = new 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);
+         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()));
-      }
+       void setSetSet(ArrayList<NameValuePair> setSet, boolean isManual) {
+         if (isManual)
+           this.setSet = new ArrayList<NameValuePair>();
+
+         for (int i = 0;i < setSet.size();i++) {
+           this.setSet.add(new NameValuePair(setSet.get(i).getAppNum(), setSet.get(i).getValue(), 
+                                             setSet.get(i).getVarName(), setSet.get(i).getIsManual()));
+           }
        }
 
        Integer getId() {
@@ -224,17 +239,17 @@ public class ConflictTracker extends ListenerAdapter {
                return successors;
        }
 
-       HashSet<NameValuePair> getInSet() {
-               return inSet;
-       }
-
-       HashSet<NameValuePair> getSetSet() {
+       ArrayList<NameValuePair> getSetSet() {
                return setSet;
        }
 
        HashSet<NameValuePair> getOutSet() {
                return outSet;
        }
+
+       HashMap<Node, ArrayList<NameValuePair>> getSetSetMap() {
+               return setSetMap;
+       }
   }
 
   static class NameValuePair {
@@ -244,10 +259,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) {
@@ -328,17 +343,16 @@ 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;
-    }
+    currentNode.setSetSet(tempSetSet, manual);
+    tempSetSet = new ArrayList<NameValuePair>(); 
+    manual = false;
 
     if (search.isNewState()) {
       detail = "new";
@@ -363,12 +377,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 setSetMap of the current node
+    for (Node i : currentNode.getPredecessors()) {
+       currentNode.getSetSetMap().put(i, i.getSetSet());
+    }
+
+    // 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 for the conflict in this edge
+    conflictFound = checkForConflict(currentNode);
+    
     // Check if the outSet of this state has changed, update all of its successors' sets if any
     if (isChanged)
        conflictFound = conflictFound || propagateTheChange(currentNode);
@@ -543,16 +563,11 @@ public class ConflictTracker extends ListenerAdapter {
     if (writer.equals("App2"))
        temp = new NameValuePair(2, value, var, manual);
     
-    if (tempSetSet.contains(temp))
-       tempSetSet.remove(temp);
     tempSetSet.add(temp);
-    // Set isSet to true
-    isSet = true;
   }
 
   @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();
@@ -586,6 +601,7 @@ public class ConflictTracker extends ListenerAdapter {
       } else {
         if (executedInsn instanceof WriteInstruction) {
           String varId = ((WriteInstruction) executedInsn).getFieldInfo().getFullName();
+
           for (String var : conflictSet) {
             if (varId.contains(var)) {
               // Get variable info