Exemplo n.º 1
0
        public Z3BoolExpr MkTest(Z3Expr t, Term type)
        {
            Contract.Assert(t != null && t.Sort.Equals(Representation));

            Term intr;
            var  unn = Owner.GetIntersection(Type, type, out intr);

            if (intr == null)
            {
                return(Context.MkFalse());
            }
            else if (intr == Type)
            {
                return(Context.MkTrue());
            }

            Z3BoolExpr test = null;
            var        nat  = (Z3IntExpr)UnboxingFun.Apply(t);

            if (unn.Contains(Index.SymbolTable.GetSortSymbol(BaseSortKind.PosInteger)))
            {
                test = test.Or(Context, nat.NEq(Context, Context.MkInt(0)));
            }

            var iNat = MkIntCoercion(t);

            foreach (var r in unn.RangeMembers)
            {
                test = test.Or(
                    Context,
                    iNat.Ge(
                        Context,
                        Context.MkInt(r.Key.ToString())).And(
                        Context,
                        iNat.Le(Context, Context.MkInt(r.Value.ToString()))));
            }

            return(test);
        }
Exemplo n.º 2
0
        public Z3Expr MkCoercion(Z3Expr t)
        {
            var srcTE = Owner.GetEmbedding(t.Sort);

            if (srcTE == this)
            {
                return(t);
            }

            Term intr;
            var  unn = Owner.GetIntersection(srcTE.GetSubtype(t), Type, out intr);

            if (unn == null)
            {
                return(DefaultMember.Item2);
            }

            switch (srcTE.Kind)
            {
            case TypeEmbeddingKind.Enum:
                return(MkCoercion(t, unn, (EnumEmbedding)srcTE));

            case TypeEmbeddingKind.Real:
                return(MkCoercion(t, unn, (RealEmbedding)srcTE));

            case TypeEmbeddingKind.Singleton:
                return(MkCoercion(t, unn, (SingletonEmbedding)srcTE));

            case TypeEmbeddingKind.String:
                return(MkCoercion(t, unn, (StringEmbedding)srcTE));

            case TypeEmbeddingKind.Union:
                return(MkCoercion(t, unn, (UnionEmbedding)srcTE));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 3
0
        public Term MkGround(Z3Expr t, Term[] args)
        {
            Contract.Assert(t != null);
            Contract.Assert(args == null || args.Length == 0);
            Contract.Assert(t.FuncDecl.Equals(BoxingFun));
            var  n = ((Z3IntNum)t.Args[0]).BigInteger;
            bool wasAdded;

            if (n.IsZero)
            {
                return(Index.MkCnst(Rational.One, out wasAdded));
            }
            else if (n.Sign > 0)
            {
                n = 2 * n;
                return(Index.MkCnst(new Rational(n, System.Numerics.BigInteger.One), out wasAdded));
            }
            else
            {
                n = (-2 * n) + 1;
                return(Index.MkCnst(new Rational(n, System.Numerics.BigInteger.One), out wasAdded));
            }
        }
Exemplo n.º 4
0
        public void Test_Runner_Mem_3()
        {
            Tools tools = this.CreateTools(); // test is slow (9min - 17min)

            tools.StateConfig.Set_All_Off();
            tools.StateConfig.RAX = true;
            tools.StateConfig.RBX = true;
            tools.StateConfig.RCX = true;
            tools.StateConfig.Mem = true;
            tools.Quiet           = true;
            bool logToDisplay2 = false;// logToDisplay;

            string programStr =
                "           mov     qword ptr[0], 10            " + Environment.NewLine +
                "           mov     qword ptr[10], 20           " + Environment.NewLine +
                "           mov     rbx, qword ptr [rax]        " + Environment.NewLine +
                "           mov     rcx, qword ptr [rbx]        ";

            StaticFlow sFlow = new StaticFlow(tools);

            sFlow.Update(programStr);
            DynamicFlow dFlow  = Runner.Construct_DynamicFlow_Backward(sFlow, tools);
            State       state0 = dFlow.Create_States_Before(0, 0);
            State       state  = dFlow.EndState;

            Microsoft.Z3.Expr rax = state0.Create(Rn.RAX).Translate(state.Ctx);
            state.Frozen = false;
            state.Add(new BranchInfo(state.Ctx.MkEq(rax, state.Ctx.MkBV(0, 64)), true));
            if (logToDisplay2)
            {
                Console.WriteLine("Forward:\n" + state);
            }

            TestTools.AreEqual(Rn.RAX, 0, state);
            TestTools.AreEqual(Rn.RBX, 10, state);
            TestTools.AreEqual(Rn.RCX, 20, state);
        }
Exemplo n.º 5
0
        public Z3BoolExpr MkTest(Z3Expr t, Term type)
        {
            Term intr;
            var  unn = Owner.GetIntersection(Type, type, out intr);
            var  st  = (Z3Expr)t;

            if (intr == null)
            {
                return(Context.MkFalse());
            }
            else if (intr == Type)
            {
                return(Context.MkTrue());
            }


            Z3BoolExpr   test = null;
            BaseCnstSymb bc;

            foreach (var e in unn.NonRangeMembers)
            {
                if (e.Kind != SymbolKind.BaseCnstSymb)
                {
                    continue;
                }

                bc = (BaseCnstSymb)e;
                if (bc.CnstKind != CnstKind.String)
                {
                    continue;
                }

                test = test.Or(Context, st.Eq(Context, MkGround(e, null)));
            }

            return(test);
        }
Exemplo n.º 6
0
        public Z3Expr MkCoercion(Z3Expr t)
        {
            var srcTE = Owner.GetEmbedding(t.Sort);

            if (srcTE == this)
            {
                return(t);
            }

            Term intr;
            var  unn = Owner.GetIntersection(srcTE.GetSubtype(t), Type, out intr);

            if (unn == null)
            {
                return(DefaultMember.Item2);
            }

            switch (srcTE.Kind)
            {
            case TypeEmbeddingKind.Enum:
            case TypeEmbeddingKind.Natural:
            case TypeEmbeddingKind.Integer:
            case TypeEmbeddingKind.PosInteger:
            case TypeEmbeddingKind.NegInteger:
            case TypeEmbeddingKind.IntRange:
            case TypeEmbeddingKind.Real:
            case TypeEmbeddingKind.String:
            case TypeEmbeddingKind.Singleton:
            case TypeEmbeddingKind.Constructor:
            case TypeEmbeddingKind.Union:
                return(MkCoercion(t, unn, srcTE));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 7
0
 public Term GetSubtype(Z3Expr t)
 {
     Contract.Assert(t != null && t.Sort.Equals(Representation));
     return(Type);
 }
Exemplo n.º 8
0
 public Term MkGround(Z3Expr t, Term[] args)
 {
     Contract.Assert(args == null || args.Length == 0);
     return(Value);
 }
Exemplo n.º 9
0
 public Z3BoolExpr MkTest(Z3Expr t, Term type)
 {
     return(Index.IsGroundMember(type, Value) ? Context.MkTrue() : Context.MkFalse());
 }
Exemplo n.º 10
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, RealEmbedding te)
 {
     return(MkPosCoercion(Context.MkReal2Int((Z3.RealExpr)t)));
 }
Exemplo n.º 11
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te)
 {
     return(MkPosCoercion(te.MkIntCoercion(t)));
 }
Exemplo n.º 12
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te)
 {
     return(Context.MkInt(((Rational)((BaseCnstSymb)te.Value.Symbol).Raw).Numerator.ToString()));
 }
Exemplo n.º 13
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te)
 {
     return(MkGround(te.Value.Symbol, null));
 }
