Пример #1
0
        /// <summary>
        /// Emits race instrumentation traces.
        /// </summary>
        /// <param name="runtime">Runtime</param>
        /// <param name="iteration">Iteration</param>
        private void EmitRaceInstrumentationTraces(PSharpBugFindingRuntime runtime, int iteration)
        {
            string name          = Path.GetFileNameWithoutExtension(this.Assembly.Location);
            string directoryPath = base.GetRuntimeTracesDirectory();

            foreach (var kvp in runtime.MachineActionTraceMap)
            {
                IO.Debug($"<RaceTracing> Machine id '{kvp.Key}'");

                foreach (var actionTrace in kvp.Value)
                {
                    if (actionTrace.Type == MachineActionType.InvocationAction)
                    {
                        IO.Debug($"<RaceTracing> Action '{actionTrace.ActionName}' " +
                                 $"'{actionTrace.ActionId}'");
                    }
                }

                if (kvp.Value.Count > 0)
                {
                    string path = directoryPath + name + "_iteration_" + iteration +
                                  "_machine_" + kvp.Key.GetHashCode() + ".osl";
                    using (FileStream stream = File.Open(path, FileMode.Create))
                    {
                        DataContractSerializer serializer = new DataContractSerializer(kvp.Value.GetType());
                        serializer.WriteObject(stream, kvp.Value);
                        IO.Debug($"... Writing {path}");
                    }
                }
            }
        }
Пример #2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal BugFindingScheduler(PSharpBugFindingRuntime runtime, ISchedulingStrategy strategy)
 {
     this.Runtime                  = runtime;
     this.Strategy                 = strategy;
     this.TaskMap                  = new ConcurrentDictionary <int, MachineInfo>();
     this.IsSchedulerRunning       = true;
     this.BugFound                 = false;
     this.HasFullyExploredSchedule = false;
 }
Пример #3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal BugFindingScheduler(PSharpBugFindingRuntime runtime, ISchedulingStrategy strategy)
 {
     this.Runtime = runtime;
     this.Strategy = strategy;
     this.MachineInfos = new ConcurrentBag<MachineInfo>();
     this.TaskMap = new ConcurrentDictionary<int, MachineInfo>();
     this.IsSchedulerRunning = true;
     this.BugFound = false;
     this.HasFullyExploredSchedule = false;
 }
Пример #4
0
        /// <summary>
        /// Constructs a reproducable trace.
        /// </summary>
        /// <param name="runtime">Runtime</param>
        private void ConstructReproducableTrace(PSharpBugFindingRuntime runtime)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (this.Strategy.IsFair())
            {
                stringBuilder.Append("--fair-scheduling").Append(Environment.NewLine);
            }

            if (base.Configuration.CacheProgramState)
            {
                stringBuilder.Append("--state-caching").Append(Environment.NewLine);
                stringBuilder.Append("--liveness-temperature-threshold:" +
                                     base.Configuration.LivenessTemperatureThreshold).
                Append(Environment.NewLine);
            }
            else
            {
                stringBuilder.Append("--liveness-temperature-threshold:" +
                                     base.Configuration.LivenessTemperatureThreshold).
                Append(Environment.NewLine);
            }

            if (!base.Configuration.TestMethodName.Equals(""))
            {
                stringBuilder.Append("--test-method:" +
                                     base.Configuration.TestMethodName).
                Append(Environment.NewLine);
            }

            for (int idx = 0; idx < runtime.ScheduleTrace.Count; idx++)
            {
                ScheduleStep step = runtime.ScheduleTrace[idx];
                if (step.Type == ScheduleStepType.SchedulingChoice)
                {
                    stringBuilder.Append($"{step.ScheduledMachineId.Type}" +
                                         $"({step.ScheduledMachineId.Value})");
                }
                else if (step.BooleanChoice != null)
                {
                    stringBuilder.Append(step.BooleanChoice.Value);
                }
                else
                {
                    stringBuilder.Append(step.IntegerChoice.Value);
                }

                if (idx < runtime.ScheduleTrace.Count - 1)
                {
                    stringBuilder.Append(Environment.NewLine);
                }
            }

            this.ReproducableTrace = stringBuilder.ToString();
        }
