Esempio n. 1
0
        public static void ProcessDiffInlineProc(List <Expr> constraints, VerificationTask vt, Program prog)
        {
            var eqProcName = vt.Eq.Name;

            eqProcName = eqProcName.Replace("EQ_", "");
            int index      = eqProcName.IndexOf("__xx__");
            var procNameV1 = eqProcName.Substring(0, index);
            var procNameV2 = eqProcName.Substring(index + 6);
        }
Esempio n. 2
0
        public static List <VerificationTask> Parse(string filename, Dictionary <string, Declaration> progDict)
        {
            try
            {
                var in_s = new StreamReader(filename);

                var vts = new List <VerificationTask>();

                string l;
                while (!in_s.EndOfStream)
                {
                    l = in_s.ReadLine().Trim();
                    if (l.StartsWith("#"))
                    {
                        continue;
                    }
                    if (l.StartsWith("vtask:"))
                    {
                        l = l.Substring(6).Replace("(", "").Replace(")", "").Replace(" ", "");
                        string[] sts = l.Split('/');
                        var      trp = sts[0].Split(',');
                        var      vt  = new VerificationTask(progDict.Get(trp[0] + "$IMPL") as Implementation,
                                                            progDict.Get(trp[1] + "$IMPL") as Implementation,
                                                            progDict.Get(trp[2] + "$IMPL") as Implementation);
                        if (sts.Length > 1)
                        {
                            vt.DesiredOutputVars =
                                sts[1].Trim().Replace(" ", "").Split(';').Map(x => progDict.Get(x + "$VAR")).ToList().Map(x => x as Variable).Filter(x => !x.Equals(""));
                        }
                        vts.Add(vt);
                    }
                }
                return(vts);
            }
            catch
            {
                Log.Out(Log.Error, "Failed to parse verification tasks");
                return(null);
            }
        }
