public void TestMethodCall() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E : Event { } class Program : Machine { int x; [Start] [OnEntry(nameof(EntryInit))] class Init : MachineState { } void EntryInit() { this.Foo(1, 3, x); } int Foo(int x, int y, int z) { return 0; } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Program)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
/// <summary> /// Constructor. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="assemblyName">Assembly name</param> private AnalysisContext(DynamicAnalysisConfiguration configuration, string assemblyName) { this.Configuration = configuration; try { this.Assembly = Assembly.LoadFrom(assemblyName); } catch (FileNotFoundException ex) { ErrorReporter.ReportAndExit(ex.Message); } this.FindEntryPoint(); }
public void TestRaisedHaltHandled() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class Ping : Event { public Ping() : base(1, -1) { } } class Pong : Event { public Pong() : base(1, -1) { } } class Success : Event { } class PING : Machine { MachineId PongId; int Count1 = 0; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(Success), typeof(SendPing))] class Init : MachineState { } void EntryInit() { PongId = this.CreateMachine(typeof(PONG)); this.Raise(new Success()); } [OnEntry(nameof(EntrySendPing))] [OnEventGotoState(typeof(Success), typeof(WaitPong))] class SendPing : MachineState { } void EntrySendPing() { this.Send(PongId, new Ping(), this.Id); this.Raise(new Success()); } [OnEventGotoState(typeof(Pong), typeof(SendPing))] class WaitPong : MachineState { } class Done : MachineState { } } class PONG : Machine { int Count2 = 0; [Start] [OnEntry(nameof(EntryWaitPing))] [OnEventGotoState(typeof(Ping), typeof(SendPong))] class WaitPing : MachineState { } void EntryWaitPing() { } [OnEntry(nameof(EntrySendPong))] [OnEventGotoState(typeof(Success), typeof(WaitPing))] [OnEventDoAction(typeof(Halt), nameof(Action1))] class SendPong : MachineState { } void EntrySendPong() { Count2 = Count2 + 1; if (Count2 == 1) { this.Send(this.Payload as MachineId, new Pong()); } if (Count2 == 2) { this.Send(this.Payload as MachineId, new Pong()); this.Raise(new Halt()); } this.Raise(new Success()); } void Action1() { this.Assert(false); // reachable } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(PING)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestBugRepro1() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class Ping : Event { public Ping() : base(1, -1) { } } class Success : Event { } class PING : Machine { int x; int y; [Start] [OnEntry(nameof(EntryPingInit))] [OnEventDoAction(typeof(Success), nameof(SuccessAction))] [OnEventDoAction(typeof(Ping), nameof(PingAction))] class PingInit : MachineState { } void EntryPingInit() { this.Raise(new Success()); } void SuccessAction() { x = Func1(1, 1); this.Assert(x == 2); y = Func2(x); // x == 2 } void PingAction() { this.Assert(x == 4); x = x + 1; this.Assert(x == 5); } // i: value passed; j: identifies caller (1: Success handler; 2: Func2) int Func1(int i, int j) { if (j == 1) { i = i + 1; // i: 2 } if (j == 2) { this.Assert(i == 3); i = i + 1; this.Assert(i == 4); this.Send(this.Id, new Ping(), i); this.Assert(i == 4); } return i; } int Func2(int v) { v = v + 1; this.Assert(v == 3); x = Func1(v, 2); this.Assert( x == 4); return v; } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(PING)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
/// <summary> /// Create a new P# dynamic analysis context from the given assembly. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="assembly">Assembly</param> /// <returns>AnalysisContext</returns> public static AnalysisContext Create(DynamicAnalysisConfiguration configuration, Assembly assembly) { return new AnalysisContext(configuration, assembly); }
public void TestSimpleAsyncAwaitFail() { var test = @" using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.PSharp; namespace SystematicTesting { class Unit : Event { } internal class TaskCreator : Machine { int Value; [Start] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(Active))] class Init : MachineState { } void InitOnEntry() { this.Value = 0; this.Raise(new Unit()); } [OnEntry(nameof(ActiveOnEntry))] class Active : MachineState { } void ActiveOnEntry() { Process(); this.Assert(this.Value < 3, ""Value is '{0}' (expected less than '3')."", this.Value); } async void Process() { Task t = Increment(); this.Value++; await t; this.Value++; } Task Increment() { Task t = new Task(() => { this.Value++; }); t.Start(); return t; } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(TaskCreator)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingIterations = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; sctConfig.ScheduleIntraMachineConcurrency = true; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); var bugReport = "Value is '3' (expected less than '3')."; Assert.AreEqual(bugReport, sctEngine.BugReport); }
public void TestWarmStateBug() { var test = @" using System; using System.Collections.Generic; using Microsoft.PSharp; namespace SystematicTesting { class Unit : Event { } class UserEvent : Event { } class Done : Event { } class Waiting : Event { } class Computing : Event { } class EventHandler : Machine { List<MachineId> Workers; [Start] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(WaitForUser))] class Init : MachineState { } void InitOnEntry() { this.CreateMonitor(typeof(WatchDog)); this.Raise(new Unit()); } [OnEntry(nameof(WaitForUserOnEntry))] [OnEventGotoState(typeof(UserEvent), typeof(HandleEvent))] class WaitForUser : MachineState { } void WaitForUserOnEntry() { this.Monitor<WatchDog>(new Waiting()); this.Send(this.Id, new UserEvent()); } [OnEntry(nameof(HandleEventOnEntry))] [OnEventGotoState(typeof(Done), typeof(WaitForUser))] class HandleEvent : MachineState { } void HandleEventOnEntry() { this.Monitor<WatchDog>(new Computing()); this.Send(this.Id, new Done()); } } class WatchDog : Monitor { List<MachineId> Workers; [Start] [OnEventGotoState(typeof(Waiting), typeof(CanGetUserInput))] [OnEventGotoState(typeof(Computing), typeof(CannotGetUserInput))] class CanGetUserInput : MonitorState { } [Hot] [OnEventGotoState(typeof(Waiting), typeof(CanGetUserInput))] [OnEventGotoState(typeof(Computing), typeof(CannotGetUserInput))] class CannotGetUserInput : MonitorState { } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(EventHandler)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.CheckLiveness = true; sctConfig.CacheProgramState = true; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); var bugReport = "Monitor 'WatchDog' detected infinite execution that violates a liveness property."; Assert.AreEqual(bugReport, sctEngine.BugReport); }
public void TestBangaloreToRedmond() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class BookCab : Event { public BookCab() : base(2, -1) { } } class BookFlight : Event { public BookFlight() : base(2, -1) { } } class FlightBooked : Event { public FlightBooked() : base(2, -1) { } } class TryAgain : Event { public TryAgain() : base(2, -1) { } } class CabBooked : Event { public CabBooked() : base(2, -1) { } } class Thanks : Event { public Thanks() : base(2, -1) { } } class ReachedAirport : Event { public ReachedAirport() : base(2, -1) { } } class MissedFlight : Event { public MissedFlight() : base(2, -1) { } } class TookFlight : Event { public TookFlight() : base(2, -1) { } } class Unit : Event { public Unit() : base(2, -1) { } } class Employee : Machine { MachineId TravelAgentMachine; MachineId CityCabMachine; bool Check; bool RemoteCheckIn; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(Unit), typeof(BangaloreOffice))] class Init : MachineState { } void EntryInit() { TravelAgentMachine = this.CreateMachine(typeof(TravelAgent), this.Id); CityCabMachine = this.CreateMachine(typeof(CityCab), this.Id); RemoteCheckIn = false; this.Raise(new Unit()); } [OnEntry(nameof(EntryBangaloreOffice))] [OnExit(nameof(ExitBangaloreOffice))] [OnEventGotoState(typeof(TryAgain), typeof(BangaloreOffice))] [OnEventGotoState(typeof(FlightBooked), typeof(SBookCab))] [OnEventPushState(typeof(Unit), typeof(SBookFlight))] class BangaloreOffice : MachineState { } void EntryBangaloreOffice() { // push SBookFlight; this.Raise(new Unit()); } void ExitBangaloreOffice() { if (this.Trigger == typeof(FlightBooked)) { this.Send(TravelAgentMachine, new Thanks()); } } [OnEntry(nameof(EntrySBookFlight))] class SBookFlight : MachineState { } void EntrySBookFlight() { this.Send(TravelAgentMachine, new BookFlight()); this.Pop(); } [OnEntry(nameof(EntrySBookCab))] [OnExit(nameof(ExitSBookCab))] [OnEventGotoState(typeof(Default), typeof(TakeBus))] [OnEventGotoState(typeof(CabBooked), typeof(TakeCab))] class SBookCab : MachineState { } void EntrySBookCab() { this.Send(CityCabMachine, new BookCab()); } void ExitSBookCab() { this.Assert(RemoteCheckIn == false); RemoteCheckIn = true; if (this.Trigger != typeof(Default)) { this.Send(CityCabMachine, new Thanks()); } } [OnEntry(nameof(EntryTakeCab))] [OnEventGotoState(typeof(ReachedAirport), typeof(AtAirport))] class TakeCab : MachineState { } void EntryTakeCab() { this.Raise(new ReachedAirport()); } [OnEntry(nameof(EntryTakeBus))] [OnEventGotoState(typeof(ReachedAirport), typeof(AtAirport))] class TakeBus : MachineState { } void EntryTakeBus() { this.Raise(new ReachedAirport()); } [OnEntry(nameof(EntryAtAirport))] [OnExit(nameof(ExitAtAirport))] [OnEventGotoState(typeof(TookFlight), typeof(ReachedRedmond))] [OnEventGotoState(typeof(MissedFlight), typeof(BangaloreOffice))] class AtAirport : MachineState { } void EntryAtAirport() { this.Assert(RemoteCheckIn == true); Check = AmILucky(); if (Check) { this.Raise(new TookFlight()); } else { this.Raise(new MissedFlight()); } } void ExitAtAirport() { RemoteCheckIn = false; } [OnEntry(nameof(EntryReachedRedmond))] class ReachedRedmond : MachineState { } void EntryReachedRedmond() { this.Assert(false); } bool AmILucky() { if (this.Nondet()) { return true; } else { return false; } } } class TravelAgent : Machine { MachineId EmployeeMachine; [Start] [OnEntry(nameof(Entry_Init))] [OnEventGotoState(typeof(Unit), typeof(Init))] class _Init : MachineState { } void Entry_Init() { EmployeeMachine = this.Payload as MachineId; this.Raise(new Unit()); } [OnEventGotoState(typeof(BookFlight), typeof(SBookFlight))] class Init : MachineState { } [OnEntry(nameof(EntrySBookFlight))] [OnEventGotoState(typeof(Unit), typeof(Init))] [OnEventGotoState(typeof(Thanks), typeof(Init))] class SBookFlight : MachineState { } void EntrySBookFlight() { if (this.Nondet()) { this.Send(EmployeeMachine, new TryAgain()); this.Raise(new Unit()); } else { this.Send(EmployeeMachine, new FlightBooked()); } } } class CityCab : Machine { MachineId EmployeeMachine; [Start] [OnEntry(nameof(Entry_Init))] [OnEventGotoState(typeof(Unit), typeof(Init))] class _Init : MachineState { } void Entry_Init() { EmployeeMachine = this.Payload as MachineId; this.Raise(new Unit()); } [OnEventGotoState(typeof(BookCab), typeof(SBookCab))] class Init : MachineState { } [OnEntry(nameof(EntrySBookCab))] [OnEventGotoState(typeof(Unit), typeof(Init))] [OnEventGotoState(typeof(Thanks), typeof(Init))] class SBookCab : MachineState { } void EntrySBookCab() { if (this.Nondet()) { this.Raise(new Unit()); } else { this.Send(EmployeeMachine, new CabBooked()); } } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Employee)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); //Assert.AreEqual(0, SCTEngine.NumOfFoundBugs); //Assert.AreEqual(5, SCTEngine.ExploredDepth); }
public void TestActions5Fail() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E1 : Event { public E1() : base(1, -1) { } } class E2 : Event { public E2() : base(1, -1) { } } class E3 : Event { public E3() : base(1, -1) { } } class E4 : Event { public E4() : base(1, -1) { } } class Unit : Event { public Unit() : base(1, -1) { } } class Real : Machine { MachineId GhostMachine; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(E4), typeof(S2))] [OnEventPushState(typeof(Unit), typeof(S1))] [OnEventDoAction(typeof(E2), nameof(Action1))] class Init : MachineState { } void EntryInit() { GhostMachine = this.CreateMachine(typeof(Ghost), this.Id); this.Raise(new Unit()); } [OnEntry(nameof(EntryS1))] class S1 : MachineState { } void EntryS1() { this.Send(GhostMachine, new E1()); // we wait in this state until E2 comes from Ghost, // then handle E2 using the inherited handler Action1 // installed by Init // then wait until E4 comes from Ghost, and since // there's no handler for E4 in this pushed state, // this state is popped, and E4 goto handler from Init // is invoked } [OnEntry(nameof(EntryS2))] class S2 : MachineState { } void EntryS2() { // this assert is reachable this.Assert(false); } void Action1() { this.Send(GhostMachine, new E3()); } } class Ghost : Machine { MachineId RealMachine; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(E1), typeof(S1))] class Init : MachineState { } void EntryInit() { RealMachine = this.Payload as MachineId; } [OnEntry(nameof(EntryS1))] [OnEventGotoState(typeof(E3), typeof(S2))] class S1 : MachineState { } void EntryS1() { this.Send(RealMachine, new E2()); } [OnEntry(nameof(EntryS2))] class S2 : MachineState { } void EntryS2() { this.Send(RealMachine, new E4()); } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Real)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestSEMOneMachine35() { var test = @" using System; using System.Collections.Generic; using System.Linq; using Microsoft.PSharp; namespace SystematicTesting { class Entry : Machine { List<int> rev; List<int> sorted; int i; int t; int s; bool swapped; bool b; [Start] [OnEntry(nameof(EntryInit))] class Init : MachineState { } void EntryInit() { rev = new List<int>(); sorted = new List<int>(); i = 0; while (i < 10) { rev.Insert(0, i); sorted.Add(i); i = i + 1; } this.Assert(rev.Count == 10); // Assert that simply reversing the list produces a sorted list sorted = Reverse(rev); this.Assert(sorted.Count == 10); b = IsSorted(sorted); this.Assert(b); b = IsSorted(rev); this.Assert(!b); // Assert that BubbleSort returns the sorted list sorted = BubbleSort(rev); this.Assert(sorted.Count == 10); b = IsSorted(sorted); this.Assert(b); b = IsSorted(rev); this.Assert(!b); } List<int> Reverse(List<int> l) { var result = l.ToList(); i = 0; s = result.Count; while (i < s) { t = result[i]; result.RemoveAt(i); result.Insert(0, t); i = i + 1; } return result; } List<int> BubbleSort(List<int> l) { var result = l.ToList(); swapped = true; while (swapped) { i = 0; swapped = false; while (i < result.Count - 1) { if (result[i] > result[i + 1]) { t = result[i]; result[i] = result[i + 1]; result[i + 1] = t; swapped = true; } i = i + 1; } } return result; } bool IsSorted(List<int> l) { i = 0; while (i < l.Count - 1) { if (l[i] > l[i + 1]) { return false; } i = i + 1; } return true; } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Entry)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
public void TestAlonBugAssertionFailure() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E : Event { } class Program : Machine { int i; [Start] [OnEntry(nameof(EntryInit))] [OnExit(nameof(ExitInit))] [OnEventGotoState(typeof(E), typeof(Call))] // Exit executes before this transition. class Init : MachineState { } void EntryInit() { i = 0; this.Raise(new E()); } void ExitInit() { // This assert is reachable. this.Assert(false, ""Bug found.""); } [OnEntry(nameof(EntryCall))] class Call : MachineState { } void EntryCall() { if (i == 3) { this.Pop(); } else { i = i + 1; } } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Program)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestFairNondet1() { var test = @" using System; using System.Collections.Generic; using Microsoft.PSharp; namespace SystematicTesting { class Unit : Event { } class UserEvent : Event { } class Done : Event { } class Loop : Event { } class Waiting : Event { } class Computing : Event { } class EventHandler : Machine { List<MachineId> Workers; [Start] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(WaitForUser))] class Init : MachineState { } void InitOnEntry() { this.CreateMonitor(typeof(WatchDog)); this.Raise(new Unit()); } [OnEntry(nameof(WaitForUserOnEntry))] [OnEventGotoState(typeof(UserEvent), typeof(HandleEvent))] class WaitForUser : MachineState { } void WaitForUserOnEntry() { this.Monitor<WatchDog>(new Waiting()); this.Send(this.Id, new UserEvent()); } [OnEntry(nameof(HandleEventOnEntry))] [OnEventGotoState(typeof(Done), typeof(WaitForUser))] [OnEventGotoState(typeof(Loop), typeof(HandleEvent))] class HandleEvent : MachineState { } void HandleEventOnEntry() { this.Monitor<WatchDog>(new Computing()); if (this.FairNondet()) { this.Send(this.Id, new Done()); } else { this.Send(this.Id, new Loop()); } } } class WatchDog : Monitor { List<MachineId> Workers; [Start] [Cold] [OnEventGotoState(typeof(Waiting), typeof(CanGetUserInput))] [OnEventGotoState(typeof(Computing), typeof(CannotGetUserInput))] class CanGetUserInput : MonitorState { } [Hot] [OnEventGotoState(typeof(Waiting), typeof(CanGetUserInput))] [OnEventGotoState(typeof(Computing), typeof(CannotGetUserInput))] class CannotGetUserInput : MonitorState { } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(EventHandler)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 3; sctConfig.CheckLiveness = true; sctConfig.CacheProgramState = true; sctConfig.DepthBound = 1000; Output.Debugging = true; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
/// <summary> /// Create a new P# dynamic analysis context from the given assembly name. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="assemblyName">Assembly name</param> /// <returns>AnalysisContext</returns> public static AnalysisContext Create(DynamicAnalysisConfiguration configuration, string assemblyName) { return new AnalysisContext(configuration, assemblyName); }
/// <summary> /// Constructor. /// </summary> /// <param name="configuration">Configuration</param> /// <param name="assembly">Assembly</param> private AnalysisContext(DynamicAnalysisConfiguration configuration, Assembly assembly) { this.Configuration = configuration; this.Assembly = assembly; this.FindEntryPoint(); }
public void TestSEMOneMachine33() { var test = @" using System; using System.Collections.Generic; using Microsoft.PSharp; namespace SystematicTesting { class Unit : Event { } class SeqPayload : Event { } class Entry : Machine { List<int> l; int i; MachineId mac; Tuple<List<int>, int> t; [Start] [OnEntry(nameof(EntryInit))] class Init : MachineState { } void EntryInit() { l = new List<int>(); l.Insert(0, 12); l.Insert(0, 23); l.Insert(0, 12); l.Insert(0, 23); l.Insert(0, 12); l.Insert(0, 23); mac = this.CreateMachine(typeof(Tester), l, 1); this.Send(mac, new SeqPayload(), l); } } class Tester : Machine { List<int> ii; List<int> rec; int i; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(SeqPayload), typeof(TestItNow))] class Init : MachineState { } void EntryInit() { ii = new List<int>(); rec = new List<int>(); ii = (this.Payload as object[])[0] as List<int>; this.Assert(((this.Payload as object[])[0] as List<int>)[0] == 23); this.Assert((int)(this.Payload as object[])[1] == 1); } [OnEntry(nameof(EntryTestItNow))] class TestItNow : MachineState { } void EntryTestItNow() { rec = this.Payload as List<int>; i = rec.Count - 1; while (i >= 0) { this.Assert(rec[i] == ii[i]); i = i - 1; } } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Entry)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; sctConfig.SchedulingIterations = 5; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
public void TestHotStateMonitor() { var test = @" using System; using System.Collections.Generic; using Microsoft.PSharp; namespace SystematicTesting { class Unit : Event { } class DoProcessing : Event { } class FinishedProcessing : Event { } class NotifyWorkerIsDone : Event { } class Master : Machine { List<MachineId> Workers; [Start] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(Active))] class Init : MachineState { } void InitOnEntry() { this.Workers = new List<MachineId>(); for (int idx = 0; idx < 3; idx++) { var worker = this.CreateMachine(typeof(Worker), this.Id); this.Workers.Add(worker); } this.CreateMonitor(typeof(M), this.Workers); this.Raise(new Unit()); } [OnEntry(nameof(ActiveOnEntry))] [OnEventDoAction(typeof(FinishedProcessing), nameof(ProcessWorkerIsDone))] class Active : MachineState { } void ActiveOnEntry() { foreach (var worker in this.Workers) { this.Send(worker, new DoProcessing()); } } void ProcessWorkerIsDone() { this.Monitor<M>(new NotifyWorkerIsDone()); } } class Worker : Machine { MachineId Master; [Start] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(Processing))] class Init : MachineState { } void InitOnEntry() { this.Master = (MachineId)this.Payload; this.Raise(new Unit()); } [OnEventGotoState(typeof(DoProcessing), typeof(Done))] class Processing : MachineState { } [OnEntry(nameof(DoneOnEntry))] class Done : MachineState { } void DoneOnEntry() { if (this.Nondet()) { this.Send(this.Master, new FinishedProcessing()); } this.Raise(new Halt()); } } class M : Monitor { List<MachineId> Workers; [Start] [Hot] [OnEntry(nameof(InitOnEntry))] [OnEventGotoState(typeof(Unit), typeof(Done))] [OnEventDoAction(typeof(NotifyWorkerIsDone), nameof(ProcessNotification))] class Init : MonitorState { } void InitOnEntry() { this.Workers = (List<MachineId>)this.Payload; } void ProcessNotification() { this.Workers.RemoveAt(0); if (this.Workers.Count == 0) { this.Raise(new Unit()); } } class Done : MonitorState { } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Master)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.CheckLiveness = true; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; Output.Debugging = true; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); var bugReport = "Monitor 'M' detected liveness property violation in hot state 'Init'."; Assert.AreEqual(bugReport, sctEngine.BugReport); }
/// <summary> /// Constructor. /// </summary> /// <param name="configuration">Configuration</param> private SystematicTestingProcess(DynamicAnalysisConfiguration configuration) { this.Configuration = configuration; }
public void TestNullHandlerInheritedByPushTransition() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E : Event { } class Program : Machine { int i; [Start] [OnEntry(nameof(EntryInit))] [OnExit(nameof(ExitInit))] [OnEventPushState(typeof(E), typeof(Call))] [OnEventDoAction(typeof(Default), nameof(InitAction))] class Init : MachineState { } void EntryInit() { i = 0; this.Raise(new E()); } void ExitInit() { } void InitAction() { this.Assert(false); // reachable } [OnEntry(nameof(EntryCall))] [OnExit(nameof(ExitCall))] [IgnoreEvents(typeof(E))] class Call : MachineState { } void EntryCall() { if (i == 0) { this.Raise(new E()); } else { i = i + 1; } } void ExitCall() { } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Program)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
/// <summary> /// Creates a P# systematic testing process. /// </summary> /// <param name="configuration">Configuration</param> /// <returns>ParsingProcess</returns> public static SystematicTestingProcess Create(DynamicAnalysisConfiguration configuration) { return new SystematicTestingProcess(configuration); }
public void TestSEMOneMachine34() { var test = @" using System; using System.Collections.Generic; using Microsoft.PSharp; namespace SystematicTesting { class E1 : Event { } class E2 : Event { } class E3 : Event { } class E4 : Event { } class MachOS : Machine { int Int; bool Bool; MachineId mach; Dictionary<int, int> m; List<bool> s; [Start] [OnEntry(nameof(EntryInit))] [OnEventDoAction(typeof(E1), nameof(Foo1))] [OnEventDoAction(typeof(E2), nameof(Foo2))] [OnEventDoAction(typeof(E3), nameof(Foo3))] [OnEventDoAction(typeof(E4), nameof(Foo4))] class Init : MachineState { } void EntryInit() { m = new Dictionary<int, int>(); s = new List<bool>(); m.Add(0, 1); m.Add(1, 2); s.Add(true); s.Add(false); s.Add(true); this.Send(this.Id, new E1(), Tuple.Create(1, true)); this.Send(this.Id, new E2(), 0, false); this.Send(this.Id, new E3(), 1); this.Send(this.Id, new E4(), Tuple.Create(m, s)); } void Foo1() { Int = (int)(this.Payload as Tuple<int, bool>).Item1; this.Assert(Int == 1); Bool = (bool)(this.Payload as Tuple<int, bool>).Item2; this.Assert(Bool == true); } void Foo2() { Int = (int)(this.Payload as object[])[0]; this.Assert(Int == 0); Bool = (bool)(this.Payload as object[])[1]; this.Assert(Bool == false); } void Foo3() { Int = (int)this.Payload; this.Assert(Int == 1); } void Foo4() { Int = ((this.Payload as Tuple<Dictionary<int, int>, List<bool>>).Item1 as Dictionary<int, int>)[0]; this.Assert(Int == 1); Bool = ((this.Payload as Tuple<Dictionary<int, int>, List<bool>>).Item2 as List<bool>)[2]; this.Assert(Bool == true); } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(MachOS)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; sctConfig.SchedulingIterations = 100; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(0, sctEngine.NumOfFoundBugs); }
public void TestTwoMachines10() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E1 : Event { public E1() : base(1, -1) { } } class E2 : Event { public E2() : base(1, -1) { } } class Real1 : Machine { bool test = false; MachineId mac; [Start] [OnEntry(nameof(EntryInit))] [OnExit(nameof(ExitInit))] [OnEventGotoState(typeof(Default), typeof(S1))] [OnEventDoAction(typeof(E1), nameof(Action1))] class Init : MachineState { } void EntryInit() { mac = this.CreateMachine(typeof(Real2), this.Id); this.Raise(new E1()); } void ExitInit() { this.Send(mac, new E2(), test); } class S1 : MachineState { } void Action1() { test = true; } } class Real2 : Machine { [Start] [OnEntry(nameof(EntryInit))] [OnEventDoAction(typeof(E2), nameof(EntryAction))] class Init : MachineState { } void EntryInit() { } void EntryAction() { if (this.Trigger == typeof(E2)) { Action2(); } else { //this.Assert(false); // unreachable } } void Action2() { this.Assert((bool)this.Payload == false); // reachable } } class M : Monitor { [Start] [OnEntry(nameof(EntryX))] class X : MonitorState { } void EntryX() { //this.Assert((bool)this.Payload == true); // reachable } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Real1)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestNewMonitor2() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E2 : Event { public E2() : base(1, -1) { } } class Real1 : Machine { bool test = false; [Start] [OnEntry(nameof(EntryInit))] class Init : MachineState { } void EntryInit() { this.CreateMonitor(typeof(M), true); this.Monitor<M>(null, test); } } class M : Monitor { [Start] [OnEntry(nameof(EntryX))] class X : MonitorState { } void EntryX() { //this.Assert((bool)this.Payload == true); // passes } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Real1)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestMaxInstances1AssertionFailure() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E1 : Event { public E1() : base(1, -1) { } } class E2 : Event { public E2() : base(1, -1) { } } class E3 : Event { public E3() : base(-1, -1) { } } class E4 : Event { } class Unit : Event { public Unit() : base(1, -1) { } } class RealMachine : Machine { MachineId GhostMachine; [Start] [OnEntry(nameof(EntryInit))] [OnEventPushState(typeof(Unit), typeof(S1))] [OnEventGotoState(typeof(E4), typeof(S2))] [OnEventDoAction(typeof(E2), nameof(Action1))] class Init : MachineState { } void EntryInit() { GhostMachine = this.CreateMachine(typeof(GhostMachine), this.Id); this.Raise(new Unit()); } [OnEntry(nameof(EntryS1))] class S1 : MachineState { } void EntryS1() { this.Send(GhostMachine, new E1()); this.Send(GhostMachine, new E1()); // error } [OnEntry(nameof(EntryS2))] [OnEventGotoState(typeof(Unit), typeof(S3))] class S2 : MachineState { } void EntryS2() { this.Raise(new Unit()); } [OnEventGotoState(typeof(E4), typeof(S3))] class S3 : MachineState { } void Action1() { this.Assert((int)this.Payload == 100); this.Send(GhostMachine, new E3()); this.Send(GhostMachine, new E3()); } } class GhostMachine : Machine { MachineId RealMachine; [Start] [OnEntry(nameof(EntryInit))] [OnEventGotoState(typeof(Unit), typeof(GhostInit))] class Init : MachineState { } void EntryInit() { RealMachine = this.Payload as MachineId; this.Raise(new Unit()); } [OnEventGotoState(typeof(E1), typeof(S1))] class GhostInit : MachineState { } [OnEntry(nameof(EntryS1))] [OnEventGotoState(typeof(E3), typeof(S2))] [IgnoreEvents(typeof(E1))] class S1 : MachineState { } void EntryS1() { this.Send(RealMachine, new E2(), 100); } [OnEntry(nameof(EntryS2))] [OnEventGotoState(typeof(E3), typeof(GhostInit))] class S2 : MachineState { } void EntryS2() { this.Send(RealMachine, new E4()); this.Send(RealMachine, new E4()); this.Send(RealMachine, new E4()); } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(RealMachine)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; sctConfig.SchedulingStrategy = SchedulingStrategy.DFS; sctConfig.DepthBound = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestPush() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E1 : Event { public E1() : base(1, -1) { } } class E2 : Event { public E2() : base(1, -1) { } } class Real1 : Machine { [Start] [OnEntry(nameof(EntryInit))] [OnExit(nameof(ExitInit))] [OnEventGotoState(typeof(E1), typeof(Init))] [OnEventPushState(typeof(E2), typeof(S1))] class Init : MachineState { } void EntryInit() { this.Send(this.Id, new E1()); } void ExitInit() { this.Send(this.Id, new E2()); } [OnEntry(nameof(EntryS1))] class S1 : MachineState { } void EntryS1() { this.Assert(false); // reachable } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Real1)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }
public void TestExitAtExplicitPop() { var test = @" using System; using Microsoft.PSharp; namespace SystematicTesting { class E : Event { } class Real1 : Machine { bool test = false; [Start] [OnEntry(nameof(EntryInit))] [OnExit(nameof(ExitInit))] [OnEventPushState(typeof(E), typeof(Call))] class Init : MachineState { } void EntryInit() { this.Raise(new E()); } void ExitInit() { } [OnEntry(nameof(EntryCall))] [OnExit(nameof(ExitCall))] class Call : MachineState { } void EntryCall() { this.Pop(); } void ExitCall() { this.Assert(false); } } public static class TestProgram { public static void Main(string[] args) { TestProgram.Execute(); Console.ReadLine(); } [Test] public static void Execute() { PSharpRuntime.CreateMachine(typeof(Real1)); } } }"; var parser = new CSharpParser(new PSharpProject(), SyntaxFactory.ParseSyntaxTree(test), true); var program = parser.Parse(); program.Rewrite(); var sctConfig = new DynamicAnalysisConfiguration(); sctConfig.SuppressTrace = true; sctConfig.Verbose = 2; var assembly = base.GetAssembly(program.GetSyntaxTree()); var context = AnalysisContext.Create(sctConfig, assembly); var sctEngine = SCTEngine.Create(context).Run(); Assert.AreEqual(1, sctEngine.NumOfFoundBugs); }