Пример #5
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="runtime">PSharpBugFindingRuntime</param>
        /// <param name="bugFindingSchedulingStrategy">ISchedulingStrategy</param>
        internal LivenessChecker(PSharpBugFindingRuntime runtime,
                                 ISchedulingStrategy bugFindingSchedulingStrategy)
        {
            this.Runtime = runtime;

            this.Monitors       = new List <Monitor>();
            this.PotentialCycle = new List <Tuple <ScheduleStep, State> >();
            this.HotMonitors    = new HashSet <Monitor>();

            this.LivenessTemperature          = 0;
            this.EndOfCycleIndex              = 0;
            this.CurrentCycleIndex            = 0;
            this.BugFindingSchedulingStrategy = bugFindingSchedulingStrategy;

            this.Seed   = this.Runtime.Configuration.RandomSchedulingSeed ?? DateTime.Now.Millisecond;
            this.Random = new Random(this.Seed);
        }
Пример #6
0
        /// <summary>
        /// Gathers the exploration strategy statistics for
        /// the current iteration.
        /// </summary>
        /// <param name="runtime">PSharpBugFindingRuntime</param>
        private void GatherIterationStatistics(PSharpBugFindingRuntime runtime)
        {
            if (base.Strategy.IsFair())
            {
                base.TestReport.NumOfExploredFairSchedules++;

                if (base.Strategy.HasReachedMaxSchedulingSteps())
                {
                    base.TestReport.MaxFairStepsHitInFairTests++;
                }

                if (runtime.BugFinder.ExploredSteps >= base.Configuration.MaxUnfairSchedulingSteps)
                {
                    base.TestReport.MaxUnfairStepsHitInFairTests++;
                }

                if (!base.Strategy.HasReachedMaxSchedulingSteps())
                {
                    base.TestReport.TotalExploredFairSteps += runtime.BugFinder.ExploredSteps;

                    if (base.TestReport.MinExploredFairSteps < 0 ||
                        base.TestReport.MinExploredFairSteps > runtime.BugFinder.ExploredSteps)
                    {
                        base.TestReport.MinExploredFairSteps = runtime.BugFinder.ExploredSteps;
                    }

                    if (base.TestReport.MaxExploredFairSteps < runtime.BugFinder.ExploredSteps)
                    {
                        base.TestReport.MaxExploredFairSteps = runtime.BugFinder.ExploredSteps;
                    }
                }
            }
            else
            {
                base.TestReport.NumOfExploredUnfairSchedules++;

                if (base.Strategy.HasReachedMaxSchedulingSteps())
                {
                    base.TestReport.MaxUnfairStepsHitInUnfairTests++;
                }
            }
        }
Пример #7
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal TaskAwareBugFindingScheduler(PSharpBugFindingRuntime runtime, ISchedulingStrategy strategy)
     : base(runtime, strategy)
 {
     this.UserTasks      = new ConcurrentBag <Task>();
     this.WrappedTaskMap = new ConcurrentDictionary <int, MachineInfo>();
 }
