Example #1
0
        public MainViewModel()
        {
            this.Assemblies = new ObservableCollection <AssemblyViewModel>();
            this.Documents  = new ObservableCollection <DocumentViewModelBase>();

            this.Commands = new List <UIDelegateCommand>();
            AddCommand("File|ToolBar", "_Open", ModifierKeys.Control, Key.O, OnOpen);
            AddSeparator();
            AddCommand("File", "_Exit", ModifierKeys.Alt, Key.F4, OnExit);

            host        = new Host();
            loader      = new CCIProvider.Loader(host);
            programInfo = new ProgramAnalysisInfo();

            PlatformTypes.Resolve(host);
        }
Example #2
0
        public static SarifLog AnalyzeDll(string inputPath, ScopeMethodKind kind,

                                          bool useScopeFactory = true, bool interProc = false, StreamWriter outputStream = null)
        {
            // Determine whether to use Interproc analysis
            AnalysisOptions.DoInterProcAnalysis = interProc;

            AnalysisStats.TotalNumberFolders++;

            var host = new MyHost();

            PlatformTypes.Resolve(host);

            var loader = new MyLoader(host);

            host.Loader = loader;

            var scopeGenAssembly = loader.LoadMainAssembly(inputPath);

            AnalysisStats.TotalDllsFound++;

            loader.LoadCoreAssembly();

            var program = new ScopeProgramAnalysis(host, loader);

            // program.interprocAnalysisManager = new InterproceduralManager(host);
            program.ScopeGenAssembly = scopeGenAssembly;
            //program.ReferenceFiles = referenceFiles;

            program.ClassFilters    = new HashSet <string>();
            program.ClousureFilters = new HashSet <string>();

            program.EntryMethods = new HashSet <string>();

            if (kind == ScopeMethodKind.Reducer || kind == ScopeMethodKind.All)
            {
                program.ClassFilters.Add("Reducer");
                program.ClousureFilters.Add("<Reduce>d__");
                program.EntryMethods.Add("Reduce");
            }
            if (kind == ScopeMethodKind.Producer || kind == ScopeMethodKind.All)
            {
                program.ClassFilters.Add("Processor");
                program.ClousureFilters.Add("<Process>d__");
                program.EntryMethods.Add("Process");
                //program.ClassFilter = "Producer";
                //program.ClousureFilter = "<Produce>d__";
                //program.EntryMethod = "Produce";
            }

            program.MethodUnderAnalysisName = "MoveNext";

            IEnumerable <Tuple <MethodDefinition, MethodDefinition, MethodDefinition> > scopeMethodPairs;

            if (useScopeFactory)
            {
                scopeMethodPairs = program.ObtainScopeMethodsToAnalyze();
                if (!scopeMethodPairs.Any())
                {
                    if (outputStream != null)
                    {
                        outputStream.WriteLine("Failed to obtain methods from the ScopeFactory. ");
                    }
                    System.Console.WriteLine("Failed to obtain methods from the ScopeFactory.");

                    //System.Console.WriteLine("Now trying to find methods in the the assembly");
                    //scopeMethodPairs = program.ObtainScopeMethodsToAnalyzeFromAssemblies();
                }
            }
            else
            {
                scopeMethodPairs = program.ObtainScopeMethodsToAnalyzeFromAssemblies();
            }


            if (scopeMethodPairs.Any())
            {
                var log = CreateSarifOutput();

                IReadOnlyDictionary <string, Tuple <Schema, Schema> > allSchemas;
                if (useScopeFactory)
                {
                    allSchemas = program.ReadSchemasFromXML(inputPath);
                }
                else
                {
                    allSchemas = program.ReadSchemasFromXML2(inputPath);
                }

                foreach (var methodPair in scopeMethodPairs)
                {
                    AnalysisStats.TotalMethods++;

                    var entryMethodDef = methodPair.Item1;
                    var moveNextMethod = methodPair.Item2;
                    var getEnumMethod  = methodPair.Item3;
                    System.Console.WriteLine("Method {0} on class {1}", moveNextMethod.Name, moveNextMethod.ContainingType.FullPathName());

                    Schema inputSchema  = null;
                    Schema outputSchema = null;
                    Tuple <Schema, Schema> schemas;
                    if (allSchemas.TryGetValue(moveNextMethod.ContainingType.ContainingType.Name, out schemas))
                    {
                        inputSchema  = schemas.Item1;
                        outputSchema = schemas.Item2;
                    }

                    try
                    {
                        InputSchema  = inputSchema;
                        OutputSchema = outputSchema;
                        var dependencyAnalysis = new SongTaoDependencyAnalysis(host, program.interprocAnalysisManager, moveNextMethod, entryMethodDef, getEnumMethod);
                        var depAnalysisResult  = dependencyAnalysis.AnalyzeMoveNextMethod();

                        WriteResultToSarifLog(inputPath, outputStream, log, moveNextMethod, depAnalysisResult, dependencyAnalysis, program.factoryReducerMap);

                        InputSchema  = null;
                        OutputSchema = null;
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine("Could not analyze {0}", inputPath);
                        System.Console.WriteLine("Exception {0}\n{1}", e.Message, e.StackTrace);
                        AnalysisStats.TotalofDepAnalysisErrors++;
                        AnalysisStats.AddAnalysisReason(new AnalysisReason(moveNextMethod, moveNextMethod.Body.Instructions[0],
                                                                           String.Format(CultureInfo.InvariantCulture, "Throw exception {0}\n{1}", e.Message, e.StackTrace.ToString())));
                    }
                }
                return(log);
            }
            else
            {
                System.Console.WriteLine("No method {0} of type {1} in {2}", program.MethodUnderAnalysisName, program.ClassFilters, inputPath);
                return(null);
            }
        }