/// <summary>
        /// Tests the given P# assembly.
        /// </summary>
        /// <param name="dll">Assembly</param>
        private void TestAssembly(string dll)
        {
            // Create a P# dynamic analysis context.
            var context = AnalysisContext.Create(this.Configuration, dll);

            // Creates and runs the systematic testing engine
            // to find bugs in the P# program.
            SCTEngine.Create(context).Run();
        }
Exemplo n.º 2
0
        public void TestSendInterleavingsAssertionFailure()
        {
            var test = @"
using System;
using Microsoft.PSharp;

namespace SystematicTesting
{
    class Event1 : Event { }
    class Event2 : Event { }

    class Receiver : Machine
    {
        [Start]
        [OnEntry(nameof(Initialize))]
        [OnEventDoAction(typeof(Event1), nameof(OnEvent1))]
        [OnEventDoAction(typeof(Event2), nameof(OnEvent2))]
        class Init : MachineState { }

        int count1 = 0;
        void Initialize()
        {
            CreateMachine(typeof(Sender1), this.Id);
            CreateMachine(typeof(Sender2), this.Id);
        }

        void OnEvent1()
        {
            count1++;
        }
        void OnEvent2()
        {
            Assert(count1 != 1);
        }
    }

    class Sender1 : Machine
    {
        [Start]
        [OnEntry(nameof(Run))]
        class State : MachineState { }

        void Run()
        {
            Send((MachineId)Payload, new Event1());
            Send((MachineId)Payload, new Event1());
        }
    }

    class Sender2 : Machine
    {
        [Start]
        [OnEntry(nameof(Run))]
        class State : MachineState { }

        void Run()
        {
            Send((MachineId)Payload, new Event2());
        }
    }

    public static class TestProgram
    {
        public static void Main(string[] args)
        {
            TestProgram.Execute();
            Console.ReadLine();
        }

        [Test]
        public static void Execute()
        {
            PSharpRuntime.CreateMachine(typeof(Receiver));
        }
    }
}";

            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 = 19;

            var assembly  = base.GetAssembly(program.GetSyntaxTree());
            var context   = AnalysisContext.Create(sctConfig, assembly);
            var sctEngine = SCTEngine.Create(context).Run();

            Assert.AreEqual(1, sctEngine.NumOfFoundBugs);
        }
Exemplo n.º 3
0
        public void TestLiveness3()
        {
            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.CreateMachine(typeof(Loop));
            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(HandleEvent))]
        class HandleEvent : MachineState { }

        void HandleEventOnEntry()
        {
            this.Monitor<WatchDog>(new Computing());
        }
    }

    class Loop : Machine
    {
        [Start]
        [OnEntry(nameof(LoopingOnEntry))]
        [OnEventGotoState(typeof(Done), typeof(Looping))]
        class Looping : MachineState { }

		void LoopingOnEntry()
        {
            this.Send(this.Id, new Done());
        }
    }

    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.SchedulingIterations = 100;

            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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public void TestSimpleTaskFail()
        {
            var test = @"
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.PSharp;

namespace SystematicTesting
{
    class Unit : Event { }

    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()
        {
            Task.Factory.StartNew(() =>
            {
                this.Value++;
            });

            this.Assert(this.Value == 0, ""Value is '{0}' (expected '0')."", this.Value);
        }
    }

    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.SchedulingStrategy              = SchedulingStrategy.DFS;
            sctConfig.SchedulingIterations            = 2;
            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 '1' (expected '0').";

            Assert.AreEqual(bugReport, sctEngine.BugReport);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        public void TestMaxInstances2()
        {
            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());
        }

        [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;

            var assembly  = base.GetAssembly(program.GetSyntaxTree());
            var context   = AnalysisContext.Create(sctConfig, assembly);
            var sctEngine = SCTEngine.Create(context).Run();

            Assert.AreEqual(0, sctEngine.NumOfFoundBugs);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        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);
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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);
        }
Exemplo n.º 15
0
        public void TestActions1Fail()
        {
            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;
        bool test = false;

        [Start]
        [OnEntry(nameof(EntryInit))]
        [OnExit(nameof(ExitInit))]
        [OnEventGotoState(typeof(E2), typeof(S1))] // exit actions are performed before transition to S1
        [OnEventDoAction(typeof(E4), nameof(Action1))] // E4, E3 have no effect on reachability of assert(false)
        class Init : MachineState { }

        void EntryInit()
        {
            GhostMachine = this.CreateMachine(typeof(Ghost), this.Id);
            this.Send(GhostMachine, new E1());
        }

        void ExitInit()
        {
            test = true;
        }

        [OnEntry(nameof(EntryS1))]
        [OnEventGotoState(typeof(Unit), typeof(S2))]
        class S1 : MachineState { }

        void EntryS1()
        {
            this.Assert(test == true); // holds
            this.Raise(new Unit());
        }

        [OnEntry(nameof(EntryS2))]
        class S2 : MachineState { }

        void EntryS2()
        {
            // this assert is reachable: Real -E1-> Ghost -E2-> Real;
            // then Real_S1 (assert holds), Real_S2 (assert fails)
            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 E4());
            this.Send(RealMachine, new E2());
        }

        class S2 : MachineState { }
    }

    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);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
