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
private boolean verboseMode;
private boolean stateReductionMode;
private final PrintWriter out;
+ private PrintWriter fileWriter;
private String detail;
private int depth;
private int id;
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;
} else {
out = null;
}
+ String outputFile = config.getString("file_output");
+ if (!outputFile.isEmpty()) {
+ try {
+ fileWriter = new PrintWriter(new FileWriter(outputFile, true), true);
+ } catch (IOException e) {
+ }
+ }
isBooleanCGFlipped = false;
numOfConflicts = 0;
numOfTransitions = 0;
}
}
+ static Logger log = JPF.getLogger("report");
+
@Override
public void searchFinished(Search search) {
if (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();
}
}
// Check and record a backtrack set for just once!
if (isConflictFound(nextInsn, eventCounter, currentChoice, fieldClass) &&
isNewConflict(currentChoice, eventCounter)) {
- createBacktrackingPoint(currentChoice, eventCounter);
+ createBacktrackingPoint(currentChoice, eventCounter, false);
}
}
}
}
}
+ // 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 ReachableTrace(ArrayList<BacktrackPoint> btrackPointList,
+ HashMap<Integer, ReadWriteSet> rwFieldsMap) {
+ pastBacktrackPointList = btrackPointList;
+ pastReadWriteFieldsMap = rwFieldsMap;
+ }
+
+ public ArrayList<BacktrackPoint> getPastBacktrackPointList() {
+ return pastBacktrackPointList;
+ }
+
+ public HashMap<Integer, ReadWriteSet> getPastReadWriteFieldsMap() {
+ return pastReadWriteFieldsMap;
+ }
+ }
+
// -- CONSTANTS
private final static String DO_CALL_METHOD = "doCall";
// We exclude fields that come from libraries (Java and Groovy), and also the infrastructure
doneBacktrackSet = new HashSet<>();
readWriteFieldsMap = new HashMap<>();
stateToChoiceCounterMap = new HashMap<>();
+ rGraph = new HashMap<>();
// Booleans
isEndOfExecution = false;
}
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);
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;
- // We use the actual choices here in case they have been modified/adjusted by the fair scheduling method
- newChoiceList[0] = choices[actualCurrCho];
+ 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++) {
return rwSet;
}
- private boolean isConflictFound(int eventCounter, int currentChoice) {
+ private boolean isConflictFound(int eventCounter, int currentChoice, boolean isPastTrace) {
- int actualCurrCho = currentChoice % refChoices.length;
+ 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) ||
- choices[actualCurrCho] == backtrackPointList.get(eventCounter).getChoice()) {
+ currActualChoice == backtrackPointList.get(eventCounter).getChoice()) {
return false;
}
// Current R/W set
choiceCounter = 0;
choices = icsCG.getAllChoices();
refChoices = copyChoices(choices);
- // Clearing data structures
- 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;
- backtrackPointList.clear();
}
}
// 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);
+ 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);
+ }
+ }
+ }
+
+ // 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;
+ }
+ }
+ 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);
+ }
+ }
+ 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++;
+ }
+ }
+
+ // 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 (checkedStateIdAndChoice.containsKey(stateId)) {
+ choiceSet = checkedStateIdAndChoice.get(stateId);
+ if (choiceSet.contains(choice)) {
+ // State ID and choice found. It has been checked!
+ return false;
+ }
+ } else {
+ choiceSet = new HashSet<>();
+ 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);
}
- conflictChoice++;
}
}
}