Skip to content

Commit

Permalink
Prefer analyze command file system instance if available. (#2700)
Browse files Browse the repository at this point in the history
* Prefer analyze command file system instance if available.

* Dump event fixes. Make certain properties configurable via XML for driving analysis.
  • Loading branch information
michaelcfanning authored Jul 27, 2023
1 parent d9b7272 commit adb6f62
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Sarif.Driver/DumpEventsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public int Run(DumpEventsOptions options)
private void DumpRulesFired(Dictionary<string, Dictionary<FailureLevel, int>> rulesFired)
{
var sortedRules = new SortedList<string, Dictionary<FailureLevel, int>>();
int maxRuleNameLength = 0;
int maxRuleNameLength = 20;

foreach (string ruleName in rulesFired.Keys)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -402,14 +402,14 @@ private void MultithreadedAnalyzeTargets(TContext globalContext,
ISet<string> disabledSkimmers)
{
globalContext.CancellationToken.ThrowIfCancellationRequested();
var channelOptions = new BoundedChannelOptions(50000)
var channelOptions = new BoundedChannelOptions(globalContext.ChannelSize)
{
SingleWriter = true,
SingleReader = false,
};
readyToScanChannel = Channel.CreateBounded<uint>(channelOptions);

channelOptions = new BoundedChannelOptions(50000)
channelOptions = new BoundedChannelOptions(globalContext.ChannelSize)
{
SingleWriter = false,
SingleReader = true,
Expand Down
2 changes: 0 additions & 2 deletions src/Sarif.Multitool.Library/ValidateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public class ValidateCommand : MultithreadedAnalyzeCommandBase<SarifValidationCo
{
private List<Assembly> _defaultPlugInAssemblies;

protected override IFileSystem FileSystem => throw new InvalidOperationException();

public ValidateCommand(IFileSystem fileSystem = null) : base(fileSystem)
{
}
Expand Down
26 changes: 26 additions & 0 deletions src/Sarif/AnalyzeContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public virtual IEnumerable<IOption> GetOptions()
AutomationGuidProperty,
AutomationIdProperty,
BaselineFilePathProperty,
ChannelSizeProperty,
DataToInsertProperty,
DataToRemoveProperty,
EventsFilePathProperty,
FailureLevelsProperty,
GlobalFilePathDenyRegexProperty,
MaxFileSizeInKilobytesProperty,
EventsBufferSizeInMegabytesProperty,
OutputFileOptionsProperty,
OutputFilePathProperty,
PluginFilePathsProperty,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -253,6 +267,11 @@ public virtual void Dispose()
GC.SuppressFinalize(this);
}

public static PerLanguageOption<int> ChannelSizeProperty { get; } =
new PerLanguageOption<int>(
"CoreSettings", nameof(ChannelSize), defaultValue: () => 50000,
"The capacity of the channels for analyzing scan targets and logging results.");

public static PerLanguageOption<Guid?> AutomationGuidProperty { get; } =
new PerLanguageOption<Guid?>(
"CoreSettings", nameof(AutomationGuid), defaultValue: () => null,
Expand Down Expand Up @@ -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<int> EventsBufferSizeInMegabytesProperty { get; } =
new PerLanguageOption<int>(
$"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<int> TimeoutInMillisecondsProperty { get; } =
new PerLanguageOption<int>(
"CoreSettings", nameof(TimeoutInMilliseconds), defaultValue: () => int.MaxValue,
Expand Down

0 comments on commit adb6f62

Please sign in to comment.