Пример #8
0
        /// <summary>
        /// Explores the P# program for bugs.
        /// </summary>
        private void FindBugs()
        {
            IO.PrintLine("... Using '{0}' strategy", this.Configuration.SchedulingStrategy);

            Task task = new Task(() =>
            {
                for (int i = 0; i < this.Configuration.SchedulingIterations; i++)
                {
                    if (this.ShouldPrintIteration(i + 1))
                    {
                        IO.PrintLine("..... Iteration #{0}", i + 1);
                    }

                    var runtime = new PSharpBugFindingRuntime(this.Configuration, this.Strategy);

                    StringWriter sw = null;
                    if (this.Configuration.RedirectConsoleOutput &&
                        this.Configuration.Verbose < 2)
                    {
                        sw = this.RedirectConsoleOutput();
                        this.HasRedirectedConsoleOutput = true;
                    }

                    // Start the test.
                    if (this.TestAction != null)
                    {
                        this.TestAction(runtime);
                    }
                    else
                    {
                        this.TestMethod.Invoke(null, new object[] { runtime });
                    }

                    // Wait for test to terminate.
                    runtime.WaitMachines();

                    // Runs the liveness checker to find any liveness property violations.
                    // Requires that no bug has been found, the scheduler terminated before
                    // reaching the depth bound, and there is state caching is not active.
                    if (this.Configuration.CheckLiveness &&
                        !this.Configuration.CacheProgramState &&
                        !runtime.BugFinder.BugFound)
                    {
                        runtime.LivenessChecker.Run();
                    }

                    if (this.HasRedirectedConsoleOutput)
                    {
                        this.ResetOutput();
                    }

                    this.ExploredSchedules++;
                    this.ExploredDepth = runtime.BugFinder.ExploredSteps;

                    if (runtime.BugFinder.BugFound)
                    {
                        this.NumOfFoundBugs++;
                        this.BugReport = runtime.BugFinder.BugReport;
                    }
                    else
                    {
                        this.BugReport = "";
                    }

                    if (this.Strategy.HasFinished())
                    {
                        break;
                    }

                    this.Strategy.ConfigureNextIteration();

                    if (!this.Configuration.FullExploration && this.NumOfFoundBugs > 0)
                    {
                        if (sw != null && !this.Configuration.SuppressTrace)
                        {
                            this.PrintTrace(sw);
                        }
                        
                        break;
                    }
                    else if (sw != null && this.Configuration.PrintTrace)
                    {
                        this.PrintTrace(sw);
                    }
                }
            });

            Profiler.StartMeasuringExecutionTime();
            task.Start();

            try
            {
                if (this.Configuration.Timeout > 0)
                {
                    task.Wait(this.Configuration.Timeout * 1000);
                }
                else
                {
                    task.Wait();
                }
            }
            catch (AggregateException ex)
            {
                if (this.HasRedirectedConsoleOutput)
                {
                    this.ResetOutput();
                }

                IO.Debug(ex.Message);
                IO.Debug(ex.StackTrace);
                ErrorReporter.ReportAndExit("Internal systematic testing exception. " +
                    "Please send a bug report to the developers.");
            }
            finally
            {
                Profiler.StopMeasuringExecutionTime();
            }
        }
Пример #9
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal BugFindingScheduler(PSharpBugFindingRuntime runtime, ISchedulingStrategy strategy)
 {
     this.Runtime = runtime;
     this.Strategy = strategy;
     this.MachineInfos = new List<MachineInfo>();
     this.TaskMap = new Dictionary<int, MachineInfo>();
     this.BugFound = false;
 }
Пример #10
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 /// <param name="strategy">SchedulingStrategy</param>
 internal TaskAwareBugFindingScheduler(PSharpBugFindingRuntime runtime, ISchedulingStrategy strategy)
     : base (runtime, strategy)
 {
     this.UserTasks = new List<Task>();
     this.WrappedTaskMap = new Dictionary<int, MachineInfo>();
 }
Пример #11
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 internal LivenessChecker(PSharpBugFindingRuntime runtime)
 {
     this.Runtime = runtime;
     this.Monitors = new List<Monitor>();
 }
Пример #12
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="runtime">PSharpBugFindingRuntime</param>
        /// <param name="bugFindingSchedulingStrategy">ISchedulingStrategy</param>
        internal LivenessChecker(PSharpBugFindingRuntime runtime,
            ISchedulingStrategy bugFindingSchedulingStrategy)
        {
            this.Runtime = runtime;

            this.Monitors = new List<Monitor>();
            this.PotentialCycle = new List<Tuple<ScheduleStep, State>>();
            this.HotMonitors = new HashSet<Monitor>();

            this.LivenessTemperature = 0;
            this.EndOfCycleIndex = 0;
            this.CurrentCycleIndex = 0;
            this.BugFindingSchedulingStrategy = bugFindingSchedulingStrategy;

            this.Seed = this.Runtime.Configuration.RandomSchedulingSeed ?? DateTime.Now.Millisecond;
            this.Random = new Random(this.Seed);
        }
