Add some minor changes to the listener to make the error message more clean!
[jpf-core.git] / src / main / gov / nasa / jpf / listener / ConflictTracker.java
index 307463e7870d6b28eb3880b2cc8a9eb63e850b0e..9fb12cf3dac2aa6a4769468a7966edf7a7d5e710 100644 (file)
@@ -41,20 +41,20 @@ public class ConflictTracker extends ListenerAdapter {
   private final PrintWriter out;
   private final HashSet<String> conflictSet = new HashSet<String>(); // Variables we want to track
   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 final DataSet tempSetSet = new DataSet(false, "NA", false, "NA");
+  private HashSet<NameValuePair> tempSetSet = new HashSet<NameValuePair>();
   private long timeout;
   private long startTime;
-  volatile private Node parentNode = new Node(-2);
-  volatile private String operation;
-  volatile private String detail;
-  volatile private int depth;
-  volatile private int id;
-  volatile private boolean conflictFound = false;
-  volatile private boolean isSet = false;
-  Transition transition;
-  Object annotation;
-  String label;
+  private Node parentNode = new Node(-2);
+  private String operation;
+  private String detail;
+  private String errorMessage;
+  private int depth;
+  private int id;
+  private boolean conflictFound = false;
+  private boolean isSet = false;
+  private boolean manual = false;
  
   
   public ConflictTracker(Config config, JPF jpf) {
@@ -74,106 +74,118 @@ public class ConflictTracker extends ListenerAdapter {
         appSet.add(var);
       }
     }
+    String[] manualClasses = config.getStringArray("manualClasses");
+    // We are not tracking anything if it is null
+    if (manualClasses != null) {
+      for (String var : manualClasses) {
+        manualSet.add(var);
+      }
+    }
+
     // Timeout input from config is in minutes, so we need to convert into millis
     timeout = config.getInt("timeout", 0) * 60 * 1000;
     startTime = System.currentTimeMillis();
   }
 
-  public void setOutSet(Node currentNode) {
-       DataList inSetApp1List = currentNode.getInSet().getApp1DataList(); // Store the app1List of inSet of current node to temp data set
-       DataList inSetApp2List = currentNode.getInSet().getApp2DataList(); // Store the app2List of inSet of current node to temp data set
-       DataList setSetApp1List = currentNode.getSetSet().getApp1DataList(); // Store the app1List of setSet of current node to temp data set
-       DataList setSetApp2List = currentNode.getSetSet().getApp2DataList(); // Store the app2List of setSet of current node to temp data set
-       DataSet outSetTemp = currentNode.getOutSet(); // Store the outSet of current node to temp data set
-
-       if ((setSetApp1List.getAppSet())&&(setSetApp2List.getAppSet())) { 
-               // App1 & App2 are writers
-               outSetTemp.setApp1DataList(setSetApp1List.getAppSet(), setSetApp1List.getValue());
-               outSetTemp.setApp2DataList(setSetApp2List.getAppSet(), setSetApp2List.getValue());
-       } else if (setSetApp2List.getAppSet()) { 
-               // App1 is a writer
-               outSetTemp.setApp1DataList(setSetApp1List.getAppSet(), setSetApp1List.getValue());
-               outSetTemp.setApp2DataList(inSetApp2List.getAppSet(), inSetApp2List.getValue());
-       } else if (setSetApp2List.getAppSet()) { 
-               // App2 is a writer
-               outSetTemp.setApp1DataList(inSetApp1List.getAppSet(),inSetApp1List.getValue());
-               outSetTemp.setApp2DataList(setSetApp2List.getAppSet(), setSetApp2List.getValue());
-       } else { 
-               // No writer for this node => outSet = inSet
-               outSetTemp.setApp1DataList(inSetApp1List.getAppSet(), inSetApp1List.getValue());
-               outSetTemp.setApp2DataList(inSetApp2List.getAppSet(), inSetApp2List.getValue());
-       }
-  }
+  boolean propagateTheChange(Node currentNode) {
+       HashSet<Node> changed = new HashSet<Node>(currentNode.getSuccessors());
 
-  public void setInSet(Node currentNode) {
-       for (Node i : currentNode.getPredecessors()) {
-               // Check to see if dataList1 of predNode(i) is valid or not: if valid get its value
-               if (i.getOutSet().getApp1DataList().getAppSet()) {
-                       currentNode.getInSet().setApp1DataList(true, i.getOutSet().getApp1DataList().getValue());
-               }
+       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 to see if dataList2 of predNode(i) is valid or not: if valid get its value
-               if (i.getOutSet().getApp2DataList().getAppSet()) {
-                       currentNode.getInSet().setApp2DataList(true, i.getOutSet().getApp2DataList().getValue());
+               // 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);
+                       }
                }
-       }
-  }
+       }
 
