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 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, Integer> stateToChoiceCounterMap; // Maps state IDs to the choice counter
// 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();
}
resetStatesForNewExecution(icsCG, vm);
// If we don't see a fair scheduling of events/choices then we have to enforce it
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++;
}
+ // Map state to event
+ mapStateToEvent(icsCG.getNextChoice());
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);
}
conflictPairMap = new HashMap<>();
doneBacktrackSet = new HashSet<>();
readWriteFieldsMap = new HashMap<>();
- // VOD graph
- prevChoiceValue = -1;
- vodGraphMap = new HashMap<>();
+ stateToChoiceCounterMap = new HashMap<>();
// Booleans
isEndOfExecution = false;
}
// 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);
}
+ stateToChoiceCounterMap.put(stateId, choiceCounter);
+ analyzeReachabilityAndCreateBacktrackPoints(search.getVM(), stateId);
justVisitedStates.add(stateId);
+ currVisitedStates.add(stateId);
}
// --- Functions related to Read/Write access analysis on shared fields
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++) {
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);
+ 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 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);
+ // --- 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 &&
+ currVisitedStates.contains(stateId) && (stateId > 0)) {
+ // 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) && isNewConflict(conflictChoice, eventCounter)) {
+ createBacktrackingPoint(conflictChoice, eventCounter);
}
}
+ 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;
}
}