Fixing a bug: merging the analysis part for locationMode w.r.t manual interaction...
[jpf-core.git] / src / main / gov / nasa / jpf / listener / StateReducer.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.listener;
19
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.ListenerAdapter;
23 import gov.nasa.jpf.search.Search;
24 import gov.nasa.jpf.jvm.bytecode.*;
25 import gov.nasa.jpf.vm.*;
26 import gov.nasa.jpf.vm.bytecode.ReadInstruction;
27 import gov.nasa.jpf.vm.bytecode.WriteInstruction;
28 import gov.nasa.jpf.vm.choice.IntChoiceFromSet;
29
30 import java.io.PrintWriter;
31
32 import java.util.*;
33
34 // TODO: Fix for Groovy's model-checking
35 // TODO: This is a setter to change the values of the ChoiceGenerator to implement POR
36 /**
37  * Simple tool to log state changes.
38  *
39  * This DPOR implementation is augmented by the algorithm presented in this SPIN paper:
40  * http://spinroot.com/spin/symposia/ws08/spin2008_submission_33.pdf
41  *
42  * The algorithm is presented on page 11 of the paper. Basically, we create a graph G
43  * (i.e., visible operation dependency graph)
44  * that maps inter-related threads/sub-programs that trigger state changes.
45  * The key to this approach is that we evaluate graph G in every iteration/recursion to
46  * only update the backtrack sets of the threads/sub-programs that are reachable in graph G
47  * from the currently running thread/sub-program.
48  */
49 public class StateReducer extends ListenerAdapter {
50
51   // Debug info fields
52   private boolean debugMode;
53   private boolean stateReductionMode;
54   private final PrintWriter out;
55   volatile private String detail;
56   volatile private int depth;
57   volatile private int id;
58   Transition transition;
59
60   // State reduction fields
61   private Integer[] choices;
62   private IntChoiceFromSet currCG;
63   private int choiceCounter;
64   private Integer choiceUpperBound;
65   private Integer maxUpperBound;
66   private boolean isInitialized;
67   private boolean isResetAfterAnalysis;
68   private boolean isBooleanCGFlipped;
69   private HashMap<IntChoiceFromSet, Integer> cgMap;
70   // Record the mapping between event number and field accesses (Read and Write)
71   private HashMap<Integer, ReadWriteSet> readWriteFieldsMap;
72   // The following is the backtrack map (set) that stores all the backtrack information
73   // e.g., event number 1 can have two backtrack sequences: {3,1,2,4,...} and {2,1,3,4,...}
74   private HashMap<Integer, LinkedList<Integer[]>> backtrackMap;
75   // Stores explored backtrack lists in the form of HashSet of Strings
76   private HashSet<String> backtrackSet;
77   private HashMap<Integer, HashSet<Integer>> conflictPairMap;
78   // Map choicelist with start index
79   //  private HashMap<Integer[],Integer> choiceListStartIndexMap;
80
81   // Map that represents graph G
82   // (i.e., visible operation dependency graph (VOD Graph)
83   private HashMap<Integer, HashSet<Integer>> vodGraphMap;
84   // Set that represents hash table H
85   // (i.e., hash table that records encountered states)
86   // VOD graph is updated when the state has not yet been seen
87   private HashSet<Integer> visitedStateSet;
88   // Current state
89   private int stateId;
90   // Previous choice number
91   private int prevChoiceValue;
92
93   public StateReducer(Config config, JPF jpf) {
94     debugMode = config.getBoolean("debug_state_transition", false);
95     stateReductionMode = config.getBoolean("activate_state_reduction", true);
96     if (debugMode) {
97       out = new PrintWriter(System.out, true);
98     } else {
99       out = null;
100     }
101     detail = null;
102     depth = 0;
103     id = 0;
104     transition = null;
105     isBooleanCGFlipped = false;
106     vodGraphMap = new HashMap<>();
107     visitedStateSet = new HashSet<>();
108     stateId = -1;
109     prevChoiceValue = -1;
110     cgMap = new HashMap<>();
111     readWriteFieldsMap = new HashMap<>();
112     backtrackMap = new HashMap<>();
113     backtrackSet = new HashSet<>();
114     conflictPairMap = new HashMap<>();
115     initializeStateReduction();
116   }
117
118   private void initializeStateReduction() {
119     if (stateReductionMode) {
120       choices = null;
121       currCG = null;
122       choiceCounter = 0;
123       choiceUpperBound = 0;
124       maxUpperBound = 0;
125       isInitialized = false;
126       isResetAfterAnalysis = false;
127       cgMap.clear();
128       readWriteFieldsMap.clear();
129       backtrackMap.clear();
130       backtrackSet.clear();
131       conflictPairMap.clear();
132     }
133   }
134
135   @Override
136   public void stateRestored(Search search) {
137     if (debugMode) {
138       id = search.getStateId();
139       depth = search.getDepth();
140       transition = search.getTransition();
141       detail = null;
142       out.println("\n==> DEBUG: The state is restored to state with id: " + id + " -- Transition: " + transition +
143               " and depth: " + depth + "\n");
144     }
145   }
146
147   //--- the ones we are interested in
148   @Override
149   public void searchStarted(Search search) {
150     if (debugMode) {
151       out.println("\n==> DEBUG: ----------------------------------- search started" + "\n");
152     }
153   }
154
155   @Override
156   public void choiceGeneratorRegistered(VM vm, ChoiceGenerator<?> nextCG, ThreadInfo currentThread, Instruction executedInstruction) {
157     if (stateReductionMode) {
158       // Initialize with necessary information from the CG
159       if (nextCG instanceof IntChoiceFromSet) {
160         IntChoiceFromSet icsCG = (IntChoiceFromSet) nextCG;
161         // Check if CG has been initialized, otherwise initialize it
162         Integer[] cgChoices = icsCG.getAllChoices();
163         if (!isInitialized) {
164           // Get the upper bound from the last element of the choices
165           choiceUpperBound = cgChoices[cgChoices.length - 1];
166           isInitialized = true;
167         }
168         // Record the subsequent Integer CGs only until we hit the upper bound
169         if (!isResetAfterAnalysis && choiceCounter <= choiceUpperBound && !cgMap.containsValue(choiceCounter)) {
170           // Update the choices of the first CG and add '-1'
171           if (choices == null) {
172             // Initialize backtrack set that stores all the explored backtrack lists
173             maxUpperBound = cgChoices.length;
174             // All the choices are always the same so we only need to update it once
175             choices = new Integer[cgChoices.length + 1];
176             System.arraycopy(cgChoices, 0, choices, 0, cgChoices.length);
177             choices[choices.length - 1] = -1;
178             String firstChoiceListString = buildStringFromChoiceList(choices);
179             backtrackSet.add(firstChoiceListString);
180           }
181           icsCG.setNewValues(choices);
182           icsCG.reset();
183           // Advance the current Integer CG
184           // This way we explore all the event numbers in the first pass
185           icsCG.advance(choices[choiceCounter]);
186           cgMap.put(icsCG, choices[choiceCounter]);
187           choiceCounter++;
188         } else {
189           // Set done the subsequent CGs
190           // We only need n CGs (n is event numbers)
191           icsCG.setDone();
192         }
193       }
194     }
195   }
196
197   private void resetAllCGs() {
198     // Extract the event numbers that have backtrack lists
199     Set<Integer> eventSet = backtrackMap.keySet();
200     // Return if there is no conflict at all (highly unlikely)
201     if (eventSet.isEmpty()) {
202       return;
203     }
204     // Reset every CG with the first backtrack lists
205     for (IntChoiceFromSet cg : cgMap.keySet()) {
206       int event = cgMap.get(cg);
207       LinkedList<Integer[]> choiceLists = backtrackMap.get(event);
208       if (choiceLists != null && choiceLists.peekFirst() != null) {
209         Integer[] choiceList = choiceLists.removeFirst();
210         // Deploy the new choice list for this CG
211         cg.setNewValues(choiceList);
212         cg.reset();
213       } else {
214         cg.setDone();
215       }
216     }
217   }
218
219   @Override
220   public void choiceGeneratorAdvanced(VM vm, ChoiceGenerator<?> currentCG) {
221
222     if (stateReductionMode) {
223       // Check the boolean CG and if it is flipped, we are resetting the analysis
224       if (currentCG instanceof BooleanChoiceGenerator) {
225         if (!isBooleanCGFlipped) {
226           isBooleanCGFlipped = true;
227         } else {
228           initializeStateReduction();
229         }
230       }
231       // Check every choice generated and make sure that all the available choices
232       // are chosen first before repeating the same choice of value twice!
233       if (currentCG instanceof IntChoiceFromSet) {
234         IntChoiceFromSet icsCG = (IntChoiceFromSet) currentCG;
235         // Update the current pointer to the current set of choices
236         if (choices == null || choices != icsCG.getAllChoices()) {
237           currCG = icsCG;
238           choices = icsCG.getAllChoices();
239           // Reset a few things for the sub-graph
240           conflictPairMap.clear();
241           readWriteFieldsMap.clear();
242           choiceCounter = 0;
243         }
244         // Traverse the sub-graphs
245         if (isResetAfterAnalysis) {
246           // Advance choice counter for sub-graphs
247           choiceCounter++;
248           // Do this for every CG after finishing each backtrack list
249           if (icsCG.getNextChoice() == -1 || visitedStateSet.contains(stateId)) {
250             if (cgMap.containsKey(icsCG)) {
251               int event = cgMap.get(icsCG);
252               LinkedList<Integer[]> choiceLists = backtrackMap.get(event);
253               if (choiceLists != null && choiceLists.peekFirst() != null) {
254                 Integer[] choiceList = choiceLists.removeFirst();
255                 // Deploy the new choice list for this CG
256                 icsCG.setNewValues(choiceList);
257                 icsCG.reset();
258               } else {
259                 // Set done if this was the last backtrack list
260                 icsCG.setDone();
261               }
262             }
263           }
264         }
265         // Update and reset the CG if needed (do this for the first time after the analysis)
266         if (!isResetAfterAnalysis && icsCG.getNextChoice() == -1) {
267           resetAllCGs();
268           isResetAfterAnalysis = true;
269         }
270       }
271     }
272   }
273
274   public void updateVODGraph(int prevChoice, int currChoice) {
275
276     HashSet<Integer> choiceSet;
277     if (vodGraphMap.containsKey(prevChoice)) {
278       // If the key already exists, just retrieve it
279       choiceSet = vodGraphMap.get(prevChoice);
280     } else {
281       // Create a new entry
282       choiceSet = new HashSet<>();
283       vodGraphMap.put(prevChoice, choiceSet);
284     }
285     choiceSet.add(currChoice);
286   }
287
288   @Override
289   public void stateAdvanced(Search search) {
290     if (debugMode) {
291       id = search.getStateId();
292       depth = search.getDepth();
293       transition = search.getTransition();
294       if (search.isNewState()) {
295         detail = "new";
296       } else {
297         detail = "visited";
298       }
299
300       if (search.isEndState()) {
301         out.println("\n==> DEBUG: This is the last state!\n");
302         detail += " end";
303       }
304       out.println("\n==> DEBUG: The state is forwarded to state with id: " + id + " with depth: " + depth +
305               " which is " + detail + " Transition: " + transition + "\n");
306     }
307     if (stateReductionMode) {
308       // Update vodGraph
309       int currChoice = choiceCounter - 1;
310       if (currChoice < 0 || currChoice > choices.length - 1 || choices[currChoice] == -1 || prevChoiceValue == choices[currChoice]) {
311         // Handle all corner cases (e.g., out of bound values)
312         return;
313       }
314       // When current choice is 0, previous choice could be -1
315       updateVODGraph(prevChoiceValue, choices[currChoice]);
316       // Current choice becomes previous choice in the next iteration
317       prevChoiceValue = choices[currChoice];
318       // Line 19 in the paper page 11 (see the heading note above)
319       stateId = search.getStateId();
320       // Add state ID into the visited state set
321       visitedStateSet.add(stateId);
322     }
323   }
324
325   @Override
326   public void stateBacktracked(Search search) {
327     if (debugMode) {
328       id = search.getStateId();
329       depth = search.getDepth();
330       transition = search.getTransition();
331       detail = null;
332
333       out.println("\n==> DEBUG: The state is backtracked to state with id: " + id + " -- Transition: " + transition +
334               " and depth: " + depth + "\n");
335     }
336   }
337
338   @Override
339   public void searchFinished(Search search) {
340     if (debugMode) {
341       out.println("\n==> DEBUG: ----------------------------------- search finished" + "\n");
342     }
343   }
344
345   // This class compactly stores Read and Write field sets
346   // We store the field name and its object ID
347   // Sharing the same field means the same field name and object ID
348   private class ReadWriteSet {
349     private HashMap<String, Integer> readSet;
350     private HashMap<String, Integer> writeSet;
351
352     public ReadWriteSet() {
353       readSet = new HashMap<>();
354       writeSet = new HashMap<>();
355     }
356
357     public void addReadField(String field, int objectId) {
358       readSet.put(field, objectId);
359     }
360
361     public void addWriteField(String field, int objectId) {
362       writeSet.put(field, objectId);
363     }
364
365     public boolean readFieldExists(String field) {
366       return readSet.containsKey(field);
367     }
368
369     public boolean writeFieldExists(String field) {
370       return writeSet.containsKey(field);
371     }
372
373     public int readFieldObjectId(String field) {
374       return readSet.get(field);
375     }
376
377     public int writeFieldObjectId(String field) {
378       return writeSet.get(field);
379     }
380   }
381
382   private void analyzeReadWriteAccesses(Instruction executedInsn, String fieldClass, int currentChoice) {
383     // Do the analysis to get Read and Write accesses to fields
384     ReadWriteSet rwSet;
385     // We already have an entry
386     if (readWriteFieldsMap.containsKey(choices[currentChoice])) {
387       rwSet = readWriteFieldsMap.get(choices[currentChoice]);
388     } else { // We need to create a new entry
389       rwSet = new ReadWriteSet();
390       readWriteFieldsMap.put(choices[currentChoice], rwSet);
391     }
392     int objectId = ((JVMFieldInstruction) executedInsn).getFieldInfo().getClassInfo().getClassObjectRef();
393     // Record the field in the map
394     if (executedInsn instanceof WriteInstruction) {
395       // Exclude certain field writes because of infrastructure needs, e.g., Event class field writes
396       for (String str : EXCLUDED_FIELDS_WRITE_INSTRUCTIONS_STARTS_WITH_LIST) {
397         if (fieldClass.startsWith(str)) {
398           return;
399         }
400       }
401       rwSet.addWriteField(fieldClass, objectId);
402     } else if (executedInsn instanceof ReadInstruction) {
403       rwSet.addReadField(fieldClass, objectId);
404     }
405   }
406
407   private boolean recordConflictPair(int currentEvent, int eventNumber) {
408     HashSet<Integer> conflictSet;
409     if (!conflictPairMap.containsKey(currentEvent)) {
410       conflictSet = new HashSet<>();
411       conflictPairMap.put(currentEvent, conflictSet);
412     } else {
413       conflictSet = conflictPairMap.get(currentEvent);
414     }
415     // If this conflict has been recorded before, we return false because
416     // we don't want to service this backtrack point twice
417     if (conflictSet.contains(eventNumber)) {
418       return false;
419     }
420     // If it hasn't been recorded, then do otherwise
421     conflictSet.add(eventNumber);
422     return true;
423   }
424
425   private String buildStringFromChoiceList(Integer[] newChoiceList) {
426
427     // When we see a choice list shorter than the upper bound, e.g., [3,2] for choices 0,1,2, and 3,
428     //  then we have to pad the beginning before we store it, because [3,2] actually means [0,1,3,2]
429     // First, calculate the difference between this choice list and the upper bound
430     //  The actual list doesn't include '-1' at the end
431     int actualListLength = newChoiceList.length - 1;
432     int diff = maxUpperBound - actualListLength;
433     StringBuilder sb = new StringBuilder();
434     // Pad the beginning if necessary
435     for (int i = 0; i < diff; i++) {
436       sb.append(i);
437     }
438     // Then continue with the actual choice list
439     // We don't include the '-1' at the end
440     for (int i = 0; i < newChoiceList.length - 1; i++) {
441       sb.append(newChoiceList[i]);
442     }
443     return sb.toString();
444   }
445
446   private void checkAndAddBacktrackList(LinkedList<Integer[]> backtrackChoiceLists, Integer[] newChoiceList) {
447
448     String newChoiceListString = buildStringFromChoiceList(newChoiceList);
449     // Add only if we haven't seen this combination before
450     if (!backtrackSet.contains(newChoiceListString)) {
451       backtrackSet.add(newChoiceListString);
452       backtrackChoiceLists.addLast(newChoiceList);
453     }
454   }
455
456   private void createBacktrackChoiceList(int currentChoice, int conflictEventNumber) {
457
458     LinkedList<Integer[]> backtrackChoiceLists;
459     // Create a new list of choices for backtrack based on the current choice and conflicting event number
460     // If we have a conflict between 1 and 3, then we create the list {3, 1, 2, 4, 5} for backtrack
461     // The backtrack point is the CG for event number 1 and the list length is one less than the original list
462     // (originally of length 6) since we don't start from event number 0
463     if (!isResetAfterAnalysis) {
464       // Check if we have a list for this choice number
465       // If not we create a new one for it
466       if (!backtrackMap.containsKey(conflictEventNumber)) {
467         backtrackChoiceLists = new LinkedList<>();
468         backtrackMap.put(conflictEventNumber, backtrackChoiceLists);
469       } else {
470         backtrackChoiceLists = backtrackMap.get(conflictEventNumber);
471       }
472       int maxListLength = choiceUpperBound + 1;
473       int listLength = maxListLength - conflictEventNumber;
474       Integer[] newChoiceList = new Integer[listLength + 1];
475       // Put the conflicting event numbers first and reverse the order
476       newChoiceList[0] = choices[currentChoice];
477       newChoiceList[1] = choices[conflictEventNumber];
478       // Put the rest of the event numbers into the array starting from the minimum to the upper bound
479       for (int i = conflictEventNumber + 1, j = 2; j < listLength; i++) {
480         if (choices[i] != choices[currentChoice]) {
481           newChoiceList[j] = choices[i];
482           j++;
483         }
484       }
485       // Set the last element to '-1' as the end of the sequence
486       newChoiceList[newChoiceList.length - 1] = -1;
487       checkAndAddBacktrackList(backtrackChoiceLists, newChoiceList);
488       // The start index for the recursion is always 1 (from the main branch)
489     } else { // This is a sub-graph
490       // There is a case/bug that after a re-initialization, currCG is not yet initialized
491       if (currCG != null && cgMap.containsKey(currCG)) {
492         int backtrackListIndex = cgMap.get(currCG);
493         backtrackChoiceLists = backtrackMap.get(backtrackListIndex);
494         int listLength = choices.length;
495         Integer[] newChoiceList = new Integer[listLength];
496         // Copy everything before the conflict number
497         for (int i = 0; i < conflictEventNumber; i++) {
498           newChoiceList[i] = choices[i];
499         }
500         // Put the conflicting events
501         newChoiceList[conflictEventNumber] = choices[currentChoice];
502         newChoiceList[conflictEventNumber + 1] = choices[conflictEventNumber];
503         // Copy the rest
504         for (int i = conflictEventNumber + 1, j = conflictEventNumber + 2; j < listLength - 1; i++) {
505           if (choices[i] != choices[currentChoice]) {
506             newChoiceList[j] = choices[i];
507             j++;
508           }
509         }
510         // Set the last element to '-1' as the end of the sequence
511         newChoiceList[newChoiceList.length - 1] = -1;
512         checkAndAddBacktrackList(backtrackChoiceLists, newChoiceList);
513       }
514     }
515   }
516
517   // We exclude fields that come from libraries (Java and Groovy), and also the infrastructure
518   private final static String[] EXCLUDED_FIELDS_STARTS_WITH_LIST =
519           // Java and Groovy libraries
520           { "java", "org", "sun", "com", "gov", "groovy"};
521   private final static String[] EXCLUDED_FIELDS_ENDS_WITH_LIST =
522           // Groovy library created fields
523           {"stMC", "callSiteArray", "metaClass", "staticClassInfo", "__constructor__",
524                   // Infrastructure
525                   "sendEvent", "Object", "reference", "location", "app", "state", "log", "functionList", "objectList",
526                   "eventList", "valueList", "settings", "printToConsole", "app1", "app2"};
527   private final static String[] EXCLUDED_FIELDS_CONTAINS_LIST = {"_closure"};
528   private final static String[] EXCLUDED_FIELDS_WRITE_INSTRUCTIONS_STARTS_WITH_LIST = {"Event"};
529
530   private boolean isFieldExcluded(String field) {
531     // Check against "starts-with" list
532     for(String str : EXCLUDED_FIELDS_STARTS_WITH_LIST) {
533       if (field.startsWith(str)) {
534         return true;
535       }
536     }
537     // Check against "ends-with" list
538     for(String str : EXCLUDED_FIELDS_ENDS_WITH_LIST) {
539       if (field.endsWith(str)) {
540         return true;
541       }
542     }
543     // Check against "contains" list
544     for(String str : EXCLUDED_FIELDS_CONTAINS_LIST) {
545       if (field.contains(str)) {
546         return true;
547       }
548     }
549
550     return false;
551   }
552
553   // This method checks whether a choice is reachable in the VOD graph from a reference choice
554   // This is a BFS search
555   private boolean isReachableInVODGraph(int checkedChoice, int referenceChoice) {
556     // Record visited choices as we search in the graph
557     HashSet<Integer> visitedChoice = new HashSet<>();
558     visitedChoice.add(referenceChoice);
559     LinkedList<Integer> nodesToVisit = new LinkedList<>();
560     // If the state doesn't advance as the threads/sub-programs are executed (basically there is no new state),
561     // there is a chance that the graph doesn't have new nodes---thus this check will return a null.
562     if (vodGraphMap.containsKey(referenceChoice)) {
563       nodesToVisit.addAll(vodGraphMap.get(referenceChoice));
564       while(!nodesToVisit.isEmpty()) {
565         int currChoice = nodesToVisit.getFirst();
566         if (currChoice == checkedChoice) {
567           return true;
568         }
569         if (visitedChoice.contains(currChoice)) {
570           // If there is a loop then we don't find it
571           return false;
572         }
573         // Continue searching
574         visitedChoice.add(currChoice);
575         HashSet<Integer> currChoiceNextNodes = vodGraphMap.get(currChoice);
576         if (currChoiceNextNodes != null) {
577           // Add only if there is a mapping for next nodes
578           for (Integer nextNode : currChoiceNextNodes) {
579             nodesToVisit.addLast(nextNode);
580           }
581         }
582       }
583     }
584     return false;
585   }
586
587   @Override
588   public void instructionExecuted(VM vm, ThreadInfo ti, Instruction nextInsn, Instruction executedInsn) {
589     if (stateReductionMode) {
590       if (isInitialized) {
591         if (choiceCounter > choices.length - 1) {
592           // We do not compute the conflicts for the choice '-1'
593           return;
594         }
595         int currentChoice = choiceCounter - 1;
596         // Record accesses from executed instructions
597         if (executedInsn instanceof JVMFieldInstruction) {
598           // Analyze only after being initialized
599           String fieldClass = ((JVMFieldInstruction) executedInsn).getFieldInfo().getFullName();
600           // We don't care about libraries
601           if (!isFieldExcluded(fieldClass)) {
602             analyzeReadWriteAccesses(executedInsn, fieldClass, currentChoice);
603           }
604         }
605         // Analyze conflicts from next instructions
606         if (nextInsn instanceof JVMFieldInstruction) {
607           // The constructor is only called once when the object is initialized
608           // It does not have shared access with other objects
609           MethodInfo mi = nextInsn.getMethodInfo();
610           if (!mi.getName().equals("<init>")) {
611             String fieldClass = ((JVMFieldInstruction) nextInsn).getFieldInfo().getFullName();
612             // We don't care about libraries
613             if (!isFieldExcluded(fieldClass)) {
614               // Check for conflict (go backward from currentChoice and get the first conflict)
615               // If the current event has conflicts with multiple events, then these will be detected
616               // one by one as this recursively checks backward when backtrack set is revisited and executed.
617               for (int eventNumber = currentChoice - 1; eventNumber >= 0; eventNumber--) {
618                 // Skip if this event number does not have any Read/Write set
619                 if (!readWriteFieldsMap.containsKey(choices[eventNumber])) {
620                   continue;
621                 }
622                 ReadWriteSet rwSet = readWriteFieldsMap.get(choices[eventNumber]);
623                 int currObjId = ((JVMFieldInstruction) nextInsn).getFieldInfo().getClassInfo().getClassObjectRef();
624                 // 1) Check for conflicts with Write fields for both Read and Write instructions
625                 if (((nextInsn instanceof WriteInstruction || nextInsn instanceof ReadInstruction) &&
626                         rwSet.writeFieldExists(fieldClass) && rwSet.writeFieldObjectId(fieldClass) == currObjId) ||
627                         (nextInsn instanceof WriteInstruction && rwSet.readFieldExists(fieldClass) &&
628                                 rwSet.readFieldObjectId(fieldClass) == currObjId)) {
629                   // We do not record and service the same backtrack pair/point twice!
630                   // If it has been serviced before, we just skip this
631                   if (recordConflictPair(currentChoice, eventNumber)) {
632                     // Lines 4-8 of the algorithm in the paper page 11 (see the heading note above)
633                     if (!visitedStateSet.contains(stateId)||
634                             (visitedStateSet.contains(stateId) && isReachableInVODGraph(choices[currentChoice], choices[currentChoice-1]))) {
635                       createBacktrackChoiceList(currentChoice, eventNumber);
636                       // Break if a conflict is found!
637                       break;
638                     }
639                   }
640                 }
641               }
642             }
643           }
644         }
645       }
646     }
647   }
648 }