Esempio n. 1
0
        public override int Run(TOptions analyzeOptions)
        {
            // 0. Initialize an common logger that drives all outputs. This
            //    object drives logging for console, statistics, etc.
            using (AggregatingLogger logger = InitializeLogger(analyzeOptions))
            {
                try
                {
                    Analyze(analyzeOptions, logger);
                }
                catch (ExitApplicationException <ExitReason> ex)
                {
                    // These exceptions have already been logged
                    ExecutionException = ex;
                    return(FAILURE);
                }
                catch (Exception ex)
                {
                    // These exceptions escaped our net and must be logged here
                    LogUnhandledEngineException(ex, logger);
                    ExecutionException = ex;
                    return(FAILURE);
                }
            }

            return(((RuntimeErrors & RuntimeConditions.Fatal) == RuntimeConditions.NoErrors) ? SUCCESS : FAILURE);
        }
Esempio n. 2
0
        private void InitializeOutputFile(TOptions analyzeOptions, TContext context, HashSet <string> targets)
        {
            string            filePath          = analyzeOptions.OutputFilePath;
            AggregatingLogger aggregatingLogger = (AggregatingLogger)context.Logger;

            if (!string.IsNullOrEmpty(filePath))
            {
                InvokeCatchingRelevantIOExceptions
                (
                    () => aggregatingLogger.Loggers.Add(
                        new SarifLogger(
                            analyzeOptions.OutputFilePath,
                            analyzeOptions.Verbose,
                            targets,
                            analyzeOptions.ComputeTargetsHash,
                            Prerelease)),
                    (ex) =>
                {
                    LogExceptionCreatingLogFile(filePath, aggregatingLogger, ex);
                    throw new ExitApplicationException <ExitReason>(SdkResources.UnexpectedApplicationExit, ex)
                    {
                        ExitReason = ExitReason.ExceptionCreatingLogFile
                    };
                }
                );
            }
        }
Esempio n. 3
0
        private void InitializeOutputFile(TOptions analyzeOptions, TContext context, HashSet <string> targets)
        {
            string            filePath          = analyzeOptions.OutputFilePath;
            AggregatingLogger aggregatingLogger = (AggregatingLogger)context.Logger;

            if (!string.IsNullOrEmpty(filePath))
            {
                InvokeCatchingRelevantIOExceptions
                (
                    () => aggregatingLogger.Loggers.Add(
                        new SarifLogger(
                            analyzeOptions.OutputFilePath,
                            targets,
                            analyzeOptions.Verbose,
                            analyzeOptions.LogEnvironment,
                            analyzeOptions.ComputeTargetsHash,
                            Prerelease,
                            invocationTokensToRedact: GenerateSensitiveTokensList())),
                    (ex) =>
                {
                    Errors.LogExceptionCreatingLogFile(context, filePath, ex);
                    ThrowExitApplicationException(context, ExitReason.ExceptionCreatingLogFile, ex);
                }
                );
            }
        }
Esempio n. 4
0
        internal AggregatingLogger InitializeLogger(IAnalyzeOptions analyzeOptions)
        {
            var logger = new AggregatingLogger();

            logger.Loggers.Add(new ConsoleLogger(analyzeOptions.Verbose));

            if (analyzeOptions.Statistics)
            {
                logger.Loggers.Add(new StatisticsLogger());
            }

            return(logger);
        }
Esempio n. 5
0
        private void Analyze(TOptions analyzeOptions, AggregatingLogger logger)
        {
            // 0. Log analysis initiation
            logger.AnalysisStarted();

            // 1. Create context object to pass to skimmers. The logger
            //    and configuration objects are common to all context
            //    instances and will be passed on again for analysis.
            this.rootContext = CreateContext(analyzeOptions, logger, RuntimeErrors);

            // 2. Perform any command line argument validation beyond what
            //    the command line parser library is capable of.
            ValidateOptions(this.rootContext, analyzeOptions);

            // 3. Create our configuration property bag, which will be
            //    shared with all rules during analysis
            ConfigureFromOptions(this.rootContext, analyzeOptions);

            // 4. Produce a comprehensive set of analysis targets
            HashSet <string> targets = CreateTargetsSet(analyzeOptions);

            // 5. Proactively validate that we can locate and
            //    access all analysis targets. Helper will return
            //    a list that potentially filters out files which
            //    did not exist, could not be accessed, etc.
            targets = ValidateTargetsExist(this.rootContext, targets);

            // 6. Initialize report file, if configured.
            InitializeOutputFile(analyzeOptions, this.rootContext, targets);

            // 7. Instantiate skimmers.
            HashSet <ISkimmer <TContext> > skimmers = CreateSkimmers(this.rootContext);

            // 8. Initialize skimmers. Initialize occurs a single time only.
            skimmers = InitializeSkimmers(skimmers, this.rootContext);

            // 9. Run all analysis
            AnalyzeTargets(analyzeOptions, skimmers, this.rootContext, targets);

            // 9. For test purposes, raise an unhandled exception if indicated
            if (RaiseUnhandledExceptionInDriverCode)
            {
                throw new InvalidOperationException(this.GetType().Name);
            }
        }
Esempio n. 6
0
        private void Analyze(TOptions analyzeOptions, AggregatingLogger logger)
        {
            // 1. Scrape the analyzer options for settings that alter
            //    behaviors of binary parsers (such as settings for
            //    symbols resolution).
            InitializeFromOptions(analyzeOptions);

            // 2. Produce a comprehensive set of analysis targets
            HashSet <string> targets = CreateTargetsSet(analyzeOptions);

            // 3. Proactively validate that we can locate and
            //    access all analysis targets. Helper will return
            //    a list that potentially filters out files which
            //    did not exist, could not be accessed, etc.
            targets = ValidateTargetsExist(logger, targets);

            // 4. Create our policy, which will be shared across
            //    all context objects that are created during analysis
            PropertyBag policy = CreatePolicyFromOptions(analyzeOptions);

            // 5. Create short-lived context object to pass to
            //    skimmers during initialization. The logger and
            //    policy objects are common to all context instances
            //    and will be passed on again for analysis.
            TContext context = CreateContext(analyzeOptions, logger, policy);

            // 6. Initialize report file, if configured.
            InitializeOutputFile(analyzeOptions, context, targets);

            // 7. Instantiate skimmers.
            HashSet <ISkimmer <TContext> > skimmers = CreateSkimmers(logger);

            // 8. Initialize skimmers. Initialize occurs a single time only.
            skimmers = InitializeSkimmers(skimmers, context);

            // 9. Run all analysis
            AnalyzeTargets(analyzeOptions, skimmers, context, targets);

            // 10. For test purposes, raise an unhandled exception if indicated
            if (RaiseUnhandledExceptionInDriverCode)
            {
                throw new InvalidOperationException(this.GetType().Name);
            }
        }