public int GetStateId(State s) { object tmp = stateHashes[new StateRecord(s.SnapshotSig)]; if(tmp == null) return -1; else return (int)tmp; }
public void AddState(State s) { if(HasState(s)) return; else stateHashes.Add(new StateRecord(s.SnapshotSig), stateHashes.Count); if(stateHashes.Count % 10000 == 0) Console.WriteLine("count={0} {1}", stateHashes.Count, DateTime.Now.Subtract(startTime)); }
public VMGlobalVariables Duplicate(State oldState, State newState) { VMGlobalVariables ret = new VMGlobalVariables(newState); for(int i = 0; i < names.Count; i++) { ret.names.Add(names[i]); ret.values.Add(values[i]); } return ret; }
/// <summary> /// Create a new thread based on the information in the thread object /// </summary> /// <param name="systemState">The State from which this thread is created</param> /// <param name="threadobj">The thread object containing information to initialize this thread</param> public ThreadState(State systemState, VMValue_thread threadobj) { this.systemState = systemState; VMValue_threadstart ts = (VMValue_threadstart)GetValue(threadobj.ThreadStartGUID); VMValue_object startingObject = (VMValue_object)systemState.Values.MakeValue(GetValue(ts.ObjGUID)); startingObject.ValueGUID = ((VMValue_object)GetValue(ts.ObjGUID)).ValueGUID; startingObject.IsConcrete = true; startingObject.IsThreadLocal = true; // VMValue_object startingObject = (VMValue_object)GetValue(ts.ObjGUID); VMValue_ftn ftn = (VMValue_ftn)GetValue(ts.FtnGUID); InitializeThread(threadobj.GUID, startingObject, ftn.Method); }
// . Check for optimal number of barriers needed for a program // by trying to put at every possible configurations starting // from the configuration with 1, 2, 3, ... barriers. // . We could not do this checking directly on the flow graph // because it is not possible to check whether a configuration // is disabled by a particular barrier (suppose there is a branch // and the barrier comes before it, the transition is a reordering // between an instruction after the branch and the one before the // branch. The barriers may even change the branch condition // . So for every configuration we need to run the reachablity // analysis again. public static void CheckOptimality(State st0) { int i; stateSet.AddState(st0); FindGoodState(st0); Console.Write("Good end values:"); for(i = 0; i < goodReportedValues.Count; i++) Console.Write(" {0}", goodReportedValues[i]); Console.WriteLine(); stateSet = new StateSet(); stateSet.AddState(st0); badState = -1; possibleLocations = new Hashtable(); FindBadState(st0); int nLocations = 0; IDictionaryEnumerator iter = possibleLocations.GetEnumerator(); while(iter.MoveNext()) if(((CILInstruction)iter.Value).ParentMethod.Name.Equals(".ctor") == false) nLocations++; marker = new int[nLocations]; for(i = 0; i < marker.Length; i++) marker[i] = 0; ins = new CILInstruction[marker.Length]; iter = possibleLocations.GetEnumerator(); i = 0; while(iter.MoveNext()) if(((CILInstruction)iter.Value).ParentMethod.Name.Equals(".ctor") == false) ins[i++] = (CILInstruction)iter.Value; found = false; int nBarriers = 1; initialState = st0; while(found == false) { Console.WriteLine("progress: nBarriers = {0}", nBarriers); last = -1; GenerateCases(nBarriers); nBarriers++; } for(i = 0; i < marker.Length; i++) if(marker[i] == 1) Console.WriteLine(ins[i].ParentMethod.Name + ":" + ins[i]); }
public bool HasState(State s) { return GetStateId(s) >= 0; }
private void Search(State st) { searchStack.Push(stateSet.GetStateId(st)); int t1 = 0; int t2 = st.GetForwardStateCount() - 1; if (CheckerConfiguration.DoPOR) { for (int i = 0; i < st.ThreadCount; i++) { int[] trans = st.GetThreadTransitions(i); if (trans.Length == 0) goto nextThread; for (int j = 0; j < trans.Length; j++) if (st.IsForwardEscaping(trans[j]) == true) goto nextThread; // now we found a potential ample set, must check C3 condition t1 = trans[0]; t2 = trans[trans.Length - 1]; for(int j = t1; j <= t2; j++) { State stnew = st.Duplicate(); stnew.Forward(j); if(stateSet.HasState(stnew)) { int k = stateSet.GetStateId(stnew); for (int u = 0; u < searchStack.Count; u++) if(searchStack[u] == k) goto nextThread; //unfortunately this new state is on the search stack } } nAmpleFound++; goto foundAmple; nextThread: ; } nNoAmpleFound++; foundAmple: ; } for (int i = t1; i <= t2; i++) { State stnew = st.Duplicate(); stnew.Forward(i); if(stnew.IsDeadLock) stnew.Report(-2); bool visited = stateSet.HasState(stnew); if (visited == false) stateSet.AddState(stnew); if (st.IsForwardReordering(i) != null) mfGraph.AddEdge(stateSet.GetStateId(st), stateSet.GetStateId(stnew), 1, st.IsForwardReordering(i)); else mfGraph.AddEdge(stateSet.GetStateId(st), stateSet.GetStateId(stnew), 100000000, null); if (stnew.IsEndState == true) { mfGraph.GetVertex(stateSet.GetStateId(stnew)).PropertyValue = stnew.ReportedValue; if(CheckerConfiguration.DoTiming) Console.WriteLine(DateTime.Now.Subtract(startTime)); }else if (visited == false) Search(stnew); } searchStack.Pop(); }
public Graph BuildGraph(State st0) { startTime = DateTime.Now; stateSet.AddState(st0); Search(st0); FindInvalidStates(); AddWeight(mfGraph); Console.WriteLine("Build graph time: {0}", DateTime.Now.Subtract(startTime)); Console.WriteLine("State count: {0}", stateSet.Count); Console.WriteLine("Ample/Full: {0} {1}", nAmpleFound, nNoAmpleFound); return mfGraph; }
public static void FindBarrierByMaxflow(State st0) { SimpleFlowGraphBuilder builder = new SimpleFlowGraphBuilder(); mfGraph = builder.BuildGraph(st0); Console.WriteLine("Done, running maxflow algorithm with {0} vertices {1} edges", mfGraph.CountVertex(), mfGraph.CountEdge()); Console.WriteLine("Ford-Fulkerson: {0}", mfGraph.FordFulkerson()); DateTime maxflowStartTime = DateTime.Now; Edge[] edges = mfGraph.MinCut; maxflowTime = DateTime.Now.Subtract(maxflowStartTime); ArrayList positions = new ArrayList(); foreach(Edge e in edges) { CILInstruction inst = ((DelayedAction)e.ExtraInfo).SourceInstruction; bool found = false; foreach(CILInstruction ii in positions) if(ii == inst) { found = true; break; } if(found == false) positions.Add(inst); } foreach(CILInstruction inst in positions) Console.WriteLine(inst.ParentMethod + "::" + inst.ToString()); }
public VMGlobalVariables(State systemState) { this.systemState = systemState; }
public static void Search(State st) { if(stateSet.HasState(st)) return; stateSet.AddState(st); int n = st.GetForwardStateCount(); for(int i = 0; i < n; i++) { State stnew = st.Duplicate(); stnew.Forward(i); if(stnew.IsDeadLock) stnew.Report(-1); Search(stnew); } }
public static void GuidedSearch(State state) { string path = CheckerConfiguration.GetProperty("guided_path"); Strtok st = new Strtok(path); while(true) { string sc = st.NextToken(); if(sc == null) break; int choice = Int32.Parse(sc); if(choice < state.GetForwardStateCount()) { DelayedAction da = state.IsForwardReordering(choice); state.Forward(choice); if(CheckerConfiguration.DoPrintExecution) if(da != null) Console.WriteLine("Reordered: " + da.SourceInstruction.ToString()); } else { Console.WriteLine("wrong path is supplied"); break; } } }
public static void FindGoodState(State st) { int n = st.GetForwardStateCount(); bool visited; for(int i = 0; i < n; i++) if(st.IsForwardReordering(i) == null) { State stnew = st.Duplicate(); stnew.Forward(i); if(stnew.IsDeadLock) stnew.Report(-1); visited = stateSet.HasState(stnew); if(visited == false) { stateSet.AddState(stnew); if(stnew.IsEndState == true) { foreach(int x in goodReportedValues) if(x == stnew.ReportedValue) goto nexti; goodReportedValues.Add(stnew.ReportedValue); } else { FindGoodState(stnew); } } nexti:; } }
public static void FindFirstBadState(State st) { int n = st.GetForwardStateCount(); bool visited; for(int i = 0; i < n; i++) { State stnew = st.Duplicate(); stnew.Forward(i); if(stnew.IsDeadLock) stnew.Report(-1); visited = stateSet.HasState(stnew); if(visited == false) { stateSet.AddState(stnew); if(stnew.IsEndState == true) { foreach(int x in goodReportedValues) if(x == stnew.ReportedValue) goto skip1; badState = stateSet.GetStateId(stnew); return; skip1: ; } } if(st.IsForwardReordering(i) != null) { CILInstruction inst = (st.IsForwardReordering(i)).SourceInstruction; if(possibleLocations.ContainsKey(inst.ID) == false) possibleLocations.Add(inst.ID, inst); } if((visited == false) && (stnew.IsEndState == false)) if(badState == -1) // break the recursion if we found bad state FindFirstBadState(stnew); } }
public static State MakeInitialState(CILProgram program) { State state = new State(); state.values = new VMValueManager(); state.heap = new VMGlobalVariables(state); state.program = program; VMValue_ftn ftn = state.Values.MakeFtnValue(program.EntryPoint); ftn.IsConcrete = true; VMValue_object obj = (VMValue_object)state.Values.MakeValue(new CILVar_object("", program.GetClass("[mscorlib]System.Object"))); obj.IsConcrete = true; VMValue_threadstart ts = state.Values.MakeThreadStartValue(obj, ftn); ts.IsConcrete = true; VMValue_thread threadobj = state.Values.MakeThreadValue(ts); threadobj.IsConcrete = true; ThreadState initThread = new ThreadState(state, threadobj); state.AddThread(initThread); state.TakeSnapshot(); return state; }
public ThreadState Duplicate(State oldState, State newState) { ThreadState ret = new ThreadState(); ret.systemState = newState; ret.threadID = threadID; ret.syncState = syncState; ret.waitingOn = waitingOn; for(int i = 0; i < threadStack.Count; i++) ret.threadStack.Add(threadStack[i]); for(int i = 0; i < localVariableBlocks.Count; i++) ret.localVariableBlocks.Push(localVariableBlocks[i].Duplicate(this, ret)); IEnumerator<DelayedAction> iter = pendingActions.GetEnumerator(); while(iter.MoveNext()) ret.pendingActions.Add(iter.Current.Duplicate()); ret.returnAddresses = new FreeStack<ReturnAddress>(); foreach(ReturnAddress ra in returnAddresses) ret.returnAddresses.Add(ra.Duplicate()); ret.currentMethod = currentMethod; ret.pc = pc; return ret; }
/// <summary> /// Return a copy of this state /// </summary> /// <returns>Reference to a copy of this state</returns> public State Duplicate() { State ret = new State(); // change this to a for instead of foreach to ensure // the order in this and in the copy is the same for DelayedPulse for(int i = 0; i < threads.Count; i++) ret.threads.Add(threads[i].Duplicate(this, ret)); ret.program = program; ret.heap = heap.Duplicate(this, ret); ret.values = values.Duplicate(); ret.observers = observers; ret.steps.AddRange(steps); ret.snapshot = snapshot; return ret; }
public static void FindBadState(State st) { bool visited; int t1 = 0, t2 = st.ThreadCount - 1; for(int i = t1; i <= t2; i++) { int[] trans = st.GetThreadTransitions(i); for (int j = 0; j < trans.Length; j++) { State stnew = st.Duplicate(); stnew.Forward(trans[j]); if (stnew.IsDeadLock) stnew.Report(-1); visited = stateSet.HasState(stnew); if (visited == false) { stateSet.AddState(stnew); if (stnew.IsEndState == true) { foreach (int x in goodReportedValues) if (x == stnew.ReportedValue) goto skip1; badState = stateSet.GetStateId(stnew); skip1: ; } } if (st.IsForwardReordering(trans[j]) != null) { CILInstruction inst = (st.IsForwardReordering(trans[j])).SourceInstruction; if (possibleLocations.ContainsKey(inst.ID) == false) possibleLocations.Add(inst.ID, inst); } if ((visited == false) && (stnew.IsEndState == false)) FindBadState(stnew); } } }