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 d58ed393183d7cec6b4e61192681c25348aece7b..9fb12cf3dac2aa6a4769468a7966edf7a7d5e710 100644 (file)
@@ -41,112 +41,151 @@ 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");
-  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 HashSet<NameValuePair> tempSetSet = new HashSet<NameValuePair>();
+  private long timeout;
+  private long startTime;
+  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 conf, JPF jpf) {
+  public ConflictTracker(Config config, JPF jpf) {
     out = new PrintWriter(System.out, true);
 
-    String[] conflictVars = conf.getStringArray("variables");
+    String[] conflictVars = config.getStringArray("variables");
     // We are not tracking anything if it is null
     if (conflictVars != null) {
       for (String var : conflictVars) {
         conflictSet.add(var);
       }
     }
-    String[] apps = conf.getStringArray("apps");
+    String[] apps = config.getStringArray("apps");
     // We are not tracking anything if it is null
     if (apps != null) {
       for (String var : apps) {
         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();
+  }
+
+  boolean propagateTheChange(Node currentNode) {
+       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;
   }
 
-  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 setOutSet(Node currentNode) {
+       Integer prevSize = currentNode.getOutSet().size();
+
+       // 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 void setInSet(Node currentNode) {
+  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());
-               }
-
-               //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());
-               }
+               currentNode.getInSet().addAll(i.getOutSet());
        }
   }
 
-  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())));
-  }
+  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
 
-  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
+       for (NameValuePair i : currentNode.getSetSet()) {
+               if (i.getIsManual()) // Manual input: we have no conflict
+                       continue;
 
-       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;
+               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;
   }
 
-  class Node {
+  boolean updateSets(Node currentNode) {
+       // Set input set according to output set of pred states of current state
+       setInSet(currentNode);
+
+       // Set outSet according to inSet, and setSet of current node, check if there is a change
+       return setOutSet(currentNode);
+  }
+
+  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;
@@ -160,103 +199,97 @@ public class ConflictTracker extends ListenerAdapter {
                successors.add(node);
        }
 
-       void emptyInSet() {
-               this.inSet = new DataSet(false, "NA", false, "NA");
-       }
-
-       void setInSet(DataSet inSet) {
-               this.inSet.setApp1DataList(inSet.getApp1DataList().getAppSet(), inSet.getApp1DataList().getValue());
-               this.inSet.setApp2DataList(inSet.getApp2DataList().getAppSet(), inSet.getApp2DataList().getValue());
-       }
-
-       void setSetSet(DataSet setSet) {
-               this.setSet.setApp1DataList(setSet.getApp1DataList().getAppSet(), setSet.getApp1DataList().getValue());
-               this.setSet.setApp2DataList(setSet.getApp2DataList().getAppSet(), setSet.getApp2DataList().getValue());
-       }
-
-       void setOutSet(DataSet outSet) {
-               this.outSet.setApp1DataList(outSet.getApp1DataList().getAppSet(), outSet.getApp1DataList().getValue());
-               this.outSet.setApp2DataList(outSet.getApp2DataList().getAppSet(), outSet.getApp2DataList().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();
        }
   }
 
@@ -269,8 +302,12 @@ 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);
+    // Update the parent node
+    if (nodes.containsKey(id)) {
+       parentNode = nodes.get(id);
+    } else {
+       parentNode = new Node(id);
+    }
   }
 
   @Override
@@ -286,18 +323,20 @@ public class ConflictTracker extends ListenerAdapter {
     depth = search.getDepth();
     operation = "forward";
 
-    //Add the node to the list of nodes        
+    // 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";
     }
@@ -309,82 +348,32 @@ public class ConflictTracker extends ListenerAdapter {
 
     out.println("The state is forwarded to state with id: "+id+", depth: "+depth+" which is "+detail+" state: "+"% "+theEnd);
     
-    //Updating the predecessors for this node
-    //Check if parent node is already in successors of the current node or not
+    // Updating the predecessors for this node
+    // Check if parent node is already in successors of the current node or not
     if (!(currentNode.getPredecessors().contains(parentNode)))
        currentNode.addPredecessor(parentNode);
 
-    //Update the successors for this node
-    //Check if current node is already in successors of the parent node or not
+    // Update the successors for this node
+    // Check if current node is already in successors of the parent node or not
     if (!(parentNode.getSuccessors().contains(currentNode)))
         parentNode.addSuccessor(currentNode);
-    
-    
-    //Set the input set of this state to empty
-    currentNode.emptyInSet();
-
-
-    //Store the out set of this state to the temporary data set
-    DataSet tempOutSet = new DataSet(currentNode.getOutSet().getApp1DataList().getAppSet(),
-                                    currentNode.getOutSet().getApp1DataList().getValue(),
-                                    currentNode.getOutSet().getApp2DataList().getAppSet(),
-                                    currentNode.getOutSet().getApp2DataList().getValue());
-
-    
-    //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);
-
-    
-    //Check for a conflict
-    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);
 
-                       //Initialize the empty input set for current node
-                       nodeToProcess.emptyInSet();
+    // Update the sets, check if the outSet is changed or not
+    boolean isChanged = updateSets(currentNode);
+   
+    // Check for a conflict
+    conflictFound = checkForConflict(currentNode);
 
-                       //Store the out set of this state to the temporary data set
-                       tempOutSet = new DataSet(nodeToProcess.getOutSet().getApp1DataList().getAppSet(),
-                                                nodeToProcess.getOutSet().getApp1DataList().getValue(),
-                                                nodeToProcess.getOutSet().getApp2DataList().getAppSet(),
-                                                nodeToProcess.getOutSet().getApp2DataList().getValue());
+    // Check if the outSet of this state has changed, update all of its successors' sets if any
+    if (isChanged)
+       conflictFound = conflictFound || propagateTheChange(currentNode);
 
-                               
-                       //Set input set according to output set of pred states of current state
-                       setInSet(nodeToProcess);
-
-                       
-                       //Set outSet to setSet if it is valid, otherwise to inSet
-                       setOutSet(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);
-                               }
-                       }
-               }
+    // Update the parent node
+    if (nodes.containsKey(id)) {
+       parentNode = nodes.get(id);
+    } else {
+       parentNode = new Node(id);
     }
-
-    //Update the parent node
-    parentNode = new Node(id);
-       
   }
 
   @Override
@@ -396,8 +385,12 @@ 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);
+    // Update the parent node
+    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;
           }
         }
       }
@@ -543,9 +534,19 @@ public class ConflictTracker extends ListenerAdapter {
 
   @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();
+        sbTimeOut.append("Execution timeout: " + (timeout / (60 * 1000)) + " minutes have passed!");
+        Instruction nextIns = ti.createAndThrowException("java.lang.RuntimeException", sbTimeOut.toString());
+        ti.setNextPC(nextIns);
+      }
+    }
+
     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) {
@@ -556,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;
                  
        }
-      }
+     }
 
       
-    }
-  
   }
+  
+ }
 
 }