Пример #13
0
        /// <summary>
        /// Emits race instrumentation traces.
        /// </summary>
        /// <param name="runtime">Runtime</param>
        /// <param name="iteration">Iteration</param>
        private void EmitRaceInstrumentationTraces(PSharpBugFindingRuntime runtime, int iteration)
        {
            string name = Path.GetFileNameWithoutExtension(this.Assembly.Location);
            string directoryPath = base.GetRuntimeTracesDirectory();

            foreach (var kvp in runtime.MachineActionTraceMap)
            {
                IO.Debug($"<RaceTracing> Machine id '{kvp.Key}'");
                foreach (var actionTrace in kvp.Value)
                {
                    if (actionTrace.Type == MachineActionType.InvocationAction)
                    {
                        IO.Debug($"<RaceTracing> Action '{actionTrace.ActionName}' " +
                            $"'{actionTrace.ActionId}'");
                    }
                }

                if (kvp.Value.Count > 0)
                {
                    string path = directoryPath + name + "_iteration_" + iteration +
                        "_machine_" + kvp.Key.GetHashCode() + ".osl";
                    using (FileStream stream = File.Open(path, FileMode.Create))
                    {
                        DataContractSerializer serializer = new DataContractSerializer(kvp.Value.GetType());
                        serializer.WriteObject(stream, kvp.Value);
                        IO.Debug($"... Writing {path}");
                    }
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Creates a new bug-finding task.
        /// </summary>
        /// <returns>Task</returns>
        private Task CreateBugFindingTask()
        {
            if (base.Configuration.TestingProcessId >= 0)
            {
                IO.Error.PrintLine($"... Task {this.Configuration.TestingProcessId} is " +
                    $"using '{base.Configuration.SchedulingStrategy}' strategy.");
            }
            else
            {
                IO.PrintLine($"... Using '{base.Configuration.SchedulingStrategy}' strategy.");
            }

            Task task = new Task(() =>
            {
                try
                {
                    if (base.TestInitMethod != null)
                    {
                        // Initializes the test state.
                        base.TestInitMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }

                int maxIterations = base.Configuration.SchedulingIterations;
                for (int i = 0; i < maxIterations; i++)
                {
                    if (this.CancellationTokenSource.IsCancellationRequested)
                    {
                        break;
                    }

                    if (this.ShouldPrintIteration(i + 1))
                    {
                        IO.PrintLine($"..... Iteration #{i + 1}");
                    }

                    var runtime = new PSharpBugFindingRuntime(base.Configuration,
                        base.Strategy, base.Visualizer);

                    StringWriter sw = null;
                    if (base.Configuration.RedirectTestConsoleOutput &&
                        base.Configuration.Verbose < 2)
                    {
                        sw = base.RedirectConsoleOutput();
                        base.HasRedirectedConsoleOutput = true;
                    }

                    // Starts the test.
                    if (base.TestAction != null)
                    {
                        base.TestAction(runtime);
                    }
                    else
                    {
                        try
                        {
                            base.TestMethod.Invoke(null, new object[] { runtime });
                        }
                        catch (TargetInvocationException ex)
                        {
                            if (!(ex.InnerException is TaskCanceledException))
                            {
                                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                            }
                        }
                    }

                    // Wait for test to terminate.
                    runtime.Wait();

                    if (base.Configuration.EnableVisualization)
                    {
                        base.Visualizer.Refresh();
                    }

                    if (this.Configuration.EnableDataRaceDetection)
                    {
                        this.EmitRaceInstrumentationTraces(runtime, i);
                    }

                    try
                    {
                        // Invokes user-provided cleanup for this iteration.
                        if (base.TestIterationDisposeMethod != null)
                        {
                            // Disposes the test state.
                            base.TestIterationDisposeMethod.Invoke(null, null);
                        }
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (!(ex.InnerException is TaskCanceledException))
                        {
                            ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                        }
                    }

                    // Invoke the per iteration callbacks, if any.
                    foreach (var callback in base.PerIterationCallbacks)
                    {
                        callback(i);
                    }

                    // Cleans up the runtime before the next
                    // iteration starts.
                    this.CleanUpRuntime();

                    // Checks for any liveness property violations. Requires
                    // that the program has terminated and no safety property
                    // violations have been found.
                    if (!runtime.BugFinder.BugFound)
                    {
                        runtime.LivenessChecker.CheckLivenessAtTermination();
                    }

                    if (base.HasRedirectedConsoleOutput)
                    {
                        base.ResetOutput();
                    }

                    this.ExploredSchedules++;
                    base.ExploredDepth = runtime.BugFinder.ExploredSteps;

                    if (base.Strategy.HasReachedMaxSchedulingSteps())
                    {
                        this.MaxStepsHit++;
                    }

                    if (runtime.BugFinder.BugFound)
                    {
                        base.NumOfFoundBugs++;
                        base.BugReport = runtime.BugFinder.BugReport;

                        if (base.Configuration.PerformFullExploration)
                        {
                            IO.PrintLine($"..... Iteration #{i + 1} triggered " +
                                $"bug #{base.NumOfFoundBugs}");
                        }
                    }
                    else
                    {
                        base.BugReport = "";
                    }

                    if (base.Strategy.HasFinished())
                    {
                        break;
                    }

                    base.Strategy.ConfigureNextIteration();

                    if (!base.Configuration.PerformFullExploration && base.NumOfFoundBugs > 0)
                    {
                        if (sw != null && !base.Configuration.SuppressTrace)
                        {
                            this.ReadableTrace = sw.ToString();
                            this.ReadableTrace += this.CreateReport("<StrategyLog>");
                            this.BugTrace = runtime.BugTrace;
                            this.ConstructReproducableTrace(runtime);
                        }

                        break;
                    }
                    else if (sw != null && base.Configuration.PrintTrace)
                    {
                        this.ReadableTrace = sw.ToString();
                        this.ReadableTrace += this.CreateReport("<StrategyLog>");
                        this.BugTrace = runtime.BugTrace;
                        this.ConstructReproducableTrace(runtime);
                    }

                    // Increases iterations if there is a specified timeout
                    // and the default iteration given.
                    if (base.Configuration.SchedulingIterations == 1 &&
                        base.Configuration.Timeout > 0)
                    {
                        maxIterations++;
                    }

                    runtime.Dispose();
                }

                try
                {
                    if (base.TestDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestDisposeMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }

            }, base.CancellationTokenSource.Token);

            return task;
        }
Пример #15
0
        /// <summary>
        /// Constructs a reproducable trace.
        /// </summary>
        /// <param name="runtime">Runtime</param>
        private void ConstructReproducableTrace(PSharpBugFindingRuntime runtime)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int idx = 0; idx < runtime.ScheduleTrace.Count; idx++)
            {
                ScheduleStep step = runtime.ScheduleTrace[idx];
                if (step.Type == ScheduleStepType.SchedulingChoice)
                {
                    stringBuilder.Append($"{step.ScheduledMachine.Id.Type}" +
                        $"({step.ScheduledMachine.Id.Value})");
                }
                else if (step.BooleanChoice != null)
                {
                    stringBuilder.Append(step.BooleanChoice.Value);
                }
                else
                {
                    stringBuilder.Append(step.IntegerChoice.Value);
                }

                if (idx < runtime.ScheduleTrace.Count - 1)
                {
                    stringBuilder.Append(Environment.NewLine);
                }
            }

            this.ReproducableTrace = stringBuilder.ToString();
        }
Пример #16
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 internal StateCache(PSharpBugFindingRuntime runtime)
 {
     this.Runtime = runtime;
     this.StateMap = new Dictionary<TraceStep, State>();
 }
Пример #17
0
        /// <summary>
        /// Creates a bug-reproducing task.
        /// </summary>
        /// <returns>Task</returns>
        private Task CreateBugReproducingTask()
        {
            Task task = new Task(() =>
            {
                var runtime = new PSharpBugFindingRuntime(base.Configuration,
                                                          base.Strategy, base.TestReport.CoverageInfo);

                StringWriter sw = null;
                if (base.Configuration.RedirectTestConsoleOutput &&
                    base.Configuration.Verbose < 2)
                {
                    sw = base.RedirectConsoleOutput();
                    base.HasRedirectedConsoleOutput = true;
                }

                // Start the test.
                if (base.TestAction != null)
                {
                    base.TestAction(runtime);
                }
                else
                {
                    try
                    {
                        if (base.TestInitMethod != null)
                        {
                            // Initializes the test state.
                            base.TestInitMethod.Invoke(null, new object[] { });
                        }

                        // Starts the test.
                        base.TestMethod.Invoke(null, new object[] { runtime });
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (!(ex.InnerException is TaskCanceledException))
                        {
                            ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                        }
                    }
                }

                // Wait for the test to terminate.
                runtime.Wait();

                try
                {
                    // Invokes user-provided cleanup for this iteration.
                    if (base.TestIterationDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestIterationDisposeMethod.Invoke(null, new object[] { });
                    }

                    // Invokes user-provided cleanup for all iterations.
                    if (base.TestDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestDisposeMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }

                // Checks for any liveness property violations. Requires
                // that the program has terminated and no safety property
                // violations have been found.
                if (!runtime.BugFinder.BugFound)
                {
                    runtime.LivenessChecker.CheckLivenessAtTermination();
                }

                if (base.HasRedirectedConsoleOutput)
                {
                    base.ResetOutput();
                }

                if (runtime.BugFinder.BugFound)
                {
                    base.TestReport.NumOfFoundBugs++;
                    base.TestReport.BugReport = runtime.BugFinder.BugReport;
                }
                else
                {
                    base.TestReport.BugReport = "";
                }
            }, base.CancellationTokenSource.Token);

            return(task);
        }
Пример #18
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="runtime">PSharpBugFindingRuntime</param>
 internal StateCache(PSharpBugFindingRuntime runtime)
 {
     this.Runtime  = runtime;
     this.StateMap = new Dictionary <ScheduleStep, State>();
 }
Пример #19
0
        /// <summary>
        /// Creates a bug-reproducing task.
        /// </summary>
        /// <returns>Task</returns>
        private Task CreateBugReproducingTask()
        {
            Task task = new Task(() =>
            {
                var runtime = new PSharpBugFindingRuntime(base.Configuration,
                    base.Strategy, base.Visualizer);

                StringWriter sw = null;
                if (base.Configuration.RedirectTestConsoleOutput &&
                    base.Configuration.Verbose < 2)
                {
                    sw = base.RedirectConsoleOutput();
                    base.HasRedirectedConsoleOutput = true;
                }

                // Start the test.
                if (base.TestAction != null)
                {
                    base.TestAction(runtime);
                }
                else
                {
                    try
                    {
                        if (base.TestInitMethod != null)
                        {
                            // Initializes the test state.
                            base.TestInitMethod.Invoke(null, new object[] { });
                        }

                        // Starts the test.
                        base.TestMethod.Invoke(null, new object[] { runtime });

                    }
                    catch (TargetInvocationException ex)
                    {
                        if (!(ex.InnerException is TaskCanceledException))
                        {
                            ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                        }
                    }
                }

                // Wait for test to terminate.
                runtime.Wait();

                if (base.Configuration.EnableVisualization)
                {
                    base.Visualizer.Refresh();
                }

                try
                {
                    // Invokes user-provided cleanup for this iteration.
                    if (base.TestIterationDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestIterationDisposeMethod.Invoke(null, new object[] { });
                    }

                    // Invokes user-provided cleanup for all iterations.
                    if (base.TestDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestDisposeMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }

                // Checks for any liveness property violations. Requires
                // that the program has terminated and no safety property
                // violations have been found.
                if (!runtime.BugFinder.BugFound)
                {
                    runtime.LivenessChecker.CheckLivenessAtTermination();
                }

                if (base.HasRedirectedConsoleOutput)
                {
                    base.ResetOutput();
                }

                base.ExploredDepth = runtime.BugFinder.ExploredSteps;

                if (runtime.BugFinder.BugFound)
                {
                    base.NumOfFoundBugs++;
                    base.BugReport = runtime.BugFinder.BugReport;
                }
                else
                {
                    base.BugReport = "";
                }
            }, base.CancellationTokenSource.Token);

            return task;
        }
Пример #20
0
        /// <summary>
        /// Creates a new bug-finding task.
        /// </summary>
        /// <returns>Task</returns>
        private Task CreateBugFindingTask()
        {
            if (base.Configuration.TestingProcessId >= 0)
            {
                IO.Error.PrintLine($"... Task {this.Configuration.TestingProcessId} is " +
                                   $"using '{base.Configuration.SchedulingStrategy}' strategy.");
            }
            else
            {
                IO.PrintLine($"... Using '{base.Configuration.SchedulingStrategy}' strategy.");
            }

            Task task = new Task(() =>
            {
                try
                {
                    if (base.TestInitMethod != null)
                    {
                        // Initializes the test state.
                        base.TestInitMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }

                int maxIterations = base.Configuration.SchedulingIterations;
                for (int i = 0; i < maxIterations; i++)
                {
                    if (this.CancellationTokenSource.IsCancellationRequested)
                    {
                        break;
                    }

                    if (this.ShouldPrintIteration(i + 1))
                    {
                        IO.PrintLine($"..... Iteration #{i + 1}");
                    }

                    var runtime = new PSharpBugFindingRuntime(base.Configuration,
                                                              base.Strategy, base.TestReport.CoverageInfo);

                    StringWriter sw = null;
                    if (base.Configuration.RedirectTestConsoleOutput &&
                        base.Configuration.Verbose < 2)
                    {
                        sw = base.RedirectConsoleOutput();
                        base.HasRedirectedConsoleOutput = true;
                    }

                    // Starts the test.
                    if (base.TestAction != null)
                    {
                        base.TestAction(runtime);
                    }
                    else
                    {
                        try
                        {
                            base.TestMethod.Invoke(null, new object[] { runtime });
                        }
                        catch (TargetInvocationException ex)
                        {
                            if (!(ex.InnerException is TaskCanceledException))
                            {
                                ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                            }
                        }
                    }

                    // Wait for the test to terminate.
                    runtime.Wait();

                    if (this.Configuration.EnableDataRaceDetection)
                    {
                        this.EmitRaceInstrumentationTraces(runtime, i);
                    }

                    try
                    {
                        // Invokes user-provided cleanup for this iteration.
                        if (base.TestIterationDisposeMethod != null)
                        {
                            // Disposes the test state.
                            base.TestIterationDisposeMethod.Invoke(null, null);
                        }
                    }
                    catch (TargetInvocationException ex)
                    {
                        if (!(ex.InnerException is TaskCanceledException))
                        {
                            ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                        }
                    }

                    // Invoke the per iteration callbacks, if any.
                    foreach (var callback in base.PerIterationCallbacks)
                    {
                        callback(i);
                    }

                    // Cleans up the runtime before the next
                    // iteration starts.
                    this.CleanUpRuntime();

                    // Checks for any liveness property violations. Requires
                    // that the program has terminated and no safety property
                    // violations have been found.
                    if (!runtime.BugFinder.BugFound)
                    {
                        runtime.LivenessChecker.CheckLivenessAtTermination();
                    }

                    if (base.HasRedirectedConsoleOutput)
                    {
                        base.ResetOutput();
                    }

                    this.GatherIterationStatistics(runtime);

                    if (runtime.BugFinder.BugFound)
                    {
                        base.TestReport.NumOfFoundBugs++;
                        base.TestReport.BugReport = runtime.BugFinder.BugReport;

                        if (base.Configuration.PerformFullExploration)
                        {
                            IO.PrintLine($"..... Iteration #{i + 1} triggered " +
                                         $"bug #{base.TestReport.NumOfFoundBugs}");
                        }
                    }
                    else
                    {
                        base.TestReport.BugReport = "";
                    }

                    if (base.Strategy.HasFinished())
                    {
                        break;
                    }

                    base.Strategy.ConfigureNextIteration();

                    if (!base.Configuration.PerformFullExploration && base.TestReport.NumOfFoundBugs > 0)
                    {
                        if (sw != null && !base.Configuration.SuppressTrace)
                        {
                            this.ReadableTrace  = sw.ToString();
                            this.ReadableTrace += this.CreateReport("<StrategyLog>");
                            this.BugTrace       = runtime.BugTrace;
                            this.ConstructReproducableTrace(runtime);
                        }

                        break;
                    }
                    else if (sw != null && base.Configuration.PrintTrace)
                    {
                        this.ReadableTrace  = sw.ToString();
                        this.ReadableTrace += this.CreateReport("<StrategyLog>");
                        this.BugTrace       = runtime.BugTrace;
                        this.ConstructReproducableTrace(runtime);
                    }

                    // Increases iterations if there is a specified timeout
                    // and the default iteration given.
                    if (base.Configuration.SchedulingIterations == 1 &&
                        base.Configuration.Timeout > 0)
                    {
                        maxIterations++;
                    }

                    runtime.Dispose();
                }

                try
                {
                    if (base.TestDisposeMethod != null)
                    {
                        // Disposes the test state.
                        base.TestDisposeMethod.Invoke(null, new object[] { });
                    }
                }
                catch (TargetInvocationException ex)
                {
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                    }
                }
            }, base.CancellationTokenSource.Token);

            return(task);
        }