From adb6f62bc60c5393b70f009dcb62d6353853f2fc Mon Sep 17 00:00:00 2001 From: "Michael C. Fanning" Date: Wed, 26 Jul 2023 18:22:48 -0700 Subject: [PATCH] Prefer analyze command file system instance if available. (#2700) * Prefer analyze command file system instance if available. * Dump event fixes. Make certain properties configurable via XML for driving analysis. --- src/Sarif.Driver/DumpEventsCommand.cs | 2 +- .../Sdk/MultithreadedAnalyzeCommandBase.cs | 10 +++---- .../ValidateCommand.cs | 2 -- src/Sarif/AnalyzeContextBase.cs | 26 +++++++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Sarif.Driver/DumpEventsCommand.cs b/src/Sarif.Driver/DumpEventsCommand.cs index ac2f36c56..42558a188 100644 --- a/src/Sarif.Driver/DumpEventsCommand.cs +++ b/src/Sarif.Driver/DumpEventsCommand.cs @@ -373,7 +373,7 @@ public int Run(DumpEventsOptions options) private void DumpRulesFired(Dictionary> rulesFired) { var sortedRules = new SortedList>(); - int maxRuleNameLength = 0; + int maxRuleNameLength = 20; foreach (string ruleName in rulesFired.Keys) { diff --git a/src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs b/src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs index ee96bc4a4..220f86dc1 100644 --- a/src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs +++ b/src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs @@ -89,7 +89,7 @@ public virtual int Run(TOptions options, ref TContext globalContext) if (globalContext.EventsFilePath.Equals("console", StringComparison.OrdinalIgnoreCase)) { globalContext.TraceEventSession = new TraceEventSession($"Sarif-Driver-{Guid.NewGuid()}"); - globalContext.TraceEventSession.BufferSizeMB = 2096; + globalContext.TraceEventSession.BufferSizeMB = globalContext.EventsBufferSizeInMegabytes; TraceEventSession traceEventSession = globalContext.TraceEventSession; globalContext.TraceEventSession.Source.Dynamic.All += (e => { @@ -111,7 +111,7 @@ public virtual int Run(TOptions options, ref TContext globalContext) : globalContext.EventsFilePath; globalContext.TraceEventSession = new TraceEventSession($"Sarif-Driver-{Guid.NewGuid()}", etlFilePath); - globalContext.TraceEventSession.BufferSizeMB = 2096; + globalContext.TraceEventSession.BufferSizeMB = globalContext.EventsBufferSizeInMegabytes; globalContext.TraceEventSession.EnableProvider(guid); } } @@ -268,7 +268,7 @@ private int Run(TContext globalContext) public virtual TContext InitializeGlobalContextFromOptions(TOptions options, ref TContext context) { context ??= new TContext(); - context.FileSystem ??= Sarif.FileSystem.Instance; + context.FileSystem ??= this.FileSystem ?? Sarif.FileSystem.Instance; context.Quiet = options.Quiet != null ? options.Quiet.Value : context.Quiet; @@ -402,14 +402,14 @@ private void MultithreadedAnalyzeTargets(TContext globalContext, ISet disabledSkimmers) { globalContext.CancellationToken.ThrowIfCancellationRequested(); - var channelOptions = new BoundedChannelOptions(50000) + var channelOptions = new BoundedChannelOptions(globalContext.ChannelSize) { SingleWriter = true, SingleReader = false, }; readyToScanChannel = Channel.CreateBounded(channelOptions); - channelOptions = new BoundedChannelOptions(50000) + channelOptions = new BoundedChannelOptions(globalContext.ChannelSize) { SingleWriter = false, SingleReader = true, diff --git a/src/Sarif.Multitool.Library/ValidateCommand.cs b/src/Sarif.Multitool.Library/ValidateCommand.cs index 624468a7f..1bede8e0d 100644 --- a/src/Sarif.Multitool.Library/ValidateCommand.cs +++ b/src/Sarif.Multitool.Library/ValidateCommand.cs @@ -21,8 +21,6 @@ public class ValidateCommand : MultithreadedAnalyzeCommandBase _defaultPlugInAssemblies; - protected override IFileSystem FileSystem => throw new InvalidOperationException(); - public ValidateCommand(IFileSystem fileSystem = null) : base(fileSystem) { } diff --git a/src/Sarif/AnalyzeContextBase.cs b/src/Sarif/AnalyzeContextBase.cs index ab2beb0b1..a2dc87463 100644 --- a/src/Sarif/AnalyzeContextBase.cs +++ b/src/Sarif/AnalyzeContextBase.cs @@ -28,12 +28,14 @@ public virtual IEnumerable GetOptions() AutomationGuidProperty, AutomationIdProperty, BaselineFilePathProperty, + ChannelSizeProperty, DataToInsertProperty, DataToRemoveProperty, EventsFilePathProperty, FailureLevelsProperty, GlobalFilePathDenyRegexProperty, MaxFileSizeInKilobytesProperty, + EventsBufferSizeInMegabytesProperty, OutputFileOptionsProperty, OutputFilePathProperty, PluginFilePathsProperty, @@ -104,6 +106,12 @@ public virtual string PostUri set => this.Policy.SetProperty(PostUriProperty, value); } + public virtual int ChannelSize + { + get => this.Policy.GetProperty(ChannelSizeProperty); + set => this.Policy.SetProperty(ChannelSizeProperty, value); + } + public virtual Guid? AutomationGuid { get => this.Policy.GetProperty(AutomationGuidProperty); @@ -220,6 +228,12 @@ public long MaxFileSizeInKilobytes set => this.Policy.SetProperty(MaxFileSizeInKilobytesProperty, value >= 0 ? value : MaxFileSizeInKilobytesProperty.DefaultValue()); } + public int EventsBufferSizeInMegabytes + { + get => this.Policy.GetProperty(EventsBufferSizeInMegabytesProperty); + set => this.Policy.SetProperty(EventsBufferSizeInMegabytesProperty, value >= 0 ? value : EventsBufferSizeInMegabytesProperty.DefaultValue()); + } + public virtual void Dispose() { var disposableLogger = this.Logger as IDisposable; @@ -253,6 +267,11 @@ public virtual void Dispose() GC.SuppressFinalize(this); } + public static PerLanguageOption ChannelSizeProperty { get; } = + new PerLanguageOption( + "CoreSettings", nameof(ChannelSize), defaultValue: () => 50000, + "The capacity of the channels for analyzing scan targets and logging results."); + public static PerLanguageOption AutomationGuidProperty { get; } = new PerLanguageOption( "CoreSettings", nameof(AutomationGuid), defaultValue: () => null, @@ -357,6 +376,13 @@ public virtual void Dispose() $" records what scan targets would have been analyzed, given current configuration.{Environment.NewLine}" + $" Negative values will be discarded in favor of the default of {MaxFileSizeInKilobytesProperty?.DefaultValue() ?? 1024} KB."); + + public static PerLanguageOption EventsBufferSizeInMegabytesProperty { get; } = + new PerLanguageOption( + $"CoreSettings", nameof(EventsBufferSizeInMegabytes), defaultValue: () => 512, + $"{Environment.NewLine}" + + $" A buffer size, in megabytes, passed to the events trace session instance when '--etw is enabled."); + public static PerLanguageOption TimeoutInMillisecondsProperty { get; } = new PerLanguageOption( "CoreSettings", nameof(TimeoutInMilliseconds), defaultValue: () => int.MaxValue,