Fixing a bug: completing reachability graph with missing past traces.
[jpf-core.git] / src / main / gov / nasa / jpf / listener / DPORStateReducer.java
index 362da3b247b00d42aa2d43ad10f155a8103edf87..885add3e489bf49126c388171fc51811bad1d4af 100644 (file)
@@ -28,8 +28,11 @@ import gov.nasa.jpf.vm.bytecode.WriteInstruction;
 import gov.nasa.jpf.vm.choice.IntChoiceFromSet;
 import gov.nasa.jpf.vm.choice.IntIntervalGenerator;
 
+import java.io.FileWriter;
 import java.io.PrintWriter;
 import java.util.*;
+import java.util.logging.Logger;
+import java.io.IOException;
 
 // TODO: Fix for Groovy's model-checking
 // TODO: This is a setter to change the values of the ChoiceGenerator to implement POR
@@ -39,12 +42,13 @@ import java.util.*;
  * This DPOR implementation is augmented by the algorithm presented in this SPIN paper:
  * http://spinroot.com/spin/symposia/ws08/spin2008_submission_33.pdf
  *
- * The algorithm is presented on page 11 of the paper. Basically, we create a graph G
- * (i.e., visible operation dependency graph)
- * that maps inter-related threads/sub-programs that trigger state changes.
- * The key to this approach is that we evaluate graph G in every iteration/recursion to
- * only update the backtrack sets of the threads/sub-programs that are reachable in graph G
- * from the currently running thread/sub-program.
+ * The algorithm is presented on page 11 of the paper. Basically, we have a graph G
+ * (i.e., visible operation dependency graph).
+ * This DPOR implementation actually fixes the algorithm in the SPIN paper that does not
+ * consider cases where a state could be matched early. In this new algorithm/implementation,
+ * each run is terminated iff:
+ * - we find a state that matches a state in a previous run, or
+ * - we have a matched state in the current run that consists of cycles that contain all choices/events.
  */
 public class DPORStateReducer extends ListenerAdapter {
 
@@ -52,6 +56,7 @@ public class DPORStateReducer extends ListenerAdapter {
   private boolean verboseMode;
   private boolean stateReductionMode;
   private final PrintWriter out;
+  private PrintWriter fileWriter;
   private String detail;
   private int depth;
   private int id;
@@ -60,7 +65,7 @@ public class DPORStateReducer extends ListenerAdapter {
   // DPOR-related fields
   // Basic information
   private Integer[] choices;
-  private Integer[] refChoices;
+  private Integer[] refChoices; // Second reference to a copy of choices (choices may be modified for fair scheduling)
   private int choiceCounter;
   private int maxEventChoice;
   // Data structure to track the events seen by each state to track cycles (containing all events) for termination
@@ -69,23 +74,24 @@ public class DPORStateReducer extends ListenerAdapter {
   private HashSet<Integer> prevVisitedStates; // States visited in the previous execution
   private HashMap<Integer, HashSet<Integer>> stateToEventMap;
   // Data structure to analyze field Read/Write accesses and conflicts
-  private HashMap<Integer, LinkedList<Integer[]>> backtrackMap;       // Track created backtracking points
-  private PriorityQueue<Integer> backtrackStateQ;                     // Heap that returns the latest state
-  private ArrayList<IntChoiceFromSet> cgList;                         // Record CGs for backtracking points
-  private HashMap<Integer, IntChoiceFromSet> cgMap;                   // Maps state IDs to CGs
-  private HashMap<Integer, HashSet<Integer>> conflictPairMap;         // Record conflicting events
-//  private HashSet<IntChoiceFromSet> activeBacktrackCGs;               // Record active backtrack CGs
-  private HashMap<Integer, ReadWriteSet> readWriteFieldsMap;          // Record fields that are accessed
-
-  // Visible operation dependency graph implementation (SPIN paper) related fields
-  private int prevChoiceValue;
-  private HashMap<Integer, HashSet<Integer>> vodGraphMap; // Visible operation dependency graph (VOD graph)
+  private HashMap<Integer, LinkedList<Integer[]>> backtrackMap;   // Track created backtracking points
+  private PriorityQueue<Integer> backtrackStateQ;                 // Heap that returns the latest state
+  private ArrayList<BacktrackPoint> backtrackPointList;           // Record backtrack points (CG, state Id, and choice)
+  private HashMap<Integer, HashSet<Integer>> conflictPairMap;     // Record conflicting events
+  private HashSet<String> doneBacktrackSet;                       // Record state ID and trace already constructed
+  private HashMap<Integer, ReadWriteSet> readWriteFieldsMap;      // Record fields that are accessed
+  private HashMap<Integer, RestorableVMState> restorableStateMap; // Maps state IDs to the restorable state object
+  private HashMap<Integer, Integer> stateToChoiceCounterMap;      // Maps state IDs to the choice counter
+  private HashMap<Integer, ArrayList<ReachableTrace>> rGraph;     // Create a reachability graph
 
   // Boolean states
   private boolean isBooleanCGFlipped;
-  private boolean isFirstResetDone;
   private boolean isEndOfExecution;
 
+  // Statistics
+  private int numOfConflicts;
+  private int numOfTransitions;
+       
   public DPORStateReducer(Config config, JPF jpf) {
     verboseMode = config.getBoolean("printout_state_transition", false);
     stateReductionMode = config.getBoolean("activate_state_reduction", true);
@@ -94,31 +100,18 @@ public class DPORStateReducer extends ListenerAdapter {
     } else {
       out = null;
     }
-    // DPOR-related
-    choices = null;
-    refChoices = null;
-    choiceCounter = 0;
-    maxEventChoice = 0;
-    // Cycle tracking
-    currVisitedStates = new HashSet<>();
-    justVisitedStates = new HashSet<>();
-    prevVisitedStates = new HashSet<>();
-    stateToEventMap = new HashMap<>();
-    // Backtracking
-    backtrackMap = new HashMap<>();
-    backtrackStateQ = new PriorityQueue<>();
-    cgList = new ArrayList<>();
-    cgMap = new HashMap<>();
-    conflictPairMap = new HashMap<>();
-//    activeBacktrackCGs = new HashSet<>();
-    readWriteFieldsMap = new HashMap<>();
-    // VOD graph
-    prevChoiceValue = -1;
-    vodGraphMap = new HashMap<>();
-    // Booleans
+    String outputFile = config.getString("file_output");
+    if (!outputFile.isEmpty()) {
+      try {
+        fileWriter = new PrintWriter(new FileWriter(outputFile, true), true);
+      } catch (IOException e) {
+      }
+    }
     isBooleanCGFlipped = false;
-    isEndOfExecution = false;
-    isFirstResetDone = false;
+               numOfConflicts = 0;
+               numOfTransitions = 0;
+    restorableStateMap = new HashMap<>();
+    initializeStatesVariables();
   }
 
   @Override
@@ -180,10 +173,26 @@ public class DPORStateReducer extends ListenerAdapter {
     }
   }
 
+  static Logger log = JPF.getLogger("report");
+
   @Override
   public void searchFinished(Search search) {
+    if (stateReductionMode) {
+      // Number of conflicts = first trace + subsequent backtrack points
+      numOfConflicts += 1 + doneBacktrackSet.size();
+    }
     if (verboseMode) {
+      out.println("\n==> DEBUG: ----------------------------------- search finished");
+      out.println("\n==> DEBUG: State reduction mode  : " + stateReductionMode);
+      out.println("\n==> DEBUG: Number of conflicts   : " + numOfConflicts);
+      out.println("\n==> DEBUG: Number of transitions : " + numOfTransitions);
       out.println("\n==> DEBUG: ----------------------------------- search finished" + "\n");
+
+      fileWriter.println("==> DEBUG: State reduction mode  : " + stateReductionMode);
+      fileWriter.println("==> DEBUG: Number of conflicts   : " + numOfConflicts);
+      fileWriter.println("==> DEBUG: Number of transitions : " + numOfTransitions);
+      fileWriter.println();
+      fileWriter.close();
     }
   }
 
@@ -209,8 +218,6 @@ public class DPORStateReducer extends ListenerAdapter {
           // Use a modulo since choiceCounter is going to keep increasing
           int choiceIndex = choiceCounter % choices.length;
           icsCG.advance(choices[choiceIndex]);
-          // Index the ChoiceGenerator to set backtracking points
-          cgList.add(icsCG);
         } else {
           // Set done all CGs while transitioning to a new execution
           icsCG.setDone();
@@ -224,31 +231,38 @@ public class DPORStateReducer extends ListenerAdapter {
 
     if (stateReductionMode) {
       // Check the boolean CG and if it is flipped, we are resetting the analysis
-//      if (currentCG instanceof BooleanChoiceGenerator) {
-//        if (!isBooleanCGFlipped) {
-//          isBooleanCGFlipped = true;
-//        } else {
-//          initializeStateReduction();
-//        }
-//      }
+      if (currentCG instanceof BooleanChoiceGenerator) {
+        if (!isBooleanCGFlipped) {
+          isBooleanCGFlipped = true;
+        } else {
+          // Number of conflicts = first trace + subsequent backtrack points
+          numOfConflicts = 1 + doneBacktrackSet.size();
+          // Allocate new objects for data structure when the boolean is flipped from "false" to "true"
+          initializeStatesVariables();
+        }
+      }
       // Check every choice generated and ensure fair scheduling!
       if (currentCG instanceof IntChoiceFromSet) {
         IntChoiceFromSet icsCG = (IntChoiceFromSet) currentCG;
         // If this is a new CG then we need to update data structures
-        resetStatesForNewExecution(icsCG);
+        resetStatesForNewExecution(icsCG, vm);
         // If we don't see a fair scheduling of events/choices then we have to enforce it
-        checkAndEnforceFairScheduling(icsCG);
+        fairSchedulingAndBacktrackPoint(icsCG, vm);
+        // Explore the next backtrack point: 
+        // 1) if we have seen this state or this state contains cycles that involve all events, and
+        // 2) after the current CG is advanced at least once
+        if (terminateCurrentExecution() && choiceCounter > 0) {
+          exploreNextBacktrackPoints(vm, icsCG);
+        } else {
+          numOfTransitions++;
+        }
         // Map state to event
         mapStateToEvent(icsCG.getNextChoice());
-        // Update the VOD graph always with the latest
-        updateVODGraph(icsCG.getNextChoice());
-        // Check if we have seen this state or this state contains cycles that involve all events
-        if (terminateCurrentExecution()) {
-          exploreNextBacktrackPoints(icsCG, vm);
-        }
         justVisitedStates.clear();
         choiceCounter++;
       }
+    } else {
+      numOfTransitions++;
     }
   }
 
@@ -288,10 +302,7 @@ public class DPORStateReducer extends ListenerAdapter {
                   // Check and record a backtrack set for just once!
                   if (isConflictFound(nextInsn, eventCounter, currentChoice, fieldClass) &&
                       isNewConflict(currentChoice, eventCounter)) {
-                    // Lines 4-8 of the algorithm in the paper page 11 (see the heading note above)
-                    if (vm.isNewState() || isReachableInVODGraph(currentChoice)) {
-                      createBacktrackingPoint(currentChoice, eventCounter);
-                    }
+                    createBacktrackingPoint(currentChoice, eventCounter, false);
                   }
                 }
               }
@@ -327,6 +338,14 @@ public class DPORStateReducer extends ListenerAdapter {
       writeSet.put(field, objectId);
     }
 
+    public Set<String> getReadSet() {
+      return readSet.keySet();
+    }
+
+    public Set<String> getWriteSet() {
+      return writeSet.keySet();
+    }
+
     public boolean readFieldExists(String field) {
       return readSet.containsKey(field);
     }
@@ -344,22 +363,46 @@ public class DPORStateReducer extends ListenerAdapter {
     }
   }
 
-  // This class compactly stores backtracking points: 1) backtracking ChoiceGenerator, and 2) backtracking choices
+  // This class compactly stores backtrack points: 1) backtrack state ID, and 2) backtracking choices
   private class BacktrackPoint {
-    private IntChoiceFromSet backtrackCG; // CG to backtrack from
-    private Integer[] backtrackChoices;   // Choices to set for this backtrack CG
+    private IntChoiceFromSet backtrackCG; // CG at this backtrack point
+    private int stateId;                  // State at this backtrack point
+    private int choice;                   // Choice chosen at this backtrack point
 
-    public BacktrackPoint(IntChoiceFromSet cg, Integer[] choices) {
+    public BacktrackPoint(IntChoiceFromSet cg, int stId, int cho) {
       backtrackCG = cg;
-      backtrackChoices = choices;
+      stateId = stId;
+      choice = cho;
+    }
+
+    public IntChoiceFromSet getBacktrackCG() { return backtrackCG; }
+
+    public int getStateId() {
+      return stateId;
+    }
+
+    public int getChoice() {
+      return choice;
     }
+  }
+
+  // This class stores a compact representation of a reachability graph for past executions
+  private class ReachableTrace {
+    private ArrayList<BacktrackPoint> pastBacktrackPointList;
+    private HashMap<Integer, ReadWriteSet> pastReadWriteFieldsMap;
 
-    public IntChoiceFromSet getBacktrackCG() {
-      return backtrackCG;
+    public ReachableTrace(ArrayList<BacktrackPoint> btrackPointList,
+                             HashMap<Integer, ReadWriteSet> rwFieldsMap) {
+      pastBacktrackPointList = btrackPointList;
+      pastReadWriteFieldsMap = rwFieldsMap;
     }
 
-    public Integer[] getBacktrackChoices() {
-      return backtrackChoices;
+    public ArrayList<BacktrackPoint> getPastBacktrackPointList() {
+      return pastBacktrackPointList;
+    }
+
+    public HashMap<Integer, ReadWriteSet> getPastReadWriteFieldsMap() {
+      return pastReadWriteFieldsMap;
     }
   }
 
@@ -384,7 +427,7 @@ public class DPORStateReducer extends ListenerAdapter {
   private final static String JAVA_STRING_LIB = "java.lang.String";
 
   // -- FUNCTIONS
-  private void checkAndEnforceFairScheduling(IntChoiceFromSet icsCG) {
+  private void fairSchedulingAndBacktrackPoint(IntChoiceFromSet icsCG, VM vm) {
     // Check the next choice and if the value is not the same as the expected then force the expected value
     int choiceIndex = choiceCounter % refChoices.length;
     int nextChoice = icsCG.getNextChoice();
@@ -395,6 +438,12 @@ public class DPORStateReducer extends ListenerAdapter {
         icsCG.setChoice(currCGIndex, expectedChoice);
       }
     }
+    // Record state ID and choice/event as backtrack point
+    int stateId = vm.getStateId();
+    backtrackPointList.add(new BacktrackPoint(icsCG, stateId, refChoices[choiceIndex]));
+    // Store restorable state object for this state (always store the latest)
+    RestorableVMState restorableState = vm.getRestorableState();
+    restorableStateMap.put(stateId, restorableState);
   }
 
   private Integer[] copyChoices(Integer[] choicesToCopy) {
@@ -429,6 +478,30 @@ public class DPORStateReducer extends ListenerAdapter {
     return true;
   }
 
+  private void initializeStatesVariables() {
+    // DPOR-related
+    choices = null;
+    refChoices = null;
+    choiceCounter = 0;
+    maxEventChoice = 0;
+    // Cycle tracking
+    currVisitedStates = new HashSet<>();
+    justVisitedStates = new HashSet<>();
+    prevVisitedStates = new HashSet<>();
+    stateToEventMap = new HashMap<>();
+    // Backtracking
+    backtrackMap = new HashMap<>();
+    backtrackStateQ = new PriorityQueue<>(Collections.reverseOrder());
+    backtrackPointList = new ArrayList<>();
+    conflictPairMap = new HashMap<>();
+    doneBacktrackSet = new HashSet<>();
+    readWriteFieldsMap = new HashMap<>();
+    stateToChoiceCounterMap = new HashMap<>();
+    rGraph = new HashMap<>();
+    // Booleans
+    isEndOfExecution = false;
+  }
+
   private void mapStateToEvent(int nextChoiceValue) {
     // Update all states with this event/choice
     // This means that all past states now see this transition
@@ -454,31 +527,45 @@ public class DPORStateReducer extends ListenerAdapter {
     // Update the state variables
     // Line 19 in the paper page 11 (see the heading note above)
     int stateId = search.getStateId();
-    currVisitedStates.add(stateId);
     // Insert state ID into the map if it is new
     if (!stateToEventMap.containsKey(stateId)) {
       HashSet<Integer> eventSet = new HashSet<>();
       stateToEventMap.put(stateId, eventSet);
     }
+    // Save execution state into the Reachability only if
+    // (1) It is not a revisited state from a past execution, or
+    // (2) It is just a new backtracking point
+    if (!prevVisitedStates.contains(stateId) ||
+            choiceCounter <= 1) {
+      ReachableTrace reachableTrace= new
+              ReachableTrace(backtrackPointList, readWriteFieldsMap);
+      ArrayList<ReachableTrace> rTrace;
+      if (!prevVisitedStates.contains(stateId)) {
+        rTrace = new ArrayList<>();
+        rGraph.put(stateId, rTrace);
+      } else {
+        rTrace = rGraph.get(stateId);
+      }
+      rTrace.add(reachableTrace);
+    }
+    stateToChoiceCounterMap.put(stateId, choiceCounter);
+    analyzeReachabilityAndCreateBacktrackPoints(search.getVM(), stateId);
     justVisitedStates.add(stateId);
+    currVisitedStates.add(stateId);
   }
 
   // --- Functions related to Read/Write access analysis on shared fields
 
-  private void addNewBacktrackPoint(IntChoiceFromSet backtrackCG, Integer[] newChoiceList) {
-    int stateId = backtrackCG.getStateId();
+  private void addNewBacktrackPoint(int stateId, Integer[] newChoiceList) {
     // Insert backtrack point to the right state ID
     LinkedList<Integer[]> backtrackList;
     if (backtrackMap.containsKey(stateId)) {
       backtrackList = backtrackMap.get(stateId);
     } else {
       backtrackList = new LinkedList<>();
+      backtrackMap.put(stateId, backtrackList);
     }
     backtrackList.addFirst(newChoiceList);
-    // Add CG for this state ID if there isn't one yet
-    if (!cgMap.containsKey(stateId)) {
-      cgMap.put(stateId, backtrackCG);
-    }
     // Add to priority queue
     if (!backtrackStateQ.contains(stateId)) {
       backtrackStateQ.add(stateId);
@@ -549,24 +636,47 @@ public class DPORStateReducer extends ListenerAdapter {
     // If current choice is not the same, then this is caused by the firing of IntIntervalGenerator
     // for certain method calls in the infrastructure, e.g., eventSince()
     int currChoiceInd = currentChoice % refChoices.length;
-    int currChoiceFromCG = getCurrentChoice(vm);
+    int currChoiceFromCG = currChoiceInd;
+    ChoiceGenerator<?> currentCG = vm.getChoiceGenerator();
+    // This is the main event CG
+    if (currentCG instanceof IntIntervalGenerator) {
+      // This is the interval CG used in device handlers
+      ChoiceGenerator<?> parentCG = ((IntIntervalGenerator) currentCG).getPreviousChoiceGenerator();
+      // Iterate until we find the IntChoiceFromSet CG
+      while (!(parentCG instanceof IntChoiceFromSet)) {
+        parentCG = ((IntIntervalGenerator) parentCG).getPreviousChoiceGenerator();
+      }
+      int actualEvtNum = ((IntChoiceFromSet) parentCG).getNextChoice();
+      // Find the index of the event/choice in refChoices
+      for (int i = 0; i<refChoices.length; i++) {
+        if (actualEvtNum == refChoices[i]) {
+          currChoiceFromCG = i;
+          break;
+        }
+      }
+    }
     if (currChoiceInd != currChoiceFromCG) {
       currentChoice = (currentChoice - currChoiceInd) + currChoiceFromCG;
     }
     return currentChoice;
   }
 
-  private void createBacktrackingPoint(int currentChoice, int confEvtNum) {
+  private void createBacktrackingPoint(int currentChoice, int confEvtNum, boolean isPastTrace) {
 
     // Create a new list of choices for backtrack based on the current choice and conflicting event number
     // E.g. if we have a conflict between 1 and 3, then we create the list {3, 1, 0, 2}
     // for the original set {0, 1, 2, 3}
     Integer[] newChoiceList = new Integer[refChoices.length];
     // Put the conflicting event numbers first and reverse the order
-    int actualCurrCho = currentChoice % refChoices.length;
-    int actualConfEvtNum = confEvtNum % refChoices.length;
-    newChoiceList[0] = refChoices[actualCurrCho];
-    newChoiceList[1] = refChoices[actualConfEvtNum];
+    if (isPastTrace) {
+      // For past trace we get the choice/event from the list
+      newChoiceList[0] = backtrackPointList.get(currentChoice).getChoice();
+    } else {
+      // We use the actual choices here in case they have been modified/adjusted by the fair scheduling method
+      int actualCurrCho = currentChoice % refChoices.length;
+      newChoiceList[0] = choices[actualCurrCho];
+    }
+    newChoiceList[1] = backtrackPointList.get(confEvtNum).getChoice();
     // Put the rest of the event numbers into the array starting from the minimum to the upper bound
     for (int i = 0, j = 2; i < refChoices.length; i++) {
       if (refChoices[i] != newChoiceList[0] && refChoices[i] != newChoiceList[1]) {
@@ -574,10 +684,13 @@ public class DPORStateReducer extends ListenerAdapter {
         j++;
       }
     }
-    // Record the backtracking point in the stack as well
-    IntChoiceFromSet backtrackCG = cgList.get(confEvtNum);
-    //BacktrackPoint backtrackPoint = new BacktrackPoint(backtrackCG, newChoiceList);
-    addNewBacktrackPoint(backtrackCG, newChoiceList);
+    // Get the backtrack CG for this backtrack point
+    int stateId = backtrackPointList.get(confEvtNum).getStateId();
+    // Check if this trace has been done starting from this state
+    if (isTraceAlreadyConstructed(newChoiceList, stateId)) {
+      return;
+    }
+    addNewBacktrackPoint(stateId, newChoiceList);
   }
 
   private boolean excludeThisForItContains(String[] excludedStrings, String className) {
@@ -607,32 +720,34 @@ public class DPORStateReducer extends ListenerAdapter {
     return false;
   }
 
-  private void exploreNextBacktrackPoints(IntChoiceFromSet icsCG, VM vm) {
-    // We can start exploring the next backtrack point after the current CG is advanced at least once
-    if (icsCG.getNextChoiceIndex() > 0) {
-      if (backtrackMap.isEmpty()) {
-        // This means we are reaching the end of our execution: no more backtracking points to explore
-        return;
-      }
-      setNextBacktrackPoint(icsCG);
-      // Save all the visited states when starting a new execution of trace
-      prevVisitedStates.addAll(currVisitedStates);
-      currVisitedStates.clear();
-      // This marks a transitional period to the new CG
-      isEndOfExecution = true;
-    }
-  }
-
-  private int getCurrentChoice(VM vm) {
-    ChoiceGenerator<?> currentCG = vm.getChoiceGenerator();
-    // This is the main event CG
-    if (currentCG instanceof IntChoiceFromSet) {
-      return ((IntChoiceFromSet) currentCG).getNextChoiceIndex();
-    } else {
-      // This is the interval CG used in device handlers
-      ChoiceGenerator<?> parentCG = ((IntIntervalGenerator) currentCG).getPreviousChoiceGenerator();
-      return ((IntChoiceFromSet) parentCG).getNextChoiceIndex();
-    }
+  private void exploreNextBacktrackPoints(VM vm, IntChoiceFromSet icsCG) {
+
+               // Check if we are reaching the end of our execution: no more backtracking points to explore
+               // cgMap, backtrackMap, backtrackStateQ are updated simultaneously (checking backtrackStateQ is enough)
+               if (!backtrackStateQ.isEmpty()) {
+                       // Set done all the other backtrack points
+                       for (BacktrackPoint backtrackPoint : backtrackPointList) {
+                               backtrackPoint.getBacktrackCG().setDone();
+                       }
+                       // Reset the next backtrack point with the latest state
+                       int hiStateId = backtrackStateQ.peek();
+                       // Restore the state first if necessary
+                       if (vm.getStateId() != hiStateId) {
+                               RestorableVMState restorableState = restorableStateMap.get(hiStateId);
+                               vm.restoreState(restorableState);
+                       }
+                       // Set the backtrack CG
+                       IntChoiceFromSet backtrackCG = (IntChoiceFromSet) vm.getChoiceGenerator();
+                       setBacktrackCG(hiStateId, backtrackCG);
+               } else {
+                       // Set done this last CG (we save a few rounds)
+                       icsCG.setDone();
+               }
+               // Save all the visited states when starting a new execution of trace
+               prevVisitedStates.addAll(currVisitedStates);
+               currVisitedStates.clear();
+               // This marks a transitional period to the new CG
+               isEndOfExecution = true;
   }
 
   private ReadWriteSet getReadWriteSet(int currentChoice) {
@@ -648,11 +763,51 @@ public class DPORStateReducer extends ListenerAdapter {
     return rwSet;
   }
 
+  private boolean isConflictFound(int eventCounter, int currentChoice, boolean isPastTrace) {
+
+    int currActualChoice;
+    if (isPastTrace) {
+      currActualChoice = backtrackPointList.get(currentChoice).getChoice();
+    } else {
+      int actualCurrCho = currentChoice % refChoices.length;
+      currActualChoice = choices[actualCurrCho];
+    }
+    // Skip if this event does not have any Read/Write set or the two events are basically the same event (number)
+    if (!readWriteFieldsMap.containsKey(eventCounter) ||
+            currActualChoice == backtrackPointList.get(eventCounter).getChoice()) {
+      return false;
+    }
+    // Current R/W set
+    ReadWriteSet currRWSet = readWriteFieldsMap.get(currentChoice);
+    // R/W set of choice/event that may have a potential conflict
+    ReadWriteSet evtRWSet = readWriteFieldsMap.get(eventCounter);
+    // Check for conflicts with Read and Write fields for Write instructions
+    Set<String> currWriteSet = currRWSet.getWriteSet();
+    for(String writeField : currWriteSet) {
+      int currObjId = currRWSet.writeFieldObjectId(writeField);
+      if ((evtRWSet.readFieldExists(writeField) && evtRWSet.readFieldObjectId(writeField) == currObjId) ||
+          (evtRWSet.writeFieldExists(writeField) && evtRWSet.writeFieldObjectId(writeField) == currObjId)) {
+        return true;
+      }
+    }
+    // Check for conflicts with Write fields for Read instructions
+    Set<String> currReadSet = currRWSet.getReadSet();
+    for(String readField : currReadSet) {
+      int currObjId = currRWSet.readFieldObjectId(readField);
+      if (evtRWSet.writeFieldExists(readField) && evtRWSet.writeFieldObjectId(readField) == currObjId) {
+        return true;
+      }
+    }
+    // Return false if no conflict is found
+    return false;
+  }
+
   private boolean isConflictFound(Instruction nextInsn, int eventCounter, int currentChoice, String fieldClass) {
-    int actualEvtCntr = eventCounter % refChoices.length;
+
     int actualCurrCho = currentChoice % refChoices.length;
     // Skip if this event does not have any Read/Write set or the two events are basically the same event (number)
-    if (!readWriteFieldsMap.containsKey(eventCounter) || (actualEvtCntr == actualCurrCho)) {
+    if (!readWriteFieldsMap.containsKey(eventCounter) ||
+         choices[actualCurrCho] == backtrackPointList.get(eventCounter).getChoice()) {
       return false;
     }
     ReadWriteSet rwSet = readWriteFieldsMap.get(eventCounter);
@@ -696,128 +851,186 @@ public class DPORStateReducer extends ListenerAdapter {
     return true;
   }
 
-  private void resetStatesForNewExecution(IntChoiceFromSet icsCG) {
+  private boolean isTraceAlreadyConstructed(Integer[] choiceList, int stateId) {
+    // Concatenate state ID and only the first event in the string, e.g., "1:1 for the trace 10234 at state 1"
+    // TODO: THIS IS AN OPTIMIZATION!
+    // This is the optimized version because after we execute, e.g., the trace 1:10234, we don't need to try
+    // another trace that starts with event 1 at state 1, e.g., the trace 1:13024
+    // The second time this event 1 is explored, it will generate the same state as the first one
+    StringBuilder sb = new StringBuilder();
+    sb.append(stateId);
+    sb.append(':');
+    sb.append(choiceList[0]);
+    // Check if the trace has been constructed as a backtrack point for this state
+    if (doneBacktrackSet.contains(sb.toString())) {
+      return true;
+    }
+    doneBacktrackSet.add(sb.toString());
+    return false;
+  }
+
+  private void resetStatesForNewExecution(IntChoiceFromSet icsCG, VM vm) {
     if (choices == null || choices != icsCG.getAllChoices()) {
       // Reset state variables
-      choiceCounter = 1;
+      choiceCounter = 0;
       choices = icsCG.getAllChoices();
       refChoices = copyChoices(choices);
-      // Clearing data structures
-      backtrackMap.clear();
-      conflictPairMap.clear();
-      readWriteFieldsMap.clear();
-      stateToEventMap.clear();
+      // Clear data structures
+      backtrackPointList = new ArrayList<>();
+      conflictPairMap = new HashMap<>();
+      readWriteFieldsMap = new HashMap<>();
+      stateToChoiceCounterMap = new HashMap<>();
+      stateToEventMap = new HashMap<>();
       isEndOfExecution = false;
-                       // Adding this CG as the first CG for this execution
-                       cgList.add(icsCG);
     }
   }
 
-  private IntChoiceFromSet setBacktrackCG(int stateId) {
+  private void setBacktrackCG(int stateId, IntChoiceFromSet backtrackCG) {
     // Set a backtrack CG based on a state ID
-    IntChoiceFromSet backtrackCG = cgMap.get(stateId);
     LinkedList<Integer[]> backtrackChoices = backtrackMap.get(stateId);
     backtrackCG.setNewValues(backtrackChoices.removeLast());  // Get the last from the queue
+    backtrackCG.setStateId(stateId);
     backtrackCG.reset();
     // Remove from the queue if we don't have more backtrack points for that state
     if (backtrackChoices.isEmpty()) {
-      cgMap.remove(stateId);
       backtrackMap.remove(stateId);
       backtrackStateQ.remove(stateId);
     }
-    return backtrackCG;
   }
 
-  private void setNextBacktrackPoint(IntChoiceFromSet icsCG) {
-
-    HashSet<IntChoiceFromSet> backtrackCGs = new HashSet<>(cgMap.values());
-    if (!isFirstResetDone) {
-      // Reset the last CG of every LinkedList in the map and set done everything else
-      for (Integer stateId : cgMap.keySet()) {
-        setBacktrackCG(stateId);
+  // --- Functions related to the reachability analysis when there is a state match
+
+  // We use backtrackPointsList to analyze the reachable states/events when there is a state match:
+  // 1) Whenever there is state match, there is a cycle of events
+  // 2) We need to analyze and find conflicts for the reachable choices/events in the cycle
+  // 3) Then we create a new backtrack point for every new conflict
+  private void analyzeReachabilityAndCreateBacktrackPoints(VM vm, int stateId) {
+    // Perform this analysis only when:
+    // 1) there is a state match,
+    // 2) this is not during a switch to a new execution,
+    // 3) at least 2 choices/events have been explored (choiceCounter > 1),
+    // 4) the matched state has been encountered in the current execution, and
+    // 5) state > 0 (state 0 is for boolean CG)
+    if (!vm.isNewState() && !isEndOfExecution && choiceCounter > 1 && (stateId > 0)) {
+      if (currVisitedStates.contains(stateId)) {
+        // Update the backtrack sets in the cycle
+        updateBacktrackSetsInCycle(stateId);
+      } else if (prevVisitedStates.contains(stateId)) { // We visit a state in a previous execution
+        // Update the backtrack sets in a previous execution
+        updateBacktrackSetsInPreviousExecution(stateId);
       }
-//      activeBacktrackCGs.addAll(cgMap.values());
-      isFirstResetDone = true;
-    } else {
-      // Check if we still have backtrack points for the current CG
-      int currStateId = icsCG.getStateId();
-      if (backtrackMap.containsKey(currStateId)) {
-        setBacktrackCG(currStateId);
-      } else {
-//        activeBacktrackCGs.remove(icsCG);
-        // We try to reset new CGs (if we do have) when we are running out of active CGs
-        if (!backtrackStateQ.isEmpty()) {
-          // Reset the next CG with the latest state
-          int hiStateId = backtrackStateQ.peek();
-          IntChoiceFromSet backtrackCG = setBacktrackCG(hiStateId);
-//          activeBacktrackCGs.add(backtrackCG);
-        }
+    }
+  }
+
+  // Get the start event for the past execution trace when there is a state matched from a past execution
+  private int getPastConflictChoice(int stateId, ArrayList<BacktrackPoint> pastBacktrackPointList) {
+    // Iterate and find the first occurrence of the state ID
+    // It is guaranteed that a choice should be found because the state ID is in the list
+    int pastConfChoice = 0;
+    for(int i = 0; i<pastBacktrackPointList.size(); i++) {
+      BacktrackPoint backtrackPoint = pastBacktrackPointList.get(i);
+      int stId = backtrackPoint.getStateId();
+      if (stId == stateId) {
+        pastConfChoice = i;
+        break;
       }
     }
-    // Clear unused CGs
-    for(IntChoiceFromSet cg : cgList) {
-      if (!backtrackCGs.contains(cg)) {
-        cg.setDone();
+    return pastConfChoice;
+  }
+
+  // Get a sorted list of reachable state IDs starting from the input stateId
+  private ArrayList<Integer> getReachableStateIds(Set<Integer> stateIds, int stateId) {
+    // Only include state IDs equal or greater than the input stateId: these are reachable states
+    ArrayList<Integer> sortedStateIds = new ArrayList<>();
+    for(Integer stId : stateIds) {
+      if (stId >= stateId) {
+        sortedStateIds.add(stId);
       }
     }
-    cgList.clear();
-  }
-
-  // --- Functions related to the visible operation dependency graph implementation discussed in the SPIN paper
-
-  // This method checks whether a choice is reachable in the VOD graph from a reference choice (BFS algorithm)
-  //private boolean isReachableInVODGraph(int checkedChoice, int referenceChoice) {
-  private boolean isReachableInVODGraph(int currentChoice) {
-    // Extract previous and current events
-    int choiceIndex = currentChoice % refChoices.length;
-    int currEvent = refChoices[choiceIndex];
-    int prevEvent = refChoices[choiceIndex - 1];
-    // Record visited choices as we search in the graph
-    HashSet<Integer> visitedChoice = new HashSet<>();
-    visitedChoice.add(prevEvent);
-    LinkedList<Integer> nodesToVisit = new LinkedList<>();
-    // If the state doesn't advance as the threads/sub-programs are executed (basically there is no new state),
-    // there is a chance that the graph doesn't have new nodes---thus this check will return a null.
-    if (vodGraphMap.containsKey(prevEvent)) {
-      nodesToVisit.addAll(vodGraphMap.get(prevEvent));
-      while(!nodesToVisit.isEmpty()) {
-        int choice = nodesToVisit.getFirst();
-        if (choice == currEvent) {
-          return true;
-        }
-        if (visitedChoice.contains(choice)) { // If there is a loop then we don't find it
-          return false;
-        }
-        // Continue searching
-        visitedChoice.add(choice);
-        HashSet<Integer> choiceNextNodes = vodGraphMap.get(choice);
-        if (choiceNextNodes != null) {
-          // Add only if there is a mapping for next nodes
-          for (Integer nextNode : choiceNextNodes) {
-            // Skip cycles
-            if (nextNode == choice) {
-              continue;
-            }
-            nodesToVisit.addLast(nextNode);
-          }
+    Collections.sort(sortedStateIds);
+    return sortedStateIds;
+  }
+
+  // Update the backtrack sets in the cycle
+  private void updateBacktrackSetsInCycle(int stateId) {
+    // Find the choice/event that marks the start of this cycle: first choice we explore for conflicts
+    int conflictChoice = stateToChoiceCounterMap.get(stateId);
+    int currentChoice = choiceCounter - 1;
+    // Find conflicts between choices/events in this cycle (we scan forward in the cycle, not backward)
+    while (conflictChoice < currentChoice) {
+      for (int eventCounter = conflictChoice + 1; eventCounter <= currentChoice; eventCounter++) {
+        if (isConflictFound(eventCounter, conflictChoice, false) && isNewConflict(conflictChoice, eventCounter)) {
+          createBacktrackingPoint(conflictChoice, eventCounter, false);
         }
       }
+      conflictChoice++;
     }
-    return false;
   }
 
-  private void updateVODGraph(int currChoiceValue) {
-    // Update the graph when we have the current choice value
+  // TODO: OPTIMIZATION!
+  // Check and make sure that state ID and choice haven't been explored for this trace
+  private boolean isNotChecked(HashMap<Integer, HashSet<Integer>> checkedStateIdAndChoice,
+                               BacktrackPoint backtrackPoint) {
+    int stateId = backtrackPoint.getStateId();
+    int choice = backtrackPoint.getChoice();
     HashSet<Integer> choiceSet;
-    if (vodGraphMap.containsKey(prevChoiceValue)) {
-      // If the key already exists, just retrieve it
-      choiceSet = vodGraphMap.get(prevChoiceValue);
+    if (checkedStateIdAndChoice.containsKey(stateId)) {
+      choiceSet = checkedStateIdAndChoice.get(stateId);
+      if (choiceSet.contains(choice)) {
+        // State ID and choice found. It has been checked!
+        return false;
+      }
     } else {
-      // Create a new entry
       choiceSet = new HashSet<>();
-      vodGraphMap.put(prevChoiceValue, choiceSet);
+      checkedStateIdAndChoice.put(stateId, choiceSet);
+    }
+    choiceSet.add(choice);
+
+    return true;
+  }
+
+  // Update the backtrack sets in a previous execution
+  private void updateBacktrackSetsInPreviousExecution(int stateId) {
+    // Don't check a past trace twice!
+    HashSet<ReachableTrace> checkedTrace = new HashSet<>();
+    // Don't check the same event twice for a revisited state
+    HashMap<Integer, HashSet<Integer>> checkedStateIdAndChoice = new HashMap<>();
+    // Get sorted reachable state IDs
+    ArrayList<Integer> reachableStateIds = getReachableStateIds(rGraph.keySet(), stateId);
+    // Iterate from this state ID until the biggest state ID
+    for(Integer stId : reachableStateIds) {
+      // Find the right reachability graph object that contains the stateId
+      ArrayList<ReachableTrace> rTraces = rGraph.get(stId);
+      for (ReachableTrace rTrace : rTraces) {
+        if (!checkedTrace.contains(rTrace)) {
+          // Find the choice/event that marks the start of the subtrace from the previous execution
+          ArrayList<BacktrackPoint> pastBacktrackPointList = rTrace.getPastBacktrackPointList();
+          HashMap<Integer, ReadWriteSet> pastReadWriteFieldsMap = rTrace.getPastReadWriteFieldsMap();
+          int pastConfChoice = getPastConflictChoice(stId, pastBacktrackPointList);
+          int conflictChoice = choiceCounter;
+          // Iterate from the starting point until the end of the past execution trace
+          while (pastConfChoice < pastBacktrackPointList.size() - 1) {  // BacktrackPoint list always has a surplus of 1
+            // Get the info of the event from the past execution trace
+            BacktrackPoint confBtrackPoint = pastBacktrackPointList.get(pastConfChoice);
+            if (isNotChecked(checkedStateIdAndChoice, confBtrackPoint)) {
+              ReadWriteSet rwSet = pastReadWriteFieldsMap.get(pastConfChoice);
+              // Append this event to the current list and map
+              backtrackPointList.add(confBtrackPoint);
+              readWriteFieldsMap.put(choiceCounter, rwSet);
+              for (int eventCounter = conflictChoice - 1; eventCounter >= 0; eventCounter--) {
+                if (isConflictFound(eventCounter, conflictChoice, true) && isNewConflict(conflictChoice, eventCounter)) {
+                  createBacktrackingPoint(conflictChoice, eventCounter, true);
+                }
+              }
+              // Remove this event to replace it with a new one
+              backtrackPointList.remove(backtrackPointList.size() - 1);
+              readWriteFieldsMap.remove(choiceCounter);
+            }
+            pastConfChoice++;
+          }
+          checkedTrace.add(rTrace);
+        }
+      }
     }
-    choiceSet.add(currChoiceValue);
-    prevChoiceValue = currChoiceValue;
   }
 }