Exemplo n.º 14
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, SingletonEmbedding te)
        {
            var val = ((Rational)((BaseCnstSymb)te.Value.Symbol).Raw).Numerator - Lower;

            return(BoxingFun.Apply(Context.MkBV(val.ToString(), bvSort.Size)));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Returns an encoding of this term, possibly after applying some normalizing rewrites.
        /// </summary>
        public Z3Expr GetTerm(Term t, out Term normalizedTerm)
        {
            Contract.Requires(t != null);
            normalizedTerm = Normalize(t);
            Z3Expr enc, encp;

            if (encodings.TryFindValue(normalizedTerm, out enc))
            {
                return(enc);
            }

            int                  i;
            bool                 wasAdded;
            ITypeEmbedding       typEmb;
            ConstructorEmbedding conEmb;

            return(normalizedTerm.Compute <Z3Expr>(
                       (x, s) =>
            {
                if (encodings.ContainsKey(x) || x.Groundness == Groundness.Ground)
                {
                    return null;
                }
                else
                {
                    return x.Args;
                }
            },
                       (x, ch, s) =>
            {
                if (encodings.TryFindValue(x, out encp))
                {
                    return encp;
                }
                else if (x.Groundness == Groundness.Ground)
                {
                    typEmb = Solver.TypeEmbedder.ChooseRepresentation(x);
                    encp = Solver.TypeEmbedder.MkGround(x, typEmb);
                    encodings.Add(x, encp);
                    return encp;
                }

                //// x must be non-ground. Because variables are already encoded, then x should not be a variable
                Contract.Assert(!x.Symbol.IsVariable);
                if (x.Symbol.IsDataConstructor)
                {
                    if (x.Symbol.Kind == SymbolKind.ConSymb)
                    {
                        conEmb = (ConstructorEmbedding)Solver.TypeEmbedder.GetEmbedding(
                            Solver.Index.MkApply(((ConSymb)x.Symbol).SortSymbol, TermIndex.EmptyArgs, out wasAdded));
                    }
                    else
                    {
                        conEmb = (ConstructorEmbedding)Solver.TypeEmbedder.GetEmbedding(
                            Solver.Index.MkApply(((MapSymb)x.Symbol).SortSymbol, TermIndex.EmptyArgs, out wasAdded));
                    }

                    i = 0;
                    var args = new Z3Expr[x.Symbol.Arity];
                    foreach (var a in ch)
                    {
                        typEmb = Solver.TypeEmbedder.GetEmbedding(conEmb.Z3Constructor.ConstructorDecl.Domain[i]);
                        args[i++] = typEmb.MkCoercion(a);
                    }

                    encp = conEmb.MkGround(x.Symbol, args);
                    encodings.Add(x, encp);
                    return encp;
                }
                else
                {
                    throw new NotImplementedException();
                }
            }));
        }
Exemplo n.º 16
0
 public static Z3BoolExpr NEq(this Z3Expr expr1, Z3Context context, Z3Expr expr2)
 {
     Contract.Requires(expr1 != null && expr2 != null);
     return(context.MkNot(context.MkEq(expr1, expr2)));
 }
Exemplo n.º 17
0
 public static Z3Expr Ite(this Z3BoolExpr cond, Z3Context context, Z3Expr ifTrue, Z3Expr ifFalse)
 {
     Contract.Assert(cond != null && ifTrue != null && ifFalse != null);
     return(context.MkITE(cond, ifTrue, ifFalse));
 }
Exemplo n.º 18
0
 public Term MkGround(Z3Expr t, Term[] args)
 {
     Contract.Assert(t != null && t.FuncDecl.Range == Representation);
     Contract.Assert(args != null && args.Length == 1);
     return(args[0]);
 }
Exemplo n.º 19
0
        public Z3BoolExpr MkTest(Z3Expr t, Term type)
        {
            ///// Produces a test of the form.
            ////  ite(isBox_T(t), Test_T(t), ...).

            Term intr;
            var  unn = Owner.GetIntersection(Type, type, out intr);

            if (intr == null)
            {
                return(Context.MkFalse());
            }
            else if (intr == Type)
            {
                return(Context.MkTrue());
            }

            bool                       wasAdded;
            Z3Con                      boxCon;
            Z3Fun                      unbox, tester;
            Z3BoolExpr                 boxTest;
            ITypeEmbedding             te;
            Term                       subtypeTrm;
            MutableTuple <Z3Con, Term> subtype;
            Z3BoolExpr                 iteTest = Context.MkFalse();

            //// For non-user sorts, want to project the type along the boxed type, so the boxed type
            //// can decide how to encode the test of this projection.
            Map <Z3Sort, MutableTuple <Z3Con, Term> > subtypes =
                new Map <Z3Sort, MutableTuple <Z3Con, Term> >((x, y) => ((int)x.Id) - ((int)y.Id));

            foreach (var s in unn.NonRangeMembers)
            {
                boxCon = GetBoxer(s);
                Contract.Assert(boxCon != null);
                unbox = boxCon.AccessorDecls[0];

                if (s.Kind == SymbolKind.UserSortSymb)
                {
                    tester     = boxCon.TesterDecl;
                    te         = Owner.GetEmbedding(unbox.Range);
                    boxTest    = (Z3BoolExpr)tester.Apply(t);
                    subtypeTrm = Index.MkApply(s, TermIndex.EmptyArgs, out wasAdded);
                    iteTest    = (Z3BoolExpr)boxTest.Ite(Context, te.MkTest(unbox.Apply(t), subtypeTrm), iteTest);
                }
                else
                {
                    if (!subtypes.TryFindValue(unbox.Range, out subtype))
                    {
                        subtype = new MutableTuple <Z3Con, Term>(
                            boxCon,
                            Index.MkApply(s, TermIndex.EmptyArgs, out wasAdded));
                        subtypes.Add(unbox.Range, subtype);
                    }
                    else
                    {
                        subtype.Item2 = Index.MkApply(
                            Index.TypeUnionSymbol,
                            new Term[] { Index.MkApply(s, TermIndex.EmptyArgs, out wasAdded), subtype.Item2 },
                            out wasAdded);
                    }
                }
            }

            foreach (var kv in unn.RangeMembers)
            {
                subtypeTrm = Index.MkApply(
                    Index.RangeSymbol,
                    new Term[]
                {
                    Index.MkCnst(new Rational(kv.Key, BigInteger.One), out wasAdded),
                    Index.MkCnst(new Rational(kv.Value, BigInteger.One), out wasAdded)
                },
                    out wasAdded);

                foreach (var bc in GetBoxers(kv.Key, kv.Value))
                {
                    unbox = bc.AccessorDecls[0];
                    if (!subtypes.TryFindValue(unbox.Range, out subtype))
                    {
                        subtype = new MutableTuple <Z3Con, Term>(bc, subtypeTrm);
                        subtypes.Add(unbox.Range, subtype);
                    }
                    else
                    {
                        subtype.Item2 = Index.MkApply(
                            Index.TypeUnionSymbol,
                            new Term[] { subtypeTrm, subtype.Item2 },
                            out wasAdded);
                    }
                }
            }

            foreach (var kv in subtypes)
            {
                boxCon = kv.Value.Item1;
                Contract.Assert(boxCon != null);
                unbox = boxCon.AccessorDecls[0];

                tester  = boxCon.TesterDecl;
                te      = Owner.GetEmbedding(unbox.Range);
                boxTest = (Z3BoolExpr)tester.Apply(t);
                iteTest = (Z3BoolExpr)boxTest.Ite(Context, te.MkTest(unbox.Apply(t), kv.Value.Item2), iteTest);
            }

            return(iteTest);
        }
Exemplo n.º 20
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te)
 {
     return(CreationFun.Apply());
 }
Exemplo n.º 21
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntegerEmbedding te)
 {
     return(Context.MkInt2Real((Z3.IntExpr)t));
 }
Exemplo n.º 22
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntegerEmbedding te)
 {
     return(MkPosCoercion((Z3IntExpr)t));
 }
Exemplo n.º 23
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, RealEmbedding te)
 {
     return(BoxingFun.Apply(
                ((Z3IntExpr)Context.MkSub(Context.MkReal2Int((Z3RealExpr)t), Context.MkInt(Lower.ToString()))).Int2BV(Context, bvSort.Size)));
 }
Exemplo n.º 24
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, NegIntegerEmbedding te)
 {
     return(te.MkIntCoercion(t));
 }
Exemplo n.º 25
0
        private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, UnionEmbedding te)
        {
            Z3Expr unbox;
            //// If the union contains real, then it cannot contain other integral numerics.
            var test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Real), t, out unbox);

            if (test != null)
            {
                return(test.Ite(
                           Context,
                           MkCoercion(unbox, unn, (RealEmbedding)Owner.GetEmbedding(BaseSortKind.Real)),
                           DefaultMember.Item2));
            }

            //// If the union contains int, then it cannot contain other integral numerics.
            test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Integer), t, out unbox);
            if (test != null)
            {
                return(test.Ite(
                           Context,
                           MkCoercion(unbox, unn, (IntegerEmbedding)Owner.GetEmbedding(BaseSortKind.Integer)),
                           DefaultMember.Item2));
            }

            //// Otherwise unn may contain subsorts of Integer or integer ranges.
            //// First handle the base subsorts
            var coercions = DefaultMember.Item2;

            test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.Natural), t, out unbox);
            if (test != null)
            {
                coercions = test.Ite(
                    Context,
                    MkCoercion(unbox, unn, (NaturalEmbedding)Owner.GetEmbedding(BaseSortKind.Natural)),
                    coercions);
            }

            test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.PosInteger), t, out unbox);
            if (test != null)
            {
                coercions = test.Ite(
                    Context,
                    MkCoercion(unbox, unn, (PosIntegerEmbedding)Owner.GetEmbedding(BaseSortKind.PosInteger)),
                    coercions);
            }

            test = te.MkTestAndUnbox(Index.SymbolTable.GetSortSymbol(BaseSortKind.NegInteger), t, out unbox);
            if (test != null)
            {
                coercions = test.Ite(
                    Context,
                    MkCoercion(unbox, unn, (NegIntegerEmbedding)Owner.GetEmbedding(BaseSortKind.NegInteger)),
                    coercions);
            }

            //// Additionally, there may be integer ranges.
            Z3BoolExpr[]   tests;
            Z3Expr[]       unboxeds;
            ITypeEmbedding tep;

            foreach (var kv in unn.RangeMembers)
            {
                tests = te.MkTestAndUnbox(kv.Key, kv.Value, t, out unboxeds);
                for (int i = 0; i < tests.Length; ++i)
                {
                    test  = tests[i];
                    unbox = unboxeds[i];
                    tep   = Owner.GetEmbedding(unbox.Sort);
                    switch (tep.Kind)
                    {
                    case TypeEmbeddingKind.IntRange:
                        coercions = test.Ite(
                            Context,
                            MkCoercion(unbox, unn, (IntRangeEmbedding)tep),
                            coercions);
                        break;

                    case TypeEmbeddingKind.Singleton:
                        coercions = test.Ite(
                            Context,
                            MkCoercion(unbox, unn, (SingletonEmbedding)tep),
                            coercions);
                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }

            return(coercions);
        }
Exemplo n.º 26
0
 private Z3Expr MkCoercion(Z3Expr t, AppFreeCanUnn unn, IntRangeEmbedding te)
 {
     return(Context.MkInt2Real(te.MkIntCoercion(t)));
 }