import gov.nasa.jpf.vm.choice.IntChoiceFromSet;
import gov.nasa.jpf.vm.choice.IntIntervalGenerator;
-import java.awt.*;
import java.io.PrintWriter;
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 {
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
-
- // 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, RestorableState> restorableStateMap; // Maps state IDs to the restorable state object
// Boolean states
private boolean isBooleanCGFlipped;
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);
out = null;
}
isBooleanCGFlipped = false;
+ numOfConflicts = 0;
+ numOfTransitions = 0;
restorableStateMap = new HashMap<>();
initializeStatesVariables();
}
@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");
}
}
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();
}
fairSchedulingAndBacktrackPoint(icsCG, vm);
// 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()) {
+ // 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++;
}
justVisitedStates.clear();
choiceCounter++;
}
+ } else {
+ numOfTransitions++;
}
}
// 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);
}
}
}
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);
}
}
}
+ // This class compactly stores: 1) restorable VM state, and 2) global choice counter
+ private class RestorableState {
+ private RestorableVMState restorableState;
+ private int choiceCounter;
+
+ public RestorableState (RestorableVMState restState, int choCtr) {
+ restorableState = restState;
+ choiceCounter = choCtr;
+ }
+
+ public RestorableVMState getRestorableState() {
+ return restorableState;
+ }
+
+ public int getChoiceCounter() {
+ return choiceCounter;
+ }
+ }
+
// -- CONSTANTS
private final static String DO_CALL_METHOD = "doCall";
// We exclude fields that come from libraries (Java and Groovy), and also the infrastructure
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);
+ restorableStateMap.put(stateId, new RestorableState(restorableState, choiceCounter));
}
private Integer[] copyChoices(Integer[] choicesToCopy) {
conflictPairMap = new HashMap<>();
doneBacktrackSet = new HashSet<>();
readWriteFieldsMap = new HashMap<>();
- // VOD graph
- prevChoiceValue = -1;
- vodGraphMap = new HashMap<>();
// Booleans
isEndOfExecution = false;
}
stateToEventMap.put(stateId, eventSet);
}
justVisitedStates.add(stateId);
+ analyzeReachabilityAndCreateBacktrackPoints(search.getVM(), stateId);
}
// --- Functions related to Read/Write access analysis on shared fields
private void exploreNextBacktrackPoints(VM vm, IntChoiceFromSet icsCG) {
- // We can start exploring the next backtrack point after the current CG is advanced at least once
- if (choiceCounter > 0) {
- // 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;
- }
+ // 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).getRestorableState();
+ 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) {
return rwSet;
}
+ private boolean isConflictFound(int eventCounter, int currentChoice) {
+
+ 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) ||
+ choices[actualCurrCho] == 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 actualCurrCho = currentChoice % refChoices.length;
}
}
- // --- 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 prevChoIndex = (currentChoice - 1) % refChoices.length;
- int currEvent = refChoices[choiceIndex];
- int prevEvent = refChoices[prevChoIndex];
- // 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 just continue the exploration
- continue;
- }
- // 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;
+ // --- 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 there is a state match
+ if (!vm.isNewState()) {
+ if (restorableStateMap.containsKey(stateId)) {
+ // Find the choice/event that marks the start of this cycle: first choice we explore for conflicts
+ int conflictChoice = restorableStateMap.get(stateId).getChoiceCounter();
+ 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) && isNewConflict(conflictChoice, eventCounter)) {
+ createBacktrackingPoint(conflictChoice, eventCounter);
}
- nodesToVisit.addLast(nextNode);
}
+ conflictChoice++;
}
}
}
- return false;
- }
-
- private void updateVODGraph(int currChoiceValue) {
- // Update the graph when we have the current choice value
- HashSet<Integer> choiceSet;
- if (vodGraphMap.containsKey(prevChoiceValue)) {
- // If the key already exists, just retrieve it
- choiceSet = vodGraphMap.get(prevChoiceValue);
- } else {
- // Create a new entry
- choiceSet = new HashSet<>();
- vodGraphMap.put(prevChoiceValue, choiceSet);
- }
- choiceSet.add(currChoiceValue);
- prevChoiceValue = currChoiceValue;
}
}