Esempio n. 3
0
        public static int RVTRunVerificationTask(SDiff.VerificationTask vt, VC.ConditionGeneration vcgen, Program prog,
                                                 HashSet <int> inlinedFns1, HashSet <int> inlinedFns2, out bool crashed)
        {
            Log.Out(Log.Verifier, "Verifying " + vt.Eq.Name);

            crashed = false;


            var attList = new List <Object>(1);

            attList.Add(Expr.Literal(1));

            //save attributes
            var sqkLeft   = vt.Left.Attributes;
            var sqkpLeft  = vt.Left.Proc.Attributes;
            var sqkRight  = vt.Right.Attributes;
            var sqkpRight = vt.Right.Proc.Attributes;

            //save postconditions
            var leftPosts  = vt.Left.Proc.Ensures;
            var rightPosts = vt.Right.Proc.Ensures;

            //Keep the postconditions correpsonding to the
            //free ensures (out == uf_Foo(inp))
            //vt.Left.Proc.Ensures = new List<Ensures>();
            //vt.Right.Proc.Ensures = new List<Ensures>();

            //inline procedures under analysis
            vt.Left.Attributes
                = Util.MkInlinedAttribute(attList);
            vt.Left.Proc.Attributes = vt.Left.Attributes;

            vt.Right.Attributes
                = Util.MkInlinedAttribute(attList);
            vt.Right.Proc.Attributes = vt.Right.Attributes;

            vt.Left.OriginalBlocks   = vt.Left.Blocks;
            vt.Left.OriginalLocVars  = vt.Left.LocVars;
            vt.Right.OriginalBlocks  = vt.Right.Blocks;
            vt.Right.OriginalLocVars = vt.Right.LocVars;


            IEnumerable <Declaration> procImplPIter = prog.TopLevelDeclarations.Where(x => x is Implementation);

            List <Implementation> inlinedImpls = new List <Implementation>();

            foreach (Implementation currentProcImpl in procImplPIter)
            {
                bool match = false;
                int  indx  = -1;
                if (fnNameToIndex1.ContainsKey(currentProcImpl.Proc.Name))
                {
                    match = true;
                    indx  = fnNameToIndex1[currentProcImpl.Proc.Name];
                }
                else if (fnNameToIndex2.ContainsKey(currentProcImpl.Proc.Name))
                {
                    match = true;
                    indx  = fnNameToIndex2[currentProcImpl.Proc.Name];
                }

                //Inline if present in
                if (match && (inlinedFns1.Contains(indx) || inlinedFns2.Contains(indx)))
                {
                    inlinedImpls.Add(currentProcImpl);
                    currentProcImpl.Attributes      = Util.MkInlinedAttribute(attList);
                    currentProcImpl.Proc.Attributes = Util.MkInlinedAttribute(attList);

                    //RUN INLINER OVER EQ FUNCTION
                    currentProcImpl.OriginalBlocks  = currentProcImpl.Blocks;
                    currentProcImpl.OriginalLocVars = currentProcImpl.LocVars;
                }
            }

            //RUN INLINER OVER EQ FUNCTION
            // prog = EQ program
            // vt.Eq = EQ_f_f' procedure with f, f' having {inline} tags
            Inliner.ProcessImplementation(prog, vt.Eq);

            if (Options.TraceVerify)
            {
                Log.Out(Log.Normal, "Ready to verify:");
                Log.LogEmit(Log.Normal, prog.Emit);
            }

            // To print the EQ files in

            Util.DumpBplAST(prog, vt.Eq.Name + "_out.bpl");
            // prog = SDiff.Boogie.Process.ParseProgram(vt.Eq.Name + "_out.bpl");

            if (BoogieUtils.ResolveAndTypeCheckThrow(prog, Options.MergedProgramOutputFile))
            {
                return(1);
            }

            Implementation newEq   = vt.Eq;
            var            newProg = prog;

            vcgen = BoogieVerify.InitializeVC(newProg);
            //SDiff.Boogie.Process.ResolveAndTypeCheck(newProg, "");
            var newDict = SDiff.Boogie.Process.BuildProgramDictionary(newProg.TopLevelDeclarations.ToList());
            //newEq = (Implementation)newDict.Get(vt.Eq.Name + "$IMPL");



            SDiffCounterexamples SErrors;
            List <Model>         errModelList;


            //Clear up the state since it might call the same procedure twice



            vt.Result = BoogieVerify.VerifyImplementation(vcgen, newEq, newProg, out SErrors, out errModelList);

            switch (vt.Result)
            {
            case SDiff.VerificationResult.Error:
                Log.Out(Log.Verifier, "Result: Error");
                break;

            case SDiff.VerificationResult.Verified:
                Log.Out(Log.Verifier, "Result: Verified");
                break;

            default:
                Log.Out(Log.Verifier, "Result: Unhandled");
                crashed = true;
                break;
            }

            //restore postconditions IN THE OLD IN-MEMORY PROGRAM
            vt.Left.Proc.Ensures  = leftPosts;
            vt.Right.Proc.Ensures = rightPosts;

            //remove the inline annotation IN THE OLD IN-MEMORY PROGRAM
            vt.Left.Attributes       = sqkLeft;
            vt.Left.Proc.Attributes  = sqkpLeft;
            vt.Right.Attributes      = sqkRight;
            vt.Right.Proc.Attributes = sqkpRight;

            //remove the inline annotation in the Inlined Procedures
            foreach (Implementation currentProcImpl in procImplPIter)
            {
                if (inlinedImpls.Contains(currentProcImpl))
                {
                    currentProcImpl.Attributes      = null;
                    currentProcImpl.Proc.Attributes = null;
                }
            }

            if (vt.Result == SDiff.VerificationResult.Verified)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 4
0
        public static int RunVerificationTask(VerificationTask vt, VC.ConditionGeneration vcgen, Program prog, out bool crashed, bool wrapper = true)
        {
            crashed = false;

            var attList = new List <Object>(1);

            attList.Add(Expr.Literal(1));

            //save attributes
            var sqkLeft   = vt.Left.Attributes;
            var sqkpLeft  = vt.Left.Proc.Attributes;
            var sqkRight  = vt.Right.Attributes;
            var sqkpRight = vt.Right.Proc.Attributes;

            //save postconditions
            var leftPosts  = vt.Left.Proc.Ensures;
            var rightPosts = vt.Right.Proc.Ensures;

            //The ensures must have been removed at the time of stripContracts
            //The recursive case is handled by RVT option anyway.
            //vt.Left.Proc.Ensures = new List<Ensures>();
            //vt.Right.Proc.Ensures = new List<Ensures>();

            //inline procedures under analysis
            vt.Left.Attributes
                = Util.MkInlinedAttribute(attList);
            vt.Left.Proc.Attributes = vt.Left.Attributes;

            vt.Right.Attributes
                = Util.MkInlinedAttribute(attList);
            vt.Right.Proc.Attributes = vt.Right.Attributes;

            //RUN INLINER OVER EQ FUNCTION
            vt.Left.OriginalBlocks   = vt.Left.Blocks;
            vt.Left.OriginalLocVars  = vt.Left.LocVars;
            vt.Right.OriginalBlocks  = vt.Right.Blocks;
            vt.Right.OriginalLocVars = vt.Right.LocVars;

            // inline diff_inline procedures
            IEnumerable <Declaration> procImplPIter = prog.TopLevelDeclarations.Where(x => x is Implementation);

            foreach (Implementation currentProcImpl in procImplPIter)
            {
                if (currentProcImpl.Name.Contains("_Diff_Inline"))
                {
                    currentProcImpl.Attributes      = Util.MkInlinedAttribute(attList);
                    currentProcImpl.Proc.Attributes = Util.MkInlinedAttribute(attList);

                    //RUN INLINER OVER EQ FUNCTION
                    currentProcImpl.OriginalBlocks  = currentProcImpl.Blocks;
                    currentProcImpl.OriginalLocVars = currentProcImpl.LocVars;
                }
            }

            // prog = EQ program
            // vt.Eq = EQ_f_f' procedure with f, f' having {inline} tags
            Inliner.ProcessImplementation(prog, vt.Eq);

            SDiffCounterexamples SErrors      = null;
            List <Model>         errModelList = null;
            Implementation       newEq        = null;
            Program newProg = null;
            Dictionary <string, Declaration> newDict = null;


            Log.Out(Log.Verifier, "Verifying " + vt.Eq.Name);
            if (Options.TraceVerify)
            {
                Log.Out(Log.Normal, "Ready to verify:");
                Log.LogEmit(Log.Normal, prog.Emit);
            }

            // To print the EQ files in
            Util.DumpBplAST(prog, vt.Eq.Name + "_out.bpl");
            //RS: Uncomment this

            /*if (wrapper)
             * {
             *  prog.RemoveTopLevelDeclaration(vt.Eq);
             *  prog.RemoveTopLevelDeclaration(vt.Eq.Proc);
             * }*/
            prog = null;
            ReplaceInFile(vt.Eq.Name + "_out.bpl", "@", "_");
            if (!wrapper)
            {
                prog = BoogieUtils.ParseProgram("RS" + vt.Eq.Name + "_out.bpl");

                if (prog == null)
                {
                    Log.Out(Log.Verifier, "Parse Error!!! in   " + vt.Eq.Name);
                    return(1);
                }
                if (BoogieUtils.ResolveAndTypeCheckThrow(prog, Options.MergedProgramOutputFile))
                {
                    return(1);
                }

                newEq   = vt.Eq;
                newProg = prog;

                vcgen = InitializeVC(newProg);
                //SDiff.Boogie.Process.ResolveAndTypeCheck(newProg, "");
                newDict = SDiff.Boogie.Process.BuildProgramDictionary(newProg.TopLevelDeclarations.ToList());

                //RS: Uncomment this
                newEq = (Implementation)newDict.Get(vt.Eq.Name + "$IMPL");

                vt.Result = VerifyImplementation(vcgen, newEq, newProg, out SErrors, out errModelList);



                switch (vt.Result)
                {
                case VerificationResult.Error:
                    Log.Out(Log.Verifier, "Result: Error");
                    break;

                case VerificationResult.Verified:
                    Log.Out(Log.Verifier, "Result: Verified");
                    break;

                case VerificationResult.OutOfMemory:
                    Log.Out(Log.Verifier, "Result: OutOfMemory");
                    break;

                case VerificationResult.TimeOut:
                    Log.Out(Log.Verifier, "Result: TimeOut");
                    break;

                default:
                    Log.Out(Log.Verifier, "Result: Unhandled");
                    crashed = true;
                    break;
                }
                vcgen.Close();
            }
            //restore postconditions IN THE OLD IN-MEMORY PROGRAM
            vt.Left.Proc.Ensures  = leftPosts;
            vt.Right.Proc.Ensures = rightPosts;

            //remove the inline annotation IN THE OLD IN-MEMORY PROGRAM
            vt.Left.Attributes       = sqkLeft;
            vt.Left.Proc.Attributes  = sqkpLeft;
            vt.Right.Attributes      = sqkRight;
            vt.Right.Proc.Attributes = sqkpRight;

            //remove the inline annotation in the Diff_Inline Procedures
            foreach (Implementation currentProcImpl in procImplPIter)
            {
                if (currentProcImpl.Name.Contains("_Diff_Inline"))
                {
                    currentProcImpl.Attributes      = null;
                    currentProcImpl.Proc.Attributes = null;
                }
            }
            if (!wrapper)
            {
                if (vt.Result != VerificationResult.Error)
                {
                    return(0);                                       //even timeouts/unhandled, as we see timeouts with 1 error, 0 model
                }
                //process counterexamples OVER THE NEW IN-MEMORY PROGRAM
                var outputVars = new List <Variable>();
                foreach (var v in vt.DesiredOutputVars)
                {
                    outputVars.Add(newDict.Get(v.Name + "$VAR") as Variable);
                }

                var globals = new List <Variable>();
                foreach (IdentifierExpr ie in newEq.Proc.Modifies)
                {
                    globals.Add(ie.Decl);
                }


                if (SErrors != null &&
                    SErrors.Count > 0 &&
                    errModelList.Count == SErrors.Count) //change as now SErrors can be nonnull, yet Count == 0. Sometimes model.Count < SErrror!!
                {
                    //somewhat misnamed...
                    if (Options.DumpBeforeVerifying)
                    {
                        Log.Out(Log.SymEx, "Dumping procedure under verification");
                        newEq.Emit(Log.LogWriter, 0);
                    }
                    if (Options.DoSymEx)
                    {
                        var vtLeftProcImpl  = (Implementation)Util.getDeclarationByName(vt.Left.Name + "_Diff_Inline", procImplPIter);
                        var vtRightProcImpl = (Implementation)Util.getDeclarationByName(vt.Right.Name + "_Diff_Inline", procImplPIter);

                        if (Options.PreciseDifferentialInline)
                        {
                            List <Declaration> consts = prog.TopLevelDeclarations.Where(x => x is Constant).ToList();
                            ProcessCounterexamplesWOSymbolicOut(SErrors, globals, vt.Eq.LocVars, vtLeftProcImpl, vtRightProcImpl, consts, errModelList);
                        }
                        else
                        {
                            ProcessCounterexamples(SErrors, globals, outputVars, newProg, vtLeftProcImpl, vtRightProcImpl);
                        }
                    }
                }

                return(SErrors == null ? 0 : SErrors.Count);
            }
            return(0);
        }