Ejemplo n.º 1
0
        // this function applies analysis-net analyses on the method defined in our assembly (methodDefinition)
        // the result is a typed stackless three address code representation of the orignal method definition body
        // you can 'out' the control flow graph because it can be reused for another analysis
        public static MethodBody ThreeAddressCode(IMethodDefinition methodDefinition, MetadataReaderHost host, out ControlFlowGraph cfg)
        {
            if (methodDefinition.IsAbstract || methodDefinition.IsExternal)
            {
                cfg = null;
                return(null);
            }

            var disassembler = new Disassembler(host, methodDefinition, null);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);

            //var cfg = cfAnalysis.GenerateNormalControlFlow();
            cfg = cfAnalysis.GenerateExceptionalControlFlow();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(cfg, methodDefinition.Type);

            typeAnalysis.Analyze();

            methodBody.UpdateVariables();

            return(methodBody);
        }
Ejemplo n.º 2
0
        public override IMethodDefinition Rewrite(IMethodDefinition methodDefinition)
        {
            var signature = MemberHelper.GetMethodSignature(methodDefinition, NameFormattingOptions.Signature | NameFormattingOptions.ParameterName);

            System.Console.WriteLine(signature);

            var disassembler = new Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            //System.Console.WriteLine(methodBody);
            //System.Console.WriteLine();

            var cfg = ControlFlowGraph.GenerateNormalControlFlow(methodBody);

            ControlFlowGraph.ComputeDominators(cfg);
            ControlFlowGraph.IdentifyLoops(cfg);

            ControlFlowGraph.ComputeDominatorTree(cfg);
            ControlFlowGraph.ComputeDominanceFrontiers(cfg);

            var splitter = new WebAnalysis(cfg);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(cfg);

            typeAnalysis.Analyze();

            var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);

            forwardCopyAnalysis.Analyze();
            forwardCopyAnalysis.Transform(methodBody);

            var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyAnalysis.Analyze();
            backwardCopyAnalysis.Transform(methodBody);

            //var pointsTo = new PointsToAnalysis(cfg);
            //var result = pointsTo.Analyze();

            var ssa = new StaticSingleAssignmentAnalysis(methodBody, cfg);

            ssa.Transform();

            methodBody.UpdateVariables();

            ////var dot = DOTSerializer.Serialize(cfg);
            var dgml = DGMLSerializer.Serialize(cfg);

            return(base.Rewrite(methodDefinition));
        }
