private ICFG contractFreeCFG; // computed on demand

        protected BasicMethodDriver(
            Method method,
            IBasicAnalysisDriver <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, LogOptions> parent
            )
        {
            Contract.Requires(parent != null);

            this.method = method;
            this.parent = parent;
            var rawCFG = this.parent.MethodCache.GetCFG(method);

            // Here we build our stack of adapters.
            //
            //  StackDepth    (Temp decoder for APC)
            //  CFG           (Unit decoder for APC, including Assume)
            //  cciilprovider (Unit decoder for PC)
            //

            rawLayer = CodeLayerFactory.Create(
                rawCFG.GetDecoder(parent.MetaDataDecoder),
                parent.MetaDataDecoder,
                parent.ContractDecoder,
                (unit) => "",
                (unit) => "",
                (v1, v2) => false);

            if (PrintIL)
            {
                Console.WriteLine("-----------------APC based CFG---------------------");
                this.RawLayer.Decoder.AssumeNotNull().Context.MethodContext.CFG.Print(Console.Out, this.RawLayer.Printer, null, null, null);
            }

            stackLayer = CodeLayerFactory.Create(
                StackDepthFactory.Create(
                    this.RawLayer.Decoder,
                    this.RawLayer.MetaDataDecoder),
                this.RawLayer.MetaDataDecoder,
                this.RawLayer.ContractDecoder,
                delegate(int i) { return("s" + i.ToString()); },
                delegate(int i) { return("s" + i.ToString()); },
                (s1, s2) => false
                );

            if (PrintIL)
            {
                Console.WriteLine("-----------------Stack based CFG---------------------");
                this.StackLayer.Decoder.AssumeNotNull().Context.MethodContext.CFG.Print(Console.Out, StackLayer.Printer, null, null, null);
            }

            // set static wp tracing option
            WeakestPreconditionProver.Trace           = parent.Options.TraceWP;
            WeakestPreconditionProver.EmitSMT2Formula = parent.Options.EmitSMT2Formula;
        }
Esempio n. 2
0
            public void RunHeapAndExpressionAnalyses(DFAController controller)
            {
                if (optimisticHeapAnalysis != null)
                {
                    return;
                }

                optimisticHeapAnalysis = new OptimisticHeapAnalyzer <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(this.StackLayer, this.Options.TraceEGraph);
                optimisticHeapAnalysis.TurnArgumentExceptionThrowsIntoAssertFalse = this.Options.TurnArgumentExceptionThrowsIntoAssertFalse;
                optimisticHeapAnalysis.IgnoreExplicitAssumptions = this.Options.IgnoreExplicitAssumptions;
                optimisticHeapAnalysis.TraceAssumptions          = this.Options.TraceAssumptions;
                var heapsolver = this.StackLayer.CreateForward(optimisticHeapAnalysis,
                                                               new DFAOptions {
                    Trace = this.Options.TraceHeapAnalysis
                }, controller);

                heapsolver(optimisticHeapAnalysis.InitialValue());

                this.ValueLayer = CodeLayerFactory.Create(optimisticHeapAnalysis.GetDecoder(this.StackLayer.Decoder),
                                                          this.StackLayer.MetaDataDecoder,
                                                          this.StackLayer.ContractDecoder,
                                                          delegate(SymbolicValue source) { return(source.ToString()); },
                                                          delegate(SymbolicValue dest) { return(dest.ToString()); },
                                                          (v1, v2) => v1.symbol.GlobalId > v2.symbol.GlobalId
                                                          );

                if (this.PrintIL)
                {
                    Console.WriteLine("-----------------Value based CFG---------------------");
                    this.ValueLayer.Decoder.Context.MethodContext.CFG.Print(Console.Out, this.ValueLayer.Printer, optimisticHeapAnalysis.GetEdgePrinter(),
                                                                            (block) => optimisticHeapAnalysis.GetContexts(block),
                                                                            null);
                }
                var exprAnalysis = new ExpressionAnalysis <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly, SymbolicValue, IValueContext <Local, Parameter, Method, Field, Type, SymbolicValue>, EdgeData>(
                    this.ValueLayer,
                    this.Options,
                    optimisticHeapAnalysis.IsUnreachable
                    );
                var exprsolver = this.ValueLayer.CreateForward(exprAnalysis.CreateExpressionAnalyzer(),
                                                               new DFAOptions {
                    Trace = this.Options.TraceExpressionAnalysis
                }, controller);

                exprsolver(exprAnalysis.InitialValue(SymbolicValue.GetUniqueKey));

                var exprDecoder = exprAnalysis.GetDecoder(this.ValueLayer.Decoder);

                expr2String = ExprPrinter.Printer(exprDecoder.Context, this);

                this.ExpressionLayer = CodeLayerFactory.Create(
                    exprDecoder,
                    this.ValueLayer.MetaDataDecoder,
                    this.ValueLayer.ContractDecoder,
                    expr2String,
                    this.ValueLayer.VariableToString,
                    (v1, v2) => v1.symbol.GlobalId > v2.symbol.GlobalId
                    );


                if (this.PrintIL)
                {
                    Console.WriteLine("-----------------Expression based CFG---------------------");
                    this.ExpressionLayer.Decoder.Context.MethodContext.CFG.Print(Console.Out, this.ExpressionLayer.Printer, exprAnalysis.GetBlockPrinter(expr2String),
                                                                                 (block) => exprAnalysis.GetContexts(block),
                                                                                 null);
                }

                this.HybridLayer = CodeLayerFactory.Create(
                    this.ValueLayer.Decoder,
                    this.ValueLayer.MetaDataDecoder,
                    this.ValueLayer.ContractDecoder,
                    this.ValueLayer.ExpressionToString,
                    this.ValueLayer.VariableToString,
                    this.ExpressionLayer.Printer,
                    this.ExpressionLayer.NewerThan
                    );

                if (Options.TraceAssumptions)
                {
                    #region Produce trace output to extract implicit assumptions. Please don't remove this code!

                    Console.WriteLine("<assumptions>");
                    Console.WriteLine(@"<subroutine id=""{0}"" methodName=""{1}"">", HybridLayer.Decoder.Context.MethodContext.CFG.Subroutine.Id, HybridLayer.Decoder.Context.MethodContext.CFG.Subroutine.Name);

                    foreach (var b in HybridLayer.Decoder.Context.MethodContext.CFG.Subroutine.Blocks)
                    {
                        Console.WriteLine(@"<block index=""{0}"">", b.Index);

                        foreach (var apc in b.APCs())
                        {
                            Console.WriteLine(@"<apc name=""{0}"" index=""{1}"" ilOffset=""{2}"" primarySourceContext=""{3}"" sourceContext=""{4}"">",
                                              apc.ToString(), apc.Index, apc.ILOffset, apc.PrimarySourceContext(), HybridLayer.Decoder.Context.MethodContext.SourceContext(apc));

                            Console.WriteLine("<code>");
                            HybridLayer.Printer(apc, "", Console.Out);
                            Console.WriteLine("</code>");

                            optimisticHeapAnalysis.Dump(apc);

                            Console.WriteLine("</apc>");
                        }

                        Console.WriteLine("</block>");
                    }

                    Console.WriteLine("</subroutine>");
                    Console.WriteLine("</assumptions>");

                    #endregion
                }
                ModifiedAtCall = optimisticHeapAnalysis.ModifiedAtCall;
            }