0
        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);
        }
Exemplo n.º 18
0
        public void TestNewInExit()
        {
            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;
        bool test = false;

        [Start]
        [OnEntry(nameof(EntryInit))]
        [OnExit(nameof(ExitInit))]
        [OnEventGotoState(typeof(E2), typeof(S1))]
        class Init : MachineState { }

        void EntryInit()
        {
            this.Raise(new E2());
        }

        void ExitInit()
        {
            test = true;
            GhostMachine = this.CreateMachine(typeof(Ghost), this.Id);
        }

        [OnEntry(nameof(EntryS1))]
        class S1 : MachineState { }

        void EntryS1()
        {
            this.Send(GhostMachine, new E1());
        }
    }

    class Ghost : Machine
    {
        MachineId RealMachine;

        [Start]
        [OnEntry(nameof(EntryInit))]
        [OnEventDoAction(typeof(E1), nameof(Action))]
        class Init : MachineState { }

        void EntryInit()
        {
            RealMachine = this.Payload as MachineId;
        }

        void Action()
        {
            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(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;

            var assembly  = base.GetAssembly(program.GetSyntaxTree());
            var context   = AnalysisContext.Create(sctConfig, assembly);
            var sctEngine = SCTEngine.Create(context).Run();

            Assert.AreEqual(1, sctEngine.NumOfFoundBugs);
        }
Exemplo n.º 19
0
        public void TestEventSentAfterSentHaltHandled()
        {
            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 PingIgnored : Event { }

    class PING : Machine
    {
        MachineId PongId;
        int Count;

        [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()
        {
            Count = Count + 1;
            if (Count == 1)
            {
                this.Send(PongId, new Ping(), this.Id);
            }
            // halt PONG after one exchange
            if (Count == 2)
            {
                this.Send(PongId, new Halt());
                this.Send(PongId, new PingIgnored());
            }

            this.Raise(new Success());
        }

        [OnEventGotoState(typeof(Pong), typeof(SendPing))]
        class WaitPong : MachineState { }

        class Done : MachineState { }
    }

    class PONG : Machine
    {
        [Start]
        [OnEventGotoState(typeof(Ping), typeof(SendPong))]
        [OnEventGotoState(typeof(Halt), typeof(PongHalt))]
        class WaitPing : MachineState { }

        [OnEntry(nameof(EntrySendPong))]
        [OnEventGotoState(typeof(Success), typeof(WaitPing))]
        class SendPong : MachineState { }

        void EntrySendPong()
        {
            this.Send(this.Payload as MachineId, new Pong());
            this.Raise(new Success());
        }

        [OnEventDoAction(typeof(PingIgnored), nameof(Action1))]
        [IgnoreEvents(typeof(Ping))]
        class PongHalt : MachineState { }

        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);
        }
Exemplo n.º 20
0
        public void TestWarmState()
        {
            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))]
        class HandleEvent : MachineState { }

        void HandleEventOnEntry()
        {
            this.Monitor<WatchDog>(new Computing());
        }
    }

    class WatchDog : Monitor
    {
        List<MachineId> Workers;

        [Start]
        [OnEventGotoState(typeof(Waiting), typeof(CanGetUserInput))]
        [OnEventGotoState(typeof(Computing), typeof(CannotGetUserInput))]
        class CanGetUserInput : MonitorState { }
        
        [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.SchedulingStrategy = SchedulingStrategy.DFS;

            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);
        }
Exemplo n.º 21
0
        public void TestRaiseHaltHandled()
        {
            var test = @"
using System;
using Microsoft.PSharp;

namespace SystematicTesting
{
    class E1 : Event {
        public E1() : base(1, -1) { }
    }

    class Real1 : Machine
    {
        bool test = false;

        [Start]
        [OnEntry(nameof(EntryInit))]
        [OnExit(nameof(ExitInit))]
        [OnEventPushState(typeof(Halt), typeof(S1))]
        [OnEventDoAction(typeof(E1), nameof(Action2))]
        class Init : MachineState { }

        void EntryInit()
        {
            this.Send(this.Id, new E1());
            this.Raise(new Halt());
        }

        void ExitInit() { }

        [OnEntry(nameof(EntryS1))]
        class S1 : MachineState { }

        void EntryS1()
        {
            test = true;
        }

        void Action2()
        {
            this.Assert(test == 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);
        }
Exemplo n.º 22
0
        public void TestNullTriggerPayload()
        {
            var test = @"
using System;
using Microsoft.PSharp;

namespace SystematicTesting
{
    class Real1 : Machine
    {
        bool test = false;

        [Start]
        [OnEntry(nameof(EntryInit))]
        [OnExit(nameof(ExitInit))]
        [OnEventGotoState(typeof(Default), typeof(S1))]
        class Init : MachineState { }

        void EntryInit() { }

        void ExitInit() { }

        [OnEntry(nameof(EntryS1))]
        class S1 : MachineState { }

        void EntryS1()
        {
            this.Assert(this.Payload != null || this.Trigger != typeof(Default)); // 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);
        }
Exemplo n.º 23
0
        public void TestNewMonitor1()
        {
            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));
            this.Monitor<M>(null, test);
        }
    }

    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);
        }
Exemplo n.º 24
0
        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);
        }