示例#1
0
        /// <summary>
        /// Explores the P# program for bugs.
        /// </summary>
        private void FindBugs()
        {
            Output.PrintLine("... Using '{0}' strategy", AnalysisContext.Configuration.SchedulingStrategy);

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

                    if (this.AnalysisContext.Configuration.ScheduleIntraMachineConcurrency)
                    {
                        PSharpRuntime.BugFinder = new TaskAwareBugFindingScheduler(this.Strategy);
                    }
                    else
                    {
                        PSharpRuntime.BugFinder = new BugFindingScheduler(this.Strategy);
                    }

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

                    // Configure the test.
                    PSharpRuntime.Configure(this.AnalysisContext.Configuration);

                    // Start the test and wait for it to terminate.
                    this.AnalysisContext.TestMethod.Invoke(null, null);
                    PSharpRuntime.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.AnalysisContext.Configuration.CheckLiveness &&
                        !this.AnalysisContext.Configuration.CacheProgramState &&
                        !PSharpRuntime.BugFinder.BugFound)
                    {
                        PSharpRuntime.LivenessChecker.Run();
                    }

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

                    this.ExploredSchedules++;
                    this.ExploredDepth = PSharpRuntime.BugFinder.SchedulingPoints;

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

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

                    this.Strategy.ConfigureNextIteration();

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

                        break;
                    }
                }
            });

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

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

                Output.Debug(ex.Message);
                Output.Debug(ex.StackTrace);
                ErrorReporter.ReportAndExit("Internal systematic testing exception. " +
                                            "Please send a bug report to the developers.");
            }
            finally
            {
                Profiler.StopMeasuringExecutionTime();
            }
        }