-  boolean checkOutSetChange(Node currentNode, DataSet tempOutSet) {
-       DataList outSetApp1List = currentNode.getOutSet().getApp1DataList(); // Store the app1List of inSet of current node to temp data set
-       DataList outSetApp2List = currentNode.getOutSet().getApp2DataList(); // Store the app2List of inSet of current node to temp data set
-       DataList tempSetApp1List = tempOutSet.getApp1DataList(); // Store the app1List of tempOutSet of current node to temp data set
-       DataList tempSetApp2List = tempOutSet.getApp2DataList(); // Store the app1List of tempOutSet of current node to temp data set
-
-       // Return 1 if the outSet has changed!
-       return ((outSetApp1List.getAppSet() != tempSetApp1List.getAppSet())||
-               !(outSetApp1List.getValue().equals(tempSetApp1List.getValue()))||
-               (outSetApp2List.getAppSet() != tempSetApp2List.getAppSet())||
-               !(outSetApp2List.getValue().equals(tempSetApp2List.getValue())));
+       return false;
   }
 
-  public void checkForConflict(Node currentNode) {
-       DataList outSetApp1List = currentNode.getOutSet().getApp1DataList(); // Store the app1List of inSet of current node to temp data set
-       DataList outSetApp2List = currentNode.getOutSet().getApp2DataList(); // Store the app2List of inSet of current node to temp data set
+  boolean setOutSet(Node currentNode) {
+       Integer prevSize = currentNode.getOutSet().size();
 
-       if ((outSetApp1List.getAppSet() == true)&&(outSetApp2List.getAppSet() == true)) { 
-               // Both apps have set the device
-               if (!(outSetApp1List.getValue().equals(outSetApp2List.getValue()))) {
-                       // Values set by the apps are not the same, and we have a conflict!
-                       conflictFound = true;
-               }
+       // Update based on setSet
+       for (NameValuePair i : currentNode.getSetSet()) {
+               if (currentNode.getOutSet().contains(i))
+                       currentNode.getOutSet().remove(i);      
+               currentNode.getOutSet().add(i);
        }
+
+       // Add all the inSet
+       currentNode.getOutSet().addAll(currentNode.getInSet());
+
+       // Check if the outSet is changed
+       if (!prevSize.equals(currentNode.getOutSet().size()))
+               return true;
+       
+       return false;
   }
 
-  public DataSet updateSets(Node currentNode) {
-       // Empty the in set of the node
-       currentNode.emptyInSet();
+  void setInSet(Node currentNode) {
+       for (Node i : currentNode.getPredecessors()) {
+               currentNode.getInSet().addAll(i.getOutSet());
+       }
+  }
 
-       DataList outSetApp1List = currentNode.getOutSet().getApp1DataList();
-        DataList outSetApp2List = currentNode.getOutSet().getApp2DataList();
+  boolean checkForConflict(Node currentNode) {
+       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
 
-       // Store the out set of this state to the temporary data set
-       DataSet tempOutSet = new DataSet(outSetApp1List.getAppSet(),
-                                        outSetApp1List.getValue(),
-                                        outSetApp2List.getAppSet(),
-                                        outSetApp2List.getValue());
+       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());
+       }
+
+       // Comparing the inSet and setSet to find the conflict
+       for (NameValuePair i : currentNode.getInSet()) {
+               if (valueMap.containsKey(i.getVarName())) {
+                       if (!(valueMap.get(i.getVarName()).equals(i.getValue()))) // We have different values
+                               if (!(writerMap.get(i.getVarName()).equals(i.getAppNum()))) {// We have different writers
+                                       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;
+                               }
+               }
+       }
 
+       return false;
+  }
+
+  boolean updateSets(Node currentNode) {
        // Set input set according to output set of pred states of current state
        setInSet(currentNode);
 
-       // Set dataLists of outSet to dataLists of setSet if it is valid, otherwise to dataLists of inSet
-       setOutSet(currentNode);
-    
-       return tempOutSet;
+       // Set outSet according to inSet, and setSet of current node, check if there is a change
+       return setOutSet(currentNode);
   }
 
-  class Node {
+  static class Node {
        Integer id;
-       ArrayList<Node> predecessors = new ArrayList<Node>();
-       ArrayList<Node> successors = new ArrayList<Node>();
-       DataSet inSet = new DataSet(false, "NA", false, "NA");
-       DataSet setSet = new DataSet(false, "NA", false, "NA");
-       DataSet outSet = new DataSet(false, "NA", false, "NA");
+       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>();
 
        Node(Integer id) {
                this.id = id;
@@ -187,109 +199,97 @@ public class ConflictTracker extends ListenerAdapter {
                successors.add(node);
        }
 
-       void emptyInSet() {
-               this.inSet = new DataSet(false, "NA", false, "NA");
-       }
-
-       void setInSet(DataSet inSet) {
-               DataList inSetApp1List = inSet.getApp1DataList();
-               DataList inSetApp2List = inSet.getApp2DataList();
-               this.inSet.setApp1DataList(inSetApp1List.getAppSet(), inSetApp1List.getValue());
-               this.inSet.setApp2DataList(inSetApp2List.getAppSet(), inSetApp2List.getValue());
-       }
-
-       void setSetSet(DataSet setSet) {
-               DataList setSetApp1List = setSet.getApp1DataList();
-               DataList setSetApp2List = setSet.getApp2DataList();
-               this.setSet.setApp1DataList(setSetApp1List.getAppSet(), setSetApp1List.getValue());
-               this.setSet.setApp2DataList(setSetApp2List.getAppSet(), setSetApp2List.getValue());
-       }
-
-       void setOutSet(DataSet outSet) {
-               DataList outSetApp1List = outSet.getApp1DataList();
-               DataList outSetApp2List = outSet.getApp2DataList();
-               this.outSet.setApp1DataList(outSetApp1List.getAppSet(), outSetApp1List.getValue());
-               this.outSet.setApp2DataList(outSetApp2List.getAppSet(), outSetApp2List.getValue());
+       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()));
+               }
        }
 
        Integer getId() {
                return id;
        }
 
-       ArrayList<Node> getPredecessors() {
+       HashSet<Node> getPredecessors() {
                return predecessors;
        }
 
-       ArrayList<Node> getSuccessors() {
+       HashSet<Node> getSuccessors() {
                return successors;
        }
 
-       DataSet getInSet() {
+       HashSet<NameValuePair> getInSet() {
                return inSet;
        }
 
-       DataSet getOutSet() {
-               return outSet;
+       HashSet<NameValuePair> getSetSet() {
+               return setSet;
        }
 
-       DataSet getSetSet() {
-               return setSet;
+       HashSet<NameValuePair> getOutSet() {
+               return outSet;
        }
   }
 
-  class DataList {
-       boolean appSet;
+  static class NameValuePair {
+       Integer appNum;
        String value;
+       String varName;
+       boolean isManual;
 
-       DataList(boolean appSet, String value) {
-               this.appSet = appSet;
+       NameValuePair(Integer appNum, String value, String varName, boolean isManual) {
+               this.appNum = appNum;
                this.value = value;
+               this.varName = varName;
+               this.isManual = isManual;
        }
 
-       void setAppSet(boolean appSet) {
-               this.appSet = appSet;
+       void setAppNum(Integer appNum) {
+               this.appNum = appNum;
        }
 
        void setValue(String value) {
                this.value = value;
        }
 
-       boolean getAppSet() {
-               return appSet;
+       void setVarName(String varName) {
+               this.varName = varName;
        }
 
-       String getValue() {
-               return value;
+        void setIsManual(String varName) {
+               this.isManual = isManual;
        }
-  }
 
-  class DataSet {
-       DataList app1DataList = new DataList(false, "NA");
-       DataList app2DataList = new DataList(false, "NA");      
+       Integer getAppNum() {
+               return appNum;
+       }
 
-       DataSet(boolean app1Set, String app1Value, boolean app2Set, String app2Value) {
-               app1DataList.setAppSet(app1Set);
-               app1DataList.setValue(app1Value);
-               app2DataList.setAppSet(app2Set);
-               app2DataList.setValue(app2Value);
+       String getValue() {
+               return value;
        }
 
-       void setApp1DataList(boolean appSet, String value) {
-               app1DataList.setAppSet(appSet);
-               app1DataList.setValue(value);
+       String getVarName() {
+               return varName;
        }
 
-       void setApp2DataList(boolean appSet, String value) {
-               app2DataList.setAppSet(appSet);
-               app2DataList.setValue(value);
+       boolean getIsManual() {
+               return isManual;
        }
 
-       DataList getApp1DataList() {
-               return app1DataList;
+       @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;
        }
 
-       DataList getApp2DataList() {
-               return app2DataList;
+       @Override
+       public int hashCode() {
+               return appNum.hashCode() * 31 + varName.hashCode();
        }
   }
 
@@ -303,7 +303,11 @@ public class ConflictTracker extends ListenerAdapter {
     out.println("The state is restored to state with id: "+id+", depth: "+depth);
   
     // Update the parent node
-    parentNode = new Node(id);
+    if (nodes.containsKey(id)) {
+       parentNode = nodes.get(id);
+    } else {
+       parentNode = new Node(id);
+    }
   }
 
   @Override
@@ -321,16 +325,18 @@ public class ConflictTracker extends ListenerAdapter {
 
     // Add the node to the list of nodes       
     nodes.put(id, new Node(id));
-    Node currentNode = nodes.get(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 (search.isNewState()) {
-      // Add this new node
       detail = "new";
-      // Update the setSet for this new node
-      if (isSet) {
-       currentNode.setSetSet(tempSetSet);
-       isSet = false;
-      }
     } else {
       detail = "visited";
     }
@@ -352,39 +358,22 @@ public class ConflictTracker extends ListenerAdapter {
     if (!(parentNode.getSuccessors().contains(currentNode)))
         parentNode.addSuccessor(currentNode);
 
-    // Update the sets, store the outSet to temp before its changes
-    DataSet tempOutSet = updateSets(currentNode);
-
+    // Update the sets, check if the outSet is changed or not
+    boolean isChanged = updateSets(currentNode);
+   
     // Check for a conflict
-    checkForConflict(currentNode);
+    conflictFound = checkForConflict(currentNode);
 
     // Check if the outSet of this state has changed, update all of its successors' sets if any
-    if (checkOutSetChange(currentNode, tempOutSet)) {
-               ArrayList<Node> changed = new ArrayList<Node>(currentNode.getSuccessors());
-               while(!changed.isEmpty()) {
-                       // Get a random node inside the changed list and remove it from the list
-                       Integer rnd = new Random().nextInt(changed.size());
-                       Node nodeToProcess  = changed.get(rnd);
-                       changed.remove(nodeToProcess);
-
-                       // Update the sets, store the outSet to temp before its changes
-                       tempOutSet = updateSets(nodeToProcess);
-
-                       // Check for a conflict
-                       checkForConflict(nodeToProcess);
-                       
-                       // Checking if the out set has changed or not(Add its successors to the change list!)
-                       if (checkOutSetChange(nodeToProcess, tempOutSet)) {
-                               for (Node i : nodeToProcess.getSuccessors()) {
-                                       if (!changed.contains(i))
-                                               changed.add(i);
-                               }
-                       }
-               }
-    }
+    if (isChanged)
+       conflictFound = conflictFound || propagateTheChange(currentNode);
 
     // Update the parent node
-    parentNode = new Node(id);
+    if (nodes.containsKey(id)) {
+       parentNode = nodes.get(id);
+    } else {
+       parentNode = new Node(id);
+    }
   }
 
   @Override
@@ -397,7 +386,11 @@ public class ConflictTracker extends ListenerAdapter {
     out.println("The state is backtracked to state with id: "+id+", depth: "+depth);
 
     // Update the parent node
-    parentNode = new Node(id);
+    if (nodes.containsKey(id)) {
+       parentNode = nodes.get(id);
+    } else {
+       parentNode = new Node(id);
+    }
   }
 
   @Override
@@ -521,18 +514,16 @@ public class ConflictTracker extends ListenerAdapter {
 
   // Find the variable writer
   // It should be one of the apps listed in the .jpf file
-  private String getWriter(List<StackFrame> sfList) {
+  private String getWriter(List<StackFrame> sfList, HashSet<String> writerSet) {
     // Start looking from the top of the stack backward
     for(int i=sfList.size()-1; i>=0; i--) {
       MethodInfo mi = sfList.get(i).getMethodInfo();
       if(!mi.isJPFInternal()) {
         String method = mi.getStackTraceName();
-        // Check against the apps in the appSet
-        for(String app : appSet) {
-          // There is only one writer at a time but we need to always
-          // check all the potential writers in the list
-          if (method.contains(app)) {
-            return app;
+        // Check against the writers in the writerSet
+        for(String writer : writerSet) {
+          if (method.contains(writer)) {
+            return writer;
           }
         }
       }
@@ -555,7 +546,7 @@ public class ConflictTracker extends ListenerAdapter {
 
     if (conflictFound) {
       StringBuilder sb = new StringBuilder();
-      sb.append("Conflict found between two apps!");
+      sb.append(errorMessage);
       Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sb.toString());
       ti.setNextPC(nextIns);
     } else if (executedInsn instanceof WriteInstruction) {
@@ -566,25 +557,28 @@ public class ConflictTracker extends ListenerAdapter {
           // Get variable info
           byte type  = getType(ti, executedInsn);
           String value = getValue(ti, executedInsn, type);
-          String writer = getWriter(ti.getStack());
+          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;
 
-       // Update the temporary Set set.
-       if (writer.equals("App1"))
-               tempSetSet.setApp1DataList(true, value);
-       else if (writer.equals("App2"))
-               tempSetSet.setApp2DataList(true, value);
-       // Set isSet to 1       
-       isSet = true;
+         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;
                  
        }
-      }
+     }
 
       
-    }
-  
   }
+  
+ }
 
 }