Ejemplo n.º 3
0
        public void Execute()
        {
            var disassembler = new Backend.Transformations.Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            MethodBody = methodBody;

            var cfAnalysis = new ControlFlowAnalysis(methodBody);

            ControlFlowGraph = cfAnalysis.GenerateExceptionalControlFlow();

            var splitter = new WebAnalysis(ControlFlowGraph, methodBody.MethodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(ControlFlowGraph, methodBody.MethodDefinition.Type);

            typeAnalysis.Analyze();

            //var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(Traverser.CFG);
            //forwardCopyAnalysis.Analyze();
            //forwardCopyAnalysis.Transform(methodBody);

            //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(Traverser.CFG);
            //backwardCopyAnalysis.Analyze();
            //backwardCopyAnalysis.Transform(methodBody);

            // TinyBCT transformations

            var fieldInitialization = new FieldInitialization(methodBody);

            fieldInitialization.Transform();

            if (!Settings.AddressesEnabled())
            {
                var refAlias = new RefAlias(methodBody);
                refAlias.Transform();
            }

            // execute this after RefAlias!
            var immutableArguments = new ImmutableArguments(methodBody);

            immutableArguments.Transform();

            methodBody.RemoveUnusedLabels();
        }
Ejemplo n.º 4
0
        public void GenerateWebs(MethodDefinition method)
        {
            var methodInfo = programInfo.GetOrAdd(method);

            //GenerateIL(method);
            //GenerateTAC(method);
            GenerateCFG(method);

            if (!methodInfo.Contains("WEBS_TEXT"))
            {
                var cfg = methodInfo.Get <ControlFlowGraph>("CFG");

                // Webs
                var splitter = new WebAnalysis(cfg);
                splitter.Analyze();
                splitter.Transform();

                method.Body.UpdateVariables();

                var typeAnalysis = new TypeInferenceAnalysis(cfg, method.ReturnType);
                typeAnalysis.Analyze();

                //// Optional
                //var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);
                //forwardCopyAnalysis.Analyze();
                //forwardCopyAnalysis.Transform(method.Body);

                //// Optional
                //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
                //backwardCopyAnalysis.Analyze();
                //backwardCopyAnalysis.Transform(method.Body);

                var text = method.Body.ToString();
                methodInfo.Add("WEBS_TEXT", text);

                text = DGMLSerializer.Serialize(cfg);
                methodInfo.Set("CFG_TEXT", text);

                text = DGMLSerializer.SerializeDominanceTree(cfg);
                methodInfo.Set("DT_TEXT", text);

                text = DGMLSerializer.SerializePostDominanceTree(cfg);
                methodInfo.Set("PDT_TEXT", text);

                text = DGMLSerializer.SerializeControlDependenceGraph(cfg);
                methodInfo.Set("CDG_TEXT", text);
            }
        }
Ejemplo n.º 5
0
        //string _code = String.Empty;

        /// <summary>
        /// For a given methodDefinition, create a CFG and run basic analyses such as
        /// stack removal, SSA transformation, live-variables analysis, and copy-propagation.
        /// </summary>
        /// <param name="methodDefinition"></param>
        /// <returns></returns>
        private ControlFlowGraph PrepareMethod(IMethodDefinition methodDefinition)
        {
            var disassembler = new Disassembler(mhost, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfg = ControlFlowGraph.GenerateNormalControlFlow(methodBody);

            ControlFlowGraph.ComputeDominators(cfg);
            ControlFlowGraph.IdentifyLoops(cfg);

            ControlFlowGraph.ComputeDominatorTree(cfg);
            ControlFlowGraph.ComputeDominanceFrontiers(cfg);

            // Uniquely rename stack variables.
            var splitter = new WebAnalysis(cfg);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            // Infer types for stack variables.
            var typeAnalysis = new TypeInferenceAnalysis(cfg);

            typeAnalysis.Analyze();

            var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyAnalysis.Analyze();
            backwardCopyAnalysis.Transform(methodBody);

            var lva = new LiveVariablesAnalysis(cfg);

            lva.Analyze();

            var ssa = new StaticSingleAssignmentAnalysis(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(lva);

            methodBody.UpdateVariables();

            //_code = methodBody.ToString();
            return(cfg);
        }
Ejemplo n.º 6
0
        public static void transformBody(MethodBody methodBody)
        {
            var cfAnalysis = new ControlFlowAnalysis(methodBody);

            //var cfg = cfAnalysis.GenerateNormalControlFlow();
            Traverser.CFG = cfAnalysis.GenerateExceptionalControlFlow();

            var splitter = new WebAnalysis(Traverser.CFG, methodBody.MethodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(Traverser.CFG, methodBody.MethodDefinition.Type);

            typeAnalysis.Analyze();

            //var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(Traverser.CFG);
            //forwardCopyAnalysis.Analyze();
            //forwardCopyAnalysis.Transform(methodBody);

            //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(Traverser.CFG);
            //backwardCopyAnalysis.Analyze();
            //backwardCopyAnalysis.Transform(methodBody);

            // TinyBCT transformations

            if (!Settings.NewAddrModelling)
            {
                var refAlias = new RefAlias(methodBody);
                refAlias.Transform();

                var immutableArguments = new ImmutableArguments(methodBody);
                immutableArguments.Transform();

                var fieldInitialization = new FieldInitialization(methodBody);
                fieldInitialization.Transform();
            }
            methodBody.RemoveUnusedLabels();
        }
Ejemplo n.º 7
0
        public static void TransformToTac(MethodDefinition method)
        {
            Disassembler disassembler = new Disassembler(method);
            MethodBody   methodBody   = disassembler.Execute();

            method.Body = methodBody;

            ControlFlowAnalysis cfAnalysis = new ControlFlowAnalysis(method.Body);

            //var cfg = cfAnalysis.GenerateNormalControlFlow();
            Backend.Model.ControlFlowGraph cfg = cfAnalysis.GenerateExceptionalControlFlow();

            WebAnalysis splitter = new WebAnalysis(cfg);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            TypeInferenceAnalysis typeAnalysis = new TypeInferenceAnalysis(cfg, method.ReturnType);

            typeAnalysis.Analyze();

            // Copy Propagation
            ForwardCopyPropagationAnalysis forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);

            forwardCopyAnalysis.Analyze();
            forwardCopyAnalysis.Transform(methodBody);

            BackwardCopyPropagationAnalysis backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyAnalysis.Analyze();
            backwardCopyAnalysis.Transform(methodBody);

            methodBody.UpdateVariables();
        }
Ejemplo n.º 8
0
        //string _code = String.Empty;

        /// <summary>
        /// For a given methodDefinition, create a CFG and run basic analyses such as
        /// stack removal, SSA transformation, live-variables analysis, and copy-propagation.
        /// </summary>
        /// <param name="methodDefinition"></param>
        /// <returns></returns>
        private ControlFlowGraph PrepareMethod(IMethodDefinition methodDefinition)
        {
            var sw2 = new Stopwatch();

            sw2.Start();
            var disassembler = new Disassembler(mhost, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            var cfg        = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);

            backwardCopyProgapagtion.Analyze();
            backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            sw2.Stop();
            this.nonAnalysisOverhead = sw2.Elapsed;
            return(cfg);

            //var cfg = ControlFlowGraph.GenerateNormalControlFlow(methodBody);
            //ControlFlowGraph.ComputeDominators(cfg);
            //ControlFlowGraph.IdentifyLoops(cfg);

            //ControlFlowGraph.ComputeDominatorTree(cfg);
            //ControlFlowGraph.ComputeDominanceFrontiers(cfg);

            //// Uniquely rename stack variables.
            //var splitter = new WebAnalysis(cfg);
            //splitter.Analyze();
            //splitter.Transform();

            //methodBody.UpdateVariables();

            //// Infer types for stack variables.
            //var typeAnalysis = new TypeInferenceAnalysis(cfg);
            //typeAnalysis.Analyze();

            //var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyAnalysis.Analyze();
            //backwardCopyAnalysis.Transform(methodBody);

            //var lva = new LiveVariablesAnalysis(cfg);
            //lva.Analyze();

            //var ssa = new StaticSingleAssignmentAnalysis(methodBody, cfg);
            //ssa.Transform();
            //ssa.Prune(lva);

            //methodBody.UpdateVariables();

            ////_code = methodBody.ToString();
            //return cfg;
        }
Ejemplo n.º 9
0
        public MethodCfgAndTac AnalyzeIntraProcedural(IMethodDefinition methodDefinition)
        {
            // System.Console.WriteLine("Traversing: {0}", methodDefinition.GetName());
            if (Stubber.SuppressM(methodDefinition))
            {
                return(null);
            }
            if (methodDefinition.IsExternal)
            {
                return(null);
            }
            if (methodDefinition.IsAbstract)
            {
                return(null);
            }

            ITypeDefinition         containingDefn         = methodDefinition.ContainingTypeDefinition;
            ISourceLocationProvider sourceLocationProvider = null;

            if (containingDefn != null)
            {
                IModule mod = TypeHelper.GetDefiningUnit(containingDefn) as IModule;
                if (moduleToPdbMap.ContainsKey(mod))
                {
                    sourceLocationProvider = moduleToPdbMap[mod];
                }
                else
                {
                    if (!(mod == null || mod == Dummy.Module || mod == Dummy.Assembly))
                    {
                        sourceLocationProvider = GetPdbReader(mod.Location);
                        moduleToPdbMap[mod]    = sourceLocationProvider;
                    }
                }
            }
            var disassembler = new Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            // var cfg = cfAnalysis.GenerateNormalControlFlow();
            var cfg = cfAnalysis.GenerateExceptionalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, methodDefinition);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();

            var typeAnalysis = new TypeInferenceAnalysis(cfg, methodDefinition.Type);

            typeAnalysis.Analyze();

            var forwardCopyAnalysis = new ForwardCopyPropagationAnalysis(cfg);

            forwardCopyAnalysis.Analyze();
            forwardCopyAnalysis.Transform(methodBody);

            // backwardCopyAnalysis is buggy - it says so in the source file - see notes in src/test
            // var backwardCopyAnalysis = new BackwardCopyPropagationAnalysis(cfg);
            // backwardCopyAnalysis.Analyze();
            // backwardCopyAnalysis.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);

            liveVariables.Analyze();

            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);

            methodBody.UpdateVariables();

            MethodCfgAndTac mct = new MethodCfgAndTac(cfg, methodBody);

            foreach (IExceptionHandlerBlock ehInfo in disassembler.GetExceptionHandlers())
            {
                if (ehInfo is CatchExceptionHandler)
                {
                    mct.ehInfoList.Add(ehInfo as CatchExceptionHandler);
                }
            }
            return(mct);
        }
Ejemplo n.º 10
0
        public static ControlFlowGraph DoAnalysisPhases(this IMethodDefinition method, IMetadataHost host, ISourceLocationProvider locationProvider,
                                                        IEnumerable <IMethodReference> methodsToTryToInline = null)
        {
            AnalysisStats.extraAnalysisOverHead.Start();
            var disassembler = new Disassembler(host, method, locationProvider);

            var methodBody = disassembler.Execute();

            MethodBodyProvider.Instance.AddBody(method, methodBody);

            if (methodsToTryToInline != null)
            {
                DoInlining(method, host, methodBody, locationProvider, methodsToTryToInline);
            }

            var cfAnalysis = new ControlFlowAnalysis(methodBody);
            //var cfg = cfAnalysis.GenerateExceptionalControlFlow();
            var cfg = cfAnalysis.GenerateNormalControlFlow();

            var domAnalysis = new DominanceAnalysis(cfg);

            domAnalysis.Analyze();
            domAnalysis.GenerateDominanceTree();

            var loopAnalysis = new NaturalLoopAnalysis(cfg);

            loopAnalysis.Analyze();

            var domFrontierAnalysis = new DominanceFrontierAnalysis(cfg);

            domFrontierAnalysis.Analyze();

            var splitter = new WebAnalysis(cfg, method);

            splitter.Analyze();
            splitter.Transform();

            methodBody.UpdateVariables();


            var analysis = new TypeInferenceAnalysis(cfg, methodBody.MethodDefinition.Type);

            analysis.Analyze();

            var copyProgapagtion = new ForwardCopyPropagationAnalysis(cfg);

            copyProgapagtion.Analyze();
            copyProgapagtion.Transform(methodBody);

            //var backwardCopyProgapagtion = new BackwardCopyPropagationAnalysis(cfg);
            //backwardCopyProgapagtion.Analyze();
            //backwardCopyProgapagtion.Transform(methodBody);

            var liveVariables = new LiveVariablesAnalysis(cfg);
            var resultLiveVar = liveVariables.Analyze();


            var ssa = new StaticSingleAssignment(methodBody, cfg);

            ssa.Transform();
            ssa.Prune(liveVariables);
            methodBody.UpdateVariables();
            AnalysisStats.extraAnalysisOverHead.Stop();
            return(cfg);
        }