private static SarifLog AnalyzeOneDll(string input, ScopeMethodKind kind, bool useScopeFactory = true, bool interProcAnalysis = false) { var folder = Path.GetDirectoryName(input); var referenceFiles = Directory.GetFiles(folder, "*.dll", SearchOption.TopDirectoryOnly).Where(fp => Path.GetFileName(fp).ToLower(CultureInfo.InvariantCulture) != Path.GetFileName(input).ToLower(CultureInfo.InvariantCulture)).ToList(); referenceFiles.AddRange(Directory.GetFiles(folder, "*.exe", SearchOption.TopDirectoryOnly)); return(AnalyzeDll(input, kind, useScopeFactory, interProcAnalysis)); }
public static SarifLog AnalyzeDll(string inputPath, ScopeMethodKind kind, bool useScopeFactory = true, bool interProc = false, StreamWriter outputStream = null, TimeSpan timeout = default(TimeSpan)) { if (timeout == default(TimeSpan)) { timeout = TimeSpan.FromMinutes(1); } if (System.Diagnostics.Debugger.IsAttached) { timeout = TimeSpan.FromMilliseconds(-1); } var task = Task.Run(() => ScopeProgramAnalysis.AnalyzeDll(inputPath, kind, useScopeFactory, interProc, outputStream)); if (task.Wait(timeout)) { return(task.Result); } else { var log = SarifLogger.CreateSarifOutput(); var r = SarifLogger.CreateRun(inputPath, "No results", "Timeout", new List <Result>()); log.Runs.Add(r); return(log); } }
public static void AnalyzeDllAndWriteLog(string inputPath, string outputPath, ScopeMethodKind kind, bool useScopeFactory = true, bool interProc = false, StreamWriter outputStream = null) { var log = AnalyzeDll(inputPath, ScopeMethodKind.All, useScopeFactory, interProc, outputStream); SarifLogger.WriteSarifOutput(log, outputPath); }
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); } }