예제 #1
0
        private static BoogieMethodData MethodDataFromImpl(
            Implementation impl,
            BoogieGlobalData globalData,
            List <Variable> extraLocalVariables = null
            )
        {
            //add out params to local variables for now
            var locals = new List <Variable>(impl.LocVars).Union(impl.OutParams);

            if (extraLocalVariables != null)
            {
                locals = locals.Union(extraLocalVariables);
            }


            /* procedures and implementations do not use the same objects for the variables in the spec --> need to sync
             * for pre- and postcondition
             */
            var formalProcImplSubst = Substituter.SubstitutionFromDictionary(impl.GetImplFormalMap());
            var preconditions       = new List <Tuple <Expr, bool> >();

            foreach (var req in impl.Proc.Requires)
            {
                var e = Substituter.Apply(formalProcImplSubst, req.Condition);
                preconditions.Add(Tuple.Create(e, req.Free));
            }

            var postconditions = new List <Tuple <Expr, bool> >();

            foreach (var ens in impl.Proc.Ensures)
            {
                var e = Substituter.Apply(formalProcImplSubst, ens.Condition);
                postconditions.Add(Tuple.Create(e, ens.Free));
            }


            return(new BoogieMethodData(
                       globalData,
                       new List <TypeVariable>(impl.TypeParameters),
                       new List <Variable>(impl.InParams),
                       locals,
                       null,
                       new List <IdentifierExpr>(impl.Proc.Modifies),
                       preconditions,
                       postconditions));
        }
예제 #2
0
        /// <summary>
        /// Compute and apply the invariants for the program using the underlying abstract domain.
        /// </summary>
        public static void ComputeProgramInvariants(Program program,
                                                    Dictionary <Procedure, Implementation[]> procedureImplementations, NativeLattice lattice)
        {
            Contract.Requires(program != null);
            Contract.Requires(procedureImplementations != null);
            Contract.Requires(lattice != null);

            // Gather all the axioms to create the initial lattice element
            // Differently stated, it is the \alpha from axioms (i.e. first order formulae) to the underlyng abstract domain
            var initialElement = lattice.Top;

            Contract.Assert(initialElement != null);
            foreach (var ax in program.Axioms)
            {
                initialElement = lattice.Constrain(initialElement, ax.Expr);
            }

            // analyze each procedure
            foreach (var proc in program.Procedures)
            {
                if (procedureImplementations.ContainsKey(proc))
                {
                    // analyze each implementation of the procedure
                    foreach (var impl in procedureImplementations[proc])
                    {
                        // add the precondition to the axioms
                        Substitution formalProcImplSubst = Substituter.SubstitutionFromDictionary(impl.GetImplFormalMap());
                        var          start = initialElement;
                        foreach (Requires pre in proc.Requires)
                        {
                            Expr e = Substituter.Apply(formalProcImplSubst, pre.Condition);
                            start = lattice.Constrain(start, e);
                        }

                        lattice.Specialize(impl);
                        Analyze(impl, lattice, start);
                        lattice.Specialize(null);
                    }
